]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/net/wireless/brcm80211/brcmfmac/usb.c
brcmfmac: clear status for in-band interrupt in brcmf_sdbrcm_isr
[mirror_ubuntu-hirsute-kernel.git] / drivers / net / wireless / brcm80211 / brcmfmac / usb.c
CommitLineData
71bb244b
AS
1/*
2 * Copyright (c) 2011 Broadcom Corporation
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 ANY
11 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17#include <linux/init.h>
18#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/kthread.h>
21#include <linux/slab.h>
22#include <linux/skbuff.h>
23#include <linux/netdevice.h>
24#include <linux/spinlock.h>
25#include <linux/ethtool.h>
26#include <linux/fcntl.h>
27#include <linux/fs.h>
28#include <linux/uaccess.h>
29#include <linux/firmware.h>
30#include <linux/usb.h>
edb9bc9a 31#include <linux/vmalloc.h>
71bb244b
AS
32#include <net/cfg80211.h>
33
34#include <defs.h>
35#include <brcmu_utils.h>
36#include <brcmu_wifi.h>
37#include <dhd_bus.h>
38#include <dhd_dbg.h>
39
40#include "usb_rdl.h"
41#include "usb.h"
42
43#define IOCTL_RESP_TIMEOUT 2000
44
45#define BRCMF_USB_SYNC_TIMEOUT 300 /* ms */
46#define BRCMF_USB_DLIMAGE_SPINWAIT 100 /* in unit of ms */
47#define BRCMF_USB_DLIMAGE_LIMIT 500 /* spinwait limit (ms) */
48
49#define BRCMF_POSTBOOT_ID 0xA123 /* ID to detect if dongle
50 has boot up */
51#define BRCMF_USB_RESETCFG_SPINWAIT 1 /* wait after resetcfg (ms) */
52
53#define BRCMF_USB_NRXQ 50
54#define BRCMF_USB_NTXQ 50
55
56#define CONFIGDESC(usb) (&((usb)->actconfig)->desc)
57#define IFPTR(usb, idx) ((usb)->actconfig->interface[(idx)])
58#define IFALTS(usb, idx) (IFPTR((usb), (idx))->altsetting[0])
59#define IFDESC(usb, idx) IFALTS((usb), (idx)).desc
60#define IFEPDESC(usb, idx, ep) (IFALTS((usb), (idx)).endpoint[(ep)]).desc
61
62#define CONTROL_IF 0
63#define BULK_IF 0
64
65#define BRCMF_USB_CBCTL_WRITE 0
66#define BRCMF_USB_CBCTL_READ 1
67#define BRCMF_USB_MAX_PKT_SIZE 1600
68
70f0822c 69#define BRCMF_USB_43143_FW_NAME "brcm/brcmfmac43143.bin"
fda82417 70#define BRCMF_USB_43236_FW_NAME "brcm/brcmfmac43236b.bin"
1212d370 71#define BRCMF_USB_43242_FW_NAME "brcm/brcmfmac43242a.bin"
71bb244b
AS
72
73enum usbdev_suspend_state {
74 USBOS_SUSPEND_STATE_DEVICE_ACTIVE = 0, /* Device is busy, won't allow
75 suspend */
76 USBOS_SUSPEND_STATE_SUSPEND_PENDING, /* Device is idle, can be
77 * suspended. Wating PM to
78 * suspend the device
79 */
80 USBOS_SUSPEND_STATE_SUSPENDED /* Device suspended */
81};
82
71bb244b
AS
83struct brcmf_usb_image {
84 void *data;
85 u32 len;
86};
87static struct brcmf_usb_image g_image = { NULL, 0 };
88
89struct intr_transfer_buf {
90 u32 notification;
91 u32 reserved;
92};
93
94struct brcmf_usbdev_info {
95 struct brcmf_usbdev bus_pub; /* MUST BE FIRST */
96 spinlock_t qlock;
97 struct list_head rx_freeq;
98 struct list_head rx_postq;
99 struct list_head tx_freeq;
100 struct list_head tx_postq;
101 enum usbdev_suspend_state suspend_state;
102 uint rx_pipe, tx_pipe, intr_pipe, rx_pipe2;
103
104 bool activity;
105 int rx_low_watermark;
106 int tx_low_watermark;
107 int tx_high_watermark;
c6ab4294
HM
108 int tx_freecount;
109 bool tx_flowblock;
71bb244b
AS
110
111 struct brcmf_usbreq *tx_reqs;
112 struct brcmf_usbreq *rx_reqs;
113
114 u8 *image; /* buffer for combine fw and nvram */
115 int image_len;
116
117 wait_queue_head_t wait;
118 bool waitdone;
119 int sync_urb_status;
120
121 struct usb_device *usbdev;
122 struct device *dev;
71bb244b
AS
123
124 int ctl_in_pipe, ctl_out_pipe;
125 struct urb *ctl_urb; /* URB for control endpoint */
126 struct usb_ctrlrequest ctl_write;
127 struct usb_ctrlrequest ctl_read;
128 u32 ctl_urb_actual_length;
129 int ctl_urb_status;
130 int ctl_completed;
131 wait_queue_head_t ioctl_resp_wait;
132 wait_queue_head_t ctrl_wait;
133 ulong ctl_op;
134
135 bool rxctl_deferrespok;
136
137 struct urb *bulk_urb; /* used for FW download */
138 struct urb *intr_urb; /* URB for interrupt endpoint */
139 int intr_size; /* Size of interrupt message */
140 int interval; /* Interrupt polling interval */
141 struct intr_transfer_buf intr; /* Data buffer for interrupt endpoint */
71bb244b
AS
142};
143
144static void brcmf_usb_rx_refill(struct brcmf_usbdev_info *devinfo,
145 struct brcmf_usbreq *req);
146
147MODULE_AUTHOR("Broadcom Corporation");
148MODULE_DESCRIPTION("Broadcom 802.11n wireless LAN fullmac usb driver.");
149MODULE_SUPPORTED_DEVICE("Broadcom 802.11n WLAN fullmac usb cards");
150MODULE_LICENSE("Dual BSD/GPL");
151
152static struct brcmf_usbdev *brcmf_usb_get_buspub(struct device *dev)
153{
154 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
155 return bus_if->bus_priv.usb;
156}
157
158static struct brcmf_usbdev_info *brcmf_usb_get_businfo(struct device *dev)
159{
160 return brcmf_usb_get_buspub(dev)->devinfo;
161}
162
71bb244b
AS
163static int brcmf_usb_ioctl_resp_wait(struct brcmf_usbdev_info *devinfo,
164 uint *condition, bool *pending)
165{
166 DECLARE_WAITQUEUE(wait, current);
167 int timeout = IOCTL_RESP_TIMEOUT;
168
169 /* Convert timeout in millsecond to jiffies */
170 timeout = msecs_to_jiffies(timeout);
171 /* Wait until control frame is available */
172 add_wait_queue(&devinfo->ioctl_resp_wait, &wait);
173 set_current_state(TASK_INTERRUPTIBLE);
174
175 smp_mb();
176 while (!(*condition) && (!signal_pending(current) && timeout)) {
177 timeout = schedule_timeout(timeout);
178 /* Wait until control frame is available */
179 smp_mb();
180 }
181
182 if (signal_pending(current))
183 *pending = true;
184
185 set_current_state(TASK_RUNNING);
186 remove_wait_queue(&devinfo->ioctl_resp_wait, &wait);
187
188 return timeout;
189}
190
191static int brcmf_usb_ioctl_resp_wake(struct brcmf_usbdev_info *devinfo)
192{
193 if (waitqueue_active(&devinfo->ioctl_resp_wait))
194 wake_up_interruptible(&devinfo->ioctl_resp_wait);
195
196 return 0;
197}
198
199static void
200brcmf_usb_ctl_complete(struct brcmf_usbdev_info *devinfo, int type, int status)
201{
202
203 if (unlikely(devinfo == NULL))
204 return;
205
206 if (type == BRCMF_USB_CBCTL_READ) {
207 if (status == 0)
208 devinfo->bus_pub.stats.rx_ctlpkts++;
209 else
210 devinfo->bus_pub.stats.rx_ctlerrs++;
211 } else if (type == BRCMF_USB_CBCTL_WRITE) {
212 if (status == 0)
213 devinfo->bus_pub.stats.tx_ctlpkts++;
214 else
215 devinfo->bus_pub.stats.tx_ctlerrs++;
216 }
217
218 devinfo->ctl_urb_status = status;
219 devinfo->ctl_completed = true;
220 brcmf_usb_ioctl_resp_wake(devinfo);
221}
222
223static void
224brcmf_usb_ctlread_complete(struct urb *urb)
225{
226 struct brcmf_usbdev_info *devinfo =
227 (struct brcmf_usbdev_info *)urb->context;
228
229 devinfo->ctl_urb_actual_length = urb->actual_length;
230 brcmf_usb_ctl_complete(devinfo, BRCMF_USB_CBCTL_READ,
231 urb->status);
232}
233
234static void
235brcmf_usb_ctlwrite_complete(struct urb *urb)
236{
237 struct brcmf_usbdev_info *devinfo =
238 (struct brcmf_usbdev_info *)urb->context;
239
240 brcmf_usb_ctl_complete(devinfo, BRCMF_USB_CBCTL_WRITE,
241 urb->status);
242}
243
244static int brcmf_usb_pnp(struct brcmf_usbdev_info *devinfo, uint state)
245{
246 return 0;
247}
248
249static int
250brcmf_usb_send_ctl(struct brcmf_usbdev_info *devinfo, u8 *buf, int len)
251{
252 int ret;
253 u16 size;
254
255 if (devinfo == NULL || buf == NULL ||
256 len == 0 || devinfo->ctl_urb == NULL)
257 return -EINVAL;
258
259 /* If the USB/HSIC bus in sleep state, wake it up */
260 if (devinfo->suspend_state == USBOS_SUSPEND_STATE_SUSPENDED)
261 if (brcmf_usb_pnp(devinfo, BCMFMAC_USB_PNP_RESUME) != 0) {
262 brcmf_dbg(ERROR, "Could not Resume the bus!\n");
263 return -EIO;
264 }
265
266 devinfo->activity = true;
267 size = len;
268 devinfo->ctl_write.wLength = cpu_to_le16p(&size);
269 devinfo->ctl_urb->transfer_buffer_length = size;
270 devinfo->ctl_urb_status = 0;
271 devinfo->ctl_urb_actual_length = 0;
272
273 usb_fill_control_urb(devinfo->ctl_urb,
274 devinfo->usbdev,
275 devinfo->ctl_out_pipe,
276 (unsigned char *) &devinfo->ctl_write,
277 buf, size,
278 (usb_complete_t)brcmf_usb_ctlwrite_complete,
279 devinfo);
280
281 ret = usb_submit_urb(devinfo->ctl_urb, GFP_ATOMIC);
282 if (ret < 0)
283 brcmf_dbg(ERROR, "usb_submit_urb failed %d\n", ret);
284
285 return ret;
286}
287
288static int
289brcmf_usb_recv_ctl(struct brcmf_usbdev_info *devinfo, u8 *buf, int len)
290{
291 int ret;
292 u16 size;
293
294 if ((devinfo == NULL) || (buf == NULL) || (len == 0)
295 || (devinfo->ctl_urb == NULL))
296 return -EINVAL;
297
298 size = len;
299 devinfo->ctl_read.wLength = cpu_to_le16p(&size);
300 devinfo->ctl_urb->transfer_buffer_length = size;
301
302 if (devinfo->rxctl_deferrespok) {
303 /* BMAC model */
304 devinfo->ctl_read.bRequestType = USB_DIR_IN
305 | USB_TYPE_VENDOR | USB_RECIP_INTERFACE;
306 devinfo->ctl_read.bRequest = DL_DEFER_RESP_OK;
307 } else {
308 /* full dongle model */
309 devinfo->ctl_read.bRequestType = USB_DIR_IN
310 | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
311 devinfo->ctl_read.bRequest = 1;
312 }
313
314 usb_fill_control_urb(devinfo->ctl_urb,
315 devinfo->usbdev,
316 devinfo->ctl_in_pipe,
317 (unsigned char *) &devinfo->ctl_read,
318 buf, size,
319 (usb_complete_t)brcmf_usb_ctlread_complete,
320 devinfo);
321
322 ret = usb_submit_urb(devinfo->ctl_urb, GFP_ATOMIC);
323 if (ret < 0)
324 brcmf_dbg(ERROR, "usb_submit_urb failed %d\n", ret);
325
326 return ret;
327}
328
329static int brcmf_usb_tx_ctlpkt(struct device *dev, u8 *buf, u32 len)
330{
331 int err = 0;
332 int timeout = 0;
333 bool pending;
334 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
335
336 if (devinfo->bus_pub.state != BCMFMAC_USB_STATE_UP) {
337 /* TODO: handle suspend/resume */
338 return -EIO;
339 }
340
341 if (test_and_set_bit(0, &devinfo->ctl_op))
342 return -EIO;
343
a77f5747 344 devinfo->ctl_completed = false;
71bb244b
AS
345 err = brcmf_usb_send_ctl(devinfo, buf, len);
346 if (err) {
347 brcmf_dbg(ERROR, "fail %d bytes: %d\n", err, len);
348 return err;
349 }
350
71bb244b
AS
351 timeout = brcmf_usb_ioctl_resp_wait(devinfo, &devinfo->ctl_completed,
352 &pending);
353 clear_bit(0, &devinfo->ctl_op);
354 if (!timeout) {
355 brcmf_dbg(ERROR, "Txctl wait timed out\n");
356 err = -EIO;
357 }
358 return err;
359}
360
361static int brcmf_usb_rx_ctlpkt(struct device *dev, u8 *buf, u32 len)
362{
363 int err = 0;
364 int timeout = 0;
365 bool pending;
366 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
367
368 if (devinfo->bus_pub.state != BCMFMAC_USB_STATE_UP) {
369 /* TODO: handle suspend/resume */
370 return -EIO;
371 }
372 if (test_and_set_bit(0, &devinfo->ctl_op))
373 return -EIO;
374
375 err = brcmf_usb_recv_ctl(devinfo, buf, len);
376 if (err) {
377 brcmf_dbg(ERROR, "fail %d bytes: %d\n", err, len);
378 return err;
379 }
380 devinfo->ctl_completed = false;
381 timeout = brcmf_usb_ioctl_resp_wait(devinfo, &devinfo->ctl_completed,
382 &pending);
383 err = devinfo->ctl_urb_status;
384 clear_bit(0, &devinfo->ctl_op);
385 if (!timeout) {
386 brcmf_dbg(ERROR, "rxctl wait timed out\n");
387 err = -EIO;
388 }
389 if (!err)
390 return devinfo->ctl_urb_actual_length;
391 else
392 return err;
393}
394
395static struct brcmf_usbreq *brcmf_usb_deq(struct brcmf_usbdev_info *devinfo,
c6ab4294 396 struct list_head *q, int *counter)
71bb244b
AS
397{
398 unsigned long flags;
399 struct brcmf_usbreq *req;
400 spin_lock_irqsave(&devinfo->qlock, flags);
401 if (list_empty(q)) {
402 spin_unlock_irqrestore(&devinfo->qlock, flags);
403 return NULL;
404 }
405 req = list_entry(q->next, struct brcmf_usbreq, list);
406 list_del_init(q->next);
c6ab4294
HM
407 if (counter)
408 (*counter)--;
71bb244b
AS
409 spin_unlock_irqrestore(&devinfo->qlock, flags);
410 return req;
411
412}
413
414static void brcmf_usb_enq(struct brcmf_usbdev_info *devinfo,
c6ab4294
HM
415 struct list_head *q, struct brcmf_usbreq *req,
416 int *counter)
71bb244b
AS
417{
418 unsigned long flags;
419 spin_lock_irqsave(&devinfo->qlock, flags);
420 list_add_tail(&req->list, q);
c6ab4294
HM
421 if (counter)
422 (*counter)++;
71bb244b
AS
423 spin_unlock_irqrestore(&devinfo->qlock, flags);
424}
425
426static struct brcmf_usbreq *
427brcmf_usbdev_qinit(struct list_head *q, int qsize)
428{
429 int i;
430 struct brcmf_usbreq *req, *reqs;
431
432 reqs = kzalloc(sizeof(struct brcmf_usbreq) * qsize, GFP_ATOMIC);
433 if (reqs == NULL) {
434 brcmf_dbg(ERROR, "fail to allocate memory!\n");
435 return NULL;
436 }
437 req = reqs;
438
439 for (i = 0; i < qsize; i++) {
440 req->urb = usb_alloc_urb(0, GFP_ATOMIC);
441 if (!req->urb)
442 goto fail;
443
444 INIT_LIST_HEAD(&req->list);
445 list_add_tail(&req->list, q);
446 req++;
447 }
448 return reqs;
449fail:
450 brcmf_dbg(ERROR, "fail!\n");
451 while (!list_empty(q)) {
452 req = list_entry(q->next, struct brcmf_usbreq, list);
453 if (req && req->urb)
454 usb_free_urb(req->urb);
455 list_del(q->next);
456 }
457 return NULL;
458
459}
460
461static void brcmf_usb_free_q(struct list_head *q, bool pending)
462{
463 struct brcmf_usbreq *req, *next;
464 int i = 0;
465 list_for_each_entry_safe(req, next, q, list) {
d4ca0099 466 if (!req->urb) {
71bb244b
AS
467 brcmf_dbg(ERROR, "bad req\n");
468 break;
469 }
470 i++;
471 if (pending) {
472 usb_kill_urb(req->urb);
473 } else {
474 usb_free_urb(req->urb);
475 list_del_init(&req->list);
476 }
477 }
478}
479
480static void brcmf_usb_del_fromq(struct brcmf_usbdev_info *devinfo,
481 struct brcmf_usbreq *req)
482{
483 unsigned long flags;
484
485 spin_lock_irqsave(&devinfo->qlock, flags);
486 list_del_init(&req->list);
487 spin_unlock_irqrestore(&devinfo->qlock, flags);
488}
489
490
491static void brcmf_usb_tx_complete(struct urb *urb)
492{
493 struct brcmf_usbreq *req = (struct brcmf_usbreq *)urb->context;
494 struct brcmf_usbdev_info *devinfo = req->devinfo;
495
496 brcmf_usb_del_fromq(devinfo, req);
497 if (urb->status == 0)
1d9c1796 498 devinfo->bus_pub.bus->dstats.tx_packets++;
71bb244b 499 else
1d9c1796 500 devinfo->bus_pub.bus->dstats.tx_errors++;
71bb244b 501
01e3331b 502 brcmu_pkt_buf_free_skb(req->skb);
71bb244b 503 req->skb = NULL;
c6ab4294
HM
504 brcmf_usb_enq(devinfo, &devinfo->tx_freeq, req, &devinfo->tx_freecount);
505 if (devinfo->tx_freecount > devinfo->tx_high_watermark &&
506 devinfo->tx_flowblock) {
507 brcmf_txflowblock(devinfo->dev, false);
508 devinfo->tx_flowblock = false;
509 }
71bb244b
AS
510}
511
512static void brcmf_usb_rx_complete(struct urb *urb)
513{
514 struct brcmf_usbreq *req = (struct brcmf_usbreq *)urb->context;
515 struct brcmf_usbdev_info *devinfo = req->devinfo;
516 struct sk_buff *skb;
517 int ifidx = 0;
518
519 brcmf_usb_del_fromq(devinfo, req);
520 skb = req->skb;
521 req->skb = NULL;
522
523 if (urb->status == 0) {
1d9c1796 524 devinfo->bus_pub.bus->dstats.rx_packets++;
71bb244b 525 } else {
1d9c1796 526 devinfo->bus_pub.bus->dstats.rx_errors++;
01e3331b 527 brcmu_pkt_buf_free_skb(skb);
c6ab4294 528 brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
71bb244b
AS
529 return;
530 }
531
532 if (devinfo->bus_pub.state == BCMFMAC_USB_STATE_UP) {
533 skb_put(skb, urb->actual_length);
534 if (brcmf_proto_hdrpull(devinfo->dev, &ifidx, skb) != 0) {
535 brcmf_dbg(ERROR, "rx protocol error\n");
536 brcmu_pkt_buf_free_skb(skb);
537 devinfo->bus_pub.bus->dstats.rx_errors++;
64477ebc 538 } else
71bb244b 539 brcmf_rx_packet(devinfo->dev, ifidx, skb);
64477ebc 540 brcmf_usb_rx_refill(devinfo, req);
71bb244b 541 } else {
01e3331b 542 brcmu_pkt_buf_free_skb(skb);
c6ab4294 543 brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
71bb244b
AS
544 }
545 return;
546
547}
548
549static void brcmf_usb_rx_refill(struct brcmf_usbdev_info *devinfo,
550 struct brcmf_usbreq *req)
551{
552 struct sk_buff *skb;
553 int ret;
554
555 if (!req || !devinfo)
556 return;
557
558 skb = dev_alloc_skb(devinfo->bus_pub.bus_mtu);
559 if (!skb) {
c6ab4294 560 brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
71bb244b
AS
561 return;
562 }
563 req->skb = skb;
564
565 usb_fill_bulk_urb(req->urb, devinfo->usbdev, devinfo->rx_pipe,
566 skb->data, skb_tailroom(skb), brcmf_usb_rx_complete,
567 req);
71bb244b 568 req->devinfo = devinfo;
c6ab4294 569 brcmf_usb_enq(devinfo, &devinfo->rx_postq, req, NULL);
71bb244b
AS
570
571 ret = usb_submit_urb(req->urb, GFP_ATOMIC);
2e875acd
HM
572 if (ret) {
573 brcmf_usb_del_fromq(devinfo, req);
01e3331b 574 brcmu_pkt_buf_free_skb(req->skb);
71bb244b 575 req->skb = NULL;
c6ab4294 576 brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
71bb244b
AS
577 }
578 return;
579}
580
581static void brcmf_usb_rx_fill_all(struct brcmf_usbdev_info *devinfo)
582{
583 struct brcmf_usbreq *req;
584
585 if (devinfo->bus_pub.state != BCMFMAC_USB_STATE_UP) {
586 brcmf_dbg(ERROR, "bus is not up\n");
587 return;
588 }
c6ab4294 589 while ((req = brcmf_usb_deq(devinfo, &devinfo->rx_freeq, NULL)) != NULL)
71bb244b
AS
590 brcmf_usb_rx_refill(devinfo, req);
591}
592
593static void
594brcmf_usb_state_change(struct brcmf_usbdev_info *devinfo, int state)
595{
596 struct brcmf_bus *bcmf_bus = devinfo->bus_pub.bus;
597 int old_state;
598
599
600 if (devinfo->bus_pub.state == state)
601 return;
602
603 old_state = devinfo->bus_pub.state;
604 brcmf_dbg(TRACE, "dbus state change from %d to to %d\n",
605 old_state, state);
606
607 /* Don't update state if it's PnP firmware re-download */
608 if (state != BCMFMAC_USB_STATE_PNP_FWDL) /* TODO */
609 devinfo->bus_pub.state = state;
610
611 if ((old_state == BCMFMAC_USB_STATE_SLEEP)
612 && (state == BCMFMAC_USB_STATE_UP)) {
613 brcmf_usb_rx_fill_all(devinfo);
614 }
615
616 /* update state of upper layer */
617 if (state == BCMFMAC_USB_STATE_DOWN) {
618 brcmf_dbg(INFO, "DBUS is down\n");
619 bcmf_bus->state = BRCMF_BUS_DOWN;
620 } else {
621 brcmf_dbg(INFO, "DBUS current state=%d\n", state);
622 }
623}
624
625static void
626brcmf_usb_intr_complete(struct urb *urb)
627{
628 struct brcmf_usbdev_info *devinfo =
629 (struct brcmf_usbdev_info *)urb->context;
630 bool killed;
631
632 if (devinfo == NULL)
633 return;
634
635 if (unlikely(urb->status)) {
636 if (devinfo->suspend_state ==
637 USBOS_SUSPEND_STATE_SUSPEND_PENDING)
638 killed = true;
639
640 if ((urb->status == -ENOENT && (!killed))
641 || urb->status == -ESHUTDOWN ||
642 urb->status == -ENODEV) {
643 brcmf_usb_state_change(devinfo, BCMFMAC_USB_STATE_DOWN);
644 }
645 }
646
647 if (devinfo->bus_pub.state == BCMFMAC_USB_STATE_DOWN) {
648 brcmf_dbg(ERROR, "intr cb when DBUS down, ignoring\n");
649 return;
650 }
651
652 if (devinfo->bus_pub.state == BCMFMAC_USB_STATE_UP)
653 usb_submit_urb(devinfo->intr_urb, GFP_ATOMIC);
654}
655
656static int brcmf_usb_tx(struct device *dev, struct sk_buff *skb)
657{
658 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
659 struct brcmf_usbreq *req;
660 int ret;
661
662 if (devinfo->bus_pub.state != BCMFMAC_USB_STATE_UP) {
663 /* TODO: handle suspend/resume */
664 return -EIO;
665 }
666
c6ab4294
HM
667 req = brcmf_usb_deq(devinfo, &devinfo->tx_freeq,
668 &devinfo->tx_freecount);
71bb244b 669 if (!req) {
2e875acd 670 brcmu_pkt_buf_free_skb(skb);
71bb244b
AS
671 brcmf_dbg(ERROR, "no req to send\n");
672 return -ENOMEM;
673 }
71bb244b
AS
674
675 req->skb = skb;
676 req->devinfo = devinfo;
677 usb_fill_bulk_urb(req->urb, devinfo->usbdev, devinfo->tx_pipe,
678 skb->data, skb->len, brcmf_usb_tx_complete, req);
679 req->urb->transfer_flags |= URB_ZERO_PACKET;
c6ab4294 680 brcmf_usb_enq(devinfo, &devinfo->tx_postq, req, NULL);
71bb244b 681 ret = usb_submit_urb(req->urb, GFP_ATOMIC);
2e875acd
HM
682 if (ret) {
683 brcmf_dbg(ERROR, "brcmf_usb_tx usb_submit_urb FAILED\n");
684 brcmf_usb_del_fromq(devinfo, req);
685 brcmu_pkt_buf_free_skb(req->skb);
71bb244b 686 req->skb = NULL;
c6ab4294
HM
687 brcmf_usb_enq(devinfo, &devinfo->tx_freeq, req,
688 &devinfo->tx_freecount);
689 } else {
690 if (devinfo->tx_freecount < devinfo->tx_low_watermark &&
691 !devinfo->tx_flowblock) {
692 brcmf_txflowblock(dev, true);
693 devinfo->tx_flowblock = true;
694 }
71bb244b
AS
695 }
696
697 return ret;
698}
699
700
701static int brcmf_usb_up(struct device *dev)
702{
703 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
704 u16 ifnum;
705
d4ca0099
DC
706 if (devinfo->bus_pub.state == BCMFMAC_USB_STATE_UP)
707 return 0;
708
71bb244b
AS
709 /* If the USB/HSIC bus in sleep state, wake it up */
710 if (devinfo->suspend_state == USBOS_SUSPEND_STATE_SUSPENDED) {
711 if (brcmf_usb_pnp(devinfo, BCMFMAC_USB_PNP_RESUME) != 0) {
712 brcmf_dbg(ERROR, "Could not Resume the bus!\n");
713 return -EIO;
714 }
715 }
716 devinfo->activity = true;
717
718 /* Success, indicate devinfo is fully up */
719 brcmf_usb_state_change(devinfo, BCMFMAC_USB_STATE_UP);
720
721 if (devinfo->intr_urb) {
722 int ret;
723
724 usb_fill_int_urb(devinfo->intr_urb, devinfo->usbdev,
725 devinfo->intr_pipe,
726 &devinfo->intr,
727 devinfo->intr_size,
728 (usb_complete_t)brcmf_usb_intr_complete,
729 devinfo,
730 devinfo->interval);
731
732 ret = usb_submit_urb(devinfo->intr_urb, GFP_ATOMIC);
733 if (ret) {
734 brcmf_dbg(ERROR, "USB_SUBMIT_URB failed with status %d\n",
735 ret);
736 return -EINVAL;
737 }
738 }
739
740 if (devinfo->ctl_urb) {
741 devinfo->ctl_in_pipe = usb_rcvctrlpipe(devinfo->usbdev, 0);
742 devinfo->ctl_out_pipe = usb_sndctrlpipe(devinfo->usbdev, 0);
743
744 ifnum = IFDESC(devinfo->usbdev, CONTROL_IF).bInterfaceNumber;
745
746 /* CTL Write */
747 devinfo->ctl_write.bRequestType =
748 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
749 devinfo->ctl_write.bRequest = 0;
750 devinfo->ctl_write.wValue = cpu_to_le16(0);
751 devinfo->ctl_write.wIndex = cpu_to_le16p(&ifnum);
752
753 /* CTL Read */
754 devinfo->ctl_read.bRequestType =
755 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
756 devinfo->ctl_read.bRequest = 1;
757 devinfo->ctl_read.wValue = cpu_to_le16(0);
758 devinfo->ctl_read.wIndex = cpu_to_le16p(&ifnum);
759 }
760 brcmf_usb_rx_fill_all(devinfo);
761 return 0;
762}
763
764static void brcmf_usb_down(struct device *dev)
765{
766 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
767
768 if (devinfo == NULL)
769 return;
770
771 brcmf_dbg(TRACE, "enter\n");
772 if (devinfo->bus_pub.state == BCMFMAC_USB_STATE_DOWN)
773 return;
774
775 brcmf_usb_state_change(devinfo, BCMFMAC_USB_STATE_DOWN);
776 if (devinfo->intr_urb)
777 usb_kill_urb(devinfo->intr_urb);
778
779 if (devinfo->ctl_urb)
780 usb_kill_urb(devinfo->ctl_urb);
781
782 if (devinfo->bulk_urb)
783 usb_kill_urb(devinfo->bulk_urb);
784 brcmf_usb_free_q(&devinfo->tx_postq, true);
785
786 brcmf_usb_free_q(&devinfo->rx_postq, true);
787}
788
789static int
790brcmf_usb_sync_wait(struct brcmf_usbdev_info *devinfo, u16 time)
791{
792 int ret;
793 int err = 0;
794 int ms = time;
795
796 ret = wait_event_interruptible_timeout(devinfo->wait,
797 devinfo->waitdone == true, (ms * HZ / 1000));
798
799 if ((devinfo->waitdone == false) || (devinfo->sync_urb_status)) {
800 brcmf_dbg(ERROR, "timeout(%d) or urb err=%d\n",
801 ret, devinfo->sync_urb_status);
802 err = -EINVAL;
803 }
804 devinfo->waitdone = false;
805 return err;
806}
807
808static void
809brcmf_usb_sync_complete(struct urb *urb)
810{
811 struct brcmf_usbdev_info *devinfo =
812 (struct brcmf_usbdev_info *)urb->context;
813
814 devinfo->waitdone = true;
815 wake_up_interruptible(&devinfo->wait);
816 devinfo->sync_urb_status = urb->status;
817}
818
819static bool brcmf_usb_dl_cmd(struct brcmf_usbdev_info *devinfo, u8 cmd,
820 void *buffer, int buflen)
821{
822 int ret = 0;
823 char *tmpbuf;
824 u16 size;
825
826 if ((!devinfo) || (devinfo->ctl_urb == NULL))
827 return false;
828
829 tmpbuf = kmalloc(buflen, GFP_ATOMIC);
830 if (!tmpbuf)
831 return false;
832
833 size = buflen;
834 devinfo->ctl_urb->transfer_buffer_length = size;
835
836 devinfo->ctl_read.wLength = cpu_to_le16p(&size);
837 devinfo->ctl_read.bRequestType = USB_DIR_IN | USB_TYPE_VENDOR |
838 USB_RECIP_INTERFACE;
839 devinfo->ctl_read.bRequest = cmd;
840
841 usb_fill_control_urb(devinfo->ctl_urb,
842 devinfo->usbdev,
843 usb_rcvctrlpipe(devinfo->usbdev, 0),
844 (unsigned char *) &devinfo->ctl_read,
845 (void *) tmpbuf, size,
846 (usb_complete_t)brcmf_usb_sync_complete, devinfo);
847
848 ret = usb_submit_urb(devinfo->ctl_urb, GFP_ATOMIC);
849 if (ret < 0) {
850 brcmf_dbg(ERROR, "usb_submit_urb failed %d\n", ret);
851 kfree(tmpbuf);
852 return false;
853 }
854
855 ret = brcmf_usb_sync_wait(devinfo, BRCMF_USB_SYNC_TIMEOUT);
856 memcpy(buffer, tmpbuf, buflen);
857 kfree(tmpbuf);
858
859 return (ret == 0);
860}
861
862static bool
863brcmf_usb_dlneeded(struct brcmf_usbdev_info *devinfo)
864{
865 struct bootrom_id_le id;
866 u32 chipid, chiprev;
867
868 brcmf_dbg(TRACE, "enter\n");
869
870 if (devinfo == NULL)
871 return false;
872
873 /* Check if firmware downloaded already by querying runtime ID */
874 id.chip = cpu_to_le32(0xDEAD);
875 brcmf_usb_dl_cmd(devinfo, DL_GETVER, &id,
876 sizeof(struct bootrom_id_le));
877
878 chipid = le32_to_cpu(id.chip);
879 chiprev = le32_to_cpu(id.chiprev);
880
881 if ((chipid & 0x4300) == 0x4300)
882 brcmf_dbg(INFO, "chip %x rev 0x%x\n", chipid, chiprev);
883 else
884 brcmf_dbg(INFO, "chip %d rev 0x%x\n", chipid, chiprev);
885 if (chipid == BRCMF_POSTBOOT_ID) {
886 brcmf_dbg(INFO, "firmware already downloaded\n");
887 brcmf_usb_dl_cmd(devinfo, DL_RESETCFG, &id,
888 sizeof(struct bootrom_id_le));
889 return false;
890 } else {
ac94f196
AS
891 devinfo->bus_pub.devid = chipid;
892 devinfo->bus_pub.chiprev = chiprev;
71bb244b
AS
893 }
894 return true;
895}
896
897static int
898brcmf_usb_resetcfg(struct brcmf_usbdev_info *devinfo)
899{
900 struct bootrom_id_le id;
901 u16 wait = 0, wait_time;
902
903 brcmf_dbg(TRACE, "enter\n");
904
905 if (devinfo == NULL)
906 return -EINVAL;
907
908 /* Give dongle chance to boot */
909 wait_time = BRCMF_USB_DLIMAGE_SPINWAIT;
910 while (wait < BRCMF_USB_DLIMAGE_LIMIT) {
911 mdelay(wait_time);
912 wait += wait_time;
913 id.chip = cpu_to_le32(0xDEAD); /* Get the ID */
914 brcmf_usb_dl_cmd(devinfo, DL_GETVER, &id,
915 sizeof(struct bootrom_id_le));
916 if (id.chip == cpu_to_le32(BRCMF_POSTBOOT_ID))
917 break;
918 }
919
920 if (id.chip == cpu_to_le32(BRCMF_POSTBOOT_ID)) {
921 brcmf_dbg(INFO, "download done %d ms postboot chip 0x%x/rev 0x%x\n",
922 wait, le32_to_cpu(id.chip), le32_to_cpu(id.chiprev));
923
924 brcmf_usb_dl_cmd(devinfo, DL_RESETCFG, &id,
925 sizeof(struct bootrom_id_le));
926
927 /* XXX this wait may not be necessary */
928 mdelay(BRCMF_USB_RESETCFG_SPINWAIT);
929 return 0;
930 } else {
931 brcmf_dbg(ERROR, "Cannot talk to Dongle. Firmware is not UP, %d ms\n",
932 wait);
933 return -EINVAL;
934 }
935}
936
937
938static int
939brcmf_usb_dl_send_bulk(struct brcmf_usbdev_info *devinfo, void *buffer, int len)
940{
941 int ret;
942
943 if ((devinfo == NULL) || (devinfo->bulk_urb == NULL))
944 return -EINVAL;
945
946 /* Prepare the URB */
947 usb_fill_bulk_urb(devinfo->bulk_urb, devinfo->usbdev,
948 devinfo->tx_pipe, buffer, len,
949 (usb_complete_t)brcmf_usb_sync_complete, devinfo);
950
951 devinfo->bulk_urb->transfer_flags |= URB_ZERO_PACKET;
952
953 ret = usb_submit_urb(devinfo->bulk_urb, GFP_ATOMIC);
954 if (ret) {
955 brcmf_dbg(ERROR, "usb_submit_urb failed %d\n", ret);
956 return ret;
957 }
958 ret = brcmf_usb_sync_wait(devinfo, BRCMF_USB_SYNC_TIMEOUT);
959 return ret;
960}
961
962static int
963brcmf_usb_dl_writeimage(struct brcmf_usbdev_info *devinfo, u8 *fw, int fwlen)
964{
965 unsigned int sendlen, sent, dllen;
966 char *bulkchunk = NULL, *dlpos;
967 struct rdl_state_le state;
968 u32 rdlstate, rdlbytes;
969 int err = 0;
970 brcmf_dbg(TRACE, "fw %p, len %d\n", fw, fwlen);
971
972 bulkchunk = kmalloc(RDL_CHUNK, GFP_ATOMIC);
973 if (bulkchunk == NULL) {
974 err = -ENOMEM;
975 goto fail;
976 }
977
978 /* 1) Prepare USB boot loader for runtime image */
979 brcmf_usb_dl_cmd(devinfo, DL_START, &state,
980 sizeof(struct rdl_state_le));
981
982 rdlstate = le32_to_cpu(state.state);
983 rdlbytes = le32_to_cpu(state.bytes);
984
985 /* 2) Check we are in the Waiting state */
986 if (rdlstate != DL_WAITING) {
987 brcmf_dbg(ERROR, "Failed to DL_START\n");
988 err = -EINVAL;
989 goto fail;
990 }
991 sent = 0;
992 dlpos = fw;
993 dllen = fwlen;
994
995 /* Get chip id and rev */
996 while (rdlbytes != dllen) {
997 /* Wait until the usb device reports it received all
998 * the bytes we sent */
999 if ((rdlbytes == sent) && (rdlbytes != dllen)) {
1000 if ((dllen-sent) < RDL_CHUNK)
1001 sendlen = dllen-sent;
1002 else
1003 sendlen = RDL_CHUNK;
1004
1005 /* simply avoid having to send a ZLP by ensuring we
1006 * never have an even
1007 * multiple of 64
1008 */
1009 if (!(sendlen % 64))
1010 sendlen -= 4;
1011
1012 /* send data */
1013 memcpy(bulkchunk, dlpos, sendlen);
1014 if (brcmf_usb_dl_send_bulk(devinfo, bulkchunk,
1015 sendlen)) {
1016 brcmf_dbg(ERROR, "send_bulk failed\n");
1017 err = -EINVAL;
1018 goto fail;
1019 }
1020
1021 dlpos += sendlen;
1022 sent += sendlen;
1023 }
1024 if (!brcmf_usb_dl_cmd(devinfo, DL_GETSTATE, &state,
1025 sizeof(struct rdl_state_le))) {
1026 brcmf_dbg(ERROR, "DL_GETSTATE Failed xxxx\n");
1027 err = -EINVAL;
1028 goto fail;
1029 }
1030
1031 rdlstate = le32_to_cpu(state.state);
1032 rdlbytes = le32_to_cpu(state.bytes);
1033
1034 /* restart if an error is reported */
1035 if (rdlstate == DL_BAD_HDR || rdlstate == DL_BAD_CRC) {
1036 brcmf_dbg(ERROR, "Bad Hdr or Bad CRC state %d\n",
1037 rdlstate);
1038 err = -EINVAL;
1039 goto fail;
1040 }
1041 }
1042
1043fail:
1044 kfree(bulkchunk);
1045 brcmf_dbg(TRACE, "err=%d\n", err);
1046 return err;
1047}
1048
1049static int brcmf_usb_dlstart(struct brcmf_usbdev_info *devinfo, u8 *fw, int len)
1050{
1051 int err;
1052
1053 brcmf_dbg(TRACE, "enter\n");
1054
1055 if (devinfo == NULL)
1056 return -EINVAL;
1057
ac94f196 1058 if (devinfo->bus_pub.devid == 0xDEAD)
71bb244b
AS
1059 return -EINVAL;
1060
1061 err = brcmf_usb_dl_writeimage(devinfo, fw, len);
1062 if (err == 0)
1063 devinfo->bus_pub.state = BCMFMAC_USB_STATE_DL_DONE;
1064 else
1065 devinfo->bus_pub.state = BCMFMAC_USB_STATE_DL_PENDING;
1066 brcmf_dbg(TRACE, "exit: err=%d\n", err);
1067
1068 return err;
1069}
1070
1071static int brcmf_usb_dlrun(struct brcmf_usbdev_info *devinfo)
1072{
1073 struct rdl_state_le state;
1074
1075 brcmf_dbg(TRACE, "enter\n");
1076 if (!devinfo)
1077 return -EINVAL;
1078
ac94f196 1079 if (devinfo->bus_pub.devid == 0xDEAD)
71bb244b
AS
1080 return -EINVAL;
1081
1082 /* Check we are runnable */
1083 brcmf_usb_dl_cmd(devinfo, DL_GETSTATE, &state,
1084 sizeof(struct rdl_state_le));
1085
1086 /* Start the image */
1087 if (state.state == cpu_to_le32(DL_RUNNABLE)) {
1088 if (!brcmf_usb_dl_cmd(devinfo, DL_GO, &state,
1089 sizeof(struct rdl_state_le)))
1090 return -ENODEV;
1091 if (brcmf_usb_resetcfg(devinfo))
1092 return -ENODEV;
1093 /* The Dongle may go for re-enumeration. */
1094 } else {
1095 brcmf_dbg(ERROR, "Dongle not runnable\n");
1096 return -EINVAL;
1097 }
1098 brcmf_dbg(TRACE, "exit\n");
1099 return 0;
1100}
1101
1102static bool brcmf_usb_chip_support(int chipid, int chiprev)
1103{
1104 switch(chipid) {
70f0822c
HM
1105 case 43143:
1106 return true;
71bb244b
AS
1107 case 43235:
1108 case 43236:
1109 case 43238:
1110 return (chiprev == 3);
1212d370
HM
1111 case 43242:
1112 return true;
71bb244b
AS
1113 default:
1114 break;
1115 }
1116 return false;
1117}
1118
1119static int
1120brcmf_usb_fw_download(struct brcmf_usbdev_info *devinfo)
1121{
ac94f196 1122 int devid, chiprev;
71bb244b
AS
1123 int err;
1124
1125 brcmf_dbg(TRACE, "enter\n");
1126 if (devinfo == NULL)
1127 return -ENODEV;
1128
ac94f196
AS
1129 devid = devinfo->bus_pub.devid;
1130 chiprev = devinfo->bus_pub.chiprev;
71bb244b 1131
ac94f196 1132 if (!brcmf_usb_chip_support(devid, chiprev)) {
71bb244b 1133 brcmf_dbg(ERROR, "unsupported chip %d rev %d\n",
ac94f196 1134 devid, chiprev);
71bb244b
AS
1135 return -EINVAL;
1136 }
1137
1138 if (!devinfo->image) {
1139 brcmf_dbg(ERROR, "No firmware!\n");
1140 return -ENOENT;
1141 }
1142
1143 err = brcmf_usb_dlstart(devinfo,
1144 devinfo->image, devinfo->image_len);
1145 if (err == 0)
1146 err = brcmf_usb_dlrun(devinfo);
1147 return err;
1148}
1149
1150
d74a0b51 1151static void brcmf_usb_detach(struct brcmf_usbdev_info *devinfo)
71bb244b 1152{
71bb244b
AS
1153 brcmf_dbg(TRACE, "devinfo %p\n", devinfo);
1154
1155 /* store the image globally */
1156 g_image.data = devinfo->image;
1157 g_image.len = devinfo->image_len;
1158
1159 /* free the URBS */
1160 brcmf_usb_free_q(&devinfo->rx_freeq, false);
1161 brcmf_usb_free_q(&devinfo->tx_freeq, false);
1162
1163 usb_free_urb(devinfo->intr_urb);
1164 usb_free_urb(devinfo->ctl_urb);
1165 usb_free_urb(devinfo->bulk_urb);
1166
1167 kfree(devinfo->tx_reqs);
1168 kfree(devinfo->rx_reqs);
71bb244b
AS
1169}
1170
1171#define TRX_MAGIC 0x30524448 /* "HDR0" */
1172#define TRX_VERSION 1 /* Version 1 */
1173#define TRX_MAX_LEN 0x3B0000 /* Max length */
1174#define TRX_NO_HEADER 1 /* Do not write TRX header */
1175#define TRX_MAX_OFFSET 3 /* Max number of individual files */
1176#define TRX_UNCOMP_IMAGE 0x20 /* Trx contains uncompressed image */
1177
1178struct trx_header_le {
1179 __le32 magic; /* "HDR0" */
1180 __le32 len; /* Length of file including header */
1181 __le32 crc32; /* CRC from flag_version to end of file */
1182 __le32 flag_version; /* 0:15 flags, 16:31 version */
1183 __le32 offsets[TRX_MAX_OFFSET]; /* Offsets of partitions from start of
1184 * header */
1185};
1186
1187static int check_file(const u8 *headers)
1188{
1189 struct trx_header_le *trx;
1190 int actual_len = -1;
1191
1192 /* Extract trx header */
1193 trx = (struct trx_header_le *) headers;
1194 if (trx->magic != cpu_to_le32(TRX_MAGIC))
1195 return -1;
1196
1197 headers += sizeof(struct trx_header_le);
1198
1199 if (le32_to_cpu(trx->flag_version) & TRX_UNCOMP_IMAGE) {
1200 actual_len = le32_to_cpu(trx->offsets[TRX_OFFSETS_DLFWLEN_IDX]);
1201 return actual_len + sizeof(struct trx_header_le);
1202 }
1203 return -1;
1204}
1205
1206static int brcmf_usb_get_fw(struct brcmf_usbdev_info *devinfo)
1207{
1208 s8 *fwname;
1209 const struct firmware *fw;
1210 int err;
1211
1212 devinfo->image = g_image.data;
1213 devinfo->image_len = g_image.len;
1214
1215 /*
1216 * if we have an image we can leave here.
1217 */
1218 if (devinfo->image)
1219 return 0;
1220
1212d370 1221 switch (devinfo->bus_pub.devid) {
70f0822c
HM
1222 case 43143:
1223 fwname = BRCMF_USB_43143_FW_NAME;
1224 break;
1212d370
HM
1225 case 43235:
1226 case 43236:
1227 case 43238:
1228 fwname = BRCMF_USB_43236_FW_NAME;
1229 break;
1230 case 43242:
1231 fwname = BRCMF_USB_43242_FW_NAME;
1232 break;
1233 default:
1234 return -EINVAL;
1235 break;
1236 }
71bb244b
AS
1237
1238 err = request_firmware(&fw, fwname, devinfo->dev);
1239 if (!fw) {
1240 brcmf_dbg(ERROR, "fail to request firmware %s\n", fwname);
1241 return err;
1242 }
1243 if (check_file(fw->data) < 0) {
1244 brcmf_dbg(ERROR, "invalid firmware %s\n", fwname);
1245 return -EINVAL;
1246 }
1247
edb9bc9a 1248 devinfo->image = vmalloc(fw->size); /* plus nvram */
71bb244b
AS
1249 if (!devinfo->image)
1250 return -ENOMEM;
1251
1252 memcpy(devinfo->image, fw->data, fw->size);
1253 devinfo->image_len = fw->size;
1254
1255 release_firmware(fw);
1256 return 0;
1257}
1258
1259
1260static
d74a0b51
HM
1261struct brcmf_usbdev *brcmf_usb_attach(struct brcmf_usbdev_info *devinfo,
1262 int nrxq, int ntxq)
71bb244b 1263{
71bb244b
AS
1264 devinfo->bus_pub.nrxq = nrxq;
1265 devinfo->rx_low_watermark = nrxq / 2;
1266 devinfo->bus_pub.devinfo = devinfo;
1267 devinfo->bus_pub.ntxq = ntxq;
1268
1269 /* flow control when too many tx urbs posted */
1270 devinfo->tx_low_watermark = ntxq / 4;
1271 devinfo->tx_high_watermark = devinfo->tx_low_watermark * 3;
71bb244b
AS
1272 devinfo->bus_pub.bus_mtu = BRCMF_USB_MAX_PKT_SIZE;
1273
1274 /* Initialize other structure content */
1275 init_waitqueue_head(&devinfo->ioctl_resp_wait);
1276
1277 /* Initialize the spinlocks */
1278 spin_lock_init(&devinfo->qlock);
1279
1280 INIT_LIST_HEAD(&devinfo->rx_freeq);
1281 INIT_LIST_HEAD(&devinfo->rx_postq);
1282
1283 INIT_LIST_HEAD(&devinfo->tx_freeq);
1284 INIT_LIST_HEAD(&devinfo->tx_postq);
1285
c6ab4294
HM
1286 devinfo->tx_flowblock = false;
1287
71bb244b
AS
1288 devinfo->rx_reqs = brcmf_usbdev_qinit(&devinfo->rx_freeq, nrxq);
1289 if (!devinfo->rx_reqs)
1290 goto error;
1291
1292 devinfo->tx_reqs = brcmf_usbdev_qinit(&devinfo->tx_freeq, ntxq);
1293 if (!devinfo->tx_reqs)
1294 goto error;
c6ab4294 1295 devinfo->tx_freecount = ntxq;
71bb244b
AS
1296
1297 devinfo->intr_urb = usb_alloc_urb(0, GFP_ATOMIC);
1298 if (!devinfo->intr_urb) {
1299 brcmf_dbg(ERROR, "usb_alloc_urb (intr) failed\n");
1300 goto error;
1301 }
1302 devinfo->ctl_urb = usb_alloc_urb(0, GFP_ATOMIC);
1303 if (!devinfo->ctl_urb) {
1304 brcmf_dbg(ERROR, "usb_alloc_urb (ctl) failed\n");
1305 goto error;
1306 }
1307 devinfo->rxctl_deferrespok = 0;
1308
1309 devinfo->bulk_urb = usb_alloc_urb(0, GFP_ATOMIC);
1310 if (!devinfo->bulk_urb) {
1311 brcmf_dbg(ERROR, "usb_alloc_urb (bulk) failed\n");
1312 goto error;
1313 }
1314
1315 init_waitqueue_head(&devinfo->wait);
1316 if (!brcmf_usb_dlneeded(devinfo))
1317 return &devinfo->bus_pub;
1318
1319 brcmf_dbg(TRACE, "start fw downloading\n");
1320 if (brcmf_usb_get_fw(devinfo))
1321 goto error;
1322
1323 if (brcmf_usb_fw_download(devinfo))
1324 goto error;
1325
1326 return &devinfo->bus_pub;
1327
1328error:
1329 brcmf_dbg(ERROR, "failed!\n");
d74a0b51 1330 brcmf_usb_detach(devinfo);
71bb244b
AS
1331 return NULL;
1332}
1333
d74a0b51
HM
1334static int brcmf_usb_probe_cb(struct brcmf_usbdev_info *devinfo,
1335 const char *desc, u32 bustype, u32 hdrlen)
71bb244b
AS
1336{
1337 struct brcmf_bus *bus = NULL;
1338 struct brcmf_usbdev *bus_pub = NULL;
1339 int ret;
d74a0b51 1340 struct device *dev = devinfo->dev;
71bb244b 1341
d74a0b51 1342 bus_pub = brcmf_usb_attach(devinfo, BRCMF_USB_NRXQ, BRCMF_USB_NTXQ);
71bb244b
AS
1343 if (!bus_pub) {
1344 ret = -ENODEV;
1345 goto fail;
1346 }
1347
1348 bus = kzalloc(sizeof(struct brcmf_bus), GFP_ATOMIC);
1349 if (!bus) {
1350 ret = -ENOMEM;
1351 goto fail;
1352 }
1353
1354 bus_pub->bus = bus;
1355 bus->brcmf_bus_txdata = brcmf_usb_tx;
1356 bus->brcmf_bus_init = brcmf_usb_up;
1357 bus->brcmf_bus_stop = brcmf_usb_down;
1358 bus->brcmf_bus_txctl = brcmf_usb_tx_ctlpkt;
1359 bus->brcmf_bus_rxctl = brcmf_usb_rx_ctlpkt;
1360 bus->type = bustype;
1361 bus->bus_priv.usb = bus_pub;
1362 dev_set_drvdata(dev, bus);
1363
1364 /* Attach to the common driver interface */
1365 ret = brcmf_attach(hdrlen, dev);
1366 if (ret) {
1367 brcmf_dbg(ERROR, "dhd_attach failed\n");
1368 goto fail;
1369 }
1370
1371 ret = brcmf_bus_start(dev);
1372 if (ret == -ENOLINK) {
1373 brcmf_dbg(ERROR, "dongle is not responding\n");
1374 brcmf_detach(dev);
1375 goto fail;
1376 }
1377
71bb244b
AS
1378 return 0;
1379fail:
1380 /* Release resources in reverse order */
71bb244b 1381 kfree(bus);
d74a0b51 1382 brcmf_usb_detach(devinfo);
71bb244b
AS
1383 return ret;
1384}
1385
1386static void
d74a0b51 1387brcmf_usb_disconnect_cb(struct brcmf_usbdev_info *devinfo)
71bb244b 1388{
d74a0b51 1389 if (!devinfo)
71bb244b 1390 return;
d74a0b51 1391 brcmf_dbg(TRACE, "enter: bus_pub %p\n", devinfo);
71bb244b 1392
d74a0b51
HM
1393 brcmf_detach(devinfo->dev);
1394 kfree(devinfo->bus_pub.bus);
1395 brcmf_usb_detach(devinfo);
71bb244b
AS
1396}
1397
1398static int
1399brcmf_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
1400{
1401 int ep;
1402 struct usb_endpoint_descriptor *endpoint;
1403 int ret = 0;
1404 struct usb_device *usb = interface_to_usbdev(intf);
1405 int num_of_eps;
1406 u8 endpoint_num;
d74a0b51 1407 struct brcmf_usbdev_info *devinfo;
71bb244b
AS
1408
1409 brcmf_dbg(TRACE, "enter\n");
1410
d74a0b51
HM
1411 devinfo = kzalloc(sizeof(*devinfo), GFP_ATOMIC);
1412 if (devinfo == NULL)
1413 return -ENOMEM;
71bb244b 1414
d74a0b51
HM
1415 devinfo->usbdev = usb;
1416 devinfo->dev = &usb->dev;
71bb244b 1417
d74a0b51 1418 usb_set_intfdata(intf, devinfo);
71bb244b
AS
1419
1420 /* Check that the device supports only one configuration */
1421 if (usb->descriptor.bNumConfigurations != 1) {
1422 ret = -1;
1423 goto fail;
1424 }
1425
1426 if (usb->descriptor.bDeviceClass != USB_CLASS_VENDOR_SPEC) {
1427 ret = -1;
1428 goto fail;
1429 }
1430
1431 /*
1432 * Only the BDC interface configuration is supported:
1433 * Device class: USB_CLASS_VENDOR_SPEC
1434 * if0 class: USB_CLASS_VENDOR_SPEC
1435 * if0/ep0: control
1436 * if0/ep1: bulk in
1437 * if0/ep2: bulk out (ok if swapped with bulk in)
1438 */
1439 if (CONFIGDESC(usb)->bNumInterfaces != 1) {
1440 ret = -1;
1441 goto fail;
1442 }
1443
1444 /* Check interface */
1445 if (IFDESC(usb, CONTROL_IF).bInterfaceClass != USB_CLASS_VENDOR_SPEC ||
1446 IFDESC(usb, CONTROL_IF).bInterfaceSubClass != 2 ||
1447 IFDESC(usb, CONTROL_IF).bInterfaceProtocol != 0xff) {
1448 brcmf_dbg(ERROR, "invalid control interface: class %d, subclass %d, proto %d\n",
1449 IFDESC(usb, CONTROL_IF).bInterfaceClass,
1450 IFDESC(usb, CONTROL_IF).bInterfaceSubClass,
1451 IFDESC(usb, CONTROL_IF).bInterfaceProtocol);
1452 ret = -1;
1453 goto fail;
1454 }
1455
1456 /* Check control endpoint */
1457 endpoint = &IFEPDESC(usb, CONTROL_IF, 0);
1458 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1459 != USB_ENDPOINT_XFER_INT) {
1460 brcmf_dbg(ERROR, "invalid control endpoint %d\n",
1461 endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
1462 ret = -1;
1463 goto fail;
1464 }
1465
1466 endpoint_num = endpoint->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
d74a0b51 1467 devinfo->intr_pipe = usb_rcvintpipe(usb, endpoint_num);
71bb244b 1468
d74a0b51
HM
1469 devinfo->rx_pipe = 0;
1470 devinfo->rx_pipe2 = 0;
1471 devinfo->tx_pipe = 0;
71bb244b
AS
1472 num_of_eps = IFDESC(usb, BULK_IF).bNumEndpoints - 1;
1473
1474 /* Check data endpoints and get pipes */
1475 for (ep = 1; ep <= num_of_eps; ep++) {
1476 endpoint = &IFEPDESC(usb, BULK_IF, ep);
1477 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
1478 USB_ENDPOINT_XFER_BULK) {
1479 brcmf_dbg(ERROR, "invalid data endpoint %d\n", ep);
1480 ret = -1;
1481 goto fail;
1482 }
1483
1484 endpoint_num = endpoint->bEndpointAddress &
1485 USB_ENDPOINT_NUMBER_MASK;
1486 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1487 == USB_DIR_IN) {
d74a0b51
HM
1488 if (!devinfo->rx_pipe) {
1489 devinfo->rx_pipe =
71bb244b
AS
1490 usb_rcvbulkpipe(usb, endpoint_num);
1491 } else {
d74a0b51 1492 devinfo->rx_pipe2 =
71bb244b
AS
1493 usb_rcvbulkpipe(usb, endpoint_num);
1494 }
1495 } else {
d74a0b51 1496 devinfo->tx_pipe = usb_sndbulkpipe(usb, endpoint_num);
71bb244b
AS
1497 }
1498 }
1499
1500 /* Allocate interrupt URB and data buffer */
1501 /* RNDIS says 8-byte intr, our old drivers used 4-byte */
1502 if (IFEPDESC(usb, CONTROL_IF, 0).wMaxPacketSize == cpu_to_le16(16))
d74a0b51 1503 devinfo->intr_size = 8;
71bb244b 1504 else
d74a0b51 1505 devinfo->intr_size = 4;
71bb244b 1506
d74a0b51 1507 devinfo->interval = IFEPDESC(usb, CONTROL_IF, 0).bInterval;
71bb244b 1508
71bb244b
AS
1509 if (usb->speed == USB_SPEED_HIGH)
1510 brcmf_dbg(INFO, "Broadcom high speed USB wireless device detected\n");
1511 else
1512 brcmf_dbg(INFO, "Broadcom full speed USB wireless device detected\n");
1513
d74a0b51 1514 ret = brcmf_usb_probe_cb(devinfo, "", USB_BUS, 0);
71bb244b
AS
1515 if (ret)
1516 goto fail;
1517
1518 /* Success */
1519 return 0;
1520
1521fail:
1522 brcmf_dbg(ERROR, "failed with errno %d\n", ret);
d74a0b51 1523 kfree(devinfo);
71bb244b
AS
1524 usb_set_intfdata(intf, NULL);
1525 return ret;
1526
1527}
1528
1529static void
1530brcmf_usb_disconnect(struct usb_interface *intf)
1531{
d74a0b51 1532 struct brcmf_usbdev_info *devinfo;
71bb244b
AS
1533
1534 brcmf_dbg(TRACE, "enter\n");
d74a0b51
HM
1535 devinfo = (struct brcmf_usbdev_info *)usb_get_intfdata(intf);
1536 brcmf_usb_disconnect_cb(devinfo);
1537 kfree(devinfo);
71bb244b
AS
1538}
1539
1540/*
1541 * only need to signal the bus being down and update the suspend state.
1542 */
1543static int brcmf_usb_suspend(struct usb_interface *intf, pm_message_t state)
1544{
1545 struct usb_device *usb = interface_to_usbdev(intf);
1546 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(&usb->dev);
1547
1548 brcmf_dbg(TRACE, "enter\n");
1549 devinfo->bus_pub.state = BCMFMAC_USB_STATE_DOWN;
1550 devinfo->suspend_state = USBOS_SUSPEND_STATE_SUSPENDED;
1551 return 0;
1552}
1553
1554/*
1555 * mark suspend state active and crank up the bus.
1556 */
1557static int brcmf_usb_resume(struct usb_interface *intf)
1558{
1559 struct usb_device *usb = interface_to_usbdev(intf);
1560 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(&usb->dev);
1561
1562 brcmf_dbg(TRACE, "enter\n");
1563 devinfo->suspend_state = USBOS_SUSPEND_STATE_DEVICE_ACTIVE;
1564 brcmf_bus_start(&usb->dev);
1565 return 0;
1566}
1567
1568#define BRCMF_USB_VENDOR_ID_BROADCOM 0x0a5c
70f0822c 1569#define BRCMF_USB_DEVICE_ID_43143 0xbd1e
71bb244b 1570#define BRCMF_USB_DEVICE_ID_43236 0xbd17
1212d370 1571#define BRCMF_USB_DEVICE_ID_43242 0xbd1f
71bb244b
AS
1572#define BRCMF_USB_DEVICE_ID_BCMFW 0x0bdc
1573
1574static struct usb_device_id brcmf_usb_devid_table[] = {
70f0822c 1575 { USB_DEVICE(BRCMF_USB_VENDOR_ID_BROADCOM, BRCMF_USB_DEVICE_ID_43143) },
71bb244b 1576 { USB_DEVICE(BRCMF_USB_VENDOR_ID_BROADCOM, BRCMF_USB_DEVICE_ID_43236) },
1212d370 1577 { USB_DEVICE(BRCMF_USB_VENDOR_ID_BROADCOM, BRCMF_USB_DEVICE_ID_43242) },
71bb244b
AS
1578 /* special entry for device with firmware loaded and running */
1579 { USB_DEVICE(BRCMF_USB_VENDOR_ID_BROADCOM, BRCMF_USB_DEVICE_ID_BCMFW) },
1580 { }
1581};
1582MODULE_DEVICE_TABLE(usb, brcmf_usb_devid_table);
70f0822c 1583MODULE_FIRMWARE(BRCMF_USB_43143_FW_NAME);
fda82417 1584MODULE_FIRMWARE(BRCMF_USB_43236_FW_NAME);
1212d370 1585MODULE_FIRMWARE(BRCMF_USB_43242_FW_NAME);
71bb244b
AS
1586
1587/* TODO: suspend and resume entries */
1588static struct usb_driver brcmf_usbdrvr = {
1589 .name = KBUILD_MODNAME,
1590 .probe = brcmf_usb_probe,
1591 .disconnect = brcmf_usb_disconnect,
1592 .id_table = brcmf_usb_devid_table,
1593 .suspend = brcmf_usb_suspend,
1594 .resume = brcmf_usb_resume,
c51fa668 1595 .supports_autosuspend = 1,
e1f12eb6 1596 .disable_hub_initiated_lpm = 1,
71bb244b
AS
1597};
1598
1599void brcmf_usb_exit(void)
1600{
1601 usb_deregister(&brcmf_usbdrvr);
edb9bc9a 1602 vfree(g_image.data);
71bb244b
AS
1603 g_image.data = NULL;
1604 g_image.len = 0;
1605}
1606
549040ab 1607void brcmf_usb_init(void)
71bb244b 1608{
549040ab 1609 usb_register(&brcmf_usbdrvr);
71bb244b 1610}