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