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(0x0e8d, 0x7612) }, /* Alfa AWUS036ACM */
25 	{ USB_DEVICE(0x0b05, 0x1833) },	/* Asus USB-AC54 */
26 	{ USB_DEVICE(0x0b05, 0x17eb) },	/* Asus USB-AC55 */
27 	{ USB_DEVICE(0x0b05, 0x180b) },	/* Asus USB-N53 B1 */
28 	{ USB_DEVICE(0x0e8d, 0x7612) },	/* Aukey USB-AC1200 */
29 	{ USB_DEVICE(0x057c, 0x8503) },	/* Avm FRITZ!WLAN AC860 */
30 	{ USB_DEVICE(0x7392, 0xb711) },	/* Edimax EW 7722 UAC */
31 	{ USB_DEVICE(0x0846, 0x9053) },	/* Netgear A6210 */
32 	{ USB_DEVICE(0x045e, 0x02e6) },	/* XBox One Wireless Adapter */
33 	{ },
34 };
35 
36 static int mt76x2u_probe(struct usb_interface *intf,
37 			 const struct usb_device_id *id)
38 {
39 	struct usb_device *udev = interface_to_usbdev(intf);
40 	struct mt76x02_dev *dev;
41 	int err;
42 
43 	dev = mt76x2u_alloc_device(&intf->dev);
44 	if (!dev)
45 		return -ENOMEM;
46 
47 	udev = usb_get_dev(udev);
48 	usb_reset_device(udev);
49 
50 	mt76x02u_init_mcu(&dev->mt76);
51 	err = mt76u_init(&dev->mt76, intf);
52 	if (err < 0)
53 		goto err;
54 
55 	dev->mt76.rev = mt76_rr(dev, MT_ASIC_VERSION);
56 	dev_info(dev->mt76.dev, "ASIC revision: %08x\n", dev->mt76.rev);
57 
58 	err = mt76x2u_register_device(dev);
59 	if (err < 0)
60 		goto err;
61 
62 	return 0;
63 
64 err:
65 	ieee80211_free_hw(mt76_hw(dev));
66 	usb_set_intfdata(intf, NULL);
67 	usb_put_dev(udev);
68 
69 	return err;
70 }
71 
72 static void mt76x2u_disconnect(struct usb_interface *intf)
73 {
74 	struct usb_device *udev = interface_to_usbdev(intf);
75 	struct mt76x02_dev *dev = usb_get_intfdata(intf);
76 	struct ieee80211_hw *hw = mt76_hw(dev);
77 
78 	set_bit(MT76_REMOVED, &dev->mt76.state);
79 	ieee80211_unregister_hw(hw);
80 	mt76x2u_cleanup(dev);
81 
82 	ieee80211_free_hw(hw);
83 	usb_set_intfdata(intf, NULL);
84 	usb_put_dev(udev);
85 }
86 
87 static int __maybe_unused mt76x2u_suspend(struct usb_interface *intf,
88 					  pm_message_t state)
89 {
90 	struct mt76x02_dev *dev = usb_get_intfdata(intf);
91 	struct mt76_usb *usb = &dev->mt76.usb;
92 
93 	mt76u_stop_queues(&dev->mt76);
94 	mt76x2u_stop_hw(dev);
95 	usb_kill_urb(usb->mcu.res.urb);
96 
97 	return 0;
98 }
99 
100 static int __maybe_unused mt76x2u_resume(struct usb_interface *intf)
101 {
102 	struct mt76x02_dev *dev = usb_get_intfdata(intf);
103 	struct mt76_usb *usb = &dev->mt76.usb;
104 	int err;
105 
106 	reinit_completion(&usb->mcu.cmpl);
107 	err = mt76u_submit_buf(&dev->mt76, USB_DIR_IN,
108 			       MT_EP_IN_CMD_RESP,
109 			       &usb->mcu.res, GFP_KERNEL,
110 			       mt76u_mcu_complete_urb,
111 			       &usb->mcu.cmpl);
112 	if (err < 0)
113 		goto err;
114 
115 	err = mt76u_submit_rx_buffers(&dev->mt76);
116 	if (err < 0)
117 		goto err;
118 
119 	tasklet_enable(&usb->rx_tasklet);
120 	tasklet_enable(&usb->tx_tasklet);
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 <lorenzo.bianconi83@gmail.com>");
153 MODULE_LICENSE("Dual BSD/GPL");
154