1 // SPDX-License-Identifier: ISC 2 /* Copyright (C) 2019 MediaTek Inc. 3 * 4 * Author: Felix Fietkau <nbd@nbd.name> 5 * Lorenzo Bianconi <lorenzo@kernel.org> 6 * Sean Wang <sean.wang@mediatek.com> 7 */ 8 9 #include <linux/kernel.h> 10 #include <linux/module.h> 11 #include <linux/usb.h> 12 13 #include "mt7615.h" 14 #include "mac.h" 15 #include "mcu.h" 16 #include "regs.h" 17 18 static const struct usb_device_id mt7615_device_table[] = { 19 { USB_DEVICE_AND_INTERFACE_INFO(0x0e8d, 0x7663, 0xff, 0xff, 0xff) }, 20 { USB_DEVICE_AND_INTERFACE_INFO(0x043e, 0x310c, 0xff, 0xff, 0xff) }, 21 { }, 22 }; 23 24 static void mt7663u_stop(struct ieee80211_hw *hw) 25 { 26 struct mt7615_phy *phy = mt7615_hw_phy(hw); 27 struct mt7615_dev *dev = hw->priv; 28 29 clear_bit(MT76_STATE_RUNNING, &dev->mphy.state); 30 del_timer_sync(&phy->roc_timer); 31 cancel_work_sync(&phy->roc_work); 32 cancel_delayed_work_sync(&phy->scan_work); 33 cancel_delayed_work_sync(&phy->mt76->mac_work); 34 mt76u_stop_tx(&dev->mt76); 35 } 36 37 static void mt7663u_cleanup(struct mt7615_dev *dev) 38 { 39 clear_bit(MT76_STATE_INITIALIZED, &dev->mphy.state); 40 mt76u_queues_deinit(&dev->mt76); 41 } 42 43 static void mt7663u_init_work(struct work_struct *work) 44 { 45 struct mt7615_dev *dev; 46 47 dev = container_of(work, struct mt7615_dev, mcu_work); 48 if (mt7663u_mcu_init(dev)) 49 return; 50 51 mt7615_init_work(dev); 52 } 53 54 static int mt7663u_probe(struct usb_interface *usb_intf, 55 const struct usb_device_id *id) 56 { 57 static const struct mt76_driver_ops drv_ops = { 58 .txwi_size = MT_USB_TXD_SIZE, 59 .drv_flags = MT_DRV_RX_DMA_HDR | MT_DRV_HW_MGMT_TXQ, 60 .tx_prepare_skb = mt7663_usb_sdio_tx_prepare_skb, 61 .tx_complete_skb = mt7663_usb_sdio_tx_complete_skb, 62 .tx_status_data = mt7663_usb_sdio_tx_status_data, 63 .rx_skb = mt7615_queue_rx_skb, 64 .sta_ps = mt7615_sta_ps, 65 .sta_add = mt7615_mac_sta_add, 66 .sta_remove = mt7615_mac_sta_remove, 67 .update_survey = mt7615_update_channel, 68 }; 69 struct usb_device *udev = interface_to_usbdev(usb_intf); 70 struct ieee80211_ops *ops; 71 struct mt7615_dev *dev; 72 struct mt76_dev *mdev; 73 int ret; 74 75 ops = devm_kmemdup(&usb_intf->dev, &mt7615_ops, sizeof(mt7615_ops), 76 GFP_KERNEL); 77 if (!ops) 78 return -ENOMEM; 79 80 ops->stop = mt7663u_stop; 81 82 mdev = mt76_alloc_device(&usb_intf->dev, sizeof(*dev), ops, &drv_ops); 83 if (!mdev) 84 return -ENOMEM; 85 86 dev = container_of(mdev, struct mt7615_dev, mt76); 87 udev = usb_get_dev(udev); 88 usb_reset_device(udev); 89 90 usb_set_intfdata(usb_intf, dev); 91 92 INIT_WORK(&dev->mcu_work, mt7663u_init_work); 93 dev->reg_map = mt7663_usb_sdio_reg_map; 94 dev->ops = ops; 95 ret = mt76u_init(mdev, usb_intf, true); 96 if (ret < 0) 97 goto error; 98 99 mdev->rev = (mt76_rr(dev, MT_HW_CHIPID) << 16) | 100 (mt76_rr(dev, MT_HW_REV) & 0xff); 101 dev_dbg(mdev->dev, "ASIC revision: %04x\n", mdev->rev); 102 103 if (mt76_poll_msec(dev, MT_CONN_ON_MISC, MT_TOP_MISC2_FW_PWR_ON, 104 FW_STATE_PWR_ON << 1, 500)) { 105 dev_dbg(dev->mt76.dev, "Usb device already powered on\n"); 106 set_bit(MT76_STATE_POWER_OFF, &dev->mphy.state); 107 goto alloc_queues; 108 } 109 110 ret = mt76u_vendor_request(&dev->mt76, MT_VEND_POWER_ON, 111 USB_DIR_OUT | USB_TYPE_VENDOR, 112 0x0, 0x1, NULL, 0); 113 if (ret) 114 goto error; 115 116 if (!mt76_poll_msec(dev, MT_CONN_ON_MISC, MT_TOP_MISC2_FW_PWR_ON, 117 FW_STATE_PWR_ON << 1, 500)) { 118 dev_err(dev->mt76.dev, "Timeout for power on\n"); 119 ret = -EIO; 120 goto error; 121 } 122 123 alloc_queues: 124 ret = mt76u_alloc_mcu_queue(&dev->mt76); 125 if (ret) 126 goto error; 127 128 ret = mt76u_alloc_queues(&dev->mt76); 129 if (ret) 130 goto error; 131 132 ret = mt7663_usb_sdio_register_device(dev); 133 if (ret) 134 goto error; 135 136 return 0; 137 138 error: 139 mt76u_queues_deinit(&dev->mt76); 140 usb_set_intfdata(usb_intf, NULL); 141 usb_put_dev(interface_to_usbdev(usb_intf)); 142 143 mt76_free_device(&dev->mt76); 144 145 return ret; 146 } 147 148 static void mt7663u_disconnect(struct usb_interface *usb_intf) 149 { 150 struct mt7615_dev *dev = usb_get_intfdata(usb_intf); 151 152 if (!test_bit(MT76_STATE_INITIALIZED, &dev->mphy.state)) 153 return; 154 155 ieee80211_unregister_hw(dev->mt76.hw); 156 mt7663u_cleanup(dev); 157 158 usb_set_intfdata(usb_intf, NULL); 159 usb_put_dev(interface_to_usbdev(usb_intf)); 160 161 mt76_free_device(&dev->mt76); 162 } 163 164 #ifdef CONFIG_PM 165 static int mt7663u_suspend(struct usb_interface *intf, pm_message_t state) 166 { 167 struct mt7615_dev *dev = usb_get_intfdata(intf); 168 169 if (!test_bit(MT76_STATE_SUSPEND, &dev->mphy.state) && 170 mt7615_firmware_offload(dev)) { 171 int err; 172 173 err = mt76_connac_mcu_set_hif_suspend(&dev->mt76, true); 174 if (err < 0) 175 return err; 176 } 177 178 mt76u_stop_rx(&dev->mt76); 179 mt76u_stop_tx(&dev->mt76); 180 181 return 0; 182 } 183 184 static int mt7663u_resume(struct usb_interface *intf) 185 { 186 struct mt7615_dev *dev = usb_get_intfdata(intf); 187 int err; 188 189 err = mt76u_vendor_request(&dev->mt76, MT_VEND_FEATURE_SET, 190 USB_DIR_OUT | USB_TYPE_VENDOR, 191 0x5, 0x0, NULL, 0); 192 if (err) 193 return err; 194 195 err = mt76u_resume_rx(&dev->mt76); 196 if (err < 0) 197 return err; 198 199 if (!test_bit(MT76_STATE_SUSPEND, &dev->mphy.state) && 200 mt7615_firmware_offload(dev)) 201 err = mt76_connac_mcu_set_hif_suspend(&dev->mt76, false); 202 203 return err; 204 } 205 #endif /* CONFIG_PM */ 206 207 MODULE_DEVICE_TABLE(usb, mt7615_device_table); 208 MODULE_FIRMWARE(MT7663_OFFLOAD_FIRMWARE_N9); 209 MODULE_FIRMWARE(MT7663_OFFLOAD_ROM_PATCH); 210 MODULE_FIRMWARE(MT7663_FIRMWARE_N9); 211 MODULE_FIRMWARE(MT7663_ROM_PATCH); 212 213 static struct usb_driver mt7663u_driver = { 214 .name = KBUILD_MODNAME, 215 .id_table = mt7615_device_table, 216 .probe = mt7663u_probe, 217 .disconnect = mt7663u_disconnect, 218 #ifdef CONFIG_PM 219 .suspend = mt7663u_suspend, 220 .resume = mt7663u_resume, 221 .reset_resume = mt7663u_resume, 222 #endif /* CONFIG_PM */ 223 .soft_unbind = 1, 224 .disable_hub_initiated_lpm = 1, 225 }; 226 module_usb_driver(mt7663u_driver); 227 228 MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>"); 229 MODULE_AUTHOR("Lorenzo Bianconi <lorenzo@kernel.org>"); 230 MODULE_LICENSE("Dual BSD/GPL"); 231