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