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>
32 #define IOCTL_RESP_TIMEOUT 2000
34 #define BRCMF_USB_RESET_GETVER_SPINWAIT 100 /* in unit of ms */
35 #define BRCMF_USB_RESET_GETVER_LOOP_CNT 10
37 #define BRCMF_POSTBOOT_ID 0xA123 /* ID to detect if dongle
39 #define BRCMF_USB_NRXQ 50
40 #define BRCMF_USB_NTXQ 50
42 #define BRCMF_USB_CBCTL_WRITE 0
43 #define BRCMF_USB_CBCTL_READ 1
44 #define BRCMF_USB_MAX_PKT_SIZE 1600
46 #define BRCMF_USB_43143_FW_NAME "/*(DEBLOBBED)*/"
47 #define BRCMF_USB_43236_FW_NAME "/*(DEBLOBBED)*/"
48 #define BRCMF_USB_43242_FW_NAME "/*(DEBLOBBED)*/"
49 #define BRCMF_USB_43569_FW_NAME "/*(DEBLOBBED)*/"
51 #define TRX_MAGIC 0x30524448 /* "HDR0" */
52 #define TRX_MAX_OFFSET 3 /* Max number of file offsets */
53 #define TRX_UNCOMP_IMAGE 0x20 /* Trx holds uncompressed img */
54 #define TRX_RDL_CHUNK 1500 /* size of each dl transfer */
55 #define TRX_OFFSETS_DLFWLEN_IDX 0
57 /* Control messages: bRequest values */
58 #define DL_GETSTATE 0 /* returns the rdl_state_t struct */
59 #define DL_CHECK_CRC 1 /* currently unused */
60 #define DL_GO 2 /* execute downloaded image */
61 #define DL_START 3 /* initialize dl state */
62 #define DL_REBOOT 4 /* reboot the device in 2 seconds */
63 #define DL_GETVER 5 /* returns the bootrom_id_t struct */
64 #define DL_GO_PROTECTED 6 /* execute the downloaded code and set reset
65 * event to occur in 2 seconds. It is the
66 * responsibility of the downloaded code to
69 #define DL_EXEC 7 /* jump to a supplied address */
70 #define DL_RESETCFG 8 /* To support single enum on dongle
71 * - Not used by bootloader
73 #define DL_DEFER_RESP_OK 9 /* Potentially defer the response to setup
78 #define DL_WAITING 0 /* waiting to rx first pkt */
79 #define DL_READY 1 /* hdr was good, waiting for more of the
82 #define DL_BAD_HDR 2 /* hdr was corrupted */
83 #define DL_BAD_CRC 3 /* compressed image was corrupted */
84 #define DL_RUNNABLE 4 /* download was successful,waiting for go cmd */
85 #define DL_START_FAIL 5 /* failed to initialize correctly */
86 #define DL_NVRAM_TOOBIG 6 /* host specified nvram data exceeds DL_NVRAM
89 #define DL_IMAGE_TOOBIG 7 /* firmware image too big */
92 struct trx_header_le {
93 __le32 magic; /* "HDR0" */
94 __le32 len; /* Length of file including header */
95 __le32 crc32; /* CRC from flag_version to end of file */
96 __le32 flag_version; /* 0:15 flags, 16:31 version */
97 __le32 offsets[TRX_MAX_OFFSET]; /* Offsets of partitions from start of
102 struct rdl_state_le {
107 struct bootrom_id_le {
108 __le32 chip; /* Chip id */
109 __le32 chiprev; /* Chip rev */
110 __le32 ramsize; /* Size of RAM */
111 __le32 remapbase; /* Current remap base address */
112 __le32 boardtype; /* Type of board */
113 __le32 boardrev; /* Board revision */
116 struct brcmf_usb_image {
117 struct list_head list;
123 struct brcmf_usbdev_info {
124 struct brcmf_usbdev bus_pub; /* MUST BE FIRST */
126 struct list_head rx_freeq;
127 struct list_head rx_postq;
128 struct list_head tx_freeq;
129 struct list_head tx_postq;
130 uint rx_pipe, tx_pipe;
132 int rx_low_watermark;
133 int tx_low_watermark;
134 int tx_high_watermark;
137 spinlock_t tx_flowblock_lock;
139 struct brcmf_usbreq *tx_reqs;
140 struct brcmf_usbreq *rx_reqs;
142 const u8 *image; /* buffer for combine fw and nvram */
145 struct usb_device *usbdev;
147 struct completion dev_init_done;
149 int ctl_in_pipe, ctl_out_pipe;
150 struct urb *ctl_urb; /* URB for control endpoint */
151 struct usb_ctrlrequest ctl_write;
152 struct usb_ctrlrequest ctl_read;
153 u32 ctl_urb_actual_length;
156 wait_queue_head_t ioctl_resp_wait;
160 struct urb *bulk_urb; /* used for FW download */
165 static void brcmf_usb_rx_refill(struct brcmf_usbdev_info *devinfo,
166 struct brcmf_usbreq *req);
168 static struct brcmf_usbdev *brcmf_usb_get_buspub(struct device *dev)
170 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
171 return bus_if->bus_priv.usb;
174 static struct brcmf_usbdev_info *brcmf_usb_get_businfo(struct device *dev)
176 return brcmf_usb_get_buspub(dev)->devinfo;
179 static int brcmf_usb_ioctl_resp_wait(struct brcmf_usbdev_info *devinfo)
181 return wait_event_timeout(devinfo->ioctl_resp_wait,
182 devinfo->ctl_completed,
183 msecs_to_jiffies(IOCTL_RESP_TIMEOUT));
186 static void brcmf_usb_ioctl_resp_wake(struct brcmf_usbdev_info *devinfo)
188 if (waitqueue_active(&devinfo->ioctl_resp_wait))
189 wake_up(&devinfo->ioctl_resp_wait);
193 brcmf_usb_ctl_complete(struct brcmf_usbdev_info *devinfo, int type, int status)
195 brcmf_dbg(USB, "Enter, status=%d\n", status);
197 if (unlikely(devinfo == NULL))
200 if (type == BRCMF_USB_CBCTL_READ) {
202 devinfo->bus_pub.stats.rx_ctlpkts++;
204 devinfo->bus_pub.stats.rx_ctlerrs++;
205 } else if (type == BRCMF_USB_CBCTL_WRITE) {
207 devinfo->bus_pub.stats.tx_ctlpkts++;
209 devinfo->bus_pub.stats.tx_ctlerrs++;
212 devinfo->ctl_urb_status = status;
213 devinfo->ctl_completed = true;
214 brcmf_usb_ioctl_resp_wake(devinfo);
218 brcmf_usb_ctlread_complete(struct urb *urb)
220 struct brcmf_usbdev_info *devinfo =
221 (struct brcmf_usbdev_info *)urb->context;
223 brcmf_dbg(USB, "Enter\n");
224 devinfo->ctl_urb_actual_length = urb->actual_length;
225 brcmf_usb_ctl_complete(devinfo, BRCMF_USB_CBCTL_READ,
230 brcmf_usb_ctlwrite_complete(struct urb *urb)
232 struct brcmf_usbdev_info *devinfo =
233 (struct brcmf_usbdev_info *)urb->context;
235 brcmf_dbg(USB, "Enter\n");
236 brcmf_usb_ctl_complete(devinfo, BRCMF_USB_CBCTL_WRITE,
241 brcmf_usb_send_ctl(struct brcmf_usbdev_info *devinfo, u8 *buf, int len)
246 brcmf_dbg(USB, "Enter\n");
247 if (devinfo == NULL || buf == NULL ||
248 len == 0 || devinfo->ctl_urb == NULL)
252 devinfo->ctl_write.wLength = cpu_to_le16p(&size);
253 devinfo->ctl_urb->transfer_buffer_length = size;
254 devinfo->ctl_urb_status = 0;
255 devinfo->ctl_urb_actual_length = 0;
257 usb_fill_control_urb(devinfo->ctl_urb,
259 devinfo->ctl_out_pipe,
260 (unsigned char *) &devinfo->ctl_write,
262 (usb_complete_t)brcmf_usb_ctlwrite_complete,
265 ret = usb_submit_urb(devinfo->ctl_urb, GFP_ATOMIC);
267 brcmf_err("usb_submit_urb failed %d\n", ret);
273 brcmf_usb_recv_ctl(struct brcmf_usbdev_info *devinfo, u8 *buf, int len)
278 brcmf_dbg(USB, "Enter\n");
279 if ((devinfo == NULL) || (buf == NULL) || (len == 0)
280 || (devinfo->ctl_urb == NULL))
284 devinfo->ctl_read.wLength = cpu_to_le16p(&size);
285 devinfo->ctl_urb->transfer_buffer_length = size;
287 devinfo->ctl_read.bRequestType = USB_DIR_IN
288 | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
289 devinfo->ctl_read.bRequest = 1;
291 usb_fill_control_urb(devinfo->ctl_urb,
293 devinfo->ctl_in_pipe,
294 (unsigned char *) &devinfo->ctl_read,
296 (usb_complete_t)brcmf_usb_ctlread_complete,
299 ret = usb_submit_urb(devinfo->ctl_urb, GFP_ATOMIC);
301 brcmf_err("usb_submit_urb failed %d\n", ret);
306 static int brcmf_usb_tx_ctlpkt(struct device *dev, u8 *buf, u32 len)
310 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
312 brcmf_dbg(USB, "Enter\n");
313 if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP)
316 if (test_and_set_bit(0, &devinfo->ctl_op))
319 devinfo->ctl_completed = false;
320 err = brcmf_usb_send_ctl(devinfo, buf, len);
322 brcmf_err("fail %d bytes: %d\n", err, len);
323 clear_bit(0, &devinfo->ctl_op);
326 timeout = brcmf_usb_ioctl_resp_wait(devinfo);
327 clear_bit(0, &devinfo->ctl_op);
329 brcmf_err("Txctl wait timed out\n");
335 static int brcmf_usb_rx_ctlpkt(struct device *dev, u8 *buf, u32 len)
339 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
341 brcmf_dbg(USB, "Enter\n");
342 if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP)
345 if (test_and_set_bit(0, &devinfo->ctl_op))
348 devinfo->ctl_completed = false;
349 err = brcmf_usb_recv_ctl(devinfo, buf, len);
351 brcmf_err("fail %d bytes: %d\n", err, len);
352 clear_bit(0, &devinfo->ctl_op);
355 timeout = brcmf_usb_ioctl_resp_wait(devinfo);
356 err = devinfo->ctl_urb_status;
357 clear_bit(0, &devinfo->ctl_op);
359 brcmf_err("rxctl wait timed out\n");
363 return devinfo->ctl_urb_actual_length;
368 static struct brcmf_usbreq *brcmf_usb_deq(struct brcmf_usbdev_info *devinfo,
369 struct list_head *q, int *counter)
372 struct brcmf_usbreq *req;
373 spin_lock_irqsave(&devinfo->qlock, flags);
375 spin_unlock_irqrestore(&devinfo->qlock, flags);
378 req = list_entry(q->next, struct brcmf_usbreq, list);
379 list_del_init(q->next);
382 spin_unlock_irqrestore(&devinfo->qlock, flags);
387 static void brcmf_usb_enq(struct brcmf_usbdev_info *devinfo,
388 struct list_head *q, struct brcmf_usbreq *req,
392 spin_lock_irqsave(&devinfo->qlock, flags);
393 list_add_tail(&req->list, q);
396 spin_unlock_irqrestore(&devinfo->qlock, flags);
399 static struct brcmf_usbreq *
400 brcmf_usbdev_qinit(struct list_head *q, int qsize)
403 struct brcmf_usbreq *req, *reqs;
405 reqs = kcalloc(qsize, sizeof(struct brcmf_usbreq), GFP_ATOMIC);
411 for (i = 0; i < qsize; i++) {
412 req->urb = usb_alloc_urb(0, GFP_ATOMIC);
416 INIT_LIST_HEAD(&req->list);
417 list_add_tail(&req->list, q);
422 brcmf_err("fail!\n");
423 while (!list_empty(q)) {
424 req = list_entry(q->next, struct brcmf_usbreq, list);
426 usb_free_urb(req->urb);
434 static void brcmf_usb_free_q(struct list_head *q, bool pending)
436 struct brcmf_usbreq *req, *next;
438 list_for_each_entry_safe(req, next, q, list) {
440 brcmf_err("bad req\n");
445 usb_kill_urb(req->urb);
447 usb_free_urb(req->urb);
448 list_del_init(&req->list);
453 static void brcmf_usb_del_fromq(struct brcmf_usbdev_info *devinfo,
454 struct brcmf_usbreq *req)
458 spin_lock_irqsave(&devinfo->qlock, flags);
459 list_del_init(&req->list);
460 spin_unlock_irqrestore(&devinfo->qlock, flags);
464 static void brcmf_usb_tx_complete(struct urb *urb)
466 struct brcmf_usbreq *req = (struct brcmf_usbreq *)urb->context;
467 struct brcmf_usbdev_info *devinfo = req->devinfo;
470 brcmf_dbg(USB, "Enter, urb->status=%d, skb=%p\n", urb->status,
472 brcmf_usb_del_fromq(devinfo, req);
474 brcmf_txcomplete(devinfo->dev, req->skb, urb->status == 0);
476 brcmf_usb_enq(devinfo, &devinfo->tx_freeq, req, &devinfo->tx_freecount);
477 spin_lock_irqsave(&devinfo->tx_flowblock_lock, flags);
478 if (devinfo->tx_freecount > devinfo->tx_high_watermark &&
479 devinfo->tx_flowblock) {
480 brcmf_txflowblock(devinfo->dev, false);
481 devinfo->tx_flowblock = false;
483 spin_unlock_irqrestore(&devinfo->tx_flowblock_lock, flags);
486 static void brcmf_usb_rx_complete(struct urb *urb)
488 struct brcmf_usbreq *req = (struct brcmf_usbreq *)urb->context;
489 struct brcmf_usbdev_info *devinfo = req->devinfo;
492 brcmf_dbg(USB, "Enter, urb->status=%d\n", urb->status);
493 brcmf_usb_del_fromq(devinfo, req);
497 /* zero lenght packets indicate usb "failure". Do not refill */
498 if (urb->status != 0 || !urb->actual_length) {
499 brcmu_pkt_buf_free_skb(skb);
500 brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
504 if (devinfo->bus_pub.state == BRCMFMAC_USB_STATE_UP) {
505 skb_put(skb, urb->actual_length);
506 brcmf_rx_frame(devinfo->dev, skb, true);
507 brcmf_usb_rx_refill(devinfo, req);
509 brcmu_pkt_buf_free_skb(skb);
510 brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
516 static void brcmf_usb_rx_refill(struct brcmf_usbdev_info *devinfo,
517 struct brcmf_usbreq *req)
522 if (!req || !devinfo)
525 skb = dev_alloc_skb(devinfo->bus_pub.bus_mtu);
527 brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
532 usb_fill_bulk_urb(req->urb, devinfo->usbdev, devinfo->rx_pipe,
533 skb->data, skb_tailroom(skb), brcmf_usb_rx_complete,
535 req->devinfo = devinfo;
536 brcmf_usb_enq(devinfo, &devinfo->rx_postq, req, NULL);
538 ret = usb_submit_urb(req->urb, GFP_ATOMIC);
540 brcmf_usb_del_fromq(devinfo, req);
541 brcmu_pkt_buf_free_skb(req->skb);
543 brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
548 static void brcmf_usb_rx_fill_all(struct brcmf_usbdev_info *devinfo)
550 struct brcmf_usbreq *req;
552 if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP) {
553 brcmf_err("bus is not up=%d\n", devinfo->bus_pub.state);
556 while ((req = brcmf_usb_deq(devinfo, &devinfo->rx_freeq, NULL)) != NULL)
557 brcmf_usb_rx_refill(devinfo, req);
561 brcmf_usb_state_change(struct brcmf_usbdev_info *devinfo, int state)
563 struct brcmf_bus *bcmf_bus = devinfo->bus_pub.bus;
566 brcmf_dbg(USB, "Enter, current state=%d, new state=%d\n",
567 devinfo->bus_pub.state, state);
569 if (devinfo->bus_pub.state == state)
572 old_state = devinfo->bus_pub.state;
573 devinfo->bus_pub.state = state;
575 /* update state of upper layer */
576 if (state == BRCMFMAC_USB_STATE_DOWN) {
577 brcmf_dbg(USB, "DBUS is down\n");
578 brcmf_bus_change_state(bcmf_bus, BRCMF_BUS_DOWN);
579 } else if (state == BRCMFMAC_USB_STATE_UP) {
580 brcmf_dbg(USB, "DBUS is up\n");
581 brcmf_bus_change_state(bcmf_bus, BRCMF_BUS_UP);
583 brcmf_dbg(USB, "DBUS current state=%d\n", state);
587 static int brcmf_usb_tx(struct device *dev, struct sk_buff *skb)
589 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
590 struct brcmf_usbreq *req;
594 brcmf_dbg(USB, "Enter, skb=%p\n", skb);
595 if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP) {
600 req = brcmf_usb_deq(devinfo, &devinfo->tx_freeq,
601 &devinfo->tx_freecount);
603 brcmf_err("no req to send\n");
609 req->devinfo = devinfo;
610 usb_fill_bulk_urb(req->urb, devinfo->usbdev, devinfo->tx_pipe,
611 skb->data, skb->len, brcmf_usb_tx_complete, req);
612 req->urb->transfer_flags |= URB_ZERO_PACKET;
613 brcmf_usb_enq(devinfo, &devinfo->tx_postq, req, NULL);
614 ret = usb_submit_urb(req->urb, GFP_ATOMIC);
616 brcmf_err("brcmf_usb_tx usb_submit_urb FAILED\n");
617 brcmf_usb_del_fromq(devinfo, req);
619 brcmf_usb_enq(devinfo, &devinfo->tx_freeq, req,
620 &devinfo->tx_freecount);
624 spin_lock_irqsave(&devinfo->tx_flowblock_lock, flags);
625 if (devinfo->tx_freecount < devinfo->tx_low_watermark &&
626 !devinfo->tx_flowblock) {
627 brcmf_txflowblock(dev, true);
628 devinfo->tx_flowblock = true;
630 spin_unlock_irqrestore(&devinfo->tx_flowblock_lock, flags);
638 static int brcmf_usb_up(struct device *dev)
640 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
642 brcmf_dbg(USB, "Enter\n");
643 if (devinfo->bus_pub.state == BRCMFMAC_USB_STATE_UP)
646 /* Success, indicate devinfo is fully up */
647 brcmf_usb_state_change(devinfo, BRCMFMAC_USB_STATE_UP);
649 if (devinfo->ctl_urb) {
650 devinfo->ctl_in_pipe = usb_rcvctrlpipe(devinfo->usbdev, 0);
651 devinfo->ctl_out_pipe = usb_sndctrlpipe(devinfo->usbdev, 0);
654 devinfo->ctl_write.bRequestType =
655 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
656 devinfo->ctl_write.bRequest = 0;
657 devinfo->ctl_write.wValue = cpu_to_le16(0);
658 devinfo->ctl_write.wIndex = cpu_to_le16(devinfo->ifnum);
661 devinfo->ctl_read.bRequestType =
662 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
663 devinfo->ctl_read.bRequest = 1;
664 devinfo->ctl_read.wValue = cpu_to_le16(0);
665 devinfo->ctl_read.wIndex = cpu_to_le16(devinfo->ifnum);
667 brcmf_usb_rx_fill_all(devinfo);
671 static void brcmf_cancel_all_urbs(struct brcmf_usbdev_info *devinfo)
675 if (devinfo->ctl_urb)
676 usb_kill_urb(devinfo->ctl_urb);
677 if (devinfo->bulk_urb)
678 usb_kill_urb(devinfo->bulk_urb);
679 if (devinfo->tx_reqs)
680 for (i = 0; i < devinfo->bus_pub.ntxq; i++)
681 usb_kill_urb(devinfo->tx_reqs[i].urb);
682 if (devinfo->rx_reqs)
683 for (i = 0; i < devinfo->bus_pub.nrxq; i++)
684 usb_kill_urb(devinfo->rx_reqs[i].urb);
687 static void brcmf_usb_down(struct device *dev)
689 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
691 brcmf_dbg(USB, "Enter\n");
695 if (devinfo->bus_pub.state == BRCMFMAC_USB_STATE_DOWN)
698 brcmf_usb_state_change(devinfo, BRCMFMAC_USB_STATE_DOWN);
700 brcmf_cancel_all_urbs(devinfo);
704 brcmf_usb_sync_complete(struct urb *urb)
706 struct brcmf_usbdev_info *devinfo =
707 (struct brcmf_usbdev_info *)urb->context;
709 devinfo->ctl_completed = true;
710 brcmf_usb_ioctl_resp_wake(devinfo);
713 static int brcmf_usb_dl_cmd(struct brcmf_usbdev_info *devinfo, u8 cmd,
714 void *buffer, int buflen)
720 if ((!devinfo) || (devinfo->ctl_urb == NULL))
723 tmpbuf = kmalloc(buflen, GFP_ATOMIC);
728 devinfo->ctl_urb->transfer_buffer_length = size;
730 devinfo->ctl_read.wLength = cpu_to_le16p(&size);
731 devinfo->ctl_read.bRequestType = USB_DIR_IN | USB_TYPE_VENDOR |
733 devinfo->ctl_read.bRequest = cmd;
735 usb_fill_control_urb(devinfo->ctl_urb,
737 usb_rcvctrlpipe(devinfo->usbdev, 0),
738 (unsigned char *) &devinfo->ctl_read,
739 (void *) tmpbuf, size,
740 (usb_complete_t)brcmf_usb_sync_complete, devinfo);
742 devinfo->ctl_completed = false;
743 ret = usb_submit_urb(devinfo->ctl_urb, GFP_ATOMIC);
745 brcmf_err("usb_submit_urb failed %d\n", ret);
749 if (!brcmf_usb_ioctl_resp_wait(devinfo)) {
750 usb_kill_urb(devinfo->ctl_urb);
753 memcpy(buffer, tmpbuf, buflen);
762 brcmf_usb_dlneeded(struct brcmf_usbdev_info *devinfo)
764 struct bootrom_id_le id;
767 brcmf_dbg(USB, "Enter\n");
772 /* Check if firmware downloaded already by querying runtime ID */
773 id.chip = cpu_to_le32(0xDEAD);
774 brcmf_usb_dl_cmd(devinfo, DL_GETVER, &id, sizeof(id));
776 chipid = le32_to_cpu(id.chip);
777 chiprev = le32_to_cpu(id.chiprev);
779 if ((chipid & 0x4300) == 0x4300)
780 brcmf_dbg(USB, "chip %x rev 0x%x\n", chipid, chiprev);
782 brcmf_dbg(USB, "chip %d rev 0x%x\n", chipid, chiprev);
783 if (chipid == BRCMF_POSTBOOT_ID) {
784 brcmf_dbg(USB, "firmware already downloaded\n");
785 brcmf_usb_dl_cmd(devinfo, DL_RESETCFG, &id, sizeof(id));
788 devinfo->bus_pub.devid = chipid;
789 devinfo->bus_pub.chiprev = chiprev;
795 brcmf_usb_resetcfg(struct brcmf_usbdev_info *devinfo)
797 struct bootrom_id_le id;
801 brcmf_dbg(USB, "Enter\n");
805 mdelay(BRCMF_USB_RESET_GETVER_SPINWAIT);
807 id.chip = cpu_to_le32(0xDEAD); /* Get the ID */
808 err = brcmf_usb_dl_cmd(devinfo, DL_GETVER, &id, sizeof(id));
809 if ((err) && (err != -ETIMEDOUT))
811 if (id.chip == cpu_to_le32(BRCMF_POSTBOOT_ID))
813 } while (loop_cnt < BRCMF_USB_RESET_GETVER_LOOP_CNT);
815 if (id.chip == cpu_to_le32(BRCMF_POSTBOOT_ID)) {
816 brcmf_dbg(USB, "postboot chip 0x%x/rev 0x%x\n",
817 le32_to_cpu(id.chip), le32_to_cpu(id.chiprev));
819 brcmf_usb_dl_cmd(devinfo, DL_RESETCFG, &id, sizeof(id));
822 brcmf_err("Cannot talk to Dongle. Firmware is not UP, %d ms\n",
823 BRCMF_USB_RESET_GETVER_SPINWAIT * loop_cnt);
830 brcmf_usb_dl_send_bulk(struct brcmf_usbdev_info *devinfo, void *buffer, int len)
834 if ((devinfo == NULL) || (devinfo->bulk_urb == NULL))
837 /* Prepare the URB */
838 usb_fill_bulk_urb(devinfo->bulk_urb, devinfo->usbdev,
839 devinfo->tx_pipe, buffer, len,
840 (usb_complete_t)brcmf_usb_sync_complete, devinfo);
842 devinfo->bulk_urb->transfer_flags |= URB_ZERO_PACKET;
844 devinfo->ctl_completed = false;
845 ret = usb_submit_urb(devinfo->bulk_urb, GFP_ATOMIC);
847 brcmf_err("usb_submit_urb failed %d\n", ret);
850 ret = brcmf_usb_ioctl_resp_wait(devinfo);
855 brcmf_usb_dl_writeimage(struct brcmf_usbdev_info *devinfo, u8 *fw, int fwlen)
857 unsigned int sendlen, sent, dllen;
858 char *bulkchunk = NULL, *dlpos;
859 struct rdl_state_le state;
860 u32 rdlstate, rdlbytes;
863 brcmf_dbg(USB, "Enter, fw %p, len %d\n", fw, fwlen);
865 bulkchunk = kmalloc(TRX_RDL_CHUNK, GFP_ATOMIC);
866 if (bulkchunk == NULL) {
871 /* 1) Prepare USB boot loader for runtime image */
872 brcmf_usb_dl_cmd(devinfo, DL_START, &state, sizeof(state));
874 rdlstate = le32_to_cpu(state.state);
875 rdlbytes = le32_to_cpu(state.bytes);
877 /* 2) Check we are in the Waiting state */
878 if (rdlstate != DL_WAITING) {
879 brcmf_err("Failed to DL_START\n");
887 /* Get chip id and rev */
888 while (rdlbytes != dllen) {
889 /* Wait until the usb device reports it received all
890 * the bytes we sent */
891 if ((rdlbytes == sent) && (rdlbytes != dllen)) {
892 if ((dllen-sent) < TRX_RDL_CHUNK)
893 sendlen = dllen-sent;
895 sendlen = TRX_RDL_CHUNK;
897 /* simply avoid having to send a ZLP by ensuring we
905 memcpy(bulkchunk, dlpos, sendlen);
906 if (brcmf_usb_dl_send_bulk(devinfo, bulkchunk,
908 brcmf_err("send_bulk failed\n");
916 err = brcmf_usb_dl_cmd(devinfo, DL_GETSTATE, &state,
919 brcmf_err("DL_GETSTATE Failed\n");
923 rdlstate = le32_to_cpu(state.state);
924 rdlbytes = le32_to_cpu(state.bytes);
926 /* restart if an error is reported */
927 if (rdlstate == DL_BAD_HDR || rdlstate == DL_BAD_CRC) {
928 brcmf_err("Bad Hdr or Bad CRC state %d\n",
937 brcmf_dbg(USB, "Exit, err=%d\n", err);
941 static int brcmf_usb_dlstart(struct brcmf_usbdev_info *devinfo, u8 *fw, int len)
945 brcmf_dbg(USB, "Enter\n");
950 if (devinfo->bus_pub.devid == 0xDEAD)
953 err = brcmf_usb_dl_writeimage(devinfo, fw, len);
955 devinfo->bus_pub.state = BRCMFMAC_USB_STATE_DL_DONE;
957 devinfo->bus_pub.state = BRCMFMAC_USB_STATE_DL_FAIL;
958 brcmf_dbg(USB, "Exit, err=%d\n", err);
963 static int brcmf_usb_dlrun(struct brcmf_usbdev_info *devinfo)
965 struct rdl_state_le state;
967 brcmf_dbg(USB, "Enter\n");
971 if (devinfo->bus_pub.devid == 0xDEAD)
974 /* Check we are runnable */
976 brcmf_usb_dl_cmd(devinfo, DL_GETSTATE, &state, sizeof(state));
978 /* Start the image */
979 if (state.state == cpu_to_le32(DL_RUNNABLE)) {
980 if (brcmf_usb_dl_cmd(devinfo, DL_GO, &state, sizeof(state)))
982 if (brcmf_usb_resetcfg(devinfo))
984 /* The Dongle may go for re-enumeration. */
986 brcmf_err("Dongle not runnable\n");
989 brcmf_dbg(USB, "Exit\n");
993 static bool brcmf_usb_chip_support(int chipid, int chiprev)
996 case BRCM_CC_43143_CHIP_ID:
998 case BRCM_CC_43235_CHIP_ID:
999 case BRCM_CC_43236_CHIP_ID:
1000 case BRCM_CC_43238_CHIP_ID:
1001 return (chiprev == 3);
1002 case BRCM_CC_43242_CHIP_ID:
1004 case BRCM_CC_43566_CHIP_ID:
1005 case BRCM_CC_43569_CHIP_ID:
1014 brcmf_usb_fw_download(struct brcmf_usbdev_info *devinfo)
1019 brcmf_dbg(USB, "Enter\n");
1020 if (devinfo == NULL)
1023 devid = devinfo->bus_pub.devid;
1024 chiprev = devinfo->bus_pub.chiprev;
1026 if (!brcmf_usb_chip_support(devid, chiprev)) {
1027 brcmf_err("unsupported chip %d rev %d\n",
1032 if (!devinfo->image) {
1033 brcmf_err("No firmware!\n");
1037 err = brcmf_usb_dlstart(devinfo,
1038 (u8 *)devinfo->image, devinfo->image_len);
1040 err = brcmf_usb_dlrun(devinfo);
1045 static void brcmf_usb_detach(struct brcmf_usbdev_info *devinfo)
1047 brcmf_dbg(USB, "Enter, devinfo %p\n", devinfo);
1050 brcmf_usb_free_q(&devinfo->rx_freeq, false);
1051 brcmf_usb_free_q(&devinfo->tx_freeq, false);
1053 usb_free_urb(devinfo->ctl_urb);
1054 usb_free_urb(devinfo->bulk_urb);
1056 kfree(devinfo->tx_reqs);
1057 kfree(devinfo->rx_reqs);
1061 static int check_file(const u8 *headers)
1063 struct trx_header_le *trx;
1064 int actual_len = -1;
1066 brcmf_dbg(USB, "Enter\n");
1067 /* Extract trx header */
1068 trx = (struct trx_header_le *) headers;
1069 if (trx->magic != cpu_to_le32(TRX_MAGIC))
1072 headers += sizeof(struct trx_header_le);
1074 if (le32_to_cpu(trx->flag_version) & TRX_UNCOMP_IMAGE) {
1075 actual_len = le32_to_cpu(trx->offsets[TRX_OFFSETS_DLFWLEN_IDX]);
1076 return actual_len + sizeof(struct trx_header_le);
1081 static const char *brcmf_usb_get_fwname(struct brcmf_usbdev_info *devinfo)
1083 switch (devinfo->bus_pub.devid) {
1084 case BRCM_CC_43143_CHIP_ID:
1085 return BRCMF_USB_43143_FW_NAME;
1086 case BRCM_CC_43235_CHIP_ID:
1087 case BRCM_CC_43236_CHIP_ID:
1088 case BRCM_CC_43238_CHIP_ID:
1089 return BRCMF_USB_43236_FW_NAME;
1090 case BRCM_CC_43242_CHIP_ID:
1091 return BRCMF_USB_43242_FW_NAME;
1092 case BRCM_CC_43566_CHIP_ID:
1093 case BRCM_CC_43569_CHIP_ID:
1094 return BRCMF_USB_43569_FW_NAME;
1102 struct brcmf_usbdev *brcmf_usb_attach(struct brcmf_usbdev_info *devinfo,
1105 brcmf_dbg(USB, "Enter\n");
1107 devinfo->bus_pub.nrxq = nrxq;
1108 devinfo->rx_low_watermark = nrxq / 2;
1109 devinfo->bus_pub.devinfo = devinfo;
1110 devinfo->bus_pub.ntxq = ntxq;
1111 devinfo->bus_pub.state = BRCMFMAC_USB_STATE_DOWN;
1113 /* flow control when too many tx urbs posted */
1114 devinfo->tx_low_watermark = ntxq / 4;
1115 devinfo->tx_high_watermark = devinfo->tx_low_watermark * 3;
1116 devinfo->bus_pub.bus_mtu = BRCMF_USB_MAX_PKT_SIZE;
1118 /* Initialize other structure content */
1119 init_waitqueue_head(&devinfo->ioctl_resp_wait);
1121 /* Initialize the spinlocks */
1122 spin_lock_init(&devinfo->qlock);
1123 spin_lock_init(&devinfo->tx_flowblock_lock);
1125 INIT_LIST_HEAD(&devinfo->rx_freeq);
1126 INIT_LIST_HEAD(&devinfo->rx_postq);
1128 INIT_LIST_HEAD(&devinfo->tx_freeq);
1129 INIT_LIST_HEAD(&devinfo->tx_postq);
1131 devinfo->tx_flowblock = false;
1133 devinfo->rx_reqs = brcmf_usbdev_qinit(&devinfo->rx_freeq, nrxq);
1134 if (!devinfo->rx_reqs)
1137 devinfo->tx_reqs = brcmf_usbdev_qinit(&devinfo->tx_freeq, ntxq);
1138 if (!devinfo->tx_reqs)
1140 devinfo->tx_freecount = ntxq;
1142 devinfo->ctl_urb = usb_alloc_urb(0, GFP_ATOMIC);
1143 if (!devinfo->ctl_urb) {
1144 brcmf_err("usb_alloc_urb (ctl) failed\n");
1147 devinfo->bulk_urb = usb_alloc_urb(0, GFP_ATOMIC);
1148 if (!devinfo->bulk_urb) {
1149 brcmf_err("usb_alloc_urb (bulk) failed\n");
1153 return &devinfo->bus_pub;
1156 brcmf_err("failed!\n");
1157 brcmf_usb_detach(devinfo);
1161 static void brcmf_usb_wowl_config(struct device *dev, bool enabled)
1163 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
1165 brcmf_dbg(USB, "Configuring WOWL, enabled=%d\n", enabled);
1166 devinfo->wowl_enabled = enabled;
1168 device_set_wakeup_enable(devinfo->dev, true);
1170 device_set_wakeup_enable(devinfo->dev, false);
1173 static struct brcmf_bus_ops brcmf_usb_bus_ops = {
1174 .txdata = brcmf_usb_tx,
1175 .stop = brcmf_usb_down,
1176 .txctl = brcmf_usb_tx_ctlpkt,
1177 .rxctl = brcmf_usb_rx_ctlpkt,
1178 .wowl_config = brcmf_usb_wowl_config,
1181 static int brcmf_usb_bus_setup(struct brcmf_usbdev_info *devinfo)
1185 /* Attach to the common driver interface */
1186 ret = brcmf_attach(devinfo->dev);
1188 brcmf_err("brcmf_attach failed\n");
1192 ret = brcmf_usb_up(devinfo->dev);
1196 ret = brcmf_bus_start(devinfo->dev);
1202 brcmf_detach(devinfo->dev);
1206 static void brcmf_usb_probe_phase2(struct device *dev,
1207 const struct firmware *fw,
1208 void *nvram, u32 nvlen)
1210 struct brcmf_bus *bus = dev_get_drvdata(dev);
1211 struct brcmf_usbdev_info *devinfo;
1214 brcmf_dbg(USB, "Start fw downloading\n");
1216 devinfo = bus->bus_priv.usb->devinfo;
1217 ret = check_file(fw->data);
1219 brcmf_err("invalid firmware\n");
1220 release_firmware(fw);
1224 devinfo->image = fw->data;
1225 devinfo->image_len = fw->size;
1227 ret = brcmf_usb_fw_download(devinfo);
1228 release_firmware(fw);
1232 ret = brcmf_usb_bus_setup(devinfo);
1236 complete(&devinfo->dev_init_done);
1239 brcmf_dbg(TRACE, "failed: dev=%s, err=%d\n", dev_name(dev), ret);
1240 complete(&devinfo->dev_init_done);
1241 device_release_driver(dev);
1244 static int brcmf_usb_probe_cb(struct brcmf_usbdev_info *devinfo)
1246 struct brcmf_bus *bus = NULL;
1247 struct brcmf_usbdev *bus_pub = NULL;
1248 struct device *dev = devinfo->dev;
1251 brcmf_dbg(USB, "Enter\n");
1252 bus_pub = brcmf_usb_attach(devinfo, BRCMF_USB_NRXQ, BRCMF_USB_NTXQ);
1256 bus = kzalloc(sizeof(struct brcmf_bus), GFP_ATOMIC);
1264 bus->bus_priv.usb = bus_pub;
1265 dev_set_drvdata(dev, bus);
1266 bus->ops = &brcmf_usb_bus_ops;
1267 bus->proto_type = BRCMF_PROTO_BCDC;
1268 bus->always_use_fws_queue = true;
1270 bus->wowl_supported = true;
1273 if (!brcmf_usb_dlneeded(devinfo)) {
1274 ret = brcmf_usb_bus_setup(devinfo);
1278 complete(&devinfo->dev_init_done);
1281 bus->chip = bus_pub->devid;
1282 bus->chiprev = bus_pub->chiprev;
1284 /* request firmware here */
1285 ret = brcmf_fw_get_firmwares(dev, 0, brcmf_usb_get_fwname(devinfo),
1286 NULL, brcmf_usb_probe_phase2);
1288 brcmf_err("firmware request failed: %d\n", ret);
1295 /* Release resources in reverse order */
1297 brcmf_usb_detach(devinfo);
1302 brcmf_usb_disconnect_cb(struct brcmf_usbdev_info *devinfo)
1306 brcmf_dbg(USB, "Enter, bus_pub %p\n", devinfo);
1308 brcmf_detach(devinfo->dev);
1309 kfree(devinfo->bus_pub.bus);
1310 brcmf_usb_detach(devinfo);
1314 brcmf_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
1316 struct usb_device *usb = interface_to_usbdev(intf);
1317 struct brcmf_usbdev_info *devinfo;
1318 struct usb_interface_descriptor *desc;
1319 struct usb_endpoint_descriptor *endpoint;
1322 u8 endpoint_num, ep;
1324 brcmf_dbg(USB, "Enter 0x%04x:0x%04x\n", id->idVendor, id->idProduct);
1326 devinfo = kzalloc(sizeof(*devinfo), GFP_ATOMIC);
1327 if (devinfo == NULL)
1330 devinfo->usbdev = usb;
1331 devinfo->dev = &usb->dev;
1332 /* Init completion, to protect for disconnect while still loading.
1333 * Necessary because of the asynchronous firmware load construction
1335 init_completion(&devinfo->dev_init_done);
1337 usb_set_intfdata(intf, devinfo);
1339 /* Check that the device supports only one configuration */
1340 if (usb->descriptor.bNumConfigurations != 1) {
1341 brcmf_err("Number of configurations: %d not supported\n",
1342 usb->descriptor.bNumConfigurations);
1347 if ((usb->descriptor.bDeviceClass != USB_CLASS_VENDOR_SPEC) &&
1348 (usb->descriptor.bDeviceClass != USB_CLASS_MISC) &&
1349 (usb->descriptor.bDeviceClass != USB_CLASS_WIRELESS_CONTROLLER)) {
1350 brcmf_err("Device class: 0x%x not supported\n",
1351 usb->descriptor.bDeviceClass);
1356 desc = &intf->cur_altsetting->desc;
1357 if ((desc->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
1358 (desc->bInterfaceSubClass != 2) ||
1359 (desc->bInterfaceProtocol != 0xff)) {
1360 brcmf_err("non WLAN interface %d: 0x%x:0x%x:0x%x\n",
1361 desc->bInterfaceNumber, desc->bInterfaceClass,
1362 desc->bInterfaceSubClass, desc->bInterfaceProtocol);
1367 num_of_eps = desc->bNumEndpoints;
1368 for (ep = 0; ep < num_of_eps; ep++) {
1369 endpoint = &intf->cur_altsetting->endpoint[ep].desc;
1370 endpoint_num = usb_endpoint_num(endpoint);
1371 if (!usb_endpoint_xfer_bulk(endpoint))
1373 if (usb_endpoint_dir_in(endpoint)) {
1374 if (!devinfo->rx_pipe)
1376 usb_rcvbulkpipe(usb, endpoint_num);
1378 if (!devinfo->tx_pipe)
1380 usb_sndbulkpipe(usb, endpoint_num);
1383 if (devinfo->rx_pipe == 0) {
1384 brcmf_err("No RX (in) Bulk EP found\n");
1388 if (devinfo->tx_pipe == 0) {
1389 brcmf_err("No TX (out) Bulk EP found\n");
1394 devinfo->ifnum = desc->bInterfaceNumber;
1396 if (usb->speed == USB_SPEED_SUPER)
1397 brcmf_dbg(USB, "Broadcom super speed USB WLAN interface detected\n");
1398 else if (usb->speed == USB_SPEED_HIGH)
1399 brcmf_dbg(USB, "Broadcom high speed USB WLAN interface detected\n");
1401 brcmf_dbg(USB, "Broadcom full speed USB WLAN interface detected\n");
1403 ret = brcmf_usb_probe_cb(devinfo);
1411 complete(&devinfo->dev_init_done);
1413 usb_set_intfdata(intf, NULL);
1418 brcmf_usb_disconnect(struct usb_interface *intf)
1420 struct brcmf_usbdev_info *devinfo;
1422 brcmf_dbg(USB, "Enter\n");
1423 devinfo = (struct brcmf_usbdev_info *)usb_get_intfdata(intf);
1426 wait_for_completion(&devinfo->dev_init_done);
1427 /* Make sure that devinfo still exists. Firmware probe routines
1428 * may have released the device and cleared the intfdata.
1430 if (!usb_get_intfdata(intf))
1433 brcmf_usb_disconnect_cb(devinfo);
1437 brcmf_dbg(USB, "Exit\n");
1441 * only need to signal the bus being down and update the state.
1443 static int brcmf_usb_suspend(struct usb_interface *intf, pm_message_t state)
1445 struct usb_device *usb = interface_to_usbdev(intf);
1446 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(&usb->dev);
1448 brcmf_dbg(USB, "Enter\n");
1449 devinfo->bus_pub.state = BRCMFMAC_USB_STATE_SLEEP;
1450 if (devinfo->wowl_enabled)
1451 brcmf_cancel_all_urbs(devinfo);
1453 brcmf_detach(&usb->dev);
1458 * (re-) start the bus.
1460 static int brcmf_usb_resume(struct usb_interface *intf)
1462 struct usb_device *usb = interface_to_usbdev(intf);
1463 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(&usb->dev);
1465 brcmf_dbg(USB, "Enter\n");
1466 if (!devinfo->wowl_enabled)
1467 return brcmf_usb_bus_setup(devinfo);
1469 devinfo->bus_pub.state = BRCMFMAC_USB_STATE_UP;
1470 brcmf_usb_rx_fill_all(devinfo);
1474 static int brcmf_usb_reset_resume(struct usb_interface *intf)
1476 struct usb_device *usb = interface_to_usbdev(intf);
1477 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(&usb->dev);
1479 brcmf_dbg(USB, "Enter\n");
1481 return brcmf_fw_get_firmwares(&usb->dev, 0,
1482 brcmf_usb_get_fwname(devinfo), NULL,
1483 brcmf_usb_probe_phase2);
1486 #define BRCMF_USB_DEVICE(dev_id) \
1487 { USB_DEVICE(BRCM_USB_VENDOR_ID_BROADCOM, dev_id) }
1489 static struct usb_device_id brcmf_usb_devid_table[] = {
1490 BRCMF_USB_DEVICE(BRCM_USB_43143_DEVICE_ID),
1491 BRCMF_USB_DEVICE(BRCM_USB_43236_DEVICE_ID),
1492 BRCMF_USB_DEVICE(BRCM_USB_43242_DEVICE_ID),
1493 BRCMF_USB_DEVICE(BRCM_USB_43569_DEVICE_ID),
1494 /* special entry for device with firmware loaded and running */
1495 BRCMF_USB_DEVICE(BRCM_USB_BCMFW_DEVICE_ID),
1496 { /* end: all zeroes */ }
1499 MODULE_DEVICE_TABLE(usb, brcmf_usb_devid_table);
1502 static struct usb_driver brcmf_usbdrvr = {
1503 .name = KBUILD_MODNAME,
1504 .probe = brcmf_usb_probe,
1505 .disconnect = brcmf_usb_disconnect,
1506 .id_table = brcmf_usb_devid_table,
1507 .suspend = brcmf_usb_suspend,
1508 .resume = brcmf_usb_resume,
1509 .reset_resume = brcmf_usb_reset_resume,
1510 .supports_autosuspend = 1,
1511 .disable_hub_initiated_lpm = 1,
1514 static int brcmf_usb_reset_device(struct device *dev, void *notused)
1516 /* device past is the usb interface so we
1517 * need to use parent here.
1519 brcmf_dev_reset(dev->parent);
1523 void brcmf_usb_exit(void)
1525 struct device_driver *drv = &brcmf_usbdrvr.drvwrap.driver;
1528 brcmf_dbg(USB, "Enter\n");
1529 ret = driver_for_each_device(drv, NULL, NULL,
1530 brcmf_usb_reset_device);
1531 usb_deregister(&brcmf_usbdrvr);
1534 void brcmf_usb_register(void)
1536 brcmf_dbg(USB, "Enter\n");
1537 usb_register(&brcmf_usbdrvr);