Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * This file contains functions used in USB interface module.
0004  */
0005 
0006 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0007 
0008 #include <linux/delay.h>
0009 #include <linux/module.h>
0010 #include <linux/firmware.h>
0011 #include <linux/netdevice.h>
0012 #include <linux/slab.h>
0013 #include <linux/usb.h>
0014 #include <linux/olpc-ec.h>
0015 
0016 #ifdef CONFIG_OLPC
0017 #include <asm/olpc.h>
0018 #endif
0019 
0020 #define DRV_NAME "usb8xxx"
0021 
0022 #include "host.h"
0023 #include "decl.h"
0024 #include "defs.h"
0025 #include "dev.h"
0026 #include "cmd.h"
0027 #include "if_usb.h"
0028 
0029 #define INSANEDEBUG 0
0030 #define lbs_deb_usb2(...) do { if (INSANEDEBUG) lbs_deb_usbd(__VA_ARGS__); } while (0)
0031 
0032 #define MESSAGE_HEADER_LEN  4
0033 
0034 MODULE_FIRMWARE("libertas/usb8388_v9.bin");
0035 MODULE_FIRMWARE("libertas/usb8388_v5.bin");
0036 MODULE_FIRMWARE("libertas/usb8388.bin");
0037 MODULE_FIRMWARE("libertas/usb8682.bin");
0038 MODULE_FIRMWARE("usb8388.bin");
0039 
0040 enum {
0041     MODEL_UNKNOWN = 0x0,
0042     MODEL_8388 = 0x1,
0043     MODEL_8682 = 0x2
0044 };
0045 
0046 /* table of firmware file names */
0047 static const struct lbs_fw_table fw_table[] = {
0048     { MODEL_8388, "libertas/usb8388_olpc.bin", NULL },
0049     { MODEL_8388, "libertas/usb8388_v9.bin", NULL },
0050     { MODEL_8388, "libertas/usb8388_v5.bin", NULL },
0051     { MODEL_8388, "libertas/usb8388.bin", NULL },
0052     { MODEL_8388, "usb8388.bin", NULL },
0053     { MODEL_8682, "libertas/usb8682.bin", NULL },
0054     { 0, NULL, NULL }
0055 };
0056 
0057 static const struct usb_device_id if_usb_table[] = {
0058     /* Enter the device signature inside */
0059     { USB_DEVICE(0x1286, 0x2001), .driver_info = MODEL_8388 },
0060     { USB_DEVICE(0x05a3, 0x8388), .driver_info = MODEL_8388 },
0061     {}  /* Terminating entry */
0062 };
0063 
0064 MODULE_DEVICE_TABLE(usb, if_usb_table);
0065 
0066 static void if_usb_receive(struct urb *urb);
0067 static void if_usb_receive_fwload(struct urb *urb);
0068 static void if_usb_prog_firmware(struct lbs_private *priv, int ret,
0069                  const struct firmware *fw,
0070                  const struct firmware *unused);
0071 static int if_usb_host_to_card(struct lbs_private *priv, uint8_t type,
0072                    uint8_t *payload, uint16_t nb);
0073 static int usb_tx_block(struct if_usb_card *cardp, uint8_t *payload,
0074             uint16_t nb);
0075 static void if_usb_free(struct if_usb_card *cardp);
0076 static int if_usb_submit_rx_urb(struct if_usb_card *cardp);
0077 static int if_usb_reset_device(struct if_usb_card *cardp);
0078 
0079 /**
0080  * if_usb_write_bulk_callback - callback function to handle the status
0081  * of the URB
0082  * @urb:    pointer to &urb structure
0083  * returns: N/A
0084  */
0085 static void if_usb_write_bulk_callback(struct urb *urb)
0086 {
0087     struct if_usb_card *cardp = (struct if_usb_card *) urb->context;
0088 
0089     /* handle the transmission complete validations */
0090 
0091     if (urb->status == 0) {
0092         struct lbs_private *priv = cardp->priv;
0093 
0094         lbs_deb_usb2(&urb->dev->dev, "URB status is successful\n");
0095         lbs_deb_usb2(&urb->dev->dev, "Actual length transmitted %d\n",
0096                  urb->actual_length);
0097 
0098         /* Boot commands such as UPDATE_FW and UPDATE_BOOT2 are not
0099          * passed up to the lbs level.
0100          */
0101         if (priv && priv->dnld_sent != DNLD_BOOTCMD_SENT)
0102             lbs_host_to_card_done(priv);
0103     } else {
0104         /* print the failure status number for debug */
0105         pr_info("URB in failure status: %d\n", urb->status);
0106     }
0107 }
0108 
0109 /**
0110  * if_usb_free - free tx/rx urb, skb and rx buffer
0111  * @cardp:  pointer to &if_usb_card
0112  * returns: N/A
0113  */
0114 static void if_usb_free(struct if_usb_card *cardp)
0115 {
0116     /* Unlink tx & rx urb */
0117     usb_kill_urb(cardp->tx_urb);
0118     usb_kill_urb(cardp->rx_urb);
0119 
0120     usb_free_urb(cardp->tx_urb);
0121     cardp->tx_urb = NULL;
0122 
0123     usb_free_urb(cardp->rx_urb);
0124     cardp->rx_urb = NULL;
0125 
0126     kfree(cardp->ep_out_buf);
0127     cardp->ep_out_buf = NULL;
0128 }
0129 
0130 static void if_usb_setup_firmware(struct lbs_private *priv)
0131 {
0132     struct if_usb_card *cardp = priv->card;
0133     struct cmd_ds_set_boot2_ver b2_cmd;
0134     struct cmd_ds_802_11_fw_wake_method wake_method;
0135 
0136     b2_cmd.hdr.size = cpu_to_le16(sizeof(b2_cmd));
0137     b2_cmd.action = 0;
0138     b2_cmd.version = cardp->boot2_version;
0139 
0140     if (lbs_cmd_with_response(priv, CMD_SET_BOOT2_VER, &b2_cmd))
0141         lbs_deb_usb("Setting boot2 version failed\n");
0142 
0143     priv->wol_gpio = 2; /* Wake via GPIO2... */
0144     priv->wol_gap = 20; /* ... after 20ms    */
0145     lbs_host_sleep_cfg(priv, EHS_WAKE_ON_UNICAST_DATA,
0146             (struct wol_config *) NULL);
0147 
0148     wake_method.hdr.size = cpu_to_le16(sizeof(wake_method));
0149     wake_method.action = cpu_to_le16(CMD_ACT_GET);
0150     if (lbs_cmd_with_response(priv, CMD_802_11_FW_WAKE_METHOD, &wake_method)) {
0151         netdev_info(priv->dev, "Firmware does not seem to support PS mode\n");
0152         priv->fwcapinfo &= ~FW_CAPINFO_PS;
0153     } else {
0154         if (le16_to_cpu(wake_method.method) == CMD_WAKE_METHOD_COMMAND_INT) {
0155             lbs_deb_usb("Firmware seems to support PS with wake-via-command\n");
0156         } else {
0157             /* The versions which boot up this way don't seem to
0158                work even if we set it to the command interrupt */
0159             priv->fwcapinfo &= ~FW_CAPINFO_PS;
0160             netdev_info(priv->dev,
0161                     "Firmware doesn't wake via command interrupt; disabling PS mode\n");
0162         }
0163     }
0164 }
0165 
0166 static void if_usb_fw_timeo(struct timer_list *t)
0167 {
0168     struct if_usb_card *cardp = from_timer(cardp, t, fw_timeout);
0169 
0170     if (cardp->fwdnldover) {
0171         lbs_deb_usb("Download complete, no event. Assuming success\n");
0172     } else {
0173         pr_err("Download timed out\n");
0174         cardp->surprise_removed = 1;
0175     }
0176     wake_up(&cardp->fw_wq);
0177 }
0178 
0179 #ifdef CONFIG_OLPC
0180 static void if_usb_reset_olpc_card(struct lbs_private *priv)
0181 {
0182     printk(KERN_CRIT "Resetting OLPC wireless via EC...\n");
0183     olpc_ec_cmd(0x25, NULL, 0, NULL, 0);
0184 }
0185 #endif
0186 
0187 /**
0188  * if_usb_probe - sets the configuration values
0189  * @intf:   &usb_interface pointer
0190  * @id: pointer to usb_device_id
0191  * returns: 0 on success, error code on failure
0192  */
0193 static int if_usb_probe(struct usb_interface *intf,
0194             const struct usb_device_id *id)
0195 {
0196     struct usb_device *udev;
0197     struct usb_host_interface *iface_desc;
0198     struct usb_endpoint_descriptor *endpoint;
0199     struct lbs_private *priv;
0200     struct if_usb_card *cardp;
0201     int r = -ENOMEM;
0202     int i;
0203 
0204     udev = interface_to_usbdev(intf);
0205 
0206     cardp = kzalloc(sizeof(struct if_usb_card), GFP_KERNEL);
0207     if (!cardp)
0208         goto error;
0209 
0210     timer_setup(&cardp->fw_timeout, if_usb_fw_timeo, 0);
0211     init_waitqueue_head(&cardp->fw_wq);
0212 
0213     cardp->udev = udev;
0214     cardp->model = (uint32_t) id->driver_info;
0215     iface_desc = intf->cur_altsetting;
0216 
0217     lbs_deb_usbd(&udev->dev, "bcdUSB = 0x%X bDeviceClass = 0x%X"
0218              " bDeviceSubClass = 0x%X, bDeviceProtocol = 0x%X\n",
0219              le16_to_cpu(udev->descriptor.bcdUSB),
0220              udev->descriptor.bDeviceClass,
0221              udev->descriptor.bDeviceSubClass,
0222              udev->descriptor.bDeviceProtocol);
0223 
0224     for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
0225         endpoint = &iface_desc->endpoint[i].desc;
0226         if (usb_endpoint_is_bulk_in(endpoint)) {
0227             cardp->ep_in_size = le16_to_cpu(endpoint->wMaxPacketSize);
0228             cardp->ep_in = usb_endpoint_num(endpoint);
0229 
0230             lbs_deb_usbd(&udev->dev, "in_endpoint = %d\n", cardp->ep_in);
0231             lbs_deb_usbd(&udev->dev, "Bulk in size is %d\n", cardp->ep_in_size);
0232 
0233         } else if (usb_endpoint_is_bulk_out(endpoint)) {
0234             cardp->ep_out_size = le16_to_cpu(endpoint->wMaxPacketSize);
0235             cardp->ep_out = usb_endpoint_num(endpoint);
0236 
0237             lbs_deb_usbd(&udev->dev, "out_endpoint = %d\n", cardp->ep_out);
0238             lbs_deb_usbd(&udev->dev, "Bulk out size is %d\n", cardp->ep_out_size);
0239         }
0240     }
0241     if (!cardp->ep_out_size || !cardp->ep_in_size) {
0242         lbs_deb_usbd(&udev->dev, "Endpoints not found\n");
0243         goto dealloc;
0244     }
0245     if (!(cardp->rx_urb = usb_alloc_urb(0, GFP_KERNEL))) {
0246         lbs_deb_usbd(&udev->dev, "Rx URB allocation failed\n");
0247         goto dealloc;
0248     }
0249     if (!(cardp->tx_urb = usb_alloc_urb(0, GFP_KERNEL))) {
0250         lbs_deb_usbd(&udev->dev, "Tx URB allocation failed\n");
0251         goto dealloc;
0252     }
0253     cardp->ep_out_buf = kmalloc(MRVDRV_ETH_TX_PACKET_BUFFER_SIZE, GFP_KERNEL);
0254     if (!cardp->ep_out_buf) {
0255         lbs_deb_usbd(&udev->dev, "Could not allocate buffer\n");
0256         goto dealloc;
0257     }
0258 
0259     priv = lbs_add_card(cardp, &intf->dev);
0260     if (IS_ERR(priv)) {
0261         r = PTR_ERR(priv);
0262         goto err_add_card;
0263     }
0264 
0265     cardp->priv = priv;
0266 
0267     priv->hw_host_to_card = if_usb_host_to_card;
0268     priv->enter_deep_sleep = NULL;
0269     priv->exit_deep_sleep = NULL;
0270     priv->reset_deep_sleep_wakeup = NULL;
0271     priv->is_polling = false;
0272 #ifdef CONFIG_OLPC
0273     if (machine_is_olpc())
0274         priv->reset_card = if_usb_reset_olpc_card;
0275 #endif
0276 
0277     cardp->boot2_version = udev->descriptor.bcdDevice;
0278 
0279     usb_get_dev(udev);
0280     usb_set_intfdata(intf, cardp);
0281 
0282     r = lbs_get_firmware_async(priv, &udev->dev, cardp->model,
0283                    fw_table, if_usb_prog_firmware);
0284     if (r)
0285         goto err_get_fw;
0286 
0287     return 0;
0288 
0289 err_get_fw:
0290     usb_put_dev(udev);
0291     lbs_remove_card(priv);
0292 err_add_card:
0293     if_usb_reset_device(cardp);
0294 dealloc:
0295     if_usb_free(cardp);
0296     kfree(cardp);
0297 
0298 error:
0299     return r;
0300 }
0301 
0302 /**
0303  * if_usb_disconnect - free resource and cleanup
0304  * @intf:   USB interface structure
0305  * returns: N/A
0306  */
0307 static void if_usb_disconnect(struct usb_interface *intf)
0308 {
0309     struct if_usb_card *cardp = usb_get_intfdata(intf);
0310     struct lbs_private *priv = cardp->priv;
0311 
0312     cardp->surprise_removed = 1;
0313 
0314     if (priv) {
0315         lbs_stop_card(priv);
0316         lbs_remove_card(priv);
0317     }
0318 
0319     /* Unlink and free urb */
0320     if_usb_free(cardp);
0321     kfree(cardp);
0322 
0323     usb_set_intfdata(intf, NULL);
0324     usb_put_dev(interface_to_usbdev(intf));
0325 }
0326 
0327 /**
0328  * if_usb_send_fw_pkt - download FW
0329  * @cardp:  pointer to &struct if_usb_card
0330  * returns: 0
0331  */
0332 static int if_usb_send_fw_pkt(struct if_usb_card *cardp)
0333 {
0334     struct fwdata *fwdata = cardp->ep_out_buf;
0335     const uint8_t *firmware = cardp->fw->data;
0336 
0337     /* If we got a CRC failure on the last block, back
0338        up and retry it */
0339     if (!cardp->CRC_OK) {
0340         cardp->totalbytes = cardp->fwlastblksent;
0341         cardp->fwseqnum--;
0342     }
0343 
0344     lbs_deb_usb2(&cardp->udev->dev, "totalbytes = %d\n",
0345              cardp->totalbytes);
0346 
0347     /* struct fwdata (which we sent to the card) has an
0348        extra __le32 field in between the header and the data,
0349        which is not in the struct fwheader in the actual
0350        firmware binary. Insert the seqnum in the middle... */
0351     memcpy(&fwdata->hdr, &firmware[cardp->totalbytes],
0352            sizeof(struct fwheader));
0353 
0354     cardp->fwlastblksent = cardp->totalbytes;
0355     cardp->totalbytes += sizeof(struct fwheader);
0356 
0357     memcpy(fwdata->data, &firmware[cardp->totalbytes],
0358            le32_to_cpu(fwdata->hdr.datalength));
0359 
0360     lbs_deb_usb2(&cardp->udev->dev, "Data length = %d\n",
0361              le32_to_cpu(fwdata->hdr.datalength));
0362 
0363     fwdata->seqnum = cpu_to_le32(++cardp->fwseqnum);
0364     cardp->totalbytes += le32_to_cpu(fwdata->hdr.datalength);
0365 
0366     usb_tx_block(cardp, cardp->ep_out_buf, sizeof(struct fwdata) +
0367              le32_to_cpu(fwdata->hdr.datalength));
0368 
0369     if (fwdata->hdr.dnldcmd == cpu_to_le32(FW_HAS_DATA_TO_RECV)) {
0370         lbs_deb_usb2(&cardp->udev->dev, "There are data to follow\n");
0371         lbs_deb_usb2(&cardp->udev->dev, "seqnum = %d totalbytes = %d\n",
0372                  cardp->fwseqnum, cardp->totalbytes);
0373     } else if (fwdata->hdr.dnldcmd == cpu_to_le32(FW_HAS_LAST_BLOCK)) {
0374         lbs_deb_usb2(&cardp->udev->dev, "Host has finished FW downloading\n");
0375         lbs_deb_usb2(&cardp->udev->dev, "Downloading FW JUMP BLOCK\n");
0376 
0377         cardp->fwfinalblk = 1;
0378     }
0379 
0380     lbs_deb_usb2(&cardp->udev->dev, "Firmware download done; size %d\n",
0381              cardp->totalbytes);
0382 
0383     return 0;
0384 }
0385 
0386 static int if_usb_reset_device(struct if_usb_card *cardp)
0387 {
0388     struct cmd_header *cmd = cardp->ep_out_buf + 4;
0389     int ret;
0390 
0391     *(__le32 *)cardp->ep_out_buf = cpu_to_le32(CMD_TYPE_REQUEST);
0392 
0393     cmd->command = cpu_to_le16(CMD_802_11_RESET);
0394     cmd->size = cpu_to_le16(sizeof(cmd));
0395     cmd->result = cpu_to_le16(0);
0396     cmd->seqnum = cpu_to_le16(0x5a5a);
0397     usb_tx_block(cardp, cardp->ep_out_buf, 4 + sizeof(struct cmd_header));
0398 
0399     msleep(100);
0400     ret = usb_reset_device(cardp->udev);
0401     msleep(100);
0402 
0403 #ifdef CONFIG_OLPC
0404     if (ret && machine_is_olpc())
0405         if_usb_reset_olpc_card(NULL);
0406 #endif
0407 
0408     return ret;
0409 }
0410 
0411 /**
0412  *  usb_tx_block - transfer the data to the device
0413  *  @cardp:     pointer to &struct if_usb_card
0414  *  @payload:   pointer to payload data
0415  *  @nb:    data length
0416  *  returns:    0 for success or negative error code
0417  */
0418 static int usb_tx_block(struct if_usb_card *cardp, uint8_t *payload, uint16_t nb)
0419 {
0420     int ret;
0421 
0422     /* check if device is removed */
0423     if (cardp->surprise_removed) {
0424         lbs_deb_usbd(&cardp->udev->dev, "Device removed\n");
0425         ret = -ENODEV;
0426         goto tx_ret;
0427     }
0428 
0429     usb_fill_bulk_urb(cardp->tx_urb, cardp->udev,
0430               usb_sndbulkpipe(cardp->udev,
0431                       cardp->ep_out),
0432               payload, nb, if_usb_write_bulk_callback, cardp);
0433 
0434     cardp->tx_urb->transfer_flags |= URB_ZERO_PACKET;
0435 
0436     if ((ret = usb_submit_urb(cardp->tx_urb, GFP_ATOMIC))) {
0437         lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb failed: %d\n", ret);
0438     } else {
0439         lbs_deb_usb2(&cardp->udev->dev, "usb_submit_urb success\n");
0440         ret = 0;
0441     }
0442 
0443 tx_ret:
0444     return ret;
0445 }
0446 
0447 static int __if_usb_submit_rx_urb(struct if_usb_card *cardp,
0448                   void (*callbackfn)(struct urb *urb))
0449 {
0450     struct sk_buff *skb;
0451     int ret = -1;
0452 
0453     if (!(skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE))) {
0454         pr_err("No free skb\n");
0455         goto rx_ret;
0456     }
0457 
0458     cardp->rx_skb = skb;
0459 
0460     /* Fill the receive configuration URB and initialise the Rx call back */
0461     usb_fill_bulk_urb(cardp->rx_urb, cardp->udev,
0462               usb_rcvbulkpipe(cardp->udev, cardp->ep_in),
0463               skb->data + IPFIELD_ALIGN_OFFSET,
0464               MRVDRV_ETH_RX_PACKET_BUFFER_SIZE, callbackfn,
0465               cardp);
0466 
0467     lbs_deb_usb2(&cardp->udev->dev, "Pointer for rx_urb %p\n", cardp->rx_urb);
0468     if ((ret = usb_submit_urb(cardp->rx_urb, GFP_ATOMIC))) {
0469         lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB failed: %d\n", ret);
0470         kfree_skb(skb);
0471         cardp->rx_skb = NULL;
0472         ret = -1;
0473     } else {
0474         lbs_deb_usb2(&cardp->udev->dev, "Submit Rx URB success\n");
0475         ret = 0;
0476     }
0477 
0478 rx_ret:
0479     return ret;
0480 }
0481 
0482 static int if_usb_submit_rx_urb_fwload(struct if_usb_card *cardp)
0483 {
0484     return __if_usb_submit_rx_urb(cardp, &if_usb_receive_fwload);
0485 }
0486 
0487 static int if_usb_submit_rx_urb(struct if_usb_card *cardp)
0488 {
0489     return __if_usb_submit_rx_urb(cardp, &if_usb_receive);
0490 }
0491 
0492 static void if_usb_receive_fwload(struct urb *urb)
0493 {
0494     struct if_usb_card *cardp = urb->context;
0495     struct sk_buff *skb = cardp->rx_skb;
0496     struct fwsyncheader *syncfwheader;
0497     struct bootcmdresp bootcmdresp;
0498 
0499     if (urb->status) {
0500         lbs_deb_usbd(&cardp->udev->dev,
0501                  "URB status is failed during fw load\n");
0502         kfree_skb(skb);
0503         return;
0504     }
0505 
0506     if (cardp->fwdnldover) {
0507         __le32 *tmp = (__le32 *)(skb->data + IPFIELD_ALIGN_OFFSET);
0508 
0509         if (tmp[0] == cpu_to_le32(CMD_TYPE_INDICATION) &&
0510             tmp[1] == cpu_to_le32(MACREG_INT_CODE_FIRMWARE_READY)) {
0511             pr_info("Firmware ready event received\n");
0512             wake_up(&cardp->fw_wq);
0513         } else {
0514             lbs_deb_usb("Waiting for confirmation; got %x %x\n",
0515                     le32_to_cpu(tmp[0]), le32_to_cpu(tmp[1]));
0516             if_usb_submit_rx_urb_fwload(cardp);
0517         }
0518         kfree_skb(skb);
0519         return;
0520     }
0521     if (cardp->bootcmdresp <= 0) {
0522         memcpy (&bootcmdresp, skb->data + IPFIELD_ALIGN_OFFSET,
0523             sizeof(bootcmdresp));
0524 
0525         if (le16_to_cpu(cardp->udev->descriptor.bcdDevice) < 0x3106) {
0526             kfree_skb(skb);
0527             if_usb_submit_rx_urb_fwload(cardp);
0528             cardp->bootcmdresp = BOOT_CMD_RESP_OK;
0529             lbs_deb_usbd(&cardp->udev->dev,
0530                      "Received valid boot command response\n");
0531             return;
0532         }
0533         if (bootcmdresp.magic != cpu_to_le32(BOOT_CMD_MAGIC_NUMBER)) {
0534             if (bootcmdresp.magic == cpu_to_le32(CMD_TYPE_REQUEST) ||
0535                 bootcmdresp.magic == cpu_to_le32(CMD_TYPE_DATA) ||
0536                 bootcmdresp.magic == cpu_to_le32(CMD_TYPE_INDICATION)) {
0537                 if (!cardp->bootcmdresp)
0538                     pr_info("Firmware already seems alive; resetting\n");
0539                 cardp->bootcmdresp = -1;
0540             } else {
0541                 pr_info("boot cmd response wrong magic number (0x%x)\n",
0542                         le32_to_cpu(bootcmdresp.magic));
0543             }
0544         } else if ((bootcmdresp.cmd != BOOT_CMD_FW_BY_USB) &&
0545                (bootcmdresp.cmd != BOOT_CMD_UPDATE_FW) &&
0546                (bootcmdresp.cmd != BOOT_CMD_UPDATE_BOOT2)) {
0547             pr_info("boot cmd response cmd_tag error (%d)\n",
0548                 bootcmdresp.cmd);
0549         } else if (bootcmdresp.result != BOOT_CMD_RESP_OK) {
0550             pr_info("boot cmd response result error (%d)\n",
0551                 bootcmdresp.result);
0552         } else {
0553             cardp->bootcmdresp = 1;
0554             lbs_deb_usbd(&cardp->udev->dev,
0555                      "Received valid boot command response\n");
0556         }
0557         kfree_skb(skb);
0558         if_usb_submit_rx_urb_fwload(cardp);
0559         return;
0560     }
0561 
0562     syncfwheader = kmemdup(skb->data + IPFIELD_ALIGN_OFFSET,
0563                    sizeof(struct fwsyncheader), GFP_ATOMIC);
0564     if (!syncfwheader) {
0565         lbs_deb_usbd(&cardp->udev->dev, "Failure to allocate syncfwheader\n");
0566         kfree_skb(skb);
0567         return;
0568     }
0569 
0570     if (!syncfwheader->cmd) {
0571         lbs_deb_usb2(&cardp->udev->dev, "FW received Blk with correct CRC\n");
0572         lbs_deb_usb2(&cardp->udev->dev, "FW received Blk seqnum = %d\n",
0573                  le32_to_cpu(syncfwheader->seqnum));
0574         cardp->CRC_OK = 1;
0575     } else {
0576         lbs_deb_usbd(&cardp->udev->dev, "FW received Blk with CRC error\n");
0577         cardp->CRC_OK = 0;
0578     }
0579 
0580     kfree_skb(skb);
0581 
0582     /* Give device 5s to either write firmware to its RAM or eeprom */
0583     mod_timer(&cardp->fw_timeout, jiffies + (HZ*5));
0584 
0585     if (cardp->fwfinalblk) {
0586         cardp->fwdnldover = 1;
0587         goto exit;
0588     }
0589 
0590     if_usb_send_fw_pkt(cardp);
0591 
0592  exit:
0593     if_usb_submit_rx_urb_fwload(cardp);
0594 
0595     kfree(syncfwheader);
0596 }
0597 
0598 #define MRVDRV_MIN_PKT_LEN  30
0599 
0600 static inline void process_cmdtypedata(int recvlength, struct sk_buff *skb,
0601                        struct if_usb_card *cardp,
0602                        struct lbs_private *priv)
0603 {
0604     if (recvlength > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + MESSAGE_HEADER_LEN
0605         || recvlength < MRVDRV_MIN_PKT_LEN) {
0606         lbs_deb_usbd(&cardp->udev->dev, "Packet length is Invalid\n");
0607         kfree_skb(skb);
0608         return;
0609     }
0610 
0611     skb_reserve(skb, IPFIELD_ALIGN_OFFSET);
0612     skb_put(skb, recvlength);
0613     skb_pull(skb, MESSAGE_HEADER_LEN);
0614 
0615     lbs_process_rxed_packet(priv, skb);
0616 }
0617 
0618 static inline void process_cmdrequest(int recvlength, uint8_t *recvbuff,
0619                       struct sk_buff *skb,
0620                       struct if_usb_card *cardp,
0621                       struct lbs_private *priv)
0622 {
0623     unsigned long flags;
0624     u8 i;
0625 
0626     if (recvlength > LBS_CMD_BUFFER_SIZE) {
0627         lbs_deb_usbd(&cardp->udev->dev,
0628                  "The receive buffer is too large\n");
0629         kfree_skb(skb);
0630         return;
0631     }
0632 
0633     spin_lock_irqsave(&priv->driver_lock, flags);
0634 
0635     i = (priv->resp_idx == 0) ? 1 : 0;
0636     BUG_ON(priv->resp_len[i]);
0637     priv->resp_len[i] = (recvlength - MESSAGE_HEADER_LEN);
0638     memcpy(priv->resp_buf[i], recvbuff + MESSAGE_HEADER_LEN,
0639         priv->resp_len[i]);
0640     kfree_skb(skb);
0641     lbs_notify_command_response(priv, i);
0642 
0643     spin_unlock_irqrestore(&priv->driver_lock, flags);
0644 
0645     lbs_deb_usbd(&cardp->udev->dev,
0646             "Wake up main thread to handle cmd response\n");
0647 }
0648 
0649 /**
0650  *  if_usb_receive - read the packet into the upload buffer,
0651  *  wake up the main thread and initialise the Rx callack
0652  *
0653  *  @urb:   pointer to &struct urb
0654  *  returns:    N/A
0655  */
0656 static void if_usb_receive(struct urb *urb)
0657 {
0658     struct if_usb_card *cardp = urb->context;
0659     struct sk_buff *skb = cardp->rx_skb;
0660     struct lbs_private *priv = cardp->priv;
0661     int recvlength = urb->actual_length;
0662     uint8_t *recvbuff = NULL;
0663     uint32_t recvtype = 0;
0664     __le32 *pkt = (__le32 *)(skb->data + IPFIELD_ALIGN_OFFSET);
0665     uint32_t event;
0666 
0667     if (recvlength) {
0668         if (urb->status) {
0669             lbs_deb_usbd(&cardp->udev->dev, "RX URB failed: %d\n",
0670                      urb->status);
0671             kfree_skb(skb);
0672             goto setup_for_next;
0673         }
0674 
0675         recvbuff = skb->data + IPFIELD_ALIGN_OFFSET;
0676         recvtype = le32_to_cpu(pkt[0]);
0677         lbs_deb_usbd(&cardp->udev->dev,
0678                 "Recv length = 0x%x, Recv type = 0x%X\n",
0679                 recvlength, recvtype);
0680     } else if (urb->status) {
0681         kfree_skb(skb);
0682         return;
0683     }
0684 
0685     switch (recvtype) {
0686     case CMD_TYPE_DATA:
0687         process_cmdtypedata(recvlength, skb, cardp, priv);
0688         break;
0689 
0690     case CMD_TYPE_REQUEST:
0691         process_cmdrequest(recvlength, recvbuff, skb, cardp, priv);
0692         break;
0693 
0694     case CMD_TYPE_INDICATION:
0695         /* Event handling */
0696         event = le32_to_cpu(pkt[1]);
0697         lbs_deb_usbd(&cardp->udev->dev, "**EVENT** 0x%X\n", event);
0698         kfree_skb(skb);
0699 
0700         /* Icky undocumented magic special case */
0701         if (event & 0xffff0000) {
0702             u32 trycount = (event & 0xffff0000) >> 16;
0703 
0704             lbs_send_tx_feedback(priv, trycount);
0705         } else
0706             lbs_queue_event(priv, event & 0xFF);
0707         break;
0708 
0709     default:
0710         lbs_deb_usbd(&cardp->udev->dev, "Unknown command type 0x%X\n",
0711                  recvtype);
0712         kfree_skb(skb);
0713         break;
0714     }
0715 
0716 setup_for_next:
0717     if_usb_submit_rx_urb(cardp);
0718 }
0719 
0720 /**
0721  *  if_usb_host_to_card - downloads data to FW
0722  *  @priv:  pointer to &struct lbs_private structure
0723  *  @type:  type of data
0724  *  @payload:   pointer to data buffer
0725  *  @nb:    number of bytes
0726  *  returns:    0 for success or negative error code
0727  */
0728 static int if_usb_host_to_card(struct lbs_private *priv, uint8_t type,
0729                    uint8_t *payload, uint16_t nb)
0730 {
0731     struct if_usb_card *cardp = priv->card;
0732 
0733     lbs_deb_usbd(&cardp->udev->dev,"*** type = %u\n", type);
0734     lbs_deb_usbd(&cardp->udev->dev,"size after = %d\n", nb);
0735 
0736     if (type == MVMS_CMD) {
0737         *(__le32 *)cardp->ep_out_buf = cpu_to_le32(CMD_TYPE_REQUEST);
0738         priv->dnld_sent = DNLD_CMD_SENT;
0739     } else {
0740         *(__le32 *)cardp->ep_out_buf = cpu_to_le32(CMD_TYPE_DATA);
0741         priv->dnld_sent = DNLD_DATA_SENT;
0742     }
0743 
0744     memcpy((cardp->ep_out_buf + MESSAGE_HEADER_LEN), payload, nb);
0745 
0746     return usb_tx_block(cardp, cardp->ep_out_buf, nb + MESSAGE_HEADER_LEN);
0747 }
0748 
0749 /**
0750  *  if_usb_issue_boot_command - issues Boot command to the Boot2 code
0751  *  @cardp: pointer to &if_usb_card
0752  *  @ivalue:    1:Boot from FW by USB-Download
0753  *      2:Boot from FW in EEPROM
0754  *  returns:    0 for success or negative error code
0755  */
0756 static int if_usb_issue_boot_command(struct if_usb_card *cardp, int ivalue)
0757 {
0758     struct bootcmd *bootcmd = cardp->ep_out_buf;
0759 
0760     /* Prepare command */
0761     bootcmd->magic = cpu_to_le32(BOOT_CMD_MAGIC_NUMBER);
0762     bootcmd->cmd = ivalue;
0763     memset(bootcmd->pad, 0, sizeof(bootcmd->pad));
0764 
0765     /* Issue command */
0766     usb_tx_block(cardp, cardp->ep_out_buf, sizeof(*bootcmd));
0767 
0768     return 0;
0769 }
0770 
0771 
0772 /**
0773  *  check_fwfile_format - check the validity of Boot2/FW image
0774  *
0775  *  @data:  pointer to image
0776  *  @totlen:    image length
0777  *  returns:     0 (good) or 1 (failure)
0778  */
0779 static int check_fwfile_format(const uint8_t *data, uint32_t totlen)
0780 {
0781     uint32_t bincmd, exit;
0782     uint32_t blksize, offset, len;
0783     int ret;
0784 
0785     ret = 1;
0786     exit = len = 0;
0787 
0788     do {
0789         struct fwheader *fwh = (void *)data;
0790 
0791         bincmd = le32_to_cpu(fwh->dnldcmd);
0792         blksize = le32_to_cpu(fwh->datalength);
0793         switch (bincmd) {
0794         case FW_HAS_DATA_TO_RECV:
0795             offset = sizeof(struct fwheader) + blksize;
0796             data += offset;
0797             len += offset;
0798             if (len >= totlen)
0799                 exit = 1;
0800             break;
0801         case FW_HAS_LAST_BLOCK:
0802             exit = 1;
0803             ret = 0;
0804             break;
0805         default:
0806             exit = 1;
0807             break;
0808         }
0809     } while (!exit);
0810 
0811     if (ret)
0812         pr_err("firmware file format check FAIL\n");
0813     else
0814         lbs_deb_fw("firmware file format check PASS\n");
0815 
0816     return ret;
0817 }
0818 
0819 static void if_usb_prog_firmware(struct lbs_private *priv, int ret,
0820                  const struct firmware *fw,
0821                  const struct firmware *unused)
0822 {
0823     struct if_usb_card *cardp = priv->card;
0824     int i = 0;
0825     static int reset_count = 10;
0826 
0827     if (ret) {
0828         pr_err("failed to find firmware (%d)\n", ret);
0829         goto done;
0830     }
0831 
0832     cardp->fw = fw;
0833     if (check_fwfile_format(cardp->fw->data, cardp->fw->size)) {
0834         ret = -EINVAL;
0835         goto done;
0836     }
0837 
0838     /* Cancel any pending usb business */
0839     usb_kill_urb(cardp->rx_urb);
0840     usb_kill_urb(cardp->tx_urb);
0841 
0842     cardp->fwlastblksent = 0;
0843     cardp->fwdnldover = 0;
0844     cardp->totalbytes = 0;
0845     cardp->fwfinalblk = 0;
0846     cardp->bootcmdresp = 0;
0847 
0848 restart:
0849     if (if_usb_submit_rx_urb_fwload(cardp) < 0) {
0850         lbs_deb_usbd(&cardp->udev->dev, "URB submission is failed\n");
0851         ret = -EIO;
0852         goto done;
0853     }
0854 
0855     cardp->bootcmdresp = 0;
0856     do {
0857         int j = 0;
0858         i++;
0859         if_usb_issue_boot_command(cardp, BOOT_CMD_FW_BY_USB);
0860         /* wait for command response */
0861         do {
0862             j++;
0863             msleep_interruptible(100);
0864         } while (cardp->bootcmdresp == 0 && j < 10);
0865     } while (cardp->bootcmdresp == 0 && i < 5);
0866 
0867     if (cardp->bootcmdresp == BOOT_CMD_RESP_NOT_SUPPORTED) {
0868         /* Return to normal operation */
0869         ret = -EOPNOTSUPP;
0870         usb_kill_urb(cardp->rx_urb);
0871         usb_kill_urb(cardp->tx_urb);
0872         if (if_usb_submit_rx_urb(cardp) < 0)
0873             ret = -EIO;
0874         goto done;
0875     } else if (cardp->bootcmdresp <= 0) {
0876         if (--reset_count >= 0) {
0877             if_usb_reset_device(cardp);
0878             goto restart;
0879         }
0880         ret = -EIO;
0881         goto done;
0882     }
0883 
0884     i = 0;
0885 
0886     cardp->totalbytes = 0;
0887     cardp->fwlastblksent = 0;
0888     cardp->CRC_OK = 1;
0889     cardp->fwdnldover = 0;
0890     cardp->fwseqnum = -1;
0891     cardp->totalbytes = 0;
0892     cardp->fwfinalblk = 0;
0893 
0894     /* Send the first firmware packet... */
0895     if_usb_send_fw_pkt(cardp);
0896 
0897     /* ... and wait for the process to complete */
0898     wait_event_interruptible(cardp->fw_wq, cardp->surprise_removed || cardp->fwdnldover);
0899 
0900     del_timer_sync(&cardp->fw_timeout);
0901     usb_kill_urb(cardp->rx_urb);
0902 
0903     if (!cardp->fwdnldover) {
0904         pr_info("failed to load fw, resetting device!\n");
0905         if (--reset_count >= 0) {
0906             if_usb_reset_device(cardp);
0907             goto restart;
0908         }
0909 
0910         pr_info("FW download failure, time = %d ms\n", i * 100);
0911         ret = -EIO;
0912         goto done;
0913     }
0914 
0915     cardp->priv->fw_ready = 1;
0916     if_usb_submit_rx_urb(cardp);
0917 
0918     if (lbs_start_card(priv))
0919         goto done;
0920 
0921     if_usb_setup_firmware(priv);
0922 
0923     /*
0924      * EHS_REMOVE_WAKEUP is not supported on all versions of the firmware.
0925      */
0926     priv->wol_criteria = EHS_REMOVE_WAKEUP;
0927     if (lbs_host_sleep_cfg(priv, priv->wol_criteria, NULL))
0928         priv->ehs_remove_supported = false;
0929 
0930  done:
0931     cardp->fw = NULL;
0932 }
0933 
0934 
0935 #ifdef CONFIG_PM
0936 static int if_usb_suspend(struct usb_interface *intf, pm_message_t message)
0937 {
0938     struct if_usb_card *cardp = usb_get_intfdata(intf);
0939     struct lbs_private *priv = cardp->priv;
0940     int ret;
0941 
0942     if (priv->psstate != PS_STATE_FULL_POWER) {
0943         ret = -1;
0944         goto out;
0945     }
0946 
0947 #ifdef CONFIG_OLPC
0948     if (machine_is_olpc()) {
0949         if (priv->wol_criteria == EHS_REMOVE_WAKEUP)
0950             olpc_ec_wakeup_clear(EC_SCI_SRC_WLAN);
0951         else
0952             olpc_ec_wakeup_set(EC_SCI_SRC_WLAN);
0953     }
0954 #endif
0955 
0956     ret = lbs_suspend(priv);
0957     if (ret)
0958         goto out;
0959 
0960     /* Unlink tx & rx urb */
0961     usb_kill_urb(cardp->tx_urb);
0962     usb_kill_urb(cardp->rx_urb);
0963 
0964  out:
0965     return ret;
0966 }
0967 
0968 static int if_usb_resume(struct usb_interface *intf)
0969 {
0970     struct if_usb_card *cardp = usb_get_intfdata(intf);
0971     struct lbs_private *priv = cardp->priv;
0972 
0973     if_usb_submit_rx_urb(cardp);
0974 
0975     lbs_resume(priv);
0976 
0977     return 0;
0978 }
0979 #else
0980 #define if_usb_suspend NULL
0981 #define if_usb_resume NULL
0982 #endif
0983 
0984 static struct usb_driver if_usb_driver = {
0985     .name = DRV_NAME,
0986     .probe = if_usb_probe,
0987     .disconnect = if_usb_disconnect,
0988     .id_table = if_usb_table,
0989     .suspend = if_usb_suspend,
0990     .resume = if_usb_resume,
0991     .reset_resume = if_usb_resume,
0992     .disable_hub_initiated_lpm = 1,
0993 };
0994 
0995 module_usb_driver(if_usb_driver);
0996 
0997 MODULE_DESCRIPTION("8388 USB WLAN Driver");
0998 MODULE_AUTHOR("Marvell International Ltd. and Red Hat, Inc.");
0999 MODULE_LICENSE("GPL");