1 // SPDX-License-Identifier: ISC
2 /*
3  * Copyright (C) 2018 Lorenzo Bianconi <[email protected]>
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(0x2c4e, 0x0103) },	/* Mercury UD13 */
21 	{ USB_DEVICE(0x0846, 0x9014) },	/* Netgear WNDA3100v3 */
22 	{ USB_DEVICE(0x0846, 0x9053) },	/* Netgear A6210 */
23 	{ USB_DEVICE(0x045e, 0x02e6) },	/* XBox One Wireless Adapter */
24 	{ USB_DEVICE(0x045e, 0x02fe) },	/* XBox One Wireless Adapter */
25 	{ USB_DEVICE(0x2357, 0x0137) },	/* TP-Link TL-WDN6200 */
26 	{ },
27 };
28 
mt76x2u_probe(struct usb_interface * intf,const struct usb_device_id * id)29 static int mt76x2u_probe(struct usb_interface *intf,
30 			 const struct usb_device_id *id)
31 {
32 	static const struct mt76_driver_ops drv_ops = {
33 		.drv_flags = MT_DRV_SW_RX_AIRTIME,
34 		.survey_flags = SURVEY_INFO_TIME_TX,
35 		.update_survey = mt76x02_update_channel,
36 		.set_channel = mt76x2u_set_channel,
37 		.tx_prepare_skb = mt76x02u_tx_prepare_skb,
38 		.tx_complete_skb = mt76x02u_tx_complete_skb,
39 		.tx_status_data = mt76x02_tx_status_data,
40 		.rx_skb = mt76x02_queue_rx_skb,
41 		.sta_ps = mt76x02_sta_ps,
42 		.sta_add = mt76x02_sta_add,
43 		.sta_remove = mt76x02_sta_remove,
44 	};
45 	struct usb_device *udev = interface_to_usbdev(intf);
46 	struct mt76x02_dev *dev;
47 	struct mt76_dev *mdev;
48 	int err;
49 
50 	mdev = mt76_alloc_device(&intf->dev, sizeof(*dev), &mt76x2u_ops,
51 				 &drv_ops);
52 	if (!mdev)
53 		return -ENOMEM;
54 
55 	dev = container_of(mdev, struct mt76x02_dev, mt76);
56 
57 	udev = usb_get_dev(udev);
58 	usb_reset_device(udev);
59 
60 	usb_set_intfdata(intf, dev);
61 
62 	mt76x02u_init_mcu(mdev);
63 	err = mt76u_init(mdev, intf);
64 	if (err < 0)
65 		goto err;
66 
67 	mdev->rev = mt76_rr(dev, MT_ASIC_VERSION);
68 	dev_info(mdev->dev, "ASIC revision: %08x\n", mdev->rev);
69 	if (!is_mt76x2(dev)) {
70 		err = -ENODEV;
71 		goto err;
72 	}
73 
74 	err = mt76x2u_register_device(dev);
75 	if (err < 0)
76 		goto err;
77 
78 	return 0;
79 
80 err:
81 	mt76u_queues_deinit(&dev->mt76);
82 	mt76_free_device(&dev->mt76);
83 	usb_set_intfdata(intf, NULL);
84 	usb_put_dev(udev);
85 
86 	return err;
87 }
88 
mt76x2u_disconnect(struct usb_interface * intf)89 static void mt76x2u_disconnect(struct usb_interface *intf)
90 {
91 	struct usb_device *udev = interface_to_usbdev(intf);
92 	struct mt76x02_dev *dev = usb_get_intfdata(intf);
93 	struct ieee80211_hw *hw = mt76_hw(dev);
94 
95 	set_bit(MT76_REMOVED, &dev->mphy.state);
96 	ieee80211_unregister_hw(hw);
97 	mt76x2u_cleanup(dev);
98 	mt76_free_device(&dev->mt76);
99 	usb_set_intfdata(intf, NULL);
100 	usb_put_dev(udev);
101 }
102 
mt76x2u_suspend(struct usb_interface * intf,pm_message_t state)103 static int __maybe_unused mt76x2u_suspend(struct usb_interface *intf,
104 					  pm_message_t state)
105 {
106 	struct mt76x02_dev *dev = usb_get_intfdata(intf);
107 
108 	mt76u_stop_rx(&dev->mt76);
109 
110 	return 0;
111 }
112 
mt76x2u_resume(struct usb_interface * intf)113 static int __maybe_unused mt76x2u_resume(struct usb_interface *intf)
114 {
115 	struct mt76x02_dev *dev = usb_get_intfdata(intf);
116 	int err;
117 
118 	err = mt76u_resume_rx(&dev->mt76);
119 	if (err < 0)
120 		goto err;
121 
122 	err = mt76x2u_init_hardware(dev);
123 	if (err < 0)
124 		goto err;
125 
126 	return 0;
127 
128 err:
129 	mt76x2u_cleanup(dev);
130 	return err;
131 }
132 
133 MODULE_DEVICE_TABLE(usb, mt76x2u_device_table);
134 MODULE_FIRMWARE(MT7662_FIRMWARE);
135 MODULE_FIRMWARE(MT7662_ROM_PATCH);
136 
137 static struct usb_driver mt76x2u_driver = {
138 	.name		= KBUILD_MODNAME,
139 	.id_table	= mt76x2u_device_table,
140 	.probe		= mt76x2u_probe,
141 	.disconnect	= mt76x2u_disconnect,
142 #ifdef CONFIG_PM
143 	.suspend	= mt76x2u_suspend,
144 	.resume		= mt76x2u_resume,
145 	.reset_resume	= mt76x2u_resume,
146 #endif /* CONFIG_PM */
147 	.soft_unbind	= 1,
148 	.disable_hub_initiated_lpm = 1,
149 };
150 module_usb_driver(mt76x2u_driver);
151 
152 MODULE_AUTHOR("Lorenzo Bianconi <[email protected]>");
153 MODULE_DESCRIPTION("MediaTek MT76x2U (USB) wireless driver");
154 MODULE_LICENSE("Dual BSD/GPL");
155