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