Linux-libre 4.4.228-gnu
[librecmc/linux-libre.git] / drivers / net / wireless / 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
31
32 #define IOCTL_RESP_TIMEOUT              2000
33
34 #define BRCMF_USB_RESET_GETVER_SPINWAIT 100     /* in unit of ms */
35 #define BRCMF_USB_RESET_GETVER_LOOP_CNT 10
36
37 #define BRCMF_POSTBOOT_ID               0xA123  /* ID to detect if dongle
38                                                    has boot up */
39 #define BRCMF_USB_NRXQ                  50
40 #define BRCMF_USB_NTXQ                  50
41
42 #define BRCMF_USB_CBCTL_WRITE           0
43 #define BRCMF_USB_CBCTL_READ            1
44 #define BRCMF_USB_MAX_PKT_SIZE          1600
45
46 #define BRCMF_USB_43143_FW_NAME         "/*(DEBLOBBED)*/"
47 #define BRCMF_USB_43236_FW_NAME         "/*(DEBLOBBED)*/"
48 #define BRCMF_USB_43242_FW_NAME         "/*(DEBLOBBED)*/"
49 #define BRCMF_USB_43569_FW_NAME         "/*(DEBLOBBED)*/"
50
51 #define TRX_MAGIC               0x30524448      /* "HDR0" */
52 #define TRX_MAX_OFFSET          3               /* Max number of file offsets */
53 #define TRX_UNCOMP_IMAGE        0x20            /* Trx holds uncompressed img */
54 #define TRX_RDL_CHUNK           1500            /* size of each dl transfer */
55 #define TRX_OFFSETS_DLFWLEN_IDX 0
56
57 /* Control messages: bRequest values */
58 #define DL_GETSTATE     0       /* returns the rdl_state_t struct */
59 #define DL_CHECK_CRC    1       /* currently unused */
60 #define DL_GO           2       /* execute downloaded image */
61 #define DL_START        3       /* initialize dl state */
62 #define DL_REBOOT       4       /* reboot the device in 2 seconds */
63 #define DL_GETVER       5       /* returns the bootrom_id_t struct */
64 #define DL_GO_PROTECTED 6       /* execute the downloaded code and set reset
65                                  * event to occur in 2 seconds.  It is the
66                                  * responsibility of the downloaded code to
67                                  * clear this event
68                                  */
69 #define DL_EXEC         7       /* jump to a supplied address */
70 #define DL_RESETCFG     8       /* To support single enum on dongle
71                                  * - Not used by bootloader
72                                  */
73 #define DL_DEFER_RESP_OK 9      /* Potentially defer the response to setup
74                                  * if resp unavailable
75                                  */
76
77 /* states */
78 #define DL_WAITING      0       /* waiting to rx first pkt */
79 #define DL_READY        1       /* hdr was good, waiting for more of the
80                                  * compressed image
81                                  */
82 #define DL_BAD_HDR      2       /* hdr was corrupted */
83 #define DL_BAD_CRC      3       /* compressed image was corrupted */
84 #define DL_RUNNABLE     4       /* download was successful,waiting for go cmd */
85 #define DL_START_FAIL   5       /* failed to initialize correctly */
86 #define DL_NVRAM_TOOBIG 6       /* host specified nvram data exceeds DL_NVRAM
87                                  * value
88                                  */
89 #define DL_IMAGE_TOOBIG 7       /* firmware image too big */
90
91
92 struct trx_header_le {
93         __le32 magic;           /* "HDR0" */
94         __le32 len;             /* Length of file including header */
95         __le32 crc32;           /* CRC from flag_version to end of file */
96         __le32 flag_version;    /* 0:15 flags, 16:31 version */
97         __le32 offsets[TRX_MAX_OFFSET]; /* Offsets of partitions from start of
98                                          * header
99                                          */
100 };
101
102 struct rdl_state_le {
103         __le32 state;
104         __le32 bytes;
105 };
106
107 struct bootrom_id_le {
108         __le32 chip;            /* Chip id */
109         __le32 chiprev;         /* Chip rev */
110         __le32 ramsize;         /* Size of  RAM */
111         __le32 remapbase;       /* Current remap base address */
112         __le32 boardtype;       /* Type of board */
113         __le32 boardrev;        /* Board revision */
114 };
115
116 struct brcmf_usb_image {
117         struct list_head list;
118         s8 *fwname;
119         u8 *image;
120         int image_len;
121 };
122
123 struct brcmf_usbdev_info {
124         struct brcmf_usbdev bus_pub; /* MUST BE FIRST */
125         spinlock_t qlock;
126         struct list_head rx_freeq;
127         struct list_head rx_postq;
128         struct list_head tx_freeq;
129         struct list_head tx_postq;
130         uint rx_pipe, tx_pipe;
131
132         int rx_low_watermark;
133         int tx_low_watermark;
134         int tx_high_watermark;
135         int tx_freecount;
136         bool tx_flowblock;
137         spinlock_t tx_flowblock_lock;
138
139         struct brcmf_usbreq *tx_reqs;
140         struct brcmf_usbreq *rx_reqs;
141
142         const u8 *image;        /* buffer for combine fw and nvram */
143         int image_len;
144
145         struct usb_device *usbdev;
146         struct device *dev;
147         struct completion dev_init_done;
148
149         int ctl_in_pipe, ctl_out_pipe;
150         struct urb *ctl_urb; /* URB for control endpoint */
151         struct usb_ctrlrequest ctl_write;
152         struct usb_ctrlrequest ctl_read;
153         u32 ctl_urb_actual_length;
154         int ctl_urb_status;
155         int ctl_completed;
156         wait_queue_head_t ioctl_resp_wait;
157         ulong ctl_op;
158         u8 ifnum;
159
160         struct urb *bulk_urb; /* used for FW download */
161
162         bool wowl_enabled;
163 };
164
165 static void brcmf_usb_rx_refill(struct brcmf_usbdev_info *devinfo,
166                                 struct brcmf_usbreq  *req);
167
168 static struct brcmf_usbdev *brcmf_usb_get_buspub(struct device *dev)
169 {
170         struct brcmf_bus *bus_if = dev_get_drvdata(dev);
171         return bus_if->bus_priv.usb;
172 }
173
174 static struct brcmf_usbdev_info *brcmf_usb_get_businfo(struct device *dev)
175 {
176         return brcmf_usb_get_buspub(dev)->devinfo;
177 }
178
179 static int brcmf_usb_ioctl_resp_wait(struct brcmf_usbdev_info *devinfo)
180 {
181         return wait_event_timeout(devinfo->ioctl_resp_wait,
182                                   devinfo->ctl_completed,
183                                   msecs_to_jiffies(IOCTL_RESP_TIMEOUT));
184 }
185
186 static void brcmf_usb_ioctl_resp_wake(struct brcmf_usbdev_info *devinfo)
187 {
188         if (waitqueue_active(&devinfo->ioctl_resp_wait))
189                 wake_up(&devinfo->ioctl_resp_wait);
190 }
191
192 static void
193 brcmf_usb_ctl_complete(struct brcmf_usbdev_info *devinfo, int type, int status)
194 {
195         brcmf_dbg(USB, "Enter, status=%d\n", status);
196
197         if (unlikely(devinfo == NULL))
198                 return;
199
200         if (type == BRCMF_USB_CBCTL_READ) {
201                 if (status == 0)
202                         devinfo->bus_pub.stats.rx_ctlpkts++;
203                 else
204                         devinfo->bus_pub.stats.rx_ctlerrs++;
205         } else if (type == BRCMF_USB_CBCTL_WRITE) {
206                 if (status == 0)
207                         devinfo->bus_pub.stats.tx_ctlpkts++;
208                 else
209                         devinfo->bus_pub.stats.tx_ctlerrs++;
210         }
211
212         devinfo->ctl_urb_status = status;
213         devinfo->ctl_completed = true;
214         brcmf_usb_ioctl_resp_wake(devinfo);
215 }
216
217 static void
218 brcmf_usb_ctlread_complete(struct urb *urb)
219 {
220         struct brcmf_usbdev_info *devinfo =
221                 (struct brcmf_usbdev_info *)urb->context;
222
223         brcmf_dbg(USB, "Enter\n");
224         devinfo->ctl_urb_actual_length = urb->actual_length;
225         brcmf_usb_ctl_complete(devinfo, BRCMF_USB_CBCTL_READ,
226                 urb->status);
227 }
228
229 static void
230 brcmf_usb_ctlwrite_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         brcmf_usb_ctl_complete(devinfo, BRCMF_USB_CBCTL_WRITE,
237                 urb->status);
238 }
239
240 static int
241 brcmf_usb_send_ctl(struct brcmf_usbdev_info *devinfo, u8 *buf, int len)
242 {
243         int ret;
244         u16 size;
245
246         brcmf_dbg(USB, "Enter\n");
247         if (devinfo == NULL || buf == NULL ||
248             len == 0 || devinfo->ctl_urb == NULL)
249                 return -EINVAL;
250
251         size = len;
252         devinfo->ctl_write.wLength = cpu_to_le16p(&size);
253         devinfo->ctl_urb->transfer_buffer_length = size;
254         devinfo->ctl_urb_status = 0;
255         devinfo->ctl_urb_actual_length = 0;
256
257         usb_fill_control_urb(devinfo->ctl_urb,
258                 devinfo->usbdev,
259                 devinfo->ctl_out_pipe,
260                 (unsigned char *) &devinfo->ctl_write,
261                 buf, size,
262                 (usb_complete_t)brcmf_usb_ctlwrite_complete,
263                 devinfo);
264
265         ret = usb_submit_urb(devinfo->ctl_urb, GFP_ATOMIC);
266         if (ret < 0)
267                 brcmf_err("usb_submit_urb failed %d\n", ret);
268
269         return ret;
270 }
271
272 static int
273 brcmf_usb_recv_ctl(struct brcmf_usbdev_info *devinfo, u8 *buf, int len)
274 {
275         int ret;
276         u16 size;
277
278         brcmf_dbg(USB, "Enter\n");
279         if ((devinfo == NULL) || (buf == NULL) || (len == 0)
280                 || (devinfo->ctl_urb == NULL))
281                 return -EINVAL;
282
283         size = len;
284         devinfo->ctl_read.wLength = cpu_to_le16p(&size);
285         devinfo->ctl_urb->transfer_buffer_length = size;
286
287         devinfo->ctl_read.bRequestType = USB_DIR_IN
288                 | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
289         devinfo->ctl_read.bRequest = 1;
290
291         usb_fill_control_urb(devinfo->ctl_urb,
292                 devinfo->usbdev,
293                 devinfo->ctl_in_pipe,
294                 (unsigned char *) &devinfo->ctl_read,
295                 buf, size,
296                 (usb_complete_t)brcmf_usb_ctlread_complete,
297                 devinfo);
298
299         ret = usb_submit_urb(devinfo->ctl_urb, GFP_ATOMIC);
300         if (ret < 0)
301                 brcmf_err("usb_submit_urb failed %d\n", ret);
302
303         return ret;
304 }
305
306 static int brcmf_usb_tx_ctlpkt(struct device *dev, u8 *buf, u32 len)
307 {
308         int err = 0;
309         int timeout = 0;
310         struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
311
312         brcmf_dbg(USB, "Enter\n");
313         if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP)
314                 return -EIO;
315
316         if (test_and_set_bit(0, &devinfo->ctl_op))
317                 return -EIO;
318
319         devinfo->ctl_completed = false;
320         err = brcmf_usb_send_ctl(devinfo, buf, len);
321         if (err) {
322                 brcmf_err("fail %d bytes: %d\n", err, len);
323                 clear_bit(0, &devinfo->ctl_op);
324                 return err;
325         }
326         timeout = brcmf_usb_ioctl_resp_wait(devinfo);
327         clear_bit(0, &devinfo->ctl_op);
328         if (!timeout) {
329                 brcmf_err("Txctl wait timed out\n");
330                 err = -EIO;
331         }
332         return err;
333 }
334
335 static int brcmf_usb_rx_ctlpkt(struct device *dev, u8 *buf, u32 len)
336 {
337         int err = 0;
338         int timeout = 0;
339         struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
340
341         brcmf_dbg(USB, "Enter\n");
342         if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP)
343                 return -EIO;
344
345         if (test_and_set_bit(0, &devinfo->ctl_op))
346                 return -EIO;
347
348         devinfo->ctl_completed = false;
349         err = brcmf_usb_recv_ctl(devinfo, buf, len);
350         if (err) {
351                 brcmf_err("fail %d bytes: %d\n", err, len);
352                 clear_bit(0, &devinfo->ctl_op);
353                 return err;
354         }
355         timeout = brcmf_usb_ioctl_resp_wait(devinfo);
356         err = devinfo->ctl_urb_status;
357         clear_bit(0, &devinfo->ctl_op);
358         if (!timeout) {
359                 brcmf_err("rxctl wait timed out\n");
360                 err = -EIO;
361         }
362         if (!err)
363                 return devinfo->ctl_urb_actual_length;
364         else
365                 return err;
366 }
367
368 static struct brcmf_usbreq *brcmf_usb_deq(struct brcmf_usbdev_info *devinfo,
369                                           struct list_head *q, int *counter)
370 {
371         unsigned long flags;
372         struct brcmf_usbreq  *req;
373         spin_lock_irqsave(&devinfo->qlock, flags);
374         if (list_empty(q)) {
375                 spin_unlock_irqrestore(&devinfo->qlock, flags);
376                 return NULL;
377         }
378         req = list_entry(q->next, struct brcmf_usbreq, list);
379         list_del_init(q->next);
380         if (counter)
381                 (*counter)--;
382         spin_unlock_irqrestore(&devinfo->qlock, flags);
383         return req;
384
385 }
386
387 static void brcmf_usb_enq(struct brcmf_usbdev_info *devinfo,
388                           struct list_head *q, struct brcmf_usbreq *req,
389                           int *counter)
390 {
391         unsigned long flags;
392         spin_lock_irqsave(&devinfo->qlock, flags);
393         list_add_tail(&req->list, q);
394         if (counter)
395                 (*counter)++;
396         spin_unlock_irqrestore(&devinfo->qlock, flags);
397 }
398
399 static struct brcmf_usbreq *
400 brcmf_usbdev_qinit(struct list_head *q, int qsize)
401 {
402         int i;
403         struct brcmf_usbreq *req, *reqs;
404
405         reqs = kcalloc(qsize, sizeof(struct brcmf_usbreq), GFP_ATOMIC);
406         if (reqs == NULL)
407                 return NULL;
408
409         req = reqs;
410
411         for (i = 0; i < qsize; i++) {
412                 req->urb = usb_alloc_urb(0, GFP_ATOMIC);
413                 if (!req->urb)
414                         goto fail;
415
416                 INIT_LIST_HEAD(&req->list);
417                 list_add_tail(&req->list, q);
418                 req++;
419         }
420         return reqs;
421 fail:
422         brcmf_err("fail!\n");
423         while (!list_empty(q)) {
424                 req = list_entry(q->next, struct brcmf_usbreq, list);
425                 if (req)
426                         usb_free_urb(req->urb);
427                 list_del(q->next);
428         }
429         kfree(reqs);
430         return NULL;
431
432 }
433
434 static void brcmf_usb_free_q(struct list_head *q, bool pending)
435 {
436         struct brcmf_usbreq *req, *next;
437         int i = 0;
438         list_for_each_entry_safe(req, next, q, list) {
439                 if (!req->urb) {
440                         brcmf_err("bad req\n");
441                         break;
442                 }
443                 i++;
444                 if (pending) {
445                         usb_kill_urb(req->urb);
446                 } else {
447                         usb_free_urb(req->urb);
448                         list_del_init(&req->list);
449                 }
450         }
451 }
452
453 static void brcmf_usb_del_fromq(struct brcmf_usbdev_info *devinfo,
454                                 struct brcmf_usbreq *req)
455 {
456         unsigned long flags;
457
458         spin_lock_irqsave(&devinfo->qlock, flags);
459         list_del_init(&req->list);
460         spin_unlock_irqrestore(&devinfo->qlock, flags);
461 }
462
463
464 static void brcmf_usb_tx_complete(struct urb *urb)
465 {
466         struct brcmf_usbreq *req = (struct brcmf_usbreq *)urb->context;
467         struct brcmf_usbdev_info *devinfo = req->devinfo;
468         unsigned long flags;
469
470         brcmf_dbg(USB, "Enter, urb->status=%d, skb=%p\n", urb->status,
471                   req->skb);
472         brcmf_usb_del_fromq(devinfo, req);
473
474         brcmf_txcomplete(devinfo->dev, req->skb, urb->status == 0);
475         req->skb = NULL;
476         brcmf_usb_enq(devinfo, &devinfo->tx_freeq, req, &devinfo->tx_freecount);
477         spin_lock_irqsave(&devinfo->tx_flowblock_lock, flags);
478         if (devinfo->tx_freecount > devinfo->tx_high_watermark &&
479                 devinfo->tx_flowblock) {
480                 brcmf_txflowblock(devinfo->dev, false);
481                 devinfo->tx_flowblock = false;
482         }
483         spin_unlock_irqrestore(&devinfo->tx_flowblock_lock, flags);
484 }
485
486 static void brcmf_usb_rx_complete(struct urb *urb)
487 {
488         struct brcmf_usbreq  *req = (struct brcmf_usbreq *)urb->context;
489         struct brcmf_usbdev_info *devinfo = req->devinfo;
490         struct sk_buff *skb;
491
492         brcmf_dbg(USB, "Enter, urb->status=%d\n", urb->status);
493         brcmf_usb_del_fromq(devinfo, req);
494         skb = req->skb;
495         req->skb = NULL;
496
497         /* zero lenght packets indicate usb "failure". Do not refill */
498         if (urb->status != 0 || !urb->actual_length) {
499                 brcmu_pkt_buf_free_skb(skb);
500                 brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
501                 return;
502         }
503
504         if (devinfo->bus_pub.state == BRCMFMAC_USB_STATE_UP) {
505                 skb_put(skb, urb->actual_length);
506                 brcmf_rx_frame(devinfo->dev, skb, true);
507                 brcmf_usb_rx_refill(devinfo, req);
508         } else {
509                 brcmu_pkt_buf_free_skb(skb);
510                 brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
511         }
512         return;
513
514 }
515
516 static void brcmf_usb_rx_refill(struct brcmf_usbdev_info *devinfo,
517                                 struct brcmf_usbreq  *req)
518 {
519         struct sk_buff *skb;
520         int ret;
521
522         if (!req || !devinfo)
523                 return;
524
525         skb = dev_alloc_skb(devinfo->bus_pub.bus_mtu);
526         if (!skb) {
527                 brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
528                 return;
529         }
530         req->skb = skb;
531
532         usb_fill_bulk_urb(req->urb, devinfo->usbdev, devinfo->rx_pipe,
533                           skb->data, skb_tailroom(skb), brcmf_usb_rx_complete,
534                           req);
535         req->devinfo = devinfo;
536         brcmf_usb_enq(devinfo, &devinfo->rx_postq, req, NULL);
537
538         ret = usb_submit_urb(req->urb, GFP_ATOMIC);
539         if (ret) {
540                 brcmf_usb_del_fromq(devinfo, req);
541                 brcmu_pkt_buf_free_skb(req->skb);
542                 req->skb = NULL;
543                 brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
544         }
545         return;
546 }
547
548 static void brcmf_usb_rx_fill_all(struct brcmf_usbdev_info *devinfo)
549 {
550         struct brcmf_usbreq *req;
551
552         if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP) {
553                 brcmf_err("bus is not up=%d\n", devinfo->bus_pub.state);
554                 return;
555         }
556         while ((req = brcmf_usb_deq(devinfo, &devinfo->rx_freeq, NULL)) != NULL)
557                 brcmf_usb_rx_refill(devinfo, req);
558 }
559
560 static void
561 brcmf_usb_state_change(struct brcmf_usbdev_info *devinfo, int state)
562 {
563         struct brcmf_bus *bcmf_bus = devinfo->bus_pub.bus;
564         int old_state;
565
566         brcmf_dbg(USB, "Enter, current state=%d, new state=%d\n",
567                   devinfo->bus_pub.state, state);
568
569         if (devinfo->bus_pub.state == state)
570                 return;
571
572         old_state = devinfo->bus_pub.state;
573         devinfo->bus_pub.state = state;
574
575         /* update state of upper layer */
576         if (state == BRCMFMAC_USB_STATE_DOWN) {
577                 brcmf_dbg(USB, "DBUS is down\n");
578                 brcmf_bus_change_state(bcmf_bus, BRCMF_BUS_DOWN);
579         } else if (state == BRCMFMAC_USB_STATE_UP) {
580                 brcmf_dbg(USB, "DBUS is up\n");
581                 brcmf_bus_change_state(bcmf_bus, BRCMF_BUS_UP);
582         } else {
583                 brcmf_dbg(USB, "DBUS current state=%d\n", state);
584         }
585 }
586
587 static int brcmf_usb_tx(struct device *dev, struct sk_buff *skb)
588 {
589         struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
590         struct brcmf_usbreq  *req;
591         int ret;
592         unsigned long flags;
593
594         brcmf_dbg(USB, "Enter, skb=%p\n", skb);
595         if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP) {
596                 ret = -EIO;
597                 goto fail;
598         }
599
600         req = brcmf_usb_deq(devinfo, &devinfo->tx_freeq,
601                                         &devinfo->tx_freecount);
602         if (!req) {
603                 brcmf_err("no req to send\n");
604                 ret = -ENOMEM;
605                 goto fail;
606         }
607
608         req->skb = skb;
609         req->devinfo = devinfo;
610         usb_fill_bulk_urb(req->urb, devinfo->usbdev, devinfo->tx_pipe,
611                           skb->data, skb->len, brcmf_usb_tx_complete, req);
612         req->urb->transfer_flags |= URB_ZERO_PACKET;
613         brcmf_usb_enq(devinfo, &devinfo->tx_postq, req, NULL);
614         ret = usb_submit_urb(req->urb, GFP_ATOMIC);
615         if (ret) {
616                 brcmf_err("brcmf_usb_tx usb_submit_urb FAILED\n");
617                 brcmf_usb_del_fromq(devinfo, req);
618                 req->skb = NULL;
619                 brcmf_usb_enq(devinfo, &devinfo->tx_freeq, req,
620                               &devinfo->tx_freecount);
621                 goto fail;
622         }
623
624         spin_lock_irqsave(&devinfo->tx_flowblock_lock, flags);
625         if (devinfo->tx_freecount < devinfo->tx_low_watermark &&
626             !devinfo->tx_flowblock) {
627                 brcmf_txflowblock(dev, true);
628                 devinfo->tx_flowblock = true;
629         }
630         spin_unlock_irqrestore(&devinfo->tx_flowblock_lock, flags);
631         return 0;
632
633 fail:
634         return ret;
635 }
636
637
638 static int brcmf_usb_up(struct device *dev)
639 {
640         struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
641
642         brcmf_dbg(USB, "Enter\n");
643         if (devinfo->bus_pub.state == BRCMFMAC_USB_STATE_UP)
644                 return 0;
645
646         /* Success, indicate devinfo is fully up */
647         brcmf_usb_state_change(devinfo, BRCMFMAC_USB_STATE_UP);
648
649         if (devinfo->ctl_urb) {
650                 devinfo->ctl_in_pipe = usb_rcvctrlpipe(devinfo->usbdev, 0);
651                 devinfo->ctl_out_pipe = usb_sndctrlpipe(devinfo->usbdev, 0);
652
653                 /* CTL Write */
654                 devinfo->ctl_write.bRequestType =
655                         USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
656                 devinfo->ctl_write.bRequest = 0;
657                 devinfo->ctl_write.wValue = cpu_to_le16(0);
658                 devinfo->ctl_write.wIndex = cpu_to_le16(devinfo->ifnum);
659
660                 /* CTL Read */
661                 devinfo->ctl_read.bRequestType =
662                         USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
663                 devinfo->ctl_read.bRequest = 1;
664                 devinfo->ctl_read.wValue = cpu_to_le16(0);
665                 devinfo->ctl_read.wIndex = cpu_to_le16(devinfo->ifnum);
666         }
667         brcmf_usb_rx_fill_all(devinfo);
668         return 0;
669 }
670
671 static void brcmf_cancel_all_urbs(struct brcmf_usbdev_info *devinfo)
672 {
673         int i;
674
675         if (devinfo->ctl_urb)
676                 usb_kill_urb(devinfo->ctl_urb);
677         if (devinfo->bulk_urb)
678                 usb_kill_urb(devinfo->bulk_urb);
679         if (devinfo->tx_reqs)
680                 for (i = 0; i < devinfo->bus_pub.ntxq; i++)
681                         usb_kill_urb(devinfo->tx_reqs[i].urb);
682         if (devinfo->rx_reqs)
683                 for (i = 0; i < devinfo->bus_pub.nrxq; i++)
684                         usb_kill_urb(devinfo->rx_reqs[i].urb);
685 }
686
687 static void brcmf_usb_down(struct device *dev)
688 {
689         struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
690
691         brcmf_dbg(USB, "Enter\n");
692         if (devinfo == NULL)
693                 return;
694
695         if (devinfo->bus_pub.state == BRCMFMAC_USB_STATE_DOWN)
696                 return;
697
698         brcmf_usb_state_change(devinfo, BRCMFMAC_USB_STATE_DOWN);
699
700         brcmf_cancel_all_urbs(devinfo);
701 }
702
703 static void
704 brcmf_usb_sync_complete(struct urb *urb)
705 {
706         struct brcmf_usbdev_info *devinfo =
707                         (struct brcmf_usbdev_info *)urb->context;
708
709         devinfo->ctl_completed = true;
710         brcmf_usb_ioctl_resp_wake(devinfo);
711 }
712
713 static int brcmf_usb_dl_cmd(struct brcmf_usbdev_info *devinfo, u8 cmd,
714                             void *buffer, int buflen)
715 {
716         int ret;
717         char *tmpbuf;
718         u16 size;
719
720         if ((!devinfo) || (devinfo->ctl_urb == NULL))
721                 return -EINVAL;
722
723         tmpbuf = kmalloc(buflen, GFP_ATOMIC);
724         if (!tmpbuf)
725                 return -ENOMEM;
726
727         size = buflen;
728         devinfo->ctl_urb->transfer_buffer_length = size;
729
730         devinfo->ctl_read.wLength = cpu_to_le16p(&size);
731         devinfo->ctl_read.bRequestType = USB_DIR_IN | USB_TYPE_VENDOR |
732                 USB_RECIP_INTERFACE;
733         devinfo->ctl_read.bRequest = cmd;
734
735         usb_fill_control_urb(devinfo->ctl_urb,
736                 devinfo->usbdev,
737                 usb_rcvctrlpipe(devinfo->usbdev, 0),
738                 (unsigned char *) &devinfo->ctl_read,
739                 (void *) tmpbuf, size,
740                 (usb_complete_t)brcmf_usb_sync_complete, devinfo);
741
742         devinfo->ctl_completed = false;
743         ret = usb_submit_urb(devinfo->ctl_urb, GFP_ATOMIC);
744         if (ret < 0) {
745                 brcmf_err("usb_submit_urb failed %d\n", ret);
746                 goto finalize;
747         }
748
749         if (!brcmf_usb_ioctl_resp_wait(devinfo)) {
750                 usb_kill_urb(devinfo->ctl_urb);
751                 ret = -ETIMEDOUT;
752         } else {
753                 memcpy(buffer, tmpbuf, buflen);
754         }
755
756 finalize:
757         kfree(tmpbuf);
758         return ret;
759 }
760
761 static bool
762 brcmf_usb_dlneeded(struct brcmf_usbdev_info *devinfo)
763 {
764         struct bootrom_id_le id;
765         u32 chipid, chiprev;
766
767         brcmf_dbg(USB, "Enter\n");
768
769         if (devinfo == NULL)
770                 return false;
771
772         /* Check if firmware downloaded already by querying runtime ID */
773         id.chip = cpu_to_le32(0xDEAD);
774         brcmf_usb_dl_cmd(devinfo, DL_GETVER, &id, sizeof(id));
775
776         chipid = le32_to_cpu(id.chip);
777         chiprev = le32_to_cpu(id.chiprev);
778
779         if ((chipid & 0x4300) == 0x4300)
780                 brcmf_dbg(USB, "chip %x rev 0x%x\n", chipid, chiprev);
781         else
782                 brcmf_dbg(USB, "chip %d rev 0x%x\n", chipid, chiprev);
783         if (chipid == BRCMF_POSTBOOT_ID) {
784                 brcmf_dbg(USB, "firmware already downloaded\n");
785                 brcmf_usb_dl_cmd(devinfo, DL_RESETCFG, &id, sizeof(id));
786                 return false;
787         } else {
788                 devinfo->bus_pub.devid = chipid;
789                 devinfo->bus_pub.chiprev = chiprev;
790         }
791         return true;
792 }
793
794 static int
795 brcmf_usb_resetcfg(struct brcmf_usbdev_info *devinfo)
796 {
797         struct bootrom_id_le id;
798         u32 loop_cnt;
799         int err;
800
801         brcmf_dbg(USB, "Enter\n");
802
803         loop_cnt = 0;
804         do {
805                 mdelay(BRCMF_USB_RESET_GETVER_SPINWAIT);
806                 loop_cnt++;
807                 id.chip = cpu_to_le32(0xDEAD);       /* Get the ID */
808                 err = brcmf_usb_dl_cmd(devinfo, DL_GETVER, &id, sizeof(id));
809                 if ((err) && (err != -ETIMEDOUT))
810                         return err;
811                 if (id.chip == cpu_to_le32(BRCMF_POSTBOOT_ID))
812                         break;
813         } while (loop_cnt < BRCMF_USB_RESET_GETVER_LOOP_CNT);
814
815         if (id.chip == cpu_to_le32(BRCMF_POSTBOOT_ID)) {
816                 brcmf_dbg(USB, "postboot chip 0x%x/rev 0x%x\n",
817                           le32_to_cpu(id.chip), le32_to_cpu(id.chiprev));
818
819                 brcmf_usb_dl_cmd(devinfo, DL_RESETCFG, &id, sizeof(id));
820                 return 0;
821         } else {
822                 brcmf_err("Cannot talk to Dongle. Firmware is not UP, %d ms\n",
823                           BRCMF_USB_RESET_GETVER_SPINWAIT * loop_cnt);
824                 return -EINVAL;
825         }
826 }
827
828
829 static int
830 brcmf_usb_dl_send_bulk(struct brcmf_usbdev_info *devinfo, void *buffer, int len)
831 {
832         int ret;
833
834         if ((devinfo == NULL) || (devinfo->bulk_urb == NULL))
835                 return -EINVAL;
836
837         /* Prepare the URB */
838         usb_fill_bulk_urb(devinfo->bulk_urb, devinfo->usbdev,
839                           devinfo->tx_pipe, buffer, len,
840                           (usb_complete_t)brcmf_usb_sync_complete, devinfo);
841
842         devinfo->bulk_urb->transfer_flags |= URB_ZERO_PACKET;
843
844         devinfo->ctl_completed = false;
845         ret = usb_submit_urb(devinfo->bulk_urb, GFP_ATOMIC);
846         if (ret) {
847                 brcmf_err("usb_submit_urb failed %d\n", ret);
848                 return ret;
849         }
850         ret = brcmf_usb_ioctl_resp_wait(devinfo);
851         return (ret == 0);
852 }
853
854 static int
855 brcmf_usb_dl_writeimage(struct brcmf_usbdev_info *devinfo, u8 *fw, int fwlen)
856 {
857         unsigned int sendlen, sent, dllen;
858         char *bulkchunk = NULL, *dlpos;
859         struct rdl_state_le state;
860         u32 rdlstate, rdlbytes;
861         int err = 0;
862
863         brcmf_dbg(USB, "Enter, fw %p, len %d\n", fw, fwlen);
864
865         bulkchunk = kmalloc(TRX_RDL_CHUNK, GFP_ATOMIC);
866         if (bulkchunk == NULL) {
867                 err = -ENOMEM;
868                 goto fail;
869         }
870
871         /* 1) Prepare USB boot loader for runtime image */
872         brcmf_usb_dl_cmd(devinfo, DL_START, &state, sizeof(state));
873
874         rdlstate = le32_to_cpu(state.state);
875         rdlbytes = le32_to_cpu(state.bytes);
876
877         /* 2) Check we are in the Waiting state */
878         if (rdlstate != DL_WAITING) {
879                 brcmf_err("Failed to DL_START\n");
880                 err = -EINVAL;
881                 goto fail;
882         }
883         sent = 0;
884         dlpos = fw;
885         dllen = fwlen;
886
887         /* Get chip id and rev */
888         while (rdlbytes != dllen) {
889                 /* Wait until the usb device reports it received all
890                  * the bytes we sent */
891                 if ((rdlbytes == sent) && (rdlbytes != dllen)) {
892                         if ((dllen-sent) < TRX_RDL_CHUNK)
893                                 sendlen = dllen-sent;
894                         else
895                                 sendlen = TRX_RDL_CHUNK;
896
897                         /* simply avoid having to send a ZLP by ensuring we
898                          * never have an even
899                          * multiple of 64
900                          */
901                         if (!(sendlen % 64))
902                                 sendlen -= 4;
903
904                         /* send data */
905                         memcpy(bulkchunk, dlpos, sendlen);
906                         if (brcmf_usb_dl_send_bulk(devinfo, bulkchunk,
907                                                    sendlen)) {
908                                 brcmf_err("send_bulk failed\n");
909                                 err = -EINVAL;
910                                 goto fail;
911                         }
912
913                         dlpos += sendlen;
914                         sent += sendlen;
915                 }
916                 err = brcmf_usb_dl_cmd(devinfo, DL_GETSTATE, &state,
917                                        sizeof(state));
918                 if (err) {
919                         brcmf_err("DL_GETSTATE Failed\n");
920                         goto fail;
921                 }
922
923                 rdlstate = le32_to_cpu(state.state);
924                 rdlbytes = le32_to_cpu(state.bytes);
925
926                 /* restart if an error is reported */
927                 if (rdlstate == DL_BAD_HDR || rdlstate == DL_BAD_CRC) {
928                         brcmf_err("Bad Hdr or Bad CRC state %d\n",
929                                   rdlstate);
930                         err = -EINVAL;
931                         goto fail;
932                 }
933         }
934
935 fail:
936         kfree(bulkchunk);
937         brcmf_dbg(USB, "Exit, err=%d\n", err);
938         return err;
939 }
940
941 static int brcmf_usb_dlstart(struct brcmf_usbdev_info *devinfo, u8 *fw, int len)
942 {
943         int err;
944
945         brcmf_dbg(USB, "Enter\n");
946
947         if (devinfo == NULL)
948                 return -EINVAL;
949
950         if (devinfo->bus_pub.devid == 0xDEAD)
951                 return -EINVAL;
952
953         err = brcmf_usb_dl_writeimage(devinfo, fw, len);
954         if (err == 0)
955                 devinfo->bus_pub.state = BRCMFMAC_USB_STATE_DL_DONE;
956         else
957                 devinfo->bus_pub.state = BRCMFMAC_USB_STATE_DL_FAIL;
958         brcmf_dbg(USB, "Exit, err=%d\n", err);
959
960         return err;
961 }
962
963 static int brcmf_usb_dlrun(struct brcmf_usbdev_info *devinfo)
964 {
965         struct rdl_state_le state;
966
967         brcmf_dbg(USB, "Enter\n");
968         if (!devinfo)
969                 return -EINVAL;
970
971         if (devinfo->bus_pub.devid == 0xDEAD)
972                 return -EINVAL;
973
974         /* Check we are runnable */
975         state.state = 0;
976         brcmf_usb_dl_cmd(devinfo, DL_GETSTATE, &state, sizeof(state));
977
978         /* Start the image */
979         if (state.state == cpu_to_le32(DL_RUNNABLE)) {
980                 if (brcmf_usb_dl_cmd(devinfo, DL_GO, &state, sizeof(state)))
981                         return -ENODEV;
982                 if (brcmf_usb_resetcfg(devinfo))
983                         return -ENODEV;
984                 /* The Dongle may go for re-enumeration. */
985         } else {
986                 brcmf_err("Dongle not runnable\n");
987                 return -EINVAL;
988         }
989         brcmf_dbg(USB, "Exit\n");
990         return 0;
991 }
992
993 static bool brcmf_usb_chip_support(int chipid, int chiprev)
994 {
995         switch(chipid) {
996         case BRCM_CC_43143_CHIP_ID:
997                 return true;
998         case BRCM_CC_43235_CHIP_ID:
999         case BRCM_CC_43236_CHIP_ID:
1000         case BRCM_CC_43238_CHIP_ID:
1001                 return (chiprev == 3);
1002         case BRCM_CC_43242_CHIP_ID:
1003                 return true;
1004         case BRCM_CC_43566_CHIP_ID:
1005         case BRCM_CC_43569_CHIP_ID:
1006                 return true;
1007         default:
1008                 break;
1009         }
1010         return false;
1011 }
1012
1013 static int
1014 brcmf_usb_fw_download(struct brcmf_usbdev_info *devinfo)
1015 {
1016         int devid, chiprev;
1017         int err;
1018
1019         brcmf_dbg(USB, "Enter\n");
1020         if (devinfo == NULL)
1021                 return -ENODEV;
1022
1023         devid = devinfo->bus_pub.devid;
1024         chiprev = devinfo->bus_pub.chiprev;
1025
1026         if (!brcmf_usb_chip_support(devid, chiprev)) {
1027                 brcmf_err("unsupported chip %d rev %d\n",
1028                           devid, chiprev);
1029                 return -EINVAL;
1030         }
1031
1032         if (!devinfo->image) {
1033                 brcmf_err("No firmware!\n");
1034                 return -ENOENT;
1035         }
1036
1037         err = brcmf_usb_dlstart(devinfo,
1038                 (u8 *)devinfo->image, devinfo->image_len);
1039         if (err == 0)
1040                 err = brcmf_usb_dlrun(devinfo);
1041         return err;
1042 }
1043
1044
1045 static void brcmf_usb_detach(struct brcmf_usbdev_info *devinfo)
1046 {
1047         brcmf_dbg(USB, "Enter, devinfo %p\n", devinfo);
1048
1049         /* free the URBS */
1050         brcmf_usb_free_q(&devinfo->rx_freeq, false);
1051         brcmf_usb_free_q(&devinfo->tx_freeq, false);
1052
1053         usb_free_urb(devinfo->ctl_urb);
1054         usb_free_urb(devinfo->bulk_urb);
1055
1056         kfree(devinfo->tx_reqs);
1057         kfree(devinfo->rx_reqs);
1058 }
1059
1060
1061 static int check_file(const u8 *headers)
1062 {
1063         struct trx_header_le *trx;
1064         int actual_len = -1;
1065
1066         brcmf_dbg(USB, "Enter\n");
1067         /* Extract trx header */
1068         trx = (struct trx_header_le *) headers;
1069         if (trx->magic != cpu_to_le32(TRX_MAGIC))
1070                 return -1;
1071
1072         headers += sizeof(struct trx_header_le);
1073
1074         if (le32_to_cpu(trx->flag_version) & TRX_UNCOMP_IMAGE) {
1075                 actual_len = le32_to_cpu(trx->offsets[TRX_OFFSETS_DLFWLEN_IDX]);
1076                 return actual_len + sizeof(struct trx_header_le);
1077         }
1078         return -1;
1079 }
1080
1081 static const char *brcmf_usb_get_fwname(struct brcmf_usbdev_info *devinfo)
1082 {
1083         switch (devinfo->bus_pub.devid) {
1084         case BRCM_CC_43143_CHIP_ID:
1085                 return BRCMF_USB_43143_FW_NAME;
1086         case BRCM_CC_43235_CHIP_ID:
1087         case BRCM_CC_43236_CHIP_ID:
1088         case BRCM_CC_43238_CHIP_ID:
1089                 return BRCMF_USB_43236_FW_NAME;
1090         case BRCM_CC_43242_CHIP_ID:
1091                 return BRCMF_USB_43242_FW_NAME;
1092         case BRCM_CC_43566_CHIP_ID:
1093         case BRCM_CC_43569_CHIP_ID:
1094                 return BRCMF_USB_43569_FW_NAME;
1095         default:
1096                 return NULL;
1097         }
1098 }
1099
1100
1101 static
1102 struct brcmf_usbdev *brcmf_usb_attach(struct brcmf_usbdev_info *devinfo,
1103                                       int nrxq, int ntxq)
1104 {
1105         brcmf_dbg(USB, "Enter\n");
1106
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;
1112
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;
1117
1118         /* Initialize other structure content */
1119         init_waitqueue_head(&devinfo->ioctl_resp_wait);
1120
1121         /* Initialize the spinlocks */
1122         spin_lock_init(&devinfo->qlock);
1123         spin_lock_init(&devinfo->tx_flowblock_lock);
1124
1125         INIT_LIST_HEAD(&devinfo->rx_freeq);
1126         INIT_LIST_HEAD(&devinfo->rx_postq);
1127
1128         INIT_LIST_HEAD(&devinfo->tx_freeq);
1129         INIT_LIST_HEAD(&devinfo->tx_postq);
1130
1131         devinfo->tx_flowblock = false;
1132
1133         devinfo->rx_reqs = brcmf_usbdev_qinit(&devinfo->rx_freeq, nrxq);
1134         if (!devinfo->rx_reqs)
1135                 goto error;
1136
1137         devinfo->tx_reqs = brcmf_usbdev_qinit(&devinfo->tx_freeq, ntxq);
1138         if (!devinfo->tx_reqs)
1139                 goto error;
1140         devinfo->tx_freecount = ntxq;
1141
1142         devinfo->ctl_urb = usb_alloc_urb(0, GFP_ATOMIC);
1143         if (!devinfo->ctl_urb) {
1144                 brcmf_err("usb_alloc_urb (ctl) failed\n");
1145                 goto error;
1146         }
1147         devinfo->bulk_urb = usb_alloc_urb(0, GFP_ATOMIC);
1148         if (!devinfo->bulk_urb) {
1149                 brcmf_err("usb_alloc_urb (bulk) failed\n");
1150                 goto error;
1151         }
1152
1153         return &devinfo->bus_pub;
1154
1155 error:
1156         brcmf_err("failed!\n");
1157         brcmf_usb_detach(devinfo);
1158         return NULL;
1159 }
1160
1161 static void brcmf_usb_wowl_config(struct device *dev, bool enabled)
1162 {
1163         struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
1164
1165         brcmf_dbg(USB, "Configuring WOWL, enabled=%d\n", enabled);
1166         devinfo->wowl_enabled = enabled;
1167         if (enabled)
1168                 device_set_wakeup_enable(devinfo->dev, true);
1169         else
1170                 device_set_wakeup_enable(devinfo->dev, false);
1171 }
1172
1173 static struct brcmf_bus_ops brcmf_usb_bus_ops = {
1174         .txdata = brcmf_usb_tx,
1175         .stop = brcmf_usb_down,
1176         .txctl = brcmf_usb_tx_ctlpkt,
1177         .rxctl = brcmf_usb_rx_ctlpkt,
1178         .wowl_config = brcmf_usb_wowl_config,
1179 };
1180
1181 static int brcmf_usb_bus_setup(struct brcmf_usbdev_info *devinfo)
1182 {
1183         int ret;
1184
1185         /* Attach to the common driver interface */
1186         ret = brcmf_attach(devinfo->dev);
1187         if (ret) {
1188                 brcmf_err("brcmf_attach failed\n");
1189                 return ret;
1190         }
1191
1192         ret = brcmf_usb_up(devinfo->dev);
1193         if (ret)
1194                 goto fail;
1195
1196         ret = brcmf_bus_start(devinfo->dev);
1197         if (ret)
1198                 goto fail;
1199
1200         return 0;
1201 fail:
1202         brcmf_detach(devinfo->dev);
1203         return ret;
1204 }
1205
1206 static void brcmf_usb_probe_phase2(struct device *dev,
1207                                    const struct firmware *fw,
1208                                    void *nvram, u32 nvlen)
1209 {
1210         struct brcmf_bus *bus = dev_get_drvdata(dev);
1211         struct brcmf_usbdev_info *devinfo;
1212         int ret;
1213
1214         brcmf_dbg(USB, "Start fw downloading\n");
1215
1216         devinfo = bus->bus_priv.usb->devinfo;
1217         ret = check_file(fw->data);
1218         if (ret < 0) {
1219                 brcmf_err("invalid firmware\n");
1220                 release_firmware(fw);
1221                 goto error;
1222         }
1223
1224         devinfo->image = fw->data;
1225         devinfo->image_len = fw->size;
1226
1227         ret = brcmf_usb_fw_download(devinfo);
1228         release_firmware(fw);
1229         if (ret)
1230                 goto error;
1231
1232         ret = brcmf_usb_bus_setup(devinfo);
1233         if (ret)
1234                 goto error;
1235
1236         complete(&devinfo->dev_init_done);
1237         return;
1238 error:
1239         brcmf_dbg(TRACE, "failed: dev=%s, err=%d\n", dev_name(dev), ret);
1240         complete(&devinfo->dev_init_done);
1241         device_release_driver(dev);
1242 }
1243
1244 static int brcmf_usb_probe_cb(struct brcmf_usbdev_info *devinfo)
1245 {
1246         struct brcmf_bus *bus = NULL;
1247         struct brcmf_usbdev *bus_pub = NULL;
1248         struct device *dev = devinfo->dev;
1249         int ret;
1250
1251         brcmf_dbg(USB, "Enter\n");
1252         bus_pub = brcmf_usb_attach(devinfo, BRCMF_USB_NRXQ, BRCMF_USB_NTXQ);
1253         if (!bus_pub)
1254                 return -ENODEV;
1255
1256         bus = kzalloc(sizeof(struct brcmf_bus), GFP_ATOMIC);
1257         if (!bus) {
1258                 ret = -ENOMEM;
1259                 goto fail;
1260         }
1261
1262         bus->dev = dev;
1263         bus_pub->bus = bus;
1264         bus->bus_priv.usb = bus_pub;
1265         dev_set_drvdata(dev, bus);
1266         bus->ops = &brcmf_usb_bus_ops;
1267         bus->proto_type = BRCMF_PROTO_BCDC;
1268         bus->always_use_fws_queue = true;
1269 #ifdef CONFIG_PM
1270         bus->wowl_supported = true;
1271 #endif
1272
1273         if (!brcmf_usb_dlneeded(devinfo)) {
1274                 ret = brcmf_usb_bus_setup(devinfo);
1275                 if (ret)
1276                         goto fail;
1277                 /* we are done */
1278                 complete(&devinfo->dev_init_done);
1279                 return 0;
1280         }
1281         bus->chip = bus_pub->devid;
1282         bus->chiprev = bus_pub->chiprev;
1283
1284         /* request firmware here */
1285         ret = brcmf_fw_get_firmwares(dev, 0, brcmf_usb_get_fwname(devinfo),
1286                                      NULL, brcmf_usb_probe_phase2);
1287         if (ret) {
1288                 brcmf_err("firmware request failed: %d\n", ret);
1289                 goto fail;
1290         }
1291
1292         return 0;
1293
1294 fail:
1295         /* Release resources in reverse order */
1296         kfree(bus);
1297         brcmf_usb_detach(devinfo);
1298         return ret;
1299 }
1300
1301 static void
1302 brcmf_usb_disconnect_cb(struct brcmf_usbdev_info *devinfo)
1303 {
1304         if (!devinfo)
1305                 return;
1306         brcmf_dbg(USB, "Enter, bus_pub %p\n", devinfo);
1307
1308         brcmf_detach(devinfo->dev);
1309         kfree(devinfo->bus_pub.bus);
1310         brcmf_usb_detach(devinfo);
1311 }
1312
1313 static int
1314 brcmf_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
1315 {
1316         struct usb_device *usb = interface_to_usbdev(intf);
1317         struct brcmf_usbdev_info *devinfo;
1318         struct usb_interface_descriptor *desc;
1319         struct usb_endpoint_descriptor *endpoint;
1320         int ret = 0;
1321         u32 num_of_eps;
1322         u8 endpoint_num, ep;
1323
1324         brcmf_dbg(USB, "Enter 0x%04x:0x%04x\n", id->idVendor, id->idProduct);
1325
1326         devinfo = kzalloc(sizeof(*devinfo), GFP_ATOMIC);
1327         if (devinfo == NULL)
1328                 return -ENOMEM;
1329
1330         devinfo->usbdev = usb;
1331         devinfo->dev = &usb->dev;
1332         /* Init completion, to protect for disconnect while still loading.
1333          * Necessary because of the asynchronous firmware load construction
1334          */
1335         init_completion(&devinfo->dev_init_done);
1336
1337         usb_set_intfdata(intf, devinfo);
1338
1339         /* Check that the device supports only one configuration */
1340         if (usb->descriptor.bNumConfigurations != 1) {
1341                 brcmf_err("Number of configurations: %d not supported\n",
1342                           usb->descriptor.bNumConfigurations);
1343                 ret = -ENODEV;
1344                 goto fail;
1345         }
1346
1347         if ((usb->descriptor.bDeviceClass != USB_CLASS_VENDOR_SPEC) &&
1348             (usb->descriptor.bDeviceClass != USB_CLASS_MISC) &&
1349             (usb->descriptor.bDeviceClass != USB_CLASS_WIRELESS_CONTROLLER)) {
1350                 brcmf_err("Device class: 0x%x not supported\n",
1351                           usb->descriptor.bDeviceClass);
1352                 ret = -ENODEV;
1353                 goto fail;
1354         }
1355
1356         desc = &intf->cur_altsetting->desc;
1357         if ((desc->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
1358             (desc->bInterfaceSubClass != 2) ||
1359             (desc->bInterfaceProtocol != 0xff)) {
1360                 brcmf_err("non WLAN interface %d: 0x%x:0x%x:0x%x\n",
1361                           desc->bInterfaceNumber, desc->bInterfaceClass,
1362                           desc->bInterfaceSubClass, desc->bInterfaceProtocol);
1363                 ret = -ENODEV;
1364                 goto fail;
1365         }
1366
1367         num_of_eps = desc->bNumEndpoints;
1368         for (ep = 0; ep < num_of_eps; ep++) {
1369                 endpoint = &intf->cur_altsetting->endpoint[ep].desc;
1370                 endpoint_num = usb_endpoint_num(endpoint);
1371                 if (!usb_endpoint_xfer_bulk(endpoint))
1372                         continue;
1373                 if (usb_endpoint_dir_in(endpoint)) {
1374                         if (!devinfo->rx_pipe)
1375                                 devinfo->rx_pipe =
1376                                         usb_rcvbulkpipe(usb, endpoint_num);
1377                 } else {
1378                         if (!devinfo->tx_pipe)
1379                                 devinfo->tx_pipe =
1380                                         usb_sndbulkpipe(usb, endpoint_num);
1381                 }
1382         }
1383         if (devinfo->rx_pipe == 0) {
1384                 brcmf_err("No RX (in) Bulk EP found\n");
1385                 ret = -ENODEV;
1386                 goto fail;
1387         }
1388         if (devinfo->tx_pipe == 0) {
1389                 brcmf_err("No TX (out) Bulk EP found\n");
1390                 ret = -ENODEV;
1391                 goto fail;
1392         }
1393
1394         devinfo->ifnum = desc->bInterfaceNumber;
1395
1396         if (usb->speed == USB_SPEED_SUPER)
1397                 brcmf_dbg(USB, "Broadcom super speed USB WLAN interface detected\n");
1398         else if (usb->speed == USB_SPEED_HIGH)
1399                 brcmf_dbg(USB, "Broadcom high speed USB WLAN interface detected\n");
1400         else
1401                 brcmf_dbg(USB, "Broadcom full speed USB WLAN interface detected\n");
1402
1403         ret = brcmf_usb_probe_cb(devinfo);
1404         if (ret)
1405                 goto fail;
1406
1407         /* Success */
1408         return 0;
1409
1410 fail:
1411         complete(&devinfo->dev_init_done);
1412         kfree(devinfo);
1413         usb_set_intfdata(intf, NULL);
1414         return ret;
1415 }
1416
1417 static void
1418 brcmf_usb_disconnect(struct usb_interface *intf)
1419 {
1420         struct brcmf_usbdev_info *devinfo;
1421
1422         brcmf_dbg(USB, "Enter\n");
1423         devinfo = (struct brcmf_usbdev_info *)usb_get_intfdata(intf);
1424
1425         if (devinfo) {
1426                 wait_for_completion(&devinfo->dev_init_done);
1427                 /* Make sure that devinfo still exists. Firmware probe routines
1428                  * may have released the device and cleared the intfdata.
1429                  */
1430                 if (!usb_get_intfdata(intf))
1431                         goto done;
1432
1433                 brcmf_usb_disconnect_cb(devinfo);
1434                 kfree(devinfo);
1435         }
1436 done:
1437         brcmf_dbg(USB, "Exit\n");
1438 }
1439
1440 /*
1441  * only need to signal the bus being down and update the state.
1442  */
1443 static int brcmf_usb_suspend(struct usb_interface *intf, pm_message_t state)
1444 {
1445         struct usb_device *usb = interface_to_usbdev(intf);
1446         struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(&usb->dev);
1447
1448         brcmf_dbg(USB, "Enter\n");
1449         devinfo->bus_pub.state = BRCMFMAC_USB_STATE_SLEEP;
1450         if (devinfo->wowl_enabled)
1451                 brcmf_cancel_all_urbs(devinfo);
1452         else
1453                 brcmf_detach(&usb->dev);
1454         return 0;
1455 }
1456
1457 /*
1458  * (re-) start the bus.
1459  */
1460 static int brcmf_usb_resume(struct usb_interface *intf)
1461 {
1462         struct usb_device *usb = interface_to_usbdev(intf);
1463         struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(&usb->dev);
1464
1465         brcmf_dbg(USB, "Enter\n");
1466         if (!devinfo->wowl_enabled)
1467                 return brcmf_usb_bus_setup(devinfo);
1468
1469         devinfo->bus_pub.state = BRCMFMAC_USB_STATE_UP;
1470         brcmf_usb_rx_fill_all(devinfo);
1471         return 0;
1472 }
1473
1474 static int brcmf_usb_reset_resume(struct usb_interface *intf)
1475 {
1476         struct usb_device *usb = interface_to_usbdev(intf);
1477         struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(&usb->dev);
1478
1479         brcmf_dbg(USB, "Enter\n");
1480
1481         return brcmf_fw_get_firmwares(&usb->dev, 0,
1482                                       brcmf_usb_get_fwname(devinfo), NULL,
1483                                       brcmf_usb_probe_phase2);
1484 }
1485
1486 #define BRCMF_USB_DEVICE(dev_id)        \
1487         { USB_DEVICE(BRCM_USB_VENDOR_ID_BROADCOM, dev_id) }
1488
1489 static struct usb_device_id brcmf_usb_devid_table[] = {
1490         BRCMF_USB_DEVICE(BRCM_USB_43143_DEVICE_ID),
1491         BRCMF_USB_DEVICE(BRCM_USB_43236_DEVICE_ID),
1492         BRCMF_USB_DEVICE(BRCM_USB_43242_DEVICE_ID),
1493         BRCMF_USB_DEVICE(BRCM_USB_43569_DEVICE_ID),
1494         /* special entry for device with firmware loaded and running */
1495         BRCMF_USB_DEVICE(BRCM_USB_BCMFW_DEVICE_ID),
1496         { /* end: all zeroes */ }
1497 };
1498
1499 MODULE_DEVICE_TABLE(usb, brcmf_usb_devid_table);
1500 /*(DEBLOBBED)*/
1501
1502 static struct usb_driver brcmf_usbdrvr = {
1503         .name = KBUILD_MODNAME,
1504         .probe = brcmf_usb_probe,
1505         .disconnect = brcmf_usb_disconnect,
1506         .id_table = brcmf_usb_devid_table,
1507         .suspend = brcmf_usb_suspend,
1508         .resume = brcmf_usb_resume,
1509         .reset_resume = brcmf_usb_reset_resume,
1510         .supports_autosuspend = 1,
1511         .disable_hub_initiated_lpm = 1,
1512 };
1513
1514 static int brcmf_usb_reset_device(struct device *dev, void *notused)
1515 {
1516         /* device past is the usb interface so we
1517          * need to use parent here.
1518          */
1519         brcmf_dev_reset(dev->parent);
1520         return 0;
1521 }
1522
1523 void brcmf_usb_exit(void)
1524 {
1525         struct device_driver *drv = &brcmf_usbdrvr.drvwrap.driver;
1526         int ret;
1527
1528         brcmf_dbg(USB, "Enter\n");
1529         ret = driver_for_each_device(drv, NULL, NULL,
1530                                      brcmf_usb_reset_device);
1531         usb_deregister(&brcmf_usbdrvr);
1532 }
1533
1534 void brcmf_usb_register(void)
1535 {
1536         brcmf_dbg(USB, "Enter\n");
1537         usb_register(&brcmf_usbdrvr);
1538 }