libertas: kill TxLockFlag
[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->adapter->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                 struct lbs_adapter *adapter = priv->adapter;
274
275                 adapter->surpriseremoved = 1;
276                 lbs_stop_card(priv);
277                 lbs_remove_mesh(priv);
278                 lbs_remove_card(priv);
279         }
280
281         /* this is (apparently?) necessary for future usage of the device */
282         lbs_prepare_and_send_command(priv, CMD_802_11_RESET, CMD_ACT_HALT,
283                         0, 0, NULL);
284
285         /* Unlink and free urb */
286         if_usb_free(cardp);
287
288         usb_set_intfdata(intf, NULL);
289         usb_put_dev(interface_to_usbdev(intf));
290
291         lbs_deb_leave(LBS_DEB_MAIN);
292 }
293
294 /**
295  *  @brief  This function download FW
296  *  @param priv         pointer to struct lbs_private
297  *  @return             0
298  */
299 static int if_prog_firmware(struct usb_card_rec *cardp)
300 {
301         struct FWData *fwdata;
302         struct fwheader *fwheader;
303         u8 *firmware = cardp->fw->data;
304
305         fwdata = kmalloc(sizeof(struct FWData), GFP_ATOMIC);
306
307         if (!fwdata)
308                 return -1;
309
310         fwheader = &fwdata->fwheader;
311
312         if (!cardp->CRC_OK) {
313                 cardp->totalbytes = cardp->fwlastblksent;
314                 cardp->fwseqnum = cardp->lastseqnum - 1;
315         }
316
317         /*
318         lbs_deb_usbd(&cardp->udev->dev, "totalbytes = %d\n",
319                     cardp->totalbytes);
320         */
321
322         memcpy(fwheader, &firmware[cardp->totalbytes],
323                sizeof(struct fwheader));
324
325         cardp->fwlastblksent = cardp->totalbytes;
326         cardp->totalbytes += sizeof(struct fwheader);
327
328         /* lbs_deb_usbd(&cardp->udev->dev,"Copy Data\n"); */
329         memcpy(fwdata->data, &firmware[cardp->totalbytes],
330                le32_to_cpu(fwdata->fwheader.datalength));
331
332         /*
333         lbs_deb_usbd(&cardp->udev->dev,
334                     "Data length = %d\n", le32_to_cpu(fwdata->fwheader.datalength));
335         */
336
337         cardp->fwseqnum = cardp->fwseqnum + 1;
338
339         fwdata->seqnum = cpu_to_le32(cardp->fwseqnum);
340         cardp->lastseqnum = cardp->fwseqnum;
341         cardp->totalbytes += le32_to_cpu(fwdata->fwheader.datalength);
342
343         if (fwheader->dnldcmd == cpu_to_le32(FW_HAS_DATA_TO_RECV)) {
344                 /*
345                 lbs_deb_usbd(&cardp->udev->dev, "There are data to follow\n");
346                 lbs_deb_usbd(&cardp->udev->dev,
347                             "seqnum = %d totalbytes = %d\n", cardp->fwseqnum,
348                             cardp->totalbytes);
349                 */
350                 memcpy(cardp->bulk_out_buffer, fwheader, FW_DATA_XMIT_SIZE);
351                 usb_tx_block(cardp, cardp->bulk_out_buffer, FW_DATA_XMIT_SIZE);
352
353         } else if (fwdata->fwheader.dnldcmd == cpu_to_le32(FW_HAS_LAST_BLOCK)) {
354                 /*
355                 lbs_deb_usbd(&cardp->udev->dev,
356                             "Host has finished FW downloading\n");
357                 lbs_deb_usbd(&cardp->udev->dev,
358                             "Donwloading FW JUMP BLOCK\n");
359                 */
360                 memcpy(cardp->bulk_out_buffer, fwheader, FW_DATA_XMIT_SIZE);
361                 usb_tx_block(cardp, cardp->bulk_out_buffer, FW_DATA_XMIT_SIZE);
362                 cardp->fwfinalblk = 1;
363         }
364
365         /*
366         lbs_deb_usbd(&cardp->udev->dev,
367                     "The firmware download is done size is %d\n",
368                     cardp->totalbytes);
369         */
370
371         kfree(fwdata);
372
373         return 0;
374 }
375
376 static int if_usb_reset_device(struct usb_card_rec *cardp)
377 {
378         int ret;
379         struct lbs_private *priv = cardp->priv;
380
381         lbs_deb_enter(LBS_DEB_USB);
382
383         /* Try a USB port reset first, if that fails send the reset
384          * command to the firmware.
385          */
386         ret = usb_reset_device(cardp->udev);
387         if (!ret && priv) {
388                 msleep(10);
389                 ret = lbs_reset_device(priv);
390                 msleep(10);
391         }
392
393         lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
394
395         return ret;
396 }
397
398 /**
399  *  @brief This function transfer the data to the device.
400  *  @param priv         pointer to struct lbs_private
401  *  @param payload      pointer to payload data
402  *  @param nb           data length
403  *  @return             0 or -1
404  */
405 static int usb_tx_block(struct usb_card_rec *cardp, u8 * payload, u16 nb)
406 {
407         int ret = -1;
408
409         /* check if device is removed */
410         if (cardp->surprise_removed) {
411                 lbs_deb_usbd(&cardp->udev->dev, "Device removed\n");
412                 goto tx_ret;
413         }
414
415         usb_fill_bulk_urb(cardp->tx_urb, cardp->udev,
416                           usb_sndbulkpipe(cardp->udev,
417                                           cardp->bulk_out_endpointAddr),
418                           payload, nb, if_usb_write_bulk_callback, cardp);
419
420         cardp->tx_urb->transfer_flags |= URB_ZERO_PACKET;
421
422         if ((ret = usb_submit_urb(cardp->tx_urb, GFP_ATOMIC))) {
423                 /*  transfer failed */
424                 lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb failed: %d\n", ret);
425                 ret = -1;
426         } else {
427                 /* lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb success\n"); */
428                 ret = 0;
429         }
430
431 tx_ret:
432         return ret;
433 }
434
435 static int __if_usb_submit_rx_urb(struct usb_card_rec *cardp,
436                                   void (*callbackfn)(struct urb *urb))
437 {
438         struct sk_buff *skb;
439         struct read_cb_info *rinfo = &cardp->rinfo;
440         int ret = -1;
441
442         if (!(skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE))) {
443                 lbs_pr_err("No free skb\n");
444                 goto rx_ret;
445         }
446
447         rinfo->skb = skb;
448
449         /* Fill the receive configuration URB and initialise the Rx call back */
450         usb_fill_bulk_urb(cardp->rx_urb, cardp->udev,
451                           usb_rcvbulkpipe(cardp->udev,
452                                           cardp->bulk_in_endpointAddr),
453                           (void *) (skb->tail + (size_t) IPFIELD_ALIGN_OFFSET),
454                           MRVDRV_ETH_RX_PACKET_BUFFER_SIZE, callbackfn,
455                           rinfo);
456
457         cardp->rx_urb->transfer_flags |= URB_ZERO_PACKET;
458
459         /* lbs_deb_usbd(&cardp->udev->dev, "Pointer for rx_urb %p\n", cardp->rx_urb); */
460         if ((ret = usb_submit_urb(cardp->rx_urb, GFP_ATOMIC))) {
461                 /* handle failure conditions */
462                 lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB failed: %d\n", ret);
463                 kfree_skb(skb);
464                 rinfo->skb = NULL;
465                 ret = -1;
466         } else {
467                 /* lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB success\n"); */
468                 ret = 0;
469         }
470
471 rx_ret:
472         return ret;
473 }
474
475 static int if_usb_submit_rx_urb_fwload(struct usb_card_rec *cardp)
476 {
477         return __if_usb_submit_rx_urb(cardp, &if_usb_receive_fwload);
478 }
479
480 static int if_usb_submit_rx_urb(struct usb_card_rec *cardp)
481 {
482         return __if_usb_submit_rx_urb(cardp, &if_usb_receive);
483 }
484
485 static void if_usb_receive_fwload(struct urb *urb)
486 {
487         struct read_cb_info *rinfo = (struct read_cb_info *)urb->context;
488         struct sk_buff *skb = rinfo->skb;
489         struct usb_card_rec *cardp = (struct usb_card_rec *)rinfo->cardp;
490         struct fwsyncheader *syncfwheader;
491         struct bootcmdrespStr bootcmdresp;
492
493         if (urb->status) {
494                 lbs_deb_usbd(&cardp->udev->dev,
495                             "URB status is failed during fw load\n");
496                 kfree_skb(skb);
497                 return;
498         }
499
500         if (cardp->bootcmdresp == 0) {
501                 memcpy (&bootcmdresp, skb->data + IPFIELD_ALIGN_OFFSET,
502                         sizeof(bootcmdresp));
503                 if (le16_to_cpu(cardp->udev->descriptor.bcdDevice) < 0x3106) {
504                         kfree_skb(skb);
505                         if_usb_submit_rx_urb_fwload(cardp);
506                         cardp->bootcmdresp = 1;
507                         lbs_deb_usbd(&cardp->udev->dev,
508                                     "Received valid boot command response\n");
509                         return;
510                 }
511                 if (bootcmdresp.u32magicnumber != cpu_to_le32(BOOT_CMD_MAGIC_NUMBER)) {
512                         lbs_pr_info(
513                                 "boot cmd response wrong magic number (0x%x)\n",
514                                 le32_to_cpu(bootcmdresp.u32magicnumber));
515                 } else if (bootcmdresp.u8cmd_tag != BOOT_CMD_FW_BY_USB) {
516                         lbs_pr_info(
517                                 "boot cmd response cmd_tag error (%d)\n",
518                                 bootcmdresp.u8cmd_tag);
519                 } else if (bootcmdresp.u8result != BOOT_CMD_RESP_OK) {
520                         lbs_pr_info(
521                                 "boot cmd response result error (%d)\n",
522                                 bootcmdresp.u8result);
523                 } else {
524                         cardp->bootcmdresp = 1;
525                         lbs_deb_usbd(&cardp->udev->dev,
526                                     "Received valid boot command response\n");
527                 }
528                 kfree_skb(skb);
529                 if_usb_submit_rx_urb_fwload(cardp);
530                 return;
531         }
532
533         syncfwheader = kmalloc(sizeof(struct fwsyncheader), GFP_ATOMIC);
534         if (!syncfwheader) {
535                 lbs_deb_usbd(&cardp->udev->dev, "Failure to allocate syncfwheader\n");
536                 kfree_skb(skb);
537                 return;
538         }
539
540         memcpy(syncfwheader, skb->data + IPFIELD_ALIGN_OFFSET,
541                         sizeof(struct fwsyncheader));
542
543         if (!syncfwheader->cmd) {
544                 /*
545                 lbs_deb_usbd(&cardp->udev->dev,
546                             "FW received Blk with correct CRC\n");
547                 lbs_deb_usbd(&cardp->udev->dev,
548                             "FW received Blk seqnum = %d\n",
549                        syncfwheader->seqnum);
550                 */
551                 cardp->CRC_OK = 1;
552         } else {
553                 lbs_deb_usbd(&cardp->udev->dev,
554                             "FW received Blk with CRC error\n");
555                 cardp->CRC_OK = 0;
556         }
557
558         kfree_skb(skb);
559
560         if (cardp->fwfinalblk) {
561                 cardp->fwdnldover = 1;
562                 goto exit;
563         }
564
565         if_prog_firmware(cardp);
566
567         if_usb_submit_rx_urb_fwload(cardp);
568 exit:
569         kfree(syncfwheader);
570
571         return;
572
573 }
574
575 #define MRVDRV_MIN_PKT_LEN      30
576
577 static inline void process_cmdtypedata(int recvlength, struct sk_buff *skb,
578                                        struct usb_card_rec *cardp,
579                                        struct lbs_private *priv)
580 {
581         if (recvlength > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE +
582             MESSAGE_HEADER_LEN || recvlength < MRVDRV_MIN_PKT_LEN) {
583                 lbs_deb_usbd(&cardp->udev->dev,
584                             "Packet length is Invalid\n");
585                 kfree_skb(skb);
586                 return;
587         }
588
589         skb_reserve(skb, IPFIELD_ALIGN_OFFSET);
590         skb_put(skb, recvlength);
591         skb_pull(skb, MESSAGE_HEADER_LEN);
592         lbs_process_rxed_packet(priv, skb);
593         priv->upld_len = (recvlength - MESSAGE_HEADER_LEN);
594 }
595
596 static inline void process_cmdrequest(int recvlength, u8 *recvbuff,
597                                       struct sk_buff *skb,
598                                       struct usb_card_rec *cardp,
599                                       struct lbs_private *priv)
600 {
601         u8 *cmdbuf;
602         if (recvlength > MRVDRV_SIZE_OF_CMD_BUFFER) {
603                 lbs_deb_usbd(&cardp->udev->dev,
604                             "The receive buffer is too large\n");
605                 kfree_skb(skb);
606                 return;
607         }
608
609         if (!in_interrupt())
610                 BUG();
611
612         spin_lock(&priv->adapter->driver_lock);
613         /* take care of cur_cmd = NULL case by reading the
614          * data to clear the interrupt */
615         if (!priv->adapter->cur_cmd) {
616                 cmdbuf = priv->upld_buf;
617                 priv->adapter->hisregcpy &= ~MRVDRV_CMD_UPLD_RDY;
618         } else
619                 cmdbuf = priv->adapter->cur_cmd->bufvirtualaddr;
620
621         cardp->usb_int_cause |= MRVDRV_CMD_UPLD_RDY;
622         priv->upld_len = (recvlength - MESSAGE_HEADER_LEN);
623         memcpy(cmdbuf, recvbuff + MESSAGE_HEADER_LEN,
624                priv->upld_len);
625
626         kfree_skb(skb);
627         lbs_interrupt(priv->dev);
628         spin_unlock(&priv->adapter->driver_lock);
629
630         lbs_deb_usbd(&cardp->udev->dev,
631                     "Wake up main thread to handle cmd response\n");
632
633         return;
634 }
635
636 /**
637  *  @brief This function reads of the packet into the upload buff,
638  *  wake up the main thread and initialise the Rx callack.
639  *
640  *  @param urb          pointer to struct urb
641  *  @return             N/A
642  */
643 static void if_usb_receive(struct urb *urb)
644 {
645         struct read_cb_info *rinfo = (struct read_cb_info *)urb->context;
646         struct sk_buff *skb = rinfo->skb;
647         struct usb_card_rec *cardp = (struct usb_card_rec *) rinfo->cardp;
648         struct lbs_private *priv = cardp->priv;
649
650         int recvlength = urb->actual_length;
651         u8 *recvbuff = NULL;
652         u32 recvtype = 0;
653
654         lbs_deb_enter(LBS_DEB_USB);
655
656         if (recvlength) {
657                 __le32 tmp;
658
659                 if (urb->status) {
660                         lbs_deb_usbd(&cardp->udev->dev,
661                                     "URB status is failed\n");
662                         kfree_skb(skb);
663                         goto setup_for_next;
664                 }
665
666                 recvbuff = skb->data + IPFIELD_ALIGN_OFFSET;
667                 memcpy(&tmp, recvbuff, sizeof(u32));
668                 recvtype = le32_to_cpu(tmp);
669                 lbs_deb_usbd(&cardp->udev->dev,
670                             "Recv length = 0x%x, Recv type = 0x%X\n",
671                             recvlength, recvtype);
672         } else if (urb->status) {
673                 kfree_skb(skb);
674                 goto rx_exit;
675         }
676
677         switch (recvtype) {
678         case CMD_TYPE_DATA:
679                 process_cmdtypedata(recvlength, skb, cardp, priv);
680                 break;
681
682         case CMD_TYPE_REQUEST:
683                 process_cmdrequest(recvlength, recvbuff, skb, cardp, priv);
684                 break;
685
686         case CMD_TYPE_INDICATION:
687                 /* Event cause handling */
688                 spin_lock(&priv->adapter->driver_lock);
689                 cardp->usb_event_cause = le32_to_cpu(*(__le32 *) (recvbuff + MESSAGE_HEADER_LEN));
690                 lbs_deb_usbd(&cardp->udev->dev,"**EVENT** 0x%X\n",
691                             cardp->usb_event_cause);
692                 if (cardp->usb_event_cause & 0xffff0000) {
693                         lbs_send_tx_feedback(priv);
694                         spin_unlock(&priv->adapter->driver_lock);
695                         break;
696                 }
697                 cardp->usb_event_cause <<= 3;
698                 cardp->usb_int_cause |= MRVDRV_CARDEVENT;
699                 kfree_skb(skb);
700                 lbs_interrupt(priv->dev);
701                 spin_unlock(&priv->adapter->driver_lock);
702                 goto rx_exit;
703         default:
704                 lbs_deb_usbd(&cardp->udev->dev, "Unknown command type 0x%X\n",
705                              recvtype);
706                 kfree_skb(skb);
707                 break;
708         }
709
710 setup_for_next:
711         if_usb_submit_rx_urb(cardp);
712 rx_exit:
713         lbs_deb_leave(LBS_DEB_USB);
714 }
715
716 /**
717  *  @brief This function downloads data to FW
718  *  @param priv         pointer to struct lbs_private structure
719  *  @param type         type of data
720  *  @param buf          pointer to data buffer
721  *  @param len          number of bytes
722  *  @return             0 or -1
723  */
724 static int if_usb_host_to_card(struct lbs_private *priv,
725         u8 type,
726         u8 *payload,
727         u16 nb)
728 {
729         struct usb_card_rec *cardp = (struct usb_card_rec *)priv->card;
730
731         lbs_deb_usbd(&cardp->udev->dev,"*** type = %u\n", type);
732         lbs_deb_usbd(&cardp->udev->dev,"size after = %d\n", nb);
733
734         if (type == MVMS_CMD) {
735                 __le32 tmp = cpu_to_le32(CMD_TYPE_REQUEST);
736                 priv->dnld_sent = DNLD_CMD_SENT;
737                 memcpy(cardp->bulk_out_buffer, (u8 *) & tmp,
738                        MESSAGE_HEADER_LEN);
739
740         } else {
741                 __le32 tmp = cpu_to_le32(CMD_TYPE_DATA);
742                 priv->dnld_sent = DNLD_DATA_SENT;
743                 memcpy(cardp->bulk_out_buffer, (u8 *) & tmp,
744                        MESSAGE_HEADER_LEN);
745         }
746
747         memcpy((cardp->bulk_out_buffer + MESSAGE_HEADER_LEN), payload, nb);
748
749         return usb_tx_block(cardp, cardp->bulk_out_buffer,
750                             nb + MESSAGE_HEADER_LEN);
751 }
752
753 /* called with adapter->driver_lock held */
754 static int if_usb_get_int_status(struct lbs_private *priv, u8 *ireg)
755 {
756         struct usb_card_rec *cardp = priv->card;
757
758         *ireg = cardp->usb_int_cause;
759         cardp->usb_int_cause = 0;
760
761         lbs_deb_usbd(&cardp->udev->dev,"Int cause is 0x%X\n", *ireg);
762
763         return 0;
764 }
765
766 static int if_usb_read_event_cause(struct lbs_private *priv)
767 {
768         struct usb_card_rec *cardp = priv->card;
769
770         priv->adapter->eventcause = cardp->usb_event_cause;
771         /* Re-submit rx urb here to avoid event lost issue */
772         if_usb_submit_rx_urb(cardp);
773         return 0;
774 }
775
776 /**
777  *  @brief This function issues Boot command to the Boot2 code
778  *  @param ivalue   1:Boot from FW by USB-Download
779  *                  2:Boot from FW in EEPROM
780  *  @return             0
781  */
782 static int if_usb_issue_boot_command(struct usb_card_rec *cardp, int ivalue)
783 {
784         struct bootcmdstr sbootcmd;
785         int i;
786
787         /* Prepare command */
788         sbootcmd.u32magicnumber = cpu_to_le32(BOOT_CMD_MAGIC_NUMBER);
789         sbootcmd.u8cmd_tag = ivalue;
790         for (i=0; i<11; i++)
791                 sbootcmd.au8dumy[i]=0x00;
792         memcpy(cardp->bulk_out_buffer, &sbootcmd, sizeof(struct bootcmdstr));
793
794         /* Issue command */
795         usb_tx_block(cardp, cardp->bulk_out_buffer, sizeof(struct bootcmdstr));
796
797         return 0;
798 }
799
800
801 /**
802  *  @brief This function checks the validity of Boot2/FW image.
803  *
804  *  @param data              pointer to image
805  *         len               image length
806  *  @return     0 or -1
807  */
808 static int check_fwfile_format(u8 *data, u32 totlen)
809 {
810         u32 bincmd, exit;
811         u32 blksize, offset, len;
812         int ret;
813
814         ret = 1;
815         exit = len = 0;
816
817         do {
818                 struct fwheader *fwh = (void *)data;
819
820                 bincmd = le32_to_cpu(fwh->dnldcmd);
821                 blksize = le32_to_cpu(fwh->datalength);
822                 switch (bincmd) {
823                 case FW_HAS_DATA_TO_RECV:
824                         offset = sizeof(struct fwheader) + blksize;
825                         data += offset;
826                         len += offset;
827                         if (len >= totlen)
828                                 exit = 1;
829                         break;
830                 case FW_HAS_LAST_BLOCK:
831                         exit = 1;
832                         ret = 0;
833                         break;
834                 default:
835                         exit = 1;
836                         break;
837                 }
838         } while (!exit);
839
840         if (ret)
841                 lbs_pr_err("firmware file format check FAIL\n");
842         else
843                 lbs_deb_fw("firmware file format check PASS\n");
844
845         return ret;
846 }
847
848
849 static int if_usb_prog_firmware(struct usb_card_rec *cardp)
850 {
851         int i = 0;
852         static int reset_count = 10;
853         int ret = 0;
854
855         lbs_deb_enter(LBS_DEB_USB);
856
857         if ((ret = request_firmware(&cardp->fw, lbs_fw_name,
858                                     &cardp->udev->dev)) < 0) {
859                 lbs_pr_err("request_firmware() failed with %#x\n", ret);
860                 lbs_pr_err("firmware %s not found\n", lbs_fw_name);
861                 goto done;
862         }
863
864         if (check_fwfile_format(cardp->fw->data, cardp->fw->size))
865                 goto release_fw;
866
867 restart:
868         if (if_usb_submit_rx_urb_fwload(cardp) < 0) {
869                 lbs_deb_usbd(&cardp->udev->dev, "URB submission is failed\n");
870                 ret = -1;
871                 goto release_fw;
872         }
873
874         cardp->bootcmdresp = 0;
875         do {
876                 int j = 0;
877                 i++;
878                 /* Issue Boot command = 1, Boot from Download-FW */
879                 if_usb_issue_boot_command(cardp, BOOT_CMD_FW_BY_USB);
880                 /* wait for command response */
881                 do {
882                         j++;
883                         msleep_interruptible(100);
884                 } while (cardp->bootcmdresp == 0 && j < 10);
885         } while (cardp->bootcmdresp == 0 && i < 5);
886
887         if (cardp->bootcmdresp == 0) {
888                 if (--reset_count >= 0) {
889                         if_usb_reset_device(cardp);
890                         goto restart;
891                 }
892                 return -1;
893         }
894
895         i = 0;
896
897         cardp->totalbytes = 0;
898         cardp->fwlastblksent = 0;
899         cardp->CRC_OK = 1;
900         cardp->fwdnldover = 0;
901         cardp->fwseqnum = -1;
902         cardp->totalbytes = 0;
903         cardp->fwfinalblk = 0;
904
905         if_prog_firmware(cardp);
906
907         do {
908                 lbs_deb_usbd(&cardp->udev->dev,"Wlan sched timeout\n");
909                 i++;
910                 msleep_interruptible(100);
911                 if (cardp->surprise_removed || i >= 20)
912                         break;
913         } while (!cardp->fwdnldover);
914
915         if (!cardp->fwdnldover) {
916                 lbs_pr_info("failed to load fw, resetting device!\n");
917                 if (--reset_count >= 0) {
918                         if_usb_reset_device(cardp);
919                         goto restart;
920                 }
921
922                 lbs_pr_info("FW download failure, time = %d ms\n", i * 100);
923                 ret = -1;
924                 goto release_fw;
925         }
926
927 release_fw:
928         release_firmware(cardp->fw);
929         cardp->fw = NULL;
930
931 done:
932         lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
933         return ret;
934 }
935
936
937 #ifdef CONFIG_PM
938 static int if_usb_suspend(struct usb_interface *intf, pm_message_t message)
939 {
940         struct usb_card_rec *cardp = usb_get_intfdata(intf);
941         struct lbs_private *priv = cardp->priv;
942
943         lbs_deb_enter(LBS_DEB_USB);
944
945         if (priv->adapter->psstate != PS_STATE_FULL_POWER)
946                 return -1;
947
948         if (priv->mesh_dev && !priv->mesh_autostart_enabled) {
949                 /* Mesh autostart must be activated while sleeping
950                  * On resume it will go back to the current state
951                  */
952                 struct cmd_ds_mesh_access mesh_access;
953                 memset(&mesh_access, 0, sizeof(mesh_access));
954                 mesh_access.data[0] = cpu_to_le32(1);
955                 lbs_prepare_and_send_command(priv,
956                                 CMD_MESH_ACCESS,
957                                 CMD_ACT_MESH_SET_AUTOSTART_ENABLED,
958                                 CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
959         }
960
961         netif_device_detach(cardp->eth_dev);
962         netif_device_detach(priv->mesh_dev);
963
964         /* Unlink tx & rx urb */
965         usb_kill_urb(cardp->tx_urb);
966         usb_kill_urb(cardp->rx_urb);
967
968         cardp->rx_urb_recall = 1;
969
970         lbs_deb_leave(LBS_DEB_USB);
971         return 0;
972 }
973
974 static int if_usb_resume(struct usb_interface *intf)
975 {
976         struct usb_card_rec *cardp = usb_get_intfdata(intf);
977         struct lbs_private *priv = cardp->priv;
978
979         lbs_deb_enter(LBS_DEB_USB);
980
981         cardp->rx_urb_recall = 0;
982
983         if_usb_submit_rx_urb(cardp->priv);
984
985         netif_device_attach(cardp->eth_dev);
986         netif_device_attach(priv->mesh_dev);
987
988         if (priv->mesh_dev && !priv->mesh_autostart_enabled) {
989                 /* Mesh autostart was activated while sleeping
990                  * Disable it if appropriate
991                  */
992                 struct cmd_ds_mesh_access mesh_access;
993                 memset(&mesh_access, 0, sizeof(mesh_access));
994                 mesh_access.data[0] = cpu_to_le32(0);
995                 lbs_prepare_and_send_command(priv,
996                                 CMD_MESH_ACCESS,
997                                 CMD_ACT_MESH_SET_AUTOSTART_ENABLED,
998                                 CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
999         }
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");