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; 130 131 ret = mt76u_alloc_queues(&dev->mt76); 132 if (ret) 133 goto error; 134 135 ret = mt7663_usb_sdio_register_device(dev); 136 if (ret) 137 goto error; 138 139 return 0; 140 141 error: 142 mt76u_queues_deinit(&dev->mt76); 143 usb_set_intfdata(usb_intf, NULL); 144 usb_put_dev(interface_to_usbdev(usb_intf)); 145 146 mt76_free_device(&dev->mt76); 147 148 return ret; 149 } 150 151 static void mt7663u_disconnect(struct usb_interface *usb_intf) 152 { 153 struct mt7615_dev *dev = usb_get_intfdata(usb_intf); 154 155 if (!test_bit(MT76_STATE_INITIALIZED, &dev->mphy.state)) 156 return; 157 158 ieee80211_unregister_hw(dev->mt76.hw); 159 mt7663u_cleanup(dev); 160 161 usb_set_intfdata(usb_intf, NULL); 162 usb_put_dev(interface_to_usbdev(usb_intf)); 163 164 mt76_free_device(&dev->mt76); 165 } 166 167 #ifdef CONFIG_PM 168 static int mt7663u_suspend(struct usb_interface *intf, pm_message_t state) 169 { 170 struct mt7615_dev *dev = usb_get_intfdata(intf); 171 172 if (!test_bit(MT76_STATE_SUSPEND, &dev->mphy.state) && 173 mt7615_firmware_offload(dev)) { 174 int err; 175 176 err = mt7615_mcu_set_hif_suspend(dev, true); 177 if (err < 0) 178 return err; 179 } 180 181 mt76u_stop_rx(&dev->mt76); 182 mt76u_stop_tx(&dev->mt76); 183 184 return 0; 185 } 186 187 static int mt7663u_resume(struct usb_interface *intf) 188 { 189 struct mt7615_dev *dev = usb_get_intfdata(intf); 190 int err; 191 192 err = mt76u_vendor_request(&dev->mt76, MT_VEND_FEATURE_SET, 193 USB_DIR_OUT | USB_TYPE_VENDOR, 194 0x5, 0x0, NULL, 0); 195 if (err) 196 return err; 197 198 err = mt76u_resume_rx(&dev->mt76); 199 if (err < 0) 200 return err; 201 202 if (!test_bit(MT76_STATE_SUSPEND, &dev->mphy.state) && 203 mt7615_firmware_offload(dev)) 204 err = mt7615_mcu_set_hif_suspend(dev, false); 205 206 return err; 207 } 208 #endif /* CONFIG_PM */ 209 210 MODULE_DEVICE_TABLE(usb, mt7615_device_table); 211 MODULE_FIRMWARE(MT7663_OFFLOAD_FIRMWARE_N9); 212 MODULE_FIRMWARE(MT7663_OFFLOAD_ROM_PATCH); 213 MODULE_FIRMWARE(MT7663_FIRMWARE_N9); 214 MODULE_FIRMWARE(MT7663_ROM_PATCH); 215 216 static struct usb_driver mt7663u_driver = { 217 .name = KBUILD_MODNAME, 218 .id_table = mt7615_device_table, 219 .probe = mt7663u_probe, 220 .disconnect = mt7663u_disconnect, 221 #ifdef CONFIG_PM 222 .suspend = mt7663u_suspend, 223 .resume = mt7663u_resume, 224 .reset_resume = mt7663u_resume, 225 #endif /* CONFIG_PM */ 226 .soft_unbind = 1, 227 .disable_hub_initiated_lpm = 1, 228 }; 229 module_usb_driver(mt7663u_driver); 230 231 MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>"); 232 MODULE_AUTHOR("Lorenzo Bianconi <lorenzo@kernel.org>"); 233 MODULE_LICENSE("Dual BSD/GPL"); 234