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