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