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