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