brcmfmac: remove unnecessary EXPORT_SYMBOL() usage
[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>
e2ff0498 21#include <linux/vmalloc.h>
71bb244b 22
71bb244b
AS
23#include <brcmu_utils.h>
24#include <brcmu_wifi.h>
25#include <dhd_bus.h>
26#include <dhd_dbg.h>
27
28#include "usb_rdl.h"
29#include "usb.h"
30
31#define IOCTL_RESP_TIMEOUT 2000
32
83bc9c31
HM
33#define BRCMF_USB_RESET_GETVER_SPINWAIT 100 /* in unit of ms */
34#define BRCMF_USB_RESET_GETVER_LOOP_CNT 10
71bb244b
AS
35
36#define BRCMF_POSTBOOT_ID 0xA123 /* ID to detect if dongle
37 has boot up */
71bb244b
AS
38#define BRCMF_USB_NRXQ 50
39#define BRCMF_USB_NTXQ 50
40
41#define CONFIGDESC(usb) (&((usb)->actconfig)->desc)
42#define IFPTR(usb, idx) ((usb)->actconfig->interface[(idx)])
43#define IFALTS(usb, idx) (IFPTR((usb), (idx))->altsetting[0])
44#define IFDESC(usb, idx) IFALTS((usb), (idx)).desc
45#define IFEPDESC(usb, idx, ep) (IFALTS((usb), (idx)).endpoint[(ep)]).desc
46
47#define CONTROL_IF 0
48#define BULK_IF 0
49
50#define BRCMF_USB_CBCTL_WRITE 0
51#define BRCMF_USB_CBCTL_READ 1
52#define BRCMF_USB_MAX_PKT_SIZE 1600
53
70f0822c 54#define BRCMF_USB_43143_FW_NAME "brcm/brcmfmac43143.bin"
fda82417 55#define BRCMF_USB_43236_FW_NAME "brcm/brcmfmac43236b.bin"
1212d370 56#define BRCMF_USB_43242_FW_NAME "brcm/brcmfmac43242a.bin"
71bb244b 57
71bb244b 58struct brcmf_usb_image {
803599d4
HM
59 struct list_head list;
60 s8 *fwname;
61 u8 *image;
62 int image_len;
71bb244b 63};
803599d4 64static struct list_head fw_image_list;
71bb244b
AS
65
66struct intr_transfer_buf {
67 u32 notification;
68 u32 reserved;
69};
70
71struct brcmf_usbdev_info {
72 struct brcmf_usbdev bus_pub; /* MUST BE FIRST */
73 spinlock_t qlock;
74 struct list_head rx_freeq;
75 struct list_head rx_postq;
76 struct list_head tx_freeq;
77 struct list_head tx_postq;
71bb244b
AS
78 uint rx_pipe, tx_pipe, intr_pipe, rx_pipe2;
79
71bb244b
AS
80 int rx_low_watermark;
81 int tx_low_watermark;
82 int tx_high_watermark;
c6ab4294
HM
83 int tx_freecount;
84 bool tx_flowblock;
df50f756 85 spinlock_t tx_flowblock_lock;
71bb244b
AS
86
87 struct brcmf_usbreq *tx_reqs;
88 struct brcmf_usbreq *rx_reqs;
89
90 u8 *image; /* buffer for combine fw and nvram */
91 int image_len;
92
71bb244b
AS
93 struct usb_device *usbdev;
94 struct device *dev;
71bb244b
AS
95
96 int ctl_in_pipe, ctl_out_pipe;
97 struct urb *ctl_urb; /* URB for control endpoint */
98 struct usb_ctrlrequest ctl_write;
99 struct usb_ctrlrequest ctl_read;
100 u32 ctl_urb_actual_length;
101 int ctl_urb_status;
102 int ctl_completed;
103 wait_queue_head_t ioctl_resp_wait;
71bb244b
AS
104 ulong ctl_op;
105
71bb244b
AS
106 struct urb *bulk_urb; /* used for FW download */
107 struct urb *intr_urb; /* URB for interrupt endpoint */
108 int intr_size; /* Size of interrupt message */
109 int interval; /* Interrupt polling interval */
110 struct intr_transfer_buf intr; /* Data buffer for interrupt endpoint */
71bb244b
AS
111};
112
113static void brcmf_usb_rx_refill(struct brcmf_usbdev_info *devinfo,
114 struct brcmf_usbreq *req);
115
71bb244b
AS
116static struct brcmf_usbdev *brcmf_usb_get_buspub(struct device *dev)
117{
118 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
119 return bus_if->bus_priv.usb;
120}
121
122static struct brcmf_usbdev_info *brcmf_usb_get_businfo(struct device *dev)
123{
124 return brcmf_usb_get_buspub(dev)->devinfo;
125}
126
70398a59 127static int brcmf_usb_ioctl_resp_wait(struct brcmf_usbdev_info *devinfo)
71bb244b 128{
70398a59
HM
129 return wait_event_timeout(devinfo->ioctl_resp_wait,
130 devinfo->ctl_completed,
131 msecs_to_jiffies(IOCTL_RESP_TIMEOUT));
71bb244b
AS
132}
133
70398a59 134static void brcmf_usb_ioctl_resp_wake(struct brcmf_usbdev_info *devinfo)
71bb244b
AS
135{
136 if (waitqueue_active(&devinfo->ioctl_resp_wait))
70398a59 137 wake_up(&devinfo->ioctl_resp_wait);
71bb244b
AS
138}
139
140static void
141brcmf_usb_ctl_complete(struct brcmf_usbdev_info *devinfo, int type, int status)
142{
cb8b73da 143 brcmf_dbg(USB, "Enter, status=%d\n", status);
71bb244b
AS
144
145 if (unlikely(devinfo == NULL))
146 return;
147
148 if (type == BRCMF_USB_CBCTL_READ) {
149 if (status == 0)
150 devinfo->bus_pub.stats.rx_ctlpkts++;
151 else
152 devinfo->bus_pub.stats.rx_ctlerrs++;
153 } else if (type == BRCMF_USB_CBCTL_WRITE) {
154 if (status == 0)
155 devinfo->bus_pub.stats.tx_ctlpkts++;
156 else
157 devinfo->bus_pub.stats.tx_ctlerrs++;
158 }
159
160 devinfo->ctl_urb_status = status;
161 devinfo->ctl_completed = true;
162 brcmf_usb_ioctl_resp_wake(devinfo);
163}
164
165static void
166brcmf_usb_ctlread_complete(struct urb *urb)
167{
168 struct brcmf_usbdev_info *devinfo =
169 (struct brcmf_usbdev_info *)urb->context;
170
cb8b73da 171 brcmf_dbg(USB, "Enter\n");
71bb244b
AS
172 devinfo->ctl_urb_actual_length = urb->actual_length;
173 brcmf_usb_ctl_complete(devinfo, BRCMF_USB_CBCTL_READ,
174 urb->status);
175}
176
177static void
178brcmf_usb_ctlwrite_complete(struct urb *urb)
179{
180 struct brcmf_usbdev_info *devinfo =
181 (struct brcmf_usbdev_info *)urb->context;
182
cb8b73da 183 brcmf_dbg(USB, "Enter\n");
71bb244b
AS
184 brcmf_usb_ctl_complete(devinfo, BRCMF_USB_CBCTL_WRITE,
185 urb->status);
186}
187
71bb244b
AS
188static int
189brcmf_usb_send_ctl(struct brcmf_usbdev_info *devinfo, u8 *buf, int len)
190{
191 int ret;
192 u16 size;
193
cb8b73da 194 brcmf_dbg(USB, "Enter\n");
71bb244b
AS
195 if (devinfo == NULL || buf == NULL ||
196 len == 0 || devinfo->ctl_urb == NULL)
197 return -EINVAL;
198
71bb244b
AS
199 size = len;
200 devinfo->ctl_write.wLength = cpu_to_le16p(&size);
201 devinfo->ctl_urb->transfer_buffer_length = size;
202 devinfo->ctl_urb_status = 0;
203 devinfo->ctl_urb_actual_length = 0;
204
205 usb_fill_control_urb(devinfo->ctl_urb,
206 devinfo->usbdev,
207 devinfo->ctl_out_pipe,
208 (unsigned char *) &devinfo->ctl_write,
209 buf, size,
210 (usb_complete_t)brcmf_usb_ctlwrite_complete,
211 devinfo);
212
213 ret = usb_submit_urb(devinfo->ctl_urb, GFP_ATOMIC);
214 if (ret < 0)
5e8149f5 215 brcmf_err("usb_submit_urb failed %d\n", ret);
71bb244b
AS
216
217 return ret;
218}
219
220static int
221brcmf_usb_recv_ctl(struct brcmf_usbdev_info *devinfo, u8 *buf, int len)
222{
223 int ret;
224 u16 size;
225
cb8b73da 226 brcmf_dbg(USB, "Enter\n");
71bb244b
AS
227 if ((devinfo == NULL) || (buf == NULL) || (len == 0)
228 || (devinfo->ctl_urb == NULL))
229 return -EINVAL;
230
231 size = len;
232 devinfo->ctl_read.wLength = cpu_to_le16p(&size);
233 devinfo->ctl_urb->transfer_buffer_length = size;
234
81643292
HM
235 devinfo->ctl_read.bRequestType = USB_DIR_IN
236 | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
237 devinfo->ctl_read.bRequest = 1;
71bb244b
AS
238
239 usb_fill_control_urb(devinfo->ctl_urb,
240 devinfo->usbdev,
241 devinfo->ctl_in_pipe,
242 (unsigned char *) &devinfo->ctl_read,
243 buf, size,
244 (usb_complete_t)brcmf_usb_ctlread_complete,
245 devinfo);
246
247 ret = usb_submit_urb(devinfo->ctl_urb, GFP_ATOMIC);
248 if (ret < 0)
5e8149f5 249 brcmf_err("usb_submit_urb failed %d\n", ret);
71bb244b
AS
250
251 return ret;
252}
253
254static int brcmf_usb_tx_ctlpkt(struct device *dev, u8 *buf, u32 len)
255{
256 int err = 0;
257 int timeout = 0;
71bb244b
AS
258 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
259
cb8b73da 260 brcmf_dbg(USB, "Enter\n");
7c38e698 261 if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP)
71bb244b 262 return -EIO;
71bb244b
AS
263
264 if (test_and_set_bit(0, &devinfo->ctl_op))
265 return -EIO;
266
a77f5747 267 devinfo->ctl_completed = false;
71bb244b
AS
268 err = brcmf_usb_send_ctl(devinfo, buf, len);
269 if (err) {
5e8149f5 270 brcmf_err("fail %d bytes: %d\n", err, len);
6385df2d 271 clear_bit(0, &devinfo->ctl_op);
71bb244b
AS
272 return err;
273 }
70398a59 274 timeout = brcmf_usb_ioctl_resp_wait(devinfo);
71bb244b
AS
275 clear_bit(0, &devinfo->ctl_op);
276 if (!timeout) {
5e8149f5 277 brcmf_err("Txctl wait timed out\n");
71bb244b
AS
278 err = -EIO;
279 }
280 return err;
281}
282
283static int brcmf_usb_rx_ctlpkt(struct device *dev, u8 *buf, u32 len)
284{
285 int err = 0;
286 int timeout = 0;
71bb244b
AS
287 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
288
cb8b73da 289 brcmf_dbg(USB, "Enter\n");
7c38e698 290 if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP)
71bb244b 291 return -EIO;
7c38e698 292
71bb244b
AS
293 if (test_and_set_bit(0, &devinfo->ctl_op))
294 return -EIO;
295
70398a59 296 devinfo->ctl_completed = false;
71bb244b
AS
297 err = brcmf_usb_recv_ctl(devinfo, buf, len);
298 if (err) {
5e8149f5 299 brcmf_err("fail %d bytes: %d\n", err, len);
6385df2d 300 clear_bit(0, &devinfo->ctl_op);
71bb244b
AS
301 return err;
302 }
70398a59 303 timeout = brcmf_usb_ioctl_resp_wait(devinfo);
71bb244b
AS
304 err = devinfo->ctl_urb_status;
305 clear_bit(0, &devinfo->ctl_op);
306 if (!timeout) {
5e8149f5 307 brcmf_err("rxctl wait timed out\n");
71bb244b
AS
308 err = -EIO;
309 }
310 if (!err)
311 return devinfo->ctl_urb_actual_length;
312 else
313 return err;
314}
315
316static struct brcmf_usbreq *brcmf_usb_deq(struct brcmf_usbdev_info *devinfo,
c6ab4294 317 struct list_head *q, int *counter)
71bb244b
AS
318{
319 unsigned long flags;
320 struct brcmf_usbreq *req;
321 spin_lock_irqsave(&devinfo->qlock, flags);
322 if (list_empty(q)) {
323 spin_unlock_irqrestore(&devinfo->qlock, flags);
324 return NULL;
325 }
326 req = list_entry(q->next, struct brcmf_usbreq, list);
327 list_del_init(q->next);
c6ab4294
HM
328 if (counter)
329 (*counter)--;
71bb244b
AS
330 spin_unlock_irqrestore(&devinfo->qlock, flags);
331 return req;
332
333}
334
335static void brcmf_usb_enq(struct brcmf_usbdev_info *devinfo,
c6ab4294
HM
336 struct list_head *q, struct brcmf_usbreq *req,
337 int *counter)
71bb244b
AS
338{
339 unsigned long flags;
340 spin_lock_irqsave(&devinfo->qlock, flags);
341 list_add_tail(&req->list, q);
c6ab4294
HM
342 if (counter)
343 (*counter)++;
71bb244b
AS
344 spin_unlock_irqrestore(&devinfo->qlock, flags);
345}
346
347static struct brcmf_usbreq *
348brcmf_usbdev_qinit(struct list_head *q, int qsize)
349{
350 int i;
351 struct brcmf_usbreq *req, *reqs;
352
14f8dc49
JP
353 reqs = kcalloc(qsize, sizeof(struct brcmf_usbreq), GFP_ATOMIC);
354 if (reqs == NULL)
71bb244b 355 return NULL;
14f8dc49 356
71bb244b
AS
357 req = reqs;
358
359 for (i = 0; i < qsize; i++) {
360 req->urb = usb_alloc_urb(0, GFP_ATOMIC);
361 if (!req->urb)
362 goto fail;
363
364 INIT_LIST_HEAD(&req->list);
365 list_add_tail(&req->list, q);
366 req++;
367 }
368 return reqs;
369fail:
5e8149f5 370 brcmf_err("fail!\n");
71bb244b
AS
371 while (!list_empty(q)) {
372 req = list_entry(q->next, struct brcmf_usbreq, list);
373 if (req && req->urb)
374 usb_free_urb(req->urb);
375 list_del(q->next);
376 }
377 return NULL;
378
379}
380
381static void brcmf_usb_free_q(struct list_head *q, bool pending)
382{
383 struct brcmf_usbreq *req, *next;
384 int i = 0;
385 list_for_each_entry_safe(req, next, q, list) {
d4ca0099 386 if (!req->urb) {
5e8149f5 387 brcmf_err("bad req\n");
71bb244b
AS
388 break;
389 }
390 i++;
391 if (pending) {
392 usb_kill_urb(req->urb);
393 } else {
394 usb_free_urb(req->urb);
395 list_del_init(&req->list);
396 }
397 }
398}
399
400static void brcmf_usb_del_fromq(struct brcmf_usbdev_info *devinfo,
401 struct brcmf_usbreq *req)
402{
403 unsigned long flags;
404
405 spin_lock_irqsave(&devinfo->qlock, flags);
406 list_del_init(&req->list);
407 spin_unlock_irqrestore(&devinfo->qlock, flags);
408}
409
410
411static void brcmf_usb_tx_complete(struct urb *urb)
412{
413 struct brcmf_usbreq *req = (struct brcmf_usbreq *)urb->context;
414 struct brcmf_usbdev_info *devinfo = req->devinfo;
df50f756 415 unsigned long flags;
71bb244b 416
cb8b73da
HM
417 brcmf_dbg(USB, "Enter, urb->status=%d, skb=%p\n", urb->status,
418 req->skb);
71bb244b 419 brcmf_usb_del_fromq(devinfo, req);
71bb244b 420
1e2d958b 421 brcmf_txcomplete(devinfo->dev, req->skb, urb->status == 0);
71bb244b 422 req->skb = NULL;
c6ab4294 423 brcmf_usb_enq(devinfo, &devinfo->tx_freeq, req, &devinfo->tx_freecount);
df50f756 424 spin_lock_irqsave(&devinfo->tx_flowblock_lock, flags);
c6ab4294
HM
425 if (devinfo->tx_freecount > devinfo->tx_high_watermark &&
426 devinfo->tx_flowblock) {
427 brcmf_txflowblock(devinfo->dev, false);
428 devinfo->tx_flowblock = false;
429 }
df50f756 430 spin_unlock_irqrestore(&devinfo->tx_flowblock_lock, flags);
71bb244b
AS
431}
432
433static void brcmf_usb_rx_complete(struct urb *urb)
434{
435 struct brcmf_usbreq *req = (struct brcmf_usbreq *)urb->context;
436 struct brcmf_usbdev_info *devinfo = req->devinfo;
437 struct sk_buff *skb;
71bb244b 438
cb8b73da 439 brcmf_dbg(USB, "Enter, urb->status=%d\n", urb->status);
71bb244b
AS
440 brcmf_usb_del_fromq(devinfo, req);
441 skb = req->skb;
442 req->skb = NULL;
443
a43af515 444 /* zero lenght packets indicate usb "failure". Do not refill */
b1a2a411 445 if (urb->status != 0 || !urb->actual_length) {
01e3331b 446 brcmu_pkt_buf_free_skb(skb);
c6ab4294 447 brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
71bb244b
AS
448 return;
449 }
450
7c38e698 451 if (devinfo->bus_pub.state == BRCMFMAC_USB_STATE_UP) {
71bb244b 452 skb_put(skb, urb->actual_length);
05f3820b 453 brcmf_rx_frame(devinfo->dev, skb);
a43af515 454 brcmf_usb_rx_refill(devinfo, req);
71bb244b 455 } else {
01e3331b 456 brcmu_pkt_buf_free_skb(skb);
c6ab4294 457 brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
71bb244b
AS
458 }
459 return;
460
461}
462
463static void brcmf_usb_rx_refill(struct brcmf_usbdev_info *devinfo,
464 struct brcmf_usbreq *req)
465{
466 struct sk_buff *skb;
467 int ret;
468
469 if (!req || !devinfo)
470 return;
471
472 skb = dev_alloc_skb(devinfo->bus_pub.bus_mtu);
473 if (!skb) {
c6ab4294 474 brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
71bb244b
AS
475 return;
476 }
477 req->skb = skb;
478
479 usb_fill_bulk_urb(req->urb, devinfo->usbdev, devinfo->rx_pipe,
480 skb->data, skb_tailroom(skb), brcmf_usb_rx_complete,
481 req);
71bb244b 482 req->devinfo = devinfo;
c6ab4294 483 brcmf_usb_enq(devinfo, &devinfo->rx_postq, req, NULL);
71bb244b
AS
484
485 ret = usb_submit_urb(req->urb, GFP_ATOMIC);
2e875acd
HM
486 if (ret) {
487 brcmf_usb_del_fromq(devinfo, req);
01e3331b 488 brcmu_pkt_buf_free_skb(req->skb);
71bb244b 489 req->skb = NULL;
c6ab4294 490 brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
71bb244b
AS
491 }
492 return;
493}
494
495static void brcmf_usb_rx_fill_all(struct brcmf_usbdev_info *devinfo)
496{
497 struct brcmf_usbreq *req;
498
7c38e698 499 if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP) {
5e8149f5 500 brcmf_err("bus is not up=%d\n", devinfo->bus_pub.state);
71bb244b
AS
501 return;
502 }
c6ab4294 503 while ((req = brcmf_usb_deq(devinfo, &devinfo->rx_freeq, NULL)) != NULL)
71bb244b
AS
504 brcmf_usb_rx_refill(devinfo, req);
505}
506
507static void
508brcmf_usb_state_change(struct brcmf_usbdev_info *devinfo, int state)
509{
510 struct brcmf_bus *bcmf_bus = devinfo->bus_pub.bus;
511 int old_state;
512
cb8b73da
HM
513 brcmf_dbg(USB, "Enter, current state=%d, new state=%d\n",
514 devinfo->bus_pub.state, state);
71bb244b
AS
515
516 if (devinfo->bus_pub.state == state)
517 return;
518
519 old_state = devinfo->bus_pub.state;
7c38e698 520 devinfo->bus_pub.state = state;
71bb244b
AS
521
522 /* update state of upper layer */
7c38e698 523 if (state == BRCMFMAC_USB_STATE_DOWN) {
cb8b73da 524 brcmf_dbg(USB, "DBUS is down\n");
71bb244b 525 bcmf_bus->state = BRCMF_BUS_DOWN;
7c38e698
HM
526 } else if (state == BRCMFMAC_USB_STATE_UP) {
527 brcmf_dbg(USB, "DBUS is up\n");
528 bcmf_bus->state = BRCMF_BUS_DATA;
71bb244b 529 } else {
cb8b73da 530 brcmf_dbg(USB, "DBUS current state=%d\n", state);
71bb244b
AS
531 }
532}
533
534static void
535brcmf_usb_intr_complete(struct urb *urb)
536{
537 struct brcmf_usbdev_info *devinfo =
538 (struct brcmf_usbdev_info *)urb->context;
7c38e698 539 int err;
71bb244b 540
cb8b73da
HM
541 brcmf_dbg(USB, "Enter, urb->status=%d\n", urb->status);
542
71bb244b
AS
543 if (devinfo == NULL)
544 return;
545
546 if (unlikely(urb->status)) {
7c38e698
HM
547 if (urb->status == -ENOENT ||
548 urb->status == -ESHUTDOWN ||
549 urb->status == -ENODEV) {
550 brcmf_usb_state_change(devinfo,
551 BRCMFMAC_USB_STATE_DOWN);
71bb244b
AS
552 }
553 }
554
7c38e698 555 if (devinfo->bus_pub.state == BRCMFMAC_USB_STATE_DOWN) {
5e8149f5 556 brcmf_err("intr cb when DBUS down, ignoring\n");
71bb244b
AS
557 return;
558 }
559
7c38e698
HM
560 if (devinfo->bus_pub.state == BRCMFMAC_USB_STATE_UP) {
561 err = usb_submit_urb(devinfo->intr_urb, GFP_ATOMIC);
562 if (err)
5e8149f5 563 brcmf_err("usb_submit_urb, err=%d\n", err);
7c38e698 564 }
71bb244b
AS
565}
566
567static int brcmf_usb_tx(struct device *dev, struct sk_buff *skb)
568{
569 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
570 struct brcmf_usbreq *req;
571 int ret;
df50f756 572 unsigned long flags;
71bb244b 573
cb8b73da 574 brcmf_dbg(USB, "Enter, skb=%p\n", skb);
6fc9ca13
AS
575 if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP) {
576 ret = -EIO;
577 goto fail;
578 }
71bb244b 579
c6ab4294
HM
580 req = brcmf_usb_deq(devinfo, &devinfo->tx_freeq,
581 &devinfo->tx_freecount);
71bb244b 582 if (!req) {
5e8149f5 583 brcmf_err("no req to send\n");
6fc9ca13
AS
584 ret = -ENOMEM;
585 goto fail;
71bb244b 586 }
71bb244b
AS
587
588 req->skb = skb;
589 req->devinfo = devinfo;
590 usb_fill_bulk_urb(req->urb, devinfo->usbdev, devinfo->tx_pipe,
591 skb->data, skb->len, brcmf_usb_tx_complete, req);
592 req->urb->transfer_flags |= URB_ZERO_PACKET;
c6ab4294 593 brcmf_usb_enq(devinfo, &devinfo->tx_postq, req, NULL);
71bb244b 594 ret = usb_submit_urb(req->urb, GFP_ATOMIC);
2e875acd 595 if (ret) {
5e8149f5 596 brcmf_err("brcmf_usb_tx usb_submit_urb FAILED\n");
2e875acd 597 brcmf_usb_del_fromq(devinfo, req);
71bb244b 598 req->skb = NULL;
c6ab4294 599 brcmf_usb_enq(devinfo, &devinfo->tx_freeq, req,
6fc9ca13
AS
600 &devinfo->tx_freecount);
601 goto fail;
71bb244b
AS
602 }
603
df50f756 604 spin_lock_irqsave(&devinfo->tx_flowblock_lock, flags);
6fc9ca13
AS
605 if (devinfo->tx_freecount < devinfo->tx_low_watermark &&
606 !devinfo->tx_flowblock) {
607 brcmf_txflowblock(dev, true);
608 devinfo->tx_flowblock = true;
609 }
df50f756 610 spin_unlock_irqrestore(&devinfo->tx_flowblock_lock, flags);
6fc9ca13
AS
611 return 0;
612
613fail:
71bb244b
AS
614 return ret;
615}
616
617
618static int brcmf_usb_up(struct device *dev)
619{
620 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
621 u16 ifnum;
7c38e698 622 int ret;
71bb244b 623
cb8b73da 624 brcmf_dbg(USB, "Enter\n");
7c38e698 625 if (devinfo->bus_pub.state == BRCMFMAC_USB_STATE_UP)
d4ca0099
DC
626 return 0;
627
71bb244b 628 /* Success, indicate devinfo is fully up */
7c38e698 629 brcmf_usb_state_change(devinfo, BRCMFMAC_USB_STATE_UP);
71bb244b
AS
630
631 if (devinfo->intr_urb) {
71bb244b
AS
632 usb_fill_int_urb(devinfo->intr_urb, devinfo->usbdev,
633 devinfo->intr_pipe,
634 &devinfo->intr,
635 devinfo->intr_size,
636 (usb_complete_t)brcmf_usb_intr_complete,
637 devinfo,
638 devinfo->interval);
639
640 ret = usb_submit_urb(devinfo->intr_urb, GFP_ATOMIC);
641 if (ret) {
5e8149f5 642 brcmf_err("USB_SUBMIT_URB failed with status %d\n",
71bb244b
AS
643 ret);
644 return -EINVAL;
645 }
646 }
647
648 if (devinfo->ctl_urb) {
649 devinfo->ctl_in_pipe = usb_rcvctrlpipe(devinfo->usbdev, 0);
650 devinfo->ctl_out_pipe = usb_sndctrlpipe(devinfo->usbdev, 0);
651
652 ifnum = IFDESC(devinfo->usbdev, CONTROL_IF).bInterfaceNumber;
653
654 /* CTL Write */
655 devinfo->ctl_write.bRequestType =
656 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
657 devinfo->ctl_write.bRequest = 0;
658 devinfo->ctl_write.wValue = cpu_to_le16(0);
659 devinfo->ctl_write.wIndex = cpu_to_le16p(&ifnum);
660
661 /* CTL Read */
662 devinfo->ctl_read.bRequestType =
663 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
664 devinfo->ctl_read.bRequest = 1;
665 devinfo->ctl_read.wValue = cpu_to_le16(0);
666 devinfo->ctl_read.wIndex = cpu_to_le16p(&ifnum);
667 }
668 brcmf_usb_rx_fill_all(devinfo);
669 return 0;
670}
671
672static void brcmf_usb_down(struct device *dev)
673{
674 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
675
cb8b73da 676 brcmf_dbg(USB, "Enter\n");
71bb244b
AS
677 if (devinfo == NULL)
678 return;
679
7c38e698 680 if (devinfo->bus_pub.state == BRCMFMAC_USB_STATE_DOWN)
71bb244b
AS
681 return;
682
7c38e698 683 brcmf_usb_state_change(devinfo, BRCMFMAC_USB_STATE_DOWN);
71bb244b
AS
684 if (devinfo->intr_urb)
685 usb_kill_urb(devinfo->intr_urb);
686
687 if (devinfo->ctl_urb)
688 usb_kill_urb(devinfo->ctl_urb);
689
690 if (devinfo->bulk_urb)
691 usb_kill_urb(devinfo->bulk_urb);
692 brcmf_usb_free_q(&devinfo->tx_postq, true);
693
694 brcmf_usb_free_q(&devinfo->rx_postq, true);
695}
696
71bb244b
AS
697static void
698brcmf_usb_sync_complete(struct urb *urb)
699{
700 struct brcmf_usbdev_info *devinfo =
701 (struct brcmf_usbdev_info *)urb->context;
702
1e271c95
HM
703 devinfo->ctl_completed = true;
704 brcmf_usb_ioctl_resp_wake(devinfo);
71bb244b
AS
705}
706
707static bool brcmf_usb_dl_cmd(struct brcmf_usbdev_info *devinfo, u8 cmd,
708 void *buffer, int buflen)
709{
710 int ret = 0;
711 char *tmpbuf;
712 u16 size;
713
714 if ((!devinfo) || (devinfo->ctl_urb == NULL))
715 return false;
716
717 tmpbuf = kmalloc(buflen, GFP_ATOMIC);
718 if (!tmpbuf)
719 return false;
720
721 size = buflen;
722 devinfo->ctl_urb->transfer_buffer_length = size;
723
724 devinfo->ctl_read.wLength = cpu_to_le16p(&size);
725 devinfo->ctl_read.bRequestType = USB_DIR_IN | USB_TYPE_VENDOR |
726 USB_RECIP_INTERFACE;
727 devinfo->ctl_read.bRequest = cmd;
728
729 usb_fill_control_urb(devinfo->ctl_urb,
730 devinfo->usbdev,
731 usb_rcvctrlpipe(devinfo->usbdev, 0),
732 (unsigned char *) &devinfo->ctl_read,
733 (void *) tmpbuf, size,
734 (usb_complete_t)brcmf_usb_sync_complete, devinfo);
735
1e271c95 736 devinfo->ctl_completed = false;
71bb244b
AS
737 ret = usb_submit_urb(devinfo->ctl_urb, GFP_ATOMIC);
738 if (ret < 0) {
5e8149f5 739 brcmf_err("usb_submit_urb failed %d\n", ret);
71bb244b
AS
740 kfree(tmpbuf);
741 return false;
742 }
743
1e271c95 744 ret = brcmf_usb_ioctl_resp_wait(devinfo);
71bb244b
AS
745 memcpy(buffer, tmpbuf, buflen);
746 kfree(tmpbuf);
747
1e271c95 748 return ret;
71bb244b
AS
749}
750
751static bool
752brcmf_usb_dlneeded(struct brcmf_usbdev_info *devinfo)
753{
754 struct bootrom_id_le id;
755 u32 chipid, chiprev;
756
cb8b73da 757 brcmf_dbg(USB, "Enter\n");
71bb244b
AS
758
759 if (devinfo == NULL)
760 return false;
761
762 /* Check if firmware downloaded already by querying runtime ID */
763 id.chip = cpu_to_le32(0xDEAD);
83bc9c31 764 brcmf_usb_dl_cmd(devinfo, DL_GETVER, &id, sizeof(id));
71bb244b
AS
765
766 chipid = le32_to_cpu(id.chip);
767 chiprev = le32_to_cpu(id.chiprev);
768
769 if ((chipid & 0x4300) == 0x4300)
cb8b73da 770 brcmf_dbg(USB, "chip %x rev 0x%x\n", chipid, chiprev);
71bb244b 771 else
cb8b73da 772 brcmf_dbg(USB, "chip %d rev 0x%x\n", chipid, chiprev);
71bb244b 773 if (chipid == BRCMF_POSTBOOT_ID) {
cb8b73da 774 brcmf_dbg(USB, "firmware already downloaded\n");
83bc9c31 775 brcmf_usb_dl_cmd(devinfo, DL_RESETCFG, &id, sizeof(id));
71bb244b
AS
776 return false;
777 } else {
ac94f196
AS
778 devinfo->bus_pub.devid = chipid;
779 devinfo->bus_pub.chiprev = chiprev;
71bb244b
AS
780 }
781 return true;
782}
783
784static int
785brcmf_usb_resetcfg(struct brcmf_usbdev_info *devinfo)
786{
787 struct bootrom_id_le id;
83bc9c31 788 u32 loop_cnt;
71bb244b 789
cb8b73da 790 brcmf_dbg(USB, "Enter\n");
71bb244b 791
83bc9c31
HM
792 loop_cnt = 0;
793 do {
794 mdelay(BRCMF_USB_RESET_GETVER_SPINWAIT);
795 loop_cnt++;
71bb244b 796 id.chip = cpu_to_le32(0xDEAD); /* Get the ID */
83bc9c31 797 brcmf_usb_dl_cmd(devinfo, DL_GETVER, &id, sizeof(id));
71bb244b
AS
798 if (id.chip == cpu_to_le32(BRCMF_POSTBOOT_ID))
799 break;
83bc9c31 800 } while (loop_cnt < BRCMF_USB_RESET_GETVER_LOOP_CNT);
71bb244b
AS
801
802 if (id.chip == cpu_to_le32(BRCMF_POSTBOOT_ID)) {
83bc9c31
HM
803 brcmf_dbg(USB, "postboot chip 0x%x/rev 0x%x\n",
804 le32_to_cpu(id.chip), le32_to_cpu(id.chiprev));
71bb244b 805
83bc9c31 806 brcmf_usb_dl_cmd(devinfo, DL_RESETCFG, &id, sizeof(id));
71bb244b
AS
807 return 0;
808 } else {
5e8149f5 809 brcmf_err("Cannot talk to Dongle. Firmware is not UP, %d ms\n",
83bc9c31 810 BRCMF_USB_RESET_GETVER_SPINWAIT * loop_cnt);
71bb244b
AS
811 return -EINVAL;
812 }
813}
814
815
816static int
817brcmf_usb_dl_send_bulk(struct brcmf_usbdev_info *devinfo, void *buffer, int len)
818{
819 int ret;
820
821 if ((devinfo == NULL) || (devinfo->bulk_urb == NULL))
822 return -EINVAL;
823
824 /* Prepare the URB */
825 usb_fill_bulk_urb(devinfo->bulk_urb, devinfo->usbdev,
826 devinfo->tx_pipe, buffer, len,
827 (usb_complete_t)brcmf_usb_sync_complete, devinfo);
828
829 devinfo->bulk_urb->transfer_flags |= URB_ZERO_PACKET;
830
1e271c95 831 devinfo->ctl_completed = false;
71bb244b
AS
832 ret = usb_submit_urb(devinfo->bulk_urb, GFP_ATOMIC);
833 if (ret) {
5e8149f5 834 brcmf_err("usb_submit_urb failed %d\n", ret);
71bb244b
AS
835 return ret;
836 }
1e271c95
HM
837 ret = brcmf_usb_ioctl_resp_wait(devinfo);
838 return (ret == 0);
71bb244b
AS
839}
840
841static int
842brcmf_usb_dl_writeimage(struct brcmf_usbdev_info *devinfo, u8 *fw, int fwlen)
843{
844 unsigned int sendlen, sent, dllen;
845 char *bulkchunk = NULL, *dlpos;
846 struct rdl_state_le state;
847 u32 rdlstate, rdlbytes;
848 int err = 0;
cb8b73da
HM
849
850 brcmf_dbg(USB, "Enter, fw %p, len %d\n", fw, fwlen);
71bb244b
AS
851
852 bulkchunk = kmalloc(RDL_CHUNK, GFP_ATOMIC);
853 if (bulkchunk == NULL) {
854 err = -ENOMEM;
855 goto fail;
856 }
857
858 /* 1) Prepare USB boot loader for runtime image */
859 brcmf_usb_dl_cmd(devinfo, DL_START, &state,
860 sizeof(struct rdl_state_le));
861
862 rdlstate = le32_to_cpu(state.state);
863 rdlbytes = le32_to_cpu(state.bytes);
864
865 /* 2) Check we are in the Waiting state */
866 if (rdlstate != DL_WAITING) {
5e8149f5 867 brcmf_err("Failed to DL_START\n");
71bb244b
AS
868 err = -EINVAL;
869 goto fail;
870 }
871 sent = 0;
872 dlpos = fw;
873 dllen = fwlen;
874
875 /* Get chip id and rev */
876 while (rdlbytes != dllen) {
877 /* Wait until the usb device reports it received all
878 * the bytes we sent */
879 if ((rdlbytes == sent) && (rdlbytes != dllen)) {
880 if ((dllen-sent) < RDL_CHUNK)
881 sendlen = dllen-sent;
882 else
883 sendlen = RDL_CHUNK;
884
885 /* simply avoid having to send a ZLP by ensuring we
886 * never have an even
887 * multiple of 64
888 */
889 if (!(sendlen % 64))
890 sendlen -= 4;
891
892 /* send data */
893 memcpy(bulkchunk, dlpos, sendlen);
894 if (brcmf_usb_dl_send_bulk(devinfo, bulkchunk,
895 sendlen)) {
5e8149f5 896 brcmf_err("send_bulk failed\n");
71bb244b
AS
897 err = -EINVAL;
898 goto fail;
899 }
900
901 dlpos += sendlen;
902 sent += sendlen;
903 }
904 if (!brcmf_usb_dl_cmd(devinfo, DL_GETSTATE, &state,
905 sizeof(struct rdl_state_le))) {
5e8149f5 906 brcmf_err("DL_GETSTATE Failed xxxx\n");
71bb244b
AS
907 err = -EINVAL;
908 goto fail;
909 }
910
911 rdlstate = le32_to_cpu(state.state);
912 rdlbytes = le32_to_cpu(state.bytes);
913
914 /* restart if an error is reported */
915 if (rdlstate == DL_BAD_HDR || rdlstate == DL_BAD_CRC) {
5e8149f5 916 brcmf_err("Bad Hdr or Bad CRC state %d\n",
71bb244b
AS
917 rdlstate);
918 err = -EINVAL;
919 goto fail;
920 }
921 }
922
923fail:
924 kfree(bulkchunk);
cb8b73da 925 brcmf_dbg(USB, "Exit, err=%d\n", err);
71bb244b
AS
926 return err;
927}
928
929static int brcmf_usb_dlstart(struct brcmf_usbdev_info *devinfo, u8 *fw, int len)
930{
931 int err;
932
cb8b73da 933 brcmf_dbg(USB, "Enter\n");
71bb244b
AS
934
935 if (devinfo == NULL)
936 return -EINVAL;
937
ac94f196 938 if (devinfo->bus_pub.devid == 0xDEAD)
71bb244b
AS
939 return -EINVAL;
940
941 err = brcmf_usb_dl_writeimage(devinfo, fw, len);
942 if (err == 0)
7c38e698 943 devinfo->bus_pub.state = BRCMFMAC_USB_STATE_DL_DONE;
71bb244b 944 else
7c38e698 945 devinfo->bus_pub.state = BRCMFMAC_USB_STATE_DL_FAIL;
cb8b73da 946 brcmf_dbg(USB, "Exit, err=%d\n", err);
71bb244b
AS
947
948 return err;
949}
950
951static int brcmf_usb_dlrun(struct brcmf_usbdev_info *devinfo)
952{
953 struct rdl_state_le state;
954
cb8b73da 955 brcmf_dbg(USB, "Enter\n");
71bb244b
AS
956 if (!devinfo)
957 return -EINVAL;
958
ac94f196 959 if (devinfo->bus_pub.devid == 0xDEAD)
71bb244b
AS
960 return -EINVAL;
961
962 /* Check we are runnable */
963 brcmf_usb_dl_cmd(devinfo, DL_GETSTATE, &state,
964 sizeof(struct rdl_state_le));
965
966 /* Start the image */
967 if (state.state == cpu_to_le32(DL_RUNNABLE)) {
968 if (!brcmf_usb_dl_cmd(devinfo, DL_GO, &state,
969 sizeof(struct rdl_state_le)))
970 return -ENODEV;
971 if (brcmf_usb_resetcfg(devinfo))
972 return -ENODEV;
973 /* The Dongle may go for re-enumeration. */
974 } else {
5e8149f5 975 brcmf_err("Dongle not runnable\n");
71bb244b
AS
976 return -EINVAL;
977 }
cb8b73da 978 brcmf_dbg(USB, "Exit\n");
71bb244b
AS
979 return 0;
980}
981
982static bool brcmf_usb_chip_support(int chipid, int chiprev)
983{
984 switch(chipid) {
70f0822c
HM
985 case 43143:
986 return true;
71bb244b
AS
987 case 43235:
988 case 43236:
989 case 43238:
990 return (chiprev == 3);
1212d370
HM
991 case 43242:
992 return true;
71bb244b
AS
993 default:
994 break;
995 }
996 return false;
997}
998
999static int
1000brcmf_usb_fw_download(struct brcmf_usbdev_info *devinfo)
1001{
ac94f196 1002 int devid, chiprev;
71bb244b
AS
1003 int err;
1004
cb8b73da 1005 brcmf_dbg(USB, "Enter\n");
71bb244b
AS
1006 if (devinfo == NULL)
1007 return -ENODEV;
1008
ac94f196
AS
1009 devid = devinfo->bus_pub.devid;
1010 chiprev = devinfo->bus_pub.chiprev;
71bb244b 1011
ac94f196 1012 if (!brcmf_usb_chip_support(devid, chiprev)) {
5e8149f5 1013 brcmf_err("unsupported chip %d rev %d\n",
ac94f196 1014 devid, chiprev);
71bb244b
AS
1015 return -EINVAL;
1016 }
1017
1018 if (!devinfo->image) {
5e8149f5 1019 brcmf_err("No firmware!\n");
71bb244b
AS
1020 return -ENOENT;
1021 }
1022
1023 err = brcmf_usb_dlstart(devinfo,
1024 devinfo->image, devinfo->image_len);
1025 if (err == 0)
1026 err = brcmf_usb_dlrun(devinfo);
1027 return err;
1028}
1029
1030
d74a0b51 1031static void brcmf_usb_detach(struct brcmf_usbdev_info *devinfo)
71bb244b 1032{
cb8b73da 1033 brcmf_dbg(USB, "Enter, devinfo %p\n", devinfo);
71bb244b 1034
71bb244b
AS
1035 /* free the URBS */
1036 brcmf_usb_free_q(&devinfo->rx_freeq, false);
1037 brcmf_usb_free_q(&devinfo->tx_freeq, false);
1038
1039 usb_free_urb(devinfo->intr_urb);
1040 usb_free_urb(devinfo->ctl_urb);
1041 usb_free_urb(devinfo->bulk_urb);
1042
1043 kfree(devinfo->tx_reqs);
1044 kfree(devinfo->rx_reqs);
71bb244b
AS
1045}
1046
1047#define TRX_MAGIC 0x30524448 /* "HDR0" */
1048#define TRX_VERSION 1 /* Version 1 */
1049#define TRX_MAX_LEN 0x3B0000 /* Max length */
1050#define TRX_NO_HEADER 1 /* Do not write TRX header */
1051#define TRX_MAX_OFFSET 3 /* Max number of individual files */
1052#define TRX_UNCOMP_IMAGE 0x20 /* Trx contains uncompressed image */
1053
1054struct trx_header_le {
1055 __le32 magic; /* "HDR0" */
1056 __le32 len; /* Length of file including header */
1057 __le32 crc32; /* CRC from flag_version to end of file */
1058 __le32 flag_version; /* 0:15 flags, 16:31 version */
1059 __le32 offsets[TRX_MAX_OFFSET]; /* Offsets of partitions from start of
1060 * header */
1061};
1062
1063static int check_file(const u8 *headers)
1064{
1065 struct trx_header_le *trx;
1066 int actual_len = -1;
1067
cb8b73da 1068 brcmf_dbg(USB, "Enter\n");
71bb244b
AS
1069 /* Extract trx header */
1070 trx = (struct trx_header_le *) headers;
1071 if (trx->magic != cpu_to_le32(TRX_MAGIC))
1072 return -1;
1073
1074 headers += sizeof(struct trx_header_le);
1075
1076 if (le32_to_cpu(trx->flag_version) & TRX_UNCOMP_IMAGE) {
1077 actual_len = le32_to_cpu(trx->offsets[TRX_OFFSETS_DLFWLEN_IDX]);
1078 return actual_len + sizeof(struct trx_header_le);
1079 }
1080 return -1;
1081}
1082
1083static int brcmf_usb_get_fw(struct brcmf_usbdev_info *devinfo)
1084{
1085 s8 *fwname;
1086 const struct firmware *fw;
803599d4 1087 struct brcmf_usb_image *fw_image;
71bb244b
AS
1088 int err;
1089
cb8b73da 1090 brcmf_dbg(USB, "Enter\n");
1212d370 1091 switch (devinfo->bus_pub.devid) {
70f0822c
HM
1092 case 43143:
1093 fwname = BRCMF_USB_43143_FW_NAME;
1094 break;
1212d370
HM
1095 case 43235:
1096 case 43236:
1097 case 43238:
1098 fwname = BRCMF_USB_43236_FW_NAME;
1099 break;
1100 case 43242:
1101 fwname = BRCMF_USB_43242_FW_NAME;
1102 break;
1103 default:
1104 return -EINVAL;
1105 break;
1106 }
cb8b73da 1107 brcmf_dbg(USB, "Loading FW %s\n", fwname);
803599d4
HM
1108 list_for_each_entry(fw_image, &fw_image_list, list) {
1109 if (fw_image->fwname == fwname) {
1110 devinfo->image = fw_image->image;
1111 devinfo->image_len = fw_image->image_len;
1112 return 0;
1113 }
1114 }
1115 /* fw image not yet loaded. Load it now and add to list */
71bb244b
AS
1116 err = request_firmware(&fw, fwname, devinfo->dev);
1117 if (!fw) {
5e8149f5 1118 brcmf_err("fail to request firmware %s\n", fwname);
71bb244b
AS
1119 return err;
1120 }
1121 if (check_file(fw->data) < 0) {
5e8149f5 1122 brcmf_err("invalid firmware %s\n", fwname);
71bb244b
AS
1123 return -EINVAL;
1124 }
1125
803599d4
HM
1126 fw_image = kzalloc(sizeof(*fw_image), GFP_ATOMIC);
1127 if (!fw_image)
1128 return -ENOMEM;
1129 INIT_LIST_HEAD(&fw_image->list);
1130 list_add_tail(&fw_image->list, &fw_image_list);
1131 fw_image->fwname = fwname;
1132 fw_image->image = vmalloc(fw->size);
1133 if (!fw_image->image)
71bb244b
AS
1134 return -ENOMEM;
1135
803599d4
HM
1136 memcpy(fw_image->image, fw->data, fw->size);
1137 fw_image->image_len = fw->size;
71bb244b
AS
1138
1139 release_firmware(fw);
803599d4
HM
1140
1141 devinfo->image = fw_image->image;
1142 devinfo->image_len = fw_image->image_len;
1143
71bb244b
AS
1144 return 0;
1145}
1146
1147
1148static
d74a0b51
HM
1149struct brcmf_usbdev *brcmf_usb_attach(struct brcmf_usbdev_info *devinfo,
1150 int nrxq, int ntxq)
71bb244b 1151{
cb8b73da
HM
1152 brcmf_dbg(USB, "Enter\n");
1153
71bb244b
AS
1154 devinfo->bus_pub.nrxq = nrxq;
1155 devinfo->rx_low_watermark = nrxq / 2;
1156 devinfo->bus_pub.devinfo = devinfo;
1157 devinfo->bus_pub.ntxq = ntxq;
7c38e698 1158 devinfo->bus_pub.state = BRCMFMAC_USB_STATE_DOWN;
71bb244b
AS
1159
1160 /* flow control when too many tx urbs posted */
1161 devinfo->tx_low_watermark = ntxq / 4;
1162 devinfo->tx_high_watermark = devinfo->tx_low_watermark * 3;
71bb244b
AS
1163 devinfo->bus_pub.bus_mtu = BRCMF_USB_MAX_PKT_SIZE;
1164
1165 /* Initialize other structure content */
1166 init_waitqueue_head(&devinfo->ioctl_resp_wait);
1167
1168 /* Initialize the spinlocks */
1169 spin_lock_init(&devinfo->qlock);
df50f756 1170 spin_lock_init(&devinfo->tx_flowblock_lock);
71bb244b
AS
1171
1172 INIT_LIST_HEAD(&devinfo->rx_freeq);
1173 INIT_LIST_HEAD(&devinfo->rx_postq);
1174
1175 INIT_LIST_HEAD(&devinfo->tx_freeq);
1176 INIT_LIST_HEAD(&devinfo->tx_postq);
1177
c6ab4294
HM
1178 devinfo->tx_flowblock = false;
1179
71bb244b
AS
1180 devinfo->rx_reqs = brcmf_usbdev_qinit(&devinfo->rx_freeq, nrxq);
1181 if (!devinfo->rx_reqs)
1182 goto error;
1183
1184 devinfo->tx_reqs = brcmf_usbdev_qinit(&devinfo->tx_freeq, ntxq);
1185 if (!devinfo->tx_reqs)
1186 goto error;
c6ab4294 1187 devinfo->tx_freecount = ntxq;
71bb244b
AS
1188
1189 devinfo->intr_urb = usb_alloc_urb(0, GFP_ATOMIC);
1190 if (!devinfo->intr_urb) {
5e8149f5 1191 brcmf_err("usb_alloc_urb (intr) failed\n");
71bb244b
AS
1192 goto error;
1193 }
1194 devinfo->ctl_urb = usb_alloc_urb(0, GFP_ATOMIC);
1195 if (!devinfo->ctl_urb) {
5e8149f5 1196 brcmf_err("usb_alloc_urb (ctl) failed\n");
71bb244b
AS
1197 goto error;
1198 }
71bb244b
AS
1199 devinfo->bulk_urb = usb_alloc_urb(0, GFP_ATOMIC);
1200 if (!devinfo->bulk_urb) {
5e8149f5 1201 brcmf_err("usb_alloc_urb (bulk) failed\n");
71bb244b
AS
1202 goto error;
1203 }
1204
71bb244b
AS
1205 if (!brcmf_usb_dlneeded(devinfo))
1206 return &devinfo->bus_pub;
1207
cb8b73da 1208 brcmf_dbg(USB, "Start fw downloading\n");
71bb244b
AS
1209 if (brcmf_usb_get_fw(devinfo))
1210 goto error;
1211
1212 if (brcmf_usb_fw_download(devinfo))
1213 goto error;
1214
1215 return &devinfo->bus_pub;
1216
1217error:
5e8149f5 1218 brcmf_err("failed!\n");
d74a0b51 1219 brcmf_usb_detach(devinfo);
71bb244b
AS
1220 return NULL;
1221}
1222
d9cb2596
AS
1223static struct brcmf_bus_ops brcmf_usb_bus_ops = {
1224 .txdata = brcmf_usb_tx,
1225 .init = brcmf_usb_up,
1226 .stop = brcmf_usb_down,
1227 .txctl = brcmf_usb_tx_ctlpkt,
1228 .rxctl = brcmf_usb_rx_ctlpkt,
1229};
1230
b87e2c48 1231static int brcmf_usb_probe_cb(struct brcmf_usbdev_info *devinfo)
71bb244b
AS
1232{
1233 struct brcmf_bus *bus = NULL;
1234 struct brcmf_usbdev *bus_pub = NULL;
1235 int ret;
d74a0b51 1236 struct device *dev = devinfo->dev;
71bb244b 1237
cb8b73da 1238 brcmf_dbg(USB, "Enter\n");
d74a0b51 1239 bus_pub = brcmf_usb_attach(devinfo, BRCMF_USB_NRXQ, BRCMF_USB_NTXQ);
9cd23a71
HM
1240 if (!bus_pub)
1241 return -ENODEV;
71bb244b
AS
1242
1243 bus = kzalloc(sizeof(struct brcmf_bus), GFP_ATOMIC);
1244 if (!bus) {
1245 ret = -ENOMEM;
1246 goto fail;
1247 }
1248
d9cb2596 1249 bus->dev = dev;
71bb244b 1250 bus_pub->bus = bus;
71bb244b
AS
1251 bus->bus_priv.usb = bus_pub;
1252 dev_set_drvdata(dev, bus);
d9cb2596 1253 bus->ops = &brcmf_usb_bus_ops;
75d907d3
AS
1254 bus->chip = bus_pub->devid;
1255 bus->chiprev = bus_pub->chiprev;
71bb244b
AS
1256
1257 /* Attach to the common driver interface */
8dee77ba 1258 ret = brcmf_attach(dev);
71bb244b 1259 if (ret) {
5e8149f5 1260 brcmf_err("brcmf_attach failed\n");
71bb244b
AS
1261 goto fail;
1262 }
1263
1264 ret = brcmf_bus_start(dev);
e270b302 1265 if (ret) {
5e8149f5 1266 brcmf_err("dongle is not responding\n");
71bb244b
AS
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) {
5e8149f5 1341 brcmf_err("invalid control interface: class %d, subclass %d, proto %d\n",
71bb244b
AS
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) {
5e8149f5 1353 brcmf_err("invalid control endpoint %d\n",
71bb244b
AS
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) {
5e8149f5 1372 brcmf_err("invalid data endpoint %d\n", ep);
71bb244b
AS
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:
5e8149f5 1415 brcmf_err("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");
8dee77ba 1457 if (!brcmf_attach(devinfo->dev))
7c38e698
HM
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};
5cfd6e88 1490
71bb244b 1491MODULE_DEVICE_TABLE(usb, brcmf_usb_devid_table);
70f0822c 1492MODULE_FIRMWARE(BRCMF_USB_43143_FW_NAME);
fda82417 1493MODULE_FIRMWARE(BRCMF_USB_43236_FW_NAME);
1212d370 1494MODULE_FIRMWARE(BRCMF_USB_43242_FW_NAME);
71bb244b 1495
71bb244b
AS
1496static struct usb_driver brcmf_usbdrvr = {
1497 .name = KBUILD_MODNAME,
1498 .probe = brcmf_usb_probe,
1499 .disconnect = brcmf_usb_disconnect,
1500 .id_table = brcmf_usb_devid_table,
1501 .suspend = brcmf_usb_suspend,
1502 .resume = brcmf_usb_resume,
7c38e698 1503 .reset_resume = brcmf_usb_reset_resume,
c51fa668 1504 .supports_autosuspend = 1,
e1f12eb6 1505 .disable_hub_initiated_lpm = 1,
71bb244b
AS
1506};
1507
803599d4
HM
1508static void brcmf_release_fw(struct list_head *q)
1509{
1510 struct brcmf_usb_image *fw_image, *next;
1511
1512 list_for_each_entry_safe(fw_image, next, q, list) {
1513 vfree(fw_image->image);
1514 list_del_init(&fw_image->list);
1515 }
1516}
1517
81d5f1bb
AS
1518static int brcmf_usb_reset_device(struct device *dev, void *notused)
1519{
1520 /* device past is the usb interface so we
1521 * need to use parent here.
1522 */
1523 brcmf_dev_reset(dev->parent);
1524 return 0;
1525}
803599d4 1526
71bb244b
AS
1527void brcmf_usb_exit(void)
1528{
81d5f1bb
AS
1529 struct device_driver *drv = &brcmf_usbdrvr.drvwrap.driver;
1530 int ret;
1531
cb8b73da 1532 brcmf_dbg(USB, "Enter\n");
81d5f1bb
AS
1533 ret = driver_for_each_device(drv, NULL, NULL,
1534 brcmf_usb_reset_device);
71bb244b 1535 usb_deregister(&brcmf_usbdrvr);
803599d4 1536 brcmf_release_fw(&fw_image_list);
71bb244b
AS
1537}
1538
db4efbbe 1539void brcmf_usb_register(void)
71bb244b 1540{
cb8b73da 1541 brcmf_dbg(USB, "Enter\n");
803599d4 1542 INIT_LIST_HEAD(&fw_image_list);
549040ab 1543 usb_register(&brcmf_usbdrvr);
71bb244b 1544}
This page took 0.230911 seconds and 5 git commands to generate.