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