1 // SPDX-License-Identifier: ISC
2 /*
3 * Copyright (C) 2018 Lorenzo Bianconi <lorenzo.bianconi83@gmail.com>
4 */
5
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8
9 #include "../mt76x02_usb.h"
10 #include "mt76x2u.h"
11
12 static const struct usb_device_id mt76x2u_device_table[] = {
13 { USB_DEVICE(0x0b05, 0x1833) }, /* Asus USB-AC54 */
14 { USB_DEVICE(0x0b05, 0x17eb) }, /* Asus USB-AC55 */
15 { USB_DEVICE(0x0b05, 0x180b) }, /* Asus USB-N53 B1 */
16 { USB_DEVICE(0x0e8d, 0x7612) }, /* Aukey USBAC1200 - Alfa AWUS036ACM */
17 { USB_DEVICE(0x057c, 0x8503) }, /* Avm FRITZ!WLAN AC860 */
18 { USB_DEVICE(0x7392, 0xb711) }, /* Edimax EW 7722 UAC */
19 { USB_DEVICE(0x0e8d, 0x7632) }, /* HC-M7662BU1 */
20 { USB_DEVICE(0x0471, 0x2126) }, /* LiteOn WN4516R module, nonstandard USB connector */
21 { USB_DEVICE(0x0471, 0x7600) }, /* LiteOn WN4519R module, nonstandard USB connector */
22 { USB_DEVICE(0x2c4e, 0x0103) }, /* Mercury UD13 */
23 { USB_DEVICE(0x0846, 0x9053) }, /* Netgear A6210 */
24 { USB_DEVICE(0x045e, 0x02e6) }, /* XBox One Wireless Adapter */
25 { USB_DEVICE(0x045e, 0x02fe) }, /* XBox One Wireless Adapter */
26 { USB_DEVICE(0x2357, 0x0137) }, /* TP-Link TL-WDN6200 */
27 { },
28 };
29
mt76x2u_probe(struct usb_interface * intf,const struct usb_device_id * id)30 static int mt76x2u_probe(struct usb_interface *intf,
31 const struct usb_device_id *id)
32 {
33 static const struct mt76_driver_ops drv_ops = {
34 .drv_flags = MT_DRV_SW_RX_AIRTIME |
35 MT_DRV_IGNORE_TXS_FAILED,
36 .survey_flags = SURVEY_INFO_TIME_TX,
37 .update_survey = mt76x02_update_channel,
38 .tx_prepare_skb = mt76x02u_tx_prepare_skb,
39 .tx_complete_skb = mt76x02u_tx_complete_skb,
40 .tx_status_data = mt76x02_tx_status_data,
41 .rx_skb = mt76x02_queue_rx_skb,
42 .sta_ps = mt76x02_sta_ps,
43 .sta_add = mt76x02_sta_add,
44 .sta_remove = mt76x02_sta_remove,
45 };
46 struct usb_device *udev = interface_to_usbdev(intf);
47 struct mt76x02_dev *dev;
48 struct mt76_dev *mdev;
49 int err;
50
51 mdev = mt76_alloc_device(&intf->dev, sizeof(*dev), &mt76x2u_ops,
52 &drv_ops);
53 if (!mdev)
54 return -ENOMEM;
55
56 dev = container_of(mdev, struct mt76x02_dev, mt76);
57
58 udev = usb_get_dev(udev);
59 usb_reset_device(udev);
60
61 usb_set_intfdata(intf, dev);
62
63 mt76x02u_init_mcu(mdev);
64 err = mt76u_init(mdev, intf);
65 if (err < 0)
66 goto err;
67
68 mdev->rev = mt76_rr(dev, MT_ASIC_VERSION);
69 dev_info(mdev->dev, "ASIC revision: %08x\n", mdev->rev);
70 if (!is_mt76x2(dev)) {
71 err = -ENODEV;
72 goto err;
73 }
74
75 err = mt76x2u_register_device(dev);
76 if (err < 0)
77 goto err;
78
79 return 0;
80
81 err:
82 mt76u_queues_deinit(&dev->mt76);
83 mt76_free_device(&dev->mt76);
84 usb_set_intfdata(intf, NULL);
85 usb_put_dev(udev);
86
87 return err;
88 }
89
mt76x2u_disconnect(struct usb_interface * intf)90 static void mt76x2u_disconnect(struct usb_interface *intf)
91 {
92 struct usb_device *udev = interface_to_usbdev(intf);
93 struct mt76x02_dev *dev = usb_get_intfdata(intf);
94 struct ieee80211_hw *hw = mt76_hw(dev);
95
96 set_bit(MT76_REMOVED, &dev->mphy.state);
97 ieee80211_unregister_hw(hw);
98 mt76x2u_cleanup(dev);
99 mt76_free_device(&dev->mt76);
100 usb_set_intfdata(intf, NULL);
101 usb_put_dev(udev);
102 }
103
mt76x2u_suspend(struct usb_interface * intf,pm_message_t state)104 static int __maybe_unused mt76x2u_suspend(struct usb_interface *intf,
105 pm_message_t state)
106 {
107 struct mt76x02_dev *dev = usb_get_intfdata(intf);
108
109 mt76u_stop_rx(&dev->mt76);
110
111 return 0;
112 }
113
mt76x2u_resume(struct usb_interface * intf)114 static int __maybe_unused mt76x2u_resume(struct usb_interface *intf)
115 {
116 struct mt76x02_dev *dev = usb_get_intfdata(intf);
117 int err;
118
119 err = mt76u_resume_rx(&dev->mt76);
120 if (err < 0)
121 goto err;
122
123 err = mt76x2u_init_hardware(dev);
124 if (err < 0)
125 goto err;
126
127 return 0;
128
129 err:
130 mt76x2u_cleanup(dev);
131 return err;
132 }
133
134 MODULE_DEVICE_TABLE(usb, mt76x2u_device_table);
135 MODULE_FIRMWARE(MT7662_FIRMWARE);
136 MODULE_FIRMWARE(MT7662_ROM_PATCH);
137
138 static struct usb_driver mt76x2u_driver = {
139 .name = KBUILD_MODNAME,
140 .id_table = mt76x2u_device_table,
141 .probe = mt76x2u_probe,
142 .disconnect = mt76x2u_disconnect,
143 #ifdef CONFIG_PM
144 .suspend = mt76x2u_suspend,
145 .resume = mt76x2u_resume,
146 .reset_resume = mt76x2u_resume,
147 #endif /* CONFIG_PM */
148 .soft_unbind = 1,
149 .disable_hub_initiated_lpm = 1,
150 };
151 module_usb_driver(mt76x2u_driver);
152
153 MODULE_AUTHOR("Lorenzo Bianconi <lorenzo.bianconi83@gmail.com>");
154 MODULE_LICENSE("Dual BSD/GPL");
155