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