2 * Copyright (C) 2015 Jakub Kicinski <kubakici@wp.pl>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2
6 * as published by the Free Software Foundation
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/usb.h>
22 static struct usb_device_id mt76x0_device_table[] = {
23 { USB_DEVICE(0x148F, 0x7610) }, /* MT7610U */
24 { USB_DEVICE(0x13B1, 0x003E) }, /* Linksys AE6000 */
25 { USB_DEVICE(0x0E8D, 0x7610) }, /* Sabrent NTWLAC */
26 { USB_DEVICE(0x7392, 0xa711) }, /* Edimax 7711mac */
27 { USB_DEVICE(0x7392, 0xb711) }, /* Edimax / Elecom */
28 { USB_DEVICE(0x148f, 0x761a) }, /* TP-Link TL-WDN5200 */
29 { USB_DEVICE(0x148f, 0x760a) }, /* TP-Link unknown */
30 { USB_DEVICE(0x0b05, 0x17d1) }, /* Asus USB-AC51 */
31 { USB_DEVICE(0x0b05, 0x17db) }, /* Asus USB-AC50 */
32 { USB_DEVICE(0x0df6, 0x0075) }, /* Sitecom WLA-3100 */
33 { USB_DEVICE(0x2019, 0xab31) }, /* Planex GW-450D */
34 { USB_DEVICE(0x2001, 0x3d02) }, /* D-LINK DWA-171 rev B1 */
35 { USB_DEVICE(0x0586, 0x3425) }, /* Zyxel NWD6505 */
36 { USB_DEVICE(0x07b8, 0x7610) }, /* AboCom AU7212 */
37 { USB_DEVICE(0x04bb, 0x0951) }, /* I-O DATA WN-AC433UK */
38 { USB_DEVICE(0x057c, 0x8502) }, /* AVM FRITZ!WLAN USB Stick AC 430 */
39 { USB_DEVICE(0x293c, 0x5702) }, /* Comcast Xfinity KXW02AAA */
40 { USB_DEVICE(0x20f4, 0x806b) }, /* TRENDnet TEW-806UBH */
41 { USB_DEVICE(0x7392, 0xc711) }, /* Devolo Wifi ac Stick */
42 { USB_DEVICE(0x0df6, 0x0079) }, /* Sitecom Europe B.V. ac Stick */
43 { USB_DEVICE(0x2357, 0x0105) }, /* TP-LINK Archer T1U */
44 { USB_DEVICE_AND_INTERFACE_INFO(0x0E8D, 0x7630, 0xff, 0x2, 0xff)}, /* MT7630U */
45 { USB_DEVICE_AND_INTERFACE_INFO(0x0E8D, 0x7650, 0xff, 0x2, 0xff)}, /* MT7650U */
49 bool mt76x0_usb_alloc_buf(struct mt76x0_dev *dev, size_t len,
50 struct mt76x0_dma_buf *buf)
52 struct usb_device *usb_dev = mt76x0_to_usb_dev(dev);
55 buf->urb = usb_alloc_urb(0, GFP_KERNEL);
56 buf->buf = usb_alloc_coherent(usb_dev, buf->len, GFP_KERNEL, &buf->dma);
58 return !buf->urb || !buf->buf;
61 void mt76x0_usb_free_buf(struct mt76x0_dev *dev, struct mt76x0_dma_buf *buf)
63 struct usb_device *usb_dev = mt76x0_to_usb_dev(dev);
65 usb_free_coherent(usb_dev, buf->len, buf->buf, buf->dma);
66 usb_free_urb(buf->urb);
69 int mt76x0_usb_submit_buf(struct mt76x0_dev *dev, int dir, int ep_idx,
70 struct mt76x0_dma_buf *buf, gfp_t gfp,
71 usb_complete_t complete_fn, void *context)
73 struct usb_device *usb_dev = mt76x0_to_usb_dev(dev);
77 if (dir == USB_DIR_IN)
78 pipe = usb_rcvbulkpipe(usb_dev, dev->in_ep[ep_idx]);
80 pipe = usb_sndbulkpipe(usb_dev, dev->out_ep[ep_idx]);
82 usb_fill_bulk_urb(buf->urb, usb_dev, pipe, buf->buf, buf->len,
83 complete_fn, context);
84 buf->urb->transfer_dma = buf->dma;
85 buf->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
87 trace_mt76x0_submit_urb(&dev->mt76, buf->urb);
88 ret = usb_submit_urb(buf->urb, gfp);
90 dev_err(dev->mt76.dev, "Error: submit URB dir:%d ep:%d failed:%d\n",
95 void mt76x0_complete_urb(struct urb *urb)
97 struct completion *cmpl = urb->context;
102 int mt76x0_vendor_request(struct mt76x0_dev *dev, const u8 req,
103 const u8 direction, const u16 val, const u16 offset,
104 void *buf, const size_t buflen)
107 struct usb_device *usb_dev = mt76x0_to_usb_dev(dev);
108 const u8 req_type = direction | USB_TYPE_VENDOR | USB_RECIP_DEVICE;
109 const unsigned int pipe = (direction == USB_DIR_IN) ?
110 usb_rcvctrlpipe(usb_dev, 0) : usb_sndctrlpipe(usb_dev, 0);
112 for (i = 0; i < MT_VEND_REQ_MAX_RETRY; i++) {
113 ret = usb_control_msg(usb_dev, pipe, req, req_type,
114 val, offset, buf, buflen,
115 MT_VEND_REQ_TOUT_MS);
116 trace_mt76x0_vend_req(&dev->mt76, pipe, req, req_type, val, offset,
120 set_bit(MT76_REMOVED, &dev->mt76.state);
121 if (ret >= 0 || ret == -ENODEV)
127 dev_err(dev->mt76.dev, "Vendor request req:%02x off:%04x failed:%d\n",
133 void mt76x0_vendor_reset(struct mt76x0_dev *dev)
135 mt76x0_vendor_request(dev, MT_VEND_DEV_MODE, USB_DIR_OUT,
136 MT_VEND_DEV_MODE_RESET, 0, NULL, 0);
139 static u32 mt76x0_rr(struct mt76_dev *dev, u32 offset)
141 struct mt76x0_dev *mdev = (struct mt76x0_dev *) dev;
145 WARN_ONCE(offset > USHRT_MAX, "read high off:%08x", offset);
147 mutex_lock(&mdev->usb_ctrl_mtx);
149 ret = mt76x0_vendor_request((struct mt76x0_dev *)dev, MT_VEND_MULTI_READ, USB_DIR_IN,
150 0, offset, mdev->data, MT_VEND_BUF);
151 if (ret == MT_VEND_BUF)
152 val = get_unaligned_le32(mdev->data);
154 dev_err(dev->dev, "Error: wrong size read:%d off:%08x\n",
157 mutex_unlock(&mdev->usb_ctrl_mtx);
159 trace_mt76x0_reg_read(dev, offset, val);
163 int mt76x0_vendor_single_wr(struct mt76x0_dev *dev, const u8 req,
164 const u16 offset, const u32 val)
166 struct mt76x0_dev *mdev = dev;
169 mutex_lock(&mdev->usb_ctrl_mtx);
171 ret = mt76x0_vendor_request(dev, req, USB_DIR_OUT,
172 val & 0xffff, offset, NULL, 0);
174 ret = mt76x0_vendor_request(dev, req, USB_DIR_OUT,
175 val >> 16, offset + 2, NULL, 0);
177 mutex_unlock(&mdev->usb_ctrl_mtx);
182 static void mt76x0_wr(struct mt76_dev *dev, u32 offset, u32 val)
184 struct mt76x0_dev *mdev = (struct mt76x0_dev *) dev;
187 WARN_ONCE(offset > USHRT_MAX, "write high off:%08x", offset);
189 mutex_lock(&mdev->usb_ctrl_mtx);
191 put_unaligned_le32(val, mdev->data);
192 ret = mt76x0_vendor_request(mdev, MT_VEND_MULTI_WRITE, USB_DIR_OUT,
193 0, offset, mdev->data, MT_VEND_BUF);
194 trace_mt76x0_reg_write(dev, offset, val);
196 mutex_unlock(&mdev->usb_ctrl_mtx);
199 static u32 mt76x0_rmw(struct mt76_dev *dev, u32 offset, u32 mask, u32 val)
201 val |= mt76x0_rr(dev, offset) & ~mask;
202 mt76x0_wr(dev, offset, val);
206 static void mt76x0_wr_copy(struct mt76_dev *dev, u32 offset,
207 const void *data, int len)
209 WARN_ONCE(offset & 3, "unaligned write copy off:%08x", offset);
210 WARN_ONCE(len & 3, "short write copy off:%08x", offset);
212 mt76x0_burst_write_regs((struct mt76x0_dev *) dev, offset, data, len / 4);
215 void mt76x0_addr_wr(struct mt76x0_dev *dev, const u32 offset, const u8 *addr)
217 mt76_wr(dev, offset, get_unaligned_le32(addr));
218 mt76_wr(dev, offset + 4, addr[4] | addr[5] << 8);
221 static int mt76x0_assign_pipes(struct usb_interface *usb_intf,
222 struct mt76x0_dev *dev)
224 struct usb_endpoint_descriptor *ep_desc;
225 struct usb_host_interface *intf_desc = usb_intf->cur_altsetting;
226 unsigned i, ep_i = 0, ep_o = 0;
228 BUILD_BUG_ON(sizeof(dev->in_ep) < __MT_EP_IN_MAX);
229 BUILD_BUG_ON(sizeof(dev->out_ep) < __MT_EP_OUT_MAX);
231 for (i = 0; i < intf_desc->desc.bNumEndpoints; i++) {
232 ep_desc = &intf_desc->endpoint[i].desc;
234 if (usb_endpoint_is_bulk_in(ep_desc) &&
235 ep_i++ < __MT_EP_IN_MAX) {
236 dev->in_ep[ep_i - 1] = usb_endpoint_num(ep_desc);
237 dev->in_max_packet = usb_endpoint_maxp(ep_desc);
238 /* Note: this is ignored by usb sub-system but vendor
239 * code does it. We can drop this at some point.
241 dev->in_ep[ep_i - 1] |= USB_DIR_IN;
242 } else if (usb_endpoint_is_bulk_out(ep_desc) &&
243 ep_o++ < __MT_EP_OUT_MAX) {
244 dev->out_ep[ep_o - 1] = usb_endpoint_num(ep_desc);
245 dev->out_max_packet = usb_endpoint_maxp(ep_desc);
249 if (ep_i != __MT_EP_IN_MAX || ep_o != __MT_EP_OUT_MAX) {
250 dev_err(dev->mt76.dev, "Error: wrong pipe number in:%d out:%d\n",
258 static int mt76x0_probe(struct usb_interface *usb_intf,
259 const struct usb_device_id *id)
261 struct usb_device *usb_dev = interface_to_usbdev(usb_intf);
262 struct mt76x0_dev *dev;
263 u32 asic_rev, mac_rev;
265 static const struct mt76_bus_ops usb_ops = {
269 .copy = mt76x0_wr_copy,
272 dev = mt76x0_alloc_device(&usb_intf->dev);
276 usb_dev = usb_get_dev(usb_dev);
277 usb_reset_device(usb_dev);
279 usb_set_intfdata(usb_intf, dev);
281 dev->mt76.bus = &usb_ops;
283 ret = mt76x0_assign_pipes(usb_intf, dev);
287 /* Disable the HW, otherwise MCU fail to initalize on hot reboot */
288 mt76x0_chip_onoff(dev, false, false);
290 ret = mt76x0_wait_asic_ready(dev);
294 asic_rev = mt76_rr(dev, MT_ASIC_VERSION);
295 mac_rev = mt76_rr(dev, MT_MAC_CSR0);
296 dev_info(dev->mt76.dev, "ASIC revision: %08x MAC revision: %08x\n",
299 /* Note: vendor driver skips this check for MT76X0U */
300 if (!(mt76_rr(dev, MT_EFUSE_CTRL) & MT_EFUSE_CTRL_SEL))
301 dev_warn(dev->mt76.dev, "Warning: eFUSE not present\n");
303 ret = mt76x0_init_hardware(dev, true);
307 ret = mt76x0_register_device(dev);
311 set_bit(MT76_STATE_INITIALIZED, &dev->mt76.state);
317 usb_set_intfdata(usb_intf, NULL);
318 usb_put_dev(interface_to_usbdev(usb_intf));
320 destroy_workqueue(dev->stat_wq);
321 ieee80211_free_hw(dev->mt76.hw);
325 static void mt76x0_disconnect(struct usb_interface *usb_intf)
327 struct mt76x0_dev *dev = usb_get_intfdata(usb_intf);
328 bool initalized = test_bit(MT76_STATE_INITIALIZED, &dev->mt76.state);
333 ieee80211_unregister_hw(dev->mt76.hw);
336 usb_set_intfdata(usb_intf, NULL);
337 usb_put_dev(interface_to_usbdev(usb_intf));
339 destroy_workqueue(dev->stat_wq);
340 ieee80211_free_hw(dev->mt76.hw);
343 static int mt76x0_suspend(struct usb_interface *usb_intf, pm_message_t state)
345 struct mt76x0_dev *dev = usb_get_intfdata(usb_intf);
352 static int mt76x0_resume(struct usb_interface *usb_intf)
354 struct mt76x0_dev *dev = usb_get_intfdata(usb_intf);
357 ret = mt76x0_init_hardware(dev, false);
361 set_bit(MT76_STATE_INITIALIZED, &dev->mt76.state);
366 MODULE_DEVICE_TABLE(usb, mt76x0_device_table);
368 MODULE_LICENSE("GPL");
370 static struct usb_driver mt76x0_driver = {
371 .name = KBUILD_MODNAME,
372 .id_table = mt76x0_device_table,
373 .probe = mt76x0_probe,
374 .disconnect = mt76x0_disconnect,
375 .suspend = mt76x0_suspend,
376 .resume = mt76x0_resume,
377 .reset_resume = mt76x0_resume,
379 .disable_hub_initiated_lpm = 1,
381 module_usb_driver(mt76x0_driver);