1 // SPDX-License-Identifier: ISC 2 /* Copyright (C) 2022 MediaTek Inc. 3 * 4 * Author: Lorenzo Bianconi <lorenzo@kernel.org> 5 */ 6 7 #include <linux/kernel.h> 8 #include <linux/module.h> 9 #include <linux/usb.h> 10 11 #include "mt7921.h" 12 #include "mcu.h" 13 #include "../mt76_connac2_mac.h" 14 15 static const struct usb_device_id mt7921u_device_table[] = { 16 { USB_DEVICE_AND_INTERFACE_INFO(0x0e8d, 0x7961, 0xff, 0xff, 0xff), 17 .driver_info = (kernel_ulong_t)MT7921_FIRMWARE_WM }, 18 /* Comfast CF-952AX */ 19 { USB_DEVICE_AND_INTERFACE_INFO(0x3574, 0x6211, 0xff, 0xff, 0xff), 20 .driver_info = (kernel_ulong_t)MT7921_FIRMWARE_WM }, 21 /* Netgear, Inc. [A8000,AXE3000] */ 22 { USB_DEVICE_AND_INTERFACE_INFO(0x0846, 0x9060, 0xff, 0xff, 0xff), 23 .driver_info = (kernel_ulong_t)MT7921_FIRMWARE_WM }, 24 { }, 25 }; 26 27 static u32 mt7921u_rr(struct mt76_dev *dev, u32 addr) 28 { 29 u32 ret; 30 31 mutex_lock(&dev->usb.usb_ctrl_mtx); 32 ret = ___mt76u_rr(dev, MT_VEND_READ_EXT, 33 USB_DIR_IN | MT_USB_TYPE_VENDOR, addr); 34 mutex_unlock(&dev->usb.usb_ctrl_mtx); 35 36 return ret; 37 } 38 39 static void mt7921u_wr(struct mt76_dev *dev, u32 addr, u32 val) 40 { 41 mutex_lock(&dev->usb.usb_ctrl_mtx); 42 ___mt76u_wr(dev, MT_VEND_WRITE_EXT, 43 USB_DIR_OUT | MT_USB_TYPE_VENDOR, addr, val); 44 mutex_unlock(&dev->usb.usb_ctrl_mtx); 45 } 46 47 static u32 mt7921u_rmw(struct mt76_dev *dev, u32 addr, 48 u32 mask, u32 val) 49 { 50 mutex_lock(&dev->usb.usb_ctrl_mtx); 51 val |= ___mt76u_rr(dev, MT_VEND_READ_EXT, 52 USB_DIR_IN | MT_USB_TYPE_VENDOR, addr) & ~mask; 53 ___mt76u_wr(dev, MT_VEND_WRITE_EXT, 54 USB_DIR_OUT | MT_USB_TYPE_VENDOR, addr, val); 55 mutex_unlock(&dev->usb.usb_ctrl_mtx); 56 57 return val; 58 } 59 60 static void mt7921u_copy(struct mt76_dev *dev, u32 offset, 61 const void *data, int len) 62 { 63 struct mt76_usb *usb = &dev->usb; 64 int ret, i = 0, batch_len; 65 const u8 *val = data; 66 67 len = round_up(len, 4); 68 69 mutex_lock(&usb->usb_ctrl_mtx); 70 while (i < len) { 71 batch_len = min_t(int, usb->data_len, len - i); 72 memcpy(usb->data, val + i, batch_len); 73 ret = __mt76u_vendor_request(dev, MT_VEND_WRITE_EXT, 74 USB_DIR_OUT | MT_USB_TYPE_VENDOR, 75 (offset + i) >> 16, offset + i, 76 usb->data, batch_len); 77 if (ret < 0) 78 break; 79 80 i += batch_len; 81 } 82 mutex_unlock(&usb->usb_ctrl_mtx); 83 } 84 85 int mt7921u_mcu_power_on(struct mt7921_dev *dev) 86 { 87 int ret; 88 89 ret = mt76u_vendor_request(&dev->mt76, MT_VEND_POWER_ON, 90 USB_DIR_OUT | MT_USB_TYPE_VENDOR, 91 0x0, 0x1, NULL, 0); 92 if (ret) 93 return ret; 94 95 if (!mt76_poll_msec(dev, MT_CONN_ON_MISC, MT_TOP_MISC2_FW_PWR_ON, 96 MT_TOP_MISC2_FW_PWR_ON, 500)) { 97 dev_err(dev->mt76.dev, "Timeout for power on\n"); 98 ret = -EIO; 99 } 100 101 return ret; 102 } 103 104 static int 105 mt7921u_mcu_send_message(struct mt76_dev *mdev, struct sk_buff *skb, 106 int cmd, int *seq) 107 { 108 struct mt7921_dev *dev = container_of(mdev, struct mt7921_dev, mt76); 109 u32 pad, ep; 110 int ret; 111 112 ret = mt76_connac2_mcu_fill_message(mdev, skb, cmd, seq); 113 if (ret) 114 return ret; 115 116 mdev->mcu.timeout = 3 * HZ; 117 118 if (cmd != MCU_CMD(FW_SCATTER)) 119 ep = MT_EP_OUT_INBAND_CMD; 120 else 121 ep = MT_EP_OUT_AC_BE; 122 123 mt7921_skb_add_usb_sdio_hdr(dev, skb, 0); 124 pad = round_up(skb->len, 4) + 4 - skb->len; 125 __skb_put_zero(skb, pad); 126 127 ret = mt76u_bulk_msg(&dev->mt76, skb->data, skb->len, NULL, 128 1000, ep); 129 dev_kfree_skb(skb); 130 131 return ret; 132 } 133 134 static int mt7921u_mcu_init(struct mt7921_dev *dev) 135 { 136 static const struct mt76_mcu_ops mcu_ops = { 137 .headroom = MT_SDIO_HDR_SIZE + 138 sizeof(struct mt76_connac2_mcu_txd), 139 .tailroom = MT_USB_TAIL_SIZE, 140 .mcu_skb_send_msg = mt7921u_mcu_send_message, 141 .mcu_parse_response = mt7921_mcu_parse_response, 142 }; 143 int ret; 144 145 dev->mt76.mcu_ops = &mcu_ops; 146 147 mt76_set(dev, MT_UDMA_TX_QSEL, MT_FW_DL_EN); 148 ret = mt7921_run_firmware(dev); 149 if (ret) 150 return ret; 151 152 set_bit(MT76_STATE_MCU_RUNNING, &dev->mphy.state); 153 mt76_clear(dev, MT_UDMA_TX_QSEL, MT_FW_DL_EN); 154 155 return 0; 156 } 157 158 static void mt7921u_stop(struct ieee80211_hw *hw) 159 { 160 struct mt7921_dev *dev = mt7921_hw_dev(hw); 161 162 mt76u_stop_tx(&dev->mt76); 163 mt7921_stop(hw); 164 } 165 166 static void mt7921u_cleanup(struct mt7921_dev *dev) 167 { 168 clear_bit(MT76_STATE_INITIALIZED, &dev->mphy.state); 169 mt7921u_wfsys_reset(dev); 170 skb_queue_purge(&dev->mt76.mcu.res_q); 171 mt76u_queues_deinit(&dev->mt76); 172 } 173 174 static int mt7921u_probe(struct usb_interface *usb_intf, 175 const struct usb_device_id *id) 176 { 177 static const struct mt76_driver_ops drv_ops = { 178 .txwi_size = MT_SDIO_TXD_SIZE, 179 .drv_flags = MT_DRV_RX_DMA_HDR | MT_DRV_HW_MGMT_TXQ | 180 MT_DRV_AMSDU_OFFLOAD, 181 .survey_flags = SURVEY_INFO_TIME_TX | 182 SURVEY_INFO_TIME_RX | 183 SURVEY_INFO_TIME_BSS_RX, 184 .tx_prepare_skb = mt7921_usb_sdio_tx_prepare_skb, 185 .tx_complete_skb = mt7921_usb_sdio_tx_complete_skb, 186 .tx_status_data = mt7921_usb_sdio_tx_status_data, 187 .rx_skb = mt7921_queue_rx_skb, 188 .rx_check = mt7921_rx_check, 189 .sta_add = mt7921_mac_sta_add, 190 .sta_assoc = mt7921_mac_sta_assoc, 191 .sta_remove = mt7921_mac_sta_remove, 192 .update_survey = mt7921_update_channel, 193 }; 194 static const struct mt7921_hif_ops hif_ops = { 195 .mcu_init = mt7921u_mcu_init, 196 .init_reset = mt7921u_init_reset, 197 .reset = mt7921u_mac_reset, 198 }; 199 static struct mt76_bus_ops bus_ops = { 200 .rr = mt7921u_rr, 201 .wr = mt7921u_wr, 202 .rmw = mt7921u_rmw, 203 .read_copy = mt76u_read_copy, 204 .write_copy = mt7921u_copy, 205 .type = MT76_BUS_USB, 206 }; 207 struct usb_device *udev = interface_to_usbdev(usb_intf); 208 struct ieee80211_ops *ops; 209 struct ieee80211_hw *hw; 210 struct mt7921_dev *dev; 211 struct mt76_dev *mdev; 212 u8 features; 213 int ret; 214 215 ops = mt7921_get_mac80211_ops(&usb_intf->dev, (void *)id->driver_info, 216 &features); 217 if (!ops) 218 return -ENOMEM; 219 220 ops->stop = mt7921u_stop; 221 mdev = mt76_alloc_device(&usb_intf->dev, sizeof(*dev), ops, &drv_ops); 222 if (!mdev) 223 return -ENOMEM; 224 225 dev = container_of(mdev, struct mt7921_dev, mt76); 226 dev->fw_features = features; 227 dev->hif_ops = &hif_ops; 228 229 udev = usb_get_dev(udev); 230 usb_reset_device(udev); 231 232 usb_set_intfdata(usb_intf, dev); 233 234 ret = __mt76u_init(mdev, usb_intf, &bus_ops); 235 if (ret < 0) 236 goto error; 237 238 mdev->rev = (mt76_rr(dev, MT_HW_CHIPID) << 16) | 239 (mt76_rr(dev, MT_HW_REV) & 0xff); 240 dev_dbg(mdev->dev, "ASIC revision: %04x\n", mdev->rev); 241 242 if (mt76_get_field(dev, MT_CONN_ON_MISC, MT_TOP_MISC2_FW_N9_RDY)) { 243 ret = mt7921u_wfsys_reset(dev); 244 if (ret) 245 goto error; 246 } 247 248 ret = mt7921u_mcu_power_on(dev); 249 if (ret) 250 goto error; 251 252 ret = mt76u_alloc_mcu_queue(&dev->mt76); 253 if (ret) 254 goto error; 255 256 ret = mt76u_alloc_queues(&dev->mt76); 257 if (ret) 258 goto error; 259 260 ret = mt7921u_dma_init(dev, false); 261 if (ret) 262 goto error; 263 264 hw = mt76_hw(dev); 265 /* check hw sg support in order to enable AMSDU */ 266 hw->max_tx_fragments = mdev->usb.sg_en ? MT_HW_TXP_MAX_BUF_NUM : 1; 267 268 ret = mt7921_register_device(dev); 269 if (ret) 270 goto error; 271 272 return 0; 273 274 error: 275 mt76u_queues_deinit(&dev->mt76); 276 277 usb_set_intfdata(usb_intf, NULL); 278 usb_put_dev(interface_to_usbdev(usb_intf)); 279 280 mt76_free_device(&dev->mt76); 281 282 return ret; 283 } 284 285 static void mt7921u_disconnect(struct usb_interface *usb_intf) 286 { 287 struct mt7921_dev *dev = usb_get_intfdata(usb_intf); 288 289 cancel_work_sync(&dev->init_work); 290 if (!test_bit(MT76_STATE_INITIALIZED, &dev->mphy.state)) 291 return; 292 293 mt76_unregister_device(&dev->mt76); 294 mt7921u_cleanup(dev); 295 296 usb_set_intfdata(usb_intf, NULL); 297 usb_put_dev(interface_to_usbdev(usb_intf)); 298 299 mt76_free_device(&dev->mt76); 300 } 301 302 #ifdef CONFIG_PM 303 static int mt7921u_suspend(struct usb_interface *intf, pm_message_t state) 304 { 305 struct mt7921_dev *dev = usb_get_intfdata(intf); 306 struct mt76_connac_pm *pm = &dev->pm; 307 int err; 308 309 pm->suspended = true; 310 flush_work(&dev->reset_work); 311 312 err = mt76_connac_mcu_set_hif_suspend(&dev->mt76, true); 313 if (err) 314 goto failed; 315 316 mt76u_stop_rx(&dev->mt76); 317 mt76u_stop_tx(&dev->mt76); 318 319 return 0; 320 321 failed: 322 pm->suspended = false; 323 324 if (err < 0) 325 mt7921_reset(&dev->mt76); 326 327 return err; 328 } 329 330 static int mt7921u_resume(struct usb_interface *intf) 331 { 332 struct mt7921_dev *dev = usb_get_intfdata(intf); 333 struct mt76_connac_pm *pm = &dev->pm; 334 bool reinit = true; 335 int err, i; 336 337 for (i = 0; i < 10; i++) { 338 u32 val = mt76_rr(dev, MT_WF_SW_DEF_CR_USB_MCU_EVENT); 339 340 if (!(val & MT_WF_SW_SER_TRIGGER_SUSPEND)) { 341 reinit = false; 342 break; 343 } 344 if (val & MT_WF_SW_SER_DONE_SUSPEND) { 345 mt76_wr(dev, MT_WF_SW_DEF_CR_USB_MCU_EVENT, 0); 346 break; 347 } 348 349 msleep(20); 350 } 351 352 if (reinit || mt7921_dma_need_reinit(dev)) { 353 err = mt7921u_dma_init(dev, true); 354 if (err) 355 goto failed; 356 } 357 358 err = mt76u_resume_rx(&dev->mt76); 359 if (err < 0) 360 goto failed; 361 362 err = mt76_connac_mcu_set_hif_suspend(&dev->mt76, false); 363 failed: 364 pm->suspended = false; 365 366 if (err < 0) 367 mt7921_reset(&dev->mt76); 368 369 return err; 370 } 371 #endif /* CONFIG_PM */ 372 373 MODULE_DEVICE_TABLE(usb, mt7921u_device_table); 374 MODULE_FIRMWARE(MT7921_FIRMWARE_WM); 375 MODULE_FIRMWARE(MT7921_ROM_PATCH); 376 377 static struct usb_driver mt7921u_driver = { 378 .name = KBUILD_MODNAME, 379 .id_table = mt7921u_device_table, 380 .probe = mt7921u_probe, 381 .disconnect = mt7921u_disconnect, 382 #ifdef CONFIG_PM 383 .suspend = mt7921u_suspend, 384 .resume = mt7921u_resume, 385 .reset_resume = mt7921u_resume, 386 #endif /* CONFIG_PM */ 387 .soft_unbind = 1, 388 .disable_hub_initiated_lpm = 1, 389 }; 390 module_usb_driver(mt7921u_driver); 391 392 MODULE_AUTHOR("Lorenzo Bianconi <lorenzo@kernel.org>"); 393 MODULE_LICENSE("Dual BSD/GPL"); 394