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