1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Copyright (C) 2008, cozybit Inc.
4  *  Copyright (C) 2003-2006, Marvell International Ltd.
5  */
6 #define DRV_NAME "lbtf_usb"
7 
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 
10 #include "libertas_tf.h"
11 #include "if_usb.h"
12 
13 #include <linux/delay.h>
14 #include <linux/module.h>
15 #include <linux/firmware.h>
16 #include <linux/netdevice.h>
17 #include <linux/slab.h>
18 #include <linux/usb.h>
19 
20 #define INSANEDEBUG	0
21 #define lbtf_deb_usb2(...) do { if (INSANEDEBUG) lbtf_deb_usbd(__VA_ARGS__); } while (0)
22 
23 #define MESSAGE_HEADER_LEN	4
24 
25 static char *lbtf_fw_name = "lbtf_usb.bin";
26 module_param_named(fw_name, lbtf_fw_name, charp, 0644);
27 
28 MODULE_FIRMWARE("lbtf_usb.bin");
29 
30 static const struct usb_device_id if_usb_table[] = {
31 	/* Enter the device signature inside */
32 	{ USB_DEVICE(0x1286, 0x2001) },
33 	{ USB_DEVICE(0x05a3, 0x8388) },
34 	{}	/* Terminating entry */
35 };
36 
37 MODULE_DEVICE_TABLE(usb, if_usb_table);
38 
39 static void if_usb_receive(struct urb *urb);
40 static void if_usb_receive_fwload(struct urb *urb);
41 static int if_usb_prog_firmware(struct lbtf_private *priv);
42 static int if_usb_host_to_card(struct lbtf_private *priv, uint8_t type,
43 			       uint8_t *payload, uint16_t nb);
44 static int usb_tx_block(struct if_usb_card *cardp, uint8_t *payload,
45 			uint16_t nb, u8 data);
46 static void if_usb_free(struct if_usb_card *cardp);
47 static int if_usb_submit_rx_urb(struct if_usb_card *cardp);
48 static int if_usb_reset_device(struct lbtf_private *priv);
49 
50 /**
51  *  if_usb_wrike_bulk_callback -  call back to handle URB status
52  *
53  *  @param urb		pointer to urb structure
54  */
55 static void if_usb_write_bulk_callback(struct urb *urb)
56 {
57 	if (urb->status != 0) {
58 		/* print the failure status number for debug */
59 		pr_info("URB in failure status: %d\n", urb->status);
60 	} else {
61 		lbtf_deb_usb2(&urb->dev->dev, "URB status is successful\n");
62 		lbtf_deb_usb2(&urb->dev->dev, "Actual length transmitted %d\n",
63 			     urb->actual_length);
64 	}
65 }
66 
67 /**
68  *  if_usb_free - free tx/rx urb, skb and rx buffer
69  *
70  *  @param cardp	pointer if_usb_card
71  */
72 static void if_usb_free(struct if_usb_card *cardp)
73 {
74 	lbtf_deb_enter(LBTF_DEB_USB);
75 
76 	/* Unlink tx & rx urb */
77 	usb_kill_urb(cardp->tx_urb);
78 	usb_kill_urb(cardp->rx_urb);
79 	usb_kill_urb(cardp->cmd_urb);
80 
81 	usb_free_urb(cardp->tx_urb);
82 	cardp->tx_urb = NULL;
83 
84 	usb_free_urb(cardp->rx_urb);
85 	cardp->rx_urb = NULL;
86 
87 	usb_free_urb(cardp->cmd_urb);
88 	cardp->cmd_urb = NULL;
89 
90 	kfree(cardp->ep_out_buf);
91 	cardp->ep_out_buf = NULL;
92 
93 	lbtf_deb_leave(LBTF_DEB_USB);
94 }
95 
96 static void if_usb_setup_firmware(struct lbtf_private *priv)
97 {
98 	struct if_usb_card *cardp = priv->card;
99 	struct cmd_ds_set_boot2_ver b2_cmd;
100 
101 	lbtf_deb_enter(LBTF_DEB_USB);
102 
103 	if_usb_submit_rx_urb(cardp);
104 	b2_cmd.hdr.size = cpu_to_le16(sizeof(b2_cmd));
105 	b2_cmd.action = 0;
106 	b2_cmd.version = cardp->boot2_version;
107 
108 	if (lbtf_cmd_with_response(priv, CMD_SET_BOOT2_VER, &b2_cmd))
109 		lbtf_deb_usb("Setting boot2 version failed\n");
110 
111 	lbtf_deb_leave(LBTF_DEB_USB);
112 }
113 
114 static void if_usb_fw_timeo(struct timer_list *t)
115 {
116 	struct if_usb_card *cardp = from_timer(cardp, t, fw_timeout);
117 
118 	lbtf_deb_enter(LBTF_DEB_USB);
119 	if (!cardp->fwdnldover) {
120 		/* Download timed out */
121 		cardp->priv->surpriseremoved = 1;
122 		pr_err("Download timed out\n");
123 	} else {
124 		lbtf_deb_usb("Download complete, no event. Assuming success\n");
125 	}
126 	wake_up(&cardp->fw_wq);
127 	lbtf_deb_leave(LBTF_DEB_USB);
128 }
129 
130 static const struct lbtf_ops if_usb_ops = {
131 	.hw_host_to_card = if_usb_host_to_card,
132 	.hw_prog_firmware = if_usb_prog_firmware,
133 	.hw_reset_device = if_usb_reset_device,
134 };
135 
136 /**
137  *  if_usb_probe - sets the configuration values
138  *
139  *  @ifnum	interface number
140  *  @id		pointer to usb_device_id
141  *
142  *  Returns: 0 on success, error code on failure
143  */
144 static int if_usb_probe(struct usb_interface *intf,
145 			const struct usb_device_id *id)
146 {
147 	struct usb_device *udev;
148 	struct usb_host_interface *iface_desc;
149 	struct usb_endpoint_descriptor *endpoint;
150 	struct lbtf_private *priv;
151 	struct if_usb_card *cardp;
152 	int i;
153 
154 	lbtf_deb_enter(LBTF_DEB_USB);
155 	udev = interface_to_usbdev(intf);
156 
157 	cardp = kzalloc(sizeof(struct if_usb_card), GFP_KERNEL);
158 	if (!cardp)
159 		goto error;
160 
161 	timer_setup(&cardp->fw_timeout, if_usb_fw_timeo, 0);
162 	init_waitqueue_head(&cardp->fw_wq);
163 
164 	cardp->udev = udev;
165 	iface_desc = intf->cur_altsetting;
166 
167 	lbtf_deb_usbd(&udev->dev, "bcdUSB = 0x%X bDeviceClass = 0x%X"
168 		     " bDeviceSubClass = 0x%X, bDeviceProtocol = 0x%X\n",
169 		     le16_to_cpu(udev->descriptor.bcdUSB),
170 		     udev->descriptor.bDeviceClass,
171 		     udev->descriptor.bDeviceSubClass,
172 		     udev->descriptor.bDeviceProtocol);
173 
174 	for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
175 		endpoint = &iface_desc->endpoint[i].desc;
176 		if (usb_endpoint_is_bulk_in(endpoint)) {
177 			cardp->ep_in_size =
178 				le16_to_cpu(endpoint->wMaxPacketSize);
179 			cardp->ep_in = usb_endpoint_num(endpoint);
180 
181 			lbtf_deb_usbd(&udev->dev, "in_endpoint = %d\n",
182 				cardp->ep_in);
183 			lbtf_deb_usbd(&udev->dev, "Bulk in size is %d\n",
184 				cardp->ep_in_size);
185 		} else if (usb_endpoint_is_bulk_out(endpoint)) {
186 			cardp->ep_out_size =
187 				le16_to_cpu(endpoint->wMaxPacketSize);
188 			cardp->ep_out = usb_endpoint_num(endpoint);
189 
190 			lbtf_deb_usbd(&udev->dev, "out_endpoint = %d\n",
191 				cardp->ep_out);
192 			lbtf_deb_usbd(&udev->dev, "Bulk out size is %d\n",
193 				cardp->ep_out_size);
194 		}
195 	}
196 	if (!cardp->ep_out_size || !cardp->ep_in_size) {
197 		lbtf_deb_usbd(&udev->dev, "Endpoints not found\n");
198 		/* Endpoints not found */
199 		goto dealloc;
200 	}
201 
202 	cardp->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
203 	if (!cardp->rx_urb)
204 		goto dealloc;
205 
206 	cardp->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
207 	if (!cardp->tx_urb)
208 		goto dealloc;
209 
210 	cardp->cmd_urb = usb_alloc_urb(0, GFP_KERNEL);
211 	if (!cardp->cmd_urb)
212 		goto dealloc;
213 
214 	cardp->ep_out_buf = kmalloc(MRVDRV_ETH_TX_PACKET_BUFFER_SIZE,
215 				    GFP_KERNEL);
216 	if (!cardp->ep_out_buf) {
217 		lbtf_deb_usbd(&udev->dev, "Could not allocate buffer\n");
218 		goto dealloc;
219 	}
220 
221 	cardp->boot2_version = udev->descriptor.bcdDevice;
222 	priv = lbtf_add_card(cardp, &udev->dev, &if_usb_ops);
223 	if (!priv)
224 		goto dealloc;
225 
226 	usb_get_dev(udev);
227 	usb_set_intfdata(intf, cardp);
228 
229 	return 0;
230 
231 dealloc:
232 	if_usb_free(cardp);
233 error:
234 lbtf_deb_leave(LBTF_DEB_MAIN);
235 	return -ENOMEM;
236 }
237 
238 /**
239  *  if_usb_disconnect -  free resource and cleanup
240  *
241  *  @intf	USB interface structure
242  */
243 static void if_usb_disconnect(struct usb_interface *intf)
244 {
245 	struct if_usb_card *cardp = usb_get_intfdata(intf);
246 	struct lbtf_private *priv = cardp->priv;
247 
248 	lbtf_deb_enter(LBTF_DEB_MAIN);
249 
250 	if_usb_reset_device(priv);
251 
252 	if (priv)
253 		lbtf_remove_card(priv);
254 
255 	/* Unlink and free urb */
256 	if_usb_free(cardp);
257 
258 	usb_set_intfdata(intf, NULL);
259 	usb_put_dev(interface_to_usbdev(intf));
260 
261 	lbtf_deb_leave(LBTF_DEB_MAIN);
262 }
263 
264 /**
265  *  if_usb_send_fw_pkt -  This function downloads the FW
266  *
267  *  @priv	pointer to struct lbtf_private
268  *
269  *  Returns: 0
270  */
271 static int if_usb_send_fw_pkt(struct if_usb_card *cardp)
272 {
273 	struct fwdata *fwdata = cardp->ep_out_buf;
274 	u8 *firmware = (u8 *) cardp->fw->data;
275 
276 	lbtf_deb_enter(LBTF_DEB_FW);
277 
278 	/* If we got a CRC failure on the last block, back
279 	   up and retry it */
280 	if (!cardp->CRC_OK) {
281 		cardp->totalbytes = cardp->fwlastblksent;
282 		cardp->fwseqnum--;
283 	}
284 
285 	lbtf_deb_usb2(&cardp->udev->dev, "totalbytes = %d\n",
286 		     cardp->totalbytes);
287 
288 	/* struct fwdata (which we sent to the card) has an
289 	   extra __le32 field in between the header and the data,
290 	   which is not in the struct fwheader in the actual
291 	   firmware binary. Insert the seqnum in the middle... */
292 	memcpy(&fwdata->hdr, &firmware[cardp->totalbytes],
293 	       sizeof(struct fwheader));
294 
295 	cardp->fwlastblksent = cardp->totalbytes;
296 	cardp->totalbytes += sizeof(struct fwheader);
297 
298 	memcpy(fwdata->data, &firmware[cardp->totalbytes],
299 	       le32_to_cpu(fwdata->hdr.datalength));
300 
301 	lbtf_deb_usb2(&cardp->udev->dev, "Data length = %d\n",
302 		     le32_to_cpu(fwdata->hdr.datalength));
303 
304 	fwdata->seqnum = cpu_to_le32(++cardp->fwseqnum);
305 	cardp->totalbytes += le32_to_cpu(fwdata->hdr.datalength);
306 
307 	usb_tx_block(cardp, cardp->ep_out_buf, sizeof(struct fwdata) +
308 		     le32_to_cpu(fwdata->hdr.datalength), 0);
309 
310 	if (fwdata->hdr.dnldcmd == cpu_to_le32(FW_HAS_DATA_TO_RECV)) {
311 		lbtf_deb_usb2(&cardp->udev->dev, "There are data to follow\n");
312 		lbtf_deb_usb2(&cardp->udev->dev,
313 			"seqnum = %d totalbytes = %d\n",
314 			cardp->fwseqnum, cardp->totalbytes);
315 	} else if (fwdata->hdr.dnldcmd == cpu_to_le32(FW_HAS_LAST_BLOCK)) {
316 		lbtf_deb_usb2(&cardp->udev->dev,
317 			"Host has finished FW downloading\n");
318 		lbtf_deb_usb2(&cardp->udev->dev, "Downloading FW JUMP BLOCK\n");
319 
320 		/* Host has finished FW downloading
321 		 * Donwloading FW JUMP BLOCK
322 		 */
323 		cardp->fwfinalblk = 1;
324 	}
325 
326 	lbtf_deb_usb2(&cardp->udev->dev, "Firmware download done; size %d\n",
327 		     cardp->totalbytes);
328 
329 	lbtf_deb_leave(LBTF_DEB_FW);
330 	return 0;
331 }
332 
333 static int if_usb_reset_device(struct lbtf_private *priv)
334 {
335 	struct if_usb_card *cardp = priv->card;
336 	struct cmd_ds_802_11_reset *cmd = cardp->ep_out_buf + 4;
337 	int ret;
338 
339 	lbtf_deb_enter(LBTF_DEB_USB);
340 
341 	*(__le32 *)cardp->ep_out_buf = cpu_to_le32(CMD_TYPE_REQUEST);
342 
343 	cmd->hdr.command = cpu_to_le16(CMD_802_11_RESET);
344 	cmd->hdr.size = cpu_to_le16(sizeof(struct cmd_ds_802_11_reset));
345 	cmd->hdr.result = cpu_to_le16(0);
346 	cmd->hdr.seqnum = cpu_to_le16(0x5a5a);
347 	cmd->action = cpu_to_le16(CMD_ACT_HALT);
348 	usb_tx_block(cardp, cardp->ep_out_buf,
349 		     4 + sizeof(struct cmd_ds_802_11_reset), 0);
350 
351 	msleep(100);
352 	ret = usb_reset_device(cardp->udev);
353 	msleep(100);
354 
355 	lbtf_deb_leave_args(LBTF_DEB_USB, "ret %d", ret);
356 
357 	return ret;
358 }
359 
360 /**
361  *  usb_tx_block - transfer data to the device
362  *
363  *  @priv	pointer to struct lbtf_private
364  *  @payload	pointer to payload data
365  *  @nb		data length
366  *  @data	non-zero for data, zero for commands
367  *
368  *  Returns: 0 on success, nonzero otherwise.
369  */
370 static int usb_tx_block(struct if_usb_card *cardp, uint8_t *payload,
371 			uint16_t nb, u8 data)
372 {
373 	int ret = -1;
374 	struct urb *urb;
375 
376 	lbtf_deb_enter(LBTF_DEB_USB);
377 	/* check if device is removed */
378 	if (cardp->priv->surpriseremoved) {
379 		lbtf_deb_usbd(&cardp->udev->dev, "Device removed\n");
380 		goto tx_ret;
381 	}
382 
383 	if (data)
384 		urb = cardp->tx_urb;
385 	else
386 		urb = cardp->cmd_urb;
387 
388 	usb_fill_bulk_urb(urb, cardp->udev,
389 			  usb_sndbulkpipe(cardp->udev,
390 					  cardp->ep_out),
391 			  payload, nb, if_usb_write_bulk_callback, cardp);
392 
393 	urb->transfer_flags |= URB_ZERO_PACKET;
394 
395 	if (usb_submit_urb(urb, GFP_ATOMIC)) {
396 		lbtf_deb_usbd(&cardp->udev->dev,
397 			"usb_submit_urb failed: %d\n", ret);
398 		goto tx_ret;
399 	}
400 
401 	lbtf_deb_usb2(&cardp->udev->dev, "usb_submit_urb success\n");
402 
403 	ret = 0;
404 
405 tx_ret:
406 	lbtf_deb_leave(LBTF_DEB_USB);
407 	return ret;
408 }
409 
410 static int __if_usb_submit_rx_urb(struct if_usb_card *cardp,
411 				  void (*callbackfn)(struct urb *urb))
412 {
413 	struct sk_buff *skb;
414 	int ret = -1;
415 
416 	lbtf_deb_enter(LBTF_DEB_USB);
417 
418 	skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE);
419 	if (!skb) {
420 		pr_err("No free skb\n");
421 		lbtf_deb_leave(LBTF_DEB_USB);
422 		return -1;
423 	}
424 
425 	cardp->rx_skb = skb;
426 
427 	/* Fill the receive configuration URB and initialise the Rx call back */
428 	usb_fill_bulk_urb(cardp->rx_urb, cardp->udev,
429 			  usb_rcvbulkpipe(cardp->udev, cardp->ep_in),
430 			  skb_tail_pointer(skb),
431 			  MRVDRV_ETH_RX_PACKET_BUFFER_SIZE, callbackfn, cardp);
432 
433 	lbtf_deb_usb2(&cardp->udev->dev, "Pointer for rx_urb %p\n",
434 		cardp->rx_urb);
435 	ret = usb_submit_urb(cardp->rx_urb, GFP_ATOMIC);
436 	if (ret) {
437 		lbtf_deb_usbd(&cardp->udev->dev,
438 			"Submit Rx URB failed: %d\n", ret);
439 		kfree_skb(skb);
440 		cardp->rx_skb = NULL;
441 		lbtf_deb_leave(LBTF_DEB_USB);
442 		return -1;
443 	} else {
444 		lbtf_deb_usb2(&cardp->udev->dev, "Submit Rx URB success\n");
445 		lbtf_deb_leave(LBTF_DEB_USB);
446 		return 0;
447 	}
448 }
449 
450 static int if_usb_submit_rx_urb_fwload(struct if_usb_card *cardp)
451 {
452 	return __if_usb_submit_rx_urb(cardp, &if_usb_receive_fwload);
453 }
454 
455 static int if_usb_submit_rx_urb(struct if_usb_card *cardp)
456 {
457 	return __if_usb_submit_rx_urb(cardp, &if_usb_receive);
458 }
459 
460 static void if_usb_receive_fwload(struct urb *urb)
461 {
462 	struct if_usb_card *cardp = urb->context;
463 	struct sk_buff *skb = cardp->rx_skb;
464 	struct fwsyncheader *syncfwheader;
465 	struct bootcmdresp bcmdresp;
466 
467 	lbtf_deb_enter(LBTF_DEB_USB);
468 	if (urb->status) {
469 		lbtf_deb_usbd(&cardp->udev->dev,
470 			     "URB status is failed during fw load\n");
471 		kfree_skb(skb);
472 		lbtf_deb_leave(LBTF_DEB_USB);
473 		return;
474 	}
475 
476 	if (cardp->fwdnldover) {
477 		__le32 *tmp = (__le32 *)(skb->data);
478 
479 		if (tmp[0] == cpu_to_le32(CMD_TYPE_INDICATION) &&
480 		    tmp[1] == cpu_to_le32(MACREG_INT_CODE_FIRMWARE_READY)) {
481 			/* Firmware ready event received */
482 			pr_info("Firmware ready event received\n");
483 			wake_up(&cardp->fw_wq);
484 		} else {
485 			lbtf_deb_usb("Waiting for confirmation; got %x %x\n",
486 				    le32_to_cpu(tmp[0]), le32_to_cpu(tmp[1]));
487 			if_usb_submit_rx_urb_fwload(cardp);
488 		}
489 		kfree_skb(skb);
490 		lbtf_deb_leave(LBTF_DEB_USB);
491 		return;
492 	}
493 	if (cardp->bootcmdresp <= 0) {
494 		memcpy(&bcmdresp, skb->data, sizeof(bcmdresp));
495 
496 		if (le16_to_cpu(cardp->udev->descriptor.bcdDevice) < 0x3106) {
497 			kfree_skb(skb);
498 			if_usb_submit_rx_urb_fwload(cardp);
499 			cardp->bootcmdresp = 1;
500 			/* Received valid boot command response */
501 			lbtf_deb_usbd(&cardp->udev->dev,
502 				     "Received valid boot command response\n");
503 			lbtf_deb_leave(LBTF_DEB_USB);
504 			return;
505 		}
506 		if (bcmdresp.magic != cpu_to_le32(BOOT_CMD_MAGIC_NUMBER)) {
507 			if (bcmdresp.magic == cpu_to_le32(CMD_TYPE_REQUEST) ||
508 			    bcmdresp.magic == cpu_to_le32(CMD_TYPE_DATA) ||
509 			    bcmdresp.magic == cpu_to_le32(CMD_TYPE_INDICATION)) {
510 				if (!cardp->bootcmdresp)
511 					pr_info("Firmware already seems alive; resetting\n");
512 				cardp->bootcmdresp = -1;
513 			} else {
514 				pr_info("boot cmd response wrong magic number (0x%x)\n",
515 					    le32_to_cpu(bcmdresp.magic));
516 			}
517 		} else if (bcmdresp.cmd != BOOT_CMD_FW_BY_USB) {
518 			pr_info("boot cmd response cmd_tag error (%d)\n",
519 				bcmdresp.cmd);
520 		} else if (bcmdresp.result != BOOT_CMD_RESP_OK) {
521 			pr_info("boot cmd response result error (%d)\n",
522 				bcmdresp.result);
523 		} else {
524 			cardp->bootcmdresp = 1;
525 			lbtf_deb_usbd(&cardp->udev->dev,
526 				"Received valid boot command response\n");
527 		}
528 
529 		kfree_skb(skb);
530 		if_usb_submit_rx_urb_fwload(cardp);
531 		lbtf_deb_leave(LBTF_DEB_USB);
532 		return;
533 	}
534 
535 	syncfwheader = kmemdup(skb->data, sizeof(struct fwsyncheader),
536 			       GFP_ATOMIC);
537 	if (!syncfwheader) {
538 		lbtf_deb_usbd(&cardp->udev->dev,
539 			"Failure to allocate syncfwheader\n");
540 		kfree_skb(skb);
541 		lbtf_deb_leave(LBTF_DEB_USB);
542 		return;
543 	}
544 
545 	if (!syncfwheader->cmd) {
546 		lbtf_deb_usb2(&cardp->udev->dev,
547 			"FW received Blk with correct CRC\n");
548 		lbtf_deb_usb2(&cardp->udev->dev,
549 			"FW received Blk seqnum = %d\n",
550 			le32_to_cpu(syncfwheader->seqnum));
551 		cardp->CRC_OK = 1;
552 	} else {
553 		lbtf_deb_usbd(&cardp->udev->dev,
554 			"FW received Blk with CRC error\n");
555 		cardp->CRC_OK = 0;
556 	}
557 
558 	kfree_skb(skb);
559 
560 	/* reschedule timer for 200ms hence */
561 	mod_timer(&cardp->fw_timeout, jiffies + (HZ/5));
562 
563 	if (cardp->fwfinalblk) {
564 		cardp->fwdnldover = 1;
565 		goto exit;
566 	}
567 
568 	if_usb_send_fw_pkt(cardp);
569 
570  exit:
571 	if_usb_submit_rx_urb_fwload(cardp);
572 
573 	kfree(syncfwheader);
574 
575 	lbtf_deb_leave(LBTF_DEB_USB);
576 }
577 
578 #define MRVDRV_MIN_PKT_LEN	30
579 
580 static inline void process_cmdtypedata(int recvlength, struct sk_buff *skb,
581 				       struct if_usb_card *cardp,
582 				       struct lbtf_private *priv)
583 {
584 	if (recvlength > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + MESSAGE_HEADER_LEN
585 	    || recvlength < MRVDRV_MIN_PKT_LEN) {
586 		lbtf_deb_usbd(&cardp->udev->dev, "Packet length is Invalid\n");
587 		kfree_skb(skb);
588 		return;
589 	}
590 
591 	skb_put(skb, recvlength);
592 	skb_pull(skb, MESSAGE_HEADER_LEN);
593 	lbtf_rx(priv, skb);
594 }
595 
596 static inline void process_cmdrequest(int recvlength, uint8_t *recvbuff,
597 				      struct sk_buff *skb,
598 				      struct if_usb_card *cardp,
599 				      struct lbtf_private *priv)
600 {
601 	unsigned long flags;
602 
603 	if (recvlength < MESSAGE_HEADER_LEN ||
604 	    recvlength > LBS_CMD_BUFFER_SIZE) {
605 		lbtf_deb_usbd(&cardp->udev->dev,
606 			     "The receive buffer is invalid: %d\n", recvlength);
607 		kfree_skb(skb);
608 		return;
609 	}
610 
611 	spin_lock_irqsave(&priv->driver_lock, flags);
612 	memcpy(priv->cmd_resp_buff, recvbuff + MESSAGE_HEADER_LEN,
613 	       recvlength - MESSAGE_HEADER_LEN);
614 	kfree_skb(skb);
615 	lbtf_cmd_response_rx(priv);
616 	spin_unlock_irqrestore(&priv->driver_lock, flags);
617 }
618 
619 /**
620  *  if_usb_receive - read data received from the device.
621  *
622  *  @urb		pointer to struct urb
623  */
624 static void if_usb_receive(struct urb *urb)
625 {
626 	struct if_usb_card *cardp = urb->context;
627 	struct sk_buff *skb = cardp->rx_skb;
628 	struct lbtf_private *priv = cardp->priv;
629 	int recvlength = urb->actual_length;
630 	uint8_t *recvbuff = NULL;
631 	uint32_t recvtype = 0;
632 	__le32 *pkt = (__le32 *) skb->data;
633 
634 	lbtf_deb_enter(LBTF_DEB_USB);
635 
636 	if (recvlength) {
637 		if (urb->status) {
638 			lbtf_deb_usbd(&cardp->udev->dev, "RX URB failed: %d\n",
639 				     urb->status);
640 			kfree_skb(skb);
641 			goto setup_for_next;
642 		}
643 
644 		recvbuff = skb->data;
645 		recvtype = le32_to_cpu(pkt[0]);
646 		lbtf_deb_usbd(&cardp->udev->dev,
647 			    "Recv length = 0x%x, Recv type = 0x%X\n",
648 			    recvlength, recvtype);
649 	} else if (urb->status) {
650 		kfree_skb(skb);
651 		lbtf_deb_leave(LBTF_DEB_USB);
652 		return;
653 	}
654 
655 	switch (recvtype) {
656 	case CMD_TYPE_DATA:
657 		process_cmdtypedata(recvlength, skb, cardp, priv);
658 		break;
659 
660 	case CMD_TYPE_REQUEST:
661 		process_cmdrequest(recvlength, recvbuff, skb, cardp, priv);
662 		break;
663 
664 	case CMD_TYPE_INDICATION:
665 	{
666 		/* Event cause handling */
667 		u32 event_cause = le32_to_cpu(pkt[1]);
668 		lbtf_deb_usbd(&cardp->udev->dev, "**EVENT** 0x%X\n",
669 			event_cause);
670 
671 		/* Icky undocumented magic special case */
672 		if (event_cause & 0xffff0000) {
673 			u16 tmp;
674 			u8 retrycnt;
675 			u8 failure;
676 
677 			tmp = event_cause >> 16;
678 			retrycnt = tmp & 0x00ff;
679 			failure = (tmp & 0xff00) >> 8;
680 			lbtf_send_tx_feedback(priv, retrycnt, failure);
681 		} else if (event_cause == LBTF_EVENT_BCN_SENT)
682 			lbtf_bcn_sent(priv);
683 		else
684 			lbtf_deb_usbd(&cardp->udev->dev,
685 			       "Unsupported notification %d received\n",
686 			       event_cause);
687 		kfree_skb(skb);
688 		break;
689 	}
690 	default:
691 		lbtf_deb_usbd(&cardp->udev->dev,
692 			"libertastf: unknown command type 0x%X\n", recvtype);
693 		kfree_skb(skb);
694 		break;
695 	}
696 
697 setup_for_next:
698 	if_usb_submit_rx_urb(cardp);
699 	lbtf_deb_leave(LBTF_DEB_USB);
700 }
701 
702 /**
703  *  if_usb_host_to_card -  Download data to the device
704  *
705  *  @priv		pointer to struct lbtf_private structure
706  *  @type		type of data
707  *  @buf		pointer to data buffer
708  *  @len		number of bytes
709  *
710  *  Returns: 0 on success, nonzero otherwise
711  */
712 static int if_usb_host_to_card(struct lbtf_private *priv, uint8_t type,
713 			       uint8_t *payload, uint16_t nb)
714 {
715 	struct if_usb_card *cardp = priv->card;
716 	u8 data = 0;
717 
718 	lbtf_deb_usbd(&cardp->udev->dev, "*** type = %u\n", type);
719 	lbtf_deb_usbd(&cardp->udev->dev, "size after = %d\n", nb);
720 
721 	if (type == MVMS_CMD) {
722 		*(__le32 *)cardp->ep_out_buf = cpu_to_le32(CMD_TYPE_REQUEST);
723 	} else {
724 		*(__le32 *)cardp->ep_out_buf = cpu_to_le32(CMD_TYPE_DATA);
725 		data = 1;
726 	}
727 
728 	memcpy((cardp->ep_out_buf + MESSAGE_HEADER_LEN), payload, nb);
729 
730 	return usb_tx_block(cardp, cardp->ep_out_buf, nb + MESSAGE_HEADER_LEN,
731 			    data);
732 }
733 
734 /**
735  *  if_usb_issue_boot_command - Issue boot command to Boot2.
736  *
737  *  @ivalue   1 boots from FW by USB-Download, 2 boots from FW in EEPROM.
738  *
739  *  Returns: 0
740  */
741 static int if_usb_issue_boot_command(struct if_usb_card *cardp, int ivalue)
742 {
743 	struct bootcmd *bootcmd = cardp->ep_out_buf;
744 
745 	/* Prepare command */
746 	bootcmd->magic = cpu_to_le32(BOOT_CMD_MAGIC_NUMBER);
747 	bootcmd->cmd = ivalue;
748 	memset(bootcmd->pad, 0, sizeof(bootcmd->pad));
749 
750 	/* Issue command */
751 	usb_tx_block(cardp, cardp->ep_out_buf, sizeof(*bootcmd), 0);
752 
753 	return 0;
754 }
755 
756 
757 /**
758  *  check_fwfile_format - Check the validity of Boot2/FW image.
759  *
760  *  @data	pointer to image
761  *  @totlen	image length
762  *
763  *  Returns: 0 if the image is valid, nonzero otherwise.
764  */
765 static int check_fwfile_format(const u8 *data, u32 totlen)
766 {
767 	u32 bincmd, exit;
768 	u32 blksize, offset, len;
769 	int ret;
770 
771 	ret = 1;
772 	exit = len = 0;
773 
774 	do {
775 		struct fwheader *fwh = (void *) data;
776 
777 		bincmd = le32_to_cpu(fwh->dnldcmd);
778 		blksize = le32_to_cpu(fwh->datalength);
779 		switch (bincmd) {
780 		case FW_HAS_DATA_TO_RECV:
781 			offset = sizeof(struct fwheader) + blksize;
782 			data += offset;
783 			len += offset;
784 			if (len >= totlen)
785 				exit = 1;
786 			break;
787 		case FW_HAS_LAST_BLOCK:
788 			exit = 1;
789 			ret = 0;
790 			break;
791 		default:
792 			exit = 1;
793 			break;
794 		}
795 	} while (!exit);
796 
797 	if (ret)
798 		pr_err("firmware file format check FAIL\n");
799 	else
800 		lbtf_deb_fw("firmware file format check PASS\n");
801 
802 	return ret;
803 }
804 
805 
806 static int if_usb_prog_firmware(struct lbtf_private *priv)
807 {
808 	struct if_usb_card *cardp = priv->card;
809 	int i = 0;
810 	static int reset_count = 10;
811 	int ret = 0;
812 
813 	lbtf_deb_enter(LBTF_DEB_USB);
814 
815 	cardp->priv = priv;
816 
817 	kernel_param_lock(THIS_MODULE);
818 	ret = request_firmware(&cardp->fw, lbtf_fw_name, &cardp->udev->dev);
819 	if (ret < 0) {
820 		pr_err("request_firmware() failed with %#x\n", ret);
821 		pr_err("firmware %s not found\n", lbtf_fw_name);
822 		kernel_param_unlock(THIS_MODULE);
823 		goto done;
824 	}
825 	kernel_param_unlock(THIS_MODULE);
826 
827 	if (check_fwfile_format(cardp->fw->data, cardp->fw->size))
828 		goto release_fw;
829 
830 restart:
831 	if (if_usb_submit_rx_urb_fwload(cardp) < 0) {
832 		lbtf_deb_usbd(&cardp->udev->dev, "URB submission is failed\n");
833 		ret = -1;
834 		goto release_fw;
835 	}
836 
837 	cardp->bootcmdresp = 0;
838 	do {
839 		int j = 0;
840 		i++;
841 		/* Issue Boot command = 1, Boot from Download-FW */
842 		if_usb_issue_boot_command(cardp, BOOT_CMD_FW_BY_USB);
843 		/* wait for command response */
844 		do {
845 			j++;
846 			msleep_interruptible(100);
847 		} while (cardp->bootcmdresp == 0 && j < 10);
848 	} while (cardp->bootcmdresp == 0 && i < 5);
849 
850 	if (cardp->bootcmdresp <= 0) {
851 		if (--reset_count >= 0) {
852 			if_usb_reset_device(priv);
853 			goto restart;
854 		}
855 		return -1;
856 	}
857 
858 	i = 0;
859 
860 	cardp->totalbytes = 0;
861 	cardp->fwlastblksent = 0;
862 	cardp->CRC_OK = 1;
863 	cardp->fwdnldover = 0;
864 	cardp->fwseqnum = -1;
865 	cardp->totalbytes = 0;
866 	cardp->fwfinalblk = 0;
867 
868 	/* Send the first firmware packet... */
869 	if_usb_send_fw_pkt(cardp);
870 
871 	/* ... and wait for the process to complete */
872 	wait_event_interruptible(cardp->fw_wq, cardp->priv->surpriseremoved ||
873 					       cardp->fwdnldover);
874 
875 	del_timer_sync(&cardp->fw_timeout);
876 	usb_kill_urb(cardp->rx_urb);
877 
878 	if (!cardp->fwdnldover) {
879 		pr_info("failed to load fw, resetting device!\n");
880 		if (--reset_count >= 0) {
881 			if_usb_reset_device(priv);
882 			goto restart;
883 		}
884 
885 		pr_info("FW download failure, time = %d ms\n", i * 100);
886 		ret = -1;
887 		goto release_fw;
888 	}
889 
890  release_fw:
891 	release_firmware(cardp->fw);
892 	cardp->fw = NULL;
893 
894 	if_usb_setup_firmware(cardp->priv);
895 
896  done:
897 	lbtf_deb_leave_args(LBTF_DEB_USB, "ret %d", ret);
898 	return ret;
899 }
900 
901 
902 #define if_usb_suspend NULL
903 #define if_usb_resume NULL
904 
905 static struct usb_driver if_usb_driver = {
906 	.name = DRV_NAME,
907 	.probe = if_usb_probe,
908 	.disconnect = if_usb_disconnect,
909 	.id_table = if_usb_table,
910 	.suspend = if_usb_suspend,
911 	.resume = if_usb_resume,
912 	.disable_hub_initiated_lpm = 1,
913 };
914 
915 module_usb_driver(if_usb_driver);
916 
917 MODULE_DESCRIPTION("8388 USB WLAN Thinfirm Driver");
918 MODULE_AUTHOR("Cozybit Inc.");
919 MODULE_LICENSE("GPL");
920