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