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