libertas: make lbs_cmd() usage nicer
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / libertas / if_usb.c
index 797d3fbf7154e4cc2ee1563257988ff02a58fa00..8ff57a6f251fb75d307069d66ae2151463b9db11 100644 (file)
@@ -5,7 +5,6 @@
 #include <linux/moduleparam.h>
 #include <linux/firmware.h>
 #include <linux/netdevice.h>
-#include <linux/list.h>
 #include <linux/usb.h>
 
 #define DRV_NAME "usb8xxx"
 #include "decl.h"
 #include "defs.h"
 #include "dev.h"
+#include "cmd.h"
 #include "if_usb.h"
 
 #define MESSAGE_HEADER_LEN     4
 
-static const char usbdriver_name[] = "usb8xxx";
-static u8 *default_fw_name = "usb8388.bin";
-
-static char *lbs_fw_name;
+static char *lbs_fw_name = "usb8388.bin";
 module_param_named(fw_name, lbs_fw_name, charp, 0644);
 
-/*
- * We need to send a RESET command to all USB devices before
- * we tear down the USB connection. Otherwise we would not
- * be able to re-init device the device if the module gets
- * loaded again. This is a list of all initialized USB devices,
- * for the reset code see if_usb_reset_device()
-*/
-static LIST_HEAD(usb_devices);
-
 static struct usb_device_id if_usb_table[] = {
        /* Enter the device signature inside */
        { USB_DEVICE(0x1286, 0x2001) },
@@ -45,9 +33,12 @@ MODULE_DEVICE_TABLE(usb, if_usb_table);
 static void if_usb_receive(struct urb *urb);
 static void if_usb_receive_fwload(struct urb *urb);
 static int if_usb_prog_firmware(struct usb_card_rec *cardp);
-static int if_usb_host_to_card(lbs_private *priv, u8 type, u8 *payload, u16 nb);
-static int if_usb_get_int_status(lbs_private *priv, u8 *);
-static int if_usb_read_event_cause(lbs_private *);
+static int if_usb_host_to_card(struct lbs_private *priv,
+       u8 type,
+       u8 *payload,
+       u16 nb);
+static int if_usb_get_int_status(struct lbs_private *priv, u8 *);
+static int if_usb_read_event_cause(struct lbs_private *);
 static int usb_tx_block(struct usb_card_rec *cardp, u8 *payload, u16 nb);
 static void if_usb_free(struct usb_card_rec *cardp);
 static int if_usb_submit_rx_urb(struct usb_card_rec *cardp);
@@ -65,7 +56,7 @@ static void if_usb_write_bulk_callback(struct urb *urb)
        /* handle the transmission complete validations */
 
        if (urb->status == 0) {
-               lbs_private *priv = cardp->priv;
+               struct lbs_private *priv = cardp->priv;
 
                /*
                lbs_deb_usbd(&urb->dev->dev, "URB status is successfull\n");
@@ -76,21 +67,8 @@ static void if_usb_write_bulk_callback(struct urb *urb)
                /* Used for both firmware TX and regular TX.  priv isn't
                 * valid at firmware load time.
                 */
-               if (priv) {
-                       lbs_adapter *adapter = priv->adapter;
-                       struct net_device *dev = priv->dev;
-
-                       priv->dnld_sent = DNLD_RES_RECEIVED;
-
-                       /* Wake main thread if commands are pending */
-                       if (!adapter->cur_cmd)
-                               wake_up_interruptible(&priv->waitq);
-
-                       if ((adapter->connect_status == LBS_CONNECTED)) {
-                               netif_wake_queue(dev);
-                               netif_wake_queue(priv->mesh_dev);
-                       }
-               }
+               if (priv)
+                       lbs_host_to_card_done(priv);
        } else {
                /* print the failure status number for debug */
                lbs_pr_info("URB in failure status: %d\n", urb->status);
@@ -124,6 +102,18 @@ static void if_usb_free(struct usb_card_rec *cardp)
        lbs_deb_leave(LBS_DEB_USB);
 }
 
+static void if_usb_set_boot2_ver(struct lbs_private *priv)
+{
+       struct cmd_ds_set_boot2_ver b2_cmd;
+
+       b2_cmd.action = 0;
+       b2_cmd.version = priv->boot2_version;
+
+       if (lbs_cmd(priv, CMD_SET_BOOT2_VER, b2_cmd, NULL, 0))
+               lbs_deb_usb("Setting boot2 version failed\n");
+}
+
+
 /**
  *  @brief sets the configuration values
  *  @param ifnum       interface number
@@ -136,7 +126,7 @@ static int if_usb_probe(struct usb_interface *intf,
        struct usb_device *udev;
        struct usb_host_interface *iface_desc;
        struct usb_endpoint_descriptor *endpoint;
-       lbs_private *priv;
+       struct lbs_private *priv;
        struct usb_card_rec *cardp;
        int i;
 
@@ -240,12 +230,12 @@ static int if_usb_probe(struct usb_interface *intf,
        /* Delay 200 ms to waiting for the FW ready */
        if_usb_submit_rx_urb(cardp);
        msleep_interruptible(200);
-       priv->adapter->fw_ready = 1;
+       priv->fw_ready = 1;
 
        if (lbs_start_card(priv))
                goto err_start_card;
 
-       list_add_tail(&cardp->list, &usb_devices);
+       if_usb_set_boot2_ver(priv);
 
        usb_get_dev(udev);
        usb_set_intfdata(intf, cardp);
@@ -273,24 +263,25 @@ error:
 static void if_usb_disconnect(struct usb_interface *intf)
 {
        struct usb_card_rec *cardp = usb_get_intfdata(intf);
-       lbs_private *priv = (lbs_private *) cardp->priv;
+       struct lbs_private *priv = (struct lbs_private *) cardp->priv;
 
        lbs_deb_enter(LBS_DEB_MAIN);
 
        /* Update Surprise removed to TRUE */
        cardp->surprise_removed = 1;
 
-       list_del(&cardp->list);
-
        if (priv) {
-               lbs_adapter *adapter = priv->adapter;
 
-               adapter->surpriseremoved = 1;
+               priv->surpriseremoved = 1;
                lbs_stop_card(priv);
                lbs_remove_mesh(priv);
                lbs_remove_card(priv);
        }
 
+       /* this is (apparently?) necessary for future usage of the device */
+       lbs_prepare_and_send_command(priv, CMD_802_11_RESET, CMD_ACT_HALT,
+                       0, 0, NULL);
+
        /* Unlink and free urb */
        if_usb_free(cardp);
 
@@ -302,7 +293,7 @@ static void if_usb_disconnect(struct usb_interface *intf)
 
 /**
  *  @brief  This function download FW
- *  @param priv                pointer to lbs_private
+ *  @param priv                pointer to struct lbs_private
  *  @return            0
  */
 static int if_prog_firmware(struct usb_card_rec *cardp)
@@ -384,20 +375,23 @@ static int if_prog_firmware(struct usb_card_rec *cardp)
 
 static int if_usb_reset_device(struct usb_card_rec *cardp)
 {
+       struct cmd_ds_command *cmd = (void *)&cardp->bulk_out_buffer[4];
        int ret;
-       lbs_private * priv = cardp->priv;
 
        lbs_deb_enter(LBS_DEB_USB);
 
-       /* Try a USB port reset first, if that fails send the reset
-        * command to the firmware.
-        */
+       *(__le32 *)cardp->bulk_out_buffer = cpu_to_le32(CMD_TYPE_REQUEST);
+
+       cmd->command = cpu_to_le16(CMD_802_11_RESET);
+       cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_reset) + S_DS_GEN);
+       cmd->result = cpu_to_le16(0);
+       cmd->seqnum = cpu_to_le16(0x5a5a);
+       cmd->params.reset.action = cpu_to_le16(CMD_ACT_HALT);
+       usb_tx_block(cardp, cardp->bulk_out_buffer, 4 + S_DS_GEN + sizeof(struct cmd_ds_802_11_reset));
+
+       msleep(10);
        ret = usb_reset_device(cardp->udev);
-       if (!ret && priv) {
-               msleep(10);
-               ret = lbs_reset_device(priv);
-               msleep(10);
-       }
+       msleep(10);
 
        lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
 
@@ -406,7 +400,7 @@ static int if_usb_reset_device(struct usb_card_rec *cardp)
 
 /**
  *  @brief This function transfer the data to the device.
- *  @param priv        pointer to lbs_private
+ *  @param priv        pointer to struct lbs_private
  *  @param payload     pointer to payload data
  *  @param nb          data length
  *  @return            0 or -1
@@ -430,7 +424,7 @@ static int usb_tx_block(struct usb_card_rec *cardp, u8 * payload, u16 nb)
 
        if ((ret = usb_submit_urb(cardp->tx_urb, GFP_ATOMIC))) {
                /*  transfer failed */
-               lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb failed\n");
+               lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb failed: %d\n", ret);
                ret = -1;
        } else {
                /* lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb success\n"); */
@@ -468,7 +462,9 @@ static int __if_usb_submit_rx_urb(struct usb_card_rec *cardp,
        /* lbs_deb_usbd(&cardp->udev->dev, "Pointer for rx_urb %p\n", cardp->rx_urb); */
        if ((ret = usb_submit_urb(cardp->rx_urb, GFP_ATOMIC))) {
                /* handle failure conditions */
-               lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB failed\n");
+               lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB failed: %d\n", ret);
+               kfree_skb(skb);
+               rinfo->skb = NULL;
                ret = -1;
        } else {
                /* lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB success\n"); */
@@ -516,9 +512,15 @@ static void if_usb_receive_fwload(struct urb *urb)
                        return;
                }
                if (bootcmdresp.u32magicnumber != cpu_to_le32(BOOT_CMD_MAGIC_NUMBER)) {
-                       lbs_pr_info(
-                               "boot cmd response wrong magic number (0x%x)\n",
-                               le32_to_cpu(bootcmdresp.u32magicnumber));
+                       if (bootcmdresp.u32magicnumber == cpu_to_le32(CMD_TYPE_REQUEST) ||
+                           bootcmdresp.u32magicnumber == cpu_to_le32(CMD_TYPE_DATA) ||
+                           bootcmdresp.u32magicnumber == cpu_to_le32(CMD_TYPE_INDICATION)) {
+                               lbs_pr_info("Firmware already seems alive; resetting\n");
+                               cardp->bootcmdresp = -1;
+                       } else {
+                               lbs_pr_info("boot cmd response wrong magic number (0x%x)\n",
+                                           le32_to_cpu(bootcmdresp.u32magicnumber));
+                       }
                } else if (bootcmdresp.u8cmd_tag != BOOT_CMD_FW_BY_USB) {
                        lbs_pr_info(
                                "boot cmd response cmd_tag error (%d)\n",
@@ -583,7 +585,7 @@ exit:
 
 static inline void process_cmdtypedata(int recvlength, struct sk_buff *skb,
                                       struct usb_card_rec *cardp,
-                                      lbs_private *priv)
+                                      struct lbs_private *priv)
 {
        if (recvlength > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE +
            MESSAGE_HEADER_LEN || recvlength < MRVDRV_MIN_PKT_LEN) {
@@ -603,7 +605,7 @@ static inline void process_cmdtypedata(int recvlength, struct sk_buff *skb,
 static inline void process_cmdrequest(int recvlength, u8 *recvbuff,
                                      struct sk_buff *skb,
                                      struct usb_card_rec *cardp,
-                                     lbs_private *priv)
+                                     struct lbs_private *priv)
 {
        u8 *cmdbuf;
        if (recvlength > MRVDRV_SIZE_OF_CMD_BUFFER) {
@@ -616,14 +618,14 @@ static inline void process_cmdrequest(int recvlength, u8 *recvbuff,
        if (!in_interrupt())
                BUG();
 
-       spin_lock(&priv->adapter->driver_lock);
+       spin_lock(&priv->driver_lock);
        /* take care of cur_cmd = NULL case by reading the
         * data to clear the interrupt */
-       if (!priv->adapter->cur_cmd) {
+       if (!priv->cur_cmd) {
                cmdbuf = priv->upld_buf;
-               priv->adapter->hisregcpy &= ~MRVDRV_CMD_UPLD_RDY;
+               priv->hisregcpy &= ~MRVDRV_CMD_UPLD_RDY;
        } else
-               cmdbuf = priv->adapter->cur_cmd->bufvirtualaddr;
+               cmdbuf = priv->cur_cmd->bufvirtualaddr;
 
        cardp->usb_int_cause |= MRVDRV_CMD_UPLD_RDY;
        priv->upld_len = (recvlength - MESSAGE_HEADER_LEN);
@@ -631,8 +633,8 @@ static inline void process_cmdrequest(int recvlength, u8 *recvbuff,
               priv->upld_len);
 
        kfree_skb(skb);
-       lbs_interrupt(priv->dev);
-       spin_unlock(&priv->adapter->driver_lock);
+       lbs_interrupt(priv);
+       spin_unlock(&priv->driver_lock);
 
        lbs_deb_usbd(&cardp->udev->dev,
                    "Wake up main thread to handle cmd response\n");
@@ -652,7 +654,7 @@ static void if_usb_receive(struct urb *urb)
        struct read_cb_info *rinfo = (struct read_cb_info *)urb->context;
        struct sk_buff *skb = rinfo->skb;
        struct usb_card_rec *cardp = (struct usb_card_rec *) rinfo->cardp;
-       lbs_private * priv = cardp->priv;
+       struct lbs_private *priv = cardp->priv;
 
        int recvlength = urb->actual_length;
        u8 *recvbuff = NULL;
@@ -676,8 +678,10 @@ static void if_usb_receive(struct urb *urb)
                lbs_deb_usbd(&cardp->udev->dev,
                            "Recv length = 0x%x, Recv type = 0x%X\n",
                            recvlength, recvtype);
-       } else if (urb->status)
+       } else if (urb->status) {
+               kfree_skb(skb);
                goto rx_exit;
+       }
 
        switch (recvtype) {
        case CMD_TYPE_DATA:
@@ -690,20 +694,20 @@ static void if_usb_receive(struct urb *urb)
 
        case CMD_TYPE_INDICATION:
                /* Event cause handling */
-               spin_lock(&priv->adapter->driver_lock);
+               spin_lock(&priv->driver_lock);
                cardp->usb_event_cause = le32_to_cpu(*(__le32 *) (recvbuff + MESSAGE_HEADER_LEN));
                lbs_deb_usbd(&cardp->udev->dev,"**EVENT** 0x%X\n",
                            cardp->usb_event_cause);
                if (cardp->usb_event_cause & 0xffff0000) {
                        lbs_send_tx_feedback(priv);
-                       spin_unlock(&priv->adapter->driver_lock);
+                       spin_unlock(&priv->driver_lock);
                        break;
                }
                cardp->usb_event_cause <<= 3;
                cardp->usb_int_cause |= MRVDRV_CARDEVENT;
                kfree_skb(skb);
-               lbs_interrupt(priv->dev);
-               spin_unlock(&priv->adapter->driver_lock);
+               lbs_interrupt(priv);
+               spin_unlock(&priv->driver_lock);
                goto rx_exit;
        default:
                lbs_deb_usbd(&cardp->udev->dev, "Unknown command type 0x%X\n",
@@ -720,13 +724,16 @@ rx_exit:
 
 /**
  *  @brief This function downloads data to FW
- *  @param priv                pointer to lbs_private structure
+ *  @param priv                pointer to struct lbs_private structure
  *  @param type                type of data
  *  @param buf         pointer to data buffer
  *  @param len         number of bytes
  *  @return            0 or -1
  */
-static int if_usb_host_to_card(lbs_private *priv, u8 type, u8 *payload, u16 nb)
+static int if_usb_host_to_card(struct lbs_private *priv,
+       u8 type,
+       u8 *payload,
+       u16 nb)
 {
        struct usb_card_rec *cardp = (struct usb_card_rec *)priv->card;
 
@@ -752,8 +759,8 @@ static int if_usb_host_to_card(lbs_private *priv, u8 type, u8 *payload, u16 nb)
                            nb + MESSAGE_HEADER_LEN);
 }
 
-/* called with adapter->driver_lock held */
-static int if_usb_get_int_status(lbs_private *priv, u8 *ireg)
+/* called with priv->driver_lock held */
+static int if_usb_get_int_status(struct lbs_private *priv, u8 *ireg)
 {
        struct usb_card_rec *cardp = priv->card;
 
@@ -765,11 +772,11 @@ static int if_usb_get_int_status(lbs_private *priv, u8 *ireg)
        return 0;
 }
 
-static int if_usb_read_event_cause(lbs_private * priv)
+static int if_usb_read_event_cause(struct lbs_private *priv)
 {
        struct usb_card_rec *cardp = priv->card;
 
-       priv->adapter->eventcause = cardp->usb_event_cause;
+       priv->eventcause = cardp->usb_event_cause;
        /* Re-submit rx urb here to avoid event lost issue */
        if_usb_submit_rx_urb(cardp);
        return 0;
@@ -886,7 +893,7 @@ restart:
                } while (cardp->bootcmdresp == 0 && j < 10);
        } while (cardp->bootcmdresp == 0 && i < 5);
 
-       if (cardp->bootcmdresp == 0) {
+       if (cardp->bootcmdresp <= 0) {
                if (--reset_count >= 0) {
                        if_usb_reset_device(cardp);
                        goto restart;
@@ -940,11 +947,11 @@ done:
 static int if_usb_suspend(struct usb_interface *intf, pm_message_t message)
 {
        struct usb_card_rec *cardp = usb_get_intfdata(intf);
-       lbs_private *priv = cardp->priv;
+       struct lbs_private *priv = cardp->priv;
 
        lbs_deb_enter(LBS_DEB_USB);
 
-       if (priv->adapter->psstate != PS_STATE_FULL_POWER)
+       if (priv->psstate != PS_STATE_FULL_POWER)
                return -1;
 
        if (priv->mesh_dev && !priv->mesh_autostart_enabled) {
@@ -976,7 +983,7 @@ static int if_usb_suspend(struct usb_interface *intf, pm_message_t message)
 static int if_usb_resume(struct usb_interface *intf)
 {
        struct usb_card_rec *cardp = usb_get_intfdata(intf);
-       lbs_private *priv = cardp->priv;
+       struct lbs_private *priv = cardp->priv;
 
        lbs_deb_enter(LBS_DEB_USB);
 
@@ -1009,46 +1016,30 @@ static int if_usb_resume(struct usb_interface *intf)
 #endif
 
 static struct usb_driver if_usb_driver = {
-       /* driver name */
-       .name = usbdriver_name,
-       /* probe function name */
+       .name = DRV_NAME,
        .probe = if_usb_probe,
-       /* disconnect function  name */
        .disconnect = if_usb_disconnect,
-       /* device signature table */
        .id_table = if_usb_table,
        .suspend = if_usb_suspend,
        .resume = if_usb_resume,
 };
 
-static int if_usb_init_module(void)
+static int __init if_usb_init_module(void)
 {
        int ret = 0;
 
        lbs_deb_enter(LBS_DEB_MAIN);
 
-       if (lbs_fw_name == NULL) {
-               lbs_fw_name = default_fw_name;
-       }
-
        ret = usb_register(&if_usb_driver);
 
        lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
        return ret;
 }
 
-static void if_usb_exit_module(void)
+static void __exit if_usb_exit_module(void)
 {
-       struct usb_card_rec *cardp, *cardp_temp;
-
        lbs_deb_enter(LBS_DEB_MAIN);
 
-       list_for_each_entry_safe(cardp, cardp_temp, &usb_devices, list) {
-               lbs_prepare_and_send_command(cardp->priv, CMD_802_11_RESET,
-                                                 CMD_ACT_HALT, 0, 0, NULL);
-       }
-
-       /* API unregisters the driver from USB subsystem */
        usb_deregister(&if_usb_driver);
 
        lbs_deb_leave(LBS_DEB_MAIN);