libertas: support boot commands to write persistent firmware and bootloader
[linux-2.6/btrfs-unstable.git] / drivers / net / wireless / libertas / if_usb.c
blobb5013ce31b9abd7732005b918f5ad53dbcab6a04
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>
10 #ifdef CONFIG_OLPC
11 #include <asm/olpc.h>
12 #endif
14 #define DRV_NAME "usb8xxx"
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"
23 #define INSANEDEBUG 0
24 #define lbs_deb_usb2(...) do { if (INSANEDEBUG) lbs_deb_usbd(__VA_ARGS__); } while (0)
26 #define MESSAGE_HEADER_LEN 4
28 static char *lbs_fw_name = "usb8388.bin";
29 module_param_named(fw_name, lbs_fw_name, charp, 0644);
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 */
38 MODULE_DEVICE_TABLE(usb, if_usb_table);
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 const char *fwname, int cmd);
44 static int if_usb_prog_firmware(struct if_usb_card *cardp,
45 const char *fwname, int cmd);
46 static int if_usb_host_to_card(struct lbs_private *priv, uint8_t type,
47 uint8_t *payload, uint16_t nb);
48 static int usb_tx_block(struct if_usb_card *cardp, uint8_t *payload,
49 uint16_t nb);
50 static void if_usb_free(struct if_usb_card *cardp);
51 static int if_usb_submit_rx_urb(struct if_usb_card *cardp);
52 static int if_usb_reset_device(struct if_usb_card *cardp);
54 /**
55 * @brief call back function to handle the status of the URB
56 * @param urb pointer to urb structure
57 * @return N/A
59 static void if_usb_write_bulk_callback(struct urb *urb)
61 struct if_usb_card *cardp = (struct if_usb_card *) urb->context;
63 /* handle the transmission complete validations */
65 if (urb->status == 0) {
66 struct lbs_private *priv = cardp->priv;
68 lbs_deb_usb2(&urb->dev->dev, "URB status is successful\n");
69 lbs_deb_usb2(&urb->dev->dev, "Actual length transmitted %d\n",
70 urb->actual_length);
72 /* Boot commands such as UPDATE_FW and UPDATE_BOOT2 are not
73 * passed up to the lbs level.
75 if (priv && priv->dnld_sent != DNLD_BOOTCMD_SENT)
76 lbs_host_to_card_done(priv);
77 } else {
78 /* print the failure status number for debug */
79 lbs_pr_info("URB in failure status: %d\n", urb->status);
82 return;
85 /**
86 * @brief free tx/rx urb, skb and rx buffer
87 * @param cardp pointer if_usb_card
88 * @return N/A
90 static void if_usb_free(struct if_usb_card *cardp)
92 lbs_deb_enter(LBS_DEB_USB);
94 /* Unlink tx & rx urb */
95 usb_kill_urb(cardp->tx_urb);
96 usb_kill_urb(cardp->rx_urb);
98 usb_free_urb(cardp->tx_urb);
99 cardp->tx_urb = NULL;
101 usb_free_urb(cardp->rx_urb);
102 cardp->rx_urb = NULL;
104 kfree(cardp->ep_out_buf);
105 cardp->ep_out_buf = NULL;
107 lbs_deb_leave(LBS_DEB_USB);
110 static void if_usb_setup_firmware(struct lbs_private *priv)
112 struct if_usb_card *cardp = priv->card;
113 struct cmd_ds_set_boot2_ver b2_cmd;
114 struct cmd_ds_802_11_fw_wake_method wake_method;
116 b2_cmd.hdr.size = cpu_to_le16(sizeof(b2_cmd));
117 b2_cmd.action = 0;
118 b2_cmd.version = cardp->boot2_version;
120 if (lbs_cmd_with_response(priv, CMD_SET_BOOT2_VER, &b2_cmd))
121 lbs_deb_usb("Setting boot2 version failed\n");
123 priv->wol_gpio = 2; /* Wake via GPIO2... */
124 priv->wol_gap = 20; /* ... after 20ms */
125 lbs_host_sleep_cfg(priv, EHS_WAKE_ON_UNICAST_DATA);
127 wake_method.hdr.size = cpu_to_le16(sizeof(wake_method));
128 wake_method.action = cpu_to_le16(CMD_ACT_GET);
129 if (lbs_cmd_with_response(priv, CMD_802_11_FW_WAKE_METHOD, &wake_method)) {
130 lbs_pr_info("Firmware does not seem to support PS mode\n");
131 } else {
132 if (le16_to_cpu(wake_method.method) == CMD_WAKE_METHOD_COMMAND_INT) {
133 lbs_deb_usb("Firmware seems to support PS with wake-via-command\n");
134 priv->ps_supported = 1;
135 } else {
136 /* The versions which boot up this way don't seem to
137 work even if we set it to the command interrupt */
138 lbs_pr_info("Firmware doesn't wake via command interrupt; disabling PS mode\n");
143 static void if_usb_fw_timeo(unsigned long priv)
145 struct if_usb_card *cardp = (void *)priv;
147 if (cardp->fwdnldover) {
148 lbs_deb_usb("Download complete, no event. Assuming success\n");
149 } else {
150 lbs_pr_err("Download timed out\n");
151 cardp->surprise_removed = 1;
153 wake_up(&cardp->fw_wq);
156 #ifdef CONFIG_OLPC
157 static void if_usb_reset_olpc_card(struct lbs_private *priv)
159 printk(KERN_CRIT "Resetting OLPC wireless via EC...\n");
160 olpc_ec_cmd(0x25, NULL, 0, NULL, 0);
162 #endif
165 * @brief sets the configuration values
166 * @param ifnum interface number
167 * @param id pointer to usb_device_id
168 * @return 0 on success, error code on failure
170 static int if_usb_probe(struct usb_interface *intf,
171 const struct usb_device_id *id)
173 struct usb_device *udev;
174 struct usb_host_interface *iface_desc;
175 struct usb_endpoint_descriptor *endpoint;
176 struct lbs_private *priv;
177 struct if_usb_card *cardp;
178 int i;
180 udev = interface_to_usbdev(intf);
182 cardp = kzalloc(sizeof(struct if_usb_card), GFP_KERNEL);
183 if (!cardp) {
184 lbs_pr_err("Out of memory allocating private data.\n");
185 goto error;
188 setup_timer(&cardp->fw_timeout, if_usb_fw_timeo, (unsigned long)cardp);
189 init_waitqueue_head(&cardp->fw_wq);
191 cardp->udev = udev;
192 iface_desc = intf->cur_altsetting;
194 lbs_deb_usbd(&udev->dev, "bcdUSB = 0x%X bDeviceClass = 0x%X"
195 " bDeviceSubClass = 0x%X, bDeviceProtocol = 0x%X\n",
196 le16_to_cpu(udev->descriptor.bcdUSB),
197 udev->descriptor.bDeviceClass,
198 udev->descriptor.bDeviceSubClass,
199 udev->descriptor.bDeviceProtocol);
201 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
202 endpoint = &iface_desc->endpoint[i].desc;
203 if (usb_endpoint_is_bulk_in(endpoint)) {
204 cardp->ep_in_size = le16_to_cpu(endpoint->wMaxPacketSize);
205 cardp->ep_in = usb_endpoint_num(endpoint);
207 lbs_deb_usbd(&udev->dev, "in_endpoint = %d\n", cardp->ep_in);
208 lbs_deb_usbd(&udev->dev, "Bulk in size is %d\n", cardp->ep_in_size);
210 } else if (usb_endpoint_is_bulk_out(endpoint)) {
211 cardp->ep_out_size = le16_to_cpu(endpoint->wMaxPacketSize);
212 cardp->ep_out = usb_endpoint_num(endpoint);
214 lbs_deb_usbd(&udev->dev, "out_endpoint = %d\n", cardp->ep_out);
215 lbs_deb_usbd(&udev->dev, "Bulk out size is %d\n", cardp->ep_out_size);
218 if (!cardp->ep_out_size || !cardp->ep_in_size) {
219 lbs_deb_usbd(&udev->dev, "Endpoints not found\n");
220 goto dealloc;
222 if (!(cardp->rx_urb = usb_alloc_urb(0, GFP_KERNEL))) {
223 lbs_deb_usbd(&udev->dev, "Rx URB allocation failed\n");
224 goto dealloc;
226 if (!(cardp->tx_urb = usb_alloc_urb(0, GFP_KERNEL))) {
227 lbs_deb_usbd(&udev->dev, "Tx URB allocation failed\n");
228 goto dealloc;
230 cardp->ep_out_buf = kmalloc(MRVDRV_ETH_TX_PACKET_BUFFER_SIZE, GFP_KERNEL);
231 if (!cardp->ep_out_buf) {
232 lbs_deb_usbd(&udev->dev, "Could not allocate buffer\n");
233 goto dealloc;
236 /* Upload firmware */
237 if (__if_usb_prog_firmware(cardp, lbs_fw_name, BOOT_CMD_FW_BY_USB)) {
238 lbs_deb_usbd(&udev->dev, "FW upload failed\n");
239 goto err_prog_firmware;
242 if (!(priv = lbs_add_card(cardp, &udev->dev)))
243 goto err_prog_firmware;
245 cardp->priv = priv;
246 cardp->priv->fw_ready = 1;
248 priv->hw_host_to_card = if_usb_host_to_card;
249 #ifdef CONFIG_OLPC
250 if (machine_is_olpc())
251 priv->reset_card = if_usb_reset_olpc_card;
252 #endif
254 cardp->boot2_version = udev->descriptor.bcdDevice;
256 if_usb_submit_rx_urb(cardp);
258 if (lbs_start_card(priv))
259 goto err_start_card;
261 if_usb_setup_firmware(priv);
263 usb_get_dev(udev);
264 usb_set_intfdata(intf, cardp);
266 return 0;
268 err_start_card:
269 lbs_remove_card(priv);
270 err_prog_firmware:
271 if_usb_reset_device(cardp);
272 dealloc:
273 if_usb_free(cardp);
275 error:
276 return -ENOMEM;
280 * @brief free resource and cleanup
281 * @param intf USB interface structure
282 * @return N/A
284 static void if_usb_disconnect(struct usb_interface *intf)
286 struct if_usb_card *cardp = usb_get_intfdata(intf);
287 struct lbs_private *priv = (struct lbs_private *) cardp->priv;
289 lbs_deb_enter(LBS_DEB_MAIN);
291 cardp->surprise_removed = 1;
293 if (priv) {
294 priv->surpriseremoved = 1;
295 lbs_stop_card(priv);
296 lbs_remove_card(priv);
299 /* Unlink and free urb */
300 if_usb_free(cardp);
302 usb_set_intfdata(intf, NULL);
303 usb_put_dev(interface_to_usbdev(intf));
305 lbs_deb_leave(LBS_DEB_MAIN);
309 * @brief This function download FW
310 * @param priv pointer to struct lbs_private
311 * @return 0
313 static int if_usb_send_fw_pkt(struct if_usb_card *cardp)
315 struct fwdata *fwdata = cardp->ep_out_buf;
316 const uint8_t *firmware = cardp->fw->data;
318 /* If we got a CRC failure on the last block, back
319 up and retry it */
320 if (!cardp->CRC_OK) {
321 cardp->totalbytes = cardp->fwlastblksent;
322 cardp->fwseqnum--;
325 lbs_deb_usb2(&cardp->udev->dev, "totalbytes = %d\n",
326 cardp->totalbytes);
328 /* struct fwdata (which we sent to the card) has an
329 extra __le32 field in between the header and the data,
330 which is not in the struct fwheader in the actual
331 firmware binary. Insert the seqnum in the middle... */
332 memcpy(&fwdata->hdr, &firmware[cardp->totalbytes],
333 sizeof(struct fwheader));
335 cardp->fwlastblksent = cardp->totalbytes;
336 cardp->totalbytes += sizeof(struct fwheader);
338 memcpy(fwdata->data, &firmware[cardp->totalbytes],
339 le32_to_cpu(fwdata->hdr.datalength));
341 lbs_deb_usb2(&cardp->udev->dev, "Data length = %d\n",
342 le32_to_cpu(fwdata->hdr.datalength));
344 fwdata->seqnum = cpu_to_le32(++cardp->fwseqnum);
345 cardp->totalbytes += le32_to_cpu(fwdata->hdr.datalength);
347 usb_tx_block(cardp, cardp->ep_out_buf, sizeof(struct fwdata) +
348 le32_to_cpu(fwdata->hdr.datalength));
350 if (fwdata->hdr.dnldcmd == cpu_to_le32(FW_HAS_DATA_TO_RECV)) {
351 lbs_deb_usb2(&cardp->udev->dev, "There are data to follow\n");
352 lbs_deb_usb2(&cardp->udev->dev, "seqnum = %d totalbytes = %d\n",
353 cardp->fwseqnum, cardp->totalbytes);
354 } else if (fwdata->hdr.dnldcmd == cpu_to_le32(FW_HAS_LAST_BLOCK)) {
355 lbs_deb_usb2(&cardp->udev->dev, "Host has finished FW downloading\n");
356 lbs_deb_usb2(&cardp->udev->dev, "Donwloading FW JUMP BLOCK\n");
358 cardp->fwfinalblk = 1;
361 lbs_deb_usb2(&cardp->udev->dev, "Firmware download done; size %d\n",
362 cardp->totalbytes);
364 return 0;
367 static int if_usb_reset_device(struct if_usb_card *cardp)
369 struct cmd_ds_command *cmd = cardp->ep_out_buf + 4;
370 int ret;
372 lbs_deb_enter(LBS_DEB_USB);
374 *(__le32 *)cardp->ep_out_buf = cpu_to_le32(CMD_TYPE_REQUEST);
376 cmd->command = cpu_to_le16(CMD_802_11_RESET);
377 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_reset) + S_DS_GEN);
378 cmd->result = cpu_to_le16(0);
379 cmd->seqnum = cpu_to_le16(0x5a5a);
380 cmd->params.reset.action = cpu_to_le16(CMD_ACT_HALT);
381 usb_tx_block(cardp, cardp->ep_out_buf, 4 + S_DS_GEN + sizeof(struct cmd_ds_802_11_reset));
383 msleep(100);
384 ret = usb_reset_device(cardp->udev);
385 msleep(100);
387 #ifdef CONFIG_OLPC
388 if (ret && machine_is_olpc())
389 if_usb_reset_olpc_card(NULL);
390 #endif
392 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
394 return ret;
398 * @brief This function transfer the data to the device.
399 * @param priv pointer to struct lbs_private
400 * @param payload pointer to payload data
401 * @param nb data length
402 * @return 0 or -1
404 static int usb_tx_block(struct if_usb_card *cardp, uint8_t *payload, uint16_t nb)
406 int ret = -1;
408 /* check if device is removed */
409 if (cardp->surprise_removed) {
410 lbs_deb_usbd(&cardp->udev->dev, "Device removed\n");
411 goto tx_ret;
414 usb_fill_bulk_urb(cardp->tx_urb, cardp->udev,
415 usb_sndbulkpipe(cardp->udev,
416 cardp->ep_out),
417 payload, nb, if_usb_write_bulk_callback, cardp);
419 cardp->tx_urb->transfer_flags |= URB_ZERO_PACKET;
421 if ((ret = usb_submit_urb(cardp->tx_urb, GFP_ATOMIC))) {
422 lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb failed: %d\n", ret);
423 ret = -1;
424 } else {
425 lbs_deb_usb2(&cardp->udev->dev, "usb_submit_urb success\n");
426 ret = 0;
429 tx_ret:
430 return ret;
433 static int __if_usb_submit_rx_urb(struct if_usb_card *cardp,
434 void (*callbackfn)(struct urb *urb))
436 struct sk_buff *skb;
437 int ret = -1;
439 if (!(skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE))) {
440 lbs_pr_err("No free skb\n");
441 goto rx_ret;
444 cardp->rx_skb = skb;
446 /* Fill the receive configuration URB and initialise the Rx call back */
447 usb_fill_bulk_urb(cardp->rx_urb, cardp->udev,
448 usb_rcvbulkpipe(cardp->udev, cardp->ep_in),
449 (void *) (skb->tail + (size_t) IPFIELD_ALIGN_OFFSET),
450 MRVDRV_ETH_RX_PACKET_BUFFER_SIZE, callbackfn,
451 cardp);
453 cardp->rx_urb->transfer_flags |= URB_ZERO_PACKET;
455 lbs_deb_usb2(&cardp->udev->dev, "Pointer for rx_urb %p\n", cardp->rx_urb);
456 if ((ret = usb_submit_urb(cardp->rx_urb, GFP_ATOMIC))) {
457 lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB failed: %d\n", ret);
458 kfree_skb(skb);
459 cardp->rx_skb = NULL;
460 ret = -1;
461 } else {
462 lbs_deb_usb2(&cardp->udev->dev, "Submit Rx URB success\n");
463 ret = 0;
466 rx_ret:
467 return ret;
470 static int if_usb_submit_rx_urb_fwload(struct if_usb_card *cardp)
472 return __if_usb_submit_rx_urb(cardp, &if_usb_receive_fwload);
475 static int if_usb_submit_rx_urb(struct if_usb_card *cardp)
477 return __if_usb_submit_rx_urb(cardp, &if_usb_receive);
480 static void if_usb_receive_fwload(struct urb *urb)
482 struct if_usb_card *cardp = urb->context;
483 struct sk_buff *skb = cardp->rx_skb;
484 struct fwsyncheader *syncfwheader;
485 struct bootcmdresp bootcmdresp;
487 if (urb->status) {
488 lbs_deb_usbd(&cardp->udev->dev,
489 "URB status is failed during fw load\n");
490 kfree_skb(skb);
491 return;
494 if (cardp->fwdnldover) {
495 __le32 *tmp = (__le32 *)(skb->data + IPFIELD_ALIGN_OFFSET);
497 if (tmp[0] == cpu_to_le32(CMD_TYPE_INDICATION) &&
498 tmp[1] == cpu_to_le32(MACREG_INT_CODE_FIRMWARE_READY)) {
499 lbs_pr_info("Firmware ready event received\n");
500 wake_up(&cardp->fw_wq);
501 } else {
502 lbs_deb_usb("Waiting for confirmation; got %x %x\n",
503 le32_to_cpu(tmp[0]), le32_to_cpu(tmp[1]));
504 if_usb_submit_rx_urb_fwload(cardp);
506 kfree_skb(skb);
507 return;
509 if (cardp->bootcmdresp <= 0) {
510 memcpy (&bootcmdresp, skb->data + IPFIELD_ALIGN_OFFSET,
511 sizeof(bootcmdresp));
513 if (le16_to_cpu(cardp->udev->descriptor.bcdDevice) < 0x3106) {
514 kfree_skb(skb);
515 if_usb_submit_rx_urb_fwload(cardp);
516 cardp->bootcmdresp = BOOT_CMD_RESP_OK;
517 lbs_deb_usbd(&cardp->udev->dev,
518 "Received valid boot command response\n");
519 return;
521 if (bootcmdresp.magic != cpu_to_le32(BOOT_CMD_MAGIC_NUMBER)) {
522 if (bootcmdresp.magic == cpu_to_le32(CMD_TYPE_REQUEST) ||
523 bootcmdresp.magic == cpu_to_le32(CMD_TYPE_DATA) ||
524 bootcmdresp.magic == cpu_to_le32(CMD_TYPE_INDICATION)) {
525 if (!cardp->bootcmdresp)
526 lbs_pr_info("Firmware already seems alive; resetting\n");
527 cardp->bootcmdresp = -1;
528 } else {
529 lbs_pr_info("boot cmd response wrong magic number (0x%x)\n",
530 le32_to_cpu(bootcmdresp.magic));
532 } else if ((bootcmdresp.cmd != BOOT_CMD_FW_BY_USB) &&
533 (bootcmdresp.cmd != BOOT_CMD_UPDATE_FW) &&
534 (bootcmdresp.cmd != BOOT_CMD_UPDATE_BOOT2)) {
535 lbs_pr_info("boot cmd response cmd_tag error (%d)\n",
536 bootcmdresp.cmd);
537 } else if (bootcmdresp.result != BOOT_CMD_RESP_OK) {
538 lbs_pr_info("boot cmd response result error (%d)\n",
539 bootcmdresp.result);
540 } else {
541 cardp->bootcmdresp = 1;
542 lbs_deb_usbd(&cardp->udev->dev,
543 "Received valid boot command response\n");
545 kfree_skb(skb);
546 if_usb_submit_rx_urb_fwload(cardp);
547 return;
550 syncfwheader = kmalloc(sizeof(struct fwsyncheader), GFP_ATOMIC);
551 if (!syncfwheader) {
552 lbs_deb_usbd(&cardp->udev->dev, "Failure to allocate syncfwheader\n");
553 kfree_skb(skb);
554 return;
557 memcpy(syncfwheader, skb->data + IPFIELD_ALIGN_OFFSET,
558 sizeof(struct fwsyncheader));
560 if (!syncfwheader->cmd) {
561 lbs_deb_usb2(&cardp->udev->dev, "FW received Blk with correct CRC\n");
562 lbs_deb_usb2(&cardp->udev->dev, "FW received Blk seqnum = %d\n",
563 le32_to_cpu(syncfwheader->seqnum));
564 cardp->CRC_OK = 1;
565 } else {
566 lbs_deb_usbd(&cardp->udev->dev, "FW received Blk with CRC error\n");
567 cardp->CRC_OK = 0;
570 kfree_skb(skb);
572 /* Give device 5s to either write firmware to its RAM or eeprom */
573 mod_timer(&cardp->fw_timeout, jiffies + (HZ*5));
575 if (cardp->fwfinalblk) {
576 cardp->fwdnldover = 1;
577 goto exit;
580 if_usb_send_fw_pkt(cardp);
582 exit:
583 if_usb_submit_rx_urb_fwload(cardp);
585 kfree(syncfwheader);
587 return;
590 #define MRVDRV_MIN_PKT_LEN 30
592 static inline void process_cmdtypedata(int recvlength, struct sk_buff *skb,
593 struct if_usb_card *cardp,
594 struct lbs_private *priv)
596 if (recvlength > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + MESSAGE_HEADER_LEN
597 || recvlength < MRVDRV_MIN_PKT_LEN) {
598 lbs_deb_usbd(&cardp->udev->dev, "Packet length is Invalid\n");
599 kfree_skb(skb);
600 return;
603 skb_reserve(skb, IPFIELD_ALIGN_OFFSET);
604 skb_put(skb, recvlength);
605 skb_pull(skb, MESSAGE_HEADER_LEN);
607 lbs_process_rxed_packet(priv, skb);
610 static inline void process_cmdrequest(int recvlength, uint8_t *recvbuff,
611 struct sk_buff *skb,
612 struct if_usb_card *cardp,
613 struct lbs_private *priv)
615 u8 i;
617 if (recvlength > LBS_CMD_BUFFER_SIZE) {
618 lbs_deb_usbd(&cardp->udev->dev,
619 "The receive buffer is too large\n");
620 kfree_skb(skb);
621 return;
624 if (!in_interrupt())
625 BUG();
627 spin_lock(&priv->driver_lock);
629 i = (priv->resp_idx == 0) ? 1 : 0;
630 BUG_ON(priv->resp_len[i]);
631 priv->resp_len[i] = (recvlength - MESSAGE_HEADER_LEN);
632 memcpy(priv->resp_buf[i], recvbuff + MESSAGE_HEADER_LEN,
633 priv->resp_len[i]);
634 kfree_skb(skb);
635 lbs_notify_command_response(priv, i);
637 spin_unlock(&priv->driver_lock);
639 lbs_deb_usbd(&cardp->udev->dev,
640 "Wake up main thread to handle cmd response\n");
644 * @brief This function reads of the packet into the upload buff,
645 * wake up the main thread and initialise the Rx callack.
647 * @param urb pointer to struct urb
648 * @return N/A
650 static void if_usb_receive(struct urb *urb)
652 struct if_usb_card *cardp = urb->context;
653 struct sk_buff *skb = cardp->rx_skb;
654 struct lbs_private *priv = cardp->priv;
655 int recvlength = urb->actual_length;
656 uint8_t *recvbuff = NULL;
657 uint32_t recvtype = 0;
658 __le32 *pkt = (__le32 *)(skb->data + IPFIELD_ALIGN_OFFSET);
659 uint32_t event;
661 lbs_deb_enter(LBS_DEB_USB);
663 if (recvlength) {
664 if (urb->status) {
665 lbs_deb_usbd(&cardp->udev->dev, "RX URB failed: %d\n",
666 urb->status);
667 kfree_skb(skb);
668 goto setup_for_next;
671 recvbuff = skb->data + IPFIELD_ALIGN_OFFSET;
672 recvtype = le32_to_cpu(pkt[0]);
673 lbs_deb_usbd(&cardp->udev->dev,
674 "Recv length = 0x%x, Recv type = 0x%X\n",
675 recvlength, recvtype);
676 } else if (urb->status) {
677 kfree_skb(skb);
678 goto rx_exit;
681 switch (recvtype) {
682 case CMD_TYPE_DATA:
683 process_cmdtypedata(recvlength, skb, cardp, priv);
684 break;
686 case CMD_TYPE_REQUEST:
687 process_cmdrequest(recvlength, recvbuff, skb, cardp, priv);
688 break;
690 case CMD_TYPE_INDICATION:
691 /* Event handling */
692 event = le32_to_cpu(pkt[1]);
693 lbs_deb_usbd(&cardp->udev->dev, "**EVENT** 0x%X\n", event);
694 kfree_skb(skb);
696 /* Icky undocumented magic special case */
697 if (event & 0xffff0000) {
698 u32 trycount = (event & 0xffff0000) >> 16;
700 lbs_send_tx_feedback(priv, trycount);
701 } else
702 lbs_queue_event(priv, event & 0xFF);
703 break;
705 default:
706 lbs_deb_usbd(&cardp->udev->dev, "Unknown command type 0x%X\n",
707 recvtype);
708 kfree_skb(skb);
709 break;
712 setup_for_next:
713 if_usb_submit_rx_urb(cardp);
714 rx_exit:
715 lbs_deb_leave(LBS_DEB_USB);
719 * @brief This function downloads data to FW
720 * @param priv pointer to struct lbs_private structure
721 * @param type type of data
722 * @param buf pointer to data buffer
723 * @param len number of bytes
724 * @return 0 or -1
726 static int if_usb_host_to_card(struct lbs_private *priv, uint8_t type,
727 uint8_t *payload, uint16_t nb)
729 struct if_usb_card *cardp = priv->card;
731 lbs_deb_usbd(&cardp->udev->dev,"*** type = %u\n", type);
732 lbs_deb_usbd(&cardp->udev->dev,"size after = %d\n", nb);
734 if (type == MVMS_CMD) {
735 *(__le32 *)cardp->ep_out_buf = cpu_to_le32(CMD_TYPE_REQUEST);
736 priv->dnld_sent = DNLD_CMD_SENT;
737 } else {
738 *(__le32 *)cardp->ep_out_buf = cpu_to_le32(CMD_TYPE_DATA);
739 priv->dnld_sent = DNLD_DATA_SENT;
742 memcpy((cardp->ep_out_buf + MESSAGE_HEADER_LEN), payload, nb);
744 return usb_tx_block(cardp, cardp->ep_out_buf, nb + MESSAGE_HEADER_LEN);
748 * @brief This function issues Boot command to the Boot2 code
749 * @param ivalue 1:Boot from FW by USB-Download
750 * 2:Boot from FW in EEPROM
751 * @return 0
753 static int if_usb_issue_boot_command(struct if_usb_card *cardp, int ivalue)
755 struct bootcmd *bootcmd = cardp->ep_out_buf;
757 /* Prepare command */
758 bootcmd->magic = cpu_to_le32(BOOT_CMD_MAGIC_NUMBER);
759 bootcmd->cmd = ivalue;
760 memset(bootcmd->pad, 0, sizeof(bootcmd->pad));
762 /* Issue command */
763 usb_tx_block(cardp, cardp->ep_out_buf, sizeof(*bootcmd));
765 return 0;
770 * @brief This function checks the validity of Boot2/FW image.
772 * @param data pointer to image
773 * len image length
774 * @return 0 or -1
776 static int check_fwfile_format(const uint8_t *data, uint32_t totlen)
778 uint32_t bincmd, exit;
779 uint32_t blksize, offset, len;
780 int ret;
782 ret = 1;
783 exit = len = 0;
785 do {
786 struct fwheader *fwh = (void *)data;
788 bincmd = le32_to_cpu(fwh->dnldcmd);
789 blksize = le32_to_cpu(fwh->datalength);
790 switch (bincmd) {
791 case FW_HAS_DATA_TO_RECV:
792 offset = sizeof(struct fwheader) + blksize;
793 data += offset;
794 len += offset;
795 if (len >= totlen)
796 exit = 1;
797 break;
798 case FW_HAS_LAST_BLOCK:
799 exit = 1;
800 ret = 0;
801 break;
802 default:
803 exit = 1;
804 break;
806 } while (!exit);
808 if (ret)
809 lbs_pr_err("firmware file format check FAIL\n");
810 else
811 lbs_deb_fw("firmware file format check PASS\n");
813 return ret;
818 * @brief This function programs the firmware subject to cmd
820 * @param cardp the if_usb_card descriptor
821 * fwname firmware or boot2 image file name
822 * cmd either BOOT_CMD_FW_BY_USB, BOOT_CMD_UPDATE_FW,
823 * or BOOT_CMD_UPDATE_BOOT2.
824 * @return 0 or error code
826 static int if_usb_prog_firmware(struct if_usb_card *cardp,
827 const char *fwname, int cmd)
829 struct lbs_private *priv = cardp->priv;
830 unsigned long flags, caps;
831 int ret;
833 caps = priv->fwcapinfo;
834 if (((cmd == BOOT_CMD_UPDATE_FW) && !(caps & FW_CAPINFO_FIRMWARE_UPGRADE)) ||
835 ((cmd == BOOT_CMD_UPDATE_BOOT2) && !(caps & FW_CAPINFO_BOOT2_UPGRADE)))
836 return -EOPNOTSUPP;
838 /* Ensure main thread is idle. */
839 spin_lock_irqsave(&priv->driver_lock, flags);
840 while (priv->cur_cmd != NULL || priv->dnld_sent != DNLD_RES_RECEIVED) {
841 spin_unlock_irqrestore(&priv->driver_lock, flags);
842 if (wait_event_interruptible(priv->waitq,
843 (priv->cur_cmd == NULL &&
844 priv->dnld_sent == DNLD_RES_RECEIVED))) {
845 return -ERESTARTSYS;
847 spin_lock_irqsave(&priv->driver_lock, flags);
849 priv->dnld_sent = DNLD_BOOTCMD_SENT;
850 spin_unlock_irqrestore(&priv->driver_lock, flags);
852 ret = __if_usb_prog_firmware(cardp, fwname, cmd);
854 spin_lock_irqsave(&priv->driver_lock, flags);
855 priv->dnld_sent = DNLD_RES_RECEIVED;
856 spin_unlock_irqrestore(&priv->driver_lock, flags);
858 wake_up_interruptible(&priv->waitq);
860 return ret;
863 static int __if_usb_prog_firmware(struct if_usb_card *cardp,
864 const char *fwname, int cmd)
866 int i = 0;
867 static int reset_count = 10;
868 int ret = 0;
870 lbs_deb_enter(LBS_DEB_USB);
872 ret = request_firmware(&cardp->fw, fwname, &cardp->udev->dev);
873 if (ret < 0) {
874 lbs_pr_err("request_firmware() failed with %#x\n", ret);
875 lbs_pr_err("firmware %s not found\n", fwname);
876 goto done;
879 if (check_fwfile_format(cardp->fw->data, cardp->fw->size)) {
880 ret = -EINVAL;
881 goto release_fw;
884 /* Cancel any pending usb business */
885 usb_kill_urb(cardp->rx_urb);
886 usb_kill_urb(cardp->tx_urb);
888 cardp->fwlastblksent = 0;
889 cardp->fwdnldover = 0;
890 cardp->totalbytes = 0;
891 cardp->fwfinalblk = 0;
892 cardp->bootcmdresp = 0;
894 restart:
895 if (if_usb_submit_rx_urb_fwload(cardp) < 0) {
896 lbs_deb_usbd(&cardp->udev->dev, "URB submission is failed\n");
897 ret = -EIO;
898 goto release_fw;
901 cardp->bootcmdresp = 0;
902 do {
903 int j = 0;
904 i++;
905 if_usb_issue_boot_command(cardp, cmd);
906 /* wait for command response */
907 do {
908 j++;
909 msleep_interruptible(100);
910 } while (cardp->bootcmdresp == 0 && j < 10);
911 } while (cardp->bootcmdresp == 0 && i < 5);
913 if (cardp->bootcmdresp == BOOT_CMD_RESP_NOT_SUPPORTED) {
914 /* Return to normal operation */
915 ret = -EOPNOTSUPP;
916 usb_kill_urb(cardp->rx_urb);
917 usb_kill_urb(cardp->tx_urb);
918 if (if_usb_submit_rx_urb(cardp) < 0)
919 ret = -EIO;
920 goto release_fw;
921 } else if (cardp->bootcmdresp <= 0) {
922 if (--reset_count >= 0) {
923 if_usb_reset_device(cardp);
924 goto restart;
926 ret = -EIO;
927 goto release_fw;
930 i = 0;
932 cardp->totalbytes = 0;
933 cardp->fwlastblksent = 0;
934 cardp->CRC_OK = 1;
935 cardp->fwdnldover = 0;
936 cardp->fwseqnum = -1;
937 cardp->totalbytes = 0;
938 cardp->fwfinalblk = 0;
940 /* Send the first firmware packet... */
941 if_usb_send_fw_pkt(cardp);
943 /* ... and wait for the process to complete */
944 wait_event_interruptible(cardp->fw_wq, cardp->surprise_removed || cardp->fwdnldover);
946 del_timer_sync(&cardp->fw_timeout);
947 usb_kill_urb(cardp->rx_urb);
949 if (!cardp->fwdnldover) {
950 lbs_pr_info("failed to load fw, resetting device!\n");
951 if (--reset_count >= 0) {
952 if_usb_reset_device(cardp);
953 goto restart;
956 lbs_pr_info("FW download failure, time = %d ms\n", i * 100);
957 ret = -EIO;
958 goto release_fw;
961 release_fw:
962 release_firmware(cardp->fw);
963 cardp->fw = NULL;
965 done:
966 lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
967 return ret;
971 #ifdef CONFIG_PM
972 static int if_usb_suspend(struct usb_interface *intf, pm_message_t message)
974 struct if_usb_card *cardp = usb_get_intfdata(intf);
975 struct lbs_private *priv = cardp->priv;
976 int ret;
978 lbs_deb_enter(LBS_DEB_USB);
980 if (priv->psstate != PS_STATE_FULL_POWER)
981 return -1;
983 ret = lbs_suspend(priv);
984 if (ret)
985 goto out;
987 /* Unlink tx & rx urb */
988 usb_kill_urb(cardp->tx_urb);
989 usb_kill_urb(cardp->rx_urb);
991 out:
992 lbs_deb_leave(LBS_DEB_USB);
993 return ret;
996 static int if_usb_resume(struct usb_interface *intf)
998 struct if_usb_card *cardp = usb_get_intfdata(intf);
999 struct lbs_private *priv = cardp->priv;
1001 lbs_deb_enter(LBS_DEB_USB);
1003 if_usb_submit_rx_urb(cardp);
1005 lbs_resume(priv);
1007 lbs_deb_leave(LBS_DEB_USB);
1008 return 0;
1010 #else
1011 #define if_usb_suspend NULL
1012 #define if_usb_resume NULL
1013 #endif
1015 static struct usb_driver if_usb_driver = {
1016 .name = DRV_NAME,
1017 .probe = if_usb_probe,
1018 .disconnect = if_usb_disconnect,
1019 .id_table = if_usb_table,
1020 .suspend = if_usb_suspend,
1021 .resume = if_usb_resume,
1022 .reset_resume = if_usb_resume,
1025 static int __init if_usb_init_module(void)
1027 int ret = 0;
1029 lbs_deb_enter(LBS_DEB_MAIN);
1031 ret = usb_register(&if_usb_driver);
1033 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1034 return ret;
1037 static void __exit if_usb_exit_module(void)
1039 lbs_deb_enter(LBS_DEB_MAIN);
1041 usb_deregister(&if_usb_driver);
1043 lbs_deb_leave(LBS_DEB_MAIN);
1046 module_init(if_usb_init_module);
1047 module_exit(if_usb_exit_module);
1049 MODULE_DESCRIPTION("8388 USB WLAN Driver");
1050 MODULE_AUTHOR("Marvell International Ltd. and Red Hat, Inc.");
1051 MODULE_LICENSE("GPL");