Version:  2.0.40 2.2.26 2.4.37 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16

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

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