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