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