WIP FPC-III support
[linux/fpc-iii.git] / drivers / net / wireless / broadcom / brcm80211 / brcmfmac / usb.c
blob586f4dfc638b9ecd69f53898f95896af9ea689ce
1 // SPDX-License-Identifier: ISC
2 /*
3 * Copyright (c) 2011 Broadcom Corporation
4 */
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/firmware.h>
9 #include <linux/usb.h>
10 #include <linux/vmalloc.h>
12 #include <brcmu_utils.h>
13 #include <brcm_hw_ids.h>
14 #include <brcmu_wifi.h>
15 #include "bus.h"
16 #include "debug.h"
17 #include "firmware.h"
18 #include "usb.h"
19 #include "core.h"
20 #include "common.h"
21 #include "bcdc.h"
24 #define IOCTL_RESP_TIMEOUT msecs_to_jiffies(2000)
26 #define BRCMF_USB_RESET_GETVER_SPINWAIT 100 /* in unit of ms */
27 #define BRCMF_USB_RESET_GETVER_LOOP_CNT 10
29 #define BRCMF_POSTBOOT_ID 0xA123 /* ID to detect if dongle
30 has boot up */
31 #define BRCMF_USB_NRXQ 50
32 #define BRCMF_USB_NTXQ 50
34 #define BRCMF_USB_CBCTL_WRITE 0
35 #define BRCMF_USB_CBCTL_READ 1
36 #define BRCMF_USB_MAX_PKT_SIZE 1600
38 BRCMF_FW_DEF(43143, "brcmfmac43143");
39 BRCMF_FW_DEF(43236B, "brcmfmac43236b");
40 BRCMF_FW_DEF(43242A, "brcmfmac43242a");
41 BRCMF_FW_DEF(43569, "brcmfmac43569");
42 BRCMF_FW_DEF(4373, "brcmfmac4373");
44 static const struct brcmf_firmware_mapping brcmf_usb_fwnames[] = {
45 BRCMF_FW_ENTRY(BRCM_CC_43143_CHIP_ID, 0xFFFFFFFF, 43143),
46 BRCMF_FW_ENTRY(BRCM_CC_43235_CHIP_ID, 0x00000008, 43236B),
47 BRCMF_FW_ENTRY(BRCM_CC_43236_CHIP_ID, 0x00000008, 43236B),
48 BRCMF_FW_ENTRY(BRCM_CC_43238_CHIP_ID, 0x00000008, 43236B),
49 BRCMF_FW_ENTRY(BRCM_CC_43242_CHIP_ID, 0xFFFFFFFF, 43242A),
50 BRCMF_FW_ENTRY(BRCM_CC_43566_CHIP_ID, 0xFFFFFFFF, 43569),
51 BRCMF_FW_ENTRY(BRCM_CC_43569_CHIP_ID, 0xFFFFFFFF, 43569),
52 BRCMF_FW_ENTRY(CY_CC_4373_CHIP_ID, 0xFFFFFFFF, 4373)
55 #define TRX_MAGIC 0x30524448 /* "HDR0" */
56 #define TRX_MAX_OFFSET 3 /* Max number of file offsets */
57 #define TRX_UNCOMP_IMAGE 0x20 /* Trx holds uncompressed img */
58 #define TRX_RDL_CHUNK 1500 /* size of each dl transfer */
59 #define TRX_OFFSETS_DLFWLEN_IDX 0
61 /* Control messages: bRequest values */
62 #define DL_GETSTATE 0 /* returns the rdl_state_t struct */
63 #define DL_CHECK_CRC 1 /* currently unused */
64 #define DL_GO 2 /* execute downloaded image */
65 #define DL_START 3 /* initialize dl state */
66 #define DL_REBOOT 4 /* reboot the device in 2 seconds */
67 #define DL_GETVER 5 /* returns the bootrom_id_t struct */
68 #define DL_GO_PROTECTED 6 /* execute the downloaded code and set reset
69 * event to occur in 2 seconds. It is the
70 * responsibility of the downloaded code to
71 * clear this event
73 #define DL_EXEC 7 /* jump to a supplied address */
74 #define DL_RESETCFG 8 /* To support single enum on dongle
75 * - Not used by bootloader
77 #define DL_DEFER_RESP_OK 9 /* Potentially defer the response to setup
78 * if resp unavailable
81 /* states */
82 #define DL_WAITING 0 /* waiting to rx first pkt */
83 #define DL_READY 1 /* hdr was good, waiting for more of the
84 * compressed image
86 #define DL_BAD_HDR 2 /* hdr was corrupted */
87 #define DL_BAD_CRC 3 /* compressed image was corrupted */
88 #define DL_RUNNABLE 4 /* download was successful,waiting for go cmd */
89 #define DL_START_FAIL 5 /* failed to initialize correctly */
90 #define DL_NVRAM_TOOBIG 6 /* host specified nvram data exceeds DL_NVRAM
91 * value
93 #define DL_IMAGE_TOOBIG 7 /* firmware image too big */
96 struct trx_header_le {
97 __le32 magic; /* "HDR0" */
98 __le32 len; /* Length of file including header */
99 __le32 crc32; /* CRC from flag_version to end of file */
100 __le32 flag_version; /* 0:15 flags, 16:31 version */
101 __le32 offsets[TRX_MAX_OFFSET]; /* Offsets of partitions from start of
102 * header
106 struct rdl_state_le {
107 __le32 state;
108 __le32 bytes;
111 struct bootrom_id_le {
112 __le32 chip; /* Chip id */
113 __le32 chiprev; /* Chip rev */
114 __le32 ramsize; /* Size of RAM */
115 __le32 remapbase; /* Current remap base address */
116 __le32 boardtype; /* Type of board */
117 __le32 boardrev; /* Board revision */
120 struct brcmf_usb_image {
121 struct list_head list;
122 s8 *fwname;
123 u8 *image;
124 int image_len;
127 struct brcmf_usbdev_info {
128 struct brcmf_usbdev bus_pub; /* MUST BE FIRST */
129 spinlock_t qlock;
130 struct list_head rx_freeq;
131 struct list_head rx_postq;
132 struct list_head tx_freeq;
133 struct list_head tx_postq;
134 uint rx_pipe, tx_pipe;
136 int rx_low_watermark;
137 int tx_low_watermark;
138 int tx_high_watermark;
139 int tx_freecount;
140 bool tx_flowblock;
141 spinlock_t tx_flowblock_lock;
143 struct brcmf_usbreq *tx_reqs;
144 struct brcmf_usbreq *rx_reqs;
146 char fw_name[BRCMF_FW_NAME_LEN];
147 const u8 *image; /* buffer for combine fw and nvram */
148 int image_len;
150 struct usb_device *usbdev;
151 struct device *dev;
152 struct completion dev_init_done;
154 int ctl_in_pipe, ctl_out_pipe;
155 struct urb *ctl_urb; /* URB for control endpoint */
156 struct usb_ctrlrequest ctl_write;
157 struct usb_ctrlrequest ctl_read;
158 u32 ctl_urb_actual_length;
159 int ctl_urb_status;
160 int ctl_completed;
161 wait_queue_head_t ioctl_resp_wait;
162 ulong ctl_op;
163 u8 ifnum;
165 struct urb *bulk_urb; /* used for FW download */
167 struct brcmf_mp_device *settings;
170 static void brcmf_usb_rx_refill(struct brcmf_usbdev_info *devinfo,
171 struct brcmf_usbreq *req);
173 static struct brcmf_usbdev *brcmf_usb_get_buspub(struct device *dev)
175 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
176 return bus_if->bus_priv.usb;
179 static struct brcmf_usbdev_info *brcmf_usb_get_businfo(struct device *dev)
181 return brcmf_usb_get_buspub(dev)->devinfo;
184 static int brcmf_usb_ioctl_resp_wait(struct brcmf_usbdev_info *devinfo)
186 return wait_event_timeout(devinfo->ioctl_resp_wait,
187 devinfo->ctl_completed, IOCTL_RESP_TIMEOUT);
190 static void brcmf_usb_ioctl_resp_wake(struct brcmf_usbdev_info *devinfo)
192 wake_up(&devinfo->ioctl_resp_wait);
195 static void
196 brcmf_usb_ctl_complete(struct brcmf_usbdev_info *devinfo, int type, int status)
198 brcmf_dbg(USB, "Enter, status=%d\n", status);
200 if (unlikely(devinfo == NULL))
201 return;
203 if (type == BRCMF_USB_CBCTL_READ) {
204 if (status == 0)
205 devinfo->bus_pub.stats.rx_ctlpkts++;
206 else
207 devinfo->bus_pub.stats.rx_ctlerrs++;
208 } else if (type == BRCMF_USB_CBCTL_WRITE) {
209 if (status == 0)
210 devinfo->bus_pub.stats.tx_ctlpkts++;
211 else
212 devinfo->bus_pub.stats.tx_ctlerrs++;
215 devinfo->ctl_urb_status = status;
216 devinfo->ctl_completed = true;
217 brcmf_usb_ioctl_resp_wake(devinfo);
220 static void
221 brcmf_usb_ctlread_complete(struct urb *urb)
223 struct brcmf_usbdev_info *devinfo =
224 (struct brcmf_usbdev_info *)urb->context;
226 brcmf_dbg(USB, "Enter\n");
227 devinfo->ctl_urb_actual_length = urb->actual_length;
228 brcmf_usb_ctl_complete(devinfo, BRCMF_USB_CBCTL_READ,
229 urb->status);
232 static void
233 brcmf_usb_ctlwrite_complete(struct urb *urb)
235 struct brcmf_usbdev_info *devinfo =
236 (struct brcmf_usbdev_info *)urb->context;
238 brcmf_dbg(USB, "Enter\n");
239 brcmf_usb_ctl_complete(devinfo, BRCMF_USB_CBCTL_WRITE,
240 urb->status);
243 static int
244 brcmf_usb_send_ctl(struct brcmf_usbdev_info *devinfo, u8 *buf, int len)
246 int ret;
247 u16 size;
249 brcmf_dbg(USB, "Enter\n");
250 if (devinfo == NULL || buf == NULL ||
251 len == 0 || devinfo->ctl_urb == NULL)
252 return -EINVAL;
254 size = len;
255 devinfo->ctl_write.wLength = cpu_to_le16p(&size);
256 devinfo->ctl_urb->transfer_buffer_length = size;
257 devinfo->ctl_urb_status = 0;
258 devinfo->ctl_urb_actual_length = 0;
260 usb_fill_control_urb(devinfo->ctl_urb,
261 devinfo->usbdev,
262 devinfo->ctl_out_pipe,
263 (unsigned char *) &devinfo->ctl_write,
264 buf, size,
265 (usb_complete_t)brcmf_usb_ctlwrite_complete,
266 devinfo);
268 ret = usb_submit_urb(devinfo->ctl_urb, GFP_ATOMIC);
269 if (ret < 0)
270 brcmf_err("usb_submit_urb failed %d\n", ret);
272 return ret;
275 static int
276 brcmf_usb_recv_ctl(struct brcmf_usbdev_info *devinfo, u8 *buf, int len)
278 int ret;
279 u16 size;
281 brcmf_dbg(USB, "Enter\n");
282 if ((devinfo == NULL) || (buf == NULL) || (len == 0)
283 || (devinfo->ctl_urb == NULL))
284 return -EINVAL;
286 size = len;
287 devinfo->ctl_read.wLength = cpu_to_le16p(&size);
288 devinfo->ctl_urb->transfer_buffer_length = size;
290 devinfo->ctl_read.bRequestType = USB_DIR_IN
291 | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
292 devinfo->ctl_read.bRequest = 1;
294 usb_fill_control_urb(devinfo->ctl_urb,
295 devinfo->usbdev,
296 devinfo->ctl_in_pipe,
297 (unsigned char *) &devinfo->ctl_read,
298 buf, size,
299 (usb_complete_t)brcmf_usb_ctlread_complete,
300 devinfo);
302 ret = usb_submit_urb(devinfo->ctl_urb, GFP_ATOMIC);
303 if (ret < 0)
304 brcmf_err("usb_submit_urb failed %d\n", ret);
306 return ret;
309 static int brcmf_usb_tx_ctlpkt(struct device *dev, u8 *buf, u32 len)
311 int err = 0;
312 int timeout = 0;
313 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
314 struct usb_interface *intf = to_usb_interface(dev);
316 brcmf_dbg(USB, "Enter\n");
318 err = usb_autopm_get_interface(intf);
319 if (err)
320 goto out;
322 if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP) {
323 err = -EIO;
324 goto fail;
327 if (test_and_set_bit(0, &devinfo->ctl_op)) {
328 err = -EIO;
329 goto fail;
332 devinfo->ctl_completed = false;
333 err = brcmf_usb_send_ctl(devinfo, buf, len);
334 if (err) {
335 brcmf_err("fail %d bytes: %d\n", err, len);
336 clear_bit(0, &devinfo->ctl_op);
337 goto fail;
339 timeout = brcmf_usb_ioctl_resp_wait(devinfo);
340 if (!timeout) {
341 brcmf_err("Txctl wait timed out\n");
342 usb_kill_urb(devinfo->ctl_urb);
343 err = -EIO;
344 goto fail;
346 clear_bit(0, &devinfo->ctl_op);
348 fail:
349 usb_autopm_put_interface(intf);
350 out:
351 return err;
354 static int brcmf_usb_rx_ctlpkt(struct device *dev, u8 *buf, u32 len)
356 int err = 0;
357 int timeout = 0;
358 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
359 struct usb_interface *intf = to_usb_interface(dev);
361 brcmf_dbg(USB, "Enter\n");
363 err = usb_autopm_get_interface(intf);
364 if (err)
365 goto out;
367 if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP) {
368 err = -EIO;
369 goto fail;
372 if (test_and_set_bit(0, &devinfo->ctl_op)) {
373 err = -EIO;
374 goto fail;
377 devinfo->ctl_completed = false;
378 err = brcmf_usb_recv_ctl(devinfo, buf, len);
379 if (err) {
380 brcmf_err("fail %d bytes: %d\n", err, len);
381 clear_bit(0, &devinfo->ctl_op);
382 goto fail;
384 timeout = brcmf_usb_ioctl_resp_wait(devinfo);
385 err = devinfo->ctl_urb_status;
386 if (!timeout) {
387 brcmf_err("rxctl wait timed out\n");
388 usb_kill_urb(devinfo->ctl_urb);
389 err = -EIO;
390 goto fail;
392 clear_bit(0, &devinfo->ctl_op);
393 fail:
394 usb_autopm_put_interface(intf);
395 if (!err)
396 return devinfo->ctl_urb_actual_length;
397 out:
398 return err;
401 static struct brcmf_usbreq *brcmf_usb_deq(struct brcmf_usbdev_info *devinfo,
402 struct list_head *q, int *counter)
404 unsigned long flags;
405 struct brcmf_usbreq *req;
406 spin_lock_irqsave(&devinfo->qlock, flags);
407 if (list_empty(q)) {
408 spin_unlock_irqrestore(&devinfo->qlock, flags);
409 return NULL;
411 req = list_entry(q->next, struct brcmf_usbreq, list);
412 list_del_init(q->next);
413 if (counter)
414 (*counter)--;
415 spin_unlock_irqrestore(&devinfo->qlock, flags);
416 return req;
420 static void brcmf_usb_enq(struct brcmf_usbdev_info *devinfo,
421 struct list_head *q, struct brcmf_usbreq *req,
422 int *counter)
424 unsigned long flags;
425 spin_lock_irqsave(&devinfo->qlock, flags);
426 list_add_tail(&req->list, q);
427 if (counter)
428 (*counter)++;
429 spin_unlock_irqrestore(&devinfo->qlock, flags);
432 static struct brcmf_usbreq *
433 brcmf_usbdev_qinit(struct list_head *q, int qsize)
435 int i;
436 struct brcmf_usbreq *req, *reqs;
438 reqs = kcalloc(qsize, sizeof(struct brcmf_usbreq), GFP_ATOMIC);
439 if (reqs == NULL)
440 return NULL;
442 req = reqs;
444 for (i = 0; i < qsize; i++) {
445 req->urb = usb_alloc_urb(0, GFP_ATOMIC);
446 if (!req->urb)
447 goto fail;
449 INIT_LIST_HEAD(&req->list);
450 list_add_tail(&req->list, q);
451 req++;
453 return reqs;
454 fail:
455 brcmf_err("fail!\n");
456 while (!list_empty(q)) {
457 req = list_entry(q->next, struct brcmf_usbreq, list);
458 if (req)
459 usb_free_urb(req->urb);
460 list_del(q->next);
462 kfree(reqs);
463 return NULL;
467 static void brcmf_usb_free_q(struct list_head *q)
469 struct brcmf_usbreq *req, *next;
471 list_for_each_entry_safe(req, next, q, list) {
472 if (!req->urb) {
473 brcmf_err("bad req\n");
474 break;
476 usb_free_urb(req->urb);
477 list_del_init(&req->list);
481 static void brcmf_usb_del_fromq(struct brcmf_usbdev_info *devinfo,
482 struct brcmf_usbreq *req)
484 unsigned long flags;
486 spin_lock_irqsave(&devinfo->qlock, flags);
487 list_del_init(&req->list);
488 spin_unlock_irqrestore(&devinfo->qlock, flags);
492 static void brcmf_usb_tx_complete(struct urb *urb)
494 struct brcmf_usbreq *req = (struct brcmf_usbreq *)urb->context;
495 struct brcmf_usbdev_info *devinfo = req->devinfo;
496 unsigned long flags;
498 brcmf_dbg(USB, "Enter, urb->status=%d, skb=%p\n", urb->status,
499 req->skb);
500 brcmf_usb_del_fromq(devinfo, req);
502 brcmf_proto_bcdc_txcomplete(devinfo->dev, req->skb, urb->status == 0);
503 req->skb = NULL;
504 brcmf_usb_enq(devinfo, &devinfo->tx_freeq, req, &devinfo->tx_freecount);
505 spin_lock_irqsave(&devinfo->tx_flowblock_lock, flags);
506 if (devinfo->tx_freecount > devinfo->tx_high_watermark &&
507 devinfo->tx_flowblock) {
508 brcmf_proto_bcdc_txflowblock(devinfo->dev, false);
509 devinfo->tx_flowblock = false;
511 spin_unlock_irqrestore(&devinfo->tx_flowblock_lock, flags);
514 static void brcmf_usb_rx_complete(struct urb *urb)
516 struct brcmf_usbreq *req = (struct brcmf_usbreq *)urb->context;
517 struct brcmf_usbdev_info *devinfo = req->devinfo;
518 struct sk_buff *skb;
520 brcmf_dbg(USB, "Enter, urb->status=%d\n", urb->status);
521 brcmf_usb_del_fromq(devinfo, req);
522 skb = req->skb;
523 req->skb = NULL;
525 /* zero length packets indicate usb "failure". Do not refill */
526 if (urb->status != 0 || !urb->actual_length) {
527 brcmu_pkt_buf_free_skb(skb);
528 brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
529 return;
532 if (devinfo->bus_pub.state == BRCMFMAC_USB_STATE_UP ||
533 devinfo->bus_pub.state == BRCMFMAC_USB_STATE_SLEEP) {
534 skb_put(skb, urb->actual_length);
535 brcmf_rx_frame(devinfo->dev, skb, true, true);
536 brcmf_usb_rx_refill(devinfo, req);
537 usb_mark_last_busy(urb->dev);
538 } else {
539 brcmu_pkt_buf_free_skb(skb);
540 brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
542 return;
546 static void brcmf_usb_rx_refill(struct brcmf_usbdev_info *devinfo,
547 struct brcmf_usbreq *req)
549 struct sk_buff *skb;
550 int ret;
552 if (!req || !devinfo)
553 return;
555 skb = dev_alloc_skb(devinfo->bus_pub.bus_mtu);
556 if (!skb) {
557 brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
558 return;
560 req->skb = skb;
562 usb_fill_bulk_urb(req->urb, devinfo->usbdev, devinfo->rx_pipe,
563 skb->data, skb_tailroom(skb), brcmf_usb_rx_complete,
564 req);
565 req->devinfo = devinfo;
566 brcmf_usb_enq(devinfo, &devinfo->rx_postq, req, NULL);
568 ret = usb_submit_urb(req->urb, GFP_ATOMIC);
569 if (ret) {
570 brcmf_usb_del_fromq(devinfo, req);
571 brcmu_pkt_buf_free_skb(req->skb);
572 req->skb = NULL;
573 brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
575 return;
578 static void brcmf_usb_rx_fill_all(struct brcmf_usbdev_info *devinfo)
580 struct brcmf_usbreq *req;
582 if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP) {
583 brcmf_err("bus is not up=%d\n", devinfo->bus_pub.state);
584 return;
586 while ((req = brcmf_usb_deq(devinfo, &devinfo->rx_freeq, NULL)) != NULL)
587 brcmf_usb_rx_refill(devinfo, req);
590 static void
591 brcmf_usb_state_change(struct brcmf_usbdev_info *devinfo, int state)
593 struct brcmf_bus *bcmf_bus = devinfo->bus_pub.bus;
595 brcmf_dbg(USB, "Enter, current state=%d, new state=%d\n",
596 devinfo->bus_pub.state, state);
598 if (devinfo->bus_pub.state == state)
599 return;
601 devinfo->bus_pub.state = state;
603 /* update state of upper layer */
604 if (state == BRCMFMAC_USB_STATE_DOWN) {
605 brcmf_dbg(USB, "DBUS is down\n");
606 brcmf_bus_change_state(bcmf_bus, BRCMF_BUS_DOWN);
607 } else if (state == BRCMFMAC_USB_STATE_UP) {
608 brcmf_dbg(USB, "DBUS is up\n");
609 brcmf_bus_change_state(bcmf_bus, BRCMF_BUS_UP);
610 } else {
611 brcmf_dbg(USB, "DBUS current state=%d\n", state);
615 static int brcmf_usb_tx(struct device *dev, struct sk_buff *skb)
617 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
618 struct brcmf_usbreq *req;
619 int ret;
620 unsigned long flags;
621 struct usb_interface *intf = to_usb_interface(dev);
623 ret = usb_autopm_get_interface(intf);
624 if (ret)
625 goto out;
627 brcmf_dbg(USB, "Enter, skb=%p\n", skb);
628 if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP) {
629 ret = -EIO;
630 goto fail;
633 req = brcmf_usb_deq(devinfo, &devinfo->tx_freeq,
634 &devinfo->tx_freecount);
635 if (!req) {
636 brcmf_err("no req to send\n");
637 ret = -ENOMEM;
638 goto fail;
641 req->skb = skb;
642 req->devinfo = devinfo;
643 usb_fill_bulk_urb(req->urb, devinfo->usbdev, devinfo->tx_pipe,
644 skb->data, skb->len, brcmf_usb_tx_complete, req);
645 req->urb->transfer_flags |= URB_ZERO_PACKET;
646 brcmf_usb_enq(devinfo, &devinfo->tx_postq, req, NULL);
647 ret = usb_submit_urb(req->urb, GFP_ATOMIC);
648 if (ret) {
649 brcmf_err("brcmf_usb_tx usb_submit_urb FAILED\n");
650 brcmf_usb_del_fromq(devinfo, req);
651 req->skb = NULL;
652 brcmf_usb_enq(devinfo, &devinfo->tx_freeq, req,
653 &devinfo->tx_freecount);
654 goto fail;
657 spin_lock_irqsave(&devinfo->tx_flowblock_lock, flags);
658 if (devinfo->tx_freecount < devinfo->tx_low_watermark &&
659 !devinfo->tx_flowblock) {
660 brcmf_proto_bcdc_txflowblock(dev, true);
661 devinfo->tx_flowblock = true;
663 spin_unlock_irqrestore(&devinfo->tx_flowblock_lock, flags);
665 fail:
666 usb_autopm_put_interface(intf);
667 out:
668 return ret;
672 static int brcmf_usb_up(struct device *dev)
674 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
676 brcmf_dbg(USB, "Enter\n");
677 if (devinfo->bus_pub.state == BRCMFMAC_USB_STATE_UP)
678 return 0;
680 /* Success, indicate devinfo is fully up */
681 brcmf_usb_state_change(devinfo, BRCMFMAC_USB_STATE_UP);
683 if (devinfo->ctl_urb) {
684 devinfo->ctl_in_pipe = usb_rcvctrlpipe(devinfo->usbdev, 0);
685 devinfo->ctl_out_pipe = usb_sndctrlpipe(devinfo->usbdev, 0);
687 /* CTL Write */
688 devinfo->ctl_write.bRequestType =
689 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
690 devinfo->ctl_write.bRequest = 0;
691 devinfo->ctl_write.wValue = cpu_to_le16(0);
692 devinfo->ctl_write.wIndex = cpu_to_le16(devinfo->ifnum);
694 /* CTL Read */
695 devinfo->ctl_read.bRequestType =
696 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
697 devinfo->ctl_read.bRequest = 1;
698 devinfo->ctl_read.wValue = cpu_to_le16(0);
699 devinfo->ctl_read.wIndex = cpu_to_le16(devinfo->ifnum);
701 brcmf_usb_rx_fill_all(devinfo);
702 return 0;
705 static void brcmf_cancel_all_urbs(struct brcmf_usbdev_info *devinfo)
707 int i;
709 if (devinfo->ctl_urb)
710 usb_kill_urb(devinfo->ctl_urb);
711 if (devinfo->bulk_urb)
712 usb_kill_urb(devinfo->bulk_urb);
713 if (devinfo->tx_reqs)
714 for (i = 0; i < devinfo->bus_pub.ntxq; i++)
715 usb_kill_urb(devinfo->tx_reqs[i].urb);
716 if (devinfo->rx_reqs)
717 for (i = 0; i < devinfo->bus_pub.nrxq; i++)
718 usb_kill_urb(devinfo->rx_reqs[i].urb);
721 static void brcmf_usb_down(struct device *dev)
723 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
725 brcmf_dbg(USB, "Enter\n");
726 if (devinfo == NULL)
727 return;
729 if (devinfo->bus_pub.state == BRCMFMAC_USB_STATE_DOWN)
730 return;
732 brcmf_usb_state_change(devinfo, BRCMFMAC_USB_STATE_DOWN);
734 brcmf_cancel_all_urbs(devinfo);
737 static void
738 brcmf_usb_sync_complete(struct urb *urb)
740 struct brcmf_usbdev_info *devinfo =
741 (struct brcmf_usbdev_info *)urb->context;
743 devinfo->ctl_completed = true;
744 brcmf_usb_ioctl_resp_wake(devinfo);
747 static int brcmf_usb_dl_cmd(struct brcmf_usbdev_info *devinfo, u8 cmd,
748 void *buffer, int buflen)
750 int ret;
751 char *tmpbuf;
752 u16 size;
754 if ((!devinfo) || (devinfo->ctl_urb == NULL))
755 return -EINVAL;
757 tmpbuf = kmalloc(buflen, GFP_ATOMIC);
758 if (!tmpbuf)
759 return -ENOMEM;
761 size = buflen;
762 devinfo->ctl_urb->transfer_buffer_length = size;
764 devinfo->ctl_read.wLength = cpu_to_le16p(&size);
765 devinfo->ctl_read.bRequestType = USB_DIR_IN | USB_TYPE_VENDOR |
766 USB_RECIP_INTERFACE;
767 devinfo->ctl_read.bRequest = cmd;
769 usb_fill_control_urb(devinfo->ctl_urb,
770 devinfo->usbdev,
771 usb_rcvctrlpipe(devinfo->usbdev, 0),
772 (unsigned char *) &devinfo->ctl_read,
773 (void *) tmpbuf, size,
774 (usb_complete_t)brcmf_usb_sync_complete, devinfo);
776 devinfo->ctl_completed = false;
777 ret = usb_submit_urb(devinfo->ctl_urb, GFP_ATOMIC);
778 if (ret < 0) {
779 brcmf_err("usb_submit_urb failed %d\n", ret);
780 goto finalize;
783 if (!brcmf_usb_ioctl_resp_wait(devinfo)) {
784 usb_kill_urb(devinfo->ctl_urb);
785 ret = -ETIMEDOUT;
786 } else {
787 memcpy(buffer, tmpbuf, buflen);
790 finalize:
791 kfree(tmpbuf);
792 return ret;
795 static bool
796 brcmf_usb_dlneeded(struct brcmf_usbdev_info *devinfo)
798 struct bootrom_id_le id;
799 u32 chipid, chiprev;
801 brcmf_dbg(USB, "Enter\n");
803 if (devinfo == NULL)
804 return false;
806 /* Check if firmware downloaded already by querying runtime ID */
807 id.chip = cpu_to_le32(0xDEAD);
808 brcmf_usb_dl_cmd(devinfo, DL_GETVER, &id, sizeof(id));
810 chipid = le32_to_cpu(id.chip);
811 chiprev = le32_to_cpu(id.chiprev);
813 if ((chipid & 0x4300) == 0x4300)
814 brcmf_dbg(USB, "chip %x rev 0x%x\n", chipid, chiprev);
815 else
816 brcmf_dbg(USB, "chip %d rev 0x%x\n", chipid, chiprev);
817 if (chipid == BRCMF_POSTBOOT_ID) {
818 brcmf_dbg(USB, "firmware already downloaded\n");
819 brcmf_usb_dl_cmd(devinfo, DL_RESETCFG, &id, sizeof(id));
820 return false;
821 } else {
822 devinfo->bus_pub.devid = chipid;
823 devinfo->bus_pub.chiprev = chiprev;
825 return true;
828 static int
829 brcmf_usb_resetcfg(struct brcmf_usbdev_info *devinfo)
831 struct bootrom_id_le id;
832 u32 loop_cnt;
833 int err;
835 brcmf_dbg(USB, "Enter\n");
837 loop_cnt = 0;
838 do {
839 mdelay(BRCMF_USB_RESET_GETVER_SPINWAIT);
840 loop_cnt++;
841 id.chip = cpu_to_le32(0xDEAD); /* Get the ID */
842 err = brcmf_usb_dl_cmd(devinfo, DL_GETVER, &id, sizeof(id));
843 if ((err) && (err != -ETIMEDOUT))
844 return err;
845 if (id.chip == cpu_to_le32(BRCMF_POSTBOOT_ID))
846 break;
847 } while (loop_cnt < BRCMF_USB_RESET_GETVER_LOOP_CNT);
849 if (id.chip == cpu_to_le32(BRCMF_POSTBOOT_ID)) {
850 brcmf_dbg(USB, "postboot chip 0x%x/rev 0x%x\n",
851 le32_to_cpu(id.chip), le32_to_cpu(id.chiprev));
853 brcmf_usb_dl_cmd(devinfo, DL_RESETCFG, &id, sizeof(id));
854 return 0;
855 } else {
856 brcmf_err("Cannot talk to Dongle. Firmware is not UP, %d ms\n",
857 BRCMF_USB_RESET_GETVER_SPINWAIT * loop_cnt);
858 return -EINVAL;
863 static int
864 brcmf_usb_dl_send_bulk(struct brcmf_usbdev_info *devinfo, void *buffer, int len)
866 int ret;
868 if ((devinfo == NULL) || (devinfo->bulk_urb == NULL))
869 return -EINVAL;
871 /* Prepare the URB */
872 usb_fill_bulk_urb(devinfo->bulk_urb, devinfo->usbdev,
873 devinfo->tx_pipe, buffer, len,
874 (usb_complete_t)brcmf_usb_sync_complete, devinfo);
876 devinfo->bulk_urb->transfer_flags |= URB_ZERO_PACKET;
878 devinfo->ctl_completed = false;
879 ret = usb_submit_urb(devinfo->bulk_urb, GFP_ATOMIC);
880 if (ret) {
881 brcmf_err("usb_submit_urb failed %d\n", ret);
882 return ret;
884 ret = brcmf_usb_ioctl_resp_wait(devinfo);
885 return (ret == 0);
888 static int
889 brcmf_usb_dl_writeimage(struct brcmf_usbdev_info *devinfo, u8 *fw, int fwlen)
891 unsigned int sendlen, sent, dllen;
892 char *bulkchunk = NULL, *dlpos;
893 struct rdl_state_le state;
894 u32 rdlstate, rdlbytes;
895 int err = 0;
897 brcmf_dbg(USB, "Enter, fw %p, len %d\n", fw, fwlen);
899 bulkchunk = kmalloc(TRX_RDL_CHUNK, GFP_ATOMIC);
900 if (bulkchunk == NULL) {
901 err = -ENOMEM;
902 goto fail;
905 /* 1) Prepare USB boot loader for runtime image */
906 brcmf_usb_dl_cmd(devinfo, DL_START, &state, sizeof(state));
908 rdlstate = le32_to_cpu(state.state);
909 rdlbytes = le32_to_cpu(state.bytes);
911 /* 2) Check we are in the Waiting state */
912 if (rdlstate != DL_WAITING) {
913 brcmf_err("Failed to DL_START\n");
914 err = -EINVAL;
915 goto fail;
917 sent = 0;
918 dlpos = fw;
919 dllen = fwlen;
921 /* Get chip id and rev */
922 while (rdlbytes != dllen) {
923 /* Wait until the usb device reports it received all
924 * the bytes we sent */
925 if ((rdlbytes == sent) && (rdlbytes != dllen)) {
926 if ((dllen-sent) < TRX_RDL_CHUNK)
927 sendlen = dllen-sent;
928 else
929 sendlen = TRX_RDL_CHUNK;
931 /* simply avoid having to send a ZLP by ensuring we
932 * never have an even
933 * multiple of 64
935 if (!(sendlen % 64))
936 sendlen -= 4;
938 /* send data */
939 memcpy(bulkchunk, dlpos, sendlen);
940 if (brcmf_usb_dl_send_bulk(devinfo, bulkchunk,
941 sendlen)) {
942 brcmf_err("send_bulk failed\n");
943 err = -EINVAL;
944 goto fail;
947 dlpos += sendlen;
948 sent += sendlen;
950 err = brcmf_usb_dl_cmd(devinfo, DL_GETSTATE, &state,
951 sizeof(state));
952 if (err) {
953 brcmf_err("DL_GETSTATE Failed\n");
954 goto fail;
957 rdlstate = le32_to_cpu(state.state);
958 rdlbytes = le32_to_cpu(state.bytes);
960 /* restart if an error is reported */
961 if (rdlstate == DL_BAD_HDR || rdlstate == DL_BAD_CRC) {
962 brcmf_err("Bad Hdr or Bad CRC state %d\n",
963 rdlstate);
964 err = -EINVAL;
965 goto fail;
969 fail:
970 kfree(bulkchunk);
971 brcmf_dbg(USB, "Exit, err=%d\n", err);
972 return err;
975 static int brcmf_usb_dlstart(struct brcmf_usbdev_info *devinfo, u8 *fw, int len)
977 int err;
979 brcmf_dbg(USB, "Enter\n");
981 if (devinfo == NULL)
982 return -EINVAL;
984 if (devinfo->bus_pub.devid == 0xDEAD)
985 return -EINVAL;
987 err = brcmf_usb_dl_writeimage(devinfo, fw, len);
988 if (err == 0)
989 devinfo->bus_pub.state = BRCMFMAC_USB_STATE_DL_DONE;
990 else
991 devinfo->bus_pub.state = BRCMFMAC_USB_STATE_DL_FAIL;
992 brcmf_dbg(USB, "Exit, err=%d\n", err);
994 return err;
997 static int brcmf_usb_dlrun(struct brcmf_usbdev_info *devinfo)
999 struct rdl_state_le state;
1001 brcmf_dbg(USB, "Enter\n");
1002 if (!devinfo)
1003 return -EINVAL;
1005 if (devinfo->bus_pub.devid == 0xDEAD)
1006 return -EINVAL;
1008 /* Check we are runnable */
1009 state.state = 0;
1010 brcmf_usb_dl_cmd(devinfo, DL_GETSTATE, &state, sizeof(state));
1012 /* Start the image */
1013 if (state.state == cpu_to_le32(DL_RUNNABLE)) {
1014 if (brcmf_usb_dl_cmd(devinfo, DL_GO, &state, sizeof(state)))
1015 return -ENODEV;
1016 if (brcmf_usb_resetcfg(devinfo))
1017 return -ENODEV;
1018 /* The Dongle may go for re-enumeration. */
1019 } else {
1020 brcmf_err("Dongle not runnable\n");
1021 return -EINVAL;
1023 brcmf_dbg(USB, "Exit\n");
1024 return 0;
1027 static int
1028 brcmf_usb_fw_download(struct brcmf_usbdev_info *devinfo)
1030 int err;
1031 struct usb_interface *intf;
1033 brcmf_dbg(USB, "Enter\n");
1034 if (!devinfo) {
1035 err = -ENODEV;
1036 goto out;
1039 if (!devinfo->image) {
1040 brcmf_err("No firmware!\n");
1041 err = -ENOENT;
1042 goto out;
1045 intf = to_usb_interface(devinfo->dev);
1046 err = usb_autopm_get_interface(intf);
1047 if (err)
1048 goto out;
1050 err = brcmf_usb_dlstart(devinfo,
1051 (u8 *)devinfo->image, devinfo->image_len);
1052 if (err == 0)
1053 err = brcmf_usb_dlrun(devinfo);
1055 usb_autopm_put_interface(intf);
1056 out:
1057 return err;
1061 static void brcmf_usb_detach(struct brcmf_usbdev_info *devinfo)
1063 brcmf_dbg(USB, "Enter, devinfo %p\n", devinfo);
1065 /* free the URBS */
1066 brcmf_usb_free_q(&devinfo->rx_freeq);
1067 brcmf_usb_free_q(&devinfo->tx_freeq);
1069 usb_free_urb(devinfo->ctl_urb);
1070 usb_free_urb(devinfo->bulk_urb);
1072 kfree(devinfo->tx_reqs);
1073 kfree(devinfo->rx_reqs);
1075 if (devinfo->settings)
1076 brcmf_release_module_param(devinfo->settings);
1080 static int check_file(const u8 *headers)
1082 struct trx_header_le *trx;
1083 int actual_len = -1;
1085 brcmf_dbg(USB, "Enter\n");
1086 /* Extract trx header */
1087 trx = (struct trx_header_le *) headers;
1088 if (trx->magic != cpu_to_le32(TRX_MAGIC))
1089 return -1;
1091 headers += sizeof(struct trx_header_le);
1093 if (le32_to_cpu(trx->flag_version) & TRX_UNCOMP_IMAGE) {
1094 actual_len = le32_to_cpu(trx->offsets[TRX_OFFSETS_DLFWLEN_IDX]);
1095 return actual_len + sizeof(struct trx_header_le);
1097 return -1;
1101 static
1102 struct brcmf_usbdev *brcmf_usb_attach(struct brcmf_usbdev_info *devinfo,
1103 int nrxq, int ntxq)
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)
1135 goto error;
1137 devinfo->tx_reqs = brcmf_usbdev_qinit(&devinfo->tx_freeq, ntxq);
1138 if (!devinfo->tx_reqs)
1139 goto error;
1140 devinfo->tx_freecount = ntxq;
1142 devinfo->ctl_urb = usb_alloc_urb(0, GFP_ATOMIC);
1143 if (!devinfo->ctl_urb)
1144 goto error;
1145 devinfo->bulk_urb = usb_alloc_urb(0, GFP_ATOMIC);
1146 if (!devinfo->bulk_urb)
1147 goto error;
1149 return &devinfo->bus_pub;
1151 error:
1152 brcmf_err("failed!\n");
1153 brcmf_usb_detach(devinfo);
1154 return NULL;
1157 static
1158 int brcmf_usb_get_fwname(struct device *dev, const char *ext, u8 *fw_name)
1160 struct brcmf_bus *bus = dev_get_drvdata(dev);
1161 struct brcmf_fw_request *fwreq;
1162 struct brcmf_fw_name fwnames[] = {
1163 { ext, fw_name },
1166 fwreq = brcmf_fw_alloc_request(bus->chip, bus->chiprev,
1167 brcmf_usb_fwnames,
1168 ARRAY_SIZE(brcmf_usb_fwnames),
1169 fwnames, ARRAY_SIZE(fwnames));
1170 if (!fwreq)
1171 return -ENOMEM;
1173 kfree(fwreq);
1174 return 0;
1177 static const struct brcmf_bus_ops brcmf_usb_bus_ops = {
1178 .preinit = brcmf_usb_up,
1179 .stop = brcmf_usb_down,
1180 .txdata = brcmf_usb_tx,
1181 .txctl = brcmf_usb_tx_ctlpkt,
1182 .rxctl = brcmf_usb_rx_ctlpkt,
1183 .get_fwname = brcmf_usb_get_fwname,
1186 #define BRCMF_USB_FW_CODE 0
1188 static void brcmf_usb_probe_phase2(struct device *dev, int ret,
1189 struct brcmf_fw_request *fwreq)
1191 struct brcmf_bus *bus = dev_get_drvdata(dev);
1192 struct brcmf_usbdev_info *devinfo = bus->bus_priv.usb->devinfo;
1193 const struct firmware *fw;
1195 if (ret)
1196 goto error;
1198 brcmf_dbg(USB, "Start fw downloading\n");
1200 fw = fwreq->items[BRCMF_USB_FW_CODE].binary;
1201 kfree(fwreq);
1203 ret = check_file(fw->data);
1204 if (ret < 0) {
1205 brcmf_err("invalid firmware\n");
1206 release_firmware(fw);
1207 goto error;
1210 devinfo->image = fw->data;
1211 devinfo->image_len = fw->size;
1213 ret = brcmf_usb_fw_download(devinfo);
1214 release_firmware(fw);
1215 if (ret)
1216 goto error;
1218 ret = brcmf_alloc(devinfo->dev, devinfo->settings);
1219 if (ret)
1220 goto error;
1222 /* Attach to the common driver interface */
1223 ret = brcmf_attach(devinfo->dev);
1224 if (ret)
1225 goto error;
1227 complete(&devinfo->dev_init_done);
1228 return;
1229 error:
1230 brcmf_dbg(TRACE, "failed: dev=%s, err=%d\n", dev_name(dev), ret);
1231 complete(&devinfo->dev_init_done);
1232 device_release_driver(dev);
1235 static struct brcmf_fw_request *
1236 brcmf_usb_prepare_fw_request(struct brcmf_usbdev_info *devinfo)
1238 struct brcmf_fw_request *fwreq;
1239 struct brcmf_fw_name fwnames[] = {
1240 { ".bin", devinfo->fw_name },
1243 fwreq = brcmf_fw_alloc_request(devinfo->bus_pub.devid,
1244 devinfo->bus_pub.chiprev,
1245 brcmf_usb_fwnames,
1246 ARRAY_SIZE(brcmf_usb_fwnames),
1247 fwnames, ARRAY_SIZE(fwnames));
1248 if (!fwreq)
1249 return NULL;
1251 fwreq->items[BRCMF_USB_FW_CODE].type = BRCMF_FW_TYPE_BINARY;
1253 return fwreq;
1256 static int brcmf_usb_probe_cb(struct brcmf_usbdev_info *devinfo)
1258 struct brcmf_bus *bus = NULL;
1259 struct brcmf_usbdev *bus_pub = NULL;
1260 struct device *dev = devinfo->dev;
1261 struct brcmf_fw_request *fwreq;
1262 int ret;
1264 brcmf_dbg(USB, "Enter\n");
1265 bus_pub = brcmf_usb_attach(devinfo, BRCMF_USB_NRXQ, BRCMF_USB_NTXQ);
1266 if (!bus_pub)
1267 return -ENODEV;
1269 bus = kzalloc(sizeof(struct brcmf_bus), GFP_ATOMIC);
1270 if (!bus) {
1271 ret = -ENOMEM;
1272 goto fail;
1275 bus->dev = dev;
1276 bus_pub->bus = bus;
1277 bus->bus_priv.usb = bus_pub;
1278 dev_set_drvdata(dev, bus);
1279 bus->ops = &brcmf_usb_bus_ops;
1280 bus->proto_type = BRCMF_PROTO_BCDC;
1281 bus->always_use_fws_queue = true;
1282 #ifdef CONFIG_PM
1283 bus->wowl_supported = true;
1284 #endif
1286 devinfo->settings = brcmf_get_module_param(bus->dev, BRCMF_BUSTYPE_USB,
1287 bus_pub->devid,
1288 bus_pub->chiprev);
1289 if (!devinfo->settings) {
1290 ret = -ENOMEM;
1291 goto fail;
1294 if (!brcmf_usb_dlneeded(devinfo)) {
1295 ret = brcmf_alloc(devinfo->dev, devinfo->settings);
1296 if (ret)
1297 goto fail;
1298 ret = brcmf_attach(devinfo->dev);
1299 if (ret)
1300 goto fail;
1301 /* we are done */
1302 complete(&devinfo->dev_init_done);
1303 return 0;
1305 bus->chip = bus_pub->devid;
1306 bus->chiprev = bus_pub->chiprev;
1308 fwreq = brcmf_usb_prepare_fw_request(devinfo);
1309 if (!fwreq) {
1310 ret = -ENOMEM;
1311 goto fail;
1314 /* request firmware here */
1315 ret = brcmf_fw_get_firmwares(dev, fwreq, brcmf_usb_probe_phase2);
1316 if (ret) {
1317 brcmf_err("firmware request failed: %d\n", ret);
1318 kfree(fwreq);
1319 goto fail;
1322 return 0;
1324 fail:
1325 /* Release resources in reverse order */
1326 brcmf_free(devinfo->dev);
1327 kfree(bus);
1328 brcmf_usb_detach(devinfo);
1329 return ret;
1332 static void
1333 brcmf_usb_disconnect_cb(struct brcmf_usbdev_info *devinfo)
1335 if (!devinfo)
1336 return;
1337 brcmf_dbg(USB, "Enter, bus_pub %p\n", devinfo);
1339 brcmf_detach(devinfo->dev);
1340 brcmf_free(devinfo->dev);
1341 kfree(devinfo->bus_pub.bus);
1342 brcmf_usb_detach(devinfo);
1345 static int
1346 brcmf_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
1348 struct usb_device *usb = interface_to_usbdev(intf);
1349 struct brcmf_usbdev_info *devinfo;
1350 struct usb_interface_descriptor *desc;
1351 struct usb_endpoint_descriptor *endpoint;
1352 int ret = 0;
1353 u32 num_of_eps;
1354 u8 endpoint_num, ep;
1356 brcmf_dbg(USB, "Enter 0x%04x:0x%04x\n", id->idVendor, id->idProduct);
1358 devinfo = kzalloc(sizeof(*devinfo), GFP_ATOMIC);
1359 if (devinfo == NULL)
1360 return -ENOMEM;
1362 devinfo->usbdev = usb;
1363 devinfo->dev = &usb->dev;
1364 /* Init completion, to protect for disconnect while still loading.
1365 * Necessary because of the asynchronous firmware load construction
1367 init_completion(&devinfo->dev_init_done);
1369 usb_set_intfdata(intf, devinfo);
1371 intf->needs_remote_wakeup = 1;
1373 /* Check that the device supports only one configuration */
1374 if (usb->descriptor.bNumConfigurations != 1) {
1375 brcmf_err("Number of configurations: %d not supported\n",
1376 usb->descriptor.bNumConfigurations);
1377 ret = -ENODEV;
1378 goto fail;
1381 if ((usb->descriptor.bDeviceClass != USB_CLASS_VENDOR_SPEC) &&
1382 (usb->descriptor.bDeviceClass != USB_CLASS_MISC) &&
1383 (usb->descriptor.bDeviceClass != USB_CLASS_WIRELESS_CONTROLLER)) {
1384 brcmf_err("Device class: 0x%x not supported\n",
1385 usb->descriptor.bDeviceClass);
1386 ret = -ENODEV;
1387 goto fail;
1390 desc = &intf->cur_altsetting->desc;
1391 if ((desc->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
1392 (desc->bInterfaceSubClass != 2) ||
1393 (desc->bInterfaceProtocol != 0xff)) {
1394 brcmf_err("non WLAN interface %d: 0x%x:0x%x:0x%x\n",
1395 desc->bInterfaceNumber, desc->bInterfaceClass,
1396 desc->bInterfaceSubClass, desc->bInterfaceProtocol);
1397 ret = -ENODEV;
1398 goto fail;
1401 num_of_eps = desc->bNumEndpoints;
1402 for (ep = 0; ep < num_of_eps; ep++) {
1403 endpoint = &intf->cur_altsetting->endpoint[ep].desc;
1404 endpoint_num = usb_endpoint_num(endpoint);
1405 if (!usb_endpoint_xfer_bulk(endpoint))
1406 continue;
1407 if (usb_endpoint_dir_in(endpoint)) {
1408 if (!devinfo->rx_pipe)
1409 devinfo->rx_pipe =
1410 usb_rcvbulkpipe(usb, endpoint_num);
1411 } else {
1412 if (!devinfo->tx_pipe)
1413 devinfo->tx_pipe =
1414 usb_sndbulkpipe(usb, endpoint_num);
1417 if (devinfo->rx_pipe == 0) {
1418 brcmf_err("No RX (in) Bulk EP found\n");
1419 ret = -ENODEV;
1420 goto fail;
1422 if (devinfo->tx_pipe == 0) {
1423 brcmf_err("No TX (out) Bulk EP found\n");
1424 ret = -ENODEV;
1425 goto fail;
1428 devinfo->ifnum = desc->bInterfaceNumber;
1430 if (usb->speed == USB_SPEED_SUPER_PLUS)
1431 brcmf_dbg(USB, "Broadcom super speed plus USB WLAN interface detected\n");
1432 else if (usb->speed == USB_SPEED_SUPER)
1433 brcmf_dbg(USB, "Broadcom super speed USB WLAN interface detected\n");
1434 else if (usb->speed == USB_SPEED_HIGH)
1435 brcmf_dbg(USB, "Broadcom high speed USB WLAN interface detected\n");
1436 else
1437 brcmf_dbg(USB, "Broadcom full speed USB WLAN interface detected\n");
1439 ret = brcmf_usb_probe_cb(devinfo);
1440 if (ret)
1441 goto fail;
1443 /* Success */
1444 return 0;
1446 fail:
1447 complete(&devinfo->dev_init_done);
1448 kfree(devinfo);
1449 usb_set_intfdata(intf, NULL);
1450 return ret;
1453 static void
1454 brcmf_usb_disconnect(struct usb_interface *intf)
1456 struct brcmf_usbdev_info *devinfo;
1458 brcmf_dbg(USB, "Enter\n");
1459 devinfo = (struct brcmf_usbdev_info *)usb_get_intfdata(intf);
1461 if (devinfo) {
1462 wait_for_completion(&devinfo->dev_init_done);
1463 /* Make sure that devinfo still exists. Firmware probe routines
1464 * may have released the device and cleared the intfdata.
1466 if (!usb_get_intfdata(intf))
1467 goto done;
1469 brcmf_usb_disconnect_cb(devinfo);
1470 kfree(devinfo);
1472 done:
1473 brcmf_dbg(USB, "Exit\n");
1477 * only need to signal the bus being down and update the state.
1479 static int brcmf_usb_suspend(struct usb_interface *intf, pm_message_t state)
1481 struct usb_device *usb = interface_to_usbdev(intf);
1482 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(&usb->dev);
1484 brcmf_dbg(USB, "Enter\n");
1485 devinfo->bus_pub.state = BRCMFMAC_USB_STATE_SLEEP;
1486 brcmf_cancel_all_urbs(devinfo);
1487 device_set_wakeup_enable(devinfo->dev, true);
1488 return 0;
1492 * (re-) start the bus.
1494 static int brcmf_usb_resume(struct usb_interface *intf)
1496 struct usb_device *usb = interface_to_usbdev(intf);
1497 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(&usb->dev);
1499 brcmf_dbg(USB, "Enter\n");
1501 devinfo->bus_pub.state = BRCMFMAC_USB_STATE_UP;
1502 brcmf_usb_rx_fill_all(devinfo);
1503 device_set_wakeup_enable(devinfo->dev, false);
1504 return 0;
1507 static int brcmf_usb_reset_resume(struct usb_interface *intf)
1509 struct usb_device *usb = interface_to_usbdev(intf);
1510 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(&usb->dev);
1511 struct brcmf_fw_request *fwreq;
1512 int ret;
1514 brcmf_dbg(USB, "Enter\n");
1516 fwreq = brcmf_usb_prepare_fw_request(devinfo);
1517 if (!fwreq)
1518 return -ENOMEM;
1520 ret = brcmf_fw_get_firmwares(&usb->dev, fwreq, brcmf_usb_probe_phase2);
1521 if (ret < 0)
1522 kfree(fwreq);
1524 return ret;
1527 #define BRCMF_USB_DEVICE(dev_id) \
1528 { USB_DEVICE(BRCM_USB_VENDOR_ID_BROADCOM, dev_id) }
1530 #define LINKSYS_USB_DEVICE(dev_id) \
1531 { USB_DEVICE(BRCM_USB_VENDOR_ID_LINKSYS, dev_id) }
1533 #define CYPRESS_USB_DEVICE(dev_id) \
1534 { USB_DEVICE(CY_USB_VENDOR_ID_CYPRESS, dev_id) }
1536 static const struct usb_device_id brcmf_usb_devid_table[] = {
1537 BRCMF_USB_DEVICE(BRCM_USB_43143_DEVICE_ID),
1538 BRCMF_USB_DEVICE(BRCM_USB_43236_DEVICE_ID),
1539 BRCMF_USB_DEVICE(BRCM_USB_43242_DEVICE_ID),
1540 BRCMF_USB_DEVICE(BRCM_USB_43569_DEVICE_ID),
1541 LINKSYS_USB_DEVICE(BRCM_USB_43235_LINKSYS_DEVICE_ID),
1542 CYPRESS_USB_DEVICE(CY_USB_4373_DEVICE_ID),
1543 { USB_DEVICE(BRCM_USB_VENDOR_ID_LG, BRCM_USB_43242_LG_DEVICE_ID) },
1544 /* special entry for device with firmware loaded and running */
1545 BRCMF_USB_DEVICE(BRCM_USB_BCMFW_DEVICE_ID),
1546 CYPRESS_USB_DEVICE(BRCM_USB_BCMFW_DEVICE_ID),
1547 { /* end: all zeroes */ }
1550 MODULE_DEVICE_TABLE(usb, brcmf_usb_devid_table);
1552 static struct usb_driver brcmf_usbdrvr = {
1553 .name = KBUILD_MODNAME,
1554 .probe = brcmf_usb_probe,
1555 .disconnect = brcmf_usb_disconnect,
1556 .id_table = brcmf_usb_devid_table,
1557 .suspend = brcmf_usb_suspend,
1558 .resume = brcmf_usb_resume,
1559 .reset_resume = brcmf_usb_reset_resume,
1560 .supports_autosuspend = true,
1561 .disable_hub_initiated_lpm = 1,
1564 static int brcmf_usb_reset_device(struct device *dev, void *notused)
1566 /* device past is the usb interface so we
1567 * need to use parent here.
1569 brcmf_dev_reset(dev->parent);
1570 return 0;
1573 void brcmf_usb_exit(void)
1575 struct device_driver *drv = &brcmf_usbdrvr.drvwrap.driver;
1576 int ret;
1578 brcmf_dbg(USB, "Enter\n");
1579 ret = driver_for_each_device(drv, NULL, NULL,
1580 brcmf_usb_reset_device);
1581 if (ret)
1582 brcmf_err("failed to reset all usb devices %d\n", ret);
1584 usb_deregister(&brcmf_usbdrvr);
1587 void brcmf_usb_register(void)
1589 int ret;
1591 brcmf_dbg(USB, "Enter\n");
1592 ret = usb_register(&brcmf_usbdrvr);
1593 if (ret)
1594 brcmf_err("usb_register failed %d\n", ret);