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 int 28 mt7921u_mcu_send_message(struct mt76_dev *mdev, struct sk_buff *skb, 29 int cmd, int *seq) 30 { 31 struct mt792x_dev *dev = container_of(mdev, struct mt792x_dev, mt76); 32 u32 pad, ep; 33 int ret; 34 35 ret = mt76_connac2_mcu_fill_message(mdev, skb, cmd, seq); 36 if (ret) 37 return ret; 38 39 mdev->mcu.timeout = 3 * HZ; 40 41 if (cmd != MCU_CMD(FW_SCATTER)) 42 ep = MT_EP_OUT_INBAND_CMD; 43 else 44 ep = MT_EP_OUT_AC_BE; 45 46 mt792x_skb_add_usb_sdio_hdr(dev, skb, 0); 47 pad = round_up(skb->len, 4) + 4 - skb->len; 48 __skb_put_zero(skb, pad); 49 50 ret = mt76u_bulk_msg(&dev->mt76, skb->data, skb->len, NULL, 51 1000, ep); 52 dev_kfree_skb(skb); 53 54 return ret; 55 } 56 57 static int mt7921u_mcu_init(struct mt792x_dev *dev) 58 { 59 static const struct mt76_mcu_ops mcu_ops = { 60 .headroom = MT_SDIO_HDR_SIZE + 61 sizeof(struct mt76_connac2_mcu_txd), 62 .tailroom = MT_USB_TAIL_SIZE, 63 .mcu_skb_send_msg = mt7921u_mcu_send_message, 64 .mcu_parse_response = mt7921_mcu_parse_response, 65 }; 66 int ret; 67 68 dev->mt76.mcu_ops = &mcu_ops; 69 70 mt76_set(dev, MT_UDMA_TX_QSEL, MT_FW_DL_EN); 71 ret = mt7921_run_firmware(dev); 72 if (ret) 73 return ret; 74 75 set_bit(MT76_STATE_MCU_RUNNING, &dev->mphy.state); 76 mt76_clear(dev, MT_UDMA_TX_QSEL, MT_FW_DL_EN); 77 78 return 0; 79 } 80 81 static int mt7921u_mac_reset(struct mt792x_dev *dev) 82 { 83 int err; 84 85 mt76_txq_schedule_all(&dev->mphy); 86 mt76_worker_disable(&dev->mt76.tx_worker); 87 88 set_bit(MT76_RESET, &dev->mphy.state); 89 set_bit(MT76_MCU_RESET, &dev->mphy.state); 90 91 wake_up(&dev->mt76.mcu.wait); 92 skb_queue_purge(&dev->mt76.mcu.res_q); 93 94 mt76u_stop_rx(&dev->mt76); 95 mt76u_stop_tx(&dev->mt76); 96 97 mt792xu_wfsys_reset(dev); 98 99 clear_bit(MT76_MCU_RESET, &dev->mphy.state); 100 err = mt76u_resume_rx(&dev->mt76); 101 if (err) 102 goto out; 103 104 err = mt792xu_mcu_power_on(dev); 105 if (err) 106 goto out; 107 108 err = mt792xu_dma_init(dev, false); 109 if (err) 110 goto out; 111 112 mt76_wr(dev, MT_SWDEF_MODE, MT_SWDEF_NORMAL_MODE); 113 mt76_set(dev, MT_UDMA_TX_QSEL, MT_FW_DL_EN); 114 115 err = mt7921_run_firmware(dev); 116 if (err) 117 goto out; 118 119 mt76_clear(dev, MT_UDMA_TX_QSEL, MT_FW_DL_EN); 120 121 err = mt7921_mcu_set_eeprom(dev); 122 if (err) 123 goto out; 124 125 err = mt7921_mac_init(dev); 126 if (err) 127 goto out; 128 129 err = __mt7921_start(&dev->phy); 130 out: 131 clear_bit(MT76_RESET, &dev->mphy.state); 132 133 mt76_worker_enable(&dev->mt76.tx_worker); 134 135 return err; 136 } 137 138 static int mt7921u_probe(struct usb_interface *usb_intf, 139 const struct usb_device_id *id) 140 { 141 static const struct mt76_driver_ops drv_ops = { 142 .txwi_size = MT_SDIO_TXD_SIZE, 143 .drv_flags = MT_DRV_RX_DMA_HDR | MT_DRV_HW_MGMT_TXQ | 144 MT_DRV_AMSDU_OFFLOAD, 145 .survey_flags = SURVEY_INFO_TIME_TX | 146 SURVEY_INFO_TIME_RX | 147 SURVEY_INFO_TIME_BSS_RX, 148 .tx_prepare_skb = mt7921_usb_sdio_tx_prepare_skb, 149 .tx_complete_skb = mt7921_usb_sdio_tx_complete_skb, 150 .tx_status_data = mt7921_usb_sdio_tx_status_data, 151 .rx_skb = mt7921_queue_rx_skb, 152 .rx_check = mt7921_rx_check, 153 .sta_add = mt7921_mac_sta_add, 154 .sta_assoc = mt7921_mac_sta_assoc, 155 .sta_remove = mt7921_mac_sta_remove, 156 .update_survey = mt792x_update_channel, 157 }; 158 static const struct mt792x_hif_ops hif_ops = { 159 .mcu_init = mt7921u_mcu_init, 160 .init_reset = mt792xu_init_reset, 161 .reset = mt7921u_mac_reset, 162 }; 163 static struct mt76_bus_ops bus_ops = { 164 .rr = mt792xu_rr, 165 .wr = mt792xu_wr, 166 .rmw = mt792xu_rmw, 167 .read_copy = mt76u_read_copy, 168 .write_copy = mt792xu_copy, 169 .type = MT76_BUS_USB, 170 }; 171 struct usb_device *udev = interface_to_usbdev(usb_intf); 172 struct ieee80211_ops *ops; 173 struct ieee80211_hw *hw; 174 struct mt792x_dev *dev; 175 struct mt76_dev *mdev; 176 u8 features; 177 int ret; 178 179 ops = mt792x_get_mac80211_ops(&usb_intf->dev, &mt7921_ops, 180 (void *)id->driver_info, &features); 181 if (!ops) 182 return -ENOMEM; 183 184 ops->stop = mt792xu_stop; 185 mdev = mt76_alloc_device(&usb_intf->dev, sizeof(*dev), ops, &drv_ops); 186 if (!mdev) 187 return -ENOMEM; 188 189 dev = container_of(mdev, struct mt792x_dev, mt76); 190 dev->fw_features = features; 191 dev->hif_ops = &hif_ops; 192 193 udev = usb_get_dev(udev); 194 usb_reset_device(udev); 195 196 usb_set_intfdata(usb_intf, dev); 197 198 ret = __mt76u_init(mdev, usb_intf, &bus_ops); 199 if (ret < 0) 200 goto error; 201 202 mdev->rev = (mt76_rr(dev, MT_HW_CHIPID) << 16) | 203 (mt76_rr(dev, MT_HW_REV) & 0xff); 204 dev_dbg(mdev->dev, "ASIC revision: %04x\n", mdev->rev); 205 206 if (mt76_get_field(dev, MT_CONN_ON_MISC, MT_TOP_MISC2_FW_N9_RDY)) { 207 ret = mt792xu_wfsys_reset(dev); 208 if (ret) 209 goto error; 210 } 211 212 ret = mt792xu_mcu_power_on(dev); 213 if (ret) 214 goto error; 215 216 ret = mt76u_alloc_mcu_queue(&dev->mt76); 217 if (ret) 218 goto error; 219 220 ret = mt76u_alloc_queues(&dev->mt76); 221 if (ret) 222 goto error; 223 224 ret = mt792xu_dma_init(dev, false); 225 if (ret) 226 goto error; 227 228 hw = mt76_hw(dev); 229 /* check hw sg support in order to enable AMSDU */ 230 hw->max_tx_fragments = mdev->usb.sg_en ? MT_HW_TXP_MAX_BUF_NUM : 1; 231 232 ret = mt7921_register_device(dev); 233 if (ret) 234 goto error; 235 236 return 0; 237 238 error: 239 mt76u_queues_deinit(&dev->mt76); 240 241 usb_set_intfdata(usb_intf, NULL); 242 usb_put_dev(interface_to_usbdev(usb_intf)); 243 244 mt76_free_device(&dev->mt76); 245 246 return ret; 247 } 248 249 #ifdef CONFIG_PM 250 static int mt7921u_suspend(struct usb_interface *intf, pm_message_t state) 251 { 252 struct mt792x_dev *dev = usb_get_intfdata(intf); 253 struct mt76_connac_pm *pm = &dev->pm; 254 int err; 255 256 pm->suspended = true; 257 flush_work(&dev->reset_work); 258 259 err = mt76_connac_mcu_set_hif_suspend(&dev->mt76, true); 260 if (err) 261 goto failed; 262 263 mt76u_stop_rx(&dev->mt76); 264 mt76u_stop_tx(&dev->mt76); 265 266 return 0; 267 268 failed: 269 pm->suspended = false; 270 271 if (err < 0) 272 mt792x_reset(&dev->mt76); 273 274 return err; 275 } 276 277 static int mt7921u_resume(struct usb_interface *intf) 278 { 279 struct mt792x_dev *dev = usb_get_intfdata(intf); 280 struct mt76_connac_pm *pm = &dev->pm; 281 bool reinit = true; 282 int err, i; 283 284 for (i = 0; i < 10; i++) { 285 u32 val = mt76_rr(dev, MT_WF_SW_DEF_CR_USB_MCU_EVENT); 286 287 if (!(val & MT_WF_SW_SER_TRIGGER_SUSPEND)) { 288 reinit = false; 289 break; 290 } 291 if (val & MT_WF_SW_SER_DONE_SUSPEND) { 292 mt76_wr(dev, MT_WF_SW_DEF_CR_USB_MCU_EVENT, 0); 293 break; 294 } 295 296 msleep(20); 297 } 298 299 if (reinit || mt792x_dma_need_reinit(dev)) { 300 err = mt792xu_dma_init(dev, true); 301 if (err) 302 goto failed; 303 } 304 305 err = mt76u_resume_rx(&dev->mt76); 306 if (err < 0) 307 goto failed; 308 309 err = mt76_connac_mcu_set_hif_suspend(&dev->mt76, false); 310 failed: 311 pm->suspended = false; 312 313 if (err < 0) 314 mt792x_reset(&dev->mt76); 315 316 return err; 317 } 318 #endif /* CONFIG_PM */ 319 320 MODULE_DEVICE_TABLE(usb, mt7921u_device_table); 321 MODULE_FIRMWARE(MT7921_FIRMWARE_WM); 322 MODULE_FIRMWARE(MT7921_ROM_PATCH); 323 324 static struct usb_driver mt7921u_driver = { 325 .name = KBUILD_MODNAME, 326 .id_table = mt7921u_device_table, 327 .probe = mt7921u_probe, 328 .disconnect = mt792xu_disconnect, 329 #ifdef CONFIG_PM 330 .suspend = mt7921u_suspend, 331 .resume = mt7921u_resume, 332 .reset_resume = mt7921u_resume, 333 #endif /* CONFIG_PM */ 334 .soft_unbind = 1, 335 .disable_hub_initiated_lpm = 1, 336 }; 337 module_usb_driver(mt7921u_driver); 338 339 MODULE_DESCRIPTION("MediaTek MT7921U (USB) wireless driver"); 340 MODULE_AUTHOR("Lorenzo Bianconi <lorenzo@kernel.org>"); 341 MODULE_LICENSE("Dual BSD/GPL"); 342