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