blob: 6e3ff6f61103d234c38933bfae3d908829ba331d [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;
Hante Meulemanc6ab4292012-09-11 21:18:48 +0200122 int tx_freecount;
123 bool tx_flowblock;
Arend van Spriel71bb2442012-02-09 21:09:08 +0100124
125 struct brcmf_usbreq *tx_reqs;
126 struct brcmf_usbreq *rx_reqs;
127
128 u8 *image; /* buffer for combine fw and nvram */
129 int image_len;
130
131 wait_queue_head_t wait;
132 bool waitdone;
133 int sync_urb_status;
134
135 struct usb_device *usbdev;
136 struct device *dev;
137 enum usb_device_speed device_speed;
138
139 int ctl_in_pipe, ctl_out_pipe;
140 struct urb *ctl_urb; /* URB for control endpoint */
141 struct usb_ctrlrequest ctl_write;
142 struct usb_ctrlrequest ctl_read;
143 u32 ctl_urb_actual_length;
144 int ctl_urb_status;
145 int ctl_completed;
146 wait_queue_head_t ioctl_resp_wait;
147 wait_queue_head_t ctrl_wait;
148 ulong ctl_op;
149
150 bool rxctl_deferrespok;
151
152 struct urb *bulk_urb; /* used for FW download */
153 struct urb *intr_urb; /* URB for interrupt endpoint */
154 int intr_size; /* Size of interrupt message */
155 int interval; /* Interrupt polling interval */
156 struct intr_transfer_buf intr; /* Data buffer for interrupt endpoint */
157
158 struct brcmf_usb_probe_info probe_info;
159
160};
161
162static void brcmf_usb_rx_refill(struct brcmf_usbdev_info *devinfo,
163 struct brcmf_usbreq *req);
164
165MODULE_AUTHOR("Broadcom Corporation");
166MODULE_DESCRIPTION("Broadcom 802.11n wireless LAN fullmac usb driver.");
167MODULE_SUPPORTED_DEVICE("Broadcom 802.11n WLAN fullmac usb cards");
168MODULE_LICENSE("Dual BSD/GPL");
169
170static struct brcmf_usbdev *brcmf_usb_get_buspub(struct device *dev)
171{
172 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
173 return bus_if->bus_priv.usb;
174}
175
176static struct brcmf_usbdev_info *brcmf_usb_get_businfo(struct device *dev)
177{
178 return brcmf_usb_get_buspub(dev)->devinfo;
179}
180
Arend van Spriel71bb2442012-02-09 21:09:08 +0100181static int brcmf_usb_ioctl_resp_wait(struct brcmf_usbdev_info *devinfo,
182 uint *condition, bool *pending)
183{
184 DECLARE_WAITQUEUE(wait, current);
185 int timeout = IOCTL_RESP_TIMEOUT;
186
187 /* Convert timeout in millsecond to jiffies */
188 timeout = msecs_to_jiffies(timeout);
189 /* Wait until control frame is available */
190 add_wait_queue(&devinfo->ioctl_resp_wait, &wait);
191 set_current_state(TASK_INTERRUPTIBLE);
192
193 smp_mb();
194 while (!(*condition) && (!signal_pending(current) && timeout)) {
195 timeout = schedule_timeout(timeout);
196 /* Wait until control frame is available */
197 smp_mb();
198 }
199
200 if (signal_pending(current))
201 *pending = true;
202
203 set_current_state(TASK_RUNNING);
204 remove_wait_queue(&devinfo->ioctl_resp_wait, &wait);
205
206 return timeout;
207}
208
209static int brcmf_usb_ioctl_resp_wake(struct brcmf_usbdev_info *devinfo)
210{
211 if (waitqueue_active(&devinfo->ioctl_resp_wait))
212 wake_up_interruptible(&devinfo->ioctl_resp_wait);
213
214 return 0;
215}
216
217static void
218brcmf_usb_ctl_complete(struct brcmf_usbdev_info *devinfo, int type, int status)
219{
220
221 if (unlikely(devinfo == NULL))
222 return;
223
224 if (type == BRCMF_USB_CBCTL_READ) {
225 if (status == 0)
226 devinfo->bus_pub.stats.rx_ctlpkts++;
227 else
228 devinfo->bus_pub.stats.rx_ctlerrs++;
229 } else if (type == BRCMF_USB_CBCTL_WRITE) {
230 if (status == 0)
231 devinfo->bus_pub.stats.tx_ctlpkts++;
232 else
233 devinfo->bus_pub.stats.tx_ctlerrs++;
234 }
235
236 devinfo->ctl_urb_status = status;
237 devinfo->ctl_completed = true;
238 brcmf_usb_ioctl_resp_wake(devinfo);
239}
240
241static void
242brcmf_usb_ctlread_complete(struct urb *urb)
243{
244 struct brcmf_usbdev_info *devinfo =
245 (struct brcmf_usbdev_info *)urb->context;
246
247 devinfo->ctl_urb_actual_length = urb->actual_length;
248 brcmf_usb_ctl_complete(devinfo, BRCMF_USB_CBCTL_READ,
249 urb->status);
250}
251
252static void
253brcmf_usb_ctlwrite_complete(struct urb *urb)
254{
255 struct brcmf_usbdev_info *devinfo =
256 (struct brcmf_usbdev_info *)urb->context;
257
258 brcmf_usb_ctl_complete(devinfo, BRCMF_USB_CBCTL_WRITE,
259 urb->status);
260}
261
262static int brcmf_usb_pnp(struct brcmf_usbdev_info *devinfo, uint state)
263{
264 return 0;
265}
266
267static int
268brcmf_usb_send_ctl(struct brcmf_usbdev_info *devinfo, u8 *buf, int len)
269{
270 int ret;
271 u16 size;
272
273 if (devinfo == NULL || buf == NULL ||
274 len == 0 || devinfo->ctl_urb == NULL)
275 return -EINVAL;
276
277 /* If the USB/HSIC bus in sleep state, wake it up */
278 if (devinfo->suspend_state == USBOS_SUSPEND_STATE_SUSPENDED)
279 if (brcmf_usb_pnp(devinfo, BCMFMAC_USB_PNP_RESUME) != 0) {
280 brcmf_dbg(ERROR, "Could not Resume the bus!\n");
281 return -EIO;
282 }
283
284 devinfo->activity = true;
285 size = len;
286 devinfo->ctl_write.wLength = cpu_to_le16p(&size);
287 devinfo->ctl_urb->transfer_buffer_length = size;
288 devinfo->ctl_urb_status = 0;
289 devinfo->ctl_urb_actual_length = 0;
290
291 usb_fill_control_urb(devinfo->ctl_urb,
292 devinfo->usbdev,
293 devinfo->ctl_out_pipe,
294 (unsigned char *) &devinfo->ctl_write,
295 buf, size,
296 (usb_complete_t)brcmf_usb_ctlwrite_complete,
297 devinfo);
298
299 ret = usb_submit_urb(devinfo->ctl_urb, GFP_ATOMIC);
300 if (ret < 0)
301 brcmf_dbg(ERROR, "usb_submit_urb failed %d\n", ret);
302
303 return ret;
304}
305
306static int
307brcmf_usb_recv_ctl(struct brcmf_usbdev_info *devinfo, u8 *buf, int len)
308{
309 int ret;
310 u16 size;
311
312 if ((devinfo == NULL) || (buf == NULL) || (len == 0)
313 || (devinfo->ctl_urb == NULL))
314 return -EINVAL;
315
316 size = len;
317 devinfo->ctl_read.wLength = cpu_to_le16p(&size);
318 devinfo->ctl_urb->transfer_buffer_length = size;
319
320 if (devinfo->rxctl_deferrespok) {
321 /* BMAC model */
322 devinfo->ctl_read.bRequestType = USB_DIR_IN
323 | USB_TYPE_VENDOR | USB_RECIP_INTERFACE;
324 devinfo->ctl_read.bRequest = DL_DEFER_RESP_OK;
325 } else {
326 /* full dongle model */
327 devinfo->ctl_read.bRequestType = USB_DIR_IN
328 | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
329 devinfo->ctl_read.bRequest = 1;
330 }
331
332 usb_fill_control_urb(devinfo->ctl_urb,
333 devinfo->usbdev,
334 devinfo->ctl_in_pipe,
335 (unsigned char *) &devinfo->ctl_read,
336 buf, size,
337 (usb_complete_t)brcmf_usb_ctlread_complete,
338 devinfo);
339
340 ret = usb_submit_urb(devinfo->ctl_urb, GFP_ATOMIC);
341 if (ret < 0)
342 brcmf_dbg(ERROR, "usb_submit_urb failed %d\n", ret);
343
344 return ret;
345}
346
347static int brcmf_usb_tx_ctlpkt(struct device *dev, u8 *buf, u32 len)
348{
349 int err = 0;
350 int timeout = 0;
351 bool pending;
352 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
353
354 if (devinfo->bus_pub.state != BCMFMAC_USB_STATE_UP) {
355 /* TODO: handle suspend/resume */
356 return -EIO;
357 }
358
359 if (test_and_set_bit(0, &devinfo->ctl_op))
360 return -EIO;
361
Hante Meulemana77f5742012-08-30 19:42:58 +0200362 devinfo->ctl_completed = false;
Arend van Spriel71bb2442012-02-09 21:09:08 +0100363 err = brcmf_usb_send_ctl(devinfo, buf, len);
364 if (err) {
365 brcmf_dbg(ERROR, "fail %d bytes: %d\n", err, len);
366 return err;
367 }
368
Arend van Spriel71bb2442012-02-09 21:09:08 +0100369 timeout = brcmf_usb_ioctl_resp_wait(devinfo, &devinfo->ctl_completed,
370 &pending);
371 clear_bit(0, &devinfo->ctl_op);
372 if (!timeout) {
373 brcmf_dbg(ERROR, "Txctl wait timed out\n");
374 err = -EIO;
375 }
376 return err;
377}
378
379static int brcmf_usb_rx_ctlpkt(struct device *dev, u8 *buf, u32 len)
380{
381 int err = 0;
382 int timeout = 0;
383 bool pending;
384 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
385
386 if (devinfo->bus_pub.state != BCMFMAC_USB_STATE_UP) {
387 /* TODO: handle suspend/resume */
388 return -EIO;
389 }
390 if (test_and_set_bit(0, &devinfo->ctl_op))
391 return -EIO;
392
393 err = brcmf_usb_recv_ctl(devinfo, buf, len);
394 if (err) {
395 brcmf_dbg(ERROR, "fail %d bytes: %d\n", err, len);
396 return err;
397 }
398 devinfo->ctl_completed = false;
399 timeout = brcmf_usb_ioctl_resp_wait(devinfo, &devinfo->ctl_completed,
400 &pending);
401 err = devinfo->ctl_urb_status;
402 clear_bit(0, &devinfo->ctl_op);
403 if (!timeout) {
404 brcmf_dbg(ERROR, "rxctl wait timed out\n");
405 err = -EIO;
406 }
407 if (!err)
408 return devinfo->ctl_urb_actual_length;
409 else
410 return err;
411}
412
413static struct brcmf_usbreq *brcmf_usb_deq(struct brcmf_usbdev_info *devinfo,
Hante Meulemanc6ab4292012-09-11 21:18:48 +0200414 struct list_head *q, int *counter)
Arend van Spriel71bb2442012-02-09 21:09:08 +0100415{
416 unsigned long flags;
417 struct brcmf_usbreq *req;
418 spin_lock_irqsave(&devinfo->qlock, flags);
419 if (list_empty(q)) {
420 spin_unlock_irqrestore(&devinfo->qlock, flags);
421 return NULL;
422 }
423 req = list_entry(q->next, struct brcmf_usbreq, list);
424 list_del_init(q->next);
Hante Meulemanc6ab4292012-09-11 21:18:48 +0200425 if (counter)
426 (*counter)--;
Arend van Spriel71bb2442012-02-09 21:09:08 +0100427 spin_unlock_irqrestore(&devinfo->qlock, flags);
428 return req;
429
430}
431
432static void brcmf_usb_enq(struct brcmf_usbdev_info *devinfo,
Hante Meulemanc6ab4292012-09-11 21:18:48 +0200433 struct list_head *q, struct brcmf_usbreq *req,
434 int *counter)
Arend van Spriel71bb2442012-02-09 21:09:08 +0100435{
436 unsigned long flags;
437 spin_lock_irqsave(&devinfo->qlock, flags);
438 list_add_tail(&req->list, q);
Hante Meulemanc6ab4292012-09-11 21:18:48 +0200439 if (counter)
440 (*counter)++;
Arend van Spriel71bb2442012-02-09 21:09:08 +0100441 spin_unlock_irqrestore(&devinfo->qlock, flags);
442}
443
444static struct brcmf_usbreq *
445brcmf_usbdev_qinit(struct list_head *q, int qsize)
446{
447 int i;
448 struct brcmf_usbreq *req, *reqs;
449
450 reqs = kzalloc(sizeof(struct brcmf_usbreq) * qsize, GFP_ATOMIC);
451 if (reqs == NULL) {
452 brcmf_dbg(ERROR, "fail to allocate memory!\n");
453 return NULL;
454 }
455 req = reqs;
456
457 for (i = 0; i < qsize; i++) {
458 req->urb = usb_alloc_urb(0, GFP_ATOMIC);
459 if (!req->urb)
460 goto fail;
461
462 INIT_LIST_HEAD(&req->list);
463 list_add_tail(&req->list, q);
464 req++;
465 }
466 return reqs;
467fail:
468 brcmf_dbg(ERROR, "fail!\n");
469 while (!list_empty(q)) {
470 req = list_entry(q->next, struct brcmf_usbreq, list);
471 if (req && req->urb)
472 usb_free_urb(req->urb);
473 list_del(q->next);
474 }
475 return NULL;
476
477}
478
479static void brcmf_usb_free_q(struct list_head *q, bool pending)
480{
481 struct brcmf_usbreq *req, *next;
482 int i = 0;
483 list_for_each_entry_safe(req, next, q, list) {
Dan Carpenterd4ca0092012-02-24 09:22:27 +0300484 if (!req->urb) {
Arend van Spriel71bb2442012-02-09 21:09:08 +0100485 brcmf_dbg(ERROR, "bad req\n");
486 break;
487 }
488 i++;
489 if (pending) {
490 usb_kill_urb(req->urb);
491 } else {
492 usb_free_urb(req->urb);
493 list_del_init(&req->list);
494 }
495 }
496}
497
498static void brcmf_usb_del_fromq(struct brcmf_usbdev_info *devinfo,
499 struct brcmf_usbreq *req)
500{
501 unsigned long flags;
502
503 spin_lock_irqsave(&devinfo->qlock, flags);
504 list_del_init(&req->list);
505 spin_unlock_irqrestore(&devinfo->qlock, flags);
506}
507
508
509static void brcmf_usb_tx_complete(struct urb *urb)
510{
511 struct brcmf_usbreq *req = (struct brcmf_usbreq *)urb->context;
512 struct brcmf_usbdev_info *devinfo = req->devinfo;
513
514 brcmf_usb_del_fromq(devinfo, req);
515 if (urb->status == 0)
Arend van Spriel1d9c1792012-03-02 22:55:47 +0100516 devinfo->bus_pub.bus->dstats.tx_packets++;
Arend van Spriel71bb2442012-02-09 21:09:08 +0100517 else
Arend van Spriel1d9c1792012-03-02 22:55:47 +0100518 devinfo->bus_pub.bus->dstats.tx_errors++;
Arend van Spriel71bb2442012-02-09 21:09:08 +0100519
Arend van Spriel01e33312012-08-30 10:05:34 +0200520 brcmu_pkt_buf_free_skb(req->skb);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100521 req->skb = NULL;
Hante Meulemanc6ab4292012-09-11 21:18:48 +0200522 brcmf_usb_enq(devinfo, &devinfo->tx_freeq, req, &devinfo->tx_freecount);
523 if (devinfo->tx_freecount > devinfo->tx_high_watermark &&
524 devinfo->tx_flowblock) {
525 brcmf_txflowblock(devinfo->dev, false);
526 devinfo->tx_flowblock = false;
527 }
Arend van Spriel71bb2442012-02-09 21:09:08 +0100528}
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 Spriel01e33312012-08-30 10:05:34 +0200545 brcmu_pkt_buf_free_skb(skb);
Hante Meulemanc6ab4292012-09-11 21:18:48 +0200546 brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100547 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++;
Hante Meuleman64477eb2012-09-11 21:18:50 +0200556 } else
Arend van Spriel71bb2442012-02-09 21:09:08 +0100557 brcmf_rx_packet(devinfo->dev, ifidx, skb);
Hante Meuleman64477eb2012-09-11 21:18:50 +0200558 brcmf_usb_rx_refill(devinfo, req);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100559 } else {
Arend van Spriel01e33312012-08-30 10:05:34 +0200560 brcmu_pkt_buf_free_skb(skb);
Hante Meulemanc6ab4292012-09-11 21:18:48 +0200561 brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100562 }
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) {
Hante Meulemanc6ab4292012-09-11 21:18:48 +0200578 brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100579 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);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100586 req->devinfo = devinfo;
Hante Meulemanc6ab4292012-09-11 21:18:48 +0200587 brcmf_usb_enq(devinfo, &devinfo->rx_postq, req, NULL);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100588
589 ret = usb_submit_urb(req->urb, GFP_ATOMIC);
Hante Meuleman2e875ac2012-08-30 10:05:36 +0200590 if (ret) {
591 brcmf_usb_del_fromq(devinfo, req);
Arend van Spriel01e33312012-08-30 10:05:34 +0200592 brcmu_pkt_buf_free_skb(req->skb);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100593 req->skb = NULL;
Hante Meulemanc6ab4292012-09-11 21:18:48 +0200594 brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100595 }
596 return;
597}
598
599static void brcmf_usb_rx_fill_all(struct brcmf_usbdev_info *devinfo)
600{
601 struct brcmf_usbreq *req;
602
603 if (devinfo->bus_pub.state != BCMFMAC_USB_STATE_UP) {
604 brcmf_dbg(ERROR, "bus is not up\n");
605 return;
606 }
Hante Meulemanc6ab4292012-09-11 21:18:48 +0200607 while ((req = brcmf_usb_deq(devinfo, &devinfo->rx_freeq, NULL)) != NULL)
Arend van Spriel71bb2442012-02-09 21:09:08 +0100608 brcmf_usb_rx_refill(devinfo, req);
609}
610
611static void
612brcmf_usb_state_change(struct brcmf_usbdev_info *devinfo, int state)
613{
614 struct brcmf_bus *bcmf_bus = devinfo->bus_pub.bus;
615 int old_state;
616
617
618 if (devinfo->bus_pub.state == state)
619 return;
620
621 old_state = devinfo->bus_pub.state;
622 brcmf_dbg(TRACE, "dbus state change from %d to to %d\n",
623 old_state, state);
624
625 /* Don't update state if it's PnP firmware re-download */
626 if (state != BCMFMAC_USB_STATE_PNP_FWDL) /* TODO */
627 devinfo->bus_pub.state = state;
628
629 if ((old_state == BCMFMAC_USB_STATE_SLEEP)
630 && (state == BCMFMAC_USB_STATE_UP)) {
631 brcmf_usb_rx_fill_all(devinfo);
632 }
633
634 /* update state of upper layer */
635 if (state == BCMFMAC_USB_STATE_DOWN) {
636 brcmf_dbg(INFO, "DBUS is down\n");
637 bcmf_bus->state = BRCMF_BUS_DOWN;
638 } else {
639 brcmf_dbg(INFO, "DBUS current state=%d\n", state);
640 }
641}
642
643static void
644brcmf_usb_intr_complete(struct urb *urb)
645{
646 struct brcmf_usbdev_info *devinfo =
647 (struct brcmf_usbdev_info *)urb->context;
648 bool killed;
649
650 if (devinfo == NULL)
651 return;
652
653 if (unlikely(urb->status)) {
654 if (devinfo->suspend_state ==
655 USBOS_SUSPEND_STATE_SUSPEND_PENDING)
656 killed = true;
657
658 if ((urb->status == -ENOENT && (!killed))
659 || urb->status == -ESHUTDOWN ||
660 urb->status == -ENODEV) {
661 brcmf_usb_state_change(devinfo, BCMFMAC_USB_STATE_DOWN);
662 }
663 }
664
665 if (devinfo->bus_pub.state == BCMFMAC_USB_STATE_DOWN) {
666 brcmf_dbg(ERROR, "intr cb when DBUS down, ignoring\n");
667 return;
668 }
669
670 if (devinfo->bus_pub.state == BCMFMAC_USB_STATE_UP)
671 usb_submit_urb(devinfo->intr_urb, GFP_ATOMIC);
672}
673
674static int brcmf_usb_tx(struct device *dev, struct sk_buff *skb)
675{
676 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
677 struct brcmf_usbreq *req;
678 int ret;
679
680 if (devinfo->bus_pub.state != BCMFMAC_USB_STATE_UP) {
681 /* TODO: handle suspend/resume */
682 return -EIO;
683 }
684
Hante Meulemanc6ab4292012-09-11 21:18:48 +0200685 req = brcmf_usb_deq(devinfo, &devinfo->tx_freeq,
686 &devinfo->tx_freecount);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100687 if (!req) {
Hante Meuleman2e875ac2012-08-30 10:05:36 +0200688 brcmu_pkt_buf_free_skb(skb);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100689 brcmf_dbg(ERROR, "no req to send\n");
690 return -ENOMEM;
691 }
Arend van Spriel71bb2442012-02-09 21:09:08 +0100692
693 req->skb = skb;
694 req->devinfo = devinfo;
695 usb_fill_bulk_urb(req->urb, devinfo->usbdev, devinfo->tx_pipe,
696 skb->data, skb->len, brcmf_usb_tx_complete, req);
697 req->urb->transfer_flags |= URB_ZERO_PACKET;
Hante Meulemanc6ab4292012-09-11 21:18:48 +0200698 brcmf_usb_enq(devinfo, &devinfo->tx_postq, req, NULL);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100699 ret = usb_submit_urb(req->urb, GFP_ATOMIC);
Hante Meuleman2e875ac2012-08-30 10:05:36 +0200700 if (ret) {
701 brcmf_dbg(ERROR, "brcmf_usb_tx usb_submit_urb FAILED\n");
702 brcmf_usb_del_fromq(devinfo, req);
703 brcmu_pkt_buf_free_skb(req->skb);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100704 req->skb = NULL;
Hante Meulemanc6ab4292012-09-11 21:18:48 +0200705 brcmf_usb_enq(devinfo, &devinfo->tx_freeq, req,
706 &devinfo->tx_freecount);
707 } else {
708 if (devinfo->tx_freecount < devinfo->tx_low_watermark &&
709 !devinfo->tx_flowblock) {
710 brcmf_txflowblock(dev, true);
711 devinfo->tx_flowblock = true;
712 }
Arend van Spriel71bb2442012-02-09 21:09:08 +0100713 }
714
715 return ret;
716}
717
718
719static int brcmf_usb_up(struct device *dev)
720{
721 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
722 u16 ifnum;
723
Dan Carpenterd4ca0092012-02-24 09:22:27 +0300724 if (devinfo->bus_pub.state == BCMFMAC_USB_STATE_UP)
725 return 0;
726
Arend van Spriel71bb2442012-02-09 21:09:08 +0100727 /* If the USB/HSIC bus in sleep state, wake it up */
728 if (devinfo->suspend_state == USBOS_SUSPEND_STATE_SUSPENDED) {
729 if (brcmf_usb_pnp(devinfo, BCMFMAC_USB_PNP_RESUME) != 0) {
730 brcmf_dbg(ERROR, "Could not Resume the bus!\n");
731 return -EIO;
732 }
733 }
734 devinfo->activity = true;
735
736 /* Success, indicate devinfo is fully up */
737 brcmf_usb_state_change(devinfo, BCMFMAC_USB_STATE_UP);
738
739 if (devinfo->intr_urb) {
740 int ret;
741
742 usb_fill_int_urb(devinfo->intr_urb, devinfo->usbdev,
743 devinfo->intr_pipe,
744 &devinfo->intr,
745 devinfo->intr_size,
746 (usb_complete_t)brcmf_usb_intr_complete,
747 devinfo,
748 devinfo->interval);
749
750 ret = usb_submit_urb(devinfo->intr_urb, GFP_ATOMIC);
751 if (ret) {
752 brcmf_dbg(ERROR, "USB_SUBMIT_URB failed with status %d\n",
753 ret);
754 return -EINVAL;
755 }
756 }
757
758 if (devinfo->ctl_urb) {
759 devinfo->ctl_in_pipe = usb_rcvctrlpipe(devinfo->usbdev, 0);
760 devinfo->ctl_out_pipe = usb_sndctrlpipe(devinfo->usbdev, 0);
761
762 ifnum = IFDESC(devinfo->usbdev, CONTROL_IF).bInterfaceNumber;
763
764 /* CTL Write */
765 devinfo->ctl_write.bRequestType =
766 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
767 devinfo->ctl_write.bRequest = 0;
768 devinfo->ctl_write.wValue = cpu_to_le16(0);
769 devinfo->ctl_write.wIndex = cpu_to_le16p(&ifnum);
770
771 /* CTL Read */
772 devinfo->ctl_read.bRequestType =
773 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
774 devinfo->ctl_read.bRequest = 1;
775 devinfo->ctl_read.wValue = cpu_to_le16(0);
776 devinfo->ctl_read.wIndex = cpu_to_le16p(&ifnum);
777 }
778 brcmf_usb_rx_fill_all(devinfo);
779 return 0;
780}
781
782static void brcmf_usb_down(struct device *dev)
783{
784 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
785
786 if (devinfo == NULL)
787 return;
788
789 brcmf_dbg(TRACE, "enter\n");
790 if (devinfo->bus_pub.state == BCMFMAC_USB_STATE_DOWN)
791 return;
792
793 brcmf_usb_state_change(devinfo, BCMFMAC_USB_STATE_DOWN);
794 if (devinfo->intr_urb)
795 usb_kill_urb(devinfo->intr_urb);
796
797 if (devinfo->ctl_urb)
798 usb_kill_urb(devinfo->ctl_urb);
799
800 if (devinfo->bulk_urb)
801 usb_kill_urb(devinfo->bulk_urb);
802 brcmf_usb_free_q(&devinfo->tx_postq, true);
803
804 brcmf_usb_free_q(&devinfo->rx_postq, true);
805}
806
807static int
808brcmf_usb_sync_wait(struct brcmf_usbdev_info *devinfo, u16 time)
809{
810 int ret;
811 int err = 0;
812 int ms = time;
813
814 ret = wait_event_interruptible_timeout(devinfo->wait,
815 devinfo->waitdone == true, (ms * HZ / 1000));
816
817 if ((devinfo->waitdone == false) || (devinfo->sync_urb_status)) {
818 brcmf_dbg(ERROR, "timeout(%d) or urb err=%d\n",
819 ret, devinfo->sync_urb_status);
820 err = -EINVAL;
821 }
822 devinfo->waitdone = false;
823 return err;
824}
825
826static void
827brcmf_usb_sync_complete(struct urb *urb)
828{
829 struct brcmf_usbdev_info *devinfo =
830 (struct brcmf_usbdev_info *)urb->context;
831
832 devinfo->waitdone = true;
833 wake_up_interruptible(&devinfo->wait);
834 devinfo->sync_urb_status = urb->status;
835}
836
837static bool brcmf_usb_dl_cmd(struct brcmf_usbdev_info *devinfo, u8 cmd,
838 void *buffer, int buflen)
839{
840 int ret = 0;
841 char *tmpbuf;
842 u16 size;
843
844 if ((!devinfo) || (devinfo->ctl_urb == NULL))
845 return false;
846
847 tmpbuf = kmalloc(buflen, GFP_ATOMIC);
848 if (!tmpbuf)
849 return false;
850
851 size = buflen;
852 devinfo->ctl_urb->transfer_buffer_length = size;
853
854 devinfo->ctl_read.wLength = cpu_to_le16p(&size);
855 devinfo->ctl_read.bRequestType = USB_DIR_IN | USB_TYPE_VENDOR |
856 USB_RECIP_INTERFACE;
857 devinfo->ctl_read.bRequest = cmd;
858
859 usb_fill_control_urb(devinfo->ctl_urb,
860 devinfo->usbdev,
861 usb_rcvctrlpipe(devinfo->usbdev, 0),
862 (unsigned char *) &devinfo->ctl_read,
863 (void *) tmpbuf, size,
864 (usb_complete_t)brcmf_usb_sync_complete, devinfo);
865
866 ret = usb_submit_urb(devinfo->ctl_urb, GFP_ATOMIC);
867 if (ret < 0) {
868 brcmf_dbg(ERROR, "usb_submit_urb failed %d\n", ret);
869 kfree(tmpbuf);
870 return false;
871 }
872
873 ret = brcmf_usb_sync_wait(devinfo, BRCMF_USB_SYNC_TIMEOUT);
874 memcpy(buffer, tmpbuf, buflen);
875 kfree(tmpbuf);
876
877 return (ret == 0);
878}
879
880static bool
881brcmf_usb_dlneeded(struct brcmf_usbdev_info *devinfo)
882{
883 struct bootrom_id_le id;
884 u32 chipid, chiprev;
885
886 brcmf_dbg(TRACE, "enter\n");
887
888 if (devinfo == NULL)
889 return false;
890
891 /* Check if firmware downloaded already by querying runtime ID */
892 id.chip = cpu_to_le32(0xDEAD);
893 brcmf_usb_dl_cmd(devinfo, DL_GETVER, &id,
894 sizeof(struct bootrom_id_le));
895
896 chipid = le32_to_cpu(id.chip);
897 chiprev = le32_to_cpu(id.chiprev);
898
899 if ((chipid & 0x4300) == 0x4300)
900 brcmf_dbg(INFO, "chip %x rev 0x%x\n", chipid, chiprev);
901 else
902 brcmf_dbg(INFO, "chip %d rev 0x%x\n", chipid, chiprev);
903 if (chipid == BRCMF_POSTBOOT_ID) {
904 brcmf_dbg(INFO, "firmware already downloaded\n");
905 brcmf_usb_dl_cmd(devinfo, DL_RESETCFG, &id,
906 sizeof(struct bootrom_id_le));
907 return false;
908 } else {
Arend van Sprielac94f192012-03-02 22:55:46 +0100909 devinfo->bus_pub.devid = chipid;
910 devinfo->bus_pub.chiprev = chiprev;
Arend van Spriel71bb2442012-02-09 21:09:08 +0100911 }
912 return true;
913}
914
915static int
916brcmf_usb_resetcfg(struct brcmf_usbdev_info *devinfo)
917{
918 struct bootrom_id_le id;
919 u16 wait = 0, wait_time;
920
921 brcmf_dbg(TRACE, "enter\n");
922
923 if (devinfo == NULL)
924 return -EINVAL;
925
926 /* Give dongle chance to boot */
927 wait_time = BRCMF_USB_DLIMAGE_SPINWAIT;
928 while (wait < BRCMF_USB_DLIMAGE_LIMIT) {
929 mdelay(wait_time);
930 wait += wait_time;
931 id.chip = cpu_to_le32(0xDEAD); /* Get the ID */
932 brcmf_usb_dl_cmd(devinfo, DL_GETVER, &id,
933 sizeof(struct bootrom_id_le));
934 if (id.chip == cpu_to_le32(BRCMF_POSTBOOT_ID))
935 break;
936 }
937
938 if (id.chip == cpu_to_le32(BRCMF_POSTBOOT_ID)) {
939 brcmf_dbg(INFO, "download done %d ms postboot chip 0x%x/rev 0x%x\n",
940 wait, le32_to_cpu(id.chip), le32_to_cpu(id.chiprev));
941
942 brcmf_usb_dl_cmd(devinfo, DL_RESETCFG, &id,
943 sizeof(struct bootrom_id_le));
944
945 /* XXX this wait may not be necessary */
946 mdelay(BRCMF_USB_RESETCFG_SPINWAIT);
947 return 0;
948 } else {
949 brcmf_dbg(ERROR, "Cannot talk to Dongle. Firmware is not UP, %d ms\n",
950 wait);
951 return -EINVAL;
952 }
953}
954
955
956static int
957brcmf_usb_dl_send_bulk(struct brcmf_usbdev_info *devinfo, void *buffer, int len)
958{
959 int ret;
960
961 if ((devinfo == NULL) || (devinfo->bulk_urb == NULL))
962 return -EINVAL;
963
964 /* Prepare the URB */
965 usb_fill_bulk_urb(devinfo->bulk_urb, devinfo->usbdev,
966 devinfo->tx_pipe, buffer, len,
967 (usb_complete_t)brcmf_usb_sync_complete, devinfo);
968
969 devinfo->bulk_urb->transfer_flags |= URB_ZERO_PACKET;
970
971 ret = usb_submit_urb(devinfo->bulk_urb, GFP_ATOMIC);
972 if (ret) {
973 brcmf_dbg(ERROR, "usb_submit_urb failed %d\n", ret);
974 return ret;
975 }
976 ret = brcmf_usb_sync_wait(devinfo, BRCMF_USB_SYNC_TIMEOUT);
977 return ret;
978}
979
980static int
981brcmf_usb_dl_writeimage(struct brcmf_usbdev_info *devinfo, u8 *fw, int fwlen)
982{
983 unsigned int sendlen, sent, dllen;
984 char *bulkchunk = NULL, *dlpos;
985 struct rdl_state_le state;
986 u32 rdlstate, rdlbytes;
987 int err = 0;
988 brcmf_dbg(TRACE, "fw %p, len %d\n", fw, fwlen);
989
990 bulkchunk = kmalloc(RDL_CHUNK, GFP_ATOMIC);
991 if (bulkchunk == NULL) {
992 err = -ENOMEM;
993 goto fail;
994 }
995
996 /* 1) Prepare USB boot loader for runtime image */
997 brcmf_usb_dl_cmd(devinfo, DL_START, &state,
998 sizeof(struct rdl_state_le));
999
1000 rdlstate = le32_to_cpu(state.state);
1001 rdlbytes = le32_to_cpu(state.bytes);
1002
1003 /* 2) Check we are in the Waiting state */
1004 if (rdlstate != DL_WAITING) {
1005 brcmf_dbg(ERROR, "Failed to DL_START\n");
1006 err = -EINVAL;
1007 goto fail;
1008 }
1009 sent = 0;
1010 dlpos = fw;
1011 dllen = fwlen;
1012
1013 /* Get chip id and rev */
1014 while (rdlbytes != dllen) {
1015 /* Wait until the usb device reports it received all
1016 * the bytes we sent */
1017 if ((rdlbytes == sent) && (rdlbytes != dllen)) {
1018 if ((dllen-sent) < RDL_CHUNK)
1019 sendlen = dllen-sent;
1020 else
1021 sendlen = RDL_CHUNK;
1022
1023 /* simply avoid having to send a ZLP by ensuring we
1024 * never have an even
1025 * multiple of 64
1026 */
1027 if (!(sendlen % 64))
1028 sendlen -= 4;
1029
1030 /* send data */
1031 memcpy(bulkchunk, dlpos, sendlen);
1032 if (brcmf_usb_dl_send_bulk(devinfo, bulkchunk,
1033 sendlen)) {
1034 brcmf_dbg(ERROR, "send_bulk failed\n");
1035 err = -EINVAL;
1036 goto fail;
1037 }
1038
1039 dlpos += sendlen;
1040 sent += sendlen;
1041 }
1042 if (!brcmf_usb_dl_cmd(devinfo, DL_GETSTATE, &state,
1043 sizeof(struct rdl_state_le))) {
1044 brcmf_dbg(ERROR, "DL_GETSTATE Failed xxxx\n");
1045 err = -EINVAL;
1046 goto fail;
1047 }
1048
1049 rdlstate = le32_to_cpu(state.state);
1050 rdlbytes = le32_to_cpu(state.bytes);
1051
1052 /* restart if an error is reported */
1053 if (rdlstate == DL_BAD_HDR || rdlstate == DL_BAD_CRC) {
1054 brcmf_dbg(ERROR, "Bad Hdr or Bad CRC state %d\n",
1055 rdlstate);
1056 err = -EINVAL;
1057 goto fail;
1058 }
1059 }
1060
1061fail:
1062 kfree(bulkchunk);
1063 brcmf_dbg(TRACE, "err=%d\n", err);
1064 return err;
1065}
1066
1067static int brcmf_usb_dlstart(struct brcmf_usbdev_info *devinfo, u8 *fw, int len)
1068{
1069 int err;
1070
1071 brcmf_dbg(TRACE, "enter\n");
1072
1073 if (devinfo == NULL)
1074 return -EINVAL;
1075
Arend van Sprielac94f192012-03-02 22:55:46 +01001076 if (devinfo->bus_pub.devid == 0xDEAD)
Arend van Spriel71bb2442012-02-09 21:09:08 +01001077 return -EINVAL;
1078
1079 err = brcmf_usb_dl_writeimage(devinfo, fw, len);
1080 if (err == 0)
1081 devinfo->bus_pub.state = BCMFMAC_USB_STATE_DL_DONE;
1082 else
1083 devinfo->bus_pub.state = BCMFMAC_USB_STATE_DL_PENDING;
1084 brcmf_dbg(TRACE, "exit: err=%d\n", err);
1085
1086 return err;
1087}
1088
1089static int brcmf_usb_dlrun(struct brcmf_usbdev_info *devinfo)
1090{
1091 struct rdl_state_le state;
1092
1093 brcmf_dbg(TRACE, "enter\n");
1094 if (!devinfo)
1095 return -EINVAL;
1096
Arend van Sprielac94f192012-03-02 22:55:46 +01001097 if (devinfo->bus_pub.devid == 0xDEAD)
Arend van Spriel71bb2442012-02-09 21:09:08 +01001098 return -EINVAL;
1099
1100 /* Check we are runnable */
1101 brcmf_usb_dl_cmd(devinfo, DL_GETSTATE, &state,
1102 sizeof(struct rdl_state_le));
1103
1104 /* Start the image */
1105 if (state.state == cpu_to_le32(DL_RUNNABLE)) {
1106 if (!brcmf_usb_dl_cmd(devinfo, DL_GO, &state,
1107 sizeof(struct rdl_state_le)))
1108 return -ENODEV;
1109 if (brcmf_usb_resetcfg(devinfo))
1110 return -ENODEV;
1111 /* The Dongle may go for re-enumeration. */
1112 } else {
1113 brcmf_dbg(ERROR, "Dongle not runnable\n");
1114 return -EINVAL;
1115 }
1116 brcmf_dbg(TRACE, "exit\n");
1117 return 0;
1118}
1119
1120static bool brcmf_usb_chip_support(int chipid, int chiprev)
1121{
1122 switch(chipid) {
Hante Meuleman70f08222012-08-30 19:43:01 +02001123 case 43143:
1124 return true;
Arend van Spriel71bb2442012-02-09 21:09:08 +01001125 case 43235:
1126 case 43236:
1127 case 43238:
1128 return (chiprev == 3);
Hante Meuleman1212d372012-08-30 19:43:00 +02001129 case 43242:
1130 return true;
Arend van Spriel71bb2442012-02-09 21:09:08 +01001131 default:
1132 break;
1133 }
1134 return false;
1135}
1136
1137static int
1138brcmf_usb_fw_download(struct brcmf_usbdev_info *devinfo)
1139{
Arend van Sprielac94f192012-03-02 22:55:46 +01001140 int devid, chiprev;
Arend van Spriel71bb2442012-02-09 21:09:08 +01001141 int err;
1142
1143 brcmf_dbg(TRACE, "enter\n");
1144 if (devinfo == NULL)
1145 return -ENODEV;
1146
Arend van Sprielac94f192012-03-02 22:55:46 +01001147 devid = devinfo->bus_pub.devid;
1148 chiprev = devinfo->bus_pub.chiprev;
Arend van Spriel71bb2442012-02-09 21:09:08 +01001149
Arend van Sprielac94f192012-03-02 22:55:46 +01001150 if (!brcmf_usb_chip_support(devid, chiprev)) {
Arend van Spriel71bb2442012-02-09 21:09:08 +01001151 brcmf_dbg(ERROR, "unsupported chip %d rev %d\n",
Arend van Sprielac94f192012-03-02 22:55:46 +01001152 devid, chiprev);
Arend van Spriel71bb2442012-02-09 21:09:08 +01001153 return -EINVAL;
1154 }
1155
1156 if (!devinfo->image) {
1157 brcmf_dbg(ERROR, "No firmware!\n");
1158 return -ENOENT;
1159 }
1160
1161 err = brcmf_usb_dlstart(devinfo,
1162 devinfo->image, devinfo->image_len);
1163 if (err == 0)
1164 err = brcmf_usb_dlrun(devinfo);
1165 return err;
1166}
1167
1168
1169static void brcmf_usb_detach(const struct brcmf_usbdev *bus_pub)
1170{
1171 struct brcmf_usbdev_info *devinfo =
1172 (struct brcmf_usbdev_info *)bus_pub;
1173
1174 brcmf_dbg(TRACE, "devinfo %p\n", devinfo);
1175
1176 /* store the image globally */
1177 g_image.data = devinfo->image;
1178 g_image.len = devinfo->image_len;
1179
1180 /* free the URBS */
1181 brcmf_usb_free_q(&devinfo->rx_freeq, false);
1182 brcmf_usb_free_q(&devinfo->tx_freeq, false);
1183
1184 usb_free_urb(devinfo->intr_urb);
1185 usb_free_urb(devinfo->ctl_urb);
1186 usb_free_urb(devinfo->bulk_urb);
1187
1188 kfree(devinfo->tx_reqs);
1189 kfree(devinfo->rx_reqs);
1190 kfree(devinfo);
1191}
1192
1193#define TRX_MAGIC 0x30524448 /* "HDR0" */
1194#define TRX_VERSION 1 /* Version 1 */
1195#define TRX_MAX_LEN 0x3B0000 /* Max length */
1196#define TRX_NO_HEADER 1 /* Do not write TRX header */
1197#define TRX_MAX_OFFSET 3 /* Max number of individual files */
1198#define TRX_UNCOMP_IMAGE 0x20 /* Trx contains uncompressed image */
1199
1200struct trx_header_le {
1201 __le32 magic; /* "HDR0" */
1202 __le32 len; /* Length of file including header */
1203 __le32 crc32; /* CRC from flag_version to end of file */
1204 __le32 flag_version; /* 0:15 flags, 16:31 version */
1205 __le32 offsets[TRX_MAX_OFFSET]; /* Offsets of partitions from start of
1206 * header */
1207};
1208
1209static int check_file(const u8 *headers)
1210{
1211 struct trx_header_le *trx;
1212 int actual_len = -1;
1213
1214 /* Extract trx header */
1215 trx = (struct trx_header_le *) headers;
1216 if (trx->magic != cpu_to_le32(TRX_MAGIC))
1217 return -1;
1218
1219 headers += sizeof(struct trx_header_le);
1220
1221 if (le32_to_cpu(trx->flag_version) & TRX_UNCOMP_IMAGE) {
1222 actual_len = le32_to_cpu(trx->offsets[TRX_OFFSETS_DLFWLEN_IDX]);
1223 return actual_len + sizeof(struct trx_header_le);
1224 }
1225 return -1;
1226}
1227
1228static int brcmf_usb_get_fw(struct brcmf_usbdev_info *devinfo)
1229{
1230 s8 *fwname;
1231 const struct firmware *fw;
1232 int err;
1233
1234 devinfo->image = g_image.data;
1235 devinfo->image_len = g_image.len;
1236
1237 /*
1238 * if we have an image we can leave here.
1239 */
1240 if (devinfo->image)
1241 return 0;
1242
Hante Meuleman1212d372012-08-30 19:43:00 +02001243 switch (devinfo->bus_pub.devid) {
Hante Meuleman70f08222012-08-30 19:43:01 +02001244 case 43143:
1245 fwname = BRCMF_USB_43143_FW_NAME;
1246 break;
Hante Meuleman1212d372012-08-30 19:43:00 +02001247 case 43235:
1248 case 43236:
1249 case 43238:
1250 fwname = BRCMF_USB_43236_FW_NAME;
1251 break;
1252 case 43242:
1253 fwname = BRCMF_USB_43242_FW_NAME;
1254 break;
1255 default:
1256 return -EINVAL;
1257 break;
1258 }
Arend van Spriel71bb2442012-02-09 21:09:08 +01001259
1260 err = request_firmware(&fw, fwname, devinfo->dev);
1261 if (!fw) {
1262 brcmf_dbg(ERROR, "fail to request firmware %s\n", fwname);
1263 return err;
1264 }
1265 if (check_file(fw->data) < 0) {
1266 brcmf_dbg(ERROR, "invalid firmware %s\n", fwname);
1267 return -EINVAL;
1268 }
1269
Hauke Mehrtensedb9bc92012-05-18 20:22:53 +02001270 devinfo->image = vmalloc(fw->size); /* plus nvram */
Arend van Spriel71bb2442012-02-09 21:09:08 +01001271 if (!devinfo->image)
1272 return -ENOMEM;
1273
1274 memcpy(devinfo->image, fw->data, fw->size);
1275 devinfo->image_len = fw->size;
1276
1277 release_firmware(fw);
1278 return 0;
1279}
1280
1281
1282static
1283struct brcmf_usbdev *brcmf_usb_attach(int nrxq, int ntxq, struct device *dev)
1284{
1285 struct brcmf_usbdev_info *devinfo;
1286
1287 devinfo = kzalloc(sizeof(struct brcmf_usbdev_info), GFP_ATOMIC);
1288 if (devinfo == NULL)
1289 return NULL;
1290
1291 devinfo->bus_pub.nrxq = nrxq;
1292 devinfo->rx_low_watermark = nrxq / 2;
1293 devinfo->bus_pub.devinfo = devinfo;
1294 devinfo->bus_pub.ntxq = ntxq;
1295
1296 /* flow control when too many tx urbs posted */
1297 devinfo->tx_low_watermark = ntxq / 4;
1298 devinfo->tx_high_watermark = devinfo->tx_low_watermark * 3;
1299 devinfo->dev = dev;
1300 devinfo->usbdev = usbdev_probe_info.usb;
1301 devinfo->tx_pipe = usbdev_probe_info.tx_pipe;
1302 devinfo->rx_pipe = usbdev_probe_info.rx_pipe;
1303 devinfo->rx_pipe2 = usbdev_probe_info.rx_pipe2;
1304 devinfo->intr_pipe = usbdev_probe_info.intr_pipe;
1305
1306 devinfo->interval = usbdev_probe_info.interval;
1307 devinfo->intr_size = usbdev_probe_info.intr_size;
1308
1309 memcpy(&devinfo->probe_info, &usbdev_probe_info,
1310 sizeof(struct brcmf_usb_probe_info));
1311 devinfo->bus_pub.bus_mtu = BRCMF_USB_MAX_PKT_SIZE;
1312
1313 /* Initialize other structure content */
1314 init_waitqueue_head(&devinfo->ioctl_resp_wait);
1315
1316 /* Initialize the spinlocks */
1317 spin_lock_init(&devinfo->qlock);
1318
1319 INIT_LIST_HEAD(&devinfo->rx_freeq);
1320 INIT_LIST_HEAD(&devinfo->rx_postq);
1321
1322 INIT_LIST_HEAD(&devinfo->tx_freeq);
1323 INIT_LIST_HEAD(&devinfo->tx_postq);
1324
Hante Meulemanc6ab4292012-09-11 21:18:48 +02001325 devinfo->tx_flowblock = false;
1326
Arend van Spriel71bb2442012-02-09 21:09:08 +01001327 devinfo->rx_reqs = brcmf_usbdev_qinit(&devinfo->rx_freeq, nrxq);
1328 if (!devinfo->rx_reqs)
1329 goto error;
1330
1331 devinfo->tx_reqs = brcmf_usbdev_qinit(&devinfo->tx_freeq, ntxq);
1332 if (!devinfo->tx_reqs)
1333 goto error;
Hante Meulemanc6ab4292012-09-11 21:18:48 +02001334 devinfo->tx_freecount = ntxq;
Arend van Spriel71bb2442012-02-09 21:09:08 +01001335
1336 devinfo->intr_urb = usb_alloc_urb(0, GFP_ATOMIC);
1337 if (!devinfo->intr_urb) {
1338 brcmf_dbg(ERROR, "usb_alloc_urb (intr) failed\n");
1339 goto error;
1340 }
1341 devinfo->ctl_urb = usb_alloc_urb(0, GFP_ATOMIC);
1342 if (!devinfo->ctl_urb) {
1343 brcmf_dbg(ERROR, "usb_alloc_urb (ctl) failed\n");
1344 goto error;
1345 }
1346 devinfo->rxctl_deferrespok = 0;
1347
1348 devinfo->bulk_urb = usb_alloc_urb(0, GFP_ATOMIC);
1349 if (!devinfo->bulk_urb) {
1350 brcmf_dbg(ERROR, "usb_alloc_urb (bulk) failed\n");
1351 goto error;
1352 }
1353
1354 init_waitqueue_head(&devinfo->wait);
1355 if (!brcmf_usb_dlneeded(devinfo))
1356 return &devinfo->bus_pub;
1357
1358 brcmf_dbg(TRACE, "start fw downloading\n");
1359 if (brcmf_usb_get_fw(devinfo))
1360 goto error;
1361
1362 if (brcmf_usb_fw_download(devinfo))
1363 goto error;
1364
1365 return &devinfo->bus_pub;
1366
1367error:
1368 brcmf_dbg(ERROR, "failed!\n");
1369 brcmf_usb_detach(&devinfo->bus_pub);
1370 return NULL;
1371}
1372
1373static int brcmf_usb_probe_cb(struct device *dev, const char *desc,
1374 u32 bustype, u32 hdrlen)
1375{
1376 struct brcmf_bus *bus = NULL;
1377 struct brcmf_usbdev *bus_pub = NULL;
1378 int ret;
1379
1380
1381 bus_pub = brcmf_usb_attach(BRCMF_USB_NRXQ, BRCMF_USB_NTXQ, dev);
1382 if (!bus_pub) {
1383 ret = -ENODEV;
1384 goto fail;
1385 }
1386
1387 bus = kzalloc(sizeof(struct brcmf_bus), GFP_ATOMIC);
1388 if (!bus) {
1389 ret = -ENOMEM;
1390 goto fail;
1391 }
1392
1393 bus_pub->bus = bus;
1394 bus->brcmf_bus_txdata = brcmf_usb_tx;
1395 bus->brcmf_bus_init = brcmf_usb_up;
1396 bus->brcmf_bus_stop = brcmf_usb_down;
1397 bus->brcmf_bus_txctl = brcmf_usb_tx_ctlpkt;
1398 bus->brcmf_bus_rxctl = brcmf_usb_rx_ctlpkt;
1399 bus->type = bustype;
1400 bus->bus_priv.usb = bus_pub;
1401 dev_set_drvdata(dev, bus);
1402
1403 /* Attach to the common driver interface */
1404 ret = brcmf_attach(hdrlen, dev);
1405 if (ret) {
1406 brcmf_dbg(ERROR, "dhd_attach failed\n");
1407 goto fail;
1408 }
1409
1410 ret = brcmf_bus_start(dev);
1411 if (ret == -ENOLINK) {
1412 brcmf_dbg(ERROR, "dongle is not responding\n");
1413 brcmf_detach(dev);
1414 goto fail;
1415 }
1416
Arend van Spriel71bb2442012-02-09 21:09:08 +01001417 return 0;
1418fail:
1419 /* Release resources in reverse order */
1420 if (bus_pub)
1421 brcmf_usb_detach(bus_pub);
1422 kfree(bus);
1423 return ret;
1424}
1425
1426static void
1427brcmf_usb_disconnect_cb(struct brcmf_usbdev *bus_pub)
1428{
1429 if (!bus_pub)
1430 return;
1431 brcmf_dbg(TRACE, "enter: bus_pub %p\n", bus_pub);
1432
1433 brcmf_detach(bus_pub->devinfo->dev);
1434 kfree(bus_pub->bus);
1435 brcmf_usb_detach(bus_pub);
1436
1437}
1438
1439static int
1440brcmf_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
1441{
1442 int ep;
1443 struct usb_endpoint_descriptor *endpoint;
1444 int ret = 0;
1445 struct usb_device *usb = interface_to_usbdev(intf);
1446 int num_of_eps;
1447 u8 endpoint_num;
1448
1449 brcmf_dbg(TRACE, "enter\n");
1450
1451 usbdev_probe_info.usb = usb;
1452 usbdev_probe_info.intf = intf;
1453
1454 if (id != NULL) {
1455 usbdev_probe_info.vid = id->idVendor;
1456 usbdev_probe_info.pid = id->idProduct;
1457 }
1458
1459 usb_set_intfdata(intf, &usbdev_probe_info);
1460
1461 /* Check that the device supports only one configuration */
1462 if (usb->descriptor.bNumConfigurations != 1) {
1463 ret = -1;
1464 goto fail;
1465 }
1466
1467 if (usb->descriptor.bDeviceClass != USB_CLASS_VENDOR_SPEC) {
1468 ret = -1;
1469 goto fail;
1470 }
1471
1472 /*
1473 * Only the BDC interface configuration is supported:
1474 * Device class: USB_CLASS_VENDOR_SPEC
1475 * if0 class: USB_CLASS_VENDOR_SPEC
1476 * if0/ep0: control
1477 * if0/ep1: bulk in
1478 * if0/ep2: bulk out (ok if swapped with bulk in)
1479 */
1480 if (CONFIGDESC(usb)->bNumInterfaces != 1) {
1481 ret = -1;
1482 goto fail;
1483 }
1484
1485 /* Check interface */
1486 if (IFDESC(usb, CONTROL_IF).bInterfaceClass != USB_CLASS_VENDOR_SPEC ||
1487 IFDESC(usb, CONTROL_IF).bInterfaceSubClass != 2 ||
1488 IFDESC(usb, CONTROL_IF).bInterfaceProtocol != 0xff) {
1489 brcmf_dbg(ERROR, "invalid control interface: class %d, subclass %d, proto %d\n",
1490 IFDESC(usb, CONTROL_IF).bInterfaceClass,
1491 IFDESC(usb, CONTROL_IF).bInterfaceSubClass,
1492 IFDESC(usb, CONTROL_IF).bInterfaceProtocol);
1493 ret = -1;
1494 goto fail;
1495 }
1496
1497 /* Check control endpoint */
1498 endpoint = &IFEPDESC(usb, CONTROL_IF, 0);
1499 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1500 != USB_ENDPOINT_XFER_INT) {
1501 brcmf_dbg(ERROR, "invalid control endpoint %d\n",
1502 endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
1503 ret = -1;
1504 goto fail;
1505 }
1506
1507 endpoint_num = endpoint->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1508 usbdev_probe_info.intr_pipe = usb_rcvintpipe(usb, endpoint_num);
1509
1510 usbdev_probe_info.rx_pipe = 0;
1511 usbdev_probe_info.rx_pipe2 = 0;
1512 usbdev_probe_info.tx_pipe = 0;
1513 num_of_eps = IFDESC(usb, BULK_IF).bNumEndpoints - 1;
1514
1515 /* Check data endpoints and get pipes */
1516 for (ep = 1; ep <= num_of_eps; ep++) {
1517 endpoint = &IFEPDESC(usb, BULK_IF, ep);
1518 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
1519 USB_ENDPOINT_XFER_BULK) {
1520 brcmf_dbg(ERROR, "invalid data endpoint %d\n", ep);
1521 ret = -1;
1522 goto fail;
1523 }
1524
1525 endpoint_num = endpoint->bEndpointAddress &
1526 USB_ENDPOINT_NUMBER_MASK;
1527 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1528 == USB_DIR_IN) {
1529 if (!usbdev_probe_info.rx_pipe) {
1530 usbdev_probe_info.rx_pipe =
1531 usb_rcvbulkpipe(usb, endpoint_num);
1532 } else {
1533 usbdev_probe_info.rx_pipe2 =
1534 usb_rcvbulkpipe(usb, endpoint_num);
1535 }
1536 } else {
1537 usbdev_probe_info.tx_pipe =
1538 usb_sndbulkpipe(usb, endpoint_num);
1539 }
1540 }
1541
1542 /* Allocate interrupt URB and data buffer */
1543 /* RNDIS says 8-byte intr, our old drivers used 4-byte */
1544 if (IFEPDESC(usb, CONTROL_IF, 0).wMaxPacketSize == cpu_to_le16(16))
1545 usbdev_probe_info.intr_size = 8;
1546 else
1547 usbdev_probe_info.intr_size = 4;
1548
1549 usbdev_probe_info.interval = IFEPDESC(usb, CONTROL_IF, 0).bInterval;
1550
1551 usbdev_probe_info.device_speed = usb->speed;
1552 if (usb->speed == USB_SPEED_HIGH)
1553 brcmf_dbg(INFO, "Broadcom high speed USB wireless device detected\n");
1554 else
1555 brcmf_dbg(INFO, "Broadcom full speed USB wireless device detected\n");
1556
1557 ret = brcmf_usb_probe_cb(&usb->dev, "", USB_BUS, 0);
1558 if (ret)
1559 goto fail;
1560
1561 /* Success */
1562 return 0;
1563
1564fail:
1565 brcmf_dbg(ERROR, "failed with errno %d\n", ret);
1566 usb_set_intfdata(intf, NULL);
1567 return ret;
1568
1569}
1570
1571static void
1572brcmf_usb_disconnect(struct usb_interface *intf)
1573{
1574 struct usb_device *usb = interface_to_usbdev(intf);
1575
1576 brcmf_dbg(TRACE, "enter\n");
1577 brcmf_usb_disconnect_cb(brcmf_usb_get_buspub(&usb->dev));
1578 usb_set_intfdata(intf, NULL);
1579}
1580
1581/*
1582 * only need to signal the bus being down and update the suspend state.
1583 */
1584static int brcmf_usb_suspend(struct usb_interface *intf, pm_message_t state)
1585{
1586 struct usb_device *usb = interface_to_usbdev(intf);
1587 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(&usb->dev);
1588
1589 brcmf_dbg(TRACE, "enter\n");
1590 devinfo->bus_pub.state = BCMFMAC_USB_STATE_DOWN;
1591 devinfo->suspend_state = USBOS_SUSPEND_STATE_SUSPENDED;
1592 return 0;
1593}
1594
1595/*
1596 * mark suspend state active and crank up the bus.
1597 */
1598static int brcmf_usb_resume(struct usb_interface *intf)
1599{
1600 struct usb_device *usb = interface_to_usbdev(intf);
1601 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(&usb->dev);
1602
1603 brcmf_dbg(TRACE, "enter\n");
1604 devinfo->suspend_state = USBOS_SUSPEND_STATE_DEVICE_ACTIVE;
1605 brcmf_bus_start(&usb->dev);
1606 return 0;
1607}
1608
1609#define BRCMF_USB_VENDOR_ID_BROADCOM 0x0a5c
Hante Meuleman70f08222012-08-30 19:43:01 +02001610#define BRCMF_USB_DEVICE_ID_43143 0xbd1e
Arend van Spriel71bb2442012-02-09 21:09:08 +01001611#define BRCMF_USB_DEVICE_ID_43236 0xbd17
Hante Meuleman1212d372012-08-30 19:43:00 +02001612#define BRCMF_USB_DEVICE_ID_43242 0xbd1f
Arend van Spriel71bb2442012-02-09 21:09:08 +01001613#define BRCMF_USB_DEVICE_ID_BCMFW 0x0bdc
1614
1615static struct usb_device_id brcmf_usb_devid_table[] = {
Hante Meuleman70f08222012-08-30 19:43:01 +02001616 { USB_DEVICE(BRCMF_USB_VENDOR_ID_BROADCOM, BRCMF_USB_DEVICE_ID_43143) },
Arend van Spriel71bb2442012-02-09 21:09:08 +01001617 { USB_DEVICE(BRCMF_USB_VENDOR_ID_BROADCOM, BRCMF_USB_DEVICE_ID_43236) },
Hante Meuleman1212d372012-08-30 19:43:00 +02001618 { USB_DEVICE(BRCMF_USB_VENDOR_ID_BROADCOM, BRCMF_USB_DEVICE_ID_43242) },
Arend van Spriel71bb2442012-02-09 21:09:08 +01001619 /* special entry for device with firmware loaded and running */
1620 { USB_DEVICE(BRCMF_USB_VENDOR_ID_BROADCOM, BRCMF_USB_DEVICE_ID_BCMFW) },
1621 { }
1622};
1623MODULE_DEVICE_TABLE(usb, brcmf_usb_devid_table);
Hante Meuleman70f08222012-08-30 19:43:01 +02001624MODULE_FIRMWARE(BRCMF_USB_43143_FW_NAME);
Rafał Miłeckifda82412012-02-24 07:22:51 +01001625MODULE_FIRMWARE(BRCMF_USB_43236_FW_NAME);
Hante Meuleman1212d372012-08-30 19:43:00 +02001626MODULE_FIRMWARE(BRCMF_USB_43242_FW_NAME);
Arend van Spriel71bb2442012-02-09 21:09:08 +01001627
1628/* TODO: suspend and resume entries */
1629static struct usb_driver brcmf_usbdrvr = {
1630 .name = KBUILD_MODNAME,
1631 .probe = brcmf_usb_probe,
1632 .disconnect = brcmf_usb_disconnect,
1633 .id_table = brcmf_usb_devid_table,
1634 .suspend = brcmf_usb_suspend,
1635 .resume = brcmf_usb_resume,
Sarah Sharpc51fa662012-05-21 05:34:24 -07001636 .supports_autosuspend = 1,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07001637 .disable_hub_initiated_lpm = 1,
Arend van Spriel71bb2442012-02-09 21:09:08 +01001638};
1639
1640void brcmf_usb_exit(void)
1641{
1642 usb_deregister(&brcmf_usbdrvr);
Hauke Mehrtensedb9bc92012-05-18 20:22:53 +02001643 vfree(g_image.data);
Arend van Spriel71bb2442012-02-09 21:09:08 +01001644 g_image.data = NULL;
1645 g_image.len = 0;
1646}
1647
Arend van Spriel549040a2012-03-02 22:55:48 +01001648void brcmf_usb_init(void)
Arend van Spriel71bb2442012-02-09 21:09:08 +01001649{
Arend van Spriel549040a2012-03-02 22:55:48 +01001650 usb_register(&brcmf_usbdrvr);
Arend van Spriel71bb2442012-02-09 21:09:08 +01001651}