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