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