]> rtime.felk.cvut.cz Git - can-eth-gw-linux.git/blob - drivers/net/wireless/brcm80211/brcmfmac/usb.c
mm/bootmem.c: remove unused wrapper function reserve_bootmem_generic()
[can-eth-gw-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/init.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/kthread.h>
21 #include <linux/slab.h>
22 #include <linux/skbuff.h>
23 #include <linux/netdevice.h>
24 #include <linux/spinlock.h>
25 #include <linux/ethtool.h>
26 #include <linux/fcntl.h>
27 #include <linux/fs.h>
28 #include <linux/uaccess.h>
29 #include <linux/firmware.h>
30 #include <linux/usb.h>
31 #include <linux/vmalloc.h>
32 #include <net/cfg80211.h>
33
34 #include <defs.h>
35 #include <brcmu_utils.h>
36 #include <brcmu_wifi.h>
37 #include <dhd_bus.h>
38 #include <dhd_dbg.h>
39
40 #include "usb_rdl.h"
41 #include "usb.h"
42
43 #define IOCTL_RESP_TIMEOUT  2000
44
45 #define BRCMF_USB_SYNC_TIMEOUT          300     /* ms */
46 #define BRCMF_USB_DLIMAGE_SPINWAIT      100     /* in unit of ms */
47 #define BRCMF_USB_DLIMAGE_LIMIT         500     /* spinwait limit (ms) */
48
49 #define BRCMF_POSTBOOT_ID               0xA123  /* ID to detect if dongle
50                                                    has boot up */
51 #define BRCMF_USB_RESETCFG_SPINWAIT     1       /* wait after resetcfg (ms) */
52
53 #define BRCMF_USB_NRXQ  50
54 #define BRCMF_USB_NTXQ  50
55
56 #define CONFIGDESC(usb)         (&((usb)->actconfig)->desc)
57 #define IFPTR(usb, idx)         ((usb)->actconfig->interface[(idx)])
58 #define IFALTS(usb, idx)        (IFPTR((usb), (idx))->altsetting[0])
59 #define IFDESC(usb, idx)        IFALTS((usb), (idx)).desc
60 #define IFEPDESC(usb, idx, ep)  (IFALTS((usb), (idx)).endpoint[(ep)]).desc
61
62 #define CONTROL_IF              0
63 #define BULK_IF                 0
64
65 #define BRCMF_USB_CBCTL_WRITE   0
66 #define BRCMF_USB_CBCTL_READ    1
67 #define BRCMF_USB_MAX_PKT_SIZE  1600
68
69 #define BRCMF_USB_43143_FW_NAME "brcm/brcmfmac43143.bin"
70 #define BRCMF_USB_43236_FW_NAME "brcm/brcmfmac43236b.bin"
71 #define BRCMF_USB_43242_FW_NAME "brcm/brcmfmac43242a.bin"
72
73 enum usbdev_suspend_state {
74         USBOS_SUSPEND_STATE_DEVICE_ACTIVE = 0, /* Device is busy, won't allow
75                                                   suspend */
76         USBOS_SUSPEND_STATE_SUSPEND_PENDING,    /* Device is idle, can be
77                                                  * suspended. Wating PM to
78                                                  * suspend the device
79                                                  */
80         USBOS_SUSPEND_STATE_SUSPENDED   /* Device suspended */
81 };
82
83 struct brcmf_usb_image {
84         struct list_head list;
85         s8 *fwname;
86         u8 *image;
87         int image_len;
88 };
89 static struct list_head fw_image_list;
90
91 struct intr_transfer_buf {
92         u32 notification;
93         u32 reserved;
94 };
95
96 struct brcmf_usbdev_info {
97         struct brcmf_usbdev bus_pub; /* MUST BE FIRST */
98         spinlock_t qlock;
99         struct list_head rx_freeq;
100         struct list_head rx_postq;
101         struct list_head tx_freeq;
102         struct list_head tx_postq;
103         enum usbdev_suspend_state suspend_state;
104         uint rx_pipe, tx_pipe, intr_pipe, rx_pipe2;
105
106         bool activity;
107         int rx_low_watermark;
108         int tx_low_watermark;
109         int tx_high_watermark;
110         int tx_freecount;
111         bool tx_flowblock;
112
113         struct brcmf_usbreq *tx_reqs;
114         struct brcmf_usbreq *rx_reqs;
115
116         u8 *image;      /* buffer for combine fw and nvram */
117         int image_len;
118
119         wait_queue_head_t wait;
120         bool waitdone;
121         int sync_urb_status;
122
123         struct usb_device *usbdev;
124         struct device *dev;
125
126         int ctl_in_pipe, ctl_out_pipe;
127         struct urb *ctl_urb; /* URB for control endpoint */
128         struct usb_ctrlrequest ctl_write;
129         struct usb_ctrlrequest ctl_read;
130         u32 ctl_urb_actual_length;
131         int ctl_urb_status;
132         int ctl_completed;
133         wait_queue_head_t ioctl_resp_wait;
134         wait_queue_head_t ctrl_wait;
135         ulong ctl_op;
136
137         struct urb *bulk_urb; /* used for FW download */
138         struct urb *intr_urb; /* URB for interrupt endpoint */
139         int intr_size;          /* Size of interrupt message */
140         int interval;           /* Interrupt polling interval */
141         struct intr_transfer_buf intr; /* Data buffer for interrupt endpoint */
142 };
143
144 static void brcmf_usb_rx_refill(struct brcmf_usbdev_info *devinfo,
145                                 struct brcmf_usbreq  *req);
146
147 MODULE_AUTHOR("Broadcom Corporation");
148 MODULE_DESCRIPTION("Broadcom 802.11n wireless LAN fullmac usb driver.");
149 MODULE_SUPPORTED_DEVICE("Broadcom 802.11n WLAN fullmac usb cards");
150 MODULE_LICENSE("Dual BSD/GPL");
151
152 static struct brcmf_usbdev *brcmf_usb_get_buspub(struct device *dev)
153 {
154         struct brcmf_bus *bus_if = dev_get_drvdata(dev);
155         return bus_if->bus_priv.usb;
156 }
157
158 static struct brcmf_usbdev_info *brcmf_usb_get_businfo(struct device *dev)
159 {
160         return brcmf_usb_get_buspub(dev)->devinfo;
161 }
162
163 static int brcmf_usb_ioctl_resp_wait(struct brcmf_usbdev_info *devinfo)
164 {
165         return wait_event_timeout(devinfo->ioctl_resp_wait,
166                                   devinfo->ctl_completed,
167                                   msecs_to_jiffies(IOCTL_RESP_TIMEOUT));
168 }
169
170 static void brcmf_usb_ioctl_resp_wake(struct brcmf_usbdev_info *devinfo)
171 {
172         if (waitqueue_active(&devinfo->ioctl_resp_wait))
173                 wake_up(&devinfo->ioctl_resp_wait);
174 }
175
176 static void
177 brcmf_usb_ctl_complete(struct brcmf_usbdev_info *devinfo, int type, int status)
178 {
179
180         if (unlikely(devinfo == NULL))
181                 return;
182
183         if (type == BRCMF_USB_CBCTL_READ) {
184                 if (status == 0)
185                         devinfo->bus_pub.stats.rx_ctlpkts++;
186                 else
187                         devinfo->bus_pub.stats.rx_ctlerrs++;
188         } else if (type == BRCMF_USB_CBCTL_WRITE) {
189                 if (status == 0)
190                         devinfo->bus_pub.stats.tx_ctlpkts++;
191                 else
192                         devinfo->bus_pub.stats.tx_ctlerrs++;
193         }
194
195         devinfo->ctl_urb_status = status;
196         devinfo->ctl_completed = true;
197         brcmf_usb_ioctl_resp_wake(devinfo);
198 }
199
200 static void
201 brcmf_usb_ctlread_complete(struct urb *urb)
202 {
203         struct brcmf_usbdev_info *devinfo =
204                 (struct brcmf_usbdev_info *)urb->context;
205
206         devinfo->ctl_urb_actual_length = urb->actual_length;
207         brcmf_usb_ctl_complete(devinfo, BRCMF_USB_CBCTL_READ,
208                 urb->status);
209 }
210
211 static void
212 brcmf_usb_ctlwrite_complete(struct urb *urb)
213 {
214         struct brcmf_usbdev_info *devinfo =
215                 (struct brcmf_usbdev_info *)urb->context;
216
217         brcmf_usb_ctl_complete(devinfo, BRCMF_USB_CBCTL_WRITE,
218                 urb->status);
219 }
220
221 static int brcmf_usb_pnp(struct brcmf_usbdev_info *devinfo, uint state)
222 {
223         return 0;
224 }
225
226 static int
227 brcmf_usb_send_ctl(struct brcmf_usbdev_info *devinfo, u8 *buf, int len)
228 {
229         int ret;
230         u16 size;
231
232         if (devinfo == NULL || buf == NULL ||
233             len == 0 || devinfo->ctl_urb == NULL)
234                 return -EINVAL;
235
236         /* If the USB/HSIC bus in sleep state, wake it up */
237         if (devinfo->suspend_state == USBOS_SUSPEND_STATE_SUSPENDED)
238                 if (brcmf_usb_pnp(devinfo, BCMFMAC_USB_PNP_RESUME) != 0) {
239                         brcmf_dbg(ERROR, "Could not Resume the bus!\n");
240                         return -EIO;
241                 }
242
243         devinfo->activity = true;
244         size = len;
245         devinfo->ctl_write.wLength = cpu_to_le16p(&size);
246         devinfo->ctl_urb->transfer_buffer_length = size;
247         devinfo->ctl_urb_status = 0;
248         devinfo->ctl_urb_actual_length = 0;
249
250         usb_fill_control_urb(devinfo->ctl_urb,
251                 devinfo->usbdev,
252                 devinfo->ctl_out_pipe,
253                 (unsigned char *) &devinfo->ctl_write,
254                 buf, size,
255                 (usb_complete_t)brcmf_usb_ctlwrite_complete,
256                 devinfo);
257
258         ret = usb_submit_urb(devinfo->ctl_urb, GFP_ATOMIC);
259         if (ret < 0)
260                 brcmf_dbg(ERROR, "usb_submit_urb failed %d\n", ret);
261
262         return ret;
263 }
264
265 static int
266 brcmf_usb_recv_ctl(struct brcmf_usbdev_info *devinfo, u8 *buf, int len)
267 {
268         int ret;
269         u16 size;
270
271         if ((devinfo == NULL) || (buf == NULL) || (len == 0)
272                 || (devinfo->ctl_urb == NULL))
273                 return -EINVAL;
274
275         size = len;
276         devinfo->ctl_read.wLength = cpu_to_le16p(&size);
277         devinfo->ctl_urb->transfer_buffer_length = size;
278
279         devinfo->ctl_read.bRequestType = USB_DIR_IN
280                 | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
281         devinfo->ctl_read.bRequest = 1;
282
283         usb_fill_control_urb(devinfo->ctl_urb,
284                 devinfo->usbdev,
285                 devinfo->ctl_in_pipe,
286                 (unsigned char *) &devinfo->ctl_read,
287                 buf, size,
288                 (usb_complete_t)brcmf_usb_ctlread_complete,
289                 devinfo);
290
291         ret = usb_submit_urb(devinfo->ctl_urb, GFP_ATOMIC);
292         if (ret < 0)
293                 brcmf_dbg(ERROR, "usb_submit_urb failed %d\n", ret);
294
295         return ret;
296 }
297
298 static int brcmf_usb_tx_ctlpkt(struct device *dev, u8 *buf, u32 len)
299 {
300         int err = 0;
301         int timeout = 0;
302         struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
303
304         if (devinfo->bus_pub.state != BCMFMAC_USB_STATE_UP) {
305                 /* TODO: handle suspend/resume */
306                 return -EIO;
307         }
308
309         if (test_and_set_bit(0, &devinfo->ctl_op))
310                 return -EIO;
311
312         devinfo->ctl_completed = false;
313         err = brcmf_usb_send_ctl(devinfo, buf, len);
314         if (err) {
315                 brcmf_dbg(ERROR, "fail %d bytes: %d\n", err, len);
316                 clear_bit(0, &devinfo->ctl_op);
317                 return err;
318         }
319         timeout = brcmf_usb_ioctl_resp_wait(devinfo);
320         clear_bit(0, &devinfo->ctl_op);
321         if (!timeout) {
322                 brcmf_dbg(ERROR, "Txctl wait timed out\n");
323                 err = -EIO;
324         }
325         return err;
326 }
327
328 static int brcmf_usb_rx_ctlpkt(struct device *dev, u8 *buf, u32 len)
329 {
330         int err = 0;
331         int timeout = 0;
332         struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
333
334         if (devinfo->bus_pub.state != BCMFMAC_USB_STATE_UP) {
335                 /* TODO: handle suspend/resume */
336                 return -EIO;
337         }
338         if (test_and_set_bit(0, &devinfo->ctl_op))
339                 return -EIO;
340
341         devinfo->ctl_completed = false;
342         err = brcmf_usb_recv_ctl(devinfo, buf, len);
343         if (err) {
344                 brcmf_dbg(ERROR, "fail %d bytes: %d\n", err, len);
345                 clear_bit(0, &devinfo->ctl_op);
346                 return err;
347         }
348         timeout = brcmf_usb_ioctl_resp_wait(devinfo);
349         err = devinfo->ctl_urb_status;
350         clear_bit(0, &devinfo->ctl_op);
351         if (!timeout) {
352                 brcmf_dbg(ERROR, "rxctl wait timed out\n");
353                 err = -EIO;
354         }
355         if (!err)
356                 return devinfo->ctl_urb_actual_length;
357         else
358                 return err;
359 }
360
361 static struct brcmf_usbreq *brcmf_usb_deq(struct brcmf_usbdev_info *devinfo,
362                                           struct list_head *q, int *counter)
363 {
364         unsigned long flags;
365         struct brcmf_usbreq  *req;
366         spin_lock_irqsave(&devinfo->qlock, flags);
367         if (list_empty(q)) {
368                 spin_unlock_irqrestore(&devinfo->qlock, flags);
369                 return NULL;
370         }
371         req = list_entry(q->next, struct brcmf_usbreq, list);
372         list_del_init(q->next);
373         if (counter)
374                 (*counter)--;
375         spin_unlock_irqrestore(&devinfo->qlock, flags);
376         return req;
377
378 }
379
380 static void brcmf_usb_enq(struct brcmf_usbdev_info *devinfo,
381                           struct list_head *q, struct brcmf_usbreq *req,
382                           int *counter)
383 {
384         unsigned long flags;
385         spin_lock_irqsave(&devinfo->qlock, flags);
386         list_add_tail(&req->list, q);
387         if (counter)
388                 (*counter)++;
389         spin_unlock_irqrestore(&devinfo->qlock, flags);
390 }
391
392 static struct brcmf_usbreq *
393 brcmf_usbdev_qinit(struct list_head *q, int qsize)
394 {
395         int i;
396         struct brcmf_usbreq *req, *reqs;
397
398         reqs = kzalloc(sizeof(struct brcmf_usbreq) * qsize, GFP_ATOMIC);
399         if (reqs == NULL) {
400                 brcmf_dbg(ERROR, "fail to allocate memory!\n");
401                 return NULL;
402         }
403         req = reqs;
404
405         for (i = 0; i < qsize; i++) {
406                 req->urb = usb_alloc_urb(0, GFP_ATOMIC);
407                 if (!req->urb)
408                         goto fail;
409
410                 INIT_LIST_HEAD(&req->list);
411                 list_add_tail(&req->list, q);
412                 req++;
413         }
414         return reqs;
415 fail:
416         brcmf_dbg(ERROR, "fail!\n");
417         while (!list_empty(q)) {
418                 req = list_entry(q->next, struct brcmf_usbreq, list);
419                 if (req && req->urb)
420                         usb_free_urb(req->urb);
421                 list_del(q->next);
422         }
423         return NULL;
424
425 }
426
427 static void brcmf_usb_free_q(struct list_head *q, bool pending)
428 {
429         struct brcmf_usbreq *req, *next;
430         int i = 0;
431         list_for_each_entry_safe(req, next, q, list) {
432                 if (!req->urb) {
433                         brcmf_dbg(ERROR, "bad req\n");
434                         break;
435                 }
436                 i++;
437                 if (pending) {
438                         usb_kill_urb(req->urb);
439                 } else {
440                         usb_free_urb(req->urb);
441                         list_del_init(&req->list);
442                 }
443         }
444 }
445
446 static void brcmf_usb_del_fromq(struct brcmf_usbdev_info *devinfo,
447                                 struct brcmf_usbreq *req)
448 {
449         unsigned long flags;
450
451         spin_lock_irqsave(&devinfo->qlock, flags);
452         list_del_init(&req->list);
453         spin_unlock_irqrestore(&devinfo->qlock, flags);
454 }
455
456
457 static void brcmf_usb_tx_complete(struct urb *urb)
458 {
459         struct brcmf_usbreq *req = (struct brcmf_usbreq *)urb->context;
460         struct brcmf_usbdev_info *devinfo = req->devinfo;
461
462         brcmf_usb_del_fromq(devinfo, req);
463         if (urb->status == 0)
464                 devinfo->bus_pub.bus->dstats.tx_packets++;
465         else
466                 devinfo->bus_pub.bus->dstats.tx_errors++;
467
468         brcmf_txcomplete(devinfo->dev, req->skb, urb->status == 0);
469
470         brcmu_pkt_buf_free_skb(req->skb);
471         req->skb = NULL;
472         brcmf_usb_enq(devinfo, &devinfo->tx_freeq, req, &devinfo->tx_freecount);
473         if (devinfo->tx_freecount > devinfo->tx_high_watermark &&
474                 devinfo->tx_flowblock) {
475                 brcmf_txflowblock(devinfo->dev, false);
476                 devinfo->tx_flowblock = false;
477         }
478 }
479
480 static void brcmf_usb_rx_complete(struct urb *urb)
481 {
482         struct brcmf_usbreq  *req = (struct brcmf_usbreq *)urb->context;
483         struct brcmf_usbdev_info *devinfo = req->devinfo;
484         struct sk_buff *skb;
485         int ifidx = 0;
486
487         brcmf_usb_del_fromq(devinfo, req);
488         skb = req->skb;
489         req->skb = NULL;
490
491         if (urb->status == 0) {
492                 devinfo->bus_pub.bus->dstats.rx_packets++;
493         } else {
494                 devinfo->bus_pub.bus->dstats.rx_errors++;
495                 brcmu_pkt_buf_free_skb(skb);
496                 brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
497                 return;
498         }
499
500         if (devinfo->bus_pub.state == BCMFMAC_USB_STATE_UP) {
501                 skb_put(skb, urb->actual_length);
502                 if (brcmf_proto_hdrpull(devinfo->dev, &ifidx, skb) != 0) {
503                         brcmf_dbg(ERROR, "rx protocol error\n");
504                         brcmu_pkt_buf_free_skb(skb);
505                         devinfo->bus_pub.bus->dstats.rx_errors++;
506                 } else
507                         brcmf_rx_packet(devinfo->dev, ifidx, skb);
508                 brcmf_usb_rx_refill(devinfo, req);
509         } else {
510                 brcmu_pkt_buf_free_skb(skb);
511                 brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
512         }
513         return;
514
515 }
516
517 static void brcmf_usb_rx_refill(struct brcmf_usbdev_info *devinfo,
518                                 struct brcmf_usbreq  *req)
519 {
520         struct sk_buff *skb;
521         int ret;
522
523         if (!req || !devinfo)
524                 return;
525
526         skb = dev_alloc_skb(devinfo->bus_pub.bus_mtu);
527         if (!skb) {
528                 brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
529                 return;
530         }
531         req->skb = skb;
532
533         usb_fill_bulk_urb(req->urb, devinfo->usbdev, devinfo->rx_pipe,
534                           skb->data, skb_tailroom(skb), brcmf_usb_rx_complete,
535                           req);
536         req->devinfo = devinfo;
537         brcmf_usb_enq(devinfo, &devinfo->rx_postq, req, NULL);
538
539         ret = usb_submit_urb(req->urb, GFP_ATOMIC);
540         if (ret) {
541                 brcmf_usb_del_fromq(devinfo, req);
542                 brcmu_pkt_buf_free_skb(req->skb);
543                 req->skb = NULL;
544                 brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
545         }
546         return;
547 }
548
549 static void brcmf_usb_rx_fill_all(struct brcmf_usbdev_info *devinfo)
550 {
551         struct brcmf_usbreq *req;
552
553         if (devinfo->bus_pub.state != BCMFMAC_USB_STATE_UP) {
554                 brcmf_dbg(ERROR, "bus is not up\n");
555                 return;
556         }
557         while ((req = brcmf_usb_deq(devinfo, &devinfo->rx_freeq, NULL)) != NULL)
558                 brcmf_usb_rx_refill(devinfo, req);
559 }
560
561 static void
562 brcmf_usb_state_change(struct brcmf_usbdev_info *devinfo, int state)
563 {
564         struct brcmf_bus *bcmf_bus = devinfo->bus_pub.bus;
565         int old_state;
566
567
568         if (devinfo->bus_pub.state == state)
569                 return;
570
571         old_state = devinfo->bus_pub.state;
572         brcmf_dbg(TRACE, "dbus state change from %d to to %d\n",
573                   old_state, state);
574
575         /* Don't update state if it's PnP firmware re-download */
576         if (state != BCMFMAC_USB_STATE_PNP_FWDL) /* TODO */
577                 devinfo->bus_pub.state = state;
578
579         if ((old_state  == BCMFMAC_USB_STATE_SLEEP)
580                 && (state == BCMFMAC_USB_STATE_UP)) {
581                 brcmf_usb_rx_fill_all(devinfo);
582         }
583
584         /* update state of upper layer */
585         if (state == BCMFMAC_USB_STATE_DOWN) {
586                 brcmf_dbg(INFO, "DBUS is down\n");
587                 bcmf_bus->state = BRCMF_BUS_DOWN;
588         } else {
589                 brcmf_dbg(INFO, "DBUS current state=%d\n", state);
590         }
591 }
592
593 static void
594 brcmf_usb_intr_complete(struct urb *urb)
595 {
596         struct brcmf_usbdev_info *devinfo =
597                         (struct brcmf_usbdev_info *)urb->context;
598         bool killed;
599
600         if (devinfo == NULL)
601                 return;
602
603         if (unlikely(urb->status)) {
604                 if (devinfo->suspend_state ==
605                         USBOS_SUSPEND_STATE_SUSPEND_PENDING)
606                         killed = true;
607
608                 if ((urb->status == -ENOENT && (!killed))
609                         || urb->status == -ESHUTDOWN ||
610                         urb->status == -ENODEV) {
611                         brcmf_usb_state_change(devinfo, BCMFMAC_USB_STATE_DOWN);
612                 }
613         }
614
615         if (devinfo->bus_pub.state == BCMFMAC_USB_STATE_DOWN) {
616                 brcmf_dbg(ERROR, "intr cb when DBUS down, ignoring\n");
617                 return;
618         }
619
620         if (devinfo->bus_pub.state == BCMFMAC_USB_STATE_UP)
621                 usb_submit_urb(devinfo->intr_urb, GFP_ATOMIC);
622 }
623
624 static int brcmf_usb_tx(struct device *dev, struct sk_buff *skb)
625 {
626         struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
627         struct brcmf_usbreq  *req;
628         int ret;
629
630         if (devinfo->bus_pub.state != BCMFMAC_USB_STATE_UP) {
631                 /* TODO: handle suspend/resume */
632                 return -EIO;
633         }
634
635         req = brcmf_usb_deq(devinfo, &devinfo->tx_freeq,
636                                         &devinfo->tx_freecount);
637         if (!req) {
638                 brcmu_pkt_buf_free_skb(skb);
639                 brcmf_dbg(ERROR, "no req to send\n");
640                 return -ENOMEM;
641         }
642
643         req->skb = skb;
644         req->devinfo = devinfo;
645         usb_fill_bulk_urb(req->urb, devinfo->usbdev, devinfo->tx_pipe,
646                           skb->data, skb->len, brcmf_usb_tx_complete, req);
647         req->urb->transfer_flags |= URB_ZERO_PACKET;
648         brcmf_usb_enq(devinfo, &devinfo->tx_postq, req, NULL);
649         ret = usb_submit_urb(req->urb, GFP_ATOMIC);
650         if (ret) {
651                 brcmf_dbg(ERROR, "brcmf_usb_tx usb_submit_urb FAILED\n");
652                 brcmf_usb_del_fromq(devinfo, req);
653                 brcmu_pkt_buf_free_skb(req->skb);
654                 req->skb = NULL;
655                 brcmf_usb_enq(devinfo, &devinfo->tx_freeq, req,
656                                                 &devinfo->tx_freecount);
657         } else {
658                 if (devinfo->tx_freecount < devinfo->tx_low_watermark &&
659                         !devinfo->tx_flowblock) {
660                         brcmf_txflowblock(dev, true);
661                         devinfo->tx_flowblock = true;
662                 }
663         }
664
665         return ret;
666 }
667
668
669 static int brcmf_usb_up(struct device *dev)
670 {
671         struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
672         u16 ifnum;
673
674         if (devinfo->bus_pub.state == BCMFMAC_USB_STATE_UP)
675                 return 0;
676
677         /* If the USB/HSIC bus in sleep state, wake it up */
678         if (devinfo->suspend_state == USBOS_SUSPEND_STATE_SUSPENDED) {
679                 if (brcmf_usb_pnp(devinfo, BCMFMAC_USB_PNP_RESUME) != 0) {
680                         brcmf_dbg(ERROR, "Could not Resume the bus!\n");
681                         return -EIO;
682                 }
683         }
684         devinfo->activity = true;
685
686         /* Success, indicate devinfo is fully up */
687         brcmf_usb_state_change(devinfo, BCMFMAC_USB_STATE_UP);
688
689         if (devinfo->intr_urb) {
690                 int ret;
691
692                 usb_fill_int_urb(devinfo->intr_urb, devinfo->usbdev,
693                         devinfo->intr_pipe,
694                         &devinfo->intr,
695                         devinfo->intr_size,
696                         (usb_complete_t)brcmf_usb_intr_complete,
697                         devinfo,
698                         devinfo->interval);
699
700                 ret = usb_submit_urb(devinfo->intr_urb, GFP_ATOMIC);
701                 if (ret) {
702                         brcmf_dbg(ERROR, "USB_SUBMIT_URB failed with status %d\n",
703                                   ret);
704                         return -EINVAL;
705                 }
706         }
707
708         if (devinfo->ctl_urb) {
709                 devinfo->ctl_in_pipe = usb_rcvctrlpipe(devinfo->usbdev, 0);
710                 devinfo->ctl_out_pipe = usb_sndctrlpipe(devinfo->usbdev, 0);
711
712                 ifnum = IFDESC(devinfo->usbdev, CONTROL_IF).bInterfaceNumber;
713
714                 /* CTL Write */
715                 devinfo->ctl_write.bRequestType =
716                         USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
717                 devinfo->ctl_write.bRequest = 0;
718                 devinfo->ctl_write.wValue = cpu_to_le16(0);
719                 devinfo->ctl_write.wIndex = cpu_to_le16p(&ifnum);
720
721                 /* CTL Read */
722                 devinfo->ctl_read.bRequestType =
723                         USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
724                 devinfo->ctl_read.bRequest = 1;
725                 devinfo->ctl_read.wValue = cpu_to_le16(0);
726                 devinfo->ctl_read.wIndex = cpu_to_le16p(&ifnum);
727         }
728         brcmf_usb_rx_fill_all(devinfo);
729         return 0;
730 }
731
732 static void brcmf_usb_down(struct device *dev)
733 {
734         struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
735
736         if (devinfo == NULL)
737                 return;
738
739         brcmf_dbg(TRACE, "enter\n");
740         if (devinfo->bus_pub.state == BCMFMAC_USB_STATE_DOWN)
741                 return;
742
743         brcmf_usb_state_change(devinfo, BCMFMAC_USB_STATE_DOWN);
744         if (devinfo->intr_urb)
745                 usb_kill_urb(devinfo->intr_urb);
746
747         if (devinfo->ctl_urb)
748                 usb_kill_urb(devinfo->ctl_urb);
749
750         if (devinfo->bulk_urb)
751                 usb_kill_urb(devinfo->bulk_urb);
752         brcmf_usb_free_q(&devinfo->tx_postq, true);
753
754         brcmf_usb_free_q(&devinfo->rx_postq, true);
755 }
756
757 static int
758 brcmf_usb_sync_wait(struct brcmf_usbdev_info *devinfo, u16 time)
759 {
760         int ret;
761         int err = 0;
762         int ms = time;
763
764         ret = wait_event_interruptible_timeout(devinfo->wait,
765                 devinfo->waitdone == true, (ms * HZ / 1000));
766
767         if ((devinfo->waitdone == false) || (devinfo->sync_urb_status)) {
768                 brcmf_dbg(ERROR, "timeout(%d) or urb err=%d\n",
769                           ret, devinfo->sync_urb_status);
770                 err = -EINVAL;
771         }
772         devinfo->waitdone = false;
773         return err;
774 }
775
776 static void
777 brcmf_usb_sync_complete(struct urb *urb)
778 {
779         struct brcmf_usbdev_info *devinfo =
780                         (struct brcmf_usbdev_info *)urb->context;
781
782         devinfo->waitdone = true;
783         wake_up_interruptible(&devinfo->wait);
784         devinfo->sync_urb_status = urb->status;
785 }
786
787 static bool brcmf_usb_dl_cmd(struct brcmf_usbdev_info *devinfo, u8 cmd,
788                              void *buffer, int buflen)
789 {
790         int ret = 0;
791         char *tmpbuf;
792         u16 size;
793
794         if ((!devinfo) || (devinfo->ctl_urb == NULL))
795                 return false;
796
797         tmpbuf = kmalloc(buflen, GFP_ATOMIC);
798         if (!tmpbuf)
799                 return false;
800
801         size = buflen;
802         devinfo->ctl_urb->transfer_buffer_length = size;
803
804         devinfo->ctl_read.wLength = cpu_to_le16p(&size);
805         devinfo->ctl_read.bRequestType = USB_DIR_IN | USB_TYPE_VENDOR |
806                 USB_RECIP_INTERFACE;
807         devinfo->ctl_read.bRequest = cmd;
808
809         usb_fill_control_urb(devinfo->ctl_urb,
810                 devinfo->usbdev,
811                 usb_rcvctrlpipe(devinfo->usbdev, 0),
812                 (unsigned char *) &devinfo->ctl_read,
813                 (void *) tmpbuf, size,
814                 (usb_complete_t)brcmf_usb_sync_complete, devinfo);
815
816         ret = usb_submit_urb(devinfo->ctl_urb, GFP_ATOMIC);
817         if (ret < 0) {
818                 brcmf_dbg(ERROR, "usb_submit_urb failed %d\n", ret);
819                 kfree(tmpbuf);
820                 return false;
821         }
822
823         ret = brcmf_usb_sync_wait(devinfo, BRCMF_USB_SYNC_TIMEOUT);
824         memcpy(buffer, tmpbuf, buflen);
825         kfree(tmpbuf);
826
827         return (ret == 0);
828 }
829
830 static bool
831 brcmf_usb_dlneeded(struct brcmf_usbdev_info *devinfo)
832 {
833         struct bootrom_id_le id;
834         u32 chipid, chiprev;
835
836         brcmf_dbg(TRACE, "enter\n");
837
838         if (devinfo == NULL)
839                 return false;
840
841         /* Check if firmware downloaded already by querying runtime ID */
842         id.chip = cpu_to_le32(0xDEAD);
843         brcmf_usb_dl_cmd(devinfo, DL_GETVER, &id,
844                 sizeof(struct bootrom_id_le));
845
846         chipid = le32_to_cpu(id.chip);
847         chiprev = le32_to_cpu(id.chiprev);
848
849         if ((chipid & 0x4300) == 0x4300)
850                 brcmf_dbg(INFO, "chip %x rev 0x%x\n", chipid, chiprev);
851         else
852                 brcmf_dbg(INFO, "chip %d rev 0x%x\n", chipid, chiprev);
853         if (chipid == BRCMF_POSTBOOT_ID) {
854                 brcmf_dbg(INFO, "firmware already downloaded\n");
855                 brcmf_usb_dl_cmd(devinfo, DL_RESETCFG, &id,
856                         sizeof(struct bootrom_id_le));
857                 return false;
858         } else {
859                 devinfo->bus_pub.devid = chipid;
860                 devinfo->bus_pub.chiprev = chiprev;
861         }
862         return true;
863 }
864
865 static int
866 brcmf_usb_resetcfg(struct brcmf_usbdev_info *devinfo)
867 {
868         struct bootrom_id_le id;
869         u16 wait = 0, wait_time;
870
871         brcmf_dbg(TRACE, "enter\n");
872
873         if (devinfo == NULL)
874                 return -EINVAL;
875
876         /* Give dongle chance to boot */
877         wait_time = BRCMF_USB_DLIMAGE_SPINWAIT;
878         while (wait < BRCMF_USB_DLIMAGE_LIMIT) {
879                 mdelay(wait_time);
880                 wait += wait_time;
881                 id.chip = cpu_to_le32(0xDEAD);       /* Get the ID */
882                 brcmf_usb_dl_cmd(devinfo, DL_GETVER, &id,
883                         sizeof(struct bootrom_id_le));
884                 if (id.chip == cpu_to_le32(BRCMF_POSTBOOT_ID))
885                         break;
886         }
887
888         if (id.chip == cpu_to_le32(BRCMF_POSTBOOT_ID)) {
889                 brcmf_dbg(INFO, "download done %d ms postboot chip 0x%x/rev 0x%x\n",
890                           wait, le32_to_cpu(id.chip), le32_to_cpu(id.chiprev));
891
892                 brcmf_usb_dl_cmd(devinfo, DL_RESETCFG, &id,
893                         sizeof(struct bootrom_id_le));
894
895                 /* XXX this wait may not be necessary */
896                 mdelay(BRCMF_USB_RESETCFG_SPINWAIT);
897                 return 0;
898         } else {
899                 brcmf_dbg(ERROR, "Cannot talk to Dongle. Firmware is not UP, %d ms\n",
900                           wait);
901                 return -EINVAL;
902         }
903 }
904
905
906 static int
907 brcmf_usb_dl_send_bulk(struct brcmf_usbdev_info *devinfo, void *buffer, int len)
908 {
909         int ret;
910
911         if ((devinfo == NULL) || (devinfo->bulk_urb == NULL))
912                 return -EINVAL;
913
914         /* Prepare the URB */
915         usb_fill_bulk_urb(devinfo->bulk_urb, devinfo->usbdev,
916                           devinfo->tx_pipe, buffer, len,
917                           (usb_complete_t)brcmf_usb_sync_complete, devinfo);
918
919         devinfo->bulk_urb->transfer_flags |= URB_ZERO_PACKET;
920
921         ret = usb_submit_urb(devinfo->bulk_urb, GFP_ATOMIC);
922         if (ret) {
923                 brcmf_dbg(ERROR, "usb_submit_urb failed %d\n", ret);
924                 return ret;
925         }
926         ret = brcmf_usb_sync_wait(devinfo, BRCMF_USB_SYNC_TIMEOUT);
927         return ret;
928 }
929
930 static int
931 brcmf_usb_dl_writeimage(struct brcmf_usbdev_info *devinfo, u8 *fw, int fwlen)
932 {
933         unsigned int sendlen, sent, dllen;
934         char *bulkchunk = NULL, *dlpos;
935         struct rdl_state_le state;
936         u32 rdlstate, rdlbytes;
937         int err = 0;
938         brcmf_dbg(TRACE, "fw %p, len %d\n", fw, fwlen);
939
940         bulkchunk = kmalloc(RDL_CHUNK, GFP_ATOMIC);
941         if (bulkchunk == NULL) {
942                 err = -ENOMEM;
943                 goto fail;
944         }
945
946         /* 1) Prepare USB boot loader for runtime image */
947         brcmf_usb_dl_cmd(devinfo, DL_START, &state,
948                          sizeof(struct rdl_state_le));
949
950         rdlstate = le32_to_cpu(state.state);
951         rdlbytes = le32_to_cpu(state.bytes);
952
953         /* 2) Check we are in the Waiting state */
954         if (rdlstate != DL_WAITING) {
955                 brcmf_dbg(ERROR, "Failed to DL_START\n");
956                 err = -EINVAL;
957                 goto fail;
958         }
959         sent = 0;
960         dlpos = fw;
961         dllen = fwlen;
962
963         /* Get chip id and rev */
964         while (rdlbytes != dllen) {
965                 /* Wait until the usb device reports it received all
966                  * the bytes we sent */
967                 if ((rdlbytes == sent) && (rdlbytes != dllen)) {
968                         if ((dllen-sent) < RDL_CHUNK)
969                                 sendlen = dllen-sent;
970                         else
971                                 sendlen = RDL_CHUNK;
972
973                         /* simply avoid having to send a ZLP by ensuring we
974                          * never have an even
975                          * multiple of 64
976                          */
977                         if (!(sendlen % 64))
978                                 sendlen -= 4;
979
980                         /* send data */
981                         memcpy(bulkchunk, dlpos, sendlen);
982                         if (brcmf_usb_dl_send_bulk(devinfo, bulkchunk,
983                                                    sendlen)) {
984                                 brcmf_dbg(ERROR, "send_bulk failed\n");
985                                 err = -EINVAL;
986                                 goto fail;
987                         }
988
989                         dlpos += sendlen;
990                         sent += sendlen;
991                 }
992                 if (!brcmf_usb_dl_cmd(devinfo, DL_GETSTATE, &state,
993                                       sizeof(struct rdl_state_le))) {
994                         brcmf_dbg(ERROR, "DL_GETSTATE Failed xxxx\n");
995                         err = -EINVAL;
996                         goto fail;
997                 }
998
999                 rdlstate = le32_to_cpu(state.state);
1000                 rdlbytes = le32_to_cpu(state.bytes);
1001
1002                 /* restart if an error is reported */
1003                 if (rdlstate == DL_BAD_HDR || rdlstate == DL_BAD_CRC) {
1004                         brcmf_dbg(ERROR, "Bad Hdr or Bad CRC state %d\n",
1005                                   rdlstate);
1006                         err = -EINVAL;
1007                         goto fail;
1008                 }
1009         }
1010
1011 fail:
1012         kfree(bulkchunk);
1013         brcmf_dbg(TRACE, "err=%d\n", err);
1014         return err;
1015 }
1016
1017 static int brcmf_usb_dlstart(struct brcmf_usbdev_info *devinfo, u8 *fw, int len)
1018 {
1019         int err;
1020
1021         brcmf_dbg(TRACE, "enter\n");
1022
1023         if (devinfo == NULL)
1024                 return -EINVAL;
1025
1026         if (devinfo->bus_pub.devid == 0xDEAD)
1027                 return -EINVAL;
1028
1029         err = brcmf_usb_dl_writeimage(devinfo, fw, len);
1030         if (err == 0)
1031                 devinfo->bus_pub.state = BCMFMAC_USB_STATE_DL_DONE;
1032         else
1033                 devinfo->bus_pub.state = BCMFMAC_USB_STATE_DL_PENDING;
1034         brcmf_dbg(TRACE, "exit: err=%d\n", err);
1035
1036         return err;
1037 }
1038
1039 static int brcmf_usb_dlrun(struct brcmf_usbdev_info *devinfo)
1040 {
1041         struct rdl_state_le state;
1042
1043         brcmf_dbg(TRACE, "enter\n");
1044         if (!devinfo)
1045                 return -EINVAL;
1046
1047         if (devinfo->bus_pub.devid == 0xDEAD)
1048                 return -EINVAL;
1049
1050         /* Check we are runnable */
1051         brcmf_usb_dl_cmd(devinfo, DL_GETSTATE, &state,
1052                 sizeof(struct rdl_state_le));
1053
1054         /* Start the image */
1055         if (state.state == cpu_to_le32(DL_RUNNABLE)) {
1056                 if (!brcmf_usb_dl_cmd(devinfo, DL_GO, &state,
1057                         sizeof(struct rdl_state_le)))
1058                         return -ENODEV;
1059                 if (brcmf_usb_resetcfg(devinfo))
1060                         return -ENODEV;
1061                 /* The Dongle may go for re-enumeration. */
1062         } else {
1063                 brcmf_dbg(ERROR, "Dongle not runnable\n");
1064                 return -EINVAL;
1065         }
1066         brcmf_dbg(TRACE, "exit\n");
1067         return 0;
1068 }
1069
1070 static bool brcmf_usb_chip_support(int chipid, int chiprev)
1071 {
1072         switch(chipid) {
1073         case 43143:
1074                 return true;
1075         case 43235:
1076         case 43236:
1077         case 43238:
1078                 return (chiprev == 3);
1079         case 43242:
1080                 return true;
1081         default:
1082                 break;
1083         }
1084         return false;
1085 }
1086
1087 static int
1088 brcmf_usb_fw_download(struct brcmf_usbdev_info *devinfo)
1089 {
1090         int devid, chiprev;
1091         int err;
1092
1093         brcmf_dbg(TRACE, "enter\n");
1094         if (devinfo == NULL)
1095                 return -ENODEV;
1096
1097         devid = devinfo->bus_pub.devid;
1098         chiprev = devinfo->bus_pub.chiprev;
1099
1100         if (!brcmf_usb_chip_support(devid, chiprev)) {
1101                 brcmf_dbg(ERROR, "unsupported chip %d rev %d\n",
1102                           devid, chiprev);
1103                 return -EINVAL;
1104         }
1105
1106         if (!devinfo->image) {
1107                 brcmf_dbg(ERROR, "No firmware!\n");
1108                 return -ENOENT;
1109         }
1110
1111         err = brcmf_usb_dlstart(devinfo,
1112                 devinfo->image, devinfo->image_len);
1113         if (err == 0)
1114                 err = brcmf_usb_dlrun(devinfo);
1115         return err;
1116 }
1117
1118
1119 static void brcmf_usb_detach(struct brcmf_usbdev_info *devinfo)
1120 {
1121         brcmf_dbg(TRACE, "devinfo %p\n", devinfo);
1122
1123         /* free the URBS */
1124         brcmf_usb_free_q(&devinfo->rx_freeq, false);
1125         brcmf_usb_free_q(&devinfo->tx_freeq, false);
1126
1127         usb_free_urb(devinfo->intr_urb);
1128         usb_free_urb(devinfo->ctl_urb);
1129         usb_free_urb(devinfo->bulk_urb);
1130
1131         kfree(devinfo->tx_reqs);
1132         kfree(devinfo->rx_reqs);
1133 }
1134
1135 #define TRX_MAGIC       0x30524448      /* "HDR0" */
1136 #define TRX_VERSION     1               /* Version 1 */
1137 #define TRX_MAX_LEN     0x3B0000        /* Max length */
1138 #define TRX_NO_HEADER   1               /* Do not write TRX header */
1139 #define TRX_MAX_OFFSET  3               /* Max number of individual files */
1140 #define TRX_UNCOMP_IMAGE        0x20    /* Trx contains uncompressed image */
1141
1142 struct trx_header_le {
1143         __le32 magic;           /* "HDR0" */
1144         __le32 len;             /* Length of file including header */
1145         __le32 crc32;           /* CRC from flag_version to end of file */
1146         __le32 flag_version;    /* 0:15 flags, 16:31 version */
1147         __le32 offsets[TRX_MAX_OFFSET]; /* Offsets of partitions from start of
1148                                          * header */
1149 };
1150
1151 static int check_file(const u8 *headers)
1152 {
1153         struct trx_header_le *trx;
1154         int actual_len = -1;
1155
1156         /* Extract trx header */
1157         trx = (struct trx_header_le *) headers;
1158         if (trx->magic != cpu_to_le32(TRX_MAGIC))
1159                 return -1;
1160
1161         headers += sizeof(struct trx_header_le);
1162
1163         if (le32_to_cpu(trx->flag_version) & TRX_UNCOMP_IMAGE) {
1164                 actual_len = le32_to_cpu(trx->offsets[TRX_OFFSETS_DLFWLEN_IDX]);
1165                 return actual_len + sizeof(struct trx_header_le);
1166         }
1167         return -1;
1168 }
1169
1170 static int brcmf_usb_get_fw(struct brcmf_usbdev_info *devinfo)
1171 {
1172         s8 *fwname;
1173         const struct firmware *fw;
1174         struct brcmf_usb_image *fw_image;
1175         int err;
1176
1177         switch (devinfo->bus_pub.devid) {
1178         case 43143:
1179                 fwname = BRCMF_USB_43143_FW_NAME;
1180                 break;
1181         case 43235:
1182         case 43236:
1183         case 43238:
1184                 fwname = BRCMF_USB_43236_FW_NAME;
1185                 break;
1186         case 43242:
1187                 fwname = BRCMF_USB_43242_FW_NAME;
1188                 break;
1189         default:
1190                 return -EINVAL;
1191                 break;
1192         }
1193
1194         list_for_each_entry(fw_image, &fw_image_list, list) {
1195                 if (fw_image->fwname == fwname) {
1196                         devinfo->image = fw_image->image;
1197                         devinfo->image_len = fw_image->image_len;
1198                         return 0;
1199                 }
1200         }
1201         /* fw image not yet loaded. Load it now and add to list */
1202         err = request_firmware(&fw, fwname, devinfo->dev);
1203         if (!fw) {
1204                 brcmf_dbg(ERROR, "fail to request firmware %s\n", fwname);
1205                 return err;
1206         }
1207         if (check_file(fw->data) < 0) {
1208                 brcmf_dbg(ERROR, "invalid firmware %s\n", fwname);
1209                 return -EINVAL;
1210         }
1211
1212         fw_image = kzalloc(sizeof(*fw_image), GFP_ATOMIC);
1213         if (!fw_image)
1214                 return -ENOMEM;
1215         INIT_LIST_HEAD(&fw_image->list);
1216         list_add_tail(&fw_image->list, &fw_image_list);
1217         fw_image->fwname = fwname;
1218         fw_image->image = vmalloc(fw->size);
1219         if (!fw_image->image)
1220                 return -ENOMEM;
1221
1222         memcpy(fw_image->image, fw->data, fw->size);
1223         fw_image->image_len = fw->size;
1224
1225         release_firmware(fw);
1226
1227         devinfo->image = fw_image->image;
1228         devinfo->image_len = fw_image->image_len;
1229
1230         return 0;
1231 }
1232
1233
1234 static
1235 struct brcmf_usbdev *brcmf_usb_attach(struct brcmf_usbdev_info *devinfo,
1236                                       int nrxq, int ntxq)
1237 {
1238         devinfo->bus_pub.nrxq = nrxq;
1239         devinfo->rx_low_watermark = nrxq / 2;
1240         devinfo->bus_pub.devinfo = devinfo;
1241         devinfo->bus_pub.ntxq = ntxq;
1242
1243         /* flow control when too many tx urbs posted */
1244         devinfo->tx_low_watermark = ntxq / 4;
1245         devinfo->tx_high_watermark = devinfo->tx_low_watermark * 3;
1246         devinfo->bus_pub.bus_mtu = BRCMF_USB_MAX_PKT_SIZE;
1247
1248         /* Initialize other structure content */
1249         init_waitqueue_head(&devinfo->ioctl_resp_wait);
1250
1251         /* Initialize the spinlocks */
1252         spin_lock_init(&devinfo->qlock);
1253
1254         INIT_LIST_HEAD(&devinfo->rx_freeq);
1255         INIT_LIST_HEAD(&devinfo->rx_postq);
1256
1257         INIT_LIST_HEAD(&devinfo->tx_freeq);
1258         INIT_LIST_HEAD(&devinfo->tx_postq);
1259
1260         devinfo->tx_flowblock = false;
1261
1262         devinfo->rx_reqs = brcmf_usbdev_qinit(&devinfo->rx_freeq, nrxq);
1263         if (!devinfo->rx_reqs)
1264                 goto error;
1265
1266         devinfo->tx_reqs = brcmf_usbdev_qinit(&devinfo->tx_freeq, ntxq);
1267         if (!devinfo->tx_reqs)
1268                 goto error;
1269         devinfo->tx_freecount = ntxq;
1270
1271         devinfo->intr_urb = usb_alloc_urb(0, GFP_ATOMIC);
1272         if (!devinfo->intr_urb) {
1273                 brcmf_dbg(ERROR, "usb_alloc_urb (intr) failed\n");
1274                 goto error;
1275         }
1276         devinfo->ctl_urb = usb_alloc_urb(0, GFP_ATOMIC);
1277         if (!devinfo->ctl_urb) {
1278                 brcmf_dbg(ERROR, "usb_alloc_urb (ctl) failed\n");
1279                 goto error;
1280         }
1281         devinfo->bulk_urb = usb_alloc_urb(0, GFP_ATOMIC);
1282         if (!devinfo->bulk_urb) {
1283                 brcmf_dbg(ERROR, "usb_alloc_urb (bulk) failed\n");
1284                 goto error;
1285         }
1286
1287         init_waitqueue_head(&devinfo->wait);
1288         if (!brcmf_usb_dlneeded(devinfo))
1289                 return &devinfo->bus_pub;
1290
1291         brcmf_dbg(TRACE, "start fw downloading\n");
1292         if (brcmf_usb_get_fw(devinfo))
1293                 goto error;
1294
1295         if (brcmf_usb_fw_download(devinfo))
1296                 goto error;
1297
1298         return &devinfo->bus_pub;
1299
1300 error:
1301         brcmf_dbg(ERROR, "failed!\n");
1302         brcmf_usb_detach(devinfo);
1303         return NULL;
1304 }
1305
1306 static int brcmf_usb_probe_cb(struct brcmf_usbdev_info *devinfo,
1307                               const char *desc, u32 bustype, u32 hdrlen)
1308 {
1309         struct brcmf_bus *bus = NULL;
1310         struct brcmf_usbdev *bus_pub = NULL;
1311         int ret;
1312         struct device *dev = devinfo->dev;
1313
1314         bus_pub = brcmf_usb_attach(devinfo, BRCMF_USB_NRXQ, BRCMF_USB_NTXQ);
1315         if (!bus_pub)
1316                 return -ENODEV;
1317
1318         bus = kzalloc(sizeof(struct brcmf_bus), GFP_ATOMIC);
1319         if (!bus) {
1320                 ret = -ENOMEM;
1321                 goto fail;
1322         }
1323
1324         bus_pub->bus = bus;
1325         bus->brcmf_bus_txdata = brcmf_usb_tx;
1326         bus->brcmf_bus_init = brcmf_usb_up;
1327         bus->brcmf_bus_stop = brcmf_usb_down;
1328         bus->brcmf_bus_txctl = brcmf_usb_tx_ctlpkt;
1329         bus->brcmf_bus_rxctl = brcmf_usb_rx_ctlpkt;
1330         bus->type = bustype;
1331         bus->bus_priv.usb = bus_pub;
1332         dev_set_drvdata(dev, bus);
1333
1334         /* Attach to the common driver interface */
1335         ret = brcmf_attach(hdrlen, dev);
1336         if (ret) {
1337                 brcmf_dbg(ERROR, "dhd_attach failed\n");
1338                 goto fail;
1339         }
1340
1341         ret = brcmf_bus_start(dev);
1342         if (ret) {
1343                 brcmf_dbg(ERROR, "dongle is not responding\n");
1344                 brcmf_detach(dev);
1345                 goto fail;
1346         }
1347
1348         return 0;
1349 fail:
1350         /* Release resources in reverse order */
1351         kfree(bus);
1352         brcmf_usb_detach(devinfo);
1353         return ret;
1354 }
1355
1356 static void
1357 brcmf_usb_disconnect_cb(struct brcmf_usbdev_info *devinfo)
1358 {
1359         if (!devinfo)
1360                 return;
1361         brcmf_dbg(TRACE, "enter: bus_pub %p\n", devinfo);
1362
1363         brcmf_detach(devinfo->dev);
1364         kfree(devinfo->bus_pub.bus);
1365         brcmf_usb_detach(devinfo);
1366 }
1367
1368 static int
1369 brcmf_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
1370 {
1371         int ep;
1372         struct usb_endpoint_descriptor *endpoint;
1373         int ret = 0;
1374         struct usb_device *usb = interface_to_usbdev(intf);
1375         int num_of_eps;
1376         u8 endpoint_num;
1377         struct brcmf_usbdev_info *devinfo;
1378
1379         brcmf_dbg(TRACE, "enter\n");
1380
1381         devinfo = kzalloc(sizeof(*devinfo), GFP_ATOMIC);
1382         if (devinfo == NULL)
1383                 return -ENOMEM;
1384
1385         devinfo->usbdev = usb;
1386         devinfo->dev = &usb->dev;
1387
1388         usb_set_intfdata(intf, devinfo);
1389
1390         /* Check that the device supports only one configuration */
1391         if (usb->descriptor.bNumConfigurations != 1) {
1392                 ret = -1;
1393                 goto fail;
1394         }
1395
1396         if (usb->descriptor.bDeviceClass != USB_CLASS_VENDOR_SPEC) {
1397                 ret = -1;
1398                 goto fail;
1399         }
1400
1401         /*
1402          * Only the BDC interface configuration is supported:
1403          *      Device class: USB_CLASS_VENDOR_SPEC
1404          *      if0 class: USB_CLASS_VENDOR_SPEC
1405          *      if0/ep0: control
1406          *      if0/ep1: bulk in
1407          *      if0/ep2: bulk out (ok if swapped with bulk in)
1408          */
1409         if (CONFIGDESC(usb)->bNumInterfaces != 1) {
1410                 ret = -1;
1411                 goto fail;
1412         }
1413
1414         /* Check interface */
1415         if (IFDESC(usb, CONTROL_IF).bInterfaceClass != USB_CLASS_VENDOR_SPEC ||
1416             IFDESC(usb, CONTROL_IF).bInterfaceSubClass != 2 ||
1417             IFDESC(usb, CONTROL_IF).bInterfaceProtocol != 0xff) {
1418                 brcmf_dbg(ERROR, "invalid control interface: class %d, subclass %d, proto %d\n",
1419                           IFDESC(usb, CONTROL_IF).bInterfaceClass,
1420                           IFDESC(usb, CONTROL_IF).bInterfaceSubClass,
1421                           IFDESC(usb, CONTROL_IF).bInterfaceProtocol);
1422                 ret = -1;
1423                 goto fail;
1424         }
1425
1426         /* Check control endpoint */
1427         endpoint = &IFEPDESC(usb, CONTROL_IF, 0);
1428         if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1429                 != USB_ENDPOINT_XFER_INT) {
1430                 brcmf_dbg(ERROR, "invalid control endpoint %d\n",
1431                           endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
1432                 ret = -1;
1433                 goto fail;
1434         }
1435
1436         endpoint_num = endpoint->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1437         devinfo->intr_pipe = usb_rcvintpipe(usb, endpoint_num);
1438
1439         devinfo->rx_pipe = 0;
1440         devinfo->rx_pipe2 = 0;
1441         devinfo->tx_pipe = 0;
1442         num_of_eps = IFDESC(usb, BULK_IF).bNumEndpoints - 1;
1443
1444         /* Check data endpoints and get pipes */
1445         for (ep = 1; ep <= num_of_eps; ep++) {
1446                 endpoint = &IFEPDESC(usb, BULK_IF, ep);
1447                 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
1448                     USB_ENDPOINT_XFER_BULK) {
1449                         brcmf_dbg(ERROR, "invalid data endpoint %d\n", ep);
1450                         ret = -1;
1451                         goto fail;
1452                 }
1453
1454                 endpoint_num = endpoint->bEndpointAddress &
1455                                USB_ENDPOINT_NUMBER_MASK;
1456                 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1457                         == USB_DIR_IN) {
1458                         if (!devinfo->rx_pipe) {
1459                                 devinfo->rx_pipe =
1460                                         usb_rcvbulkpipe(usb, endpoint_num);
1461                         } else {
1462                                 devinfo->rx_pipe2 =
1463                                         usb_rcvbulkpipe(usb, endpoint_num);
1464                         }
1465                 } else {
1466                         devinfo->tx_pipe = usb_sndbulkpipe(usb, endpoint_num);
1467                 }
1468         }
1469
1470         /* Allocate interrupt URB and data buffer */
1471         /* RNDIS says 8-byte intr, our old drivers used 4-byte */
1472         if (IFEPDESC(usb, CONTROL_IF, 0).wMaxPacketSize == cpu_to_le16(16))
1473                 devinfo->intr_size = 8;
1474         else
1475                 devinfo->intr_size = 4;
1476
1477         devinfo->interval = IFEPDESC(usb, CONTROL_IF, 0).bInterval;
1478
1479         if (usb->speed == USB_SPEED_HIGH)
1480                 brcmf_dbg(INFO, "Broadcom high speed USB wireless device detected\n");
1481         else
1482                 brcmf_dbg(INFO, "Broadcom full speed USB wireless device detected\n");
1483
1484         ret = brcmf_usb_probe_cb(devinfo, "", USB_BUS, 0);
1485         if (ret)
1486                 goto fail;
1487
1488         /* Success */
1489         return 0;
1490
1491 fail:
1492         brcmf_dbg(ERROR, "failed with errno %d\n", ret);
1493         kfree(devinfo);
1494         usb_set_intfdata(intf, NULL);
1495         return ret;
1496
1497 }
1498
1499 static void
1500 brcmf_usb_disconnect(struct usb_interface *intf)
1501 {
1502         struct brcmf_usbdev_info *devinfo;
1503
1504         brcmf_dbg(TRACE, "enter\n");
1505         devinfo = (struct brcmf_usbdev_info *)usb_get_intfdata(intf);
1506         brcmf_usb_disconnect_cb(devinfo);
1507         kfree(devinfo);
1508 }
1509
1510 /*
1511  *      only need to signal the bus being down and update the suspend state.
1512  */
1513 static int brcmf_usb_suspend(struct usb_interface *intf, pm_message_t state)
1514 {
1515         struct usb_device *usb = interface_to_usbdev(intf);
1516         struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(&usb->dev);
1517
1518         brcmf_dbg(TRACE, "enter\n");
1519         devinfo->bus_pub.state = BCMFMAC_USB_STATE_DOWN;
1520         devinfo->suspend_state = USBOS_SUSPEND_STATE_SUSPENDED;
1521         return 0;
1522 }
1523
1524 /*
1525  *      mark suspend state active and crank up the bus.
1526  */
1527 static int brcmf_usb_resume(struct usb_interface *intf)
1528 {
1529         struct usb_device *usb = interface_to_usbdev(intf);
1530         struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(&usb->dev);
1531
1532         brcmf_dbg(TRACE, "enter\n");
1533         devinfo->suspend_state = USBOS_SUSPEND_STATE_DEVICE_ACTIVE;
1534         brcmf_bus_start(&usb->dev);
1535         return 0;
1536 }
1537
1538 #define BRCMF_USB_VENDOR_ID_BROADCOM    0x0a5c
1539 #define BRCMF_USB_DEVICE_ID_43143       0xbd1e
1540 #define BRCMF_USB_DEVICE_ID_43236       0xbd17
1541 #define BRCMF_USB_DEVICE_ID_43242       0xbd1f
1542 #define BRCMF_USB_DEVICE_ID_BCMFW       0x0bdc
1543
1544 static struct usb_device_id brcmf_usb_devid_table[] = {
1545         { USB_DEVICE(BRCMF_USB_VENDOR_ID_BROADCOM, BRCMF_USB_DEVICE_ID_43143) },
1546         { USB_DEVICE(BRCMF_USB_VENDOR_ID_BROADCOM, BRCMF_USB_DEVICE_ID_43236) },
1547         { USB_DEVICE(BRCMF_USB_VENDOR_ID_BROADCOM, BRCMF_USB_DEVICE_ID_43242) },
1548         /* special entry for device with firmware loaded and running */
1549         { USB_DEVICE(BRCMF_USB_VENDOR_ID_BROADCOM, BRCMF_USB_DEVICE_ID_BCMFW) },
1550         { }
1551 };
1552 MODULE_DEVICE_TABLE(usb, brcmf_usb_devid_table);
1553 MODULE_FIRMWARE(BRCMF_USB_43143_FW_NAME);
1554 MODULE_FIRMWARE(BRCMF_USB_43236_FW_NAME);
1555 MODULE_FIRMWARE(BRCMF_USB_43242_FW_NAME);
1556
1557 /* TODO: suspend and resume entries */
1558 static struct usb_driver brcmf_usbdrvr = {
1559         .name = KBUILD_MODNAME,
1560         .probe = brcmf_usb_probe,
1561         .disconnect = brcmf_usb_disconnect,
1562         .id_table = brcmf_usb_devid_table,
1563         .suspend = brcmf_usb_suspend,
1564         .resume = brcmf_usb_resume,
1565         .supports_autosuspend = 1,
1566         .disable_hub_initiated_lpm = 1,
1567 };
1568
1569 static void brcmf_release_fw(struct list_head *q)
1570 {
1571         struct brcmf_usb_image *fw_image, *next;
1572
1573         list_for_each_entry_safe(fw_image, next, q, list) {
1574                 vfree(fw_image->image);
1575                 list_del_init(&fw_image->list);
1576         }
1577 }
1578
1579
1580 void brcmf_usb_exit(void)
1581 {
1582         usb_deregister(&brcmf_usbdrvr);
1583         brcmf_release_fw(&fw_image_list);
1584 }
1585
1586 void brcmf_usb_init(void)
1587 {
1588         INIT_LIST_HEAD(&fw_image_list);
1589         usb_register(&brcmf_usbdrvr);
1590 }