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