aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStanislaw Gruszka <sgruszka@redhat.com>2018-07-31 08:40:53 -0400
committerKalle Valo <kvalo@codeaurora.org>2018-08-02 14:50:46 -0400
commitff69c75ee5392320ab3a8dd01db46d3cd097eb46 (patch)
treef7936c92211bb2ca4645b5aa85f31bd20873f2b8
parenta774434981372fc583dfdf39a4094b3f0e3c4677 (diff)
mt76x0: usb files
Add usb files of mt76x0 driver. Signed-off-by: Stanislaw Gruszka <sgruszka@redhat.com> Signed-off-by: Kalle Valo <kvalo@codeaurora.org>
-rw-r--r--drivers/net/wireless/mediatek/mt76/mt76x0/usb.c377
-rw-r--r--drivers/net/wireless/mediatek/mt76/mt76x0/usb.h61
2 files changed, 438 insertions, 0 deletions
diff --git a/drivers/net/wireless/mediatek/mt76/mt76x0/usb.c b/drivers/net/wireless/mediatek/mt76/mt76x0/usb.c
new file mode 100644
index 000000000000..0871fdef59c3
--- /dev/null
+++ b/drivers/net/wireless/mediatek/mt76/mt76x0/usb.c
@@ -0,0 +1,377 @@
1/*
2 * Copyright (C) 2015 Jakub Kicinski <kubakici@wp.pl>
3 *
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
7 *
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.
12 */
13
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/usb.h>
17
18#include "mt76x0.h"
19#include "usb.h"
20#include "trace.h"
21
22static 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 */
46 { 0, }
47};
48
49bool mt76x0_usb_alloc_buf(struct mt76x0_dev *dev, size_t len,
50 struct mt76x0_dma_buf *buf)
51{
52 struct usb_device *usb_dev = mt76x0_to_usb_dev(dev);
53
54 buf->len = len;
55 buf->urb = usb_alloc_urb(0, GFP_KERNEL);
56 buf->buf = usb_alloc_coherent(usb_dev, buf->len, GFP_KERNEL, &buf->dma);
57
58 return !buf->urb || !buf->buf;
59}
60
61void mt76x0_usb_free_buf(struct mt76x0_dev *dev, struct mt76x0_dma_buf *buf)
62{
63 struct usb_device *usb_dev = mt76x0_to_usb_dev(dev);
64
65 usb_free_coherent(usb_dev, buf->len, buf->buf, buf->dma);
66 usb_free_urb(buf->urb);
67}
68
69int 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)
72{
73 struct usb_device *usb_dev = mt76x0_to_usb_dev(dev);
74 unsigned pipe;
75 int ret;
76
77 if (dir == USB_DIR_IN)
78 pipe = usb_rcvbulkpipe(usb_dev, dev->in_ep[ep_idx]);
79 else
80 pipe = usb_sndbulkpipe(usb_dev, dev->out_ep[ep_idx]);
81
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;
86
87 trace_mt_submit_urb(&dev->mt76, buf->urb);
88 ret = usb_submit_urb(buf->urb, gfp);
89 if (ret)
90 dev_err(dev->mt76.dev, "Error: submit URB dir:%d ep:%d failed:%d\n",
91 dir, ep_idx, ret);
92 return ret;
93}
94
95void mt76x0_complete_urb(struct urb *urb)
96{
97 struct completion *cmpl = urb->context;
98
99 complete(cmpl);
100}
101
102int 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)
105{
106 int i, ret;
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);
111
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_mt_vend_req(&dev->mt76, pipe, req, req_type, val, offset,
117 buf, buflen, ret);
118
119 if (ret == -ENODEV)
120 set_bit(MT76_REMOVED, &dev->mt76.state);
121 if (ret >= 0 || ret == -ENODEV)
122 return ret;
123
124 msleep(5);
125 }
126
127 dev_err(dev->mt76.dev, "Vendor request req:%02x off:%04x failed:%d\n",
128 req, offset, ret);
129
130 return ret;
131}
132
133void mt76x0_vendor_reset(struct mt76x0_dev *dev)
134{
135 mt76x0_vendor_request(dev, MT_VEND_DEV_MODE, USB_DIR_OUT,
136 MT_VEND_DEV_MODE_RESET, 0, NULL, 0);
137}
138
139static u32 mt76x0_rr(struct mt76_dev *dev, u32 offset)
140{
141 struct mt76x0_dev *mdev = (struct mt76x0_dev *) dev;
142 int ret;
143 u32 val = ~0;
144
145 WARN_ONCE(offset > USHRT_MAX, "read high off:%08x", offset);
146
147 mutex_lock(&mdev->usb_ctrl_mtx);
148
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);
153 else if (ret > 0)
154 dev_err(dev->dev, "Error: wrong size read:%d off:%08x\n",
155 ret, offset);
156
157 mutex_unlock(&mdev->usb_ctrl_mtx);
158
159 trace_reg_read(dev, offset, val);
160 return val;
161}
162
163int mt76x0_vendor_single_wr(struct mt76x0_dev *dev, const u8 req,
164 const u16 offset, const u32 val)
165{
166 struct mt76x0_dev *mdev = dev;
167 int ret;
168
169 mutex_lock(&mdev->usb_ctrl_mtx);
170
171 ret = mt76x0_vendor_request(dev, req, USB_DIR_OUT,
172 val & 0xffff, offset, NULL, 0);
173 if (!ret)
174 ret = mt76x0_vendor_request(dev, req, USB_DIR_OUT,
175 val >> 16, offset + 2, NULL, 0);
176
177 mutex_unlock(&mdev->usb_ctrl_mtx);
178
179 return ret;
180}
181
182static void mt76x0_wr(struct mt76_dev *dev, u32 offset, u32 val)
183{
184 struct mt76x0_dev *mdev = (struct mt76x0_dev *) dev;
185 int ret;
186
187 WARN_ONCE(offset > USHRT_MAX, "write high off:%08x", offset);
188
189 mutex_lock(&mdev->usb_ctrl_mtx);
190
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_reg_write(dev, offset, val);
195
196 mutex_unlock(&mdev->usb_ctrl_mtx);
197}
198
199static u32 mt76x0_rmw(struct mt76_dev *dev, u32 offset, u32 mask, u32 val)
200{
201 val |= mt76x0_rr(dev, offset) & ~mask;
202 mt76x0_wr(dev, offset, val);
203 return val;
204}
205
206static void mt76x0_wr_copy(struct mt76_dev *dev, u32 offset,
207 const void *data, int len)
208{
209 WARN_ONCE(offset & 3, "unaligned write copy off:%08x", offset);
210 WARN_ONCE(len & 3, "short write copy off:%08x", offset);
211
212 mt76x0_burst_write_regs((struct mt76x0_dev *) dev, offset, data, len / 4);
213}
214
215void mt76x0_addr_wr(struct mt76x0_dev *dev, const u32 offset, const u8 *addr)
216{
217 mt76_wr(dev, offset, get_unaligned_le32(addr));
218 mt76_wr(dev, offset + 4, addr[4] | addr[5] << 8);
219}
220
221static int mt76x0_assign_pipes(struct usb_interface *usb_intf,
222 struct mt76x0_dev *dev)
223{
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;
227
228 BUILD_BUG_ON(sizeof(dev->in_ep) < __MT_EP_IN_MAX);
229 BUILD_BUG_ON(sizeof(dev->out_ep) < __MT_EP_OUT_MAX);
230
231 for (i = 0; i < intf_desc->desc.bNumEndpoints; i++) {
232 ep_desc = &intf_desc->endpoint[i].desc;
233
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.
240 */
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);
246 }
247 }
248
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",
251 ep_i, ep_o);
252 return -EINVAL;
253 }
254
255 return 0;
256}
257
258static int mt76x0_probe(struct usb_interface *usb_intf,
259 const struct usb_device_id *id)
260{
261 struct usb_device *usb_dev = interface_to_usbdev(usb_intf);
262 struct mt76x0_dev *dev;
263 u32 asic_rev, mac_rev;
264 int ret;
265 static const struct mt76_bus_ops usb_ops = {
266 .rr = mt76x0_rr,
267 .wr = mt76x0_wr,
268 .rmw = mt76x0_rmw,
269 .copy = mt76x0_wr_copy,
270 };
271
272 dev = mt76x0_alloc_device(&usb_intf->dev);
273 if (!dev)
274 return -ENOMEM;
275
276 usb_dev = usb_get_dev(usb_dev);
277 usb_reset_device(usb_dev);
278
279 usb_set_intfdata(usb_intf, dev);
280
281 dev->mt76.bus = &usb_ops;
282
283 ret = mt76x0_assign_pipes(usb_intf, dev);
284 if (ret)
285 goto err;
286 ret = mt76x0_wait_asic_ready(dev);
287 if (ret)
288 goto err;
289
290 asic_rev = mt76_rr(dev, MT_ASIC_VERSION);
291 mac_rev = mt76_rr(dev, MT_MAC_CSR0);
292 dev_info(dev->mt76.dev, "ASIC revision: %08x MAC revision: %08x\n",
293 asic_rev, mac_rev);
294
295 /* Note: vendor driver skips this check for MT76X0U */
296 if (!(mt76_rr(dev, MT_EFUSE_CTRL) & MT_EFUSE_CTRL_SEL))
297 dev_warn(dev->mt76.dev, "Warning: eFUSE not present\n");
298
299 ret = mt76x0_init_hardware(dev);
300 if (ret)
301 goto err;
302
303 ret = mt76x0_register_device(dev);
304 if (ret)
305 goto err_hw;
306
307 set_bit(MT76_STATE_INITIALIZED, &dev->mt76.state);
308
309 return 0;
310err_hw:
311 mt76x0_cleanup(dev);
312err:
313 usb_set_intfdata(usb_intf, NULL);
314 usb_put_dev(interface_to_usbdev(usb_intf));
315
316 destroy_workqueue(dev->stat_wq);
317 ieee80211_free_hw(dev->mt76.hw);
318 return ret;
319}
320
321static void mt76x0_disconnect(struct usb_interface *usb_intf)
322{
323 struct mt76x0_dev *dev = usb_get_intfdata(usb_intf);
324 bool initalized = test_bit(MT76_STATE_INITIALIZED, &dev->mt76.state);
325
326 if (!initalized)
327 return;
328
329 ieee80211_unregister_hw(dev->mt76.hw);
330 mt76x0_cleanup(dev);
331
332 usb_set_intfdata(usb_intf, NULL);
333 usb_put_dev(interface_to_usbdev(usb_intf));
334
335 destroy_workqueue(dev->stat_wq);
336 ieee80211_free_hw(dev->mt76.hw);
337}
338
339static int mt76x0_suspend(struct usb_interface *usb_intf, pm_message_t state)
340{
341 struct mt76x0_dev *dev = usb_get_intfdata(usb_intf);
342
343 mt76x0_cleanup(dev);
344
345 return 0;
346}
347
348static int mt76x0_resume(struct usb_interface *usb_intf)
349{
350 struct mt76x0_dev *dev = usb_get_intfdata(usb_intf);
351 int ret;
352
353 ret = mt76x0_init_hardware(dev);
354 if (ret)
355 return ret;
356
357 set_bit(MT76_STATE_INITIALIZED, &dev->mt76.state);
358
359 return 0;
360}
361
362MODULE_DEVICE_TABLE(usb, mt76x0_device_table);
363MODULE_FIRMWARE(MT7610_FIRMWARE);
364MODULE_LICENSE("GPL");
365
366static struct usb_driver mt76x0_driver = {
367 .name = KBUILD_MODNAME,
368 .id_table = mt76x0_device_table,
369 .probe = mt76x0_probe,
370 .disconnect = mt76x0_disconnect,
371 .suspend = mt76x0_suspend,
372 .resume = mt76x0_resume,
373 .reset_resume = mt76x0_resume,
374 .soft_unbind = 1,
375 .disable_hub_initiated_lpm = 1,
376};
377module_usb_driver(mt76x0_driver);
diff --git a/drivers/net/wireless/mediatek/mt76/mt76x0/usb.h b/drivers/net/wireless/mediatek/mt76/mt76x0/usb.h
new file mode 100644
index 000000000000..9e42dfa665de
--- /dev/null
+++ b/drivers/net/wireless/mediatek/mt76/mt76x0/usb.h
@@ -0,0 +1,61 @@
1/*
2 * Copyright (C) 2015 Jakub Kicinski <kubakici@wp.pl>
3 *
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
7 *
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.
12 */
13
14#ifndef __MT76X0U_USB_H
15#define __MT76X0U_USB_H
16
17#include "mt76x0.h"
18
19#define MT7610_FIRMWARE "mt7610.bin"
20
21#define MT_VEND_REQ_MAX_RETRY 10
22#define MT_VEND_REQ_TOUT_MS 300
23
24#define MT_VEND_DEV_MODE_RESET 1
25
26#define MT_VEND_BUF sizeof(__le32)
27
28static inline struct usb_device *mt76x0_to_usb_dev(struct mt76x0_dev *mt76x0)
29{
30 return interface_to_usbdev(to_usb_interface(mt76x0->mt76.dev));
31}
32
33static inline struct usb_device *mt76_to_usb_dev(struct mt76_dev *mt76)
34{
35 return interface_to_usbdev(to_usb_interface(mt76->dev));
36}
37
38static inline bool mt76x0_urb_has_error(struct urb *urb)
39{
40 return urb->status &&
41 urb->status != -ENOENT &&
42 urb->status != -ECONNRESET &&
43 urb->status != -ESHUTDOWN;
44}
45
46bool mt76x0_usb_alloc_buf(struct mt76x0_dev *dev, size_t len,
47 struct mt76x0_dma_buf *buf);
48void mt76x0_usb_free_buf(struct mt76x0_dev *dev, struct mt76x0_dma_buf *buf);
49int mt76x0_usb_submit_buf(struct mt76x0_dev *dev, int dir, int ep_idx,
50 struct mt76x0_dma_buf *buf, gfp_t gfp,
51 usb_complete_t complete_fn, void *context);
52void mt76x0_complete_urb(struct urb *urb);
53
54int mt76x0_vendor_request(struct mt76x0_dev *dev, const u8 req,
55 const u8 direction, const u16 val, const u16 offset,
56 void *buf, const size_t buflen);
57void mt76x0_vendor_reset(struct mt76x0_dev *dev);
58int mt76x0_vendor_single_wr(struct mt76x0_dev *dev, const u8 req,
59 const u16 offset, const u32 val);
60
61#endif