2 * Copyright (C) 2008, cozybit Inc.
3 * Copyright (C) 2003-2006, Marvell International Ltd.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
10 #define DRV_NAME "lbtf_usb"
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include "libertas_tf.h"
17 #include <linux/delay.h>
18 #include <linux/module.h>
19 #include <linux/firmware.h>
20 #include <linux/netdevice.h>
21 #include <linux/slab.h>
22 #include <linux/usb.h>
25 #define lbtf_deb_usb2(...) do { if (INSANEDEBUG) lbtf_deb_usbd(__VA_ARGS__); } while (0)
27 #define MESSAGE_HEADER_LEN 4
29 static char *lbtf_fw_name = "/*(DEBLOBBED)*/";
30 module_param_named(fw_name, lbtf_fw_name, charp, 0644);
34 static struct usb_device_id if_usb_table[] = {
35 /* Enter the device signature inside */
36 { USB_DEVICE(0x1286, 0x2001) },
37 { USB_DEVICE(0x05a3, 0x8388) },
38 {} /* Terminating entry */
41 MODULE_DEVICE_TABLE(usb, if_usb_table);
43 static void if_usb_receive(struct urb *urb);
44 static void if_usb_receive_fwload(struct urb *urb);
45 static int if_usb_prog_firmware(struct if_usb_card *cardp);
46 static int if_usb_host_to_card(struct lbtf_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, u8 data);
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);
55 * if_usb_wrike_bulk_callback - call back to handle URB status
57 * @param urb pointer to urb structure
59 static void if_usb_write_bulk_callback(struct urb *urb)
61 if (urb->status != 0) {
62 /* print the failure status number for debug */
63 pr_info("URB in failure status: %d\n", urb->status);
65 lbtf_deb_usb2(&urb->dev->dev, "URB status is successful\n");
66 lbtf_deb_usb2(&urb->dev->dev, "Actual length transmitted %d\n",
72 * if_usb_free - free tx/rx urb, skb and rx buffer
74 * @param cardp pointer if_usb_card
76 static void if_usb_free(struct if_usb_card *cardp)
78 lbtf_deb_enter(LBTF_DEB_USB);
80 /* Unlink tx & rx urb */
81 usb_kill_urb(cardp->tx_urb);
82 usb_kill_urb(cardp->rx_urb);
83 usb_kill_urb(cardp->cmd_urb);
85 usb_free_urb(cardp->tx_urb);
88 usb_free_urb(cardp->rx_urb);
91 usb_free_urb(cardp->cmd_urb);
92 cardp->cmd_urb = NULL;
94 kfree(cardp->ep_out_buf);
95 cardp->ep_out_buf = NULL;
97 lbtf_deb_leave(LBTF_DEB_USB);
100 static void if_usb_setup_firmware(struct lbtf_private *priv)
102 struct if_usb_card *cardp = priv->card;
103 struct cmd_ds_set_boot2_ver b2_cmd;
105 lbtf_deb_enter(LBTF_DEB_USB);
107 if_usb_submit_rx_urb(cardp);
108 b2_cmd.hdr.size = cpu_to_le16(sizeof(b2_cmd));
110 b2_cmd.version = cardp->boot2_version;
112 if (lbtf_cmd_with_response(priv, CMD_SET_BOOT2_VER, &b2_cmd))
113 lbtf_deb_usb("Setting boot2 version failed\n");
115 lbtf_deb_leave(LBTF_DEB_USB);
118 static void if_usb_fw_timeo(unsigned long priv)
120 struct if_usb_card *cardp = (void *)priv;
122 lbtf_deb_enter(LBTF_DEB_USB);
123 if (!cardp->fwdnldover) {
124 /* Download timed out */
125 cardp->priv->surpriseremoved = 1;
126 pr_err("Download timed out\n");
128 lbtf_deb_usb("Download complete, no event. Assuming success\n");
130 wake_up(&cardp->fw_wq);
131 lbtf_deb_leave(LBTF_DEB_USB);
135 * if_usb_probe - sets the configuration values
137 * @ifnum interface number
138 * @id pointer to usb_device_id
140 * Returns: 0 on success, error code on failure
142 static int if_usb_probe(struct usb_interface *intf,
143 const struct usb_device_id *id)
145 struct usb_device *udev;
146 struct usb_host_interface *iface_desc;
147 struct usb_endpoint_descriptor *endpoint;
148 struct lbtf_private *priv;
149 struct if_usb_card *cardp;
152 lbtf_deb_enter(LBTF_DEB_USB);
153 udev = interface_to_usbdev(intf);
155 cardp = kzalloc(sizeof(struct if_usb_card), GFP_KERNEL);
159 setup_timer(&cardp->fw_timeout, if_usb_fw_timeo, (unsigned long)cardp);
160 init_waitqueue_head(&cardp->fw_wq);
163 iface_desc = intf->cur_altsetting;
165 lbtf_deb_usbd(&udev->dev, "bcdUSB = 0x%X bDeviceClass = 0x%X"
166 " bDeviceSubClass = 0x%X, bDeviceProtocol = 0x%X\n",
167 le16_to_cpu(udev->descriptor.bcdUSB),
168 udev->descriptor.bDeviceClass,
169 udev->descriptor.bDeviceSubClass,
170 udev->descriptor.bDeviceProtocol);
172 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
173 endpoint = &iface_desc->endpoint[i].desc;
174 if (usb_endpoint_is_bulk_in(endpoint)) {
176 le16_to_cpu(endpoint->wMaxPacketSize);
177 cardp->ep_in = usb_endpoint_num(endpoint);
179 lbtf_deb_usbd(&udev->dev, "in_endpoint = %d\n",
181 lbtf_deb_usbd(&udev->dev, "Bulk in size is %d\n",
183 } else if (usb_endpoint_is_bulk_out(endpoint)) {
185 le16_to_cpu(endpoint->wMaxPacketSize);
186 cardp->ep_out = usb_endpoint_num(endpoint);
188 lbtf_deb_usbd(&udev->dev, "out_endpoint = %d\n",
190 lbtf_deb_usbd(&udev->dev, "Bulk out size is %d\n",
194 if (!cardp->ep_out_size || !cardp->ep_in_size) {
195 lbtf_deb_usbd(&udev->dev, "Endpoints not found\n");
196 /* Endpoints not found */
200 cardp->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
201 if (!cardp->rx_urb) {
202 lbtf_deb_usbd(&udev->dev, "Rx URB allocation failed\n");
206 cardp->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
207 if (!cardp->tx_urb) {
208 lbtf_deb_usbd(&udev->dev, "Tx URB allocation failed\n");
212 cardp->cmd_urb = usb_alloc_urb(0, GFP_KERNEL);
213 if (!cardp->cmd_urb) {
214 lbtf_deb_usbd(&udev->dev, "Cmd URB allocation failed\n");
218 cardp->ep_out_buf = kmalloc(MRVDRV_ETH_TX_PACKET_BUFFER_SIZE,
220 if (!cardp->ep_out_buf) {
221 lbtf_deb_usbd(&udev->dev, "Could not allocate buffer\n");
225 priv = lbtf_add_card(cardp, &udev->dev);
231 priv->hw_host_to_card = if_usb_host_to_card;
232 priv->hw_prog_firmware = if_usb_prog_firmware;
233 priv->hw_reset_device = if_usb_reset_device;
234 cardp->boot2_version = udev->descriptor.bcdDevice;
237 usb_set_intfdata(intf, cardp);
245 lbtf_deb_leave(LBTF_DEB_MAIN);
250 * if_usb_disconnect - free resource and cleanup
252 * @intf USB interface structure
254 static void if_usb_disconnect(struct usb_interface *intf)
256 struct if_usb_card *cardp = usb_get_intfdata(intf);
257 struct lbtf_private *priv = cardp->priv;
259 lbtf_deb_enter(LBTF_DEB_MAIN);
261 if_usb_reset_device(cardp);
264 lbtf_remove_card(priv);
266 /* Unlink and free urb */
270 usb_set_intfdata(intf, NULL);
271 usb_put_dev(interface_to_usbdev(intf));
273 lbtf_deb_leave(LBTF_DEB_MAIN);
277 * if_usb_send_fw_pkt - This function downloads the FW
279 * @priv pointer to struct lbtf_private
283 static int if_usb_send_fw_pkt(struct if_usb_card *cardp)
285 struct fwdata *fwdata = cardp->ep_out_buf;
286 u8 *firmware = (u8 *) cardp->fw->data;
288 lbtf_deb_enter(LBTF_DEB_FW);
290 /* If we got a CRC failure on the last block, back
292 if (!cardp->CRC_OK) {
293 cardp->totalbytes = cardp->fwlastblksent;
297 lbtf_deb_usb2(&cardp->udev->dev, "totalbytes = %d\n",
300 /* struct fwdata (which we sent to the card) has an
301 extra __le32 field in between the header and the data,
302 which is not in the struct fwheader in the actual
303 firmware binary. Insert the seqnum in the middle... */
304 memcpy(&fwdata->hdr, &firmware[cardp->totalbytes],
305 sizeof(struct fwheader));
307 cardp->fwlastblksent = cardp->totalbytes;
308 cardp->totalbytes += sizeof(struct fwheader);
310 memcpy(fwdata->data, &firmware[cardp->totalbytes],
311 le32_to_cpu(fwdata->hdr.datalength));
313 lbtf_deb_usb2(&cardp->udev->dev, "Data length = %d\n",
314 le32_to_cpu(fwdata->hdr.datalength));
316 fwdata->seqnum = cpu_to_le32(++cardp->fwseqnum);
317 cardp->totalbytes += le32_to_cpu(fwdata->hdr.datalength);
319 usb_tx_block(cardp, cardp->ep_out_buf, sizeof(struct fwdata) +
320 le32_to_cpu(fwdata->hdr.datalength), 0);
322 if (fwdata->hdr.dnldcmd == cpu_to_le32(FW_HAS_DATA_TO_RECV)) {
323 lbtf_deb_usb2(&cardp->udev->dev, "There are data to follow\n");
324 lbtf_deb_usb2(&cardp->udev->dev,
325 "seqnum = %d totalbytes = %d\n",
326 cardp->fwseqnum, cardp->totalbytes);
327 } else if (fwdata->hdr.dnldcmd == cpu_to_le32(FW_HAS_LAST_BLOCK)) {
328 lbtf_deb_usb2(&cardp->udev->dev,
329 "Host has finished FW downloading\n");
330 lbtf_deb_usb2(&cardp->udev->dev, "Donwloading FW JUMP BLOCK\n");
332 /* Host has finished FW downloading
333 * Donwloading FW JUMP BLOCK
335 cardp->fwfinalblk = 1;
338 lbtf_deb_usb2(&cardp->udev->dev, "Firmware download done; size %d\n",
341 lbtf_deb_leave(LBTF_DEB_FW);
345 static int if_usb_reset_device(struct if_usb_card *cardp)
347 struct cmd_ds_802_11_reset *cmd = cardp->ep_out_buf + 4;
350 lbtf_deb_enter(LBTF_DEB_USB);
352 *(__le32 *)cardp->ep_out_buf = cpu_to_le32(CMD_TYPE_REQUEST);
354 cmd->hdr.command = cpu_to_le16(CMD_802_11_RESET);
355 cmd->hdr.size = cpu_to_le16(sizeof(struct cmd_ds_802_11_reset));
356 cmd->hdr.result = cpu_to_le16(0);
357 cmd->hdr.seqnum = cpu_to_le16(0x5a5a);
358 cmd->action = cpu_to_le16(CMD_ACT_HALT);
359 usb_tx_block(cardp, cardp->ep_out_buf,
360 4 + sizeof(struct cmd_ds_802_11_reset), 0);
363 ret = usb_reset_device(cardp->udev);
366 lbtf_deb_leave_args(LBTF_DEB_USB, "ret %d", ret);
372 * usb_tx_block - transfer data to the device
374 * @priv pointer to struct lbtf_private
375 * @payload pointer to payload data
377 * @data non-zero for data, zero for commands
379 * Returns: 0 on success, nonzero otherwise.
381 static int usb_tx_block(struct if_usb_card *cardp, uint8_t *payload,
382 uint16_t nb, u8 data)
387 lbtf_deb_enter(LBTF_DEB_USB);
388 /* check if device is removed */
389 if (cardp->priv->surpriseremoved) {
390 lbtf_deb_usbd(&cardp->udev->dev, "Device removed\n");
397 urb = cardp->cmd_urb;
399 usb_fill_bulk_urb(urb, cardp->udev,
400 usb_sndbulkpipe(cardp->udev,
402 payload, nb, if_usb_write_bulk_callback, cardp);
404 urb->transfer_flags |= URB_ZERO_PACKET;
406 if (usb_submit_urb(urb, GFP_ATOMIC)) {
407 lbtf_deb_usbd(&cardp->udev->dev,
408 "usb_submit_urb failed: %d\n", ret);
412 lbtf_deb_usb2(&cardp->udev->dev, "usb_submit_urb success\n");
417 lbtf_deb_leave(LBTF_DEB_USB);
421 static int __if_usb_submit_rx_urb(struct if_usb_card *cardp,
422 void (*callbackfn)(struct urb *urb))
427 lbtf_deb_enter(LBTF_DEB_USB);
429 skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE);
431 pr_err("No free skb\n");
432 lbtf_deb_leave(LBTF_DEB_USB);
438 /* Fill the receive configuration URB and initialise the Rx call back */
439 usb_fill_bulk_urb(cardp->rx_urb, cardp->udev,
440 usb_rcvbulkpipe(cardp->udev, cardp->ep_in),
441 skb_tail_pointer(skb),
442 MRVDRV_ETH_RX_PACKET_BUFFER_SIZE, callbackfn, cardp);
444 cardp->rx_urb->transfer_flags |= URB_ZERO_PACKET;
446 lbtf_deb_usb2(&cardp->udev->dev, "Pointer for rx_urb %p\n",
448 ret = usb_submit_urb(cardp->rx_urb, GFP_ATOMIC);
450 lbtf_deb_usbd(&cardp->udev->dev,
451 "Submit Rx URB failed: %d\n", ret);
453 cardp->rx_skb = NULL;
454 lbtf_deb_leave(LBTF_DEB_USB);
457 lbtf_deb_usb2(&cardp->udev->dev, "Submit Rx URB success\n");
458 lbtf_deb_leave(LBTF_DEB_USB);
463 static int if_usb_submit_rx_urb_fwload(struct if_usb_card *cardp)
465 return __if_usb_submit_rx_urb(cardp, &if_usb_receive_fwload);
468 static int if_usb_submit_rx_urb(struct if_usb_card *cardp)
470 return __if_usb_submit_rx_urb(cardp, &if_usb_receive);
473 static void if_usb_receive_fwload(struct urb *urb)
475 struct if_usb_card *cardp = urb->context;
476 struct sk_buff *skb = cardp->rx_skb;
477 struct fwsyncheader *syncfwheader;
478 struct bootcmdresp bcmdresp;
480 lbtf_deb_enter(LBTF_DEB_USB);
482 lbtf_deb_usbd(&cardp->udev->dev,
483 "URB status is failed during fw load\n");
485 lbtf_deb_leave(LBTF_DEB_USB);
489 if (cardp->fwdnldover) {
490 __le32 *tmp = (__le32 *)(skb->data);
492 if (tmp[0] == cpu_to_le32(CMD_TYPE_INDICATION) &&
493 tmp[1] == cpu_to_le32(MACREG_INT_CODE_FIRMWARE_READY)) {
494 /* Firmware ready event received */
495 pr_info("Firmware ready event received\n");
496 wake_up(&cardp->fw_wq);
498 lbtf_deb_usb("Waiting for confirmation; got %x %x\n",
499 le32_to_cpu(tmp[0]), le32_to_cpu(tmp[1]));
500 if_usb_submit_rx_urb_fwload(cardp);
503 lbtf_deb_leave(LBTF_DEB_USB);
506 if (cardp->bootcmdresp <= 0) {
507 memcpy(&bcmdresp, skb->data, sizeof(bcmdresp));
509 if (le16_to_cpu(cardp->udev->descriptor.bcdDevice) < 0x3106) {
511 if_usb_submit_rx_urb_fwload(cardp);
512 cardp->bootcmdresp = 1;
513 /* Received valid boot command response */
514 lbtf_deb_usbd(&cardp->udev->dev,
515 "Received valid boot command response\n");
516 lbtf_deb_leave(LBTF_DEB_USB);
519 if (bcmdresp.magic != cpu_to_le32(BOOT_CMD_MAGIC_NUMBER)) {
520 if (bcmdresp.magic == cpu_to_le32(CMD_TYPE_REQUEST) ||
521 bcmdresp.magic == cpu_to_le32(CMD_TYPE_DATA) ||
522 bcmdresp.magic == cpu_to_le32(CMD_TYPE_INDICATION)) {
523 if (!cardp->bootcmdresp)
524 pr_info("Firmware already seems alive; resetting\n");
525 cardp->bootcmdresp = -1;
527 pr_info("boot cmd response wrong magic number (0x%x)\n",
528 le32_to_cpu(bcmdresp.magic));
530 } else if (bcmdresp.cmd != BOOT_CMD_FW_BY_USB) {
531 pr_info("boot cmd response cmd_tag error (%d)\n",
533 } else if (bcmdresp.result != BOOT_CMD_RESP_OK) {
534 pr_info("boot cmd response result error (%d)\n",
537 cardp->bootcmdresp = 1;
538 lbtf_deb_usbd(&cardp->udev->dev,
539 "Received valid boot command response\n");
543 if_usb_submit_rx_urb_fwload(cardp);
544 lbtf_deb_leave(LBTF_DEB_USB);
548 syncfwheader = kmemdup(skb->data, sizeof(struct fwsyncheader),
551 lbtf_deb_usbd(&cardp->udev->dev,
552 "Failure to allocate syncfwheader\n");
554 lbtf_deb_leave(LBTF_DEB_USB);
558 if (!syncfwheader->cmd) {
559 lbtf_deb_usb2(&cardp->udev->dev,
560 "FW received Blk with correct CRC\n");
561 lbtf_deb_usb2(&cardp->udev->dev,
562 "FW received Blk seqnum = %d\n",
563 le32_to_cpu(syncfwheader->seqnum));
566 lbtf_deb_usbd(&cardp->udev->dev,
567 "FW received Blk with CRC error\n");
573 /* reschedule timer for 200ms hence */
574 mod_timer(&cardp->fw_timeout, jiffies + (HZ/5));
576 if (cardp->fwfinalblk) {
577 cardp->fwdnldover = 1;
581 if_usb_send_fw_pkt(cardp);
584 if_usb_submit_rx_urb_fwload(cardp);
588 lbtf_deb_leave(LBTF_DEB_USB);
591 #define MRVDRV_MIN_PKT_LEN 30
593 static inline void process_cmdtypedata(int recvlength, struct sk_buff *skb,
594 struct if_usb_card *cardp,
595 struct lbtf_private *priv)
597 if (recvlength > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + MESSAGE_HEADER_LEN
598 || recvlength < MRVDRV_MIN_PKT_LEN) {
599 lbtf_deb_usbd(&cardp->udev->dev, "Packet length is Invalid\n");
604 skb_put(skb, recvlength);
605 skb_pull(skb, MESSAGE_HEADER_LEN);
609 static inline void process_cmdrequest(int recvlength, uint8_t *recvbuff,
611 struct if_usb_card *cardp,
612 struct lbtf_private *priv)
614 if (recvlength > LBS_CMD_BUFFER_SIZE) {
615 lbtf_deb_usbd(&cardp->udev->dev,
616 "The receive buffer is too large\n");
621 BUG_ON(!in_interrupt());
623 spin_lock(&priv->driver_lock);
624 memcpy(priv->cmd_resp_buff, recvbuff + MESSAGE_HEADER_LEN,
625 recvlength - MESSAGE_HEADER_LEN);
627 lbtf_cmd_response_rx(priv);
628 spin_unlock(&priv->driver_lock);
632 * if_usb_receive - read data received from the device.
634 * @urb pointer to struct urb
636 static void if_usb_receive(struct urb *urb)
638 struct if_usb_card *cardp = urb->context;
639 struct sk_buff *skb = cardp->rx_skb;
640 struct lbtf_private *priv = cardp->priv;
641 int recvlength = urb->actual_length;
642 uint8_t *recvbuff = NULL;
643 uint32_t recvtype = 0;
644 __le32 *pkt = (__le32 *) skb->data;
646 lbtf_deb_enter(LBTF_DEB_USB);
650 lbtf_deb_usbd(&cardp->udev->dev, "RX URB failed: %d\n",
656 recvbuff = skb->data;
657 recvtype = le32_to_cpu(pkt[0]);
658 lbtf_deb_usbd(&cardp->udev->dev,
659 "Recv length = 0x%x, Recv type = 0x%X\n",
660 recvlength, recvtype);
661 } else if (urb->status) {
663 lbtf_deb_leave(LBTF_DEB_USB);
669 process_cmdtypedata(recvlength, skb, cardp, priv);
672 case CMD_TYPE_REQUEST:
673 process_cmdrequest(recvlength, recvbuff, skb, cardp, priv);
676 case CMD_TYPE_INDICATION:
678 /* Event cause handling */
679 u32 event_cause = le32_to_cpu(pkt[1]);
680 lbtf_deb_usbd(&cardp->udev->dev, "**EVENT** 0x%X\n",
683 /* Icky undocumented magic special case */
684 if (event_cause & 0xffff0000) {
689 tmp = event_cause >> 16;
690 retrycnt = tmp & 0x00ff;
691 failure = (tmp & 0xff00) >> 8;
692 lbtf_send_tx_feedback(priv, retrycnt, failure);
693 } else if (event_cause == LBTF_EVENT_BCN_SENT)
696 lbtf_deb_usbd(&cardp->udev->dev,
697 "Unsupported notification %d received\n",
703 lbtf_deb_usbd(&cardp->udev->dev,
704 "libertastf: unknown command type 0x%X\n", recvtype);
710 if_usb_submit_rx_urb(cardp);
711 lbtf_deb_leave(LBTF_DEB_USB);
715 * if_usb_host_to_card - Download data to the device
717 * @priv pointer to struct lbtf_private structure
719 * @buf pointer to data buffer
720 * @len number of bytes
722 * Returns: 0 on success, nonzero otherwise
724 static int if_usb_host_to_card(struct lbtf_private *priv, uint8_t type,
725 uint8_t *payload, uint16_t nb)
727 struct if_usb_card *cardp = priv->card;
730 lbtf_deb_usbd(&cardp->udev->dev, "*** type = %u\n", type);
731 lbtf_deb_usbd(&cardp->udev->dev, "size after = %d\n", nb);
733 if (type == MVMS_CMD) {
734 *(__le32 *)cardp->ep_out_buf = cpu_to_le32(CMD_TYPE_REQUEST);
736 *(__le32 *)cardp->ep_out_buf = cpu_to_le32(CMD_TYPE_DATA);
740 memcpy((cardp->ep_out_buf + MESSAGE_HEADER_LEN), payload, nb);
742 return usb_tx_block(cardp, cardp->ep_out_buf, nb + MESSAGE_HEADER_LEN,
747 * if_usb_issue_boot_command - Issue boot command to Boot2.
749 * @ivalue 1 boots from FW by USB-Download, 2 boots from FW in EEPROM.
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));
763 usb_tx_block(cardp, cardp->ep_out_buf, sizeof(*bootcmd), 0);
770 * check_fwfile_format - Check the validity of Boot2/FW image.
772 * @data pointer to image
773 * @totlen image length
775 * Returns: 0 if the image is valid, nonzero otherwise.
777 static int check_fwfile_format(const u8 *data, u32 totlen)
780 u32 blksize, offset, len;
787 struct fwheader *fwh = (void *) data;
789 bincmd = le32_to_cpu(fwh->dnldcmd);
790 blksize = le32_to_cpu(fwh->datalength);
792 case FW_HAS_DATA_TO_RECV:
793 offset = sizeof(struct fwheader) + blksize;
799 case FW_HAS_LAST_BLOCK:
810 pr_err("firmware file format check FAIL\n");
812 lbtf_deb_fw("firmware file format check PASS\n");
818 static int if_usb_prog_firmware(struct if_usb_card *cardp)
821 static int reset_count = 10;
824 lbtf_deb_enter(LBTF_DEB_USB);
826 kernel_param_lock(THIS_MODULE);
827 ret = reject_firmware(&cardp->fw, lbtf_fw_name, &cardp->udev->dev);
829 pr_err("reject_firmware() failed with %#x\n", ret);
830 pr_err("firmware %s not found\n", lbtf_fw_name);
831 kernel_param_unlock(THIS_MODULE);
834 kernel_param_unlock(THIS_MODULE);
836 if (check_fwfile_format(cardp->fw->data, cardp->fw->size))
840 if (if_usb_submit_rx_urb_fwload(cardp) < 0) {
841 lbtf_deb_usbd(&cardp->udev->dev, "URB submission is failed\n");
846 cardp->bootcmdresp = 0;
850 /* Issue Boot command = 1, Boot from Download-FW */
851 if_usb_issue_boot_command(cardp, BOOT_CMD_FW_BY_USB);
852 /* wait for command response */
855 msleep_interruptible(100);
856 } while (cardp->bootcmdresp == 0 && j < 10);
857 } while (cardp->bootcmdresp == 0 && i < 5);
859 if (cardp->bootcmdresp <= 0) {
860 if (--reset_count >= 0) {
861 if_usb_reset_device(cardp);
869 cardp->totalbytes = 0;
870 cardp->fwlastblksent = 0;
872 cardp->fwdnldover = 0;
873 cardp->fwseqnum = -1;
874 cardp->totalbytes = 0;
875 cardp->fwfinalblk = 0;
877 /* Send the first firmware packet... */
878 if_usb_send_fw_pkt(cardp);
880 /* ... and wait for the process to complete */
881 wait_event_interruptible(cardp->fw_wq, cardp->priv->surpriseremoved ||
884 del_timer_sync(&cardp->fw_timeout);
885 usb_kill_urb(cardp->rx_urb);
887 if (!cardp->fwdnldover) {
888 pr_info("failed to load fw, resetting device!\n");
889 if (--reset_count >= 0) {
890 if_usb_reset_device(cardp);
894 pr_info("FW download failure, time = %d ms\n", i * 100);
899 cardp->priv->fw_ready = 1;
902 release_firmware(cardp->fw);
905 if_usb_setup_firmware(cardp->priv);
908 lbtf_deb_leave_args(LBTF_DEB_USB, "ret %d", ret);
913 #define if_usb_suspend NULL
914 #define if_usb_resume NULL
916 static struct usb_driver if_usb_driver = {
918 .probe = if_usb_probe,
919 .disconnect = if_usb_disconnect,
920 .id_table = if_usb_table,
921 .suspend = if_usb_suspend,
922 .resume = if_usb_resume,
923 .disable_hub_initiated_lpm = 1,
926 module_usb_driver(if_usb_driver);
928 MODULE_DESCRIPTION("8388 USB WLAN Thinfirm Driver");
929 MODULE_AUTHOR("Cozybit Inc.");
930 MODULE_LICENSE("GPL");