1 /*
2  * Copyright (C) 2018 Lorenzo Bianconi <lorenzo.bianconi83@gmail.com>
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 
20 #include "../mt76x02_usb.h"
21 #include "mt76x2u.h"
22 
23 static const struct usb_device_id mt76x2u_device_table[] = {
24 	{ USB_DEVICE(0x0b05, 0x1833) },	/* Asus USB-AC54 */
25 	{ USB_DEVICE(0x0b05, 0x17eb) },	/* Asus USB-AC55 */
26 	{ USB_DEVICE(0x0b05, 0x180b) },	/* Asus USB-N53 B1 */
27 	{ USB_DEVICE(0x0e8d, 0x7612) },	/* Aukey USBAC1200 - Alfa AWUS036ACM */
28 	{ USB_DEVICE(0x057c, 0x8503) },	/* Avm FRITZ!WLAN AC860 */
29 	{ USB_DEVICE(0x7392, 0xb711) },	/* Edimax EW 7722 UAC */
30 	{ USB_DEVICE(0x0846, 0x9053) },	/* Netgear A6210 */
31 	{ USB_DEVICE(0x045e, 0x02e6) },	/* XBox One Wireless Adapter */
32 	{ },
33 };
34 
35 static int mt76x2u_probe(struct usb_interface *intf,
36 			 const struct usb_device_id *id)
37 {
38 	static const struct mt76_driver_ops drv_ops = {
39 		.tx_prepare_skb = mt76x02u_tx_prepare_skb,
40 		.tx_complete_skb = mt76x02u_tx_complete_skb,
41 		.tx_status_data = mt76x02_tx_status_data,
42 		.rx_skb = mt76x02_queue_rx_skb,
43 		.sta_ps = mt76x02_sta_ps,
44 		.sta_add = mt76x02_sta_add,
45 		.sta_remove = mt76x02_sta_remove,
46 	};
47 	struct usb_device *udev = interface_to_usbdev(intf);
48 	struct mt76x02_dev *dev;
49 	struct mt76_dev *mdev;
50 	int err;
51 
52 	mdev = mt76_alloc_device(&udev->dev, sizeof(*dev), &mt76x2u_ops,
53 				 &drv_ops);
54 	if (!mdev)
55 		return -ENOMEM;
56 
57 	dev = container_of(mdev, struct mt76x02_dev, mt76);
58 
59 	udev = usb_get_dev(udev);
60 	usb_reset_device(udev);
61 
62 	usb_set_intfdata(intf, dev);
63 
64 	mt76x02u_init_mcu(mdev);
65 	err = mt76u_init(mdev, intf);
66 	if (err < 0)
67 		goto err;
68 
69 	mdev->rev = mt76_rr(dev, MT_ASIC_VERSION);
70 	dev_info(mdev->dev, "ASIC revision: %08x\n", mdev->rev);
71 	if (!is_mt76x2(dev)) {
72 		err = -ENODEV;
73 		goto err;
74 	}
75 
76 	err = mt76x2u_register_device(dev);
77 	if (err < 0)
78 		goto err;
79 
80 	return 0;
81 
82 err:
83 	ieee80211_free_hw(mt76_hw(dev));
84 	usb_set_intfdata(intf, NULL);
85 	usb_put_dev(udev);
86 
87 	return err;
88 }
89 
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->mt76.state);
97 	ieee80211_unregister_hw(hw);
98 	mt76x2u_cleanup(dev);
99 
100 	ieee80211_free_hw(hw);
101 	usb_set_intfdata(intf, NULL);
102 	usb_put_dev(udev);
103 }
104 
105 static int __maybe_unused mt76x2u_suspend(struct usb_interface *intf,
106 					  pm_message_t state)
107 {
108 	struct mt76x02_dev *dev = usb_get_intfdata(intf);
109 
110 	mt76u_stop_rx(&dev->mt76);
111 
112 	return 0;
113 }
114 
115 static int __maybe_unused mt76x2u_resume(struct usb_interface *intf)
116 {
117 	struct mt76x02_dev *dev = usb_get_intfdata(intf);
118 	int err;
119 
120 	err = mt76u_resume_rx(&dev->mt76);
121 	if (err < 0)
122 		goto err;
123 
124 	err = mt76x2u_init_hardware(dev);
125 	if (err < 0)
126 		goto err;
127 
128 	return 0;
129 
130 err:
131 	mt76x2u_cleanup(dev);
132 	return err;
133 }
134 
135 MODULE_DEVICE_TABLE(usb, mt76x2u_device_table);
136 MODULE_FIRMWARE(MT7662_FIRMWARE);
137 MODULE_FIRMWARE(MT7662_ROM_PATCH);
138 
139 static struct usb_driver mt76x2u_driver = {
140 	.name		= KBUILD_MODNAME,
141 	.id_table	= mt76x2u_device_table,
142 	.probe		= mt76x2u_probe,
143 	.disconnect	= mt76x2u_disconnect,
144 #ifdef CONFIG_PM
145 	.suspend	= mt76x2u_suspend,
146 	.resume		= mt76x2u_resume,
147 	.reset_resume	= mt76x2u_resume,
148 #endif /* CONFIG_PM */
149 	.soft_unbind	= 1,
150 	.disable_hub_initiated_lpm = 1,
151 };
152 module_usb_driver(mt76x2u_driver);
153 
154 MODULE_AUTHOR("Lorenzo Bianconi <lorenzo.bianconi83@gmail.com>");
155 MODULE_LICENSE("Dual BSD/GPL");
156