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