| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* |
| 3 |
* Copyright (C) 2015 Jakub Kicinski <[email protected]> |
| 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_US_v1 */ |
| 36 |
{ USB_DEVICE(0x2357, 0x010b) }, /* TP-LINK T2UHP_UN_v1 */ |
| 37 |
/* TP-LINK Archer T1U */ |
| 38 |
{ USB_DEVICE(0x2357, 0x0105), .driver_info = 1, }, |
| 39 |
/* MT7630U */ |
| 40 |
{ USB_DEVICE_AND_INTERFACE_INFO(0x0E8D, 0x7630, 0xff, 0x2, 0xff)}, |
| 41 |
/* MT7650U */ |
| 42 |
{ USB_DEVICE_AND_INTERFACE_INFO(0x0E8D, 0x7650, 0xff, 0x2, 0xff)}, |
| 43 |
{ 0, } |
| 44 |
}; |
| 45 |
|
| 46 |
static void mt76x0_init_usb_dma(struct mt76x02_dev *dev) |
| 47 |
{ |
| 48 |
u32 val; |
| 49 |
|
| 50 |
val = mt76_rr(dev, MT_USB_DMA_CFG); |
| 51 |
|
| 52 |
val |= MT_USB_DMA_CFG_RX_BULK_EN | |
| 53 |
MT_USB_DMA_CFG_TX_BULK_EN; |
| 54 |
|
| 55 |
/* disable AGGR_BULK_RX in order to receive one |
| 56 |
* frame in each rx urb and avoid copies |
| 57 |
*/ |
| 58 |
val &= ~MT_USB_DMA_CFG_RX_BULK_AGG_EN; |
| 59 |
mt76_wr(dev, MT_USB_DMA_CFG, val); |
| 60 |
|
| 61 |
val = mt76_rr(dev, MT_COM_REG0); |
| 62 |
if (val & 1) |
| 63 |
dev_dbg(dev->mt76.dev, "MCU not ready\n"); |
| 64 |
|
| 65 |
val = mt76_rr(dev, MT_USB_DMA_CFG); |
| 66 |
|
| 67 |
val |= MT_USB_DMA_CFG_RX_DROP_OR_PAD; |
| 68 |
mt76_wr(dev, MT_USB_DMA_CFG, val); |
| 69 |
val &= ~MT_USB_DMA_CFG_RX_DROP_OR_PAD; |
| 70 |
mt76_wr(dev, MT_USB_DMA_CFG, val); |
| 71 |
} |
| 72 |
|
| 73 |
static void mt76x0u_cleanup(struct mt76x02_dev *dev) |
| 74 |
{ |
| 75 |
clear_bit(MT76_STATE_INITIALIZED, &dev->mphy.state); |
| 76 |
mt76x0_chip_onoff(dev, false, false); |
| 77 |
mt76u_queues_deinit(&dev->mt76); |
| 78 |
} |
| 79 |
|
| 80 |
static void mt76x0u_stop(struct ieee80211_hw *hw, bool suspend) |
| 81 |
{ |
| 82 |
struct mt76x02_dev *dev = hw->priv; |
| 83 |
|
| 84 |
clear_bit(MT76_STATE_RUNNING, &dev->mphy.state); |
| 85 |
cancel_delayed_work_sync(&dev->cal_work); |
| 86 |
cancel_delayed_work_sync(&dev->mphy.mac_work); |
| 87 |
mt76u_stop_tx(&dev->mt76); |
| 88 |
mt76x02u_exit_beacon_config(dev); |
| 89 |
|
| 90 |
if (test_bit(MT76_REMOVED, &dev->mphy.state)) |
| 91 |
return; |
| 92 |
|
| 93 |
if (!mt76_poll(dev, MT_USB_DMA_CFG, MT_USB_DMA_CFG_TX_BUSY, 0, 1000)) |
| 94 |
dev_warn(dev->mt76.dev, "TX DMA did not stop\n"); |
| 95 |
|
| 96 |
mt76x0_mac_stop(dev); |
| 97 |
|
| 98 |
if (!mt76_poll(dev, MT_USB_DMA_CFG, MT_USB_DMA_CFG_RX_BUSY, 0, 1000)) |
| 99 |
dev_warn(dev->mt76.dev, "RX DMA did not stop\n"); |
| 100 |
} |
| 101 |
|
| 102 |
static int mt76x0u_start(struct ieee80211_hw *hw) |
| 103 |
{ |
| 104 |
struct mt76x02_dev *dev = hw->priv; |
| 105 |
int ret; |
| 106 |
|
| 107 |
ret = mt76x02u_mac_start(dev); |
| 108 |
if (ret) |
| 109 |
return ret; |
| 110 |
|
| 111 |
mt76x0_phy_calibrate(dev, true); |
| 112 |
ieee80211_queue_delayed_work(dev->mt76.hw, &dev->mphy.mac_work, |
| 113 |
MT_MAC_WORK_INTERVAL); |
| 114 |
ieee80211_queue_delayed_work(dev->mt76.hw, &dev->cal_work, |
| 115 |
MT_CALIBRATE_INTERVAL); |
| 116 |
set_bit(MT76_STATE_RUNNING, &dev->mphy.state); |
| 117 |
return 0; |
| 118 |
} |
| 119 |
|
| 120 |
static const struct ieee80211_ops mt76x0u_ops = { |
| 121 |
.add_chanctx = ieee80211_emulate_add_chanctx, |
| 122 |
.remove_chanctx = ieee80211_emulate_remove_chanctx, |
| 123 |
.change_chanctx = ieee80211_emulate_change_chanctx, |
| 124 |
.switch_vif_chanctx = ieee80211_emulate_switch_vif_chanctx, |
| 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 |
.sta_pre_rcu_remove = mt76_sta_pre_rcu_remove, |
| 135 |
.set_key = mt76x02_set_key, |
| 136 |
.conf_tx = mt76x02_conf_tx, |
| 137 |
.sw_scan_start = mt76_sw_scan, |
| 138 |
.sw_scan_complete = mt76x02_sw_scan_complete, |
| 139 |
.ampdu_action = mt76x02_ampdu_action, |
| 140 |
.sta_rate_tbl_update = mt76x02_sta_rate_tbl_update, |
| 141 |
.set_rts_threshold = mt76x02_set_rts_threshold, |
| 142 |
.wake_tx_queue = mt76_wake_tx_queue, |
| 143 |
.get_txpower = mt76_get_txpower, |
| 144 |
.get_survey = mt76_get_survey, |
| 145 |
.set_tim = mt76_set_tim, |
| 146 |
.release_buffered_frames = mt76_release_buffered_frames, |
| 147 |
.get_antenna = mt76_get_antenna, |
| 148 |
.set_sar_specs = mt76x0_set_sar_specs, |
| 149 |
}; |
| 150 |
|
| 151 |
static int mt76x0u_init_hardware(struct mt76x02_dev *dev, bool reset) |
| 152 |
{ |
| 153 |
int err; |
| 154 |
|
| 155 |
mt76x0_chip_onoff(dev, true, reset); |
| 156 |
|
| 157 |
if (!mt76x02_wait_for_mac(&dev->mt76)) |
| 158 |
return -ETIMEDOUT; |
| 159 |
|
| 160 |
err = mt76x0u_mcu_init(dev); |
| 161 |
if (err < 0) |
| 162 |
return err; |
| 163 |
|
| 164 |
mt76x0_init_usb_dma(dev); |
| 165 |
err = mt76x0_init_hardware(dev); |
| 166 |
if (err < 0) |
| 167 |
return err; |
| 168 |
|
| 169 |
mt76x02u_init_beacon_config(dev); |
| 170 |
|
| 171 |
mt76_rmw(dev, MT_US_CYC_CFG, MT_US_CYC_CNT, 0x1e); |
| 172 |
mt76_wr(dev, MT_TXOP_CTRL_CFG, |
| 173 |
FIELD_PREP(MT_TXOP_TRUN_EN, 0x3f) | |
| 174 |
FIELD_PREP(MT_TXOP_EXT_CCA_DLY, 0x58)); |
| 175 |
|
| 176 |
return 0; |
| 177 |
} |
| 178 |
|
| 179 |
static int mt76x0u_register_device(struct mt76x02_dev *dev) |
| 180 |
{ |
| 181 |
struct ieee80211_hw *hw = dev->mt76.hw; |
| 182 |
struct mt76_usb *usb = &dev->mt76.usb; |
| 183 |
int err; |
| 184 |
|
| 185 |
usb->mcu.data = devm_kmalloc(dev->mt76.dev, MCU_RESP_URB_SIZE, |
| 186 |
GFP_KERNEL); |
| 187 |
if (!usb->mcu.data) |
| 188 |
return -ENOMEM; |
| 189 |
|
| 190 |
err = mt76u_alloc_queues(&dev->mt76); |
| 191 |
if (err < 0) |
| 192 |
goto out_err; |
| 193 |
|
| 194 |
err = mt76x0u_init_hardware(dev, true); |
| 195 |
if (err < 0) |
| 196 |
goto out_err; |
| 197 |
|
| 198 |
/* check hw sg support in order to enable AMSDU */ |
| 199 |
hw->max_tx_fragments = dev->mt76.usb.sg_en ? MT_TX_SG_MAX_SIZE : 1; |
| 200 |
err = mt76x0_register_device(dev); |
| 201 |
if (err < 0) |
| 202 |
goto out_err; |
| 203 |
|
| 204 |
set_bit(MT76_STATE_INITIALIZED, &dev->mphy.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 |
.drv_flags = MT_DRV_SW_RX_AIRTIME | |
| 218 |
MT_DRV_IGNORE_TXS_FAILED, |
| 219 |
.survey_flags = SURVEY_INFO_TIME_TX, |
| 220 |
.update_survey = mt76x02_update_channel, |
| 221 |
.set_channel = mt76x0_set_channel, |
| 222 |
.tx_prepare_skb = mt76x02u_tx_prepare_skb, |
| 223 |
.tx_complete_skb = mt76x02u_tx_complete_skb, |
| 224 |
.tx_status_data = mt76x02_tx_status_data, |
| 225 |
.rx_skb = mt76x02_queue_rx_skb, |
| 226 |
.sta_ps = mt76x02_sta_ps, |
| 227 |
.sta_add = mt76x02_sta_add, |
| 228 |
.sta_remove = mt76x02_sta_remove, |
| 229 |
}; |
| 230 |
struct usb_device *usb_dev = interface_to_usbdev(usb_intf); |
| 231 |
struct mt76x02_dev *dev; |
| 232 |
struct mt76_dev *mdev; |
| 233 |
u32 mac_rev; |
| 234 |
int ret; |
| 235 |
|
| 236 |
mdev = mt76_alloc_device(&usb_intf->dev, sizeof(*dev), &mt76x0u_ops, |
| 237 |
&drv_ops); |
| 238 |
if (!mdev) |
| 239 |
return -ENOMEM; |
| 240 |
|
| 241 |
dev = container_of(mdev, struct mt76x02_dev, mt76); |
| 242 |
mutex_init(&dev->phy_mutex); |
| 243 |
|
| 244 |
/* Quirk for Archer T1U */ |
| 245 |
if (id->driver_info) |
| 246 |
dev->no_2ghz = true; |
| 247 |
|
| 248 |
usb_dev = usb_get_dev(usb_dev); |
| 249 |
usb_reset_device(usb_dev); |
| 250 |
|
| 251 |
usb_set_intfdata(usb_intf, dev); |
| 252 |
|
| 253 |
mt76x02u_init_mcu(mdev); |
| 254 |
ret = mt76u_init(mdev, usb_intf); |
| 255 |
if (ret) |
| 256 |
goto err; |
| 257 |
|
| 258 |
/* Disable the HW, otherwise MCU fail to initialize on hot reboot */ |
| 259 |
mt76x0_chip_onoff(dev, false, false); |
| 260 |
|
| 261 |
if (!mt76x02_wait_for_mac(mdev)) { |
| 262 |
ret = -ETIMEDOUT; |
| 263 |
goto err; |
| 264 |
} |
| 265 |
|
| 266 |
mdev->rev = mt76_rr(dev, MT_ASIC_VERSION); |
| 267 |
mac_rev = mt76_rr(dev, MT_MAC_CSR0); |
| 268 |
dev_info(mdev->dev, "ASIC revision: %08x MAC revision: %08x\n", |
| 269 |
mdev->rev, mac_rev); |
| 270 |
if (!is_mt76x0(dev)) { |
| 271 |
ret = -ENODEV; |
| 272 |
goto err; |
| 273 |
} |
| 274 |
|
| 275 |
/* Note: vendor driver skips this check for MT76X0U */ |
| 276 |
if (!(mt76_rr(dev, MT_EFUSE_CTRL) & MT_EFUSE_CTRL_SEL)) |
| 277 |
dev_warn(mdev->dev, "Warning: eFUSE not present\n"); |
| 278 |
|
| 279 |
ret = mt76x0u_register_device(dev); |
| 280 |
if (ret < 0) |
| 281 |
goto err; |
| 282 |
|
| 283 |
return 0; |
| 284 |
|
| 285 |
err: |
| 286 |
usb_set_intfdata(usb_intf, NULL); |
| 287 |
usb_put_dev(interface_to_usbdev(usb_intf)); |
| 288 |
mt76u_queues_deinit(&dev->mt76); |
| 289 |
mt76_free_device(&dev->mt76); |
| 290 |
|
| 291 |
return ret; |
| 292 |
} |
| 293 |
|
| 294 |
static void mt76x0_disconnect(struct usb_interface *usb_intf) |
| 295 |
{ |
| 296 |
struct mt76x02_dev *dev = usb_get_intfdata(usb_intf); |
| 297 |
bool initialized = test_bit(MT76_STATE_INITIALIZED, &dev->mphy.state); |
| 298 |
|
| 299 |
if (!initialized) |
| 300 |
return; |
| 301 |
|
| 302 |
ieee80211_unregister_hw(dev->mt76.hw); |
| 303 |
mt76x0u_cleanup(dev); |
| 304 |
|
| 305 |
usb_set_intfdata(usb_intf, NULL); |
| 306 |
usb_put_dev(interface_to_usbdev(usb_intf)); |
| 307 |
|
| 308 |
mt76_free_device(&dev->mt76); |
| 309 |
} |
| 310 |
|
| 311 |
static int __maybe_unused mt76x0_suspend(struct usb_interface *usb_intf, |
| 312 |
pm_message_t state) |
| 313 |
{ |
| 314 |
struct mt76x02_dev *dev = usb_get_intfdata(usb_intf); |
| 315 |
|
| 316 |
mt76u_stop_rx(&dev->mt76); |
| 317 |
clear_bit(MT76_STATE_MCU_RUNNING, &dev->mphy.state); |
| 318 |
mt76x0_chip_onoff(dev, false, false); |
| 319 |
|
| 320 |
return 0; |
| 321 |
} |
| 322 |
|
| 323 |
static int __maybe_unused mt76x0_resume(struct usb_interface *usb_intf) |
| 324 |
{ |
| 325 |
struct mt76x02_dev *dev = usb_get_intfdata(usb_intf); |
| 326 |
int ret; |
| 327 |
|
| 328 |
ret = mt76u_resume_rx(&dev->mt76); |
| 329 |
if (ret < 0) |
| 330 |
goto err; |
| 331 |
|
| 332 |
ret = mt76x0u_init_hardware(dev, false); |
| 333 |
if (ret) |
| 334 |
goto err; |
| 335 |
|
| 336 |
return 0; |
| 337 |
err: |
| 338 |
mt76x0u_cleanup(dev); |
| 339 |
return ret; |
| 340 |
} |
| 341 |
|
| 342 |
MODULE_DEVICE_TABLE(usb, mt76x0_device_table); |
| 343 |
MODULE_FIRMWARE(MT7610E_FIRMWARE); |
| 344 |
MODULE_FIRMWARE(MT7610U_FIRMWARE); |
| 345 |
MODULE_DESCRIPTION("MediaTek MT76x0U (USB) wireless driver"); |
| 346 |
MODULE_LICENSE("GPL"); |
| 347 |
|
| 348 |
static struct usb_driver mt76x0_driver = { |
| 349 |
.name = KBUILD_MODNAME, |
| 350 |
.id_table = mt76x0_device_table, |
| 351 |
.probe = mt76x0u_probe, |
| 352 |
.disconnect = mt76x0_disconnect, |
| 353 |
.suspend = mt76x0_suspend, |
| 354 |
.resume = mt76x0_resume, |
| 355 |
.reset_resume = mt76x0_resume, |
| 356 |
.soft_unbind = 1, |
| 357 |
.disable_hub_initiated_lpm = 1, |
| 358 |
}; |
| 359 |
module_usb_driver(mt76x0_driver); |