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