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_mac_stop(struct mt76x02_dev *dev) 85 { 86 clear_bit(MT76_STATE_RUNNING, &dev->mt76.state); 87 cancel_delayed_work_sync(&dev->cal_work); 88 cancel_delayed_work_sync(&dev->mac_work); 89 mt76u_stop_stat_wk(&dev->mt76); 90 91 if (test_bit(MT76_REMOVED, &dev->mt76.state)) 92 return; 93 94 mt76_clear(dev, MT_BEACON_TIME_CFG, MT_BEACON_TIME_CFG_TIMER_EN | 95 MT_BEACON_TIME_CFG_SYNC_MODE | MT_BEACON_TIME_CFG_TBTT_EN | 96 MT_BEACON_TIME_CFG_BEACON_TX); 97 98 if (!mt76_poll(dev, MT_USB_DMA_CFG, MT_USB_DMA_CFG_TX_BUSY, 0, 1000)) 99 dev_warn(dev->mt76.dev, "TX DMA did not stop\n"); 100 101 mt76x0_mac_stop(dev); 102 103 if (!mt76_poll(dev, MT_USB_DMA_CFG, MT_USB_DMA_CFG_RX_BUSY, 0, 1000)) 104 dev_warn(dev->mt76.dev, "RX DMA did not stop\n"); 105 } 106 107 static int mt76x0u_start(struct ieee80211_hw *hw) 108 { 109 struct mt76x02_dev *dev = hw->priv; 110 int ret; 111 112 mutex_lock(&dev->mt76.mutex); 113 114 ret = mt76x0_mac_start(dev); 115 if (ret) 116 goto out; 117 118 mt76x0_phy_calibrate(dev, true); 119 ieee80211_queue_delayed_work(dev->mt76.hw, &dev->mac_work, 120 MT_MAC_WORK_INTERVAL); 121 ieee80211_queue_delayed_work(dev->mt76.hw, &dev->cal_work, 122 MT_CALIBRATE_INTERVAL); 123 set_bit(MT76_STATE_RUNNING, &dev->mt76.state); 124 125 out: 126 mutex_unlock(&dev->mt76.mutex); 127 return ret; 128 } 129 130 static void mt76x0u_stop(struct ieee80211_hw *hw) 131 { 132 struct mt76x02_dev *dev = hw->priv; 133 134 mutex_lock(&dev->mt76.mutex); 135 mt76x0u_mac_stop(dev); 136 mutex_unlock(&dev->mt76.mutex); 137 } 138 139 static const struct ieee80211_ops mt76x0u_ops = { 140 .tx = mt76x02_tx, 141 .start = mt76x0u_start, 142 .stop = mt76x0u_stop, 143 .add_interface = mt76x02_add_interface, 144 .remove_interface = mt76x02_remove_interface, 145 .config = mt76x0_config, 146 .configure_filter = mt76x02_configure_filter, 147 .bss_info_changed = mt76x02_bss_info_changed, 148 .sta_state = mt76_sta_state, 149 .set_key = mt76x02_set_key, 150 .conf_tx = mt76x02_conf_tx, 151 .sw_scan_start = mt76x02_sw_scan, 152 .sw_scan_complete = mt76x02_sw_scan_complete, 153 .ampdu_action = mt76x02_ampdu_action, 154 .sta_rate_tbl_update = mt76x02_sta_rate_tbl_update, 155 .set_rts_threshold = mt76x02_set_rts_threshold, 156 .wake_tx_queue = mt76_wake_tx_queue, 157 .get_txpower = mt76_get_txpower, 158 }; 159 160 static int mt76x0u_init_hardware(struct mt76x02_dev *dev) 161 { 162 int err; 163 164 mt76x0_chip_onoff(dev, true, true); 165 166 if (!mt76x02_wait_for_mac(&dev->mt76)) 167 return -ETIMEDOUT; 168 169 err = mt76x0u_mcu_init(dev); 170 if (err < 0) 171 return err; 172 173 mt76x0_init_usb_dma(dev); 174 err = mt76x0_init_hardware(dev); 175 if (err < 0) 176 return err; 177 178 mt76_rmw(dev, MT_US_CYC_CFG, MT_US_CYC_CNT, 0x1e); 179 mt76_wr(dev, MT_TXOP_CTRL_CFG, 180 FIELD_PREP(MT_TXOP_TRUN_EN, 0x3f) | 181 FIELD_PREP(MT_TXOP_EXT_CCA_DLY, 0x58)); 182 183 return 0; 184 } 185 186 static int mt76x0u_register_device(struct mt76x02_dev *dev) 187 { 188 struct ieee80211_hw *hw = dev->mt76.hw; 189 int err; 190 191 err = mt76u_alloc_queues(&dev->mt76); 192 if (err < 0) 193 goto out_err; 194 195 err = mt76x0u_init_hardware(dev); 196 if (err < 0) 197 goto out_err; 198 199 err = mt76x0_register_device(dev); 200 if (err < 0) 201 goto out_err; 202 203 /* check hw sg support in order to enable AMSDU */ 204 if (dev->mt76.usb.sg_en) 205 hw->max_tx_fragments = MT_SG_MAX_SIZE; 206 else 207 hw->max_tx_fragments = 1; 208 209 set_bit(MT76_STATE_INITIALIZED, &dev->mt76.state); 210 211 return 0; 212 213 out_err: 214 mt76x0u_cleanup(dev); 215 return err; 216 } 217 218 static int mt76x0u_probe(struct usb_interface *usb_intf, 219 const struct usb_device_id *id) 220 { 221 static const struct mt76_driver_ops drv_ops = { 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_add = mt76x02_sta_add, 227 .sta_remove = mt76x02_sta_remove, 228 }; 229 struct usb_device *usb_dev = interface_to_usbdev(usb_intf); 230 struct mt76x02_dev *dev; 231 struct mt76_dev *mdev; 232 u32 mac_rev; 233 int ret; 234 235 mdev = mt76_alloc_device(&usb_intf->dev, sizeof(*dev), &mt76x0u_ops, 236 &drv_ops); 237 if (!mdev) 238 return -ENOMEM; 239 240 dev = container_of(mdev, struct mt76x02_dev, mt76); 241 mutex_init(&dev->phy_mutex); 242 243 /* Quirk for Archer T1U */ 244 if (id->driver_info) 245 dev->no_2ghz = true; 246 247 usb_dev = usb_get_dev(usb_dev); 248 usb_reset_device(usb_dev); 249 250 usb_set_intfdata(usb_intf, dev); 251 252 mt76x02u_init_mcu(mdev); 253 ret = mt76u_init(mdev, usb_intf); 254 if (ret) 255 goto err; 256 257 /* Disable the HW, otherwise MCU fail to initalize on hot reboot */ 258 mt76x0_chip_onoff(dev, false, false); 259 260 if (!mt76x02_wait_for_mac(mdev)) { 261 ret = -ETIMEDOUT; 262 goto err; 263 } 264 265 mdev->rev = mt76_rr(dev, MT_ASIC_VERSION); 266 mac_rev = mt76_rr(dev, MT_MAC_CSR0); 267 dev_info(mdev->dev, "ASIC revision: %08x MAC revision: %08x\n", 268 mdev->rev, mac_rev); 269 if (!is_mt76x0(dev)) { 270 ret = -ENODEV; 271 goto err; 272 } 273 274 /* Note: vendor driver skips this check for MT76X0U */ 275 if (!(mt76_rr(dev, MT_EFUSE_CTRL) & MT_EFUSE_CTRL_SEL)) 276 dev_warn(mdev->dev, "Warning: eFUSE not present\n"); 277 278 ret = mt76x0u_register_device(dev); 279 if (ret < 0) 280 goto err; 281 282 return 0; 283 284 err: 285 usb_set_intfdata(usb_intf, NULL); 286 usb_put_dev(interface_to_usbdev(usb_intf)); 287 288 ieee80211_free_hw(mdev->hw); 289 return ret; 290 } 291 292 static void mt76x0_disconnect(struct usb_interface *usb_intf) 293 { 294 struct mt76x02_dev *dev = usb_get_intfdata(usb_intf); 295 bool initalized = test_bit(MT76_STATE_INITIALIZED, &dev->mt76.state); 296 297 if (!initalized) 298 return; 299 300 ieee80211_unregister_hw(dev->mt76.hw); 301 mt76x0u_cleanup(dev); 302 303 usb_set_intfdata(usb_intf, NULL); 304 usb_put_dev(interface_to_usbdev(usb_intf)); 305 306 ieee80211_free_hw(dev->mt76.hw); 307 } 308 309 static int __maybe_unused mt76x0_suspend(struct usb_interface *usb_intf, 310 pm_message_t state) 311 { 312 struct mt76x02_dev *dev = usb_get_intfdata(usb_intf); 313 314 mt76u_stop_queues(&dev->mt76); 315 mt76x0u_mac_stop(dev); 316 clear_bit(MT76_STATE_MCU_RUNNING, &dev->mt76.state); 317 mt76x0_chip_onoff(dev, false, false); 318 319 return 0; 320 } 321 322 static int __maybe_unused mt76x0_resume(struct usb_interface *usb_intf) 323 { 324 struct mt76x02_dev *dev = usb_get_intfdata(usb_intf); 325 struct mt76_usb *usb = &dev->mt76.usb; 326 int ret; 327 328 ret = mt76u_submit_rx_buffers(&dev->mt76); 329 if (ret < 0) 330 goto err; 331 332 tasklet_enable(&usb->rx_tasklet); 333 tasklet_enable(&usb->tx_tasklet); 334 335 ret = mt76x0u_init_hardware(dev); 336 if (ret) 337 goto err; 338 339 return 0; 340 err: 341 mt76x0u_cleanup(dev); 342 return ret; 343 } 344 345 MODULE_DEVICE_TABLE(usb, mt76x0_device_table); 346 MODULE_FIRMWARE(MT7610E_FIRMWARE); 347 MODULE_FIRMWARE(MT7610U_FIRMWARE); 348 MODULE_LICENSE("GPL"); 349 350 static struct usb_driver mt76x0_driver = { 351 .name = KBUILD_MODNAME, 352 .id_table = mt76x0_device_table, 353 .probe = mt76x0u_probe, 354 .disconnect = mt76x0_disconnect, 355 .suspend = mt76x0_suspend, 356 .resume = mt76x0_resume, 357 .reset_resume = mt76x0_resume, 358 .soft_unbind = 1, 359 .disable_hub_initiated_lpm = 1, 360 }; 361 module_usb_driver(mt76x0_driver); 362