GNU Linux-libre 4.9.306-gnu1
[releases.git] / drivers / net / wireless / broadcom / brcm80211 / brcmfmac / usb.c
1 /*
2  * Copyright (c) 2011 Broadcom Corporation
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/firmware.h>
20 #include <linux/usb.h>
21 #include <linux/vmalloc.h>
22
23 #include <brcmu_utils.h>
24 #include <brcm_hw_ids.h>
25 #include <brcmu_wifi.h>
26 #include "bus.h"
27 #include "debug.h"
28 #include "firmware.h"
29 #include "usb.h"
30 #include "core.h"
31 #include "common.h"
32
33
34 #define IOCTL_RESP_TIMEOUT              msecs_to_jiffies(2000)
35
36 #define BRCMF_USB_RESET_GETVER_SPINWAIT 100     /* in unit of ms */
37 #define BRCMF_USB_RESET_GETVER_LOOP_CNT 10
38
39 #define BRCMF_POSTBOOT_ID               0xA123  /* ID to detect if dongle
40                                                    has boot up */
41 #define BRCMF_USB_NRXQ                  50
42 #define BRCMF_USB_NTXQ                  50
43
44 #define BRCMF_USB_CBCTL_WRITE           0
45 #define BRCMF_USB_CBCTL_READ            1
46 #define BRCMF_USB_MAX_PKT_SIZE          1600
47
48 BRCMF_FW_DEF(43143, "/*(DEBLOBBED)*/");
49 BRCMF_FW_DEF(43236B, "/*(DEBLOBBED)*/");
50 BRCMF_FW_DEF(43242A, "/*(DEBLOBBED)*/");
51 BRCMF_FW_DEF(43569, "/*(DEBLOBBED)*/");
52
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)
61 };
62
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
68
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
79                                  * clear this event
80                                  */
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
84                                  */
85 #define DL_DEFER_RESP_OK 9      /* Potentially defer the response to setup
86                                  * if resp unavailable
87                                  */
88
89 /* states */
90 #define DL_WAITING      0       /* waiting to rx first pkt */
91 #define DL_READY        1       /* hdr was good, waiting for more of the
92                                  * compressed image
93                                  */
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
99                                  * value
100                                  */
101 #define DL_IMAGE_TOOBIG 7       /* firmware image too big */
102
103
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
110                                          * header
111                                          */
112 };
113
114 struct rdl_state_le {
115         __le32 state;
116         __le32 bytes;
117 };
118
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 */
126 };
127
128 struct brcmf_usb_image {
129         struct list_head list;
130         s8 *fwname;
131         u8 *image;
132         int image_len;
133 };
134
135 struct brcmf_usbdev_info {
136         struct brcmf_usbdev bus_pub; /* MUST BE FIRST */
137         spinlock_t qlock;
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;
143
144         int rx_low_watermark;
145         int tx_low_watermark;
146         int tx_high_watermark;
147         int tx_freecount;
148         bool tx_flowblock;
149         spinlock_t tx_flowblock_lock;
150
151         struct brcmf_usbreq *tx_reqs;
152         struct brcmf_usbreq *rx_reqs;
153
154         char fw_name[BRCMF_FW_NAME_LEN];
155         const u8 *image;        /* buffer for combine fw and nvram */
156         int image_len;
157
158         struct usb_device *usbdev;
159         struct device *dev;
160         struct completion dev_init_done;
161
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;
167         int ctl_urb_status;
168         int ctl_completed;
169         wait_queue_head_t ioctl_resp_wait;
170         ulong ctl_op;
171         u8 ifnum;
172
173         struct urb *bulk_urb; /* used for FW download */
174
175         bool wowl_enabled;
176         struct brcmf_mp_device *settings;
177 };
178
179 static void brcmf_usb_rx_refill(struct brcmf_usbdev_info *devinfo,
180                                 struct brcmf_usbreq  *req);
181
182 static struct brcmf_usbdev *brcmf_usb_get_buspub(struct device *dev)
183 {
184         struct brcmf_bus *bus_if = dev_get_drvdata(dev);
185         return bus_if->bus_priv.usb;
186 }
187
188 static struct brcmf_usbdev_info *brcmf_usb_get_businfo(struct device *dev)
189 {
190         return brcmf_usb_get_buspub(dev)->devinfo;
191 }
192
193 static int brcmf_usb_ioctl_resp_wait(struct brcmf_usbdev_info *devinfo)
194 {
195         return wait_event_timeout(devinfo->ioctl_resp_wait,
196                                   devinfo->ctl_completed, IOCTL_RESP_TIMEOUT);
197 }
198
199 static void brcmf_usb_ioctl_resp_wake(struct brcmf_usbdev_info *devinfo)
200 {
201         wake_up(&devinfo->ioctl_resp_wait);
202 }
203
204 static void
205 brcmf_usb_ctl_complete(struct brcmf_usbdev_info *devinfo, int type, int status)
206 {
207         brcmf_dbg(USB, "Enter, status=%d\n", status);
208
209         if (unlikely(devinfo == NULL))
210                 return;
211
212         if (type == BRCMF_USB_CBCTL_READ) {
213                 if (status == 0)
214                         devinfo->bus_pub.stats.rx_ctlpkts++;
215                 else
216                         devinfo->bus_pub.stats.rx_ctlerrs++;
217         } else if (type == BRCMF_USB_CBCTL_WRITE) {
218                 if (status == 0)
219                         devinfo->bus_pub.stats.tx_ctlpkts++;
220                 else
221                         devinfo->bus_pub.stats.tx_ctlerrs++;
222         }
223
224         devinfo->ctl_urb_status = status;
225         devinfo->ctl_completed = true;
226         brcmf_usb_ioctl_resp_wake(devinfo);
227 }
228
229 static void
230 brcmf_usb_ctlread_complete(struct urb *urb)
231 {
232         struct brcmf_usbdev_info *devinfo =
233                 (struct brcmf_usbdev_info *)urb->context;
234
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,
238                 urb->status);
239 }
240
241 static void
242 brcmf_usb_ctlwrite_complete(struct urb *urb)
243 {
244         struct brcmf_usbdev_info *devinfo =
245                 (struct brcmf_usbdev_info *)urb->context;
246
247         brcmf_dbg(USB, "Enter\n");
248         brcmf_usb_ctl_complete(devinfo, BRCMF_USB_CBCTL_WRITE,
249                 urb->status);
250 }
251
252 static int
253 brcmf_usb_send_ctl(struct brcmf_usbdev_info *devinfo, u8 *buf, int len)
254 {
255         int ret;
256         u16 size;
257
258         brcmf_dbg(USB, "Enter\n");
259         if (devinfo == NULL || buf == NULL ||
260             len == 0 || devinfo->ctl_urb == NULL)
261                 return -EINVAL;
262
263         size = len;
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;
268
269         usb_fill_control_urb(devinfo->ctl_urb,
270                 devinfo->usbdev,
271                 devinfo->ctl_out_pipe,
272                 (unsigned char *) &devinfo->ctl_write,
273                 buf, size,
274                 (usb_complete_t)brcmf_usb_ctlwrite_complete,
275                 devinfo);
276
277         ret = usb_submit_urb(devinfo->ctl_urb, GFP_ATOMIC);
278         if (ret < 0)
279                 brcmf_err("usb_submit_urb failed %d\n", ret);
280
281         return ret;
282 }
283
284 static int
285 brcmf_usb_recv_ctl(struct brcmf_usbdev_info *devinfo, u8 *buf, int len)
286 {
287         int ret;
288         u16 size;
289
290         brcmf_dbg(USB, "Enter\n");
291         if ((devinfo == NULL) || (buf == NULL) || (len == 0)
292                 || (devinfo->ctl_urb == NULL))
293                 return -EINVAL;
294
295         size = len;
296         devinfo->ctl_read.wLength = cpu_to_le16p(&size);
297         devinfo->ctl_urb->transfer_buffer_length = size;
298
299         devinfo->ctl_read.bRequestType = USB_DIR_IN
300                 | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
301         devinfo->ctl_read.bRequest = 1;
302
303         usb_fill_control_urb(devinfo->ctl_urb,
304                 devinfo->usbdev,
305                 devinfo->ctl_in_pipe,
306                 (unsigned char *) &devinfo->ctl_read,
307                 buf, size,
308                 (usb_complete_t)brcmf_usb_ctlread_complete,
309                 devinfo);
310
311         ret = usb_submit_urb(devinfo->ctl_urb, GFP_ATOMIC);
312         if (ret < 0)
313                 brcmf_err("usb_submit_urb failed %d\n", ret);
314
315         return ret;
316 }
317
318 static int brcmf_usb_tx_ctlpkt(struct device *dev, u8 *buf, u32 len)
319 {
320         int err = 0;
321         int timeout = 0;
322         struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
323
324         brcmf_dbg(USB, "Enter\n");
325         if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP)
326                 return -EIO;
327
328         if (test_and_set_bit(0, &devinfo->ctl_op))
329                 return -EIO;
330
331         devinfo->ctl_completed = false;
332         err = brcmf_usb_send_ctl(devinfo, buf, len);
333         if (err) {
334                 brcmf_err("fail %d bytes: %d\n", err, len);
335                 clear_bit(0, &devinfo->ctl_op);
336                 return err;
337         }
338         timeout = brcmf_usb_ioctl_resp_wait(devinfo);
339         clear_bit(0, &devinfo->ctl_op);
340         if (!timeout) {
341                 brcmf_err("Txctl wait timed out\n");
342                 err = -EIO;
343         }
344         return err;
345 }
346
347 static int brcmf_usb_rx_ctlpkt(struct device *dev, u8 *buf, u32 len)
348 {
349         int err = 0;
350         int timeout = 0;
351         struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
352
353         brcmf_dbg(USB, "Enter\n");
354         if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP)
355                 return -EIO;
356
357         if (test_and_set_bit(0, &devinfo->ctl_op))
358                 return -EIO;
359
360         devinfo->ctl_completed = false;
361         err = brcmf_usb_recv_ctl(devinfo, buf, len);
362         if (err) {
363                 brcmf_err("fail %d bytes: %d\n", err, len);
364                 clear_bit(0, &devinfo->ctl_op);
365                 return err;
366         }
367         timeout = brcmf_usb_ioctl_resp_wait(devinfo);
368         err = devinfo->ctl_urb_status;
369         clear_bit(0, &devinfo->ctl_op);
370         if (!timeout) {
371                 brcmf_err("rxctl wait timed out\n");
372                 err = -EIO;
373         }
374         if (!err)
375                 return devinfo->ctl_urb_actual_length;
376         else
377                 return err;
378 }
379
380 static struct brcmf_usbreq *brcmf_usb_deq(struct brcmf_usbdev_info *devinfo,
381                                           struct list_head *q, int *counter)
382 {
383         unsigned long flags;
384         struct brcmf_usbreq  *req;
385         spin_lock_irqsave(&devinfo->qlock, flags);
386         if (list_empty(q)) {
387                 spin_unlock_irqrestore(&devinfo->qlock, flags);
388                 return NULL;
389         }
390         req = list_entry(q->next, struct brcmf_usbreq, list);
391         list_del_init(q->next);
392         if (counter)
393                 (*counter)--;
394         spin_unlock_irqrestore(&devinfo->qlock, flags);
395         return req;
396
397 }
398
399 static void brcmf_usb_enq(struct brcmf_usbdev_info *devinfo,
400                           struct list_head *q, struct brcmf_usbreq *req,
401                           int *counter)
402 {
403         unsigned long flags;
404         spin_lock_irqsave(&devinfo->qlock, flags);
405         list_add_tail(&req->list, q);
406         if (counter)
407                 (*counter)++;
408         spin_unlock_irqrestore(&devinfo->qlock, flags);
409 }
410
411 static struct brcmf_usbreq *
412 brcmf_usbdev_qinit(struct list_head *q, int qsize)
413 {
414         int i;
415         struct brcmf_usbreq *req, *reqs;
416
417         reqs = kcalloc(qsize, sizeof(struct brcmf_usbreq), GFP_ATOMIC);
418         if (reqs == NULL)
419                 return NULL;
420
421         req = reqs;
422
423         for (i = 0; i < qsize; i++) {
424                 req->urb = usb_alloc_urb(0, GFP_ATOMIC);
425                 if (!req->urb)
426                         goto fail;
427
428                 INIT_LIST_HEAD(&req->list);
429                 list_add_tail(&req->list, q);
430                 req++;
431         }
432         return reqs;
433 fail:
434         brcmf_err("fail!\n");
435         while (!list_empty(q)) {
436                 req = list_entry(q->next, struct brcmf_usbreq, list);
437                 if (req)
438                         usb_free_urb(req->urb);
439                 list_del(q->next);
440         }
441         kfree(reqs);
442         return NULL;
443
444 }
445
446 static void brcmf_usb_free_q(struct list_head *q, bool pending)
447 {
448         struct brcmf_usbreq *req, *next;
449         int i = 0;
450         list_for_each_entry_safe(req, next, q, list) {
451                 if (!req->urb) {
452                         brcmf_err("bad req\n");
453                         break;
454                 }
455                 i++;
456                 if (pending) {
457                         usb_kill_urb(req->urb);
458                 } else {
459                         usb_free_urb(req->urb);
460                         list_del_init(&req->list);
461                 }
462         }
463 }
464
465 static void brcmf_usb_del_fromq(struct brcmf_usbdev_info *devinfo,
466                                 struct brcmf_usbreq *req)
467 {
468         unsigned long flags;
469
470         spin_lock_irqsave(&devinfo->qlock, flags);
471         list_del_init(&req->list);
472         spin_unlock_irqrestore(&devinfo->qlock, flags);
473 }
474
475
476 static void brcmf_usb_tx_complete(struct urb *urb)
477 {
478         struct brcmf_usbreq *req = (struct brcmf_usbreq *)urb->context;
479         struct brcmf_usbdev_info *devinfo = req->devinfo;
480         unsigned long flags;
481
482         brcmf_dbg(USB, "Enter, urb->status=%d, skb=%p\n", urb->status,
483                   req->skb);
484         brcmf_usb_del_fromq(devinfo, req);
485
486         brcmf_txcomplete(devinfo->dev, req->skb, urb->status == 0);
487         req->skb = NULL;
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;
494         }
495         spin_unlock_irqrestore(&devinfo->tx_flowblock_lock, flags);
496 }
497
498 static void brcmf_usb_rx_complete(struct urb *urb)
499 {
500         struct brcmf_usbreq  *req = (struct brcmf_usbreq *)urb->context;
501         struct brcmf_usbdev_info *devinfo = req->devinfo;
502         struct sk_buff *skb;
503
504         brcmf_dbg(USB, "Enter, urb->status=%d\n", urb->status);
505         brcmf_usb_del_fromq(devinfo, req);
506         skb = req->skb;
507         req->skb = NULL;
508
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);
513                 return;
514         }
515
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);
520         } else {
521                 brcmu_pkt_buf_free_skb(skb);
522                 brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
523         }
524         return;
525
526 }
527
528 static void brcmf_usb_rx_refill(struct brcmf_usbdev_info *devinfo,
529                                 struct brcmf_usbreq  *req)
530 {
531         struct sk_buff *skb;
532         int ret;
533
534         if (!req || !devinfo)
535                 return;
536
537         skb = dev_alloc_skb(devinfo->bus_pub.bus_mtu);
538         if (!skb) {
539                 brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
540                 return;
541         }
542         req->skb = skb;
543
544         usb_fill_bulk_urb(req->urb, devinfo->usbdev, devinfo->rx_pipe,
545                           skb->data, skb_tailroom(skb), brcmf_usb_rx_complete,
546                           req);
547         req->devinfo = devinfo;
548         brcmf_usb_enq(devinfo, &devinfo->rx_postq, req, NULL);
549
550         ret = usb_submit_urb(req->urb, GFP_ATOMIC);
551         if (ret) {
552                 brcmf_usb_del_fromq(devinfo, req);
553                 brcmu_pkt_buf_free_skb(req->skb);
554                 req->skb = NULL;
555                 brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
556         }
557         return;
558 }
559
560 static void brcmf_usb_rx_fill_all(struct brcmf_usbdev_info *devinfo)
561 {
562         struct brcmf_usbreq *req;
563
564         if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP) {
565                 brcmf_err("bus is not up=%d\n", devinfo->bus_pub.state);
566                 return;
567         }
568         while ((req = brcmf_usb_deq(devinfo, &devinfo->rx_freeq, NULL)) != NULL)
569                 brcmf_usb_rx_refill(devinfo, req);
570 }
571
572 static void
573 brcmf_usb_state_change(struct brcmf_usbdev_info *devinfo, int state)
574 {
575         struct brcmf_bus *bcmf_bus = devinfo->bus_pub.bus;
576         int old_state;
577
578         brcmf_dbg(USB, "Enter, current state=%d, new state=%d\n",
579                   devinfo->bus_pub.state, state);
580
581         if (devinfo->bus_pub.state == state)
582                 return;
583
584         old_state = devinfo->bus_pub.state;
585         devinfo->bus_pub.state = state;
586
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);
594         } else {
595                 brcmf_dbg(USB, "DBUS current state=%d\n", state);
596         }
597 }
598
599 static int brcmf_usb_tx(struct device *dev, struct sk_buff *skb)
600 {
601         struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
602         struct brcmf_usbreq  *req;
603         int ret;
604         unsigned long flags;
605
606         brcmf_dbg(USB, "Enter, skb=%p\n", skb);
607         if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP) {
608                 ret = -EIO;
609                 goto fail;
610         }
611
612         req = brcmf_usb_deq(devinfo, &devinfo->tx_freeq,
613                                         &devinfo->tx_freecount);
614         if (!req) {
615                 brcmf_err("no req to send\n");
616                 ret = -ENOMEM;
617                 goto fail;
618         }
619
620         req->skb = skb;
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);
627         if (ret) {
628                 brcmf_err("brcmf_usb_tx usb_submit_urb FAILED\n");
629                 brcmf_usb_del_fromq(devinfo, req);
630                 req->skb = NULL;
631                 brcmf_usb_enq(devinfo, &devinfo->tx_freeq, req,
632                               &devinfo->tx_freecount);
633                 goto fail;
634         }
635
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;
641         }
642         spin_unlock_irqrestore(&devinfo->tx_flowblock_lock, flags);
643         return 0;
644
645 fail:
646         return ret;
647 }
648
649
650 static int brcmf_usb_up(struct device *dev)
651 {
652         struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
653
654         brcmf_dbg(USB, "Enter\n");
655         if (devinfo->bus_pub.state == BRCMFMAC_USB_STATE_UP)
656                 return 0;
657
658         /* Success, indicate devinfo is fully up */
659         brcmf_usb_state_change(devinfo, BRCMFMAC_USB_STATE_UP);
660
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);
664
665                 /* CTL Write */
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);
671
672                 /* CTL Read */
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);
678         }
679         brcmf_usb_rx_fill_all(devinfo);
680         return 0;
681 }
682
683 static void brcmf_cancel_all_urbs(struct brcmf_usbdev_info *devinfo)
684 {
685         int i;
686
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);
697 }
698
699 static void brcmf_usb_down(struct device *dev)
700 {
701         struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
702
703         brcmf_dbg(USB, "Enter\n");
704         if (devinfo == NULL)
705                 return;
706
707         if (devinfo->bus_pub.state == BRCMFMAC_USB_STATE_DOWN)
708                 return;
709
710         brcmf_usb_state_change(devinfo, BRCMFMAC_USB_STATE_DOWN);
711
712         brcmf_cancel_all_urbs(devinfo);
713 }
714
715 static void
716 brcmf_usb_sync_complete(struct urb *urb)
717 {
718         struct brcmf_usbdev_info *devinfo =
719                         (struct brcmf_usbdev_info *)urb->context;
720
721         devinfo->ctl_completed = true;
722         brcmf_usb_ioctl_resp_wake(devinfo);
723 }
724
725 static int brcmf_usb_dl_cmd(struct brcmf_usbdev_info *devinfo, u8 cmd,
726                             void *buffer, int buflen)
727 {
728         int ret;
729         char *tmpbuf;
730         u16 size;
731
732         if ((!devinfo) || (devinfo->ctl_urb == NULL))
733                 return -EINVAL;
734
735         tmpbuf = kmalloc(buflen, GFP_ATOMIC);
736         if (!tmpbuf)
737                 return -ENOMEM;
738
739         size = buflen;
740         devinfo->ctl_urb->transfer_buffer_length = size;
741
742         devinfo->ctl_read.wLength = cpu_to_le16p(&size);
743         devinfo->ctl_read.bRequestType = USB_DIR_IN | USB_TYPE_VENDOR |
744                 USB_RECIP_INTERFACE;
745         devinfo->ctl_read.bRequest = cmd;
746
747         usb_fill_control_urb(devinfo->ctl_urb,
748                 devinfo->usbdev,
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);
753
754         devinfo->ctl_completed = false;
755         ret = usb_submit_urb(devinfo->ctl_urb, GFP_ATOMIC);
756         if (ret < 0) {
757                 brcmf_err("usb_submit_urb failed %d\n", ret);
758                 goto finalize;
759         }
760
761         if (!brcmf_usb_ioctl_resp_wait(devinfo)) {
762                 usb_kill_urb(devinfo->ctl_urb);
763                 ret = -ETIMEDOUT;
764         } else {
765                 memcpy(buffer, tmpbuf, buflen);
766         }
767
768 finalize:
769         kfree(tmpbuf);
770         return ret;
771 }
772
773 static bool
774 brcmf_usb_dlneeded(struct brcmf_usbdev_info *devinfo)
775 {
776         struct bootrom_id_le id;
777         u32 chipid, chiprev;
778
779         brcmf_dbg(USB, "Enter\n");
780
781         if (devinfo == NULL)
782                 return false;
783
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));
787
788         chipid = le32_to_cpu(id.chip);
789         chiprev = le32_to_cpu(id.chiprev);
790
791         if ((chipid & 0x4300) == 0x4300)
792                 brcmf_dbg(USB, "chip %x rev 0x%x\n", chipid, chiprev);
793         else
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));
798                 return false;
799         } else {
800                 devinfo->bus_pub.devid = chipid;
801                 devinfo->bus_pub.chiprev = chiprev;
802         }
803         return true;
804 }
805
806 static int
807 brcmf_usb_resetcfg(struct brcmf_usbdev_info *devinfo)
808 {
809         struct bootrom_id_le id;
810         u32 loop_cnt;
811         int err;
812
813         brcmf_dbg(USB, "Enter\n");
814
815         loop_cnt = 0;
816         do {
817                 mdelay(BRCMF_USB_RESET_GETVER_SPINWAIT);
818                 loop_cnt++;
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))
822                         return err;
823                 if (id.chip == cpu_to_le32(BRCMF_POSTBOOT_ID))
824                         break;
825         } while (loop_cnt < BRCMF_USB_RESET_GETVER_LOOP_CNT);
826
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));
830
831                 brcmf_usb_dl_cmd(devinfo, DL_RESETCFG, &id, sizeof(id));
832                 return 0;
833         } else {
834                 brcmf_err("Cannot talk to Dongle. Firmware is not UP, %d ms\n",
835                           BRCMF_USB_RESET_GETVER_SPINWAIT * loop_cnt);
836                 return -EINVAL;
837         }
838 }
839
840
841 static int
842 brcmf_usb_dl_send_bulk(struct brcmf_usbdev_info *devinfo, void *buffer, int len)
843 {
844         int ret;
845
846         if ((devinfo == NULL) || (devinfo->bulk_urb == NULL))
847                 return -EINVAL;
848
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);
853
854         devinfo->bulk_urb->transfer_flags |= URB_ZERO_PACKET;
855
856         devinfo->ctl_completed = false;
857         ret = usb_submit_urb(devinfo->bulk_urb, GFP_ATOMIC);
858         if (ret) {
859                 brcmf_err("usb_submit_urb failed %d\n", ret);
860                 return ret;
861         }
862         ret = brcmf_usb_ioctl_resp_wait(devinfo);
863         return (ret == 0);
864 }
865
866 static int
867 brcmf_usb_dl_writeimage(struct brcmf_usbdev_info *devinfo, u8 *fw, int fwlen)
868 {
869         unsigned int sendlen, sent, dllen;
870         char *bulkchunk = NULL, *dlpos;
871         struct rdl_state_le state;
872         u32 rdlstate, rdlbytes;
873         int err = 0;
874
875         brcmf_dbg(USB, "Enter, fw %p, len %d\n", fw, fwlen);
876
877         bulkchunk = kmalloc(TRX_RDL_CHUNK, GFP_ATOMIC);
878         if (bulkchunk == NULL) {
879                 err = -ENOMEM;
880                 goto fail;
881         }
882
883         /* 1) Prepare USB boot loader for runtime image */
884         brcmf_usb_dl_cmd(devinfo, DL_START, &state, sizeof(state));
885
886         rdlstate = le32_to_cpu(state.state);
887         rdlbytes = le32_to_cpu(state.bytes);
888
889         /* 2) Check we are in the Waiting state */
890         if (rdlstate != DL_WAITING) {
891                 brcmf_err("Failed to DL_START\n");
892                 err = -EINVAL;
893                 goto fail;
894         }
895         sent = 0;
896         dlpos = fw;
897         dllen = fwlen;
898
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;
906                         else
907                                 sendlen = TRX_RDL_CHUNK;
908
909                         /* simply avoid having to send a ZLP by ensuring we
910                          * never have an even
911                          * multiple of 64
912                          */
913                         if (!(sendlen % 64))
914                                 sendlen -= 4;
915
916                         /* send data */
917                         memcpy(bulkchunk, dlpos, sendlen);
918                         if (brcmf_usb_dl_send_bulk(devinfo, bulkchunk,
919                                                    sendlen)) {
920                                 brcmf_err("send_bulk failed\n");
921                                 err = -EINVAL;
922                                 goto fail;
923                         }
924
925                         dlpos += sendlen;
926                         sent += sendlen;
927                 }
928                 err = brcmf_usb_dl_cmd(devinfo, DL_GETSTATE, &state,
929                                        sizeof(state));
930                 if (err) {
931                         brcmf_err("DL_GETSTATE Failed\n");
932                         goto fail;
933                 }
934
935                 rdlstate = le32_to_cpu(state.state);
936                 rdlbytes = le32_to_cpu(state.bytes);
937
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",
941                                   rdlstate);
942                         err = -EINVAL;
943                         goto fail;
944                 }
945         }
946
947 fail:
948         kfree(bulkchunk);
949         brcmf_dbg(USB, "Exit, err=%d\n", err);
950         return err;
951 }
952
953 static int brcmf_usb_dlstart(struct brcmf_usbdev_info *devinfo, u8 *fw, int len)
954 {
955         int err;
956
957         brcmf_dbg(USB, "Enter\n");
958
959         if (devinfo == NULL)
960                 return -EINVAL;
961
962         if (devinfo->bus_pub.devid == 0xDEAD)
963                 return -EINVAL;
964
965         err = brcmf_usb_dl_writeimage(devinfo, fw, len);
966         if (err == 0)
967                 devinfo->bus_pub.state = BRCMFMAC_USB_STATE_DL_DONE;
968         else
969                 devinfo->bus_pub.state = BRCMFMAC_USB_STATE_DL_FAIL;
970         brcmf_dbg(USB, "Exit, err=%d\n", err);
971
972         return err;
973 }
974
975 static int brcmf_usb_dlrun(struct brcmf_usbdev_info *devinfo)
976 {
977         struct rdl_state_le state;
978
979         brcmf_dbg(USB, "Enter\n");
980         if (!devinfo)
981                 return -EINVAL;
982
983         if (devinfo->bus_pub.devid == 0xDEAD)
984                 return -EINVAL;
985
986         /* Check we are runnable */
987         state.state = 0;
988         brcmf_usb_dl_cmd(devinfo, DL_GETSTATE, &state, sizeof(state));
989
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)))
993                         return -ENODEV;
994                 if (brcmf_usb_resetcfg(devinfo))
995                         return -ENODEV;
996                 /* The Dongle may go for re-enumeration. */
997         } else {
998                 brcmf_err("Dongle not runnable\n");
999                 return -EINVAL;
1000         }
1001         brcmf_dbg(USB, "Exit\n");
1002         return 0;
1003 }
1004
1005 static int
1006 brcmf_usb_fw_download(struct brcmf_usbdev_info *devinfo)
1007 {
1008         int err;
1009
1010         brcmf_dbg(USB, "Enter\n");
1011         if (devinfo == NULL)
1012                 return -ENODEV;
1013
1014         if (!devinfo->image) {
1015                 brcmf_err("No firmware!\n");
1016                 return -ENOENT;
1017         }
1018
1019         err = brcmf_usb_dlstart(devinfo,
1020                 (u8 *)devinfo->image, devinfo->image_len);
1021         if (err == 0)
1022                 err = brcmf_usb_dlrun(devinfo);
1023         return err;
1024 }
1025
1026
1027 static void brcmf_usb_detach(struct brcmf_usbdev_info *devinfo)
1028 {
1029         brcmf_dbg(USB, "Enter, devinfo %p\n", devinfo);
1030
1031         /* free the URBS */
1032         brcmf_usb_free_q(&devinfo->rx_freeq, false);
1033         brcmf_usb_free_q(&devinfo->tx_freeq, false);
1034
1035         usb_free_urb(devinfo->ctl_urb);
1036         usb_free_urb(devinfo->bulk_urb);
1037
1038         kfree(devinfo->tx_reqs);
1039         kfree(devinfo->rx_reqs);
1040
1041         if (devinfo->settings)
1042                 brcmf_release_module_param(devinfo->settings);
1043 }
1044
1045
1046 static int check_file(const u8 *headers)
1047 {
1048         struct trx_header_le *trx;
1049         int actual_len = -1;
1050
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))
1055                 return -1;
1056
1057         headers += sizeof(struct trx_header_le);
1058
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);
1062         }
1063         return -1;
1064 }
1065
1066
1067 static
1068 struct brcmf_usbdev *brcmf_usb_attach(struct brcmf_usbdev_info *devinfo,
1069                                       int nrxq, int ntxq)
1070 {
1071         brcmf_dbg(USB, "Enter\n");
1072
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;
1078
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;
1083
1084         /* Initialize other structure content */
1085         init_waitqueue_head(&devinfo->ioctl_resp_wait);
1086
1087         /* Initialize the spinlocks */
1088         spin_lock_init(&devinfo->qlock);
1089         spin_lock_init(&devinfo->tx_flowblock_lock);
1090
1091         INIT_LIST_HEAD(&devinfo->rx_freeq);
1092         INIT_LIST_HEAD(&devinfo->rx_postq);
1093
1094         INIT_LIST_HEAD(&devinfo->tx_freeq);
1095         INIT_LIST_HEAD(&devinfo->tx_postq);
1096
1097         devinfo->tx_flowblock = false;
1098
1099         devinfo->rx_reqs = brcmf_usbdev_qinit(&devinfo->rx_freeq, nrxq);
1100         if (!devinfo->rx_reqs)
1101                 goto error;
1102
1103         devinfo->tx_reqs = brcmf_usbdev_qinit(&devinfo->tx_freeq, ntxq);
1104         if (!devinfo->tx_reqs)
1105                 goto error;
1106         devinfo->tx_freecount = ntxq;
1107
1108         devinfo->ctl_urb = usb_alloc_urb(0, GFP_ATOMIC);
1109         if (!devinfo->ctl_urb)
1110                 goto error;
1111         devinfo->bulk_urb = usb_alloc_urb(0, GFP_ATOMIC);
1112         if (!devinfo->bulk_urb)
1113                 goto error;
1114
1115         return &devinfo->bus_pub;
1116
1117 error:
1118         brcmf_err("failed!\n");
1119         brcmf_usb_detach(devinfo);
1120         return NULL;
1121 }
1122
1123 static void brcmf_usb_wowl_config(struct device *dev, bool enabled)
1124 {
1125         struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
1126
1127         brcmf_dbg(USB, "Configuring WOWL, enabled=%d\n", enabled);
1128         devinfo->wowl_enabled = enabled;
1129         if (enabled)
1130                 device_set_wakeup_enable(devinfo->dev, true);
1131         else
1132                 device_set_wakeup_enable(devinfo->dev, false);
1133 }
1134
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,
1141 };
1142
1143 static int brcmf_usb_bus_setup(struct brcmf_usbdev_info *devinfo)
1144 {
1145         int ret;
1146
1147         /* Attach to the common driver interface */
1148         ret = brcmf_attach(devinfo->dev, devinfo->settings);
1149         if (ret) {
1150                 brcmf_err("brcmf_attach failed\n");
1151                 return ret;
1152         }
1153
1154         ret = brcmf_usb_up(devinfo->dev);
1155         if (ret)
1156                 goto fail;
1157
1158         ret = brcmf_bus_start(devinfo->dev);
1159         if (ret)
1160                 goto fail;
1161
1162         return 0;
1163 fail:
1164         brcmf_detach(devinfo->dev);
1165         return ret;
1166 }
1167
1168 static void brcmf_usb_probe_phase2(struct device *dev, int ret,
1169                                    const struct firmware *fw,
1170                                    void *nvram, u32 nvlen)
1171 {
1172         struct brcmf_bus *bus = dev_get_drvdata(dev);
1173         struct brcmf_usbdev_info *devinfo = bus->bus_priv.usb->devinfo;
1174
1175         if (ret)
1176                 goto error;
1177
1178         brcmf_dbg(USB, "Start fw downloading\n");
1179
1180         ret = check_file(fw->data);
1181         if (ret < 0) {
1182                 brcmf_err("invalid firmware\n");
1183                 release_firmware(fw);
1184                 goto error;
1185         }
1186
1187         devinfo->image = fw->data;
1188         devinfo->image_len = fw->size;
1189
1190         ret = brcmf_usb_fw_download(devinfo);
1191         release_firmware(fw);
1192         if (ret)
1193                 goto error;
1194
1195         ret = brcmf_usb_bus_setup(devinfo);
1196         if (ret)
1197                 goto error;
1198
1199         complete(&devinfo->dev_init_done);
1200         return;
1201 error:
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);
1205 }
1206
1207 static int brcmf_usb_probe_cb(struct brcmf_usbdev_info *devinfo)
1208 {
1209         struct brcmf_bus *bus = NULL;
1210         struct brcmf_usbdev *bus_pub = NULL;
1211         struct device *dev = devinfo->dev;
1212         int ret;
1213
1214         brcmf_dbg(USB, "Enter\n");
1215         bus_pub = brcmf_usb_attach(devinfo, BRCMF_USB_NRXQ, BRCMF_USB_NTXQ);
1216         if (!bus_pub)
1217                 return -ENODEV;
1218
1219         bus = kzalloc(sizeof(struct brcmf_bus), GFP_ATOMIC);
1220         if (!bus) {
1221                 ret = -ENOMEM;
1222                 goto fail;
1223         }
1224
1225         bus->dev = dev;
1226         bus_pub->bus = bus;
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;
1232 #ifdef CONFIG_PM
1233         bus->wowl_supported = true;
1234 #endif
1235
1236         devinfo->settings = brcmf_get_module_param(bus->dev, BRCMF_BUSTYPE_USB,
1237                                                    bus_pub->devid,
1238                                                    bus_pub->chiprev);
1239         if (!devinfo->settings) {
1240                 ret = -ENOMEM;
1241                 goto fail;
1242         }
1243
1244         if (!brcmf_usb_dlneeded(devinfo)) {
1245                 ret = brcmf_usb_bus_setup(devinfo);
1246                 if (ret)
1247                         goto fail;
1248                 /* we are done */
1249                 complete(&devinfo->dev_init_done);
1250                 return 0;
1251         }
1252         bus->chip = bus_pub->devid;
1253         bus->chiprev = bus_pub->chiprev;
1254
1255         ret = brcmf_fw_map_chip_to_name(bus_pub->devid, bus_pub->chiprev,
1256                                         brcmf_usb_fwnames,
1257                                         ARRAY_SIZE(brcmf_usb_fwnames),
1258                                         devinfo->fw_name, NULL);
1259         if (ret)
1260                 goto fail;
1261
1262         /* request firmware here */
1263         ret = brcmf_fw_get_firmwares(dev, 0, devinfo->fw_name, NULL,
1264                                      brcmf_usb_probe_phase2);
1265         if (ret) {
1266                 brcmf_err("firmware request failed: %d\n", ret);
1267                 goto fail;
1268         }
1269
1270         return 0;
1271
1272 fail:
1273         /* Release resources in reverse order */
1274         kfree(bus);
1275         brcmf_usb_detach(devinfo);
1276         return ret;
1277 }
1278
1279 static void
1280 brcmf_usb_disconnect_cb(struct brcmf_usbdev_info *devinfo)
1281 {
1282         if (!devinfo)
1283                 return;
1284         brcmf_dbg(USB, "Enter, bus_pub %p\n", devinfo);
1285
1286         brcmf_detach(devinfo->dev);
1287         kfree(devinfo->bus_pub.bus);
1288         brcmf_usb_detach(devinfo);
1289 }
1290
1291 static int
1292 brcmf_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
1293 {
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;
1298         int ret = 0;
1299         u32 num_of_eps;
1300         u8 endpoint_num, ep;
1301
1302         brcmf_dbg(USB, "Enter 0x%04x:0x%04x\n", id->idVendor, id->idProduct);
1303
1304         devinfo = kzalloc(sizeof(*devinfo), GFP_ATOMIC);
1305         if (devinfo == NULL)
1306                 return -ENOMEM;
1307
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
1312          */
1313         init_completion(&devinfo->dev_init_done);
1314
1315         usb_set_intfdata(intf, devinfo);
1316
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);
1321                 ret = -ENODEV;
1322                 goto fail;
1323         }
1324
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);
1330                 ret = -ENODEV;
1331                 goto fail;
1332         }
1333
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);
1341                 ret = -ENODEV;
1342                 goto fail;
1343         }
1344
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))
1350                         continue;
1351                 if (usb_endpoint_dir_in(endpoint)) {
1352                         if (!devinfo->rx_pipe)
1353                                 devinfo->rx_pipe =
1354                                         usb_rcvbulkpipe(usb, endpoint_num);
1355                 } else {
1356                         if (!devinfo->tx_pipe)
1357                                 devinfo->tx_pipe =
1358                                         usb_sndbulkpipe(usb, endpoint_num);
1359                 }
1360         }
1361         if (devinfo->rx_pipe == 0) {
1362                 brcmf_err("No RX (in) Bulk EP found\n");
1363                 ret = -ENODEV;
1364                 goto fail;
1365         }
1366         if (devinfo->tx_pipe == 0) {
1367                 brcmf_err("No TX (out) Bulk EP found\n");
1368                 ret = -ENODEV;
1369                 goto fail;
1370         }
1371
1372         devinfo->ifnum = desc->bInterfaceNumber;
1373
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");
1380         else
1381                 brcmf_dbg(USB, "Broadcom full speed USB WLAN interface detected\n");
1382
1383         ret = brcmf_usb_probe_cb(devinfo);
1384         if (ret)
1385                 goto fail;
1386
1387         /* Success */
1388         return 0;
1389
1390 fail:
1391         complete(&devinfo->dev_init_done);
1392         kfree(devinfo);
1393         usb_set_intfdata(intf, NULL);
1394         return ret;
1395 }
1396
1397 static void
1398 brcmf_usb_disconnect(struct usb_interface *intf)
1399 {
1400         struct brcmf_usbdev_info *devinfo;
1401
1402         brcmf_dbg(USB, "Enter\n");
1403         devinfo = (struct brcmf_usbdev_info *)usb_get_intfdata(intf);
1404
1405         if (devinfo) {
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.
1409                  */
1410                 if (!usb_get_intfdata(intf))
1411                         goto done;
1412
1413                 brcmf_usb_disconnect_cb(devinfo);
1414                 kfree(devinfo);
1415         }
1416 done:
1417         brcmf_dbg(USB, "Exit\n");
1418 }
1419
1420 /*
1421  * only need to signal the bus being down and update the state.
1422  */
1423 static int brcmf_usb_suspend(struct usb_interface *intf, pm_message_t state)
1424 {
1425         struct usb_device *usb = interface_to_usbdev(intf);
1426         struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(&usb->dev);
1427
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);
1432         else
1433                 brcmf_detach(&usb->dev);
1434         return 0;
1435 }
1436
1437 /*
1438  * (re-) start the bus.
1439  */
1440 static int brcmf_usb_resume(struct usb_interface *intf)
1441 {
1442         struct usb_device *usb = interface_to_usbdev(intf);
1443         struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(&usb->dev);
1444
1445         brcmf_dbg(USB, "Enter\n");
1446         if (!devinfo->wowl_enabled)
1447                 return brcmf_usb_bus_setup(devinfo);
1448
1449         devinfo->bus_pub.state = BRCMFMAC_USB_STATE_UP;
1450         brcmf_usb_rx_fill_all(devinfo);
1451         return 0;
1452 }
1453
1454 static int brcmf_usb_reset_resume(struct usb_interface *intf)
1455 {
1456         struct usb_device *usb = interface_to_usbdev(intf);
1457         struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(&usb->dev);
1458
1459         brcmf_dbg(USB, "Enter\n");
1460
1461         return brcmf_fw_get_firmwares(&usb->dev, 0, devinfo->fw_name, NULL,
1462                                       brcmf_usb_probe_phase2);
1463 }
1464
1465 #define BRCMF_USB_DEVICE(dev_id)        \
1466         { USB_DEVICE(BRCM_USB_VENDOR_ID_BROADCOM, dev_id) }
1467
1468 #define LINKSYS_USB_DEVICE(dev_id)      \
1469         { USB_DEVICE(BRCM_USB_VENDOR_ID_LINKSYS, dev_id) }
1470
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 */ }
1481 };
1482
1483 MODULE_DEVICE_TABLE(usb, brcmf_usb_devid_table);
1484
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,
1494 };
1495
1496 static int brcmf_usb_reset_device(struct device *dev, void *notused)
1497 {
1498         /* device past is the usb interface so we
1499          * need to use parent here.
1500          */
1501         brcmf_dev_reset(dev->parent);
1502         return 0;
1503 }
1504
1505 void brcmf_usb_exit(void)
1506 {
1507         struct device_driver *drv = &brcmf_usbdrvr.drvwrap.driver;
1508         int ret;
1509
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);
1514 }
1515
1516 void brcmf_usb_register(void)
1517 {
1518         brcmf_dbg(USB, "Enter\n");
1519         usb_register(&brcmf_usbdrvr);
1520 }