Version:  2.0.40 2.2.26 2.4.37 3.6 3.7 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

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

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