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