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