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