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>
34 #define IOCTL_RESP_TIMEOUT msecs_to_jiffies(2000)
36 #define BRCMF_USB_RESET_GETVER_SPINWAIT 100 /* in unit of ms */
37 #define BRCMF_USB_RESET_GETVER_LOOP_CNT 10
39 #define BRCMF_POSTBOOT_ID 0xA123 /* ID to detect if dongle
41 #define BRCMF_USB_NRXQ 50
42 #define BRCMF_USB_NTXQ 50
44 #define BRCMF_USB_CBCTL_WRITE 0
45 #define BRCMF_USB_CBCTL_READ 1
46 #define BRCMF_USB_MAX_PKT_SIZE 1600
48 BRCMF_FW_DEF(43143, "/*(DEBLOBBED)*/");
49 BRCMF_FW_DEF(43236B, "/*(DEBLOBBED)*/");
50 BRCMF_FW_DEF(43242A, "/*(DEBLOBBED)*/");
51 BRCMF_FW_DEF(43569, "/*(DEBLOBBED)*/");
53 static struct brcmf_firmware_mapping brcmf_usb_fwnames[] = {
54 BRCMF_FW_ENTRY(BRCM_CC_43143_CHIP_ID, 0xFFFFFFFF, 43143),
55 BRCMF_FW_ENTRY(BRCM_CC_43235_CHIP_ID, 0x00000008, 43236B),
56 BRCMF_FW_ENTRY(BRCM_CC_43236_CHIP_ID, 0x00000008, 43236B),
57 BRCMF_FW_ENTRY(BRCM_CC_43238_CHIP_ID, 0x00000008, 43236B),
58 BRCMF_FW_ENTRY(BRCM_CC_43242_CHIP_ID, 0xFFFFFFFF, 43242A),
59 BRCMF_FW_ENTRY(BRCM_CC_43566_CHIP_ID, 0xFFFFFFFF, 43569),
60 BRCMF_FW_ENTRY(BRCM_CC_43569_CHIP_ID, 0xFFFFFFFF, 43569)
63 #define TRX_MAGIC 0x30524448 /* "HDR0" */
64 #define TRX_MAX_OFFSET 3 /* Max number of file offsets */
65 #define TRX_UNCOMP_IMAGE 0x20 /* Trx holds uncompressed img */
66 #define TRX_RDL_CHUNK 1500 /* size of each dl transfer */
67 #define TRX_OFFSETS_DLFWLEN_IDX 0
69 /* Control messages: bRequest values */
70 #define DL_GETSTATE 0 /* returns the rdl_state_t struct */
71 #define DL_CHECK_CRC 1 /* currently unused */
72 #define DL_GO 2 /* execute downloaded image */
73 #define DL_START 3 /* initialize dl state */
74 #define DL_REBOOT 4 /* reboot the device in 2 seconds */
75 #define DL_GETVER 5 /* returns the bootrom_id_t struct */
76 #define DL_GO_PROTECTED 6 /* execute the downloaded code and set reset
77 * event to occur in 2 seconds. It is the
78 * responsibility of the downloaded code to
81 #define DL_EXEC 7 /* jump to a supplied address */
82 #define DL_RESETCFG 8 /* To support single enum on dongle
83 * - Not used by bootloader
85 #define DL_DEFER_RESP_OK 9 /* Potentially defer the response to setup
90 #define DL_WAITING 0 /* waiting to rx first pkt */
91 #define DL_READY 1 /* hdr was good, waiting for more of the
94 #define DL_BAD_HDR 2 /* hdr was corrupted */
95 #define DL_BAD_CRC 3 /* compressed image was corrupted */
96 #define DL_RUNNABLE 4 /* download was successful,waiting for go cmd */
97 #define DL_START_FAIL 5 /* failed to initialize correctly */
98 #define DL_NVRAM_TOOBIG 6 /* host specified nvram data exceeds DL_NVRAM
101 #define DL_IMAGE_TOOBIG 7 /* firmware image too big */
104 struct trx_header_le {
105 __le32 magic; /* "HDR0" */
106 __le32 len; /* Length of file including header */
107 __le32 crc32; /* CRC from flag_version to end of file */
108 __le32 flag_version; /* 0:15 flags, 16:31 version */
109 __le32 offsets[TRX_MAX_OFFSET]; /* Offsets of partitions from start of
114 struct rdl_state_le {
119 struct bootrom_id_le {
120 __le32 chip; /* Chip id */
121 __le32 chiprev; /* Chip rev */
122 __le32 ramsize; /* Size of RAM */
123 __le32 remapbase; /* Current remap base address */
124 __le32 boardtype; /* Type of board */
125 __le32 boardrev; /* Board revision */
128 struct brcmf_usb_image {
129 struct list_head list;
135 struct brcmf_usbdev_info {
136 struct brcmf_usbdev bus_pub; /* MUST BE FIRST */
138 struct list_head rx_freeq;
139 struct list_head rx_postq;
140 struct list_head tx_freeq;
141 struct list_head tx_postq;
142 uint rx_pipe, tx_pipe;
144 int rx_low_watermark;
145 int tx_low_watermark;
146 int tx_high_watermark;
149 spinlock_t tx_flowblock_lock;
151 struct brcmf_usbreq *tx_reqs;
152 struct brcmf_usbreq *rx_reqs;
154 char fw_name[BRCMF_FW_NAME_LEN];
155 const u8 *image; /* buffer for combine fw and nvram */
158 struct usb_device *usbdev;
160 struct completion dev_init_done;
162 int ctl_in_pipe, ctl_out_pipe;
163 struct urb *ctl_urb; /* URB for control endpoint */
164 struct usb_ctrlrequest ctl_write;
165 struct usb_ctrlrequest ctl_read;
166 u32 ctl_urb_actual_length;
169 wait_queue_head_t ioctl_resp_wait;
173 struct urb *bulk_urb; /* used for FW download */
176 struct brcmf_mp_device *settings;
179 static void brcmf_usb_rx_refill(struct brcmf_usbdev_info *devinfo,
180 struct brcmf_usbreq *req);
182 static struct brcmf_usbdev *brcmf_usb_get_buspub(struct device *dev)
184 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
185 return bus_if->bus_priv.usb;
188 static struct brcmf_usbdev_info *brcmf_usb_get_businfo(struct device *dev)
190 return brcmf_usb_get_buspub(dev)->devinfo;
193 static int brcmf_usb_ioctl_resp_wait(struct brcmf_usbdev_info *devinfo)
195 return wait_event_timeout(devinfo->ioctl_resp_wait,
196 devinfo->ctl_completed, IOCTL_RESP_TIMEOUT);
199 static void brcmf_usb_ioctl_resp_wake(struct brcmf_usbdev_info *devinfo)
201 wake_up(&devinfo->ioctl_resp_wait);
205 brcmf_usb_ctl_complete(struct brcmf_usbdev_info *devinfo, int type, int status)
207 brcmf_dbg(USB, "Enter, status=%d\n", status);
209 if (unlikely(devinfo == NULL))
212 if (type == BRCMF_USB_CBCTL_READ) {
214 devinfo->bus_pub.stats.rx_ctlpkts++;
216 devinfo->bus_pub.stats.rx_ctlerrs++;
217 } else if (type == BRCMF_USB_CBCTL_WRITE) {
219 devinfo->bus_pub.stats.tx_ctlpkts++;
221 devinfo->bus_pub.stats.tx_ctlerrs++;
224 devinfo->ctl_urb_status = status;
225 devinfo->ctl_completed = true;
226 brcmf_usb_ioctl_resp_wake(devinfo);
230 brcmf_usb_ctlread_complete(struct urb *urb)
232 struct brcmf_usbdev_info *devinfo =
233 (struct brcmf_usbdev_info *)urb->context;
235 brcmf_dbg(USB, "Enter\n");
236 devinfo->ctl_urb_actual_length = urb->actual_length;
237 brcmf_usb_ctl_complete(devinfo, BRCMF_USB_CBCTL_READ,
242 brcmf_usb_ctlwrite_complete(struct urb *urb)
244 struct brcmf_usbdev_info *devinfo =
245 (struct brcmf_usbdev_info *)urb->context;
247 brcmf_dbg(USB, "Enter\n");
248 brcmf_usb_ctl_complete(devinfo, BRCMF_USB_CBCTL_WRITE,
253 brcmf_usb_send_ctl(struct brcmf_usbdev_info *devinfo, u8 *buf, int len)
258 brcmf_dbg(USB, "Enter\n");
259 if (devinfo == NULL || buf == NULL ||
260 len == 0 || devinfo->ctl_urb == NULL)
264 devinfo->ctl_write.wLength = cpu_to_le16p(&size);
265 devinfo->ctl_urb->transfer_buffer_length = size;
266 devinfo->ctl_urb_status = 0;
267 devinfo->ctl_urb_actual_length = 0;
269 usb_fill_control_urb(devinfo->ctl_urb,
271 devinfo->ctl_out_pipe,
272 (unsigned char *) &devinfo->ctl_write,
274 (usb_complete_t)brcmf_usb_ctlwrite_complete,
277 ret = usb_submit_urb(devinfo->ctl_urb, GFP_ATOMIC);
279 brcmf_err("usb_submit_urb failed %d\n", ret);
285 brcmf_usb_recv_ctl(struct brcmf_usbdev_info *devinfo, u8 *buf, int len)
290 brcmf_dbg(USB, "Enter\n");
291 if ((devinfo == NULL) || (buf == NULL) || (len == 0)
292 || (devinfo->ctl_urb == NULL))
296 devinfo->ctl_read.wLength = cpu_to_le16p(&size);
297 devinfo->ctl_urb->transfer_buffer_length = size;
299 devinfo->ctl_read.bRequestType = USB_DIR_IN
300 | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
301 devinfo->ctl_read.bRequest = 1;
303 usb_fill_control_urb(devinfo->ctl_urb,
305 devinfo->ctl_in_pipe,
306 (unsigned char *) &devinfo->ctl_read,
308 (usb_complete_t)brcmf_usb_ctlread_complete,
311 ret = usb_submit_urb(devinfo->ctl_urb, GFP_ATOMIC);
313 brcmf_err("usb_submit_urb failed %d\n", ret);
318 static int brcmf_usb_tx_ctlpkt(struct device *dev, u8 *buf, u32 len)
322 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
324 brcmf_dbg(USB, "Enter\n");
325 if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP)
328 if (test_and_set_bit(0, &devinfo->ctl_op))
331 devinfo->ctl_completed = false;
332 err = brcmf_usb_send_ctl(devinfo, buf, len);
334 brcmf_err("fail %d bytes: %d\n", err, len);
335 clear_bit(0, &devinfo->ctl_op);
338 timeout = brcmf_usb_ioctl_resp_wait(devinfo);
339 clear_bit(0, &devinfo->ctl_op);
341 brcmf_err("Txctl wait timed out\n");
347 static int brcmf_usb_rx_ctlpkt(struct device *dev, u8 *buf, u32 len)
351 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
353 brcmf_dbg(USB, "Enter\n");
354 if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP)
357 if (test_and_set_bit(0, &devinfo->ctl_op))
360 devinfo->ctl_completed = false;
361 err = brcmf_usb_recv_ctl(devinfo, buf, len);
363 brcmf_err("fail %d bytes: %d\n", err, len);
364 clear_bit(0, &devinfo->ctl_op);
367 timeout = brcmf_usb_ioctl_resp_wait(devinfo);
368 err = devinfo->ctl_urb_status;
369 clear_bit(0, &devinfo->ctl_op);
371 brcmf_err("rxctl wait timed out\n");
375 return devinfo->ctl_urb_actual_length;
380 static struct brcmf_usbreq *brcmf_usb_deq(struct brcmf_usbdev_info *devinfo,
381 struct list_head *q, int *counter)
384 struct brcmf_usbreq *req;
385 spin_lock_irqsave(&devinfo->qlock, flags);
387 spin_unlock_irqrestore(&devinfo->qlock, flags);
390 req = list_entry(q->next, struct brcmf_usbreq, list);
391 list_del_init(q->next);
394 spin_unlock_irqrestore(&devinfo->qlock, flags);
399 static void brcmf_usb_enq(struct brcmf_usbdev_info *devinfo,
400 struct list_head *q, struct brcmf_usbreq *req,
404 spin_lock_irqsave(&devinfo->qlock, flags);
405 list_add_tail(&req->list, q);
408 spin_unlock_irqrestore(&devinfo->qlock, flags);
411 static struct brcmf_usbreq *
412 brcmf_usbdev_qinit(struct list_head *q, int qsize)
415 struct brcmf_usbreq *req, *reqs;
417 reqs = kcalloc(qsize, sizeof(struct brcmf_usbreq), GFP_ATOMIC);
423 for (i = 0; i < qsize; i++) {
424 req->urb = usb_alloc_urb(0, GFP_ATOMIC);
428 INIT_LIST_HEAD(&req->list);
429 list_add_tail(&req->list, q);
434 brcmf_err("fail!\n");
435 while (!list_empty(q)) {
436 req = list_entry(q->next, struct brcmf_usbreq, list);
438 usb_free_urb(req->urb);
446 static void brcmf_usb_free_q(struct list_head *q, bool pending)
448 struct brcmf_usbreq *req, *next;
450 list_for_each_entry_safe(req, next, q, list) {
452 brcmf_err("bad req\n");
457 usb_kill_urb(req->urb);
459 usb_free_urb(req->urb);
460 list_del_init(&req->list);
465 static void brcmf_usb_del_fromq(struct brcmf_usbdev_info *devinfo,
466 struct brcmf_usbreq *req)
470 spin_lock_irqsave(&devinfo->qlock, flags);
471 list_del_init(&req->list);
472 spin_unlock_irqrestore(&devinfo->qlock, flags);
476 static void brcmf_usb_tx_complete(struct urb *urb)
478 struct brcmf_usbreq *req = (struct brcmf_usbreq *)urb->context;
479 struct brcmf_usbdev_info *devinfo = req->devinfo;
482 brcmf_dbg(USB, "Enter, urb->status=%d, skb=%p\n", urb->status,
484 brcmf_usb_del_fromq(devinfo, req);
486 brcmf_txcomplete(devinfo->dev, req->skb, urb->status == 0);
488 brcmf_usb_enq(devinfo, &devinfo->tx_freeq, req, &devinfo->tx_freecount);
489 spin_lock_irqsave(&devinfo->tx_flowblock_lock, flags);
490 if (devinfo->tx_freecount > devinfo->tx_high_watermark &&
491 devinfo->tx_flowblock) {
492 brcmf_txflowblock(devinfo->dev, false);
493 devinfo->tx_flowblock = false;
495 spin_unlock_irqrestore(&devinfo->tx_flowblock_lock, flags);
498 static void brcmf_usb_rx_complete(struct urb *urb)
500 struct brcmf_usbreq *req = (struct brcmf_usbreq *)urb->context;
501 struct brcmf_usbdev_info *devinfo = req->devinfo;
504 brcmf_dbg(USB, "Enter, urb->status=%d\n", urb->status);
505 brcmf_usb_del_fromq(devinfo, req);
509 /* zero lenght packets indicate usb "failure". Do not refill */
510 if (urb->status != 0 || !urb->actual_length) {
511 brcmu_pkt_buf_free_skb(skb);
512 brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
516 if (devinfo->bus_pub.state == BRCMFMAC_USB_STATE_UP) {
517 skb_put(skb, urb->actual_length);
518 brcmf_rx_frame(devinfo->dev, skb, true);
519 brcmf_usb_rx_refill(devinfo, req);
521 brcmu_pkt_buf_free_skb(skb);
522 brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
528 static void brcmf_usb_rx_refill(struct brcmf_usbdev_info *devinfo,
529 struct brcmf_usbreq *req)
534 if (!req || !devinfo)
537 skb = dev_alloc_skb(devinfo->bus_pub.bus_mtu);
539 brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
544 usb_fill_bulk_urb(req->urb, devinfo->usbdev, devinfo->rx_pipe,
545 skb->data, skb_tailroom(skb), brcmf_usb_rx_complete,
547 req->devinfo = devinfo;
548 brcmf_usb_enq(devinfo, &devinfo->rx_postq, req, NULL);
550 ret = usb_submit_urb(req->urb, GFP_ATOMIC);
552 brcmf_usb_del_fromq(devinfo, req);
553 brcmu_pkt_buf_free_skb(req->skb);
555 brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
560 static void brcmf_usb_rx_fill_all(struct brcmf_usbdev_info *devinfo)
562 struct brcmf_usbreq *req;
564 if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP) {
565 brcmf_err("bus is not up=%d\n", devinfo->bus_pub.state);
568 while ((req = brcmf_usb_deq(devinfo, &devinfo->rx_freeq, NULL)) != NULL)
569 brcmf_usb_rx_refill(devinfo, req);
573 brcmf_usb_state_change(struct brcmf_usbdev_info *devinfo, int state)
575 struct brcmf_bus *bcmf_bus = devinfo->bus_pub.bus;
578 brcmf_dbg(USB, "Enter, current state=%d, new state=%d\n",
579 devinfo->bus_pub.state, state);
581 if (devinfo->bus_pub.state == state)
584 old_state = devinfo->bus_pub.state;
585 devinfo->bus_pub.state = state;
587 /* update state of upper layer */
588 if (state == BRCMFMAC_USB_STATE_DOWN) {
589 brcmf_dbg(USB, "DBUS is down\n");
590 brcmf_bus_change_state(bcmf_bus, BRCMF_BUS_DOWN);
591 } else if (state == BRCMFMAC_USB_STATE_UP) {
592 brcmf_dbg(USB, "DBUS is up\n");
593 brcmf_bus_change_state(bcmf_bus, BRCMF_BUS_UP);
595 brcmf_dbg(USB, "DBUS current state=%d\n", state);
599 static int brcmf_usb_tx(struct device *dev, struct sk_buff *skb)
601 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
602 struct brcmf_usbreq *req;
606 brcmf_dbg(USB, "Enter, skb=%p\n", skb);
607 if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP) {
612 req = brcmf_usb_deq(devinfo, &devinfo->tx_freeq,
613 &devinfo->tx_freecount);
615 brcmf_err("no req to send\n");
621 req->devinfo = devinfo;
622 usb_fill_bulk_urb(req->urb, devinfo->usbdev, devinfo->tx_pipe,
623 skb->data, skb->len, brcmf_usb_tx_complete, req);
624 req->urb->transfer_flags |= URB_ZERO_PACKET;
625 brcmf_usb_enq(devinfo, &devinfo->tx_postq, req, NULL);
626 ret = usb_submit_urb(req->urb, GFP_ATOMIC);
628 brcmf_err("brcmf_usb_tx usb_submit_urb FAILED\n");
629 brcmf_usb_del_fromq(devinfo, req);
631 brcmf_usb_enq(devinfo, &devinfo->tx_freeq, req,
632 &devinfo->tx_freecount);
636 spin_lock_irqsave(&devinfo->tx_flowblock_lock, flags);
637 if (devinfo->tx_freecount < devinfo->tx_low_watermark &&
638 !devinfo->tx_flowblock) {
639 brcmf_txflowblock(dev, true);
640 devinfo->tx_flowblock = true;
642 spin_unlock_irqrestore(&devinfo->tx_flowblock_lock, flags);
650 static int brcmf_usb_up(struct device *dev)
652 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
654 brcmf_dbg(USB, "Enter\n");
655 if (devinfo->bus_pub.state == BRCMFMAC_USB_STATE_UP)
658 /* Success, indicate devinfo is fully up */
659 brcmf_usb_state_change(devinfo, BRCMFMAC_USB_STATE_UP);
661 if (devinfo->ctl_urb) {
662 devinfo->ctl_in_pipe = usb_rcvctrlpipe(devinfo->usbdev, 0);
663 devinfo->ctl_out_pipe = usb_sndctrlpipe(devinfo->usbdev, 0);
666 devinfo->ctl_write.bRequestType =
667 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
668 devinfo->ctl_write.bRequest = 0;
669 devinfo->ctl_write.wValue = cpu_to_le16(0);
670 devinfo->ctl_write.wIndex = cpu_to_le16(devinfo->ifnum);
673 devinfo->ctl_read.bRequestType =
674 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
675 devinfo->ctl_read.bRequest = 1;
676 devinfo->ctl_read.wValue = cpu_to_le16(0);
677 devinfo->ctl_read.wIndex = cpu_to_le16(devinfo->ifnum);
679 brcmf_usb_rx_fill_all(devinfo);
683 static void brcmf_cancel_all_urbs(struct brcmf_usbdev_info *devinfo)
687 if (devinfo->ctl_urb)
688 usb_kill_urb(devinfo->ctl_urb);
689 if (devinfo->bulk_urb)
690 usb_kill_urb(devinfo->bulk_urb);
691 if (devinfo->tx_reqs)
692 for (i = 0; i < devinfo->bus_pub.ntxq; i++)
693 usb_kill_urb(devinfo->tx_reqs[i].urb);
694 if (devinfo->rx_reqs)
695 for (i = 0; i < devinfo->bus_pub.nrxq; i++)
696 usb_kill_urb(devinfo->rx_reqs[i].urb);
699 static void brcmf_usb_down(struct device *dev)
701 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
703 brcmf_dbg(USB, "Enter\n");
707 if (devinfo->bus_pub.state == BRCMFMAC_USB_STATE_DOWN)
710 brcmf_usb_state_change(devinfo, BRCMFMAC_USB_STATE_DOWN);
712 brcmf_cancel_all_urbs(devinfo);
716 brcmf_usb_sync_complete(struct urb *urb)
718 struct brcmf_usbdev_info *devinfo =
719 (struct brcmf_usbdev_info *)urb->context;
721 devinfo->ctl_completed = true;
722 brcmf_usb_ioctl_resp_wake(devinfo);
725 static int brcmf_usb_dl_cmd(struct brcmf_usbdev_info *devinfo, u8 cmd,
726 void *buffer, int buflen)
732 if ((!devinfo) || (devinfo->ctl_urb == NULL))
735 tmpbuf = kmalloc(buflen, GFP_ATOMIC);
740 devinfo->ctl_urb->transfer_buffer_length = size;
742 devinfo->ctl_read.wLength = cpu_to_le16p(&size);
743 devinfo->ctl_read.bRequestType = USB_DIR_IN | USB_TYPE_VENDOR |
745 devinfo->ctl_read.bRequest = cmd;
747 usb_fill_control_urb(devinfo->ctl_urb,
749 usb_rcvctrlpipe(devinfo->usbdev, 0),
750 (unsigned char *) &devinfo->ctl_read,
751 (void *) tmpbuf, size,
752 (usb_complete_t)brcmf_usb_sync_complete, devinfo);
754 devinfo->ctl_completed = false;
755 ret = usb_submit_urb(devinfo->ctl_urb, GFP_ATOMIC);
757 brcmf_err("usb_submit_urb failed %d\n", ret);
761 if (!brcmf_usb_ioctl_resp_wait(devinfo)) {
762 usb_kill_urb(devinfo->ctl_urb);
765 memcpy(buffer, tmpbuf, buflen);
774 brcmf_usb_dlneeded(struct brcmf_usbdev_info *devinfo)
776 struct bootrom_id_le id;
779 brcmf_dbg(USB, "Enter\n");
784 /* Check if firmware downloaded already by querying runtime ID */
785 id.chip = cpu_to_le32(0xDEAD);
786 brcmf_usb_dl_cmd(devinfo, DL_GETVER, &id, sizeof(id));
788 chipid = le32_to_cpu(id.chip);
789 chiprev = le32_to_cpu(id.chiprev);
791 if ((chipid & 0x4300) == 0x4300)
792 brcmf_dbg(USB, "chip %x rev 0x%x\n", chipid, chiprev);
794 brcmf_dbg(USB, "chip %d rev 0x%x\n", chipid, chiprev);
795 if (chipid == BRCMF_POSTBOOT_ID) {
796 brcmf_dbg(USB, "firmware already downloaded\n");
797 brcmf_usb_dl_cmd(devinfo, DL_RESETCFG, &id, sizeof(id));
800 devinfo->bus_pub.devid = chipid;
801 devinfo->bus_pub.chiprev = chiprev;
807 brcmf_usb_resetcfg(struct brcmf_usbdev_info *devinfo)
809 struct bootrom_id_le id;
813 brcmf_dbg(USB, "Enter\n");
817 mdelay(BRCMF_USB_RESET_GETVER_SPINWAIT);
819 id.chip = cpu_to_le32(0xDEAD); /* Get the ID */
820 err = brcmf_usb_dl_cmd(devinfo, DL_GETVER, &id, sizeof(id));
821 if ((err) && (err != -ETIMEDOUT))
823 if (id.chip == cpu_to_le32(BRCMF_POSTBOOT_ID))
825 } while (loop_cnt < BRCMF_USB_RESET_GETVER_LOOP_CNT);
827 if (id.chip == cpu_to_le32(BRCMF_POSTBOOT_ID)) {
828 brcmf_dbg(USB, "postboot chip 0x%x/rev 0x%x\n",
829 le32_to_cpu(id.chip), le32_to_cpu(id.chiprev));
831 brcmf_usb_dl_cmd(devinfo, DL_RESETCFG, &id, sizeof(id));
834 brcmf_err("Cannot talk to Dongle. Firmware is not UP, %d ms\n",
835 BRCMF_USB_RESET_GETVER_SPINWAIT * loop_cnt);
842 brcmf_usb_dl_send_bulk(struct brcmf_usbdev_info *devinfo, void *buffer, int len)
846 if ((devinfo == NULL) || (devinfo->bulk_urb == NULL))
849 /* Prepare the URB */
850 usb_fill_bulk_urb(devinfo->bulk_urb, devinfo->usbdev,
851 devinfo->tx_pipe, buffer, len,
852 (usb_complete_t)brcmf_usb_sync_complete, devinfo);
854 devinfo->bulk_urb->transfer_flags |= URB_ZERO_PACKET;
856 devinfo->ctl_completed = false;
857 ret = usb_submit_urb(devinfo->bulk_urb, GFP_ATOMIC);
859 brcmf_err("usb_submit_urb failed %d\n", ret);
862 ret = brcmf_usb_ioctl_resp_wait(devinfo);
867 brcmf_usb_dl_writeimage(struct brcmf_usbdev_info *devinfo, u8 *fw, int fwlen)
869 unsigned int sendlen, sent, dllen;
870 char *bulkchunk = NULL, *dlpos;
871 struct rdl_state_le state;
872 u32 rdlstate, rdlbytes;
875 brcmf_dbg(USB, "Enter, fw %p, len %d\n", fw, fwlen);
877 bulkchunk = kmalloc(TRX_RDL_CHUNK, GFP_ATOMIC);
878 if (bulkchunk == NULL) {
883 /* 1) Prepare USB boot loader for runtime image */
884 brcmf_usb_dl_cmd(devinfo, DL_START, &state, sizeof(state));
886 rdlstate = le32_to_cpu(state.state);
887 rdlbytes = le32_to_cpu(state.bytes);
889 /* 2) Check we are in the Waiting state */
890 if (rdlstate != DL_WAITING) {
891 brcmf_err("Failed to DL_START\n");
899 /* Get chip id and rev */
900 while (rdlbytes != dllen) {
901 /* Wait until the usb device reports it received all
902 * the bytes we sent */
903 if ((rdlbytes == sent) && (rdlbytes != dllen)) {
904 if ((dllen-sent) < TRX_RDL_CHUNK)
905 sendlen = dllen-sent;
907 sendlen = TRX_RDL_CHUNK;
909 /* simply avoid having to send a ZLP by ensuring we
917 memcpy(bulkchunk, dlpos, sendlen);
918 if (brcmf_usb_dl_send_bulk(devinfo, bulkchunk,
920 brcmf_err("send_bulk failed\n");
928 err = brcmf_usb_dl_cmd(devinfo, DL_GETSTATE, &state,
931 brcmf_err("DL_GETSTATE Failed\n");
935 rdlstate = le32_to_cpu(state.state);
936 rdlbytes = le32_to_cpu(state.bytes);
938 /* restart if an error is reported */
939 if (rdlstate == DL_BAD_HDR || rdlstate == DL_BAD_CRC) {
940 brcmf_err("Bad Hdr or Bad CRC state %d\n",
949 brcmf_dbg(USB, "Exit, err=%d\n", err);
953 static int brcmf_usb_dlstart(struct brcmf_usbdev_info *devinfo, u8 *fw, int len)
957 brcmf_dbg(USB, "Enter\n");
962 if (devinfo->bus_pub.devid == 0xDEAD)
965 err = brcmf_usb_dl_writeimage(devinfo, fw, len);
967 devinfo->bus_pub.state = BRCMFMAC_USB_STATE_DL_DONE;
969 devinfo->bus_pub.state = BRCMFMAC_USB_STATE_DL_FAIL;
970 brcmf_dbg(USB, "Exit, err=%d\n", err);
975 static int brcmf_usb_dlrun(struct brcmf_usbdev_info *devinfo)
977 struct rdl_state_le state;
979 brcmf_dbg(USB, "Enter\n");
983 if (devinfo->bus_pub.devid == 0xDEAD)
986 /* Check we are runnable */
988 brcmf_usb_dl_cmd(devinfo, DL_GETSTATE, &state, sizeof(state));
990 /* Start the image */
991 if (state.state == cpu_to_le32(DL_RUNNABLE)) {
992 if (brcmf_usb_dl_cmd(devinfo, DL_GO, &state, sizeof(state)))
994 if (brcmf_usb_resetcfg(devinfo))
996 /* The Dongle may go for re-enumeration. */
998 brcmf_err("Dongle not runnable\n");
1001 brcmf_dbg(USB, "Exit\n");
1006 brcmf_usb_fw_download(struct brcmf_usbdev_info *devinfo)
1010 brcmf_dbg(USB, "Enter\n");
1011 if (devinfo == NULL)
1014 if (!devinfo->image) {
1015 brcmf_err("No firmware!\n");
1019 err = brcmf_usb_dlstart(devinfo,
1020 (u8 *)devinfo->image, devinfo->image_len);
1022 err = brcmf_usb_dlrun(devinfo);
1027 static void brcmf_usb_detach(struct brcmf_usbdev_info *devinfo)
1029 brcmf_dbg(USB, "Enter, devinfo %p\n", devinfo);
1032 brcmf_usb_free_q(&devinfo->rx_freeq, false);
1033 brcmf_usb_free_q(&devinfo->tx_freeq, false);
1035 usb_free_urb(devinfo->ctl_urb);
1036 usb_free_urb(devinfo->bulk_urb);
1038 kfree(devinfo->tx_reqs);
1039 kfree(devinfo->rx_reqs);
1041 if (devinfo->settings)
1042 brcmf_release_module_param(devinfo->settings);
1046 static int check_file(const u8 *headers)
1048 struct trx_header_le *trx;
1049 int actual_len = -1;
1051 brcmf_dbg(USB, "Enter\n");
1052 /* Extract trx header */
1053 trx = (struct trx_header_le *) headers;
1054 if (trx->magic != cpu_to_le32(TRX_MAGIC))
1057 headers += sizeof(struct trx_header_le);
1059 if (le32_to_cpu(trx->flag_version) & TRX_UNCOMP_IMAGE) {
1060 actual_len = le32_to_cpu(trx->offsets[TRX_OFFSETS_DLFWLEN_IDX]);
1061 return actual_len + sizeof(struct trx_header_le);
1068 struct brcmf_usbdev *brcmf_usb_attach(struct brcmf_usbdev_info *devinfo,
1071 brcmf_dbg(USB, "Enter\n");
1073 devinfo->bus_pub.nrxq = nrxq;
1074 devinfo->rx_low_watermark = nrxq / 2;
1075 devinfo->bus_pub.devinfo = devinfo;
1076 devinfo->bus_pub.ntxq = ntxq;
1077 devinfo->bus_pub.state = BRCMFMAC_USB_STATE_DOWN;
1079 /* flow control when too many tx urbs posted */
1080 devinfo->tx_low_watermark = ntxq / 4;
1081 devinfo->tx_high_watermark = devinfo->tx_low_watermark * 3;
1082 devinfo->bus_pub.bus_mtu = BRCMF_USB_MAX_PKT_SIZE;
1084 /* Initialize other structure content */
1085 init_waitqueue_head(&devinfo->ioctl_resp_wait);
1087 /* Initialize the spinlocks */
1088 spin_lock_init(&devinfo->qlock);
1089 spin_lock_init(&devinfo->tx_flowblock_lock);
1091 INIT_LIST_HEAD(&devinfo->rx_freeq);
1092 INIT_LIST_HEAD(&devinfo->rx_postq);
1094 INIT_LIST_HEAD(&devinfo->tx_freeq);
1095 INIT_LIST_HEAD(&devinfo->tx_postq);
1097 devinfo->tx_flowblock = false;
1099 devinfo->rx_reqs = brcmf_usbdev_qinit(&devinfo->rx_freeq, nrxq);
1100 if (!devinfo->rx_reqs)
1103 devinfo->tx_reqs = brcmf_usbdev_qinit(&devinfo->tx_freeq, ntxq);
1104 if (!devinfo->tx_reqs)
1106 devinfo->tx_freecount = ntxq;
1108 devinfo->ctl_urb = usb_alloc_urb(0, GFP_ATOMIC);
1109 if (!devinfo->ctl_urb)
1111 devinfo->bulk_urb = usb_alloc_urb(0, GFP_ATOMIC);
1112 if (!devinfo->bulk_urb)
1115 return &devinfo->bus_pub;
1118 brcmf_err("failed!\n");
1119 brcmf_usb_detach(devinfo);
1123 static void brcmf_usb_wowl_config(struct device *dev, bool enabled)
1125 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
1127 brcmf_dbg(USB, "Configuring WOWL, enabled=%d\n", enabled);
1128 devinfo->wowl_enabled = enabled;
1130 device_set_wakeup_enable(devinfo->dev, true);
1132 device_set_wakeup_enable(devinfo->dev, false);
1135 static const struct brcmf_bus_ops brcmf_usb_bus_ops = {
1136 .txdata = brcmf_usb_tx,
1137 .stop = brcmf_usb_down,
1138 .txctl = brcmf_usb_tx_ctlpkt,
1139 .rxctl = brcmf_usb_rx_ctlpkt,
1140 .wowl_config = brcmf_usb_wowl_config,
1143 static int brcmf_usb_bus_setup(struct brcmf_usbdev_info *devinfo)
1147 /* Attach to the common driver interface */
1148 ret = brcmf_attach(devinfo->dev, devinfo->settings);
1150 brcmf_err("brcmf_attach failed\n");
1154 ret = brcmf_usb_up(devinfo->dev);
1158 ret = brcmf_bus_start(devinfo->dev);
1164 brcmf_detach(devinfo->dev);
1168 static void brcmf_usb_probe_phase2(struct device *dev, int ret,
1169 const struct firmware *fw,
1170 void *nvram, u32 nvlen)
1172 struct brcmf_bus *bus = dev_get_drvdata(dev);
1173 struct brcmf_usbdev_info *devinfo = bus->bus_priv.usb->devinfo;
1178 brcmf_dbg(USB, "Start fw downloading\n");
1180 ret = check_file(fw->data);
1182 brcmf_err("invalid firmware\n");
1183 release_firmware(fw);
1187 devinfo->image = fw->data;
1188 devinfo->image_len = fw->size;
1190 ret = brcmf_usb_fw_download(devinfo);
1191 release_firmware(fw);
1195 ret = brcmf_usb_bus_setup(devinfo);
1199 complete(&devinfo->dev_init_done);
1202 brcmf_dbg(TRACE, "failed: dev=%s, err=%d\n", dev_name(dev), ret);
1203 complete(&devinfo->dev_init_done);
1204 device_release_driver(dev);
1207 static int brcmf_usb_probe_cb(struct brcmf_usbdev_info *devinfo)
1209 struct brcmf_bus *bus = NULL;
1210 struct brcmf_usbdev *bus_pub = NULL;
1211 struct device *dev = devinfo->dev;
1214 brcmf_dbg(USB, "Enter\n");
1215 bus_pub = brcmf_usb_attach(devinfo, BRCMF_USB_NRXQ, BRCMF_USB_NTXQ);
1219 bus = kzalloc(sizeof(struct brcmf_bus), GFP_ATOMIC);
1227 bus->bus_priv.usb = bus_pub;
1228 dev_set_drvdata(dev, bus);
1229 bus->ops = &brcmf_usb_bus_ops;
1230 bus->proto_type = BRCMF_PROTO_BCDC;
1231 bus->always_use_fws_queue = true;
1233 bus->wowl_supported = true;
1236 devinfo->settings = brcmf_get_module_param(bus->dev, BRCMF_BUSTYPE_USB,
1239 if (!devinfo->settings) {
1244 if (!brcmf_usb_dlneeded(devinfo)) {
1245 ret = brcmf_usb_bus_setup(devinfo);
1249 complete(&devinfo->dev_init_done);
1252 bus->chip = bus_pub->devid;
1253 bus->chiprev = bus_pub->chiprev;
1255 ret = brcmf_fw_map_chip_to_name(bus_pub->devid, bus_pub->chiprev,
1257 ARRAY_SIZE(brcmf_usb_fwnames),
1258 devinfo->fw_name, NULL);
1262 /* request firmware here */
1263 ret = brcmf_fw_get_firmwares(dev, 0, devinfo->fw_name, NULL,
1264 brcmf_usb_probe_phase2);
1266 brcmf_err("firmware request failed: %d\n", ret);
1273 /* Release resources in reverse order */
1275 brcmf_usb_detach(devinfo);
1280 brcmf_usb_disconnect_cb(struct brcmf_usbdev_info *devinfo)
1284 brcmf_dbg(USB, "Enter, bus_pub %p\n", devinfo);
1286 brcmf_detach(devinfo->dev);
1287 kfree(devinfo->bus_pub.bus);
1288 brcmf_usb_detach(devinfo);
1292 brcmf_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
1294 struct usb_device *usb = interface_to_usbdev(intf);
1295 struct brcmf_usbdev_info *devinfo;
1296 struct usb_interface_descriptor *desc;
1297 struct usb_endpoint_descriptor *endpoint;
1300 u8 endpoint_num, ep;
1302 brcmf_dbg(USB, "Enter 0x%04x:0x%04x\n", id->idVendor, id->idProduct);
1304 devinfo = kzalloc(sizeof(*devinfo), GFP_ATOMIC);
1305 if (devinfo == NULL)
1308 devinfo->usbdev = usb;
1309 devinfo->dev = &usb->dev;
1310 /* Init completion, to protect for disconnect while still loading.
1311 * Necessary because of the asynchronous firmware load construction
1313 init_completion(&devinfo->dev_init_done);
1315 usb_set_intfdata(intf, devinfo);
1317 /* Check that the device supports only one configuration */
1318 if (usb->descriptor.bNumConfigurations != 1) {
1319 brcmf_err("Number of configurations: %d not supported\n",
1320 usb->descriptor.bNumConfigurations);
1325 if ((usb->descriptor.bDeviceClass != USB_CLASS_VENDOR_SPEC) &&
1326 (usb->descriptor.bDeviceClass != USB_CLASS_MISC) &&
1327 (usb->descriptor.bDeviceClass != USB_CLASS_WIRELESS_CONTROLLER)) {
1328 brcmf_err("Device class: 0x%x not supported\n",
1329 usb->descriptor.bDeviceClass);
1334 desc = &intf->cur_altsetting->desc;
1335 if ((desc->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
1336 (desc->bInterfaceSubClass != 2) ||
1337 (desc->bInterfaceProtocol != 0xff)) {
1338 brcmf_err("non WLAN interface %d: 0x%x:0x%x:0x%x\n",
1339 desc->bInterfaceNumber, desc->bInterfaceClass,
1340 desc->bInterfaceSubClass, desc->bInterfaceProtocol);
1345 num_of_eps = desc->bNumEndpoints;
1346 for (ep = 0; ep < num_of_eps; ep++) {
1347 endpoint = &intf->cur_altsetting->endpoint[ep].desc;
1348 endpoint_num = usb_endpoint_num(endpoint);
1349 if (!usb_endpoint_xfer_bulk(endpoint))
1351 if (usb_endpoint_dir_in(endpoint)) {
1352 if (!devinfo->rx_pipe)
1354 usb_rcvbulkpipe(usb, endpoint_num);
1356 if (!devinfo->tx_pipe)
1358 usb_sndbulkpipe(usb, endpoint_num);
1361 if (devinfo->rx_pipe == 0) {
1362 brcmf_err("No RX (in) Bulk EP found\n");
1366 if (devinfo->tx_pipe == 0) {
1367 brcmf_err("No TX (out) Bulk EP found\n");
1372 devinfo->ifnum = desc->bInterfaceNumber;
1374 if (usb->speed == USB_SPEED_SUPER_PLUS)
1375 brcmf_dbg(USB, "Broadcom super speed plus USB WLAN interface detected\n");
1376 else if (usb->speed == USB_SPEED_SUPER)
1377 brcmf_dbg(USB, "Broadcom super speed USB WLAN interface detected\n");
1378 else if (usb->speed == USB_SPEED_HIGH)
1379 brcmf_dbg(USB, "Broadcom high speed USB WLAN interface detected\n");
1381 brcmf_dbg(USB, "Broadcom full speed USB WLAN interface detected\n");
1383 ret = brcmf_usb_probe_cb(devinfo);
1391 complete(&devinfo->dev_init_done);
1393 usb_set_intfdata(intf, NULL);
1398 brcmf_usb_disconnect(struct usb_interface *intf)
1400 struct brcmf_usbdev_info *devinfo;
1402 brcmf_dbg(USB, "Enter\n");
1403 devinfo = (struct brcmf_usbdev_info *)usb_get_intfdata(intf);
1406 wait_for_completion(&devinfo->dev_init_done);
1407 /* Make sure that devinfo still exists. Firmware probe routines
1408 * may have released the device and cleared the intfdata.
1410 if (!usb_get_intfdata(intf))
1413 brcmf_usb_disconnect_cb(devinfo);
1417 brcmf_dbg(USB, "Exit\n");
1421 * only need to signal the bus being down and update the state.
1423 static int brcmf_usb_suspend(struct usb_interface *intf, pm_message_t state)
1425 struct usb_device *usb = interface_to_usbdev(intf);
1426 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(&usb->dev);
1428 brcmf_dbg(USB, "Enter\n");
1429 devinfo->bus_pub.state = BRCMFMAC_USB_STATE_SLEEP;
1430 if (devinfo->wowl_enabled)
1431 brcmf_cancel_all_urbs(devinfo);
1433 brcmf_detach(&usb->dev);
1438 * (re-) start the bus.
1440 static int brcmf_usb_resume(struct usb_interface *intf)
1442 struct usb_device *usb = interface_to_usbdev(intf);
1443 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(&usb->dev);
1445 brcmf_dbg(USB, "Enter\n");
1446 if (!devinfo->wowl_enabled)
1447 return brcmf_usb_bus_setup(devinfo);
1449 devinfo->bus_pub.state = BRCMFMAC_USB_STATE_UP;
1450 brcmf_usb_rx_fill_all(devinfo);
1454 static int brcmf_usb_reset_resume(struct usb_interface *intf)
1456 struct usb_device *usb = interface_to_usbdev(intf);
1457 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(&usb->dev);
1459 brcmf_dbg(USB, "Enter\n");
1461 return brcmf_fw_get_firmwares(&usb->dev, 0, devinfo->fw_name, NULL,
1462 brcmf_usb_probe_phase2);
1465 #define BRCMF_USB_DEVICE(dev_id) \
1466 { USB_DEVICE(BRCM_USB_VENDOR_ID_BROADCOM, dev_id) }
1468 #define LINKSYS_USB_DEVICE(dev_id) \
1469 { USB_DEVICE(BRCM_USB_VENDOR_ID_LINKSYS, dev_id) }
1471 static struct usb_device_id brcmf_usb_devid_table[] = {
1472 BRCMF_USB_DEVICE(BRCM_USB_43143_DEVICE_ID),
1473 BRCMF_USB_DEVICE(BRCM_USB_43236_DEVICE_ID),
1474 BRCMF_USB_DEVICE(BRCM_USB_43242_DEVICE_ID),
1475 BRCMF_USB_DEVICE(BRCM_USB_43569_DEVICE_ID),
1476 LINKSYS_USB_DEVICE(BRCM_USB_43235_LINKSYS_DEVICE_ID),
1477 { USB_DEVICE(BRCM_USB_VENDOR_ID_LG, BRCM_USB_43242_LG_DEVICE_ID) },
1478 /* special entry for device with firmware loaded and running */
1479 BRCMF_USB_DEVICE(BRCM_USB_BCMFW_DEVICE_ID),
1480 { /* end: all zeroes */ }
1483 MODULE_DEVICE_TABLE(usb, brcmf_usb_devid_table);
1485 static struct usb_driver brcmf_usbdrvr = {
1486 .name = KBUILD_MODNAME,
1487 .probe = brcmf_usb_probe,
1488 .disconnect = brcmf_usb_disconnect,
1489 .id_table = brcmf_usb_devid_table,
1490 .suspend = brcmf_usb_suspend,
1491 .resume = brcmf_usb_resume,
1492 .reset_resume = brcmf_usb_reset_resume,
1493 .disable_hub_initiated_lpm = 1,
1496 static int brcmf_usb_reset_device(struct device *dev, void *notused)
1498 /* device past is the usb interface so we
1499 * need to use parent here.
1501 brcmf_dev_reset(dev->parent);
1505 void brcmf_usb_exit(void)
1507 struct device_driver *drv = &brcmf_usbdrvr.drvwrap.driver;
1510 brcmf_dbg(USB, "Enter\n");
1511 ret = driver_for_each_device(drv, NULL, NULL,
1512 brcmf_usb_reset_device);
1513 usb_deregister(&brcmf_usbdrvr);
1516 void brcmf_usb_register(void)
1518 brcmf_dbg(USB, "Enter\n");
1519 usb_register(&brcmf_usbdrvr);