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