GNU Linux-libre 4.19.286-gnu1
[releases.git] / drivers / net / wireless / mediatek / mt76 / mt76x2_usb.c
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 "mt76x2u.h"
21
22 static const struct usb_device_id mt76x2u_device_table[] = {
23         { USB_DEVICE(0x0b05, 0x1833) }, /* Asus USB-AC54 */
24         { USB_DEVICE(0x0b05, 0x17eb) }, /* Asus USB-AC55 */
25         { USB_DEVICE(0x0b05, 0x180b) }, /* Asus USB-N53 B1 */
26         { USB_DEVICE(0x0e8d, 0x7612) }, /* Aukey USB-AC1200 */
27         { USB_DEVICE(0x057c, 0x8503) }, /* Avm FRITZ!WLAN AC860 */
28         { USB_DEVICE(0x7392, 0xb711) }, /* Edimax EW 7722 UAC */
29         { USB_DEVICE(0x0846, 0x9053) }, /* Netgear A6210 */
30         { USB_DEVICE(0x045e, 0x02e6) }, /* XBox One Wireless Adapter */
31         { },
32 };
33
34 static int mt76x2u_probe(struct usb_interface *intf,
35                          const struct usb_device_id *id)
36 {
37         struct usb_device *udev = interface_to_usbdev(intf);
38         struct mt76x2_dev *dev;
39         int err;
40
41         dev = mt76x2u_alloc_device(&intf->dev);
42         if (!dev)
43                 return -ENOMEM;
44
45         udev = usb_get_dev(udev);
46         usb_reset_device(udev);
47
48         err = mt76u_init(&dev->mt76, intf);
49         if (err < 0)
50                 goto err;
51
52         dev->mt76.rev = mt76_rr(dev, MT_ASIC_VERSION);
53         dev_info(dev->mt76.dev, "ASIC revision: %08x\n", dev->mt76.rev);
54
55         err = mt76x2u_register_device(dev);
56         if (err < 0)
57                 goto err;
58
59         return 0;
60
61 err:
62         ieee80211_free_hw(mt76_hw(dev));
63         usb_set_intfdata(intf, NULL);
64         usb_put_dev(udev);
65
66         return err;
67 }
68
69 static void mt76x2u_disconnect(struct usb_interface *intf)
70 {
71         struct usb_device *udev = interface_to_usbdev(intf);
72         struct mt76x2_dev *dev = usb_get_intfdata(intf);
73         struct ieee80211_hw *hw = mt76_hw(dev);
74
75         set_bit(MT76_REMOVED, &dev->mt76.state);
76         ieee80211_unregister_hw(hw);
77         mt76x2u_cleanup(dev);
78
79         ieee80211_free_hw(hw);
80         usb_set_intfdata(intf, NULL);
81         usb_put_dev(udev);
82 }
83
84 static int __maybe_unused mt76x2u_suspend(struct usb_interface *intf,
85                                           pm_message_t state)
86 {
87         struct mt76x2_dev *dev = usb_get_intfdata(intf);
88         struct mt76_usb *usb = &dev->mt76.usb;
89
90         mt76u_stop_queues(&dev->mt76);
91         mt76x2u_stop_hw(dev);
92         usb_kill_urb(usb->mcu.res.urb);
93
94         return 0;
95 }
96
97 static int __maybe_unused mt76x2u_resume(struct usb_interface *intf)
98 {
99         struct mt76x2_dev *dev = usb_get_intfdata(intf);
100         struct mt76_usb *usb = &dev->mt76.usb;
101         int err;
102
103         reinit_completion(&usb->mcu.cmpl);
104         err = mt76u_submit_buf(&dev->mt76, USB_DIR_IN,
105                                MT_EP_IN_CMD_RESP,
106                                &usb->mcu.res, GFP_KERNEL,
107                                mt76u_mcu_complete_urb,
108                                &usb->mcu.cmpl);
109         if (err < 0)
110                 goto err;
111
112         err = mt76u_submit_rx_buffers(&dev->mt76);
113         if (err < 0)
114                 goto err;
115
116         tasklet_enable(&usb->rx_tasklet);
117         tasklet_enable(&usb->tx_tasklet);
118
119         err = mt76x2u_init_hardware(dev);
120         if (err < 0)
121                 goto err;
122
123         return 0;
124
125 err:
126         mt76x2u_cleanup(dev);
127         return err;
128 }
129
130 MODULE_DEVICE_TABLE(usb, mt76x2u_device_table);
131 /*(DEBLOBBED)*/
132
133 static struct usb_driver mt76x2u_driver = {
134         .name           = KBUILD_MODNAME,
135         .id_table       = mt76x2u_device_table,
136         .probe          = mt76x2u_probe,
137         .disconnect     = mt76x2u_disconnect,
138 #ifdef CONFIG_PM
139         .suspend        = mt76x2u_suspend,
140         .resume         = mt76x2u_resume,
141         .reset_resume   = mt76x2u_resume,
142 #endif /* CONFIG_PM */
143         .soft_unbind    = 1,
144         .disable_hub_initiated_lpm = 1,
145 };
146 module_usb_driver(mt76x2u_driver);
147
148 MODULE_AUTHOR("Lorenzo Bianconi <lorenzo.bianconi83@gmail.com>");
149 MODULE_LICENSE("Dual BSD/GPL");