1 /*
2  * Copyright (C) 2015 Jakub Kicinski <kubakici@wp.pl>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2
6  * as published by the Free Software Foundation
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  */
13 
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/usb.h>
17 
18 #include "mt76x0.h"
19 #include "mcu.h"
20 #include "../mt76x02_usb.h"
21 
22 static struct usb_device_id mt76x0_device_table[] = {
23 	{ USB_DEVICE(0x148F, 0x7610) },	/* MT7610U */
24 	{ USB_DEVICE(0x13B1, 0x003E) },	/* Linksys AE6000 */
25 	{ USB_DEVICE(0x0E8D, 0x7610) },	/* Sabrent NTWLAC */
26 	{ USB_DEVICE(0x7392, 0xa711) },	/* Edimax 7711mac */
27 	{ USB_DEVICE(0x7392, 0xb711) },	/* Edimax / Elecom  */
28 	{ USB_DEVICE(0x148f, 0x761a) },	/* TP-Link TL-WDN5200 */
29 	{ USB_DEVICE(0x148f, 0x760a) },	/* TP-Link unknown */
30 	{ USB_DEVICE(0x0b05, 0x17d1) },	/* Asus USB-AC51 */
31 	{ USB_DEVICE(0x0b05, 0x17db) },	/* Asus USB-AC50 */
32 	{ USB_DEVICE(0x0df6, 0x0075) },	/* Sitecom WLA-3100 */
33 	{ USB_DEVICE(0x2019, 0xab31) },	/* Planex GW-450D */
34 	{ USB_DEVICE(0x2001, 0x3d02) },	/* D-LINK DWA-171 rev B1 */
35 	{ USB_DEVICE(0x0586, 0x3425) },	/* Zyxel NWD6505 */
36 	{ USB_DEVICE(0x07b8, 0x7610) },	/* AboCom AU7212 */
37 	{ USB_DEVICE(0x04bb, 0x0951) },	/* I-O DATA WN-AC433UK */
38 	{ USB_DEVICE(0x057c, 0x8502) },	/* AVM FRITZ!WLAN USB Stick AC 430 */
39 	{ USB_DEVICE(0x293c, 0x5702) },	/* Comcast Xfinity KXW02AAA  */
40 	{ USB_DEVICE(0x20f4, 0x806b) },	/* TRENDnet TEW-806UBH  */
41 	{ USB_DEVICE(0x7392, 0xc711) }, /* Devolo Wifi ac Stick */
42 	{ USB_DEVICE(0x0df6, 0x0079) }, /* Sitecom Europe B.V. ac  Stick */
43 	{ USB_DEVICE(0x2357, 0x0105),
44 	  .driver_info = 1,	     }, /* TP-LINK Archer T1U */
45 	{ USB_DEVICE_AND_INTERFACE_INFO(0x0E8D, 0x7630, 0xff, 0x2, 0xff)}, /* MT7630U */
46 	{ USB_DEVICE_AND_INTERFACE_INFO(0x0E8D, 0x7650, 0xff, 0x2, 0xff)}, /* MT7650U */
47 	{ 0, }
48 };
49 
50 static void mt76x0_init_usb_dma(struct mt76x02_dev *dev)
51 {
52 	u32 val;
53 
54 	val = mt76_rr(dev, MT_USB_DMA_CFG);
55 
56 	val |= MT_USB_DMA_CFG_RX_BULK_EN |
57 	       MT_USB_DMA_CFG_TX_BULK_EN;
58 
59 	/* disable AGGR_BULK_RX in order to receive one
60 	 * frame in each rx urb and avoid copies
61 	 */
62 	val &= ~MT_USB_DMA_CFG_RX_BULK_AGG_EN;
63 	mt76_wr(dev, MT_USB_DMA_CFG, val);
64 
65 	val = mt76_rr(dev, MT_COM_REG0);
66 	if (val & 1)
67 		dev_dbg(dev->mt76.dev, "MCU not ready\n");
68 
69 	val = mt76_rr(dev, MT_USB_DMA_CFG);
70 
71 	val |= MT_USB_DMA_CFG_RX_DROP_OR_PAD;
72 	mt76_wr(dev, MT_USB_DMA_CFG, val);
73 	val &= ~MT_USB_DMA_CFG_RX_DROP_OR_PAD;
74 	mt76_wr(dev, MT_USB_DMA_CFG, val);
75 }
76 
77 static void mt76x0u_cleanup(struct mt76x02_dev *dev)
78 {
79 	clear_bit(MT76_STATE_INITIALIZED, &dev->mt76.state);
80 	mt76x0_chip_onoff(dev, false, false);
81 	mt76u_queues_deinit(&dev->mt76);
82 }
83 
84 static void mt76x0u_stop(struct ieee80211_hw *hw)
85 {
86 	struct mt76x02_dev *dev = hw->priv;
87 
88 	clear_bit(MT76_STATE_RUNNING, &dev->mt76.state);
89 	cancel_delayed_work_sync(&dev->cal_work);
90 	cancel_delayed_work_sync(&dev->mt76.mac_work);
91 	mt76u_stop_tx(&dev->mt76);
92 	mt76x02u_exit_beacon_config(dev);
93 
94 	if (test_bit(MT76_REMOVED, &dev->mt76.state))
95 		return;
96 
97 	if (!mt76_poll(dev, MT_USB_DMA_CFG, MT_USB_DMA_CFG_TX_BUSY, 0, 1000))
98 		dev_warn(dev->mt76.dev, "TX DMA did not stop\n");
99 
100 	mt76x0_mac_stop(dev);
101 
102 	if (!mt76_poll(dev, MT_USB_DMA_CFG, MT_USB_DMA_CFG_RX_BUSY, 0, 1000))
103 		dev_warn(dev->mt76.dev, "RX DMA did not stop\n");
104 }
105 
106 static int mt76x0u_start(struct ieee80211_hw *hw)
107 {
108 	struct mt76x02_dev *dev = hw->priv;
109 	int ret;
110 
111 	ret = mt76x0_mac_start(dev);
112 	if (ret)
113 		return ret;
114 
115 	mt76x0_phy_calibrate(dev, true);
116 	ieee80211_queue_delayed_work(dev->mt76.hw, &dev->mt76.mac_work,
117 				     MT_MAC_WORK_INTERVAL);
118 	ieee80211_queue_delayed_work(dev->mt76.hw, &dev->cal_work,
119 				     MT_CALIBRATE_INTERVAL);
120 	set_bit(MT76_STATE_RUNNING, &dev->mt76.state);
121 	return 0;
122 }
123 
124 static const struct ieee80211_ops mt76x0u_ops = {
125 	.tx = mt76x02_tx,
126 	.start = mt76x0u_start,
127 	.stop = mt76x0u_stop,
128 	.add_interface = mt76x02_add_interface,
129 	.remove_interface = mt76x02_remove_interface,
130 	.config = mt76x0_config,
131 	.configure_filter = mt76x02_configure_filter,
132 	.bss_info_changed = mt76x02_bss_info_changed,
133 	.sta_state = mt76_sta_state,
134 	.set_key = mt76x02_set_key,
135 	.conf_tx = mt76x02_conf_tx,
136 	.sw_scan_start = mt76x02_sw_scan,
137 	.sw_scan_complete = mt76x02_sw_scan_complete,
138 	.ampdu_action = mt76x02_ampdu_action,
139 	.sta_rate_tbl_update = mt76x02_sta_rate_tbl_update,
140 	.set_rts_threshold = mt76x02_set_rts_threshold,
141 	.wake_tx_queue = mt76_wake_tx_queue,
142 	.get_txpower = mt76_get_txpower,
143 	.set_tim = mt76_set_tim,
144 	.release_buffered_frames = mt76_release_buffered_frames,
145 };
146 
147 static int mt76x0u_init_hardware(struct mt76x02_dev *dev)
148 {
149 	int err;
150 
151 	mt76x0_chip_onoff(dev, true, true);
152 
153 	if (!mt76x02_wait_for_mac(&dev->mt76))
154 		return -ETIMEDOUT;
155 
156 	err = mt76x0u_mcu_init(dev);
157 	if (err < 0)
158 		return err;
159 
160 	mt76x0_init_usb_dma(dev);
161 	err = mt76x0_init_hardware(dev);
162 	if (err < 0)
163 		return err;
164 
165 	mt76x02u_init_beacon_config(dev);
166 
167 	mt76_rmw(dev, MT_US_CYC_CFG, MT_US_CYC_CNT, 0x1e);
168 	mt76_wr(dev, MT_TXOP_CTRL_CFG,
169 		FIELD_PREP(MT_TXOP_TRUN_EN, 0x3f) |
170 		FIELD_PREP(MT_TXOP_EXT_CCA_DLY, 0x58));
171 
172 	return 0;
173 }
174 
175 static int mt76x0u_register_device(struct mt76x02_dev *dev)
176 {
177 	struct ieee80211_hw *hw = dev->mt76.hw;
178 	int err;
179 
180 	err = mt76u_alloc_queues(&dev->mt76);
181 	if (err < 0)
182 		goto out_err;
183 
184 	err = mt76x0u_init_hardware(dev);
185 	if (err < 0)
186 		goto out_err;
187 
188 	err = mt76x0_register_device(dev);
189 	if (err < 0)
190 		goto out_err;
191 
192 	/* check hw sg support in order to enable AMSDU */
193 	if (dev->mt76.usb.sg_en)
194 		hw->max_tx_fragments = MT_SG_MAX_SIZE;
195 	else
196 		hw->max_tx_fragments = 1;
197 
198 	set_bit(MT76_STATE_INITIALIZED, &dev->mt76.state);
199 
200 	return 0;
201 
202 out_err:
203 	mt76x0u_cleanup(dev);
204 	return err;
205 }
206 
207 static int mt76x0u_probe(struct usb_interface *usb_intf,
208 			 const struct usb_device_id *id)
209 {
210 	static const struct mt76_driver_ops drv_ops = {
211 		.tx_prepare_skb = mt76x02u_tx_prepare_skb,
212 		.tx_complete_skb = mt76x02u_tx_complete_skb,
213 		.tx_status_data = mt76x02_tx_status_data,
214 		.rx_skb = mt76x02_queue_rx_skb,
215 		.sta_ps = mt76x02_sta_ps,
216 		.sta_add = mt76x02_sta_add,
217 		.sta_remove = mt76x02_sta_remove,
218 	};
219 	struct usb_device *usb_dev = interface_to_usbdev(usb_intf);
220 	struct mt76x02_dev *dev;
221 	struct mt76_dev *mdev;
222 	u32 mac_rev;
223 	int ret;
224 
225 	mdev = mt76_alloc_device(&usb_dev->dev, sizeof(*dev), &mt76x0u_ops,
226 				 &drv_ops);
227 	if (!mdev)
228 		return -ENOMEM;
229 
230 	dev = container_of(mdev, struct mt76x02_dev, mt76);
231 	mutex_init(&dev->phy_mutex);
232 
233 	/* Quirk for Archer T1U */
234 	if (id->driver_info)
235 		dev->no_2ghz = true;
236 
237 	usb_dev = usb_get_dev(usb_dev);
238 	usb_reset_device(usb_dev);
239 
240 	usb_set_intfdata(usb_intf, dev);
241 
242 	mt76x02u_init_mcu(mdev);
243 	ret = mt76u_init(mdev, usb_intf);
244 	if (ret)
245 		goto err;
246 
247 	/* Disable the HW, otherwise MCU fail to initalize on hot reboot */
248 	mt76x0_chip_onoff(dev, false, false);
249 
250 	if (!mt76x02_wait_for_mac(mdev)) {
251 		ret = -ETIMEDOUT;
252 		goto err;
253 	}
254 
255 	mdev->rev = mt76_rr(dev, MT_ASIC_VERSION);
256 	mac_rev = mt76_rr(dev, MT_MAC_CSR0);
257 	dev_info(mdev->dev, "ASIC revision: %08x MAC revision: %08x\n",
258 		 mdev->rev, mac_rev);
259 	if (!is_mt76x0(dev)) {
260 		ret = -ENODEV;
261 		goto err;
262 	}
263 
264 	/* Note: vendor driver skips this check for MT76X0U */
265 	if (!(mt76_rr(dev, MT_EFUSE_CTRL) & MT_EFUSE_CTRL_SEL))
266 		dev_warn(mdev->dev, "Warning: eFUSE not present\n");
267 
268 	ret = mt76x0u_register_device(dev);
269 	if (ret < 0)
270 		goto err;
271 
272 	return 0;
273 
274 err:
275 	usb_set_intfdata(usb_intf, NULL);
276 	usb_put_dev(interface_to_usbdev(usb_intf));
277 
278 	ieee80211_free_hw(mdev->hw);
279 	return ret;
280 }
281 
282 static void mt76x0_disconnect(struct usb_interface *usb_intf)
283 {
284 	struct mt76x02_dev *dev = usb_get_intfdata(usb_intf);
285 	bool initalized = test_bit(MT76_STATE_INITIALIZED, &dev->mt76.state);
286 
287 	if (!initalized)
288 		return;
289 
290 	ieee80211_unregister_hw(dev->mt76.hw);
291 	mt76x0u_cleanup(dev);
292 
293 	usb_set_intfdata(usb_intf, NULL);
294 	usb_put_dev(interface_to_usbdev(usb_intf));
295 
296 	ieee80211_free_hw(dev->mt76.hw);
297 }
298 
299 static int __maybe_unused mt76x0_suspend(struct usb_interface *usb_intf,
300 					 pm_message_t state)
301 {
302 	struct mt76x02_dev *dev = usb_get_intfdata(usb_intf);
303 
304 	mt76u_stop_rx(&dev->mt76);
305 	clear_bit(MT76_STATE_MCU_RUNNING, &dev->mt76.state);
306 	mt76x0_chip_onoff(dev, false, false);
307 
308 	return 0;
309 }
310 
311 static int __maybe_unused mt76x0_resume(struct usb_interface *usb_intf)
312 {
313 	struct mt76x02_dev *dev = usb_get_intfdata(usb_intf);
314 	int ret;
315 
316 	ret = mt76u_resume_rx(&dev->mt76);
317 	if (ret < 0)
318 		goto err;
319 
320 	ret = mt76x0u_init_hardware(dev);
321 	if (ret)
322 		goto err;
323 
324 	return 0;
325 err:
326 	mt76x0u_cleanup(dev);
327 	return ret;
328 }
329 
330 MODULE_DEVICE_TABLE(usb, mt76x0_device_table);
331 MODULE_FIRMWARE(MT7610E_FIRMWARE);
332 MODULE_FIRMWARE(MT7610U_FIRMWARE);
333 MODULE_LICENSE("GPL");
334 
335 static struct usb_driver mt76x0_driver = {
336 	.name		= KBUILD_MODNAME,
337 	.id_table	= mt76x0_device_table,
338 	.probe		= mt76x0u_probe,
339 	.disconnect	= mt76x0_disconnect,
340 	.suspend	= mt76x0_suspend,
341 	.resume		= mt76x0_resume,
342 	.reset_resume	= mt76x0_resume,
343 	.soft_unbind	= 1,
344 	.disable_hub_initiated_lpm = 1,
345 };
346 module_usb_driver(mt76x0_driver);
347