Version:  2.0.40 2.2.26 2.4.37 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0 4.1 4.2 4.3 4.4

Linux/drivers/net/wireless/brcm80211/brcmfmac/usb.c

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

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us