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