if_usb.c revision 69f9032d9dfeb763b467fdbe8cf5938f5457083a
1/**
2  * This file contains functions used in USB interface module.
3  */
4#include <linux/delay.h>
5#include <linux/moduleparam.h>
6#include <linux/firmware.h>
7#include <linux/netdevice.h>
8#include <linux/usb.h>
9
10#define DRV_NAME "usb8xxx"
11
12#include "host.h"
13#include "decl.h"
14#include "defs.h"
15#include "dev.h"
16#include "if_usb.h"
17
18#define MESSAGE_HEADER_LEN	4
19
20static char *lbs_fw_name = "usb8388.bin";
21module_param_named(fw_name, lbs_fw_name, charp, 0644);
22
23static struct usb_device_id if_usb_table[] = {
24	/* Enter the device signature inside */
25	{ USB_DEVICE(0x1286, 0x2001) },
26	{ USB_DEVICE(0x05a3, 0x8388) },
27	{}	/* Terminating entry */
28};
29
30MODULE_DEVICE_TABLE(usb, if_usb_table);
31
32static void if_usb_receive(struct urb *urb);
33static void if_usb_receive_fwload(struct urb *urb);
34static int if_usb_prog_firmware(struct usb_card_rec *cardp);
35static int if_usb_host_to_card(struct lbs_private *priv,
36	u8 type,
37	u8 *payload,
38	u16 nb);
39static int if_usb_get_int_status(struct lbs_private *priv, u8 *);
40static int if_usb_read_event_cause(struct lbs_private *);
41static int usb_tx_block(struct usb_card_rec *cardp, u8 *payload, u16 nb);
42static void if_usb_free(struct usb_card_rec *cardp);
43static int if_usb_submit_rx_urb(struct usb_card_rec *cardp);
44static int if_usb_reset_device(struct usb_card_rec *cardp);
45
46/**
47 *  @brief  call back function to handle the status of the URB
48 *  @param urb 		pointer to urb structure
49 *  @return 	   	N/A
50 */
51static void if_usb_write_bulk_callback(struct urb *urb)
52{
53	struct usb_card_rec *cardp = (struct usb_card_rec *) urb->context;
54
55	/* handle the transmission complete validations */
56
57	if (urb->status == 0) {
58		struct lbs_private *priv = cardp->priv;
59
60		/*
61		lbs_deb_usbd(&urb->dev->dev, "URB status is successfull\n");
62		lbs_deb_usbd(&urb->dev->dev, "Actual length transmitted %d\n",
63		       urb->actual_length);
64		*/
65
66		/* Used for both firmware TX and regular TX.  priv isn't
67		 * valid at firmware load time.
68		 */
69		if (priv) {
70			struct lbs_adapter *adapter = priv->adapter;
71			struct net_device *dev = priv->dev;
72
73			priv->dnld_sent = DNLD_RES_RECEIVED;
74
75			/* Wake main thread if commands are pending */
76			if (!adapter->cur_cmd)
77				wake_up_interruptible(&priv->waitq);
78
79			if (adapter->connect_status == LBS_CONNECTED)
80				netif_wake_queue(dev);
81
82			if (priv->mesh_dev && (adapter->mesh_connect_status == LBS_CONNECTED))
83				netif_wake_queue(priv->mesh_dev);
84		}
85	} else {
86		/* print the failure status number for debug */
87		lbs_pr_info("URB in failure status: %d\n", urb->status);
88	}
89
90	return;
91}
92
93/**
94 *  @brief  free tx/rx urb, skb and rx buffer
95 *  @param cardp	pointer usb_card_rec
96 *  @return 	   	N/A
97 */
98static void if_usb_free(struct usb_card_rec *cardp)
99{
100	lbs_deb_enter(LBS_DEB_USB);
101
102	/* Unlink tx & rx urb */
103	usb_kill_urb(cardp->tx_urb);
104	usb_kill_urb(cardp->rx_urb);
105
106	usb_free_urb(cardp->tx_urb);
107	cardp->tx_urb = NULL;
108
109	usb_free_urb(cardp->rx_urb);
110	cardp->rx_urb = NULL;
111
112	kfree(cardp->bulk_out_buffer);
113	cardp->bulk_out_buffer = NULL;
114
115	lbs_deb_leave(LBS_DEB_USB);
116}
117
118/**
119 *  @brief sets the configuration values
120 *  @param ifnum	interface number
121 *  @param id		pointer to usb_device_id
122 *  @return 	   	0 on success, error code on failure
123 */
124static int if_usb_probe(struct usb_interface *intf,
125			const struct usb_device_id *id)
126{
127	struct usb_device *udev;
128	struct usb_host_interface *iface_desc;
129	struct usb_endpoint_descriptor *endpoint;
130	struct lbs_private *priv;
131	struct usb_card_rec *cardp;
132	int i;
133
134	udev = interface_to_usbdev(intf);
135
136	cardp = kzalloc(sizeof(struct usb_card_rec), GFP_KERNEL);
137	if (!cardp) {
138		lbs_pr_err("Out of memory allocating private data.\n");
139		goto error;
140	}
141
142	cardp->udev = udev;
143	iface_desc = intf->cur_altsetting;
144
145	lbs_deb_usbd(&udev->dev, "bcdUSB = 0x%X bDeviceClass = 0x%X"
146	       " bDeviceSubClass = 0x%X, bDeviceProtocol = 0x%X\n",
147		     le16_to_cpu(udev->descriptor.bcdUSB),
148		     udev->descriptor.bDeviceClass,
149		     udev->descriptor.bDeviceSubClass,
150		     udev->descriptor.bDeviceProtocol);
151
152	for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
153		endpoint = &iface_desc->endpoint[i].desc;
154		if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
155		    && ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
156			USB_ENDPOINT_XFER_BULK)) {
157			/* we found a bulk in endpoint */
158			lbs_deb_usbd(&udev->dev, "Bulk in size is %d\n",
159				     le16_to_cpu(endpoint->wMaxPacketSize));
160			if (!(cardp->rx_urb = usb_alloc_urb(0, GFP_KERNEL))) {
161				lbs_deb_usbd(&udev->dev,
162				       "Rx URB allocation failed\n");
163				goto dealloc;
164			}
165			cardp->rx_urb_recall = 0;
166
167			cardp->bulk_in_size =
168				le16_to_cpu(endpoint->wMaxPacketSize);
169			cardp->bulk_in_endpointAddr =
170			    (endpoint->
171			     bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
172			lbs_deb_usbd(&udev->dev, "in_endpoint = %d\n",
173			       endpoint->bEndpointAddress);
174		}
175
176		if (((endpoint->
177		      bEndpointAddress & USB_ENDPOINT_DIR_MASK) ==
178		     USB_DIR_OUT)
179		    && ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
180			USB_ENDPOINT_XFER_BULK)) {
181			/* We found bulk out endpoint */
182			if (!(cardp->tx_urb = usb_alloc_urb(0, GFP_KERNEL))) {
183				lbs_deb_usbd(&udev->dev,
184				       "Tx URB allocation failed\n");
185				goto dealloc;
186			}
187
188			cardp->bulk_out_size =
189				le16_to_cpu(endpoint->wMaxPacketSize);
190			lbs_deb_usbd(&udev->dev,
191				     "Bulk out size is %d\n",
192				     le16_to_cpu(endpoint->wMaxPacketSize));
193			cardp->bulk_out_endpointAddr =
194			    endpoint->bEndpointAddress;
195			lbs_deb_usbd(&udev->dev, "out_endpoint = %d\n",
196				    endpoint->bEndpointAddress);
197			cardp->bulk_out_buffer =
198			    kmalloc(MRVDRV_ETH_TX_PACKET_BUFFER_SIZE,
199				    GFP_KERNEL);
200
201			if (!cardp->bulk_out_buffer) {
202				lbs_deb_usbd(&udev->dev,
203				       "Could not allocate buffer\n");
204				goto dealloc;
205			}
206		}
207	}
208
209	/* Upload firmware */
210	cardp->rinfo.cardp = cardp;
211	if (if_usb_prog_firmware(cardp)) {
212		lbs_deb_usbd(&udev->dev, "FW upload failed");
213		goto err_prog_firmware;
214	}
215
216	if (!(priv = lbs_add_card(cardp, &udev->dev)))
217		goto err_prog_firmware;
218
219	cardp->priv = priv;
220
221	if (lbs_add_mesh(priv, &udev->dev))
222		goto err_add_mesh;
223
224	cardp->eth_dev = priv->dev;
225
226	priv->hw_host_to_card = if_usb_host_to_card;
227	priv->hw_get_int_status = if_usb_get_int_status;
228	priv->hw_read_event_cause = if_usb_read_event_cause;
229	priv->boot2_version = udev->descriptor.bcdDevice;
230
231	/* Delay 200 ms to waiting for the FW ready */
232	if_usb_submit_rx_urb(cardp);
233	msleep_interruptible(200);
234	priv->adapter->fw_ready = 1;
235
236	if (lbs_start_card(priv))
237		goto err_start_card;
238
239	usb_get_dev(udev);
240	usb_set_intfdata(intf, cardp);
241
242	return 0;
243
244err_start_card:
245	lbs_remove_mesh(priv);
246err_add_mesh:
247	lbs_remove_card(priv);
248err_prog_firmware:
249	if_usb_reset_device(cardp);
250dealloc:
251	if_usb_free(cardp);
252
253error:
254	return -ENOMEM;
255}
256
257/**
258 *  @brief free resource and cleanup
259 *  @param intf		USB interface structure
260 *  @return 	   	N/A
261 */
262static void if_usb_disconnect(struct usb_interface *intf)
263{
264	struct usb_card_rec *cardp = usb_get_intfdata(intf);
265	struct lbs_private *priv = (struct lbs_private *) cardp->priv;
266
267	lbs_deb_enter(LBS_DEB_MAIN);
268
269	/* Update Surprise removed to TRUE */
270	cardp->surprise_removed = 1;
271
272	if (priv) {
273		struct lbs_adapter *adapter = priv->adapter;
274
275		adapter->surpriseremoved = 1;
276		lbs_stop_card(priv);
277		lbs_remove_mesh(priv);
278		lbs_remove_card(priv);
279	}
280
281	/* this is (apparently?) necessary for future usage of the device */
282	lbs_prepare_and_send_command(priv, CMD_802_11_RESET, CMD_ACT_HALT,
283			0, 0, NULL);
284
285	/* Unlink and free urb */
286	if_usb_free(cardp);
287
288	usb_set_intfdata(intf, NULL);
289	usb_put_dev(interface_to_usbdev(intf));
290
291	lbs_deb_leave(LBS_DEB_MAIN);
292}
293
294/**
295 *  @brief  This function download FW
296 *  @param priv		pointer to struct lbs_private
297 *  @return 	   	0
298 */
299static int if_prog_firmware(struct usb_card_rec *cardp)
300{
301	struct FWData *fwdata;
302	struct fwheader *fwheader;
303	u8 *firmware = cardp->fw->data;
304
305	fwdata = kmalloc(sizeof(struct FWData), GFP_ATOMIC);
306
307	if (!fwdata)
308		return -1;
309
310	fwheader = &fwdata->fwheader;
311
312	if (!cardp->CRC_OK) {
313		cardp->totalbytes = cardp->fwlastblksent;
314		cardp->fwseqnum = cardp->lastseqnum - 1;
315	}
316
317	/*
318	lbs_deb_usbd(&cardp->udev->dev, "totalbytes = %d\n",
319		    cardp->totalbytes);
320	*/
321
322	memcpy(fwheader, &firmware[cardp->totalbytes],
323	       sizeof(struct fwheader));
324
325	cardp->fwlastblksent = cardp->totalbytes;
326	cardp->totalbytes += sizeof(struct fwheader);
327
328	/* lbs_deb_usbd(&cardp->udev->dev,"Copy Data\n"); */
329	memcpy(fwdata->data, &firmware[cardp->totalbytes],
330	       le32_to_cpu(fwdata->fwheader.datalength));
331
332	/*
333	lbs_deb_usbd(&cardp->udev->dev,
334		    "Data length = %d\n", le32_to_cpu(fwdata->fwheader.datalength));
335	*/
336
337	cardp->fwseqnum = cardp->fwseqnum + 1;
338
339	fwdata->seqnum = cpu_to_le32(cardp->fwseqnum);
340	cardp->lastseqnum = cardp->fwseqnum;
341	cardp->totalbytes += le32_to_cpu(fwdata->fwheader.datalength);
342
343	if (fwheader->dnldcmd == cpu_to_le32(FW_HAS_DATA_TO_RECV)) {
344		/*
345		lbs_deb_usbd(&cardp->udev->dev, "There are data to follow\n");
346		lbs_deb_usbd(&cardp->udev->dev,
347			    "seqnum = %d totalbytes = %d\n", cardp->fwseqnum,
348			    cardp->totalbytes);
349		*/
350		memcpy(cardp->bulk_out_buffer, fwheader, FW_DATA_XMIT_SIZE);
351		usb_tx_block(cardp, cardp->bulk_out_buffer, FW_DATA_XMIT_SIZE);
352
353	} else if (fwdata->fwheader.dnldcmd == cpu_to_le32(FW_HAS_LAST_BLOCK)) {
354		/*
355		lbs_deb_usbd(&cardp->udev->dev,
356			    "Host has finished FW downloading\n");
357		lbs_deb_usbd(&cardp->udev->dev,
358			    "Donwloading FW JUMP BLOCK\n");
359		*/
360		memcpy(cardp->bulk_out_buffer, fwheader, FW_DATA_XMIT_SIZE);
361		usb_tx_block(cardp, cardp->bulk_out_buffer, FW_DATA_XMIT_SIZE);
362		cardp->fwfinalblk = 1;
363	}
364
365	/*
366	lbs_deb_usbd(&cardp->udev->dev,
367		    "The firmware download is done size is %d\n",
368		    cardp->totalbytes);
369	*/
370
371	kfree(fwdata);
372
373	return 0;
374}
375
376static int if_usb_reset_device(struct usb_card_rec *cardp)
377{
378	int ret;
379	struct lbs_private *priv = cardp->priv;
380
381	lbs_deb_enter(LBS_DEB_USB);
382
383	/* Try a USB port reset first, if that fails send the reset
384	 * command to the firmware.
385	 */
386	ret = usb_reset_device(cardp->udev);
387	if (!ret && priv) {
388		msleep(10);
389		ret = lbs_reset_device(priv);
390		msleep(10);
391	}
392
393	lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
394
395	return ret;
396}
397
398/**
399 *  @brief This function transfer the data to the device.
400 *  @param priv 	pointer to struct lbs_private
401 *  @param payload	pointer to payload data
402 *  @param nb		data length
403 *  @return 	   	0 or -1
404 */
405static int usb_tx_block(struct usb_card_rec *cardp, u8 * payload, u16 nb)
406{
407	int ret = -1;
408
409	/* check if device is removed */
410	if (cardp->surprise_removed) {
411		lbs_deb_usbd(&cardp->udev->dev, "Device removed\n");
412		goto tx_ret;
413	}
414
415	usb_fill_bulk_urb(cardp->tx_urb, cardp->udev,
416			  usb_sndbulkpipe(cardp->udev,
417					  cardp->bulk_out_endpointAddr),
418			  payload, nb, if_usb_write_bulk_callback, cardp);
419
420	cardp->tx_urb->transfer_flags |= URB_ZERO_PACKET;
421
422	if ((ret = usb_submit_urb(cardp->tx_urb, GFP_ATOMIC))) {
423		/*  transfer failed */
424		lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb failed\n");
425		ret = -1;
426	} else {
427		/* lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb success\n"); */
428		ret = 0;
429	}
430
431tx_ret:
432	return ret;
433}
434
435static int __if_usb_submit_rx_urb(struct usb_card_rec *cardp,
436				  void (*callbackfn)(struct urb *urb))
437{
438	struct sk_buff *skb;
439	struct read_cb_info *rinfo = &cardp->rinfo;
440	int ret = -1;
441
442	if (!(skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE))) {
443		lbs_pr_err("No free skb\n");
444		goto rx_ret;
445	}
446
447	rinfo->skb = skb;
448
449	/* Fill the receive configuration URB and initialise the Rx call back */
450	usb_fill_bulk_urb(cardp->rx_urb, cardp->udev,
451			  usb_rcvbulkpipe(cardp->udev,
452					  cardp->bulk_in_endpointAddr),
453			  (void *) (skb->tail + (size_t) IPFIELD_ALIGN_OFFSET),
454			  MRVDRV_ETH_RX_PACKET_BUFFER_SIZE, callbackfn,
455			  rinfo);
456
457	cardp->rx_urb->transfer_flags |= URB_ZERO_PACKET;
458
459	/* lbs_deb_usbd(&cardp->udev->dev, "Pointer for rx_urb %p\n", cardp->rx_urb); */
460	if ((ret = usb_submit_urb(cardp->rx_urb, GFP_ATOMIC))) {
461		/* handle failure conditions */
462		lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB failed\n");
463		ret = -1;
464	} else {
465		/* lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB success\n"); */
466		ret = 0;
467	}
468
469rx_ret:
470	return ret;
471}
472
473static int if_usb_submit_rx_urb_fwload(struct usb_card_rec *cardp)
474{
475	return __if_usb_submit_rx_urb(cardp, &if_usb_receive_fwload);
476}
477
478static int if_usb_submit_rx_urb(struct usb_card_rec *cardp)
479{
480	return __if_usb_submit_rx_urb(cardp, &if_usb_receive);
481}
482
483static void if_usb_receive_fwload(struct urb *urb)
484{
485	struct read_cb_info *rinfo = (struct read_cb_info *)urb->context;
486	struct sk_buff *skb = rinfo->skb;
487	struct usb_card_rec *cardp = (struct usb_card_rec *)rinfo->cardp;
488	struct fwsyncheader *syncfwheader;
489	struct bootcmdrespStr bootcmdresp;
490
491	if (urb->status) {
492		lbs_deb_usbd(&cardp->udev->dev,
493			    "URB status is failed during fw load\n");
494		kfree_skb(skb);
495		return;
496	}
497
498	if (cardp->bootcmdresp == 0) {
499		memcpy (&bootcmdresp, skb->data + IPFIELD_ALIGN_OFFSET,
500			sizeof(bootcmdresp));
501		if (le16_to_cpu(cardp->udev->descriptor.bcdDevice) < 0x3106) {
502			kfree_skb(skb);
503			if_usb_submit_rx_urb_fwload(cardp);
504			cardp->bootcmdresp = 1;
505			lbs_deb_usbd(&cardp->udev->dev,
506				    "Received valid boot command response\n");
507			return;
508		}
509		if (bootcmdresp.u32magicnumber != cpu_to_le32(BOOT_CMD_MAGIC_NUMBER)) {
510			lbs_pr_info(
511				"boot cmd response wrong magic number (0x%x)\n",
512				le32_to_cpu(bootcmdresp.u32magicnumber));
513		} else if (bootcmdresp.u8cmd_tag != BOOT_CMD_FW_BY_USB) {
514			lbs_pr_info(
515				"boot cmd response cmd_tag error (%d)\n",
516				bootcmdresp.u8cmd_tag);
517		} else if (bootcmdresp.u8result != BOOT_CMD_RESP_OK) {
518			lbs_pr_info(
519				"boot cmd response result error (%d)\n",
520				bootcmdresp.u8result);
521		} else {
522			cardp->bootcmdresp = 1;
523			lbs_deb_usbd(&cardp->udev->dev,
524				    "Received valid boot command response\n");
525		}
526		kfree_skb(skb);
527		if_usb_submit_rx_urb_fwload(cardp);
528		return;
529	}
530
531	syncfwheader = kmalloc(sizeof(struct fwsyncheader), GFP_ATOMIC);
532	if (!syncfwheader) {
533		lbs_deb_usbd(&cardp->udev->dev, "Failure to allocate syncfwheader\n");
534		kfree_skb(skb);
535		return;
536	}
537
538	memcpy(syncfwheader, skb->data + IPFIELD_ALIGN_OFFSET,
539			sizeof(struct fwsyncheader));
540
541	if (!syncfwheader->cmd) {
542		/*
543		lbs_deb_usbd(&cardp->udev->dev,
544			    "FW received Blk with correct CRC\n");
545		lbs_deb_usbd(&cardp->udev->dev,
546			    "FW received Blk seqnum = %d\n",
547		       syncfwheader->seqnum);
548		*/
549		cardp->CRC_OK = 1;
550	} else {
551		lbs_deb_usbd(&cardp->udev->dev,
552			    "FW received Blk with CRC error\n");
553		cardp->CRC_OK = 0;
554	}
555
556	kfree_skb(skb);
557
558	if (cardp->fwfinalblk) {
559		cardp->fwdnldover = 1;
560		goto exit;
561	}
562
563	if_prog_firmware(cardp);
564
565	if_usb_submit_rx_urb_fwload(cardp);
566exit:
567	kfree(syncfwheader);
568
569	return;
570
571}
572
573#define MRVDRV_MIN_PKT_LEN	30
574
575static inline void process_cmdtypedata(int recvlength, struct sk_buff *skb,
576				       struct usb_card_rec *cardp,
577				       struct lbs_private *priv)
578{
579	if (recvlength > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE +
580	    MESSAGE_HEADER_LEN || recvlength < MRVDRV_MIN_PKT_LEN) {
581		lbs_deb_usbd(&cardp->udev->dev,
582			    "Packet length is Invalid\n");
583		kfree_skb(skb);
584		return;
585	}
586
587	skb_reserve(skb, IPFIELD_ALIGN_OFFSET);
588	skb_put(skb, recvlength);
589	skb_pull(skb, MESSAGE_HEADER_LEN);
590	lbs_process_rxed_packet(priv, skb);
591	priv->upld_len = (recvlength - MESSAGE_HEADER_LEN);
592}
593
594static inline void process_cmdrequest(int recvlength, u8 *recvbuff,
595				      struct sk_buff *skb,
596				      struct usb_card_rec *cardp,
597				      struct lbs_private *priv)
598{
599	u8 *cmdbuf;
600	if (recvlength > MRVDRV_SIZE_OF_CMD_BUFFER) {
601		lbs_deb_usbd(&cardp->udev->dev,
602			    "The receive buffer is too large\n");
603		kfree_skb(skb);
604		return;
605	}
606
607	if (!in_interrupt())
608		BUG();
609
610	spin_lock(&priv->adapter->driver_lock);
611	/* take care of cur_cmd = NULL case by reading the
612	 * data to clear the interrupt */
613	if (!priv->adapter->cur_cmd) {
614		cmdbuf = priv->upld_buf;
615		priv->adapter->hisregcpy &= ~MRVDRV_CMD_UPLD_RDY;
616	} else
617		cmdbuf = priv->adapter->cur_cmd->bufvirtualaddr;
618
619	cardp->usb_int_cause |= MRVDRV_CMD_UPLD_RDY;
620	priv->upld_len = (recvlength - MESSAGE_HEADER_LEN);
621	memcpy(cmdbuf, recvbuff + MESSAGE_HEADER_LEN,
622	       priv->upld_len);
623
624	kfree_skb(skb);
625	lbs_interrupt(priv->dev);
626	spin_unlock(&priv->adapter->driver_lock);
627
628	lbs_deb_usbd(&cardp->udev->dev,
629		    "Wake up main thread to handle cmd response\n");
630
631	return;
632}
633
634/**
635 *  @brief This function reads of the packet into the upload buff,
636 *  wake up the main thread and initialise the Rx callack.
637 *
638 *  @param urb		pointer to struct urb
639 *  @return 	   	N/A
640 */
641static void if_usb_receive(struct urb *urb)
642{
643	struct read_cb_info *rinfo = (struct read_cb_info *)urb->context;
644	struct sk_buff *skb = rinfo->skb;
645	struct usb_card_rec *cardp = (struct usb_card_rec *) rinfo->cardp;
646	struct lbs_private *priv = cardp->priv;
647
648	int recvlength = urb->actual_length;
649	u8 *recvbuff = NULL;
650	u32 recvtype = 0;
651
652	lbs_deb_enter(LBS_DEB_USB);
653
654	if (recvlength) {
655		__le32 tmp;
656
657		if (urb->status) {
658			lbs_deb_usbd(&cardp->udev->dev,
659				    "URB status is failed\n");
660			kfree_skb(skb);
661			goto setup_for_next;
662		}
663
664		recvbuff = skb->data + IPFIELD_ALIGN_OFFSET;
665		memcpy(&tmp, recvbuff, sizeof(u32));
666		recvtype = le32_to_cpu(tmp);
667		lbs_deb_usbd(&cardp->udev->dev,
668			    "Recv length = 0x%x, Recv type = 0x%X\n",
669			    recvlength, recvtype);
670	} else if (urb->status)
671		goto rx_exit;
672
673	switch (recvtype) {
674	case CMD_TYPE_DATA:
675		process_cmdtypedata(recvlength, skb, cardp, priv);
676		break;
677
678	case CMD_TYPE_REQUEST:
679		process_cmdrequest(recvlength, recvbuff, skb, cardp, priv);
680		break;
681
682	case CMD_TYPE_INDICATION:
683		/* Event cause handling */
684		spin_lock(&priv->adapter->driver_lock);
685		cardp->usb_event_cause = le32_to_cpu(*(__le32 *) (recvbuff + MESSAGE_HEADER_LEN));
686		lbs_deb_usbd(&cardp->udev->dev,"**EVENT** 0x%X\n",
687			    cardp->usb_event_cause);
688		if (cardp->usb_event_cause & 0xffff0000) {
689			lbs_send_tx_feedback(priv);
690			spin_unlock(&priv->adapter->driver_lock);
691			break;
692		}
693		cardp->usb_event_cause <<= 3;
694		cardp->usb_int_cause |= MRVDRV_CARDEVENT;
695		kfree_skb(skb);
696		lbs_interrupt(priv->dev);
697		spin_unlock(&priv->adapter->driver_lock);
698		goto rx_exit;
699	default:
700		lbs_deb_usbd(&cardp->udev->dev, "Unknown command type 0x%X\n",
701		             recvtype);
702		kfree_skb(skb);
703		break;
704	}
705
706setup_for_next:
707	if_usb_submit_rx_urb(cardp);
708rx_exit:
709	lbs_deb_leave(LBS_DEB_USB);
710}
711
712/**
713 *  @brief This function downloads data to FW
714 *  @param priv		pointer to struct lbs_private structure
715 *  @param type		type of data
716 *  @param buf		pointer to data buffer
717 *  @param len		number of bytes
718 *  @return 	   	0 or -1
719 */
720static int if_usb_host_to_card(struct lbs_private *priv,
721	u8 type,
722	u8 *payload,
723	u16 nb)
724{
725	struct usb_card_rec *cardp = (struct usb_card_rec *)priv->card;
726
727	lbs_deb_usbd(&cardp->udev->dev,"*** type = %u\n", type);
728	lbs_deb_usbd(&cardp->udev->dev,"size after = %d\n", nb);
729
730	if (type == MVMS_CMD) {
731		__le32 tmp = cpu_to_le32(CMD_TYPE_REQUEST);
732		priv->dnld_sent = DNLD_CMD_SENT;
733		memcpy(cardp->bulk_out_buffer, (u8 *) & tmp,
734		       MESSAGE_HEADER_LEN);
735
736	} else {
737		__le32 tmp = cpu_to_le32(CMD_TYPE_DATA);
738		priv->dnld_sent = DNLD_DATA_SENT;
739		memcpy(cardp->bulk_out_buffer, (u8 *) & tmp,
740		       MESSAGE_HEADER_LEN);
741	}
742
743	memcpy((cardp->bulk_out_buffer + MESSAGE_HEADER_LEN), payload, nb);
744
745	return usb_tx_block(cardp, cardp->bulk_out_buffer,
746	                    nb + MESSAGE_HEADER_LEN);
747}
748
749/* called with adapter->driver_lock held */
750static int if_usb_get_int_status(struct lbs_private *priv, u8 *ireg)
751{
752	struct usb_card_rec *cardp = priv->card;
753
754	*ireg = cardp->usb_int_cause;
755	cardp->usb_int_cause = 0;
756
757	lbs_deb_usbd(&cardp->udev->dev,"Int cause is 0x%X\n", *ireg);
758
759	return 0;
760}
761
762static int if_usb_read_event_cause(struct lbs_private *priv)
763{
764	struct usb_card_rec *cardp = priv->card;
765
766	priv->adapter->eventcause = cardp->usb_event_cause;
767	/* Re-submit rx urb here to avoid event lost issue */
768	if_usb_submit_rx_urb(cardp);
769	return 0;
770}
771
772/**
773 *  @brief This function issues Boot command to the Boot2 code
774 *  @param ivalue   1:Boot from FW by USB-Download
775 *                  2:Boot from FW in EEPROM
776 *  @return 	   	0
777 */
778static int if_usb_issue_boot_command(struct usb_card_rec *cardp, int ivalue)
779{
780	struct bootcmdstr sbootcmd;
781	int i;
782
783	/* Prepare command */
784	sbootcmd.u32magicnumber = cpu_to_le32(BOOT_CMD_MAGIC_NUMBER);
785	sbootcmd.u8cmd_tag = ivalue;
786	for (i=0; i<11; i++)
787		sbootcmd.au8dumy[i]=0x00;
788	memcpy(cardp->bulk_out_buffer, &sbootcmd, sizeof(struct bootcmdstr));
789
790	/* Issue command */
791	usb_tx_block(cardp, cardp->bulk_out_buffer, sizeof(struct bootcmdstr));
792
793	return 0;
794}
795
796
797/**
798 *  @brief This function checks the validity of Boot2/FW image.
799 *
800 *  @param data              pointer to image
801 *         len               image length
802 *  @return     0 or -1
803 */
804static int check_fwfile_format(u8 *data, u32 totlen)
805{
806	u32 bincmd, exit;
807	u32 blksize, offset, len;
808	int ret;
809
810	ret = 1;
811	exit = len = 0;
812
813	do {
814		struct fwheader *fwh = (void *)data;
815
816		bincmd = le32_to_cpu(fwh->dnldcmd);
817		blksize = le32_to_cpu(fwh->datalength);
818		switch (bincmd) {
819		case FW_HAS_DATA_TO_RECV:
820			offset = sizeof(struct fwheader) + blksize;
821			data += offset;
822			len += offset;
823			if (len >= totlen)
824				exit = 1;
825			break;
826		case FW_HAS_LAST_BLOCK:
827			exit = 1;
828			ret = 0;
829			break;
830		default:
831			exit = 1;
832			break;
833		}
834	} while (!exit);
835
836	if (ret)
837		lbs_pr_err("firmware file format check FAIL\n");
838	else
839		lbs_deb_fw("firmware file format check PASS\n");
840
841	return ret;
842}
843
844
845static int if_usb_prog_firmware(struct usb_card_rec *cardp)
846{
847	int i = 0;
848	static int reset_count = 10;
849	int ret = 0;
850
851	lbs_deb_enter(LBS_DEB_USB);
852
853	if ((ret = request_firmware(&cardp->fw, lbs_fw_name,
854				    &cardp->udev->dev)) < 0) {
855		lbs_pr_err("request_firmware() failed with %#x\n", ret);
856		lbs_pr_err("firmware %s not found\n", lbs_fw_name);
857		goto done;
858	}
859
860	if (check_fwfile_format(cardp->fw->data, cardp->fw->size))
861		goto release_fw;
862
863restart:
864	if (if_usb_submit_rx_urb_fwload(cardp) < 0) {
865		lbs_deb_usbd(&cardp->udev->dev, "URB submission is failed\n");
866		ret = -1;
867		goto release_fw;
868	}
869
870	cardp->bootcmdresp = 0;
871	do {
872		int j = 0;
873		i++;
874		/* Issue Boot command = 1, Boot from Download-FW */
875		if_usb_issue_boot_command(cardp, BOOT_CMD_FW_BY_USB);
876		/* wait for command response */
877		do {
878			j++;
879			msleep_interruptible(100);
880		} while (cardp->bootcmdresp == 0 && j < 10);
881	} while (cardp->bootcmdresp == 0 && i < 5);
882
883	if (cardp->bootcmdresp == 0) {
884		if (--reset_count >= 0) {
885			if_usb_reset_device(cardp);
886			goto restart;
887		}
888		return -1;
889	}
890
891	i = 0;
892
893	cardp->totalbytes = 0;
894	cardp->fwlastblksent = 0;
895	cardp->CRC_OK = 1;
896	cardp->fwdnldover = 0;
897	cardp->fwseqnum = -1;
898	cardp->totalbytes = 0;
899	cardp->fwfinalblk = 0;
900
901	if_prog_firmware(cardp);
902
903	do {
904		lbs_deb_usbd(&cardp->udev->dev,"Wlan sched timeout\n");
905		i++;
906		msleep_interruptible(100);
907		if (cardp->surprise_removed || i >= 20)
908			break;
909	} while (!cardp->fwdnldover);
910
911	if (!cardp->fwdnldover) {
912		lbs_pr_info("failed to load fw, resetting device!\n");
913		if (--reset_count >= 0) {
914			if_usb_reset_device(cardp);
915			goto restart;
916		}
917
918		lbs_pr_info("FW download failure, time = %d ms\n", i * 100);
919		ret = -1;
920		goto release_fw;
921	}
922
923release_fw:
924	release_firmware(cardp->fw);
925	cardp->fw = NULL;
926
927done:
928	lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
929	return ret;
930}
931
932
933#ifdef CONFIG_PM
934static int if_usb_suspend(struct usb_interface *intf, pm_message_t message)
935{
936	struct usb_card_rec *cardp = usb_get_intfdata(intf);
937	struct lbs_private *priv = cardp->priv;
938
939	lbs_deb_enter(LBS_DEB_USB);
940
941	if (priv->adapter->psstate != PS_STATE_FULL_POWER)
942		return -1;
943
944	if (priv->mesh_dev && !priv->mesh_autostart_enabled) {
945		/* Mesh autostart must be activated while sleeping
946		 * On resume it will go back to the current state
947		 */
948		struct cmd_ds_mesh_access mesh_access;
949		memset(&mesh_access, 0, sizeof(mesh_access));
950		mesh_access.data[0] = cpu_to_le32(1);
951		lbs_prepare_and_send_command(priv,
952				CMD_MESH_ACCESS,
953				CMD_ACT_MESH_SET_AUTOSTART_ENABLED,
954				CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
955	}
956
957	netif_device_detach(cardp->eth_dev);
958	netif_device_detach(priv->mesh_dev);
959
960	/* Unlink tx & rx urb */
961	usb_kill_urb(cardp->tx_urb);
962	usb_kill_urb(cardp->rx_urb);
963
964	cardp->rx_urb_recall = 1;
965
966	lbs_deb_leave(LBS_DEB_USB);
967	return 0;
968}
969
970static int if_usb_resume(struct usb_interface *intf)
971{
972	struct usb_card_rec *cardp = usb_get_intfdata(intf);
973	struct lbs_private *priv = cardp->priv;
974
975	lbs_deb_enter(LBS_DEB_USB);
976
977	cardp->rx_urb_recall = 0;
978
979	if_usb_submit_rx_urb(cardp->priv);
980
981	netif_device_attach(cardp->eth_dev);
982	netif_device_attach(priv->mesh_dev);
983
984	if (priv->mesh_dev && !priv->mesh_autostart_enabled) {
985		/* Mesh autostart was activated while sleeping
986		 * Disable it if appropriate
987		 */
988		struct cmd_ds_mesh_access mesh_access;
989		memset(&mesh_access, 0, sizeof(mesh_access));
990		mesh_access.data[0] = cpu_to_le32(0);
991		lbs_prepare_and_send_command(priv,
992				CMD_MESH_ACCESS,
993				CMD_ACT_MESH_SET_AUTOSTART_ENABLED,
994				CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
995	}
996
997	lbs_deb_leave(LBS_DEB_USB);
998	return 0;
999}
1000#else
1001#define if_usb_suspend NULL
1002#define if_usb_resume NULL
1003#endif
1004
1005static struct usb_driver if_usb_driver = {
1006	.name = DRV_NAME,
1007	.probe = if_usb_probe,
1008	.disconnect = if_usb_disconnect,
1009	.id_table = if_usb_table,
1010	.suspend = if_usb_suspend,
1011	.resume = if_usb_resume,
1012};
1013
1014static int __init if_usb_init_module(void)
1015{
1016	int ret = 0;
1017
1018	lbs_deb_enter(LBS_DEB_MAIN);
1019
1020	ret = usb_register(&if_usb_driver);
1021
1022	lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1023	return ret;
1024}
1025
1026static void __exit if_usb_exit_module(void)
1027{
1028	lbs_deb_enter(LBS_DEB_MAIN);
1029
1030	usb_deregister(&if_usb_driver);
1031
1032	lbs_deb_leave(LBS_DEB_MAIN);
1033}
1034
1035module_init(if_usb_init_module);
1036module_exit(if_usb_exit_module);
1037
1038MODULE_DESCRIPTION("8388 USB WLAN Driver");
1039MODULE_AUTHOR("Marvell International Ltd.");
1040MODULE_LICENSE("GPL");
1041