]> git.ipfire.org Git - people/arne_f/kernel.git/blame - drivers/net/wireless/broadcom/brcm80211/brcmfmac/usb.c
brcmfmac: add CLM download support
[people/arne_f/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;
0f2aa386 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 }
67cd8ccd 444 kfree(reqs);
71bb244b
AS
445 return NULL;
446
447}
448
449static void brcmf_usb_free_q(struct list_head *q, bool pending)
450{
451 struct brcmf_usbreq *req, *next;
452 int i = 0;
453 list_for_each_entry_safe(req, next, q, list) {
d4ca0099 454 if (!req->urb) {
5e8149f5 455 brcmf_err("bad req\n");
71bb244b
AS
456 break;
457 }
458 i++;
459 if (pending) {
460 usb_kill_urb(req->urb);
461 } else {
462 usb_free_urb(req->urb);
463 list_del_init(&req->list);
464 }
465 }
466}
467
468static void brcmf_usb_del_fromq(struct brcmf_usbdev_info *devinfo,
469 struct brcmf_usbreq *req)
470{
471 unsigned long flags;
472
473 spin_lock_irqsave(&devinfo->qlock, flags);
474 list_del_init(&req->list);
475 spin_unlock_irqrestore(&devinfo->qlock, flags);
476}
477
478
479static void brcmf_usb_tx_complete(struct urb *urb)
480{
481 struct brcmf_usbreq *req = (struct brcmf_usbreq *)urb->context;
482 struct brcmf_usbdev_info *devinfo = req->devinfo;
df50f756 483 unsigned long flags;
71bb244b 484
cb8b73da
HM
485 brcmf_dbg(USB, "Enter, urb->status=%d, skb=%p\n", urb->status,
486 req->skb);
71bb244b 487 brcmf_usb_del_fromq(devinfo, req);
71bb244b 488
7b584396 489 brcmf_proto_bcdc_txcomplete(devinfo->dev, req->skb, urb->status == 0);
71bb244b 490 req->skb = NULL;
c6ab4294 491 brcmf_usb_enq(devinfo, &devinfo->tx_freeq, req, &devinfo->tx_freecount);
df50f756 492 spin_lock_irqsave(&devinfo->tx_flowblock_lock, flags);
c6ab4294
HM
493 if (devinfo->tx_freecount > devinfo->tx_high_watermark &&
494 devinfo->tx_flowblock) {
20ec4f57 495 brcmf_proto_bcdc_txflowblock(devinfo->dev, false);
c6ab4294
HM
496 devinfo->tx_flowblock = false;
497 }
df50f756 498 spin_unlock_irqrestore(&devinfo->tx_flowblock_lock, flags);
71bb244b
AS
499}
500
501static void brcmf_usb_rx_complete(struct urb *urb)
502{
503 struct brcmf_usbreq *req = (struct brcmf_usbreq *)urb->context;
504 struct brcmf_usbdev_info *devinfo = req->devinfo;
505 struct sk_buff *skb;
71bb244b 506
cb8b73da 507 brcmf_dbg(USB, "Enter, urb->status=%d\n", urb->status);
71bb244b
AS
508 brcmf_usb_del_fromq(devinfo, req);
509 skb = req->skb;
510 req->skb = NULL;
511
a43af515 512 /* zero lenght packets indicate usb "failure". Do not refill */
b1a2a411 513 if (urb->status != 0 || !urb->actual_length) {
01e3331b 514 brcmu_pkt_buf_free_skb(skb);
c6ab4294 515 brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
71bb244b
AS
516 return;
517 }
518
7c38e698 519 if (devinfo->bus_pub.state == BRCMFMAC_USB_STATE_UP) {
71bb244b 520 skb_put(skb, urb->actual_length);
c56caa9d 521 brcmf_rx_frame(devinfo->dev, skb, true);
a43af515 522 brcmf_usb_rx_refill(devinfo, req);
71bb244b 523 } else {
01e3331b 524 brcmu_pkt_buf_free_skb(skb);
c6ab4294 525 brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
71bb244b
AS
526 }
527 return;
528
529}
530
531static void brcmf_usb_rx_refill(struct brcmf_usbdev_info *devinfo,
532 struct brcmf_usbreq *req)
533{
534 struct sk_buff *skb;
535 int ret;
536
537 if (!req || !devinfo)
538 return;
539
540 skb = dev_alloc_skb(devinfo->bus_pub.bus_mtu);
541 if (!skb) {
c6ab4294 542 brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
71bb244b
AS
543 return;
544 }
545 req->skb = skb;
546
547 usb_fill_bulk_urb(req->urb, devinfo->usbdev, devinfo->rx_pipe,
548 skb->data, skb_tailroom(skb), brcmf_usb_rx_complete,
549 req);
71bb244b 550 req->devinfo = devinfo;
c6ab4294 551 brcmf_usb_enq(devinfo, &devinfo->rx_postq, req, NULL);
71bb244b
AS
552
553 ret = usb_submit_urb(req->urb, GFP_ATOMIC);
2e875acd
HM
554 if (ret) {
555 brcmf_usb_del_fromq(devinfo, req);
01e3331b 556 brcmu_pkt_buf_free_skb(req->skb);
71bb244b 557 req->skb = NULL;
c6ab4294 558 brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
71bb244b
AS
559 }
560 return;
561}
562
563static void brcmf_usb_rx_fill_all(struct brcmf_usbdev_info *devinfo)
564{
565 struct brcmf_usbreq *req;
566
7c38e698 567 if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP) {
5e8149f5 568 brcmf_err("bus is not up=%d\n", devinfo->bus_pub.state);
71bb244b
AS
569 return;
570 }
c6ab4294 571 while ((req = brcmf_usb_deq(devinfo, &devinfo->rx_freeq, NULL)) != NULL)
71bb244b
AS
572 brcmf_usb_rx_refill(devinfo, req);
573}
574
575static void
576brcmf_usb_state_change(struct brcmf_usbdev_info *devinfo, int state)
577{
578 struct brcmf_bus *bcmf_bus = devinfo->bus_pub.bus;
579 int old_state;
580
cb8b73da
HM
581 brcmf_dbg(USB, "Enter, current state=%d, new state=%d\n",
582 devinfo->bus_pub.state, state);
71bb244b
AS
583
584 if (devinfo->bus_pub.state == state)
585 return;
586
587 old_state = devinfo->bus_pub.state;
7c38e698 588 devinfo->bus_pub.state = state;
71bb244b
AS
589
590 /* update state of upper layer */
7c38e698 591 if (state == BRCMFMAC_USB_STATE_DOWN) {
cb8b73da 592 brcmf_dbg(USB, "DBUS is down\n");
bb350711 593 brcmf_bus_change_state(bcmf_bus, BRCMF_BUS_DOWN);
7c38e698
HM
594 } else if (state == BRCMFMAC_USB_STATE_UP) {
595 brcmf_dbg(USB, "DBUS is up\n");
a1cee865 596 brcmf_bus_change_state(bcmf_bus, BRCMF_BUS_UP);
71bb244b 597 } else {
cb8b73da 598 brcmf_dbg(USB, "DBUS current state=%d\n", state);
71bb244b
AS
599 }
600}
601
71bb244b
AS
602static int brcmf_usb_tx(struct device *dev, struct sk_buff *skb)
603{
604 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
605 struct brcmf_usbreq *req;
606 int ret;
df50f756 607 unsigned long flags;
71bb244b 608
cb8b73da 609 brcmf_dbg(USB, "Enter, skb=%p\n", skb);
6fc9ca13
AS
610 if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP) {
611 ret = -EIO;
612 goto fail;
613 }
71bb244b 614
c6ab4294
HM
615 req = brcmf_usb_deq(devinfo, &devinfo->tx_freeq,
616 &devinfo->tx_freecount);
71bb244b 617 if (!req) {
5e8149f5 618 brcmf_err("no req to send\n");
6fc9ca13
AS
619 ret = -ENOMEM;
620 goto fail;
71bb244b 621 }
71bb244b
AS
622
623 req->skb = skb;
624 req->devinfo = devinfo;
625 usb_fill_bulk_urb(req->urb, devinfo->usbdev, devinfo->tx_pipe,
626 skb->data, skb->len, brcmf_usb_tx_complete, req);
627 req->urb->transfer_flags |= URB_ZERO_PACKET;
c6ab4294 628 brcmf_usb_enq(devinfo, &devinfo->tx_postq, req, NULL);
71bb244b 629 ret = usb_submit_urb(req->urb, GFP_ATOMIC);
2e875acd 630 if (ret) {
5e8149f5 631 brcmf_err("brcmf_usb_tx usb_submit_urb FAILED\n");
2e875acd 632 brcmf_usb_del_fromq(devinfo, req);
71bb244b 633 req->skb = NULL;
c6ab4294 634 brcmf_usb_enq(devinfo, &devinfo->tx_freeq, req,
6fc9ca13
AS
635 &devinfo->tx_freecount);
636 goto fail;
71bb244b
AS
637 }
638
df50f756 639 spin_lock_irqsave(&devinfo->tx_flowblock_lock, flags);
6fc9ca13
AS
640 if (devinfo->tx_freecount < devinfo->tx_low_watermark &&
641 !devinfo->tx_flowblock) {
20ec4f57 642 brcmf_proto_bcdc_txflowblock(dev, true);
6fc9ca13
AS
643 devinfo->tx_flowblock = true;
644 }
df50f756 645 spin_unlock_irqrestore(&devinfo->tx_flowblock_lock, flags);
6fc9ca13
AS
646 return 0;
647
648fail:
71bb244b
AS
649 return ret;
650}
651
652
653static int brcmf_usb_up(struct device *dev)
654{
655 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
71bb244b 656
cb8b73da 657 brcmf_dbg(USB, "Enter\n");
7c38e698 658 if (devinfo->bus_pub.state == BRCMFMAC_USB_STATE_UP)
d4ca0099
DC
659 return 0;
660
71bb244b 661 /* Success, indicate devinfo is fully up */
7c38e698 662 brcmf_usb_state_change(devinfo, BRCMFMAC_USB_STATE_UP);
71bb244b 663
71bb244b
AS
664 if (devinfo->ctl_urb) {
665 devinfo->ctl_in_pipe = usb_rcvctrlpipe(devinfo->usbdev, 0);
666 devinfo->ctl_out_pipe = usb_sndctrlpipe(devinfo->usbdev, 0);
667
71bb244b
AS
668 /* CTL Write */
669 devinfo->ctl_write.bRequestType =
670 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
671 devinfo->ctl_write.bRequest = 0;
672 devinfo->ctl_write.wValue = cpu_to_le16(0);
51c7f5ed 673 devinfo->ctl_write.wIndex = cpu_to_le16(devinfo->ifnum);
71bb244b
AS
674
675 /* CTL Read */
676 devinfo->ctl_read.bRequestType =
677 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
678 devinfo->ctl_read.bRequest = 1;
679 devinfo->ctl_read.wValue = cpu_to_le16(0);
51c7f5ed 680 devinfo->ctl_read.wIndex = cpu_to_le16(devinfo->ifnum);
71bb244b
AS
681 }
682 brcmf_usb_rx_fill_all(devinfo);
683 return 0;
684}
685
244b124c
HM
686static void brcmf_cancel_all_urbs(struct brcmf_usbdev_info *devinfo)
687{
dfbe5ff0
PF
688 int i;
689
244b124c
HM
690 if (devinfo->ctl_urb)
691 usb_kill_urb(devinfo->ctl_urb);
692 if (devinfo->bulk_urb)
693 usb_kill_urb(devinfo->bulk_urb);
dfbe5ff0
PF
694 if (devinfo->tx_reqs)
695 for (i = 0; i < devinfo->bus_pub.ntxq; i++)
696 usb_kill_urb(devinfo->tx_reqs[i].urb);
697 if (devinfo->rx_reqs)
698 for (i = 0; i < devinfo->bus_pub.nrxq; i++)
699 usb_kill_urb(devinfo->rx_reqs[i].urb);
244b124c
HM
700}
701
71bb244b
AS
702static void brcmf_usb_down(struct device *dev)
703{
704 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
705
cb8b73da 706 brcmf_dbg(USB, "Enter\n");
71bb244b
AS
707 if (devinfo == NULL)
708 return;
709
7c38e698 710 if (devinfo->bus_pub.state == BRCMFMAC_USB_STATE_DOWN)
71bb244b
AS
711 return;
712
7c38e698 713 brcmf_usb_state_change(devinfo, BRCMFMAC_USB_STATE_DOWN);
71bb244b 714
244b124c 715 brcmf_cancel_all_urbs(devinfo);
71bb244b
AS
716}
717
71bb244b
AS
718static void
719brcmf_usb_sync_complete(struct urb *urb)
720{
721 struct brcmf_usbdev_info *devinfo =
722 (struct brcmf_usbdev_info *)urb->context;
723
1e271c95
HM
724 devinfo->ctl_completed = true;
725 brcmf_usb_ioctl_resp_wake(devinfo);
71bb244b
AS
726}
727
51c7f5ed
HM
728static int brcmf_usb_dl_cmd(struct brcmf_usbdev_info *devinfo, u8 cmd,
729 void *buffer, int buflen)
71bb244b 730{
51c7f5ed 731 int ret;
71bb244b
AS
732 char *tmpbuf;
733 u16 size;
734
735 if ((!devinfo) || (devinfo->ctl_urb == NULL))
51c7f5ed 736 return -EINVAL;
71bb244b
AS
737
738 tmpbuf = kmalloc(buflen, GFP_ATOMIC);
739 if (!tmpbuf)
51c7f5ed 740 return -ENOMEM;
71bb244b
AS
741
742 size = buflen;
743 devinfo->ctl_urb->transfer_buffer_length = size;
744
745 devinfo->ctl_read.wLength = cpu_to_le16p(&size);
746 devinfo->ctl_read.bRequestType = USB_DIR_IN | USB_TYPE_VENDOR |
747 USB_RECIP_INTERFACE;
748 devinfo->ctl_read.bRequest = cmd;
749
750 usb_fill_control_urb(devinfo->ctl_urb,
751 devinfo->usbdev,
752 usb_rcvctrlpipe(devinfo->usbdev, 0),
753 (unsigned char *) &devinfo->ctl_read,
754 (void *) tmpbuf, size,
755 (usb_complete_t)brcmf_usb_sync_complete, devinfo);
756
1e271c95 757 devinfo->ctl_completed = false;
71bb244b
AS
758 ret = usb_submit_urb(devinfo->ctl_urb, GFP_ATOMIC);
759 if (ret < 0) {
5e8149f5 760 brcmf_err("usb_submit_urb failed %d\n", ret);
51c7f5ed 761 goto finalize;
71bb244b
AS
762 }
763
8180bd47
MV
764 if (!brcmf_usb_ioctl_resp_wait(devinfo)) {
765 usb_kill_urb(devinfo->ctl_urb);
51c7f5ed 766 ret = -ETIMEDOUT;
8180bd47 767 } else {
51c7f5ed 768 memcpy(buffer, tmpbuf, buflen);
8180bd47 769 }
71bb244b 770
51c7f5ed
HM
771finalize:
772 kfree(tmpbuf);
1e271c95 773 return ret;
71bb244b
AS
774}
775
776static bool
777brcmf_usb_dlneeded(struct brcmf_usbdev_info *devinfo)
778{
779 struct bootrom_id_le id;
780 u32 chipid, chiprev;
781
cb8b73da 782 brcmf_dbg(USB, "Enter\n");
71bb244b
AS
783
784 if (devinfo == NULL)
785 return false;
786
787 /* Check if firmware downloaded already by querying runtime ID */
788 id.chip = cpu_to_le32(0xDEAD);
83bc9c31 789 brcmf_usb_dl_cmd(devinfo, DL_GETVER, &id, sizeof(id));
71bb244b
AS
790
791 chipid = le32_to_cpu(id.chip);
792 chiprev = le32_to_cpu(id.chiprev);
793
794 if ((chipid & 0x4300) == 0x4300)
cb8b73da 795 brcmf_dbg(USB, "chip %x rev 0x%x\n", chipid, chiprev);
71bb244b 796 else
cb8b73da 797 brcmf_dbg(USB, "chip %d rev 0x%x\n", chipid, chiprev);
71bb244b 798 if (chipid == BRCMF_POSTBOOT_ID) {
cb8b73da 799 brcmf_dbg(USB, "firmware already downloaded\n");
83bc9c31 800 brcmf_usb_dl_cmd(devinfo, DL_RESETCFG, &id, sizeof(id));
71bb244b
AS
801 return false;
802 } else {
ac94f196
AS
803 devinfo->bus_pub.devid = chipid;
804 devinfo->bus_pub.chiprev = chiprev;
71bb244b
AS
805 }
806 return true;
807}
808
809static int
810brcmf_usb_resetcfg(struct brcmf_usbdev_info *devinfo)
811{
812 struct bootrom_id_le id;
83bc9c31 813 u32 loop_cnt;
51c7f5ed 814 int err;
71bb244b 815
cb8b73da 816 brcmf_dbg(USB, "Enter\n");
71bb244b 817
83bc9c31
HM
818 loop_cnt = 0;
819 do {
820 mdelay(BRCMF_USB_RESET_GETVER_SPINWAIT);
821 loop_cnt++;
71bb244b 822 id.chip = cpu_to_le32(0xDEAD); /* Get the ID */
51c7f5ed
HM
823 err = brcmf_usb_dl_cmd(devinfo, DL_GETVER, &id, sizeof(id));
824 if ((err) && (err != -ETIMEDOUT))
825 return err;
71bb244b
AS
826 if (id.chip == cpu_to_le32(BRCMF_POSTBOOT_ID))
827 break;
83bc9c31 828 } while (loop_cnt < BRCMF_USB_RESET_GETVER_LOOP_CNT);
71bb244b
AS
829
830 if (id.chip == cpu_to_le32(BRCMF_POSTBOOT_ID)) {
83bc9c31
HM
831 brcmf_dbg(USB, "postboot chip 0x%x/rev 0x%x\n",
832 le32_to_cpu(id.chip), le32_to_cpu(id.chiprev));
71bb244b 833
83bc9c31 834 brcmf_usb_dl_cmd(devinfo, DL_RESETCFG, &id, sizeof(id));
71bb244b
AS
835 return 0;
836 } else {
5e8149f5 837 brcmf_err("Cannot talk to Dongle. Firmware is not UP, %d ms\n",
83bc9c31 838 BRCMF_USB_RESET_GETVER_SPINWAIT * loop_cnt);
71bb244b
AS
839 return -EINVAL;
840 }
841}
842
843
844static int
845brcmf_usb_dl_send_bulk(struct brcmf_usbdev_info *devinfo, void *buffer, int len)
846{
847 int ret;
848
849 if ((devinfo == NULL) || (devinfo->bulk_urb == NULL))
850 return -EINVAL;
851
852 /* Prepare the URB */
853 usb_fill_bulk_urb(devinfo->bulk_urb, devinfo->usbdev,
854 devinfo->tx_pipe, buffer, len,
855 (usb_complete_t)brcmf_usb_sync_complete, devinfo);
856
857 devinfo->bulk_urb->transfer_flags |= URB_ZERO_PACKET;
858
1e271c95 859 devinfo->ctl_completed = false;
71bb244b
AS
860 ret = usb_submit_urb(devinfo->bulk_urb, GFP_ATOMIC);
861 if (ret) {
5e8149f5 862 brcmf_err("usb_submit_urb failed %d\n", ret);
71bb244b
AS
863 return ret;
864 }
1e271c95
HM
865 ret = brcmf_usb_ioctl_resp_wait(devinfo);
866 return (ret == 0);
71bb244b
AS
867}
868
869static int
870brcmf_usb_dl_writeimage(struct brcmf_usbdev_info *devinfo, u8 *fw, int fwlen)
871{
872 unsigned int sendlen, sent, dllen;
873 char *bulkchunk = NULL, *dlpos;
874 struct rdl_state_le state;
875 u32 rdlstate, rdlbytes;
876 int err = 0;
cb8b73da
HM
877
878 brcmf_dbg(USB, "Enter, fw %p, len %d\n", fw, fwlen);
71bb244b 879
ac83d0b0 880 bulkchunk = kmalloc(TRX_RDL_CHUNK, GFP_ATOMIC);
71bb244b
AS
881 if (bulkchunk == NULL) {
882 err = -ENOMEM;
883 goto fail;
884 }
885
886 /* 1) Prepare USB boot loader for runtime image */
51c7f5ed 887 brcmf_usb_dl_cmd(devinfo, DL_START, &state, sizeof(state));
71bb244b
AS
888
889 rdlstate = le32_to_cpu(state.state);
890 rdlbytes = le32_to_cpu(state.bytes);
891
892 /* 2) Check we are in the Waiting state */
893 if (rdlstate != DL_WAITING) {
5e8149f5 894 brcmf_err("Failed to DL_START\n");
71bb244b
AS
895 err = -EINVAL;
896 goto fail;
897 }
898 sent = 0;
899 dlpos = fw;
900 dllen = fwlen;
901
902 /* Get chip id and rev */
903 while (rdlbytes != dllen) {
904 /* Wait until the usb device reports it received all
905 * the bytes we sent */
906 if ((rdlbytes == sent) && (rdlbytes != dllen)) {
ac83d0b0 907 if ((dllen-sent) < TRX_RDL_CHUNK)
71bb244b
AS
908 sendlen = dllen-sent;
909 else
ac83d0b0 910 sendlen = TRX_RDL_CHUNK;
71bb244b
AS
911
912 /* simply avoid having to send a ZLP by ensuring we
913 * never have an even
914 * multiple of 64
915 */
916 if (!(sendlen % 64))
917 sendlen -= 4;
918
919 /* send data */
920 memcpy(bulkchunk, dlpos, sendlen);
921 if (brcmf_usb_dl_send_bulk(devinfo, bulkchunk,
922 sendlen)) {
5e8149f5 923 brcmf_err("send_bulk failed\n");
71bb244b
AS
924 err = -EINVAL;
925 goto fail;
926 }
927
928 dlpos += sendlen;
929 sent += sendlen;
930 }
51c7f5ed
HM
931 err = brcmf_usb_dl_cmd(devinfo, DL_GETSTATE, &state,
932 sizeof(state));
933 if (err) {
934 brcmf_err("DL_GETSTATE Failed\n");
71bb244b
AS
935 goto fail;
936 }
937
938 rdlstate = le32_to_cpu(state.state);
939 rdlbytes = le32_to_cpu(state.bytes);
940
941 /* restart if an error is reported */
942 if (rdlstate == DL_BAD_HDR || rdlstate == DL_BAD_CRC) {
5e8149f5 943 brcmf_err("Bad Hdr or Bad CRC state %d\n",
71bb244b
AS
944 rdlstate);
945 err = -EINVAL;
946 goto fail;
947 }
948 }
949
950fail:
951 kfree(bulkchunk);
cb8b73da 952 brcmf_dbg(USB, "Exit, err=%d\n", err);
71bb244b
AS
953 return err;
954}
955
956static int brcmf_usb_dlstart(struct brcmf_usbdev_info *devinfo, u8 *fw, int len)
957{
958 int err;
959
cb8b73da 960 brcmf_dbg(USB, "Enter\n");
71bb244b
AS
961
962 if (devinfo == NULL)
963 return -EINVAL;
964
ac94f196 965 if (devinfo->bus_pub.devid == 0xDEAD)
71bb244b
AS
966 return -EINVAL;
967
968 err = brcmf_usb_dl_writeimage(devinfo, fw, len);
969 if (err == 0)
7c38e698 970 devinfo->bus_pub.state = BRCMFMAC_USB_STATE_DL_DONE;
71bb244b 971 else
7c38e698 972 devinfo->bus_pub.state = BRCMFMAC_USB_STATE_DL_FAIL;
cb8b73da 973 brcmf_dbg(USB, "Exit, err=%d\n", err);
71bb244b
AS
974
975 return err;
976}
977
978static int brcmf_usb_dlrun(struct brcmf_usbdev_info *devinfo)
979{
980 struct rdl_state_le state;
981
cb8b73da 982 brcmf_dbg(USB, "Enter\n");
71bb244b
AS
983 if (!devinfo)
984 return -EINVAL;
985
ac94f196 986 if (devinfo->bus_pub.devid == 0xDEAD)
71bb244b
AS
987 return -EINVAL;
988
989 /* Check we are runnable */
51c7f5ed
HM
990 state.state = 0;
991 brcmf_usb_dl_cmd(devinfo, DL_GETSTATE, &state, sizeof(state));
71bb244b
AS
992
993 /* Start the image */
994 if (state.state == cpu_to_le32(DL_RUNNABLE)) {
51c7f5ed 995 if (brcmf_usb_dl_cmd(devinfo, DL_GO, &state, sizeof(state)))
71bb244b
AS
996 return -ENODEV;
997 if (brcmf_usb_resetcfg(devinfo))
998 return -ENODEV;
999 /* The Dongle may go for re-enumeration. */
1000 } else {
5e8149f5 1001 brcmf_err("Dongle not runnable\n");
71bb244b
AS
1002 return -EINVAL;
1003 }
cb8b73da 1004 brcmf_dbg(USB, "Exit\n");
71bb244b
AS
1005 return 0;
1006}
1007
71bb244b
AS
1008static int
1009brcmf_usb_fw_download(struct brcmf_usbdev_info *devinfo)
1010{
71bb244b
AS
1011 int err;
1012
cb8b73da 1013 brcmf_dbg(USB, "Enter\n");
71bb244b
AS
1014 if (devinfo == NULL)
1015 return -ENODEV;
1016
71bb244b 1017 if (!devinfo->image) {
5e8149f5 1018 brcmf_err("No firmware!\n");
71bb244b
AS
1019 return -ENOENT;
1020 }
1021
1022 err = brcmf_usb_dlstart(devinfo,
5b8045d4 1023 (u8 *)devinfo->image, devinfo->image_len);
71bb244b
AS
1024 if (err == 0)
1025 err = brcmf_usb_dlrun(devinfo);
1026 return err;
1027}
1028
1029
d74a0b51 1030static void brcmf_usb_detach(struct brcmf_usbdev_info *devinfo)
71bb244b 1031{
cb8b73da 1032 brcmf_dbg(USB, "Enter, devinfo %p\n", devinfo);
71bb244b 1033
71bb244b
AS
1034 /* free the URBS */
1035 brcmf_usb_free_q(&devinfo->rx_freeq, false);
1036 brcmf_usb_free_q(&devinfo->tx_freeq, false);
1037
71bb244b
AS
1038 usb_free_urb(devinfo->ctl_urb);
1039 usb_free_urb(devinfo->bulk_urb);
1040
1041 kfree(devinfo->tx_reqs);
1042 kfree(devinfo->rx_reqs);
af5b5e62
HM
1043
1044 if (devinfo->settings)
1045 brcmf_release_module_param(devinfo->settings);
71bb244b
AS
1046}
1047
71bb244b
AS
1048
1049static int check_file(const u8 *headers)
1050{
1051 struct trx_header_le *trx;
1052 int actual_len = -1;
1053
cb8b73da 1054 brcmf_dbg(USB, "Enter\n");
71bb244b
AS
1055 /* Extract trx header */
1056 trx = (struct trx_header_le *) headers;
1057 if (trx->magic != cpu_to_le32(TRX_MAGIC))
1058 return -1;
1059
1060 headers += sizeof(struct trx_header_le);
1061
1062 if (le32_to_cpu(trx->flag_version) & TRX_UNCOMP_IMAGE) {
1063 actual_len = le32_to_cpu(trx->offsets[TRX_OFFSETS_DLFWLEN_IDX]);
1064 return actual_len + sizeof(struct trx_header_le);
1065 }
1066 return -1;
1067}
1068
71bb244b
AS
1069
1070static
d74a0b51
HM
1071struct brcmf_usbdev *brcmf_usb_attach(struct brcmf_usbdev_info *devinfo,
1072 int nrxq, int ntxq)
71bb244b 1073{
cb8b73da
HM
1074 brcmf_dbg(USB, "Enter\n");
1075
71bb244b
AS
1076 devinfo->bus_pub.nrxq = nrxq;
1077 devinfo->rx_low_watermark = nrxq / 2;
1078 devinfo->bus_pub.devinfo = devinfo;
1079 devinfo->bus_pub.ntxq = ntxq;
7c38e698 1080 devinfo->bus_pub.state = BRCMFMAC_USB_STATE_DOWN;
71bb244b
AS
1081
1082 /* flow control when too many tx urbs posted */
1083 devinfo->tx_low_watermark = ntxq / 4;
1084 devinfo->tx_high_watermark = devinfo->tx_low_watermark * 3;
71bb244b
AS
1085 devinfo->bus_pub.bus_mtu = BRCMF_USB_MAX_PKT_SIZE;
1086
1087 /* Initialize other structure content */
1088 init_waitqueue_head(&devinfo->ioctl_resp_wait);
1089
1090 /* Initialize the spinlocks */
1091 spin_lock_init(&devinfo->qlock);
df50f756 1092 spin_lock_init(&devinfo->tx_flowblock_lock);
71bb244b
AS
1093
1094 INIT_LIST_HEAD(&devinfo->rx_freeq);
1095 INIT_LIST_HEAD(&devinfo->rx_postq);
1096
1097 INIT_LIST_HEAD(&devinfo->tx_freeq);
1098 INIT_LIST_HEAD(&devinfo->tx_postq);
1099
c6ab4294
HM
1100 devinfo->tx_flowblock = false;
1101
71bb244b
AS
1102 devinfo->rx_reqs = brcmf_usbdev_qinit(&devinfo->rx_freeq, nrxq);
1103 if (!devinfo->rx_reqs)
1104 goto error;
1105
1106 devinfo->tx_reqs = brcmf_usbdev_qinit(&devinfo->tx_freeq, ntxq);
1107 if (!devinfo->tx_reqs)
1108 goto error;
c6ab4294 1109 devinfo->tx_freecount = ntxq;
71bb244b 1110
71bb244b 1111 devinfo->ctl_urb = usb_alloc_urb(0, GFP_ATOMIC);
938f89e5 1112 if (!devinfo->ctl_urb)
71bb244b 1113 goto error;
71bb244b 1114 devinfo->bulk_urb = usb_alloc_urb(0, GFP_ATOMIC);
938f89e5 1115 if (!devinfo->bulk_urb)
71bb244b 1116 goto error;
71bb244b 1117
71bb244b
AS
1118 return &devinfo->bus_pub;
1119
1120error:
5e8149f5 1121 brcmf_err("failed!\n");
d74a0b51 1122 brcmf_usb_detach(devinfo);
71bb244b
AS
1123 return NULL;
1124}
1125
244b124c
HM
1126static void brcmf_usb_wowl_config(struct device *dev, bool enabled)
1127{
1128 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
1129
1130 brcmf_dbg(USB, "Configuring WOWL, enabled=%d\n", enabled);
1131 devinfo->wowl_enabled = enabled;
1132 if (enabled)
1133 device_set_wakeup_enable(devinfo->dev, true);
1134 else
1135 device_set_wakeup_enable(devinfo->dev, false);
1136}
1137
da65bad3
CHH
1138static int brcmf_usb_get_fwname(struct device *dev, u32 chip, u32 chiprev,
1139 u8 *fw_name)
1140{
1141 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
1142 int ret = 0;
1143
1144 if (devinfo->fw_name[0] != '\0')
1145 strlcpy(fw_name, devinfo->fw_name, BRCMF_FW_NAME_LEN);
1146 else
1147 ret = brcmf_fw_map_chip_to_name(chip, chiprev,
1148 brcmf_usb_fwnames,
1149 ARRAY_SIZE(brcmf_usb_fwnames),
1150 fw_name, NULL);
1151
1152 return ret;
1153}
1154
6866a64a 1155static const struct brcmf_bus_ops brcmf_usb_bus_ops = {
d9cb2596 1156 .txdata = brcmf_usb_tx,
d9cb2596
AS
1157 .stop = brcmf_usb_down,
1158 .txctl = brcmf_usb_tx_ctlpkt,
1159 .rxctl = brcmf_usb_rx_ctlpkt,
244b124c 1160 .wowl_config = brcmf_usb_wowl_config,
da65bad3 1161 .get_fwname = brcmf_usb_get_fwname,
d9cb2596
AS
1162};
1163
4dd7de1f
AS
1164static int brcmf_usb_bus_setup(struct brcmf_usbdev_info *devinfo)
1165{
1166 int ret;
1167
1168 /* Attach to the common driver interface */
af5b5e62 1169 ret = brcmf_attach(devinfo->dev, devinfo->settings);
4dd7de1f
AS
1170 if (ret) {
1171 brcmf_err("brcmf_attach failed\n");
1172 return ret;
1173 }
1174
1175 ret = brcmf_usb_up(devinfo->dev);
1176 if (ret)
1177 goto fail;
1178
e8cd4750 1179 ret = brcmf_bus_started(devinfo->dev);
4dd7de1f
AS
1180 if (ret)
1181 goto fail;
1182
1183 return 0;
1184fail:
1185 brcmf_detach(devinfo->dev);
1186 return ret;
1187}
1188
6d0507a7 1189static void brcmf_usb_probe_phase2(struct device *dev, int ret,
5b8045d4
AS
1190 const struct firmware *fw,
1191 void *nvram, u32 nvlen)
1192{
1193 struct brcmf_bus *bus = dev_get_drvdata(dev);
35abcd4f 1194 struct brcmf_usbdev_info *devinfo = bus->bus_priv.usb->devinfo;
6d0507a7
AVS
1195
1196 if (ret)
1197 goto error;
5b8045d4
AS
1198
1199 brcmf_dbg(USB, "Start fw downloading\n");
185f0eb0 1200
5b8045d4
AS
1201 ret = check_file(fw->data);
1202 if (ret < 0) {
1203 brcmf_err("invalid firmware\n");
1204 release_firmware(fw);
1205 goto error;
1206 }
1207
5b8045d4
AS
1208 devinfo->image = fw->data;
1209 devinfo->image_len = fw->size;
1210
1211 ret = brcmf_usb_fw_download(devinfo);
1212 release_firmware(fw);
1213 if (ret)
1214 goto error;
1215
1216 ret = brcmf_usb_bus_setup(devinfo);
1217 if (ret)
1218 goto error;
1219
0f2aa386 1220 complete(&devinfo->dev_init_done);
5b8045d4
AS
1221 return;
1222error:
1223 brcmf_dbg(TRACE, "failed: dev=%s, err=%d\n", dev_name(dev), ret);
0f2aa386 1224 complete(&devinfo->dev_init_done);
5b8045d4
AS
1225 device_release_driver(dev);
1226}
1227
b87e2c48 1228static int brcmf_usb_probe_cb(struct brcmf_usbdev_info *devinfo)
71bb244b
AS
1229{
1230 struct brcmf_bus *bus = NULL;
1231 struct brcmf_usbdev *bus_pub = NULL;
d74a0b51 1232 struct device *dev = devinfo->dev;
5b8045d4 1233 int ret;
71bb244b 1234
cb8b73da 1235 brcmf_dbg(USB, "Enter\n");
d74a0b51 1236 bus_pub = brcmf_usb_attach(devinfo, BRCMF_USB_NRXQ, BRCMF_USB_NTXQ);
9cd23a71
HM
1237 if (!bus_pub)
1238 return -ENODEV;
71bb244b
AS
1239
1240 bus = kzalloc(sizeof(struct brcmf_bus), GFP_ATOMIC);
1241 if (!bus) {
1242 ret = -ENOMEM;
1243 goto fail;
1244 }
1245
d9cb2596 1246 bus->dev = dev;
71bb244b 1247 bus_pub->bus = bus;
71bb244b
AS
1248 bus->bus_priv.usb = bus_pub;
1249 dev_set_drvdata(dev, bus);
d9cb2596 1250 bus->ops = &brcmf_usb_bus_ops;
943258b6 1251 bus->proto_type = BRCMF_PROTO_BCDC;
9cd18359 1252 bus->always_use_fws_queue = true;
244b124c
HM
1253#ifdef CONFIG_PM
1254 bus->wowl_supported = true;
1255#endif
71bb244b 1256
af5b5e62
HM
1257 devinfo->settings = brcmf_get_module_param(bus->dev, BRCMF_BUSTYPE_USB,
1258 bus_pub->devid,
1259 bus_pub->chiprev);
1260 if (!devinfo->settings) {
1261 ret = -ENOMEM;
1262 goto fail;
1263 }
1264
5b8045d4
AS
1265 if (!brcmf_usb_dlneeded(devinfo)) {
1266 ret = brcmf_usb_bus_setup(devinfo);
1267 if (ret)
1268 goto fail;
86fec35e 1269 /* we are done */
0f2aa386 1270 complete(&devinfo->dev_init_done);
86fec35e 1271 return 0;
71bb244b 1272 }
c6bff544
AS
1273 bus->chip = bus_pub->devid;
1274 bus->chiprev = bus_pub->chiprev;
1275
46d703a7
HM
1276 ret = brcmf_fw_map_chip_to_name(bus_pub->devid, bus_pub->chiprev,
1277 brcmf_usb_fwnames,
1278 ARRAY_SIZE(brcmf_usb_fwnames),
1279 devinfo->fw_name, NULL);
1280 if (ret)
1281 goto fail;
1282
5b8045d4 1283 /* request firmware here */
46d703a7
HM
1284 ret = brcmf_fw_get_firmwares(dev, 0, devinfo->fw_name, NULL,
1285 brcmf_usb_probe_phase2);
6e651045
RM
1286 if (ret) {
1287 brcmf_err("firmware request failed: %d\n", ret);
1288 goto fail;
1289 }
1290
71bb244b 1291 return 0;
5b8045d4 1292
71bb244b
AS
1293fail:
1294 /* Release resources in reverse order */
71bb244b 1295 kfree(bus);
d74a0b51 1296 brcmf_usb_detach(devinfo);
71bb244b
AS
1297 return ret;
1298}
1299
1300static void
d74a0b51 1301brcmf_usb_disconnect_cb(struct brcmf_usbdev_info *devinfo)
71bb244b 1302{
d74a0b51 1303 if (!devinfo)
71bb244b 1304 return;
cb8b73da 1305 brcmf_dbg(USB, "Enter, bus_pub %p\n", devinfo);
71bb244b 1306
d74a0b51
HM
1307 brcmf_detach(devinfo->dev);
1308 kfree(devinfo->bus_pub.bus);
1309 brcmf_usb_detach(devinfo);
71bb244b
AS
1310}
1311
1312static int
1313brcmf_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
1314{
71bb244b 1315 struct usb_device *usb = interface_to_usbdev(intf);
d74a0b51 1316 struct brcmf_usbdev_info *devinfo;
51c7f5ed
HM
1317 struct usb_interface_descriptor *desc;
1318 struct usb_endpoint_descriptor *endpoint;
1319 int ret = 0;
1320 u32 num_of_eps;
1321 u8 endpoint_num, ep;
71bb244b 1322
b6fd7fd2 1323 brcmf_dbg(USB, "Enter 0x%04x:0x%04x\n", id->idVendor, id->idProduct);
71bb244b 1324
d74a0b51
HM
1325 devinfo = kzalloc(sizeof(*devinfo), GFP_ATOMIC);
1326 if (devinfo == NULL)
1327 return -ENOMEM;
71bb244b 1328
d74a0b51
HM
1329 devinfo->usbdev = usb;
1330 devinfo->dev = &usb->dev;
0f2aa386 1331 /* Init completion, to protect for disconnect while still loading.
185f0eb0
HM
1332 * Necessary because of the asynchronous firmware load construction
1333 */
0f2aa386 1334 init_completion(&devinfo->dev_init_done);
185f0eb0 1335
d74a0b51 1336 usb_set_intfdata(intf, devinfo);
71bb244b
AS
1337
1338 /* Check that the device supports only one configuration */
1339 if (usb->descriptor.bNumConfigurations != 1) {
51c7f5ed
HM
1340 brcmf_err("Number of configurations: %d not supported\n",
1341 usb->descriptor.bNumConfigurations);
1342 ret = -ENODEV;
71bb244b
AS
1343 goto fail;
1344 }
1345
51c7f5ed
HM
1346 if ((usb->descriptor.bDeviceClass != USB_CLASS_VENDOR_SPEC) &&
1347 (usb->descriptor.bDeviceClass != USB_CLASS_MISC) &&
1348 (usb->descriptor.bDeviceClass != USB_CLASS_WIRELESS_CONTROLLER)) {
1349 brcmf_err("Device class: 0x%x not supported\n",
1350 usb->descriptor.bDeviceClass);
1351 ret = -ENODEV;
71bb244b
AS
1352 goto fail;
1353 }
1354
684cf943 1355 desc = &intf->cur_altsetting->desc;
51c7f5ed
HM
1356 if ((desc->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
1357 (desc->bInterfaceSubClass != 2) ||
1358 (desc->bInterfaceProtocol != 0xff)) {
1359 brcmf_err("non WLAN interface %d: 0x%x:0x%x:0x%x\n",
1360 desc->bInterfaceNumber, desc->bInterfaceClass,
1361 desc->bInterfaceSubClass, desc->bInterfaceProtocol);
1362 ret = -ENODEV;
71bb244b
AS
1363 goto fail;
1364 }
1365
51c7f5ed
HM
1366 num_of_eps = desc->bNumEndpoints;
1367 for (ep = 0; ep < num_of_eps; ep++) {
684cf943 1368 endpoint = &intf->cur_altsetting->endpoint[ep].desc;
51c7f5ed
HM
1369 endpoint_num = usb_endpoint_num(endpoint);
1370 if (!usb_endpoint_xfer_bulk(endpoint))
1371 continue;
1372 if (usb_endpoint_dir_in(endpoint)) {
1373 if (!devinfo->rx_pipe)
d74a0b51 1374 devinfo->rx_pipe =
71bb244b 1375 usb_rcvbulkpipe(usb, endpoint_num);
71bb244b 1376 } else {
51c7f5ed
HM
1377 if (!devinfo->tx_pipe)
1378 devinfo->tx_pipe =
1379 usb_sndbulkpipe(usb, endpoint_num);
71bb244b
AS
1380 }
1381 }
51c7f5ed
HM
1382 if (devinfo->rx_pipe == 0) {
1383 brcmf_err("No RX (in) Bulk EP found\n");
1384 ret = -ENODEV;
1385 goto fail;
1386 }
1387 if (devinfo->tx_pipe == 0) {
1388 brcmf_err("No TX (out) Bulk EP found\n");
1389 ret = -ENODEV;
1390 goto fail;
1391 }
1392
1393 devinfo->ifnum = desc->bInterfaceNumber;
71bb244b 1394
8caf115c
ON
1395 if (usb->speed == USB_SPEED_SUPER_PLUS)
1396 brcmf_dbg(USB, "Broadcom super speed plus USB WLAN interface detected\n");
1397 else if (usb->speed == USB_SPEED_SUPER)
51c7f5ed 1398 brcmf_dbg(USB, "Broadcom super speed USB WLAN interface detected\n");
de389a53 1399 else if (usb->speed == USB_SPEED_HIGH)
51c7f5ed 1400 brcmf_dbg(USB, "Broadcom high speed USB WLAN interface detected\n");
71bb244b 1401 else
51c7f5ed 1402 brcmf_dbg(USB, "Broadcom full speed USB WLAN interface detected\n");
71bb244b 1403
b87e2c48 1404 ret = brcmf_usb_probe_cb(devinfo);
71bb244b
AS
1405 if (ret)
1406 goto fail;
1407
1408 /* Success */
1409 return 0;
1410
1411fail:
0f2aa386 1412 complete(&devinfo->dev_init_done);
d74a0b51 1413 kfree(devinfo);
71bb244b
AS
1414 usb_set_intfdata(intf, NULL);
1415 return ret;
71bb244b
AS
1416}
1417
1418static void
1419brcmf_usb_disconnect(struct usb_interface *intf)
1420{
d74a0b51 1421 struct brcmf_usbdev_info *devinfo;
71bb244b 1422
cb8b73da 1423 brcmf_dbg(USB, "Enter\n");
d74a0b51 1424 devinfo = (struct brcmf_usbdev_info *)usb_get_intfdata(intf);
185f0eb0
HM
1425
1426 if (devinfo) {
0f2aa386 1427 wait_for_completion(&devinfo->dev_init_done);
185f0eb0
HM
1428 /* Make sure that devinfo still exists. Firmware probe routines
1429 * may have released the device and cleared the intfdata.
1430 */
1431 if (!usb_get_intfdata(intf))
1432 goto done;
1433
1434 brcmf_usb_disconnect_cb(devinfo);
1435 kfree(devinfo);
1436 }
1437done:
cb8b73da 1438 brcmf_dbg(USB, "Exit\n");
71bb244b
AS
1439}
1440
1441/*
7c38e698 1442 * only need to signal the bus being down and update the state.
71bb244b
AS
1443 */
1444static int brcmf_usb_suspend(struct usb_interface *intf, pm_message_t state)
1445{
1446 struct usb_device *usb = interface_to_usbdev(intf);
1447 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(&usb->dev);
1448
cb8b73da 1449 brcmf_dbg(USB, "Enter\n");
7c38e698 1450 devinfo->bus_pub.state = BRCMFMAC_USB_STATE_SLEEP;
244b124c
HM
1451 if (devinfo->wowl_enabled)
1452 brcmf_cancel_all_urbs(devinfo);
1453 else
1454 brcmf_detach(&usb->dev);
71bb244b
AS
1455 return 0;
1456}
1457
1458/*
7c38e698 1459 * (re-) start the bus.
71bb244b
AS
1460 */
1461static int brcmf_usb_resume(struct usb_interface *intf)
1462{
1463 struct usb_device *usb = interface_to_usbdev(intf);
1464 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(&usb->dev);
1465
cb8b73da 1466 brcmf_dbg(USB, "Enter\n");
244b124c
HM
1467 if (!devinfo->wowl_enabled)
1468 return brcmf_usb_bus_setup(devinfo);
1469
1470 devinfo->bus_pub.state = BRCMFMAC_USB_STATE_UP;
1471 brcmf_usb_rx_fill_all(devinfo);
1472 return 0;
71bb244b
AS
1473}
1474
7c38e698
HM
1475static int brcmf_usb_reset_resume(struct usb_interface *intf)
1476{
1477 struct usb_device *usb = interface_to_usbdev(intf);
1478 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(&usb->dev);
51c7f5ed 1479
7c38e698
HM
1480 brcmf_dbg(USB, "Enter\n");
1481
46d703a7 1482 return brcmf_fw_get_firmwares(&usb->dev, 0, devinfo->fw_name, NULL,
5b8045d4 1483 brcmf_usb_probe_phase2);
7c38e698
HM
1484}
1485
5779ae6a
HM
1486#define BRCMF_USB_DEVICE(dev_id) \
1487 { USB_DEVICE(BRCM_USB_VENDOR_ID_BROADCOM, dev_id) }
71bb244b 1488
bccf3ffc
IL
1489#define LINKSYS_USB_DEVICE(dev_id) \
1490 { USB_DEVICE(BRCM_USB_VENDOR_ID_LINKSYS, dev_id) }
1491
0ec9eb90
CHL
1492#define CYPRESS_USB_DEVICE(dev_id) \
1493 { USB_DEVICE(CY_USB_VENDOR_ID_CYPRESS, dev_id) }
1494
a67b133b 1495static const struct usb_device_id brcmf_usb_devid_table[] = {
5779ae6a
HM
1496 BRCMF_USB_DEVICE(BRCM_USB_43143_DEVICE_ID),
1497 BRCMF_USB_DEVICE(BRCM_USB_43236_DEVICE_ID),
1498 BRCMF_USB_DEVICE(BRCM_USB_43242_DEVICE_ID),
1499 BRCMF_USB_DEVICE(BRCM_USB_43569_DEVICE_ID),
bccf3ffc 1500 LINKSYS_USB_DEVICE(BRCM_USB_43235_LINKSYS_DEVICE_ID),
0ec9eb90 1501 CYPRESS_USB_DEVICE(CY_USB_4373_DEVICE_ID),
540313d8 1502 { USB_DEVICE(BRCM_USB_VENDOR_ID_LG, BRCM_USB_43242_LG_DEVICE_ID) },
71bb244b 1503 /* special entry for device with firmware loaded and running */
5779ae6a 1504 BRCMF_USB_DEVICE(BRCM_USB_BCMFW_DEVICE_ID),
0ec9eb90 1505 CYPRESS_USB_DEVICE(BRCM_USB_BCMFW_DEVICE_ID),
5779ae6a 1506 { /* end: all zeroes */ }
71bb244b 1507};
5cfd6e88 1508
71bb244b 1509MODULE_DEVICE_TABLE(usb, brcmf_usb_devid_table);
71bb244b 1510
71bb244b
AS
1511static struct usb_driver brcmf_usbdrvr = {
1512 .name = KBUILD_MODNAME,
1513 .probe = brcmf_usb_probe,
1514 .disconnect = brcmf_usb_disconnect,
1515 .id_table = brcmf_usb_devid_table,
1516 .suspend = brcmf_usb_suspend,
1517 .resume = brcmf_usb_resume,
7c38e698 1518 .reset_resume = brcmf_usb_reset_resume,
e1f12eb6 1519 .disable_hub_initiated_lpm = 1,
71bb244b
AS
1520};
1521
81d5f1bb
AS
1522static int brcmf_usb_reset_device(struct device *dev, void *notused)
1523{
1524 /* device past is the usb interface so we
1525 * need to use parent here.
1526 */
1527 brcmf_dev_reset(dev->parent);
1528 return 0;
1529}
803599d4 1530
71bb244b
AS
1531void brcmf_usb_exit(void)
1532{
81d5f1bb
AS
1533 struct device_driver *drv = &brcmf_usbdrvr.drvwrap.driver;
1534 int ret;
1535
cb8b73da 1536 brcmf_dbg(USB, "Enter\n");
81d5f1bb
AS
1537 ret = driver_for_each_device(drv, NULL, NULL,
1538 brcmf_usb_reset_device);
71bb244b 1539 usb_deregister(&brcmf_usbdrvr);
71bb244b
AS
1540}
1541
db4efbbe 1542void brcmf_usb_register(void)
71bb244b 1543{
cb8b73da 1544 brcmf_dbg(USB, "Enter\n");
549040ab 1545 usb_register(&brcmf_usbdrvr);
71bb244b 1546}