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