]>
Commit | Line | Data |
---|---|---|
1802d0be | 1 | // SPDX-License-Identifier: GPL-2.0-only |
ff69c75e SG |
2 | /* |
3 | * Copyright (C) 2015 Jakub Kicinski <kubakici@wp.pl> | |
ff69c75e SG |
4 | */ |
5 | ||
6 | #include <linux/kernel.h> | |
7 | #include <linux/module.h> | |
8 | #include <linux/usb.h> | |
9 | ||
10 | #include "mt76x0.h" | |
576ecf65 | 11 | #include "mcu.h" |
905db747 | 12 | #include "../mt76x02_usb.h" |
ff69c75e SG |
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 */ | |
27c7bfc5 | 35 | { USB_DEVICE(0x2357, 0x0123) }, /* TP-LINK T2UHP */ |
ff97c52a RL |
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)}, | |
ff69c75e SG |
42 | { 0, } |
43 | }; | |
44 | ||
b2d871c0 | 45 | static void mt76x0_init_usb_dma(struct mt76x02_dev *dev) |
faa605bd LB |
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 | ||
b2d871c0 | 72 | static void mt76x0u_cleanup(struct mt76x02_dev *dev) |
faa605bd LB |
73 | { |
74 | clear_bit(MT76_STATE_INITIALIZED, &dev->mt76.state); | |
75 | mt76x0_chip_onoff(dev, false, false); | |
76 | mt76u_queues_deinit(&dev->mt76); | |
faa605bd LB |
77 | } |
78 | ||
091a79fd | 79 | static void mt76x0u_stop(struct ieee80211_hw *hw) |
b11e1969 | 80 | { |
091a79fd SG |
81 | struct mt76x02_dev *dev = hw->priv; |
82 | ||
b11e1969 LB |
83 | clear_bit(MT76_STATE_RUNNING, &dev->mt76.state); |
84 | cancel_delayed_work_sync(&dev->cal_work); | |
37426fb6 | 85 | cancel_delayed_work_sync(&dev->mt76.mac_work); |
39d501d9 | 86 | mt76u_stop_tx(&dev->mt76); |
c6ad1feb | 87 | mt76x02u_exit_beacon_config(dev); |
b11e1969 | 88 | |
14190227 LB |
89 | if (test_bit(MT76_REMOVED, &dev->mt76.state)) |
90 | return; | |
91 | ||
b11e1969 LB |
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 | { | |
b2d871c0 | 103 | struct mt76x02_dev *dev = hw->priv; |
b11e1969 LB |
104 | int ret; |
105 | ||
fdb96b06 | 106 | ret = mt76x02u_mac_start(dev); |
b11e1969 | 107 | if (ret) |
091a79fd | 108 | return ret; |
b11e1969 | 109 | |
1163bdb6 | 110 | mt76x0_phy_calibrate(dev, true); |
37426fb6 | 111 | ieee80211_queue_delayed_work(dev->mt76.hw, &dev->mt76.mac_work, |
2e405024 | 112 | MT_MAC_WORK_INTERVAL); |
b11e1969 LB |
113 | ieee80211_queue_delayed_work(dev->mt76.hw, &dev->cal_work, |
114 | MT_CALIBRATE_INTERVAL); | |
115 | set_bit(MT76_STATE_RUNNING, &dev->mt76.state); | |
091a79fd | 116 | return 0; |
b11e1969 LB |
117 | } |
118 | ||
119 | static const struct ieee80211_ops mt76x0u_ops = { | |
2f0308d0 | 120 | .tx = mt76x02_tx, |
b11e1969 LB |
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, | |
cc726268 | 127 | .bss_info_changed = mt76x02_bss_info_changed, |
e28487ea | 128 | .sta_state = mt76_sta_state, |
b11e1969 LB |
129 | .set_key = mt76x02_set_key, |
130 | .conf_tx = mt76x02_conf_tx, | |
8b8ab5c2 | 131 | .sw_scan_start = mt76_sw_scan, |
c2756a1c | 132 | .sw_scan_complete = mt76x02_sw_scan_complete, |
b11e1969 LB |
133 | .ampdu_action = mt76x02_ampdu_action, |
134 | .sta_rate_tbl_update = mt76x02_sta_rate_tbl_update, | |
317ed42b | 135 | .set_rts_threshold = mt76x02_set_rts_threshold, |
b11e1969 | 136 | .wake_tx_queue = mt76_wake_tx_queue, |
9313faac | 137 | .get_txpower = mt76_get_txpower, |
168aea24 | 138 | .get_survey = mt76_get_survey, |
87d53103 | 139 | .set_tim = mt76_set_tim, |
8300ee7c | 140 | .release_buffered_frames = mt76_release_buffered_frames, |
e49c76d4 | 141 | .get_antenna = mt76_get_antenna, |
b11e1969 LB |
142 | }; |
143 | ||
8f2d163c | 144 | static int mt76x0u_init_hardware(struct mt76x02_dev *dev, bool reset) |
faa605bd | 145 | { |
faa605bd LB |
146 | int err; |
147 | ||
8f2d163c | 148 | mt76x0_chip_onoff(dev, true, reset); |
d04ca383 SG |
149 | |
150 | if (!mt76x02_wait_for_mac(&dev->mt76)) | |
151 | return -ETIMEDOUT; | |
faa605bd LB |
152 | |
153 | err = mt76x0u_mcu_init(dev); | |
154 | if (err < 0) | |
d04ca383 | 155 | return err; |
faa605bd LB |
156 | |
157 | mt76x0_init_usb_dma(dev); | |
158 | err = mt76x0_init_hardware(dev); | |
159 | if (err < 0) | |
d04ca383 | 160 | return err; |
faa605bd | 161 | |
8d71aef9 SG |
162 | mt76x02u_init_beacon_config(dev); |
163 | ||
faa605bd LB |
164 | mt76_rmw(dev, MT_US_CYC_CFG, MT_US_CYC_CNT, 0x1e); |
165 | mt76_wr(dev, MT_TXOP_CTRL_CFG, | |
166 | FIELD_PREP(MT_TXOP_TRUN_EN, 0x3f) | | |
167 | FIELD_PREP(MT_TXOP_EXT_CCA_DLY, 0x58)); | |
168 | ||
d04ca383 SG |
169 | return 0; |
170 | } | |
171 | ||
172 | static int mt76x0u_register_device(struct mt76x02_dev *dev) | |
173 | { | |
174 | struct ieee80211_hw *hw = dev->mt76.hw; | |
175 | int err; | |
176 | ||
177 | err = mt76u_alloc_queues(&dev->mt76); | |
178 | if (err < 0) | |
179 | goto out_err; | |
180 | ||
8f2d163c | 181 | err = mt76x0u_init_hardware(dev, true); |
d04ca383 SG |
182 | if (err < 0) |
183 | goto out_err; | |
184 | ||
faa605bd LB |
185 | err = mt76x0_register_device(dev); |
186 | if (err < 0) | |
0536478c | 187 | goto out_err; |
faa605bd LB |
188 | |
189 | /* check hw sg support in order to enable AMSDU */ | |
63a7de5d | 190 | if (dev->mt76.usb.sg_en) |
14663f0c | 191 | hw->max_tx_fragments = MT_TX_SG_MAX_SIZE; |
faa605bd LB |
192 | else |
193 | hw->max_tx_fragments = 1; | |
194 | ||
195 | set_bit(MT76_STATE_INITIALIZED, &dev->mt76.state); | |
196 | ||
197 | return 0; | |
198 | ||
0536478c | 199 | out_err: |
faa605bd LB |
200 | mt76x0u_cleanup(dev); |
201 | return err; | |
202 | } | |
203 | ||
6d1bced1 | 204 | static int mt76x0u_probe(struct usb_interface *usb_intf, |
ff69c75e SG |
205 | const struct usb_device_id *id) |
206 | { | |
835123b7 | 207 | static const struct mt76_driver_ops drv_ops = { |
5ce09c1a | 208 | .drv_flags = MT_DRV_SW_RX_AIRTIME, |
355f8d00 | 209 | .survey_flags = SURVEY_INFO_TIME_TX, |
168aea24 | 210 | .update_survey = mt76x02_update_channel, |
c4ed5088 | 211 | .tx_prepare_skb = mt76x02u_tx_prepare_skb, |
b38383e6 | 212 | .tx_complete_skb = mt76x02u_tx_complete_skb, |
835123b7 | 213 | .tx_status_data = mt76x02_tx_status_data, |
9ba1e0e6 | 214 | .rx_skb = mt76x02_queue_rx_skb, |
2baed5db | 215 | .sta_ps = mt76x02_sta_ps, |
e28487ea FF |
216 | .sta_add = mt76x02_sta_add, |
217 | .sta_remove = mt76x02_sta_remove, | |
835123b7 | 218 | }; |
ff69c75e | 219 | struct usb_device *usb_dev = interface_to_usbdev(usb_intf); |
b2d871c0 | 220 | struct mt76x02_dev *dev; |
fa235526 | 221 | struct mt76_dev *mdev; |
40b94161 | 222 | u32 mac_rev; |
ff69c75e | 223 | int ret; |
ff69c75e | 224 | |
80df01f4 | 225 | mdev = mt76_alloc_device(&usb_intf->dev, sizeof(*dev), &mt76x0u_ops, |
fa235526 LB |
226 | &drv_ops); |
227 | if (!mdev) | |
ff69c75e SG |
228 | return -ENOMEM; |
229 | ||
fa235526 LB |
230 | dev = container_of(mdev, struct mt76x02_dev, mt76); |
231 | mutex_init(&dev->phy_mutex); | |
232 | ||
d6500cf3 SG |
233 | /* Quirk for Archer T1U */ |
234 | if (id->driver_info) | |
235 | dev->no_2ghz = true; | |
236 | ||
ff69c75e SG |
237 | usb_dev = usb_get_dev(usb_dev); |
238 | usb_reset_device(usb_dev); | |
239 | ||
240 | usb_set_intfdata(usb_intf, dev); | |
241 | ||
fa235526 LB |
242 | mt76x02u_init_mcu(mdev); |
243 | ret = mt76u_init(mdev, usb_intf); | |
ff69c75e SG |
244 | if (ret) |
245 | goto err; | |
369bbecd | 246 | |
ff97c52a | 247 | /* Disable the HW, otherwise MCU fail to initialize on hot reboot */ |
369bbecd SG |
248 | mt76x0_chip_onoff(dev, false, false); |
249 | ||
fa235526 | 250 | if (!mt76x02_wait_for_mac(mdev)) { |
2735a6dd | 251 | ret = -ETIMEDOUT; |
ff69c75e | 252 | goto err; |
2735a6dd | 253 | } |
ff69c75e | 254 | |
40b94161 | 255 | mdev->rev = mt76_rr(dev, MT_ASIC_VERSION); |
ff69c75e | 256 | mac_rev = mt76_rr(dev, MT_MAC_CSR0); |
fa235526 | 257 | dev_info(mdev->dev, "ASIC revision: %08x MAC revision: %08x\n", |
40b94161 SG |
258 | mdev->rev, mac_rev); |
259 | if (!is_mt76x0(dev)) { | |
260 | ret = -ENODEV; | |
261 | goto err; | |
262 | } | |
ff69c75e SG |
263 | |
264 | /* Note: vendor driver skips this check for MT76X0U */ | |
265 | if (!(mt76_rr(dev, MT_EFUSE_CTRL) & MT_EFUSE_CTRL_SEL)) | |
fa235526 | 266 | dev_warn(mdev->dev, "Warning: eFUSE not present\n"); |
ff69c75e | 267 | |
faa605bd | 268 | ret = mt76x0u_register_device(dev); |
6d1bced1 SG |
269 | if (ret < 0) |
270 | goto err; | |
271 | ||
ff69c75e | 272 | return 0; |
faa605bd | 273 | |
ff69c75e SG |
274 | err: |
275 | usb_set_intfdata(usb_intf, NULL); | |
276 | usb_put_dev(interface_to_usbdev(usb_intf)); | |
284efb47 | 277 | mt76u_deinit(&dev->mt76); |
ff69c75e | 278 | |
fa235526 | 279 | ieee80211_free_hw(mdev->hw); |
ff69c75e SG |
280 | return ret; |
281 | } | |
282 | ||
283 | static void mt76x0_disconnect(struct usb_interface *usb_intf) | |
284 | { | |
b2d871c0 | 285 | struct mt76x02_dev *dev = usb_get_intfdata(usb_intf); |
ff97c52a | 286 | bool initialized = test_bit(MT76_STATE_INITIALIZED, &dev->mt76.state); |
ff69c75e | 287 | |
ff97c52a | 288 | if (!initialized) |
ff69c75e SG |
289 | return; |
290 | ||
291 | ieee80211_unregister_hw(dev->mt76.hw); | |
faa605bd | 292 | mt76x0u_cleanup(dev); |
ff69c75e SG |
293 | |
294 | usb_set_intfdata(usb_intf, NULL); | |
295 | usb_put_dev(interface_to_usbdev(usb_intf)); | |
296 | ||
284efb47 | 297 | mt76u_deinit(&dev->mt76); |
ff69c75e SG |
298 | ieee80211_free_hw(dev->mt76.hw); |
299 | } | |
300 | ||
6f15ed16 LB |
301 | static int __maybe_unused mt76x0_suspend(struct usb_interface *usb_intf, |
302 | pm_message_t state) | |
ff69c75e | 303 | { |
b2d871c0 | 304 | struct mt76x02_dev *dev = usb_get_intfdata(usb_intf); |
ff69c75e | 305 | |
39d501d9 | 306 | mt76u_stop_rx(&dev->mt76); |
d04ca383 SG |
307 | clear_bit(MT76_STATE_MCU_RUNNING, &dev->mt76.state); |
308 | mt76x0_chip_onoff(dev, false, false); | |
ff69c75e SG |
309 | |
310 | return 0; | |
311 | } | |
312 | ||
6f15ed16 | 313 | static int __maybe_unused mt76x0_resume(struct usb_interface *usb_intf) |
ff69c75e | 314 | { |
b2d871c0 | 315 | struct mt76x02_dev *dev = usb_get_intfdata(usb_intf); |
ff69c75e SG |
316 | int ret; |
317 | ||
39d501d9 | 318 | ret = mt76u_resume_rx(&dev->mt76); |
6f15ed16 LB |
319 | if (ret < 0) |
320 | goto err; | |
321 | ||
8f2d163c | 322 | ret = mt76x0u_init_hardware(dev, false); |
6f15ed16 LB |
323 | if (ret) |
324 | goto err; | |
ff69c75e SG |
325 | |
326 | return 0; | |
6f15ed16 | 327 | err: |
faa605bd | 328 | mt76x0u_cleanup(dev); |
6f15ed16 | 329 | return ret; |
ff69c75e SG |
330 | } |
331 | ||
332 | MODULE_DEVICE_TABLE(usb, mt76x0_device_table); | |
f4730140 LB |
333 | MODULE_FIRMWARE(MT7610E_FIRMWARE); |
334 | MODULE_FIRMWARE(MT7610U_FIRMWARE); | |
ff69c75e SG |
335 | MODULE_LICENSE("GPL"); |
336 | ||
337 | static struct usb_driver mt76x0_driver = { | |
338 | .name = KBUILD_MODNAME, | |
339 | .id_table = mt76x0_device_table, | |
6d1bced1 | 340 | .probe = mt76x0u_probe, |
ff69c75e SG |
341 | .disconnect = mt76x0_disconnect, |
342 | .suspend = mt76x0_suspend, | |
343 | .resume = mt76x0_resume, | |
344 | .reset_resume = mt76x0_resume, | |
345 | .soft_unbind = 1, | |
346 | .disable_hub_initiated_lpm = 1, | |
347 | }; | |
348 | module_usb_driver(mt76x0_driver); |