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