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

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

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