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