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