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