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_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 	mt76x02u_init_mcu(mdev);
62 	err = mt76u_init(mdev, intf);
63 	if (err < 0)
64 		goto err;
65 
66 	mdev->rev = mt76_rr(dev, MT_ASIC_VERSION);
67 	dev_info(mdev->dev, "ASIC revision: %08x\n", mdev->rev);
68 	if (!is_mt76x2(dev)) {
69 		err = -ENODEV;
70 		goto err;
71 	}
72 
73 	err = mt76x2u_register_device(dev);
74 	if (err < 0)
75 		goto err;
76 
77 	return 0;
78 
79 err:
80 	ieee80211_free_hw(mt76_hw(dev));
81 	usb_set_intfdata(intf, NULL);
82 	usb_put_dev(udev);
83 
84 	return err;
85 }
86 
87 static void mt76x2u_disconnect(struct usb_interface *intf)
88 {
89 	struct usb_device *udev = interface_to_usbdev(intf);
90 	struct mt76x02_dev *dev = usb_get_intfdata(intf);
91 	struct ieee80211_hw *hw = mt76_hw(dev);
92 
93 	set_bit(MT76_REMOVED, &dev->mt76.state);
94 	ieee80211_unregister_hw(hw);
95 	mt76x2u_cleanup(dev);
96 
97 	ieee80211_free_hw(hw);
98 	usb_set_intfdata(intf, NULL);
99 	usb_put_dev(udev);
100 }
101 
102 static int __maybe_unused mt76x2u_suspend(struct usb_interface *intf,
103 					  pm_message_t state)
104 {
105 	struct mt76x02_dev *dev = usb_get_intfdata(intf);
106 
107 	mt76u_stop_queues(&dev->mt76);
108 	mt76x2u_stop_hw(dev);
109 
110 	return 0;
111 }
112 
113 static int __maybe_unused mt76x2u_resume(struct usb_interface *intf)
114 {
115 	struct mt76x02_dev *dev = usb_get_intfdata(intf);
116 	struct mt76_usb *usb = &dev->mt76.usb;
117 	int err;
118 
119 	err = mt76u_submit_rx_buffers(&dev->mt76);
120 	if (err < 0)
121 		goto err;
122 
123 	tasklet_enable(&usb->rx_tasklet);
124 	tasklet_enable(&usb->tx_tasklet);
125 
126 	err = mt76x2u_init_hardware(dev);
127 	if (err < 0)
128 		goto err;
129 
130 	return 0;
131 
132 err:
133 	mt76x2u_cleanup(dev);
134 	return err;
135 }
136 
137 MODULE_DEVICE_TABLE(usb, mt76x2u_device_table);
138 MODULE_FIRMWARE(MT7662_FIRMWARE);
139 MODULE_FIRMWARE(MT7662_ROM_PATCH);
140 
141 static struct usb_driver mt76x2u_driver = {
142 	.name		= KBUILD_MODNAME,
143 	.id_table	= mt76x2u_device_table,
144 	.probe		= mt76x2u_probe,
145 	.disconnect	= mt76x2u_disconnect,
146 #ifdef CONFIG_PM
147 	.suspend	= mt76x2u_suspend,
148 	.resume		= mt76x2u_resume,
149 	.reset_resume	= mt76x2u_resume,
150 #endif /* CONFIG_PM */
151 	.soft_unbind	= 1,
152 	.disable_hub_initiated_lpm = 1,
153 };
154 module_usb_driver(mt76x2u_driver);
155 
156 MODULE_AUTHOR("Lorenzo Bianconi <lorenzo.bianconi83@gmail.com>");
157 MODULE_LICENSE("Dual BSD/GPL");
158