2 * Copyright (c) 2011 Broadcom Corporation
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.
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.
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/firmware.h>
20 #include <linux/usb.h>
21 #include <linux/vmalloc.h>
23 #include <brcmu_utils.h>
24 #include <brcm_hw_ids.h>
25 #include <brcmu_wifi.h>
33 #define IOCTL_RESP_TIMEOUT 2000
35 #define BRCMF_USB_RESET_GETVER_SPINWAIT 100 /* in unit of ms */
36 #define BRCMF_USB_RESET_GETVER_LOOP_CNT 10
38 #define BRCMF_POSTBOOT_ID 0xA123 /* ID to detect if dongle
40 #define BRCMF_USB_NRXQ 50
41 #define BRCMF_USB_NTXQ 50
43 #define BRCMF_USB_CBCTL_WRITE 0
44 #define BRCMF_USB_CBCTL_READ 1
45 #define BRCMF_USB_MAX_PKT_SIZE 1600
47 #define BRCMF_USB_43143_FW_NAME "brcm/brcmfmac43143.bin"
48 #define BRCMF_USB_43236_FW_NAME "brcm/brcmfmac43236b.bin"
49 #define BRCMF_USB_43242_FW_NAME "brcm/brcmfmac43242a.bin"
50 #define BRCMF_USB_43569_FW_NAME "brcm/brcmfmac43569.bin"
52 struct brcmf_usb_image
{
53 struct list_head list
;
59 struct brcmf_usbdev_info
{
60 struct brcmf_usbdev bus_pub
; /* MUST BE FIRST */
62 struct list_head rx_freeq
;
63 struct list_head rx_postq
;
64 struct list_head tx_freeq
;
65 struct list_head tx_postq
;
66 uint rx_pipe
, tx_pipe
;
70 int tx_high_watermark
;
73 spinlock_t tx_flowblock_lock
;
75 struct brcmf_usbreq
*tx_reqs
;
76 struct brcmf_usbreq
*rx_reqs
;
78 const u8
*image
; /* buffer for combine fw and nvram */
81 struct usb_device
*usbdev
;
84 int ctl_in_pipe
, ctl_out_pipe
;
85 struct urb
*ctl_urb
; /* URB for control endpoint */
86 struct usb_ctrlrequest ctl_write
;
87 struct usb_ctrlrequest ctl_read
;
88 u32 ctl_urb_actual_length
;
91 wait_queue_head_t ioctl_resp_wait
;
95 struct urb
*bulk_urb
; /* used for FW download */
98 static void brcmf_usb_rx_refill(struct brcmf_usbdev_info
*devinfo
,
99 struct brcmf_usbreq
*req
);
101 static struct brcmf_usbdev
*brcmf_usb_get_buspub(struct device
*dev
)
103 struct brcmf_bus
*bus_if
= dev_get_drvdata(dev
);
104 return bus_if
->bus_priv
.usb
;
107 static struct brcmf_usbdev_info
*brcmf_usb_get_businfo(struct device
*dev
)
109 return brcmf_usb_get_buspub(dev
)->devinfo
;
112 static int brcmf_usb_ioctl_resp_wait(struct brcmf_usbdev_info
*devinfo
)
114 return wait_event_timeout(devinfo
->ioctl_resp_wait
,
115 devinfo
->ctl_completed
,
116 msecs_to_jiffies(IOCTL_RESP_TIMEOUT
));
119 static void brcmf_usb_ioctl_resp_wake(struct brcmf_usbdev_info
*devinfo
)
121 if (waitqueue_active(&devinfo
->ioctl_resp_wait
))
122 wake_up(&devinfo
->ioctl_resp_wait
);
126 brcmf_usb_ctl_complete(struct brcmf_usbdev_info
*devinfo
, int type
, int status
)
128 brcmf_dbg(USB
, "Enter, status=%d\n", status
);
130 if (unlikely(devinfo
== NULL
))
133 if (type
== BRCMF_USB_CBCTL_READ
) {
135 devinfo
->bus_pub
.stats
.rx_ctlpkts
++;
137 devinfo
->bus_pub
.stats
.rx_ctlerrs
++;
138 } else if (type
== BRCMF_USB_CBCTL_WRITE
) {
140 devinfo
->bus_pub
.stats
.tx_ctlpkts
++;
142 devinfo
->bus_pub
.stats
.tx_ctlerrs
++;
145 devinfo
->ctl_urb_status
= status
;
146 devinfo
->ctl_completed
= true;
147 brcmf_usb_ioctl_resp_wake(devinfo
);
151 brcmf_usb_ctlread_complete(struct urb
*urb
)
153 struct brcmf_usbdev_info
*devinfo
=
154 (struct brcmf_usbdev_info
*)urb
->context
;
156 brcmf_dbg(USB
, "Enter\n");
157 devinfo
->ctl_urb_actual_length
= urb
->actual_length
;
158 brcmf_usb_ctl_complete(devinfo
, BRCMF_USB_CBCTL_READ
,
163 brcmf_usb_ctlwrite_complete(struct urb
*urb
)
165 struct brcmf_usbdev_info
*devinfo
=
166 (struct brcmf_usbdev_info
*)urb
->context
;
168 brcmf_dbg(USB
, "Enter\n");
169 brcmf_usb_ctl_complete(devinfo
, BRCMF_USB_CBCTL_WRITE
,
174 brcmf_usb_send_ctl(struct brcmf_usbdev_info
*devinfo
, u8
*buf
, int len
)
179 brcmf_dbg(USB
, "Enter\n");
180 if (devinfo
== NULL
|| buf
== NULL
||
181 len
== 0 || devinfo
->ctl_urb
== NULL
)
185 devinfo
->ctl_write
.wLength
= cpu_to_le16p(&size
);
186 devinfo
->ctl_urb
->transfer_buffer_length
= size
;
187 devinfo
->ctl_urb_status
= 0;
188 devinfo
->ctl_urb_actual_length
= 0;
190 usb_fill_control_urb(devinfo
->ctl_urb
,
192 devinfo
->ctl_out_pipe
,
193 (unsigned char *) &devinfo
->ctl_write
,
195 (usb_complete_t
)brcmf_usb_ctlwrite_complete
,
198 ret
= usb_submit_urb(devinfo
->ctl_urb
, GFP_ATOMIC
);
200 brcmf_err("usb_submit_urb failed %d\n", ret
);
206 brcmf_usb_recv_ctl(struct brcmf_usbdev_info
*devinfo
, u8
*buf
, int len
)
211 brcmf_dbg(USB
, "Enter\n");
212 if ((devinfo
== NULL
) || (buf
== NULL
) || (len
== 0)
213 || (devinfo
->ctl_urb
== NULL
))
217 devinfo
->ctl_read
.wLength
= cpu_to_le16p(&size
);
218 devinfo
->ctl_urb
->transfer_buffer_length
= size
;
220 devinfo
->ctl_read
.bRequestType
= USB_DIR_IN
221 | USB_TYPE_CLASS
| USB_RECIP_INTERFACE
;
222 devinfo
->ctl_read
.bRequest
= 1;
224 usb_fill_control_urb(devinfo
->ctl_urb
,
226 devinfo
->ctl_in_pipe
,
227 (unsigned char *) &devinfo
->ctl_read
,
229 (usb_complete_t
)brcmf_usb_ctlread_complete
,
232 ret
= usb_submit_urb(devinfo
->ctl_urb
, GFP_ATOMIC
);
234 brcmf_err("usb_submit_urb failed %d\n", ret
);
239 static int brcmf_usb_tx_ctlpkt(struct device
*dev
, u8
*buf
, u32 len
)
243 struct brcmf_usbdev_info
*devinfo
= brcmf_usb_get_businfo(dev
);
245 brcmf_dbg(USB
, "Enter\n");
246 if (devinfo
->bus_pub
.state
!= BRCMFMAC_USB_STATE_UP
)
249 if (test_and_set_bit(0, &devinfo
->ctl_op
))
252 devinfo
->ctl_completed
= false;
253 err
= brcmf_usb_send_ctl(devinfo
, buf
, len
);
255 brcmf_err("fail %d bytes: %d\n", err
, len
);
256 clear_bit(0, &devinfo
->ctl_op
);
259 timeout
= brcmf_usb_ioctl_resp_wait(devinfo
);
260 clear_bit(0, &devinfo
->ctl_op
);
262 brcmf_err("Txctl wait timed out\n");
268 static int brcmf_usb_rx_ctlpkt(struct device
*dev
, u8
*buf
, u32 len
)
272 struct brcmf_usbdev_info
*devinfo
= brcmf_usb_get_businfo(dev
);
274 brcmf_dbg(USB
, "Enter\n");
275 if (devinfo
->bus_pub
.state
!= BRCMFMAC_USB_STATE_UP
)
278 if (test_and_set_bit(0, &devinfo
->ctl_op
))
281 devinfo
->ctl_completed
= false;
282 err
= brcmf_usb_recv_ctl(devinfo
, buf
, len
);
284 brcmf_err("fail %d bytes: %d\n", err
, len
);
285 clear_bit(0, &devinfo
->ctl_op
);
288 timeout
= brcmf_usb_ioctl_resp_wait(devinfo
);
289 err
= devinfo
->ctl_urb_status
;
290 clear_bit(0, &devinfo
->ctl_op
);
292 brcmf_err("rxctl wait timed out\n");
296 return devinfo
->ctl_urb_actual_length
;
301 static struct brcmf_usbreq
*brcmf_usb_deq(struct brcmf_usbdev_info
*devinfo
,
302 struct list_head
*q
, int *counter
)
305 struct brcmf_usbreq
*req
;
306 spin_lock_irqsave(&devinfo
->qlock
, flags
);
308 spin_unlock_irqrestore(&devinfo
->qlock
, flags
);
311 req
= list_entry(q
->next
, struct brcmf_usbreq
, list
);
312 list_del_init(q
->next
);
315 spin_unlock_irqrestore(&devinfo
->qlock
, flags
);
320 static void brcmf_usb_enq(struct brcmf_usbdev_info
*devinfo
,
321 struct list_head
*q
, struct brcmf_usbreq
*req
,
325 spin_lock_irqsave(&devinfo
->qlock
, flags
);
326 list_add_tail(&req
->list
, q
);
329 spin_unlock_irqrestore(&devinfo
->qlock
, flags
);
332 static struct brcmf_usbreq
*
333 brcmf_usbdev_qinit(struct list_head
*q
, int qsize
)
336 struct brcmf_usbreq
*req
, *reqs
;
338 reqs
= kcalloc(qsize
, sizeof(struct brcmf_usbreq
), GFP_ATOMIC
);
344 for (i
= 0; i
< qsize
; i
++) {
345 req
->urb
= usb_alloc_urb(0, GFP_ATOMIC
);
349 INIT_LIST_HEAD(&req
->list
);
350 list_add_tail(&req
->list
, q
);
355 brcmf_err("fail!\n");
356 while (!list_empty(q
)) {
357 req
= list_entry(q
->next
, struct brcmf_usbreq
, list
);
359 usb_free_urb(req
->urb
);
366 static void brcmf_usb_free_q(struct list_head
*q
, bool pending
)
368 struct brcmf_usbreq
*req
, *next
;
370 list_for_each_entry_safe(req
, next
, q
, list
) {
372 brcmf_err("bad req\n");
377 usb_kill_urb(req
->urb
);
379 usb_free_urb(req
->urb
);
380 list_del_init(&req
->list
);
385 static void brcmf_usb_del_fromq(struct brcmf_usbdev_info
*devinfo
,
386 struct brcmf_usbreq
*req
)
390 spin_lock_irqsave(&devinfo
->qlock
, flags
);
391 list_del_init(&req
->list
);
392 spin_unlock_irqrestore(&devinfo
->qlock
, flags
);
396 static void brcmf_usb_tx_complete(struct urb
*urb
)
398 struct brcmf_usbreq
*req
= (struct brcmf_usbreq
*)urb
->context
;
399 struct brcmf_usbdev_info
*devinfo
= req
->devinfo
;
402 brcmf_dbg(USB
, "Enter, urb->status=%d, skb=%p\n", urb
->status
,
404 brcmf_usb_del_fromq(devinfo
, req
);
406 brcmf_txcomplete(devinfo
->dev
, req
->skb
, urb
->status
== 0);
408 brcmf_usb_enq(devinfo
, &devinfo
->tx_freeq
, req
, &devinfo
->tx_freecount
);
409 spin_lock_irqsave(&devinfo
->tx_flowblock_lock
, flags
);
410 if (devinfo
->tx_freecount
> devinfo
->tx_high_watermark
&&
411 devinfo
->tx_flowblock
) {
412 brcmf_txflowblock(devinfo
->dev
, false);
413 devinfo
->tx_flowblock
= false;
415 spin_unlock_irqrestore(&devinfo
->tx_flowblock_lock
, flags
);
418 static void brcmf_usb_rx_complete(struct urb
*urb
)
420 struct brcmf_usbreq
*req
= (struct brcmf_usbreq
*)urb
->context
;
421 struct brcmf_usbdev_info
*devinfo
= req
->devinfo
;
424 brcmf_dbg(USB
, "Enter, urb->status=%d\n", urb
->status
);
425 brcmf_usb_del_fromq(devinfo
, req
);
429 /* zero lenght packets indicate usb "failure". Do not refill */
430 if (urb
->status
!= 0 || !urb
->actual_length
) {
431 brcmu_pkt_buf_free_skb(skb
);
432 brcmf_usb_enq(devinfo
, &devinfo
->rx_freeq
, req
, NULL
);
436 if (devinfo
->bus_pub
.state
== BRCMFMAC_USB_STATE_UP
) {
437 skb_put(skb
, urb
->actual_length
);
438 brcmf_rx_frame(devinfo
->dev
, skb
);
439 brcmf_usb_rx_refill(devinfo
, req
);
441 brcmu_pkt_buf_free_skb(skb
);
442 brcmf_usb_enq(devinfo
, &devinfo
->rx_freeq
, req
, NULL
);
448 static void brcmf_usb_rx_refill(struct brcmf_usbdev_info
*devinfo
,
449 struct brcmf_usbreq
*req
)
454 if (!req
|| !devinfo
)
457 skb
= dev_alloc_skb(devinfo
->bus_pub
.bus_mtu
);
459 brcmf_usb_enq(devinfo
, &devinfo
->rx_freeq
, req
, NULL
);
464 usb_fill_bulk_urb(req
->urb
, devinfo
->usbdev
, devinfo
->rx_pipe
,
465 skb
->data
, skb_tailroom(skb
), brcmf_usb_rx_complete
,
467 req
->devinfo
= devinfo
;
468 brcmf_usb_enq(devinfo
, &devinfo
->rx_postq
, req
, NULL
);
470 ret
= usb_submit_urb(req
->urb
, GFP_ATOMIC
);
472 brcmf_usb_del_fromq(devinfo
, req
);
473 brcmu_pkt_buf_free_skb(req
->skb
);
475 brcmf_usb_enq(devinfo
, &devinfo
->rx_freeq
, req
, NULL
);
480 static void brcmf_usb_rx_fill_all(struct brcmf_usbdev_info
*devinfo
)
482 struct brcmf_usbreq
*req
;
484 if (devinfo
->bus_pub
.state
!= BRCMFMAC_USB_STATE_UP
) {
485 brcmf_err("bus is not up=%d\n", devinfo
->bus_pub
.state
);
488 while ((req
= brcmf_usb_deq(devinfo
, &devinfo
->rx_freeq
, NULL
)) != NULL
)
489 brcmf_usb_rx_refill(devinfo
, req
);
493 brcmf_usb_state_change(struct brcmf_usbdev_info
*devinfo
, int state
)
495 struct brcmf_bus
*bcmf_bus
= devinfo
->bus_pub
.bus
;
498 brcmf_dbg(USB
, "Enter, current state=%d, new state=%d\n",
499 devinfo
->bus_pub
.state
, state
);
501 if (devinfo
->bus_pub
.state
== state
)
504 old_state
= devinfo
->bus_pub
.state
;
505 devinfo
->bus_pub
.state
= state
;
507 /* update state of upper layer */
508 if (state
== BRCMFMAC_USB_STATE_DOWN
) {
509 brcmf_dbg(USB
, "DBUS is down\n");
510 brcmf_bus_change_state(bcmf_bus
, BRCMF_BUS_DOWN
);
511 } else if (state
== BRCMFMAC_USB_STATE_UP
) {
512 brcmf_dbg(USB
, "DBUS is up\n");
513 brcmf_bus_change_state(bcmf_bus
, BRCMF_BUS_DATA
);
515 brcmf_dbg(USB
, "DBUS current state=%d\n", state
);
519 static int brcmf_usb_tx(struct device
*dev
, struct sk_buff
*skb
)
521 struct brcmf_usbdev_info
*devinfo
= brcmf_usb_get_businfo(dev
);
522 struct brcmf_usbreq
*req
;
526 brcmf_dbg(USB
, "Enter, skb=%p\n", skb
);
527 if (devinfo
->bus_pub
.state
!= BRCMFMAC_USB_STATE_UP
) {
532 req
= brcmf_usb_deq(devinfo
, &devinfo
->tx_freeq
,
533 &devinfo
->tx_freecount
);
535 brcmf_err("no req to send\n");
541 req
->devinfo
= devinfo
;
542 usb_fill_bulk_urb(req
->urb
, devinfo
->usbdev
, devinfo
->tx_pipe
,
543 skb
->data
, skb
->len
, brcmf_usb_tx_complete
, req
);
544 req
->urb
->transfer_flags
|= URB_ZERO_PACKET
;
545 brcmf_usb_enq(devinfo
, &devinfo
->tx_postq
, req
, NULL
);
546 ret
= usb_submit_urb(req
->urb
, GFP_ATOMIC
);
548 brcmf_err("brcmf_usb_tx usb_submit_urb FAILED\n");
549 brcmf_usb_del_fromq(devinfo
, req
);
551 brcmf_usb_enq(devinfo
, &devinfo
->tx_freeq
, req
,
552 &devinfo
->tx_freecount
);
556 spin_lock_irqsave(&devinfo
->tx_flowblock_lock
, flags
);
557 if (devinfo
->tx_freecount
< devinfo
->tx_low_watermark
&&
558 !devinfo
->tx_flowblock
) {
559 brcmf_txflowblock(dev
, true);
560 devinfo
->tx_flowblock
= true;
562 spin_unlock_irqrestore(&devinfo
->tx_flowblock_lock
, flags
);
570 static int brcmf_usb_up(struct device
*dev
)
572 struct brcmf_usbdev_info
*devinfo
= brcmf_usb_get_businfo(dev
);
574 brcmf_dbg(USB
, "Enter\n");
575 if (devinfo
->bus_pub
.state
== BRCMFMAC_USB_STATE_UP
)
578 /* Success, indicate devinfo is fully up */
579 brcmf_usb_state_change(devinfo
, BRCMFMAC_USB_STATE_UP
);
581 if (devinfo
->ctl_urb
) {
582 devinfo
->ctl_in_pipe
= usb_rcvctrlpipe(devinfo
->usbdev
, 0);
583 devinfo
->ctl_out_pipe
= usb_sndctrlpipe(devinfo
->usbdev
, 0);
586 devinfo
->ctl_write
.bRequestType
=
587 USB_DIR_OUT
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
;
588 devinfo
->ctl_write
.bRequest
= 0;
589 devinfo
->ctl_write
.wValue
= cpu_to_le16(0);
590 devinfo
->ctl_write
.wIndex
= cpu_to_le16(devinfo
->ifnum
);
593 devinfo
->ctl_read
.bRequestType
=
594 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
;
595 devinfo
->ctl_read
.bRequest
= 1;
596 devinfo
->ctl_read
.wValue
= cpu_to_le16(0);
597 devinfo
->ctl_read
.wIndex
= cpu_to_le16(devinfo
->ifnum
);
599 brcmf_usb_rx_fill_all(devinfo
);
603 static void brcmf_usb_down(struct device
*dev
)
605 struct brcmf_usbdev_info
*devinfo
= brcmf_usb_get_businfo(dev
);
607 brcmf_dbg(USB
, "Enter\n");
611 if (devinfo
->bus_pub
.state
== BRCMFMAC_USB_STATE_DOWN
)
614 brcmf_usb_state_change(devinfo
, BRCMFMAC_USB_STATE_DOWN
);
616 if (devinfo
->ctl_urb
)
617 usb_kill_urb(devinfo
->ctl_urb
);
619 if (devinfo
->bulk_urb
)
620 usb_kill_urb(devinfo
->bulk_urb
);
621 brcmf_usb_free_q(&devinfo
->tx_postq
, true);
623 brcmf_usb_free_q(&devinfo
->rx_postq
, true);
627 brcmf_usb_sync_complete(struct urb
*urb
)
629 struct brcmf_usbdev_info
*devinfo
=
630 (struct brcmf_usbdev_info
*)urb
->context
;
632 devinfo
->ctl_completed
= true;
633 brcmf_usb_ioctl_resp_wake(devinfo
);
636 static int brcmf_usb_dl_cmd(struct brcmf_usbdev_info
*devinfo
, u8 cmd
,
637 void *buffer
, int buflen
)
643 if ((!devinfo
) || (devinfo
->ctl_urb
== NULL
))
646 tmpbuf
= kmalloc(buflen
, GFP_ATOMIC
);
651 devinfo
->ctl_urb
->transfer_buffer_length
= size
;
653 devinfo
->ctl_read
.wLength
= cpu_to_le16p(&size
);
654 devinfo
->ctl_read
.bRequestType
= USB_DIR_IN
| USB_TYPE_VENDOR
|
656 devinfo
->ctl_read
.bRequest
= cmd
;
658 usb_fill_control_urb(devinfo
->ctl_urb
,
660 usb_rcvctrlpipe(devinfo
->usbdev
, 0),
661 (unsigned char *) &devinfo
->ctl_read
,
662 (void *) tmpbuf
, size
,
663 (usb_complete_t
)brcmf_usb_sync_complete
, devinfo
);
665 devinfo
->ctl_completed
= false;
666 ret
= usb_submit_urb(devinfo
->ctl_urb
, GFP_ATOMIC
);
668 brcmf_err("usb_submit_urb failed %d\n", ret
);
672 if (!brcmf_usb_ioctl_resp_wait(devinfo
))
675 memcpy(buffer
, tmpbuf
, buflen
);
683 brcmf_usb_dlneeded(struct brcmf_usbdev_info
*devinfo
)
685 struct bootrom_id_le id
;
688 brcmf_dbg(USB
, "Enter\n");
693 /* Check if firmware downloaded already by querying runtime ID */
694 id
.chip
= cpu_to_le32(0xDEAD);
695 brcmf_usb_dl_cmd(devinfo
, DL_GETVER
, &id
, sizeof(id
));
697 chipid
= le32_to_cpu(id
.chip
);
698 chiprev
= le32_to_cpu(id
.chiprev
);
700 if ((chipid
& 0x4300) == 0x4300)
701 brcmf_dbg(USB
, "chip %x rev 0x%x\n", chipid
, chiprev
);
703 brcmf_dbg(USB
, "chip %d rev 0x%x\n", chipid
, chiprev
);
704 if (chipid
== BRCMF_POSTBOOT_ID
) {
705 brcmf_dbg(USB
, "firmware already downloaded\n");
706 brcmf_usb_dl_cmd(devinfo
, DL_RESETCFG
, &id
, sizeof(id
));
709 devinfo
->bus_pub
.devid
= chipid
;
710 devinfo
->bus_pub
.chiprev
= chiprev
;
716 brcmf_usb_resetcfg(struct brcmf_usbdev_info
*devinfo
)
718 struct bootrom_id_le id
;
722 brcmf_dbg(USB
, "Enter\n");
726 mdelay(BRCMF_USB_RESET_GETVER_SPINWAIT
);
728 id
.chip
= cpu_to_le32(0xDEAD); /* Get the ID */
729 err
= brcmf_usb_dl_cmd(devinfo
, DL_GETVER
, &id
, sizeof(id
));
730 if ((err
) && (err
!= -ETIMEDOUT
))
732 if (id
.chip
== cpu_to_le32(BRCMF_POSTBOOT_ID
))
734 } while (loop_cnt
< BRCMF_USB_RESET_GETVER_LOOP_CNT
);
736 if (id
.chip
== cpu_to_le32(BRCMF_POSTBOOT_ID
)) {
737 brcmf_dbg(USB
, "postboot chip 0x%x/rev 0x%x\n",
738 le32_to_cpu(id
.chip
), le32_to_cpu(id
.chiprev
));
740 brcmf_usb_dl_cmd(devinfo
, DL_RESETCFG
, &id
, sizeof(id
));
743 brcmf_err("Cannot talk to Dongle. Firmware is not UP, %d ms\n",
744 BRCMF_USB_RESET_GETVER_SPINWAIT
* loop_cnt
);
751 brcmf_usb_dl_send_bulk(struct brcmf_usbdev_info
*devinfo
, void *buffer
, int len
)
755 if ((devinfo
== NULL
) || (devinfo
->bulk_urb
== NULL
))
758 /* Prepare the URB */
759 usb_fill_bulk_urb(devinfo
->bulk_urb
, devinfo
->usbdev
,
760 devinfo
->tx_pipe
, buffer
, len
,
761 (usb_complete_t
)brcmf_usb_sync_complete
, devinfo
);
763 devinfo
->bulk_urb
->transfer_flags
|= URB_ZERO_PACKET
;
765 devinfo
->ctl_completed
= false;
766 ret
= usb_submit_urb(devinfo
->bulk_urb
, GFP_ATOMIC
);
768 brcmf_err("usb_submit_urb failed %d\n", ret
);
771 ret
= brcmf_usb_ioctl_resp_wait(devinfo
);
776 brcmf_usb_dl_writeimage(struct brcmf_usbdev_info
*devinfo
, u8
*fw
, int fwlen
)
778 unsigned int sendlen
, sent
, dllen
;
779 char *bulkchunk
= NULL
, *dlpos
;
780 struct rdl_state_le state
;
781 u32 rdlstate
, rdlbytes
;
784 brcmf_dbg(USB
, "Enter, fw %p, len %d\n", fw
, fwlen
);
786 bulkchunk
= kmalloc(RDL_CHUNK
, GFP_ATOMIC
);
787 if (bulkchunk
== NULL
) {
792 /* 1) Prepare USB boot loader for runtime image */
793 brcmf_usb_dl_cmd(devinfo
, DL_START
, &state
, sizeof(state
));
795 rdlstate
= le32_to_cpu(state
.state
);
796 rdlbytes
= le32_to_cpu(state
.bytes
);
798 /* 2) Check we are in the Waiting state */
799 if (rdlstate
!= DL_WAITING
) {
800 brcmf_err("Failed to DL_START\n");
808 /* Get chip id and rev */
809 while (rdlbytes
!= dllen
) {
810 /* Wait until the usb device reports it received all
811 * the bytes we sent */
812 if ((rdlbytes
== sent
) && (rdlbytes
!= dllen
)) {
813 if ((dllen
-sent
) < RDL_CHUNK
)
814 sendlen
= dllen
-sent
;
818 /* simply avoid having to send a ZLP by ensuring we
826 memcpy(bulkchunk
, dlpos
, sendlen
);
827 if (brcmf_usb_dl_send_bulk(devinfo
, bulkchunk
,
829 brcmf_err("send_bulk failed\n");
837 err
= brcmf_usb_dl_cmd(devinfo
, DL_GETSTATE
, &state
,
840 brcmf_err("DL_GETSTATE Failed\n");
844 rdlstate
= le32_to_cpu(state
.state
);
845 rdlbytes
= le32_to_cpu(state
.bytes
);
847 /* restart if an error is reported */
848 if (rdlstate
== DL_BAD_HDR
|| rdlstate
== DL_BAD_CRC
) {
849 brcmf_err("Bad Hdr or Bad CRC state %d\n",
858 brcmf_dbg(USB
, "Exit, err=%d\n", err
);
862 static int brcmf_usb_dlstart(struct brcmf_usbdev_info
*devinfo
, u8
*fw
, int len
)
866 brcmf_dbg(USB
, "Enter\n");
871 if (devinfo
->bus_pub
.devid
== 0xDEAD)
874 err
= brcmf_usb_dl_writeimage(devinfo
, fw
, len
);
876 devinfo
->bus_pub
.state
= BRCMFMAC_USB_STATE_DL_DONE
;
878 devinfo
->bus_pub
.state
= BRCMFMAC_USB_STATE_DL_FAIL
;
879 brcmf_dbg(USB
, "Exit, err=%d\n", err
);
884 static int brcmf_usb_dlrun(struct brcmf_usbdev_info
*devinfo
)
886 struct rdl_state_le state
;
888 brcmf_dbg(USB
, "Enter\n");
892 if (devinfo
->bus_pub
.devid
== 0xDEAD)
895 /* Check we are runnable */
897 brcmf_usb_dl_cmd(devinfo
, DL_GETSTATE
, &state
, sizeof(state
));
899 /* Start the image */
900 if (state
.state
== cpu_to_le32(DL_RUNNABLE
)) {
901 if (brcmf_usb_dl_cmd(devinfo
, DL_GO
, &state
, sizeof(state
)))
903 if (brcmf_usb_resetcfg(devinfo
))
905 /* The Dongle may go for re-enumeration. */
907 brcmf_err("Dongle not runnable\n");
910 brcmf_dbg(USB
, "Exit\n");
914 static bool brcmf_usb_chip_support(int chipid
, int chiprev
)
917 case BRCM_CC_43143_CHIP_ID
:
919 case BRCM_CC_43235_CHIP_ID
:
920 case BRCM_CC_43236_CHIP_ID
:
921 case BRCM_CC_43238_CHIP_ID
:
922 return (chiprev
== 3);
923 case BRCM_CC_43242_CHIP_ID
:
925 case BRCM_CC_43566_CHIP_ID
:
926 case BRCM_CC_43569_CHIP_ID
:
935 brcmf_usb_fw_download(struct brcmf_usbdev_info
*devinfo
)
940 brcmf_dbg(USB
, "Enter\n");
944 devid
= devinfo
->bus_pub
.devid
;
945 chiprev
= devinfo
->bus_pub
.chiprev
;
947 if (!brcmf_usb_chip_support(devid
, chiprev
)) {
948 brcmf_err("unsupported chip %d rev %d\n",
953 if (!devinfo
->image
) {
954 brcmf_err("No firmware!\n");
958 err
= brcmf_usb_dlstart(devinfo
,
959 (u8
*)devinfo
->image
, devinfo
->image_len
);
961 err
= brcmf_usb_dlrun(devinfo
);
966 static void brcmf_usb_detach(struct brcmf_usbdev_info
*devinfo
)
968 brcmf_dbg(USB
, "Enter, devinfo %p\n", devinfo
);
971 brcmf_usb_free_q(&devinfo
->rx_freeq
, false);
972 brcmf_usb_free_q(&devinfo
->tx_freeq
, false);
974 usb_free_urb(devinfo
->ctl_urb
);
975 usb_free_urb(devinfo
->bulk_urb
);
977 kfree(devinfo
->tx_reqs
);
978 kfree(devinfo
->rx_reqs
);
981 #define TRX_MAGIC 0x30524448 /* "HDR0" */
982 #define TRX_VERSION 1 /* Version 1 */
983 #define TRX_MAX_LEN 0x3B0000 /* Max length */
984 #define TRX_NO_HEADER 1 /* Do not write TRX header */
985 #define TRX_MAX_OFFSET 3 /* Max number of individual files */
986 #define TRX_UNCOMP_IMAGE 0x20 /* Trx contains uncompressed image */
988 struct trx_header_le
{
989 __le32 magic
; /* "HDR0" */
990 __le32 len
; /* Length of file including header */
991 __le32 crc32
; /* CRC from flag_version to end of file */
992 __le32 flag_version
; /* 0:15 flags, 16:31 version */
993 __le32 offsets
[TRX_MAX_OFFSET
]; /* Offsets of partitions from start of
997 static int check_file(const u8
*headers
)
999 struct trx_header_le
*trx
;
1000 int actual_len
= -1;
1002 brcmf_dbg(USB
, "Enter\n");
1003 /* Extract trx header */
1004 trx
= (struct trx_header_le
*) headers
;
1005 if (trx
->magic
!= cpu_to_le32(TRX_MAGIC
))
1008 headers
+= sizeof(struct trx_header_le
);
1010 if (le32_to_cpu(trx
->flag_version
) & TRX_UNCOMP_IMAGE
) {
1011 actual_len
= le32_to_cpu(trx
->offsets
[TRX_OFFSETS_DLFWLEN_IDX
]);
1012 return actual_len
+ sizeof(struct trx_header_le
);
1017 static const char *brcmf_usb_get_fwname(struct brcmf_usbdev_info
*devinfo
)
1019 switch (devinfo
->bus_pub
.devid
) {
1020 case BRCM_CC_43143_CHIP_ID
:
1021 return BRCMF_USB_43143_FW_NAME
;
1022 case BRCM_CC_43235_CHIP_ID
:
1023 case BRCM_CC_43236_CHIP_ID
:
1024 case BRCM_CC_43238_CHIP_ID
:
1025 return BRCMF_USB_43236_FW_NAME
;
1026 case BRCM_CC_43242_CHIP_ID
:
1027 return BRCMF_USB_43242_FW_NAME
;
1028 case BRCM_CC_43566_CHIP_ID
:
1029 case BRCM_CC_43569_CHIP_ID
:
1030 return BRCMF_USB_43569_FW_NAME
;
1038 struct brcmf_usbdev
*brcmf_usb_attach(struct brcmf_usbdev_info
*devinfo
,
1041 brcmf_dbg(USB
, "Enter\n");
1043 devinfo
->bus_pub
.nrxq
= nrxq
;
1044 devinfo
->rx_low_watermark
= nrxq
/ 2;
1045 devinfo
->bus_pub
.devinfo
= devinfo
;
1046 devinfo
->bus_pub
.ntxq
= ntxq
;
1047 devinfo
->bus_pub
.state
= BRCMFMAC_USB_STATE_DOWN
;
1049 /* flow control when too many tx urbs posted */
1050 devinfo
->tx_low_watermark
= ntxq
/ 4;
1051 devinfo
->tx_high_watermark
= devinfo
->tx_low_watermark
* 3;
1052 devinfo
->bus_pub
.bus_mtu
= BRCMF_USB_MAX_PKT_SIZE
;
1054 /* Initialize other structure content */
1055 init_waitqueue_head(&devinfo
->ioctl_resp_wait
);
1057 /* Initialize the spinlocks */
1058 spin_lock_init(&devinfo
->qlock
);
1059 spin_lock_init(&devinfo
->tx_flowblock_lock
);
1061 INIT_LIST_HEAD(&devinfo
->rx_freeq
);
1062 INIT_LIST_HEAD(&devinfo
->rx_postq
);
1064 INIT_LIST_HEAD(&devinfo
->tx_freeq
);
1065 INIT_LIST_HEAD(&devinfo
->tx_postq
);
1067 devinfo
->tx_flowblock
= false;
1069 devinfo
->rx_reqs
= brcmf_usbdev_qinit(&devinfo
->rx_freeq
, nrxq
);
1070 if (!devinfo
->rx_reqs
)
1073 devinfo
->tx_reqs
= brcmf_usbdev_qinit(&devinfo
->tx_freeq
, ntxq
);
1074 if (!devinfo
->tx_reqs
)
1076 devinfo
->tx_freecount
= ntxq
;
1078 devinfo
->ctl_urb
= usb_alloc_urb(0, GFP_ATOMIC
);
1079 if (!devinfo
->ctl_urb
) {
1080 brcmf_err("usb_alloc_urb (ctl) failed\n");
1083 devinfo
->bulk_urb
= usb_alloc_urb(0, GFP_ATOMIC
);
1084 if (!devinfo
->bulk_urb
) {
1085 brcmf_err("usb_alloc_urb (bulk) failed\n");
1089 return &devinfo
->bus_pub
;
1092 brcmf_err("failed!\n");
1093 brcmf_usb_detach(devinfo
);
1097 static struct brcmf_bus_ops brcmf_usb_bus_ops
= {
1098 .txdata
= brcmf_usb_tx
,
1099 .stop
= brcmf_usb_down
,
1100 .txctl
= brcmf_usb_tx_ctlpkt
,
1101 .rxctl
= brcmf_usb_rx_ctlpkt
,
1104 static int brcmf_usb_bus_setup(struct brcmf_usbdev_info
*devinfo
)
1108 /* Attach to the common driver interface */
1109 ret
= brcmf_attach(devinfo
->dev
);
1111 brcmf_err("brcmf_attach failed\n");
1115 ret
= brcmf_usb_up(devinfo
->dev
);
1119 ret
= brcmf_bus_start(devinfo
->dev
);
1125 brcmf_detach(devinfo
->dev
);
1129 static void brcmf_usb_probe_phase2(struct device
*dev
,
1130 const struct firmware
*fw
,
1131 void *nvram
, u32 nvlen
)
1133 struct brcmf_bus
*bus
= dev_get_drvdata(dev
);
1134 struct brcmf_usbdev_info
*devinfo
;
1137 brcmf_dbg(USB
, "Start fw downloading\n");
1138 ret
= check_file(fw
->data
);
1140 brcmf_err("invalid firmware\n");
1141 release_firmware(fw
);
1145 devinfo
= bus
->bus_priv
.usb
->devinfo
;
1146 devinfo
->image
= fw
->data
;
1147 devinfo
->image_len
= fw
->size
;
1149 ret
= brcmf_usb_fw_download(devinfo
);
1150 release_firmware(fw
);
1154 ret
= brcmf_usb_bus_setup(devinfo
);
1160 brcmf_dbg(TRACE
, "failed: dev=%s, err=%d\n", dev_name(dev
), ret
);
1161 device_release_driver(dev
);
1164 static int brcmf_usb_probe_cb(struct brcmf_usbdev_info
*devinfo
)
1166 struct brcmf_bus
*bus
= NULL
;
1167 struct brcmf_usbdev
*bus_pub
= NULL
;
1168 struct device
*dev
= devinfo
->dev
;
1171 brcmf_dbg(USB
, "Enter\n");
1172 bus_pub
= brcmf_usb_attach(devinfo
, BRCMF_USB_NRXQ
, BRCMF_USB_NTXQ
);
1176 bus
= kzalloc(sizeof(struct brcmf_bus
), GFP_ATOMIC
);
1184 bus
->bus_priv
.usb
= bus_pub
;
1185 dev_set_drvdata(dev
, bus
);
1186 bus
->ops
= &brcmf_usb_bus_ops
;
1187 bus
->proto_type
= BRCMF_PROTO_BCDC
;
1188 bus
->always_use_fws_queue
= true;
1190 if (!brcmf_usb_dlneeded(devinfo
)) {
1191 ret
= brcmf_usb_bus_setup(devinfo
);
1195 bus
->chip
= bus_pub
->devid
;
1196 bus
->chiprev
= bus_pub
->chiprev
;
1198 /* request firmware here */
1199 brcmf_fw_get_firmwares(dev
, 0, brcmf_usb_get_fwname(devinfo
), NULL
,
1200 brcmf_usb_probe_phase2
);
1204 /* Release resources in reverse order */
1206 brcmf_usb_detach(devinfo
);
1211 brcmf_usb_disconnect_cb(struct brcmf_usbdev_info
*devinfo
)
1215 brcmf_dbg(USB
, "Enter, bus_pub %p\n", devinfo
);
1217 brcmf_detach(devinfo
->dev
);
1218 kfree(devinfo
->bus_pub
.bus
);
1219 brcmf_usb_detach(devinfo
);
1223 brcmf_usb_probe(struct usb_interface
*intf
, const struct usb_device_id
*id
)
1225 struct usb_device
*usb
= interface_to_usbdev(intf
);
1226 struct brcmf_usbdev_info
*devinfo
;
1227 struct usb_interface_descriptor
*desc
;
1228 struct usb_endpoint_descriptor
*endpoint
;
1231 u8 endpoint_num
, ep
;
1233 brcmf_dbg(USB
, "Enter 0x%04x:0x%04x\n", id
->idVendor
, id
->idProduct
);
1235 devinfo
= kzalloc(sizeof(*devinfo
), GFP_ATOMIC
);
1236 if (devinfo
== NULL
)
1239 devinfo
->usbdev
= usb
;
1240 devinfo
->dev
= &usb
->dev
;
1241 usb_set_intfdata(intf
, devinfo
);
1243 /* Check that the device supports only one configuration */
1244 if (usb
->descriptor
.bNumConfigurations
!= 1) {
1245 brcmf_err("Number of configurations: %d not supported\n",
1246 usb
->descriptor
.bNumConfigurations
);
1251 if ((usb
->descriptor
.bDeviceClass
!= USB_CLASS_VENDOR_SPEC
) &&
1252 (usb
->descriptor
.bDeviceClass
!= USB_CLASS_MISC
) &&
1253 (usb
->descriptor
.bDeviceClass
!= USB_CLASS_WIRELESS_CONTROLLER
)) {
1254 brcmf_err("Device class: 0x%x not supported\n",
1255 usb
->descriptor
.bDeviceClass
);
1260 desc
= &intf
->altsetting
[0].desc
;
1261 if ((desc
->bInterfaceClass
!= USB_CLASS_VENDOR_SPEC
) ||
1262 (desc
->bInterfaceSubClass
!= 2) ||
1263 (desc
->bInterfaceProtocol
!= 0xff)) {
1264 brcmf_err("non WLAN interface %d: 0x%x:0x%x:0x%x\n",
1265 desc
->bInterfaceNumber
, desc
->bInterfaceClass
,
1266 desc
->bInterfaceSubClass
, desc
->bInterfaceProtocol
);
1271 num_of_eps
= desc
->bNumEndpoints
;
1272 for (ep
= 0; ep
< num_of_eps
; ep
++) {
1273 endpoint
= &intf
->altsetting
[0].endpoint
[ep
].desc
;
1274 endpoint_num
= usb_endpoint_num(endpoint
);
1275 if (!usb_endpoint_xfer_bulk(endpoint
))
1277 if (usb_endpoint_dir_in(endpoint
)) {
1278 if (!devinfo
->rx_pipe
)
1280 usb_rcvbulkpipe(usb
, endpoint_num
);
1282 if (!devinfo
->tx_pipe
)
1284 usb_sndbulkpipe(usb
, endpoint_num
);
1287 if (devinfo
->rx_pipe
== 0) {
1288 brcmf_err("No RX (in) Bulk EP found\n");
1292 if (devinfo
->tx_pipe
== 0) {
1293 brcmf_err("No TX (out) Bulk EP found\n");
1298 devinfo
->ifnum
= desc
->bInterfaceNumber
;
1300 if (usb
->speed
== USB_SPEED_SUPER
)
1301 brcmf_dbg(USB
, "Broadcom super speed USB WLAN interface detected\n");
1302 else if (usb
->speed
== USB_SPEED_HIGH
)
1303 brcmf_dbg(USB
, "Broadcom high speed USB WLAN interface detected\n");
1305 brcmf_dbg(USB
, "Broadcom full speed USB WLAN interface detected\n");
1307 ret
= brcmf_usb_probe_cb(devinfo
);
1316 usb_set_intfdata(intf
, NULL
);
1321 brcmf_usb_disconnect(struct usb_interface
*intf
)
1323 struct brcmf_usbdev_info
*devinfo
;
1325 brcmf_dbg(USB
, "Enter\n");
1326 devinfo
= (struct brcmf_usbdev_info
*)usb_get_intfdata(intf
);
1327 brcmf_usb_disconnect_cb(devinfo
);
1329 brcmf_dbg(USB
, "Exit\n");
1333 * only need to signal the bus being down and update the state.
1335 static int brcmf_usb_suspend(struct usb_interface
*intf
, pm_message_t state
)
1337 struct usb_device
*usb
= interface_to_usbdev(intf
);
1338 struct brcmf_usbdev_info
*devinfo
= brcmf_usb_get_businfo(&usb
->dev
);
1340 brcmf_dbg(USB
, "Enter\n");
1341 devinfo
->bus_pub
.state
= BRCMFMAC_USB_STATE_SLEEP
;
1342 brcmf_detach(&usb
->dev
);
1347 * (re-) start the bus.
1349 static int brcmf_usb_resume(struct usb_interface
*intf
)
1351 struct usb_device
*usb
= interface_to_usbdev(intf
);
1352 struct brcmf_usbdev_info
*devinfo
= brcmf_usb_get_businfo(&usb
->dev
);
1354 brcmf_dbg(USB
, "Enter\n");
1355 return brcmf_usb_bus_setup(devinfo
);
1358 static int brcmf_usb_reset_resume(struct usb_interface
*intf
)
1360 struct usb_device
*usb
= interface_to_usbdev(intf
);
1361 struct brcmf_usbdev_info
*devinfo
= brcmf_usb_get_businfo(&usb
->dev
);
1363 brcmf_dbg(USB
, "Enter\n");
1365 return brcmf_fw_get_firmwares(&usb
->dev
, 0,
1366 brcmf_usb_get_fwname(devinfo
), NULL
,
1367 brcmf_usb_probe_phase2
);
1370 #define BRCMF_USB_DEVICE(dev_id) \
1371 { USB_DEVICE(BRCM_USB_VENDOR_ID_BROADCOM, dev_id) }
1373 static struct usb_device_id brcmf_usb_devid_table
[] = {
1374 BRCMF_USB_DEVICE(BRCM_USB_43143_DEVICE_ID
),
1375 BRCMF_USB_DEVICE(BRCM_USB_43236_DEVICE_ID
),
1376 BRCMF_USB_DEVICE(BRCM_USB_43242_DEVICE_ID
),
1377 BRCMF_USB_DEVICE(BRCM_USB_43569_DEVICE_ID
),
1378 /* special entry for device with firmware loaded and running */
1379 BRCMF_USB_DEVICE(BRCM_USB_BCMFW_DEVICE_ID
),
1380 { /* end: all zeroes */ }
1383 MODULE_DEVICE_TABLE(usb
, brcmf_usb_devid_table
);
1384 MODULE_FIRMWARE(BRCMF_USB_43143_FW_NAME
);
1385 MODULE_FIRMWARE(BRCMF_USB_43236_FW_NAME
);
1386 MODULE_FIRMWARE(BRCMF_USB_43242_FW_NAME
);
1387 MODULE_FIRMWARE(BRCMF_USB_43569_FW_NAME
);
1389 static struct usb_driver brcmf_usbdrvr
= {
1390 .name
= KBUILD_MODNAME
,
1391 .probe
= brcmf_usb_probe
,
1392 .disconnect
= brcmf_usb_disconnect
,
1393 .id_table
= brcmf_usb_devid_table
,
1394 .suspend
= brcmf_usb_suspend
,
1395 .resume
= brcmf_usb_resume
,
1396 .reset_resume
= brcmf_usb_reset_resume
,
1397 .supports_autosuspend
= 1,
1398 .disable_hub_initiated_lpm
= 1,
1401 static int brcmf_usb_reset_device(struct device
*dev
, void *notused
)
1403 /* device past is the usb interface so we
1404 * need to use parent here.
1406 brcmf_dev_reset(dev
->parent
);
1410 void brcmf_usb_exit(void)
1412 struct device_driver
*drv
= &brcmf_usbdrvr
.drvwrap
.driver
;
1415 brcmf_dbg(USB
, "Enter\n");
1416 ret
= driver_for_each_device(drv
, NULL
, NULL
,
1417 brcmf_usb_reset_device
);
1418 usb_deregister(&brcmf_usbdrvr
);
1421 void brcmf_usb_register(void)
1423 brcmf_dbg(USB
, "Enter\n");
1424 usb_register(&brcmf_usbdrvr
);