Luis Carlos Cobo | c305a19 | 2008-08-14 10:41:06 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008, cozybit Inc. |
| 3 | * Copyright (C) 2003-2006, Marvell International Ltd. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or (at |
| 8 | * your option) any later version. |
| 9 | */ |
| 10 | #include <linux/delay.h> |
| 11 | #include <linux/moduleparam.h> |
| 12 | #include <linux/firmware.h> |
| 13 | #include <linux/netdevice.h> |
| 14 | #include <linux/usb.h> |
| 15 | |
| 16 | #define DRV_NAME "lbtf_usb" |
| 17 | |
| 18 | #include "libertas_tf.h" |
| 19 | #include "if_usb.h" |
| 20 | |
| 21 | #define MESSAGE_HEADER_LEN 4 |
| 22 | |
| 23 | static char *lbtf_fw_name = "lbtf_usb.bin"; |
| 24 | module_param_named(fw_name, lbtf_fw_name, charp, 0644); |
| 25 | |
Ben Hutchings | 790e756 | 2009-11-07 22:00:38 +0000 | [diff] [blame] | 26 | MODULE_FIRMWARE("lbtf_usb.bin"); |
| 27 | |
Luis Carlos Cobo | c305a19 | 2008-08-14 10:41:06 -0700 | [diff] [blame] | 28 | static struct usb_device_id if_usb_table[] = { |
| 29 | /* Enter the device signature inside */ |
| 30 | { USB_DEVICE(0x1286, 0x2001) }, |
| 31 | { USB_DEVICE(0x05a3, 0x8388) }, |
| 32 | {} /* Terminating entry */ |
| 33 | }; |
| 34 | |
| 35 | MODULE_DEVICE_TABLE(usb, if_usb_table); |
| 36 | |
| 37 | static void if_usb_receive(struct urb *urb); |
| 38 | static void if_usb_receive_fwload(struct urb *urb); |
| 39 | static int if_usb_prog_firmware(struct if_usb_card *cardp); |
| 40 | static int if_usb_host_to_card(struct lbtf_private *priv, uint8_t type, |
| 41 | uint8_t *payload, uint16_t nb); |
| 42 | static int usb_tx_block(struct if_usb_card *cardp, uint8_t *payload, |
| 43 | uint16_t nb, u8 data); |
| 44 | static void if_usb_free(struct if_usb_card *cardp); |
| 45 | static int if_usb_submit_rx_urb(struct if_usb_card *cardp); |
| 46 | static int if_usb_reset_device(struct if_usb_card *cardp); |
| 47 | |
| 48 | /** |
| 49 | * if_usb_wrike_bulk_callback - call back to handle URB status |
| 50 | * |
| 51 | * @param urb pointer to urb structure |
| 52 | */ |
| 53 | static void if_usb_write_bulk_callback(struct urb *urb) |
| 54 | { |
| 55 | if (urb->status != 0) |
| 56 | printk(KERN_INFO "libertastf: URB in failure status: %d\n", |
| 57 | urb->status); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * if_usb_free - free tx/rx urb, skb and rx buffer |
| 62 | * |
| 63 | * @param cardp pointer if_usb_card |
| 64 | */ |
| 65 | static void if_usb_free(struct if_usb_card *cardp) |
| 66 | { |
| 67 | /* Unlink tx & rx urb */ |
| 68 | usb_kill_urb(cardp->tx_urb); |
| 69 | usb_kill_urb(cardp->rx_urb); |
| 70 | usb_kill_urb(cardp->cmd_urb); |
| 71 | |
| 72 | usb_free_urb(cardp->tx_urb); |
| 73 | cardp->tx_urb = NULL; |
| 74 | |
| 75 | usb_free_urb(cardp->rx_urb); |
| 76 | cardp->rx_urb = NULL; |
| 77 | |
| 78 | usb_free_urb(cardp->cmd_urb); |
| 79 | cardp->cmd_urb = NULL; |
| 80 | |
| 81 | kfree(cardp->ep_out_buf); |
| 82 | cardp->ep_out_buf = NULL; |
| 83 | } |
| 84 | |
| 85 | static void if_usb_setup_firmware(struct lbtf_private *priv) |
| 86 | { |
| 87 | struct if_usb_card *cardp = priv->card; |
| 88 | struct cmd_ds_set_boot2_ver b2_cmd; |
| 89 | |
| 90 | if_usb_submit_rx_urb(cardp); |
| 91 | b2_cmd.hdr.size = cpu_to_le16(sizeof(b2_cmd)); |
| 92 | b2_cmd.action = 0; |
| 93 | b2_cmd.version = cardp->boot2_version; |
| 94 | |
| 95 | if (lbtf_cmd_with_response(priv, CMD_SET_BOOT2_VER, &b2_cmd)) |
| 96 | printk(KERN_INFO "libertastf: setting boot2 version failed\n"); |
| 97 | } |
| 98 | |
| 99 | static void if_usb_fw_timeo(unsigned long priv) |
| 100 | { |
| 101 | struct if_usb_card *cardp = (void *)priv; |
| 102 | |
| 103 | if (!cardp->fwdnldover) |
| 104 | /* Download timed out */ |
| 105 | cardp->priv->surpriseremoved = 1; |
| 106 | wake_up(&cardp->fw_wq); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * if_usb_probe - sets the configuration values |
| 111 | * |
| 112 | * @ifnum interface number |
| 113 | * @id pointer to usb_device_id |
| 114 | * |
| 115 | * Returns: 0 on success, error code on failure |
| 116 | */ |
| 117 | static int if_usb_probe(struct usb_interface *intf, |
| 118 | const struct usb_device_id *id) |
| 119 | { |
| 120 | struct usb_device *udev; |
| 121 | struct usb_host_interface *iface_desc; |
| 122 | struct usb_endpoint_descriptor *endpoint; |
| 123 | struct lbtf_private *priv; |
| 124 | struct if_usb_card *cardp; |
| 125 | int i; |
| 126 | |
| 127 | udev = interface_to_usbdev(intf); |
| 128 | |
| 129 | cardp = kzalloc(sizeof(struct if_usb_card), GFP_KERNEL); |
| 130 | if (!cardp) |
| 131 | goto error; |
| 132 | |
| 133 | setup_timer(&cardp->fw_timeout, if_usb_fw_timeo, (unsigned long)cardp); |
| 134 | init_waitqueue_head(&cardp->fw_wq); |
| 135 | |
| 136 | cardp->udev = udev; |
| 137 | iface_desc = intf->cur_altsetting; |
| 138 | |
| 139 | for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { |
| 140 | endpoint = &iface_desc->endpoint[i].desc; |
| 141 | if (usb_endpoint_is_bulk_in(endpoint)) { |
| 142 | cardp->ep_in_size = |
| 143 | le16_to_cpu(endpoint->wMaxPacketSize); |
| 144 | cardp->ep_in = usb_endpoint_num(endpoint); |
| 145 | } else if (usb_endpoint_is_bulk_out(endpoint)) { |
| 146 | cardp->ep_out_size = |
| 147 | le16_to_cpu(endpoint->wMaxPacketSize); |
| 148 | cardp->ep_out = usb_endpoint_num(endpoint); |
| 149 | } |
| 150 | } |
| 151 | if (!cardp->ep_out_size || !cardp->ep_in_size) |
| 152 | /* Endpoints not found */ |
| 153 | goto dealloc; |
| 154 | |
| 155 | cardp->rx_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 156 | if (!cardp->rx_urb) |
| 157 | goto dealloc; |
| 158 | |
| 159 | cardp->tx_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 160 | if (!cardp->tx_urb) |
| 161 | goto dealloc; |
| 162 | |
| 163 | cardp->cmd_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 164 | if (!cardp->cmd_urb) |
| 165 | goto dealloc; |
| 166 | |
| 167 | cardp->ep_out_buf = kmalloc(MRVDRV_ETH_TX_PACKET_BUFFER_SIZE, |
| 168 | GFP_KERNEL); |
| 169 | if (!cardp->ep_out_buf) |
| 170 | goto dealloc; |
| 171 | |
| 172 | priv = lbtf_add_card(cardp, &udev->dev); |
| 173 | if (!priv) |
| 174 | goto dealloc; |
| 175 | |
| 176 | cardp->priv = priv; |
| 177 | |
| 178 | priv->hw_host_to_card = if_usb_host_to_card; |
| 179 | priv->hw_prog_firmware = if_usb_prog_firmware; |
| 180 | priv->hw_reset_device = if_usb_reset_device; |
| 181 | cardp->boot2_version = udev->descriptor.bcdDevice; |
| 182 | |
| 183 | usb_get_dev(udev); |
| 184 | usb_set_intfdata(intf, cardp); |
| 185 | |
| 186 | return 0; |
| 187 | |
| 188 | dealloc: |
| 189 | if_usb_free(cardp); |
| 190 | error: |
| 191 | return -ENOMEM; |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * if_usb_disconnect - free resource and cleanup |
| 196 | * |
| 197 | * @intf USB interface structure |
| 198 | */ |
| 199 | static void if_usb_disconnect(struct usb_interface *intf) |
| 200 | { |
| 201 | struct if_usb_card *cardp = usb_get_intfdata(intf); |
| 202 | struct lbtf_private *priv = (struct lbtf_private *) cardp->priv; |
| 203 | |
| 204 | if_usb_reset_device(cardp); |
| 205 | |
| 206 | if (priv) |
| 207 | lbtf_remove_card(priv); |
| 208 | |
| 209 | /* Unlink and free urb */ |
| 210 | if_usb_free(cardp); |
| 211 | |
| 212 | usb_set_intfdata(intf, NULL); |
| 213 | usb_put_dev(interface_to_usbdev(intf)); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * if_usb_send_fw_pkt - This function downloads the FW |
| 218 | * |
| 219 | * @priv pointer to struct lbtf_private |
| 220 | * |
| 221 | * Returns: 0 |
| 222 | */ |
| 223 | static int if_usb_send_fw_pkt(struct if_usb_card *cardp) |
| 224 | { |
| 225 | struct fwdata *fwdata = cardp->ep_out_buf; |
| 226 | u8 *firmware = (u8 *) cardp->fw->data; |
| 227 | |
| 228 | /* If we got a CRC failure on the last block, back |
| 229 | up and retry it */ |
| 230 | if (!cardp->CRC_OK) { |
| 231 | cardp->totalbytes = cardp->fwlastblksent; |
| 232 | cardp->fwseqnum--; |
| 233 | } |
| 234 | |
| 235 | /* struct fwdata (which we sent to the card) has an |
| 236 | extra __le32 field in between the header and the data, |
| 237 | which is not in the struct fwheader in the actual |
| 238 | firmware binary. Insert the seqnum in the middle... */ |
| 239 | memcpy(&fwdata->hdr, &firmware[cardp->totalbytes], |
| 240 | sizeof(struct fwheader)); |
| 241 | |
| 242 | cardp->fwlastblksent = cardp->totalbytes; |
| 243 | cardp->totalbytes += sizeof(struct fwheader); |
| 244 | |
| 245 | memcpy(fwdata->data, &firmware[cardp->totalbytes], |
| 246 | le32_to_cpu(fwdata->hdr.datalength)); |
| 247 | |
| 248 | fwdata->seqnum = cpu_to_le32(++cardp->fwseqnum); |
| 249 | cardp->totalbytes += le32_to_cpu(fwdata->hdr.datalength); |
| 250 | |
| 251 | usb_tx_block(cardp, cardp->ep_out_buf, sizeof(struct fwdata) + |
| 252 | le32_to_cpu(fwdata->hdr.datalength), 0); |
| 253 | |
| 254 | if (fwdata->hdr.dnldcmd == cpu_to_le32(FW_HAS_LAST_BLOCK)) |
| 255 | /* Host has finished FW downloading |
| 256 | * Donwloading FW JUMP BLOCK |
| 257 | */ |
| 258 | cardp->fwfinalblk = 1; |
| 259 | |
| 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | static int if_usb_reset_device(struct if_usb_card *cardp) |
| 264 | { |
| 265 | struct cmd_ds_802_11_reset *cmd = cardp->ep_out_buf + 4; |
| 266 | int ret; |
| 267 | |
| 268 | *(__le32 *)cardp->ep_out_buf = cpu_to_le32(CMD_TYPE_REQUEST); |
| 269 | |
| 270 | cmd->hdr.command = cpu_to_le16(CMD_802_11_RESET); |
| 271 | cmd->hdr.size = cpu_to_le16(sizeof(struct cmd_ds_802_11_reset)); |
| 272 | cmd->hdr.result = cpu_to_le16(0); |
| 273 | cmd->hdr.seqnum = cpu_to_le16(0x5a5a); |
| 274 | cmd->action = cpu_to_le16(CMD_ACT_HALT); |
| 275 | usb_tx_block(cardp, cardp->ep_out_buf, |
| 276 | 4 + sizeof(struct cmd_ds_802_11_reset), 0); |
| 277 | |
| 278 | msleep(100); |
| 279 | ret = usb_reset_device(cardp->udev); |
| 280 | msleep(100); |
| 281 | |
| 282 | return ret; |
| 283 | } |
| 284 | EXPORT_SYMBOL_GPL(if_usb_reset_device); |
| 285 | |
| 286 | /** |
| 287 | * usb_tx_block - transfer data to the device |
| 288 | * |
| 289 | * @priv pointer to struct lbtf_private |
| 290 | * @payload pointer to payload data |
| 291 | * @nb data length |
| 292 | * @data non-zero for data, zero for commands |
| 293 | * |
| 294 | * Returns: 0 on success, nonzero otherwise. |
| 295 | */ |
| 296 | static int usb_tx_block(struct if_usb_card *cardp, uint8_t *payload, |
| 297 | uint16_t nb, u8 data) |
| 298 | { |
| 299 | struct urb *urb; |
| 300 | |
| 301 | /* check if device is removed */ |
| 302 | if (cardp->priv->surpriseremoved) |
| 303 | return -1; |
| 304 | |
| 305 | if (data) |
| 306 | urb = cardp->tx_urb; |
| 307 | else |
| 308 | urb = cardp->cmd_urb; |
| 309 | |
| 310 | usb_fill_bulk_urb(urb, cardp->udev, |
| 311 | usb_sndbulkpipe(cardp->udev, |
| 312 | cardp->ep_out), |
| 313 | payload, nb, if_usb_write_bulk_callback, cardp); |
| 314 | |
| 315 | urb->transfer_flags |= URB_ZERO_PACKET; |
| 316 | |
| 317 | if (usb_submit_urb(urb, GFP_ATOMIC)) |
| 318 | return -1; |
| 319 | return 0; |
| 320 | } |
| 321 | |
| 322 | static int __if_usb_submit_rx_urb(struct if_usb_card *cardp, |
| 323 | void (*callbackfn)(struct urb *urb)) |
| 324 | { |
| 325 | struct sk_buff *skb; |
| 326 | |
| 327 | skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE); |
| 328 | if (!skb) |
| 329 | return -1; |
| 330 | |
| 331 | cardp->rx_skb = skb; |
| 332 | |
| 333 | /* Fill the receive configuration URB and initialise the Rx call back */ |
| 334 | usb_fill_bulk_urb(cardp->rx_urb, cardp->udev, |
| 335 | usb_rcvbulkpipe(cardp->udev, cardp->ep_in), |
Johannes Berg | 9b44fb8 | 2008-10-29 23:24:14 +0100 | [diff] [blame] | 336 | skb_tail_pointer(skb), |
Luis Carlos Cobo | c305a19 | 2008-08-14 10:41:06 -0700 | [diff] [blame] | 337 | MRVDRV_ETH_RX_PACKET_BUFFER_SIZE, callbackfn, cardp); |
| 338 | |
| 339 | cardp->rx_urb->transfer_flags |= URB_ZERO_PACKET; |
| 340 | |
| 341 | if (usb_submit_urb(cardp->rx_urb, GFP_ATOMIC)) { |
| 342 | kfree_skb(skb); |
| 343 | cardp->rx_skb = NULL; |
| 344 | return -1; |
| 345 | } else |
| 346 | return 0; |
| 347 | } |
| 348 | |
| 349 | static int if_usb_submit_rx_urb_fwload(struct if_usb_card *cardp) |
| 350 | { |
| 351 | return __if_usb_submit_rx_urb(cardp, &if_usb_receive_fwload); |
| 352 | } |
| 353 | |
| 354 | static int if_usb_submit_rx_urb(struct if_usb_card *cardp) |
| 355 | { |
| 356 | return __if_usb_submit_rx_urb(cardp, &if_usb_receive); |
| 357 | } |
| 358 | |
| 359 | static void if_usb_receive_fwload(struct urb *urb) |
| 360 | { |
| 361 | struct if_usb_card *cardp = urb->context; |
| 362 | struct sk_buff *skb = cardp->rx_skb; |
| 363 | struct fwsyncheader *syncfwheader; |
| 364 | struct bootcmdresp bcmdresp; |
| 365 | |
| 366 | if (urb->status) { |
| 367 | kfree_skb(skb); |
| 368 | return; |
| 369 | } |
| 370 | |
| 371 | if (cardp->fwdnldover) { |
| 372 | __le32 *tmp = (__le32 *)(skb->data); |
| 373 | |
| 374 | if (tmp[0] == cpu_to_le32(CMD_TYPE_INDICATION) && |
| 375 | tmp[1] == cpu_to_le32(MACREG_INT_CODE_FIRMWARE_READY)) |
| 376 | /* Firmware ready event received */ |
| 377 | wake_up(&cardp->fw_wq); |
| 378 | else |
| 379 | if_usb_submit_rx_urb_fwload(cardp); |
| 380 | kfree_skb(skb); |
| 381 | return; |
| 382 | } |
| 383 | if (cardp->bootcmdresp <= 0) { |
| 384 | memcpy(&bcmdresp, skb->data, sizeof(bcmdresp)); |
| 385 | |
| 386 | if (le16_to_cpu(cardp->udev->descriptor.bcdDevice) < 0x3106) { |
| 387 | kfree_skb(skb); |
| 388 | if_usb_submit_rx_urb_fwload(cardp); |
| 389 | cardp->bootcmdresp = 1; |
| 390 | /* Received valid boot command response */ |
| 391 | return; |
| 392 | } |
| 393 | if (bcmdresp.magic != cpu_to_le32(BOOT_CMD_MAGIC_NUMBER)) { |
| 394 | if (bcmdresp.magic == cpu_to_le32(CMD_TYPE_REQUEST) || |
| 395 | bcmdresp.magic == cpu_to_le32(CMD_TYPE_DATA) || |
| 396 | bcmdresp.magic == cpu_to_le32(CMD_TYPE_INDICATION)) |
| 397 | cardp->bootcmdresp = -1; |
| 398 | } else if (bcmdresp.cmd == BOOT_CMD_FW_BY_USB && |
| 399 | bcmdresp.result == BOOT_CMD_RESP_OK) |
| 400 | cardp->bootcmdresp = 1; |
| 401 | |
| 402 | kfree_skb(skb); |
| 403 | if_usb_submit_rx_urb_fwload(cardp); |
| 404 | return; |
| 405 | } |
| 406 | |
| 407 | syncfwheader = kmalloc(sizeof(struct fwsyncheader), GFP_ATOMIC); |
| 408 | if (!syncfwheader) { |
| 409 | kfree_skb(skb); |
| 410 | return; |
| 411 | } |
| 412 | |
| 413 | memcpy(syncfwheader, skb->data, sizeof(struct fwsyncheader)); |
| 414 | |
| 415 | if (!syncfwheader->cmd) |
| 416 | cardp->CRC_OK = 1; |
| 417 | else |
| 418 | cardp->CRC_OK = 0; |
| 419 | kfree_skb(skb); |
| 420 | |
| 421 | /* reschedule timer for 200ms hence */ |
| 422 | mod_timer(&cardp->fw_timeout, jiffies + (HZ/5)); |
| 423 | |
| 424 | if (cardp->fwfinalblk) { |
| 425 | cardp->fwdnldover = 1; |
| 426 | goto exit; |
| 427 | } |
| 428 | |
| 429 | if_usb_send_fw_pkt(cardp); |
| 430 | |
| 431 | exit: |
| 432 | if_usb_submit_rx_urb_fwload(cardp); |
| 433 | |
| 434 | kfree(syncfwheader); |
| 435 | |
| 436 | return; |
| 437 | } |
| 438 | |
| 439 | #define MRVDRV_MIN_PKT_LEN 30 |
| 440 | |
| 441 | static inline void process_cmdtypedata(int recvlength, struct sk_buff *skb, |
| 442 | struct if_usb_card *cardp, |
| 443 | struct lbtf_private *priv) |
| 444 | { |
| 445 | if (recvlength > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + MESSAGE_HEADER_LEN |
| 446 | || recvlength < MRVDRV_MIN_PKT_LEN) { |
| 447 | kfree_skb(skb); |
| 448 | return; |
| 449 | } |
| 450 | |
| 451 | skb_put(skb, recvlength); |
| 452 | skb_pull(skb, MESSAGE_HEADER_LEN); |
| 453 | lbtf_rx(priv, skb); |
| 454 | } |
| 455 | |
| 456 | static inline void process_cmdrequest(int recvlength, uint8_t *recvbuff, |
| 457 | struct sk_buff *skb, |
| 458 | struct if_usb_card *cardp, |
| 459 | struct lbtf_private *priv) |
| 460 | { |
| 461 | if (recvlength > LBS_CMD_BUFFER_SIZE) { |
| 462 | kfree_skb(skb); |
| 463 | return; |
| 464 | } |
| 465 | |
Alexander Beregalov | 0ee904c | 2009-04-11 14:50:23 +0000 | [diff] [blame] | 466 | BUG_ON(!in_interrupt()); |
Luis Carlos Cobo | c305a19 | 2008-08-14 10:41:06 -0700 | [diff] [blame] | 467 | |
| 468 | spin_lock(&priv->driver_lock); |
| 469 | memcpy(priv->cmd_resp_buff, recvbuff + MESSAGE_HEADER_LEN, |
| 470 | recvlength - MESSAGE_HEADER_LEN); |
| 471 | kfree_skb(skb); |
| 472 | lbtf_cmd_response_rx(priv); |
| 473 | spin_unlock(&priv->driver_lock); |
| 474 | } |
| 475 | |
| 476 | /** |
| 477 | * if_usb_receive - read data received from the device. |
| 478 | * |
| 479 | * @urb pointer to struct urb |
| 480 | */ |
| 481 | static void if_usb_receive(struct urb *urb) |
| 482 | { |
| 483 | struct if_usb_card *cardp = urb->context; |
| 484 | struct sk_buff *skb = cardp->rx_skb; |
| 485 | struct lbtf_private *priv = cardp->priv; |
| 486 | int recvlength = urb->actual_length; |
| 487 | uint8_t *recvbuff = NULL; |
| 488 | uint32_t recvtype = 0; |
| 489 | __le32 *pkt = (__le32 *) skb->data; |
| 490 | |
| 491 | if (recvlength) { |
| 492 | if (urb->status) { |
| 493 | kfree_skb(skb); |
| 494 | goto setup_for_next; |
| 495 | } |
| 496 | |
| 497 | recvbuff = skb->data; |
| 498 | recvtype = le32_to_cpu(pkt[0]); |
| 499 | } else if (urb->status) { |
| 500 | kfree_skb(skb); |
| 501 | return; |
| 502 | } |
| 503 | |
| 504 | switch (recvtype) { |
| 505 | case CMD_TYPE_DATA: |
| 506 | process_cmdtypedata(recvlength, skb, cardp, priv); |
| 507 | break; |
| 508 | |
| 509 | case CMD_TYPE_REQUEST: |
| 510 | process_cmdrequest(recvlength, recvbuff, skb, cardp, priv); |
| 511 | break; |
| 512 | |
| 513 | case CMD_TYPE_INDICATION: |
| 514 | { |
| 515 | /* Event cause handling */ |
| 516 | u32 event_cause = le32_to_cpu(pkt[1]); |
| 517 | |
| 518 | /* Icky undocumented magic special case */ |
| 519 | if (event_cause & 0xffff0000) { |
| 520 | u16 tmp; |
| 521 | u8 retrycnt; |
| 522 | u8 failure; |
| 523 | |
| 524 | tmp = event_cause >> 16; |
| 525 | retrycnt = tmp & 0x00ff; |
| 526 | failure = (tmp & 0xff00) >> 8; |
| 527 | lbtf_send_tx_feedback(priv, retrycnt, failure); |
| 528 | } else if (event_cause == LBTF_EVENT_BCN_SENT) |
| 529 | lbtf_bcn_sent(priv); |
| 530 | else |
| 531 | printk(KERN_DEBUG |
| 532 | "Unsupported notification %d received\n", |
| 533 | event_cause); |
| 534 | kfree_skb(skb); |
| 535 | break; |
| 536 | } |
| 537 | default: |
| 538 | printk(KERN_DEBUG "libertastf: unknown command type 0x%X\n", |
| 539 | recvtype); |
| 540 | kfree_skb(skb); |
| 541 | break; |
| 542 | } |
| 543 | |
| 544 | setup_for_next: |
| 545 | if_usb_submit_rx_urb(cardp); |
| 546 | } |
| 547 | |
| 548 | /** |
| 549 | * if_usb_host_to_card - Download data to the device |
| 550 | * |
| 551 | * @priv pointer to struct lbtf_private structure |
| 552 | * @type type of data |
| 553 | * @buf pointer to data buffer |
| 554 | * @len number of bytes |
| 555 | * |
| 556 | * Returns: 0 on success, nonzero otherwise |
| 557 | */ |
| 558 | static int if_usb_host_to_card(struct lbtf_private *priv, uint8_t type, |
| 559 | uint8_t *payload, uint16_t nb) |
| 560 | { |
| 561 | struct if_usb_card *cardp = priv->card; |
| 562 | u8 data = 0; |
| 563 | |
| 564 | if (type == MVMS_CMD) { |
| 565 | *(__le32 *)cardp->ep_out_buf = cpu_to_le32(CMD_TYPE_REQUEST); |
| 566 | } else { |
| 567 | *(__le32 *)cardp->ep_out_buf = cpu_to_le32(CMD_TYPE_DATA); |
| 568 | data = 1; |
| 569 | } |
| 570 | |
| 571 | memcpy((cardp->ep_out_buf + MESSAGE_HEADER_LEN), payload, nb); |
| 572 | |
| 573 | return usb_tx_block(cardp, cardp->ep_out_buf, nb + MESSAGE_HEADER_LEN, |
| 574 | data); |
| 575 | } |
| 576 | |
| 577 | /** |
| 578 | * if_usb_issue_boot_command - Issue boot command to Boot2. |
| 579 | * |
| 580 | * @ivalue 1 boots from FW by USB-Download, 2 boots from FW in EEPROM. |
| 581 | * |
| 582 | * Returns: 0 |
| 583 | */ |
| 584 | static int if_usb_issue_boot_command(struct if_usb_card *cardp, int ivalue) |
| 585 | { |
| 586 | struct bootcmd *bootcmd = cardp->ep_out_buf; |
| 587 | |
| 588 | /* Prepare command */ |
| 589 | bootcmd->magic = cpu_to_le32(BOOT_CMD_MAGIC_NUMBER); |
| 590 | bootcmd->cmd = ivalue; |
| 591 | memset(bootcmd->pad, 0, sizeof(bootcmd->pad)); |
| 592 | |
| 593 | /* Issue command */ |
| 594 | usb_tx_block(cardp, cardp->ep_out_buf, sizeof(*bootcmd), 0); |
| 595 | |
| 596 | return 0; |
| 597 | } |
| 598 | |
| 599 | |
| 600 | /** |
| 601 | * check_fwfile_format - Check the validity of Boot2/FW image. |
| 602 | * |
| 603 | * @data pointer to image |
| 604 | * @totlen image length |
| 605 | * |
| 606 | * Returns: 0 if the image is valid, nonzero otherwise. |
| 607 | */ |
| 608 | static int check_fwfile_format(const u8 *data, u32 totlen) |
| 609 | { |
| 610 | u32 bincmd, exit; |
| 611 | u32 blksize, offset, len; |
| 612 | int ret; |
| 613 | |
| 614 | ret = 1; |
| 615 | exit = len = 0; |
| 616 | |
| 617 | do { |
| 618 | struct fwheader *fwh = (void *) data; |
| 619 | |
| 620 | bincmd = le32_to_cpu(fwh->dnldcmd); |
| 621 | blksize = le32_to_cpu(fwh->datalength); |
| 622 | switch (bincmd) { |
| 623 | case FW_HAS_DATA_TO_RECV: |
| 624 | offset = sizeof(struct fwheader) + blksize; |
| 625 | data += offset; |
| 626 | len += offset; |
| 627 | if (len >= totlen) |
| 628 | exit = 1; |
| 629 | break; |
| 630 | case FW_HAS_LAST_BLOCK: |
| 631 | exit = 1; |
| 632 | ret = 0; |
| 633 | break; |
| 634 | default: |
| 635 | exit = 1; |
| 636 | break; |
| 637 | } |
| 638 | } while (!exit); |
| 639 | |
| 640 | if (ret) |
| 641 | printk(KERN_INFO |
| 642 | "libertastf: firmware file format check failed\n"); |
| 643 | return ret; |
| 644 | } |
| 645 | |
| 646 | |
| 647 | static int if_usb_prog_firmware(struct if_usb_card *cardp) |
| 648 | { |
| 649 | int i = 0; |
| 650 | static int reset_count = 10; |
| 651 | int ret = 0; |
| 652 | |
| 653 | ret = request_firmware(&cardp->fw, lbtf_fw_name, &cardp->udev->dev); |
| 654 | if (ret < 0) { |
| 655 | printk(KERN_INFO "libertastf: firmware %s not found\n", |
| 656 | lbtf_fw_name); |
| 657 | goto done; |
| 658 | } |
| 659 | |
| 660 | if (check_fwfile_format(cardp->fw->data, cardp->fw->size)) |
| 661 | goto release_fw; |
| 662 | |
| 663 | restart: |
| 664 | if (if_usb_submit_rx_urb_fwload(cardp) < 0) { |
| 665 | ret = -1; |
| 666 | goto release_fw; |
| 667 | } |
| 668 | |
| 669 | cardp->bootcmdresp = 0; |
| 670 | do { |
| 671 | int j = 0; |
| 672 | i++; |
| 673 | /* Issue Boot command = 1, Boot from Download-FW */ |
| 674 | if_usb_issue_boot_command(cardp, BOOT_CMD_FW_BY_USB); |
| 675 | /* wait for command response */ |
| 676 | do { |
| 677 | j++; |
| 678 | msleep_interruptible(100); |
| 679 | } while (cardp->bootcmdresp == 0 && j < 10); |
| 680 | } while (cardp->bootcmdresp == 0 && i < 5); |
| 681 | |
| 682 | if (cardp->bootcmdresp <= 0) { |
| 683 | if (--reset_count >= 0) { |
| 684 | if_usb_reset_device(cardp); |
| 685 | goto restart; |
| 686 | } |
| 687 | return -1; |
| 688 | } |
| 689 | |
| 690 | i = 0; |
| 691 | |
| 692 | cardp->totalbytes = 0; |
| 693 | cardp->fwlastblksent = 0; |
| 694 | cardp->CRC_OK = 1; |
| 695 | cardp->fwdnldover = 0; |
| 696 | cardp->fwseqnum = -1; |
| 697 | cardp->totalbytes = 0; |
| 698 | cardp->fwfinalblk = 0; |
| 699 | |
| 700 | /* Send the first firmware packet... */ |
| 701 | if_usb_send_fw_pkt(cardp); |
| 702 | |
| 703 | /* ... and wait for the process to complete */ |
| 704 | wait_event_interruptible(cardp->fw_wq, cardp->priv->surpriseremoved || |
| 705 | cardp->fwdnldover); |
| 706 | |
| 707 | del_timer_sync(&cardp->fw_timeout); |
| 708 | usb_kill_urb(cardp->rx_urb); |
| 709 | |
| 710 | if (!cardp->fwdnldover) { |
| 711 | printk(KERN_INFO "libertastf: failed to load fw," |
| 712 | " resetting device!\n"); |
| 713 | if (--reset_count >= 0) { |
| 714 | if_usb_reset_device(cardp); |
| 715 | goto restart; |
| 716 | } |
| 717 | |
| 718 | printk(KERN_INFO "libertastf: fw download failure\n"); |
| 719 | ret = -1; |
| 720 | goto release_fw; |
| 721 | } |
| 722 | |
| 723 | cardp->priv->fw_ready = 1; |
| 724 | |
| 725 | release_fw: |
| 726 | release_firmware(cardp->fw); |
| 727 | cardp->fw = NULL; |
| 728 | |
| 729 | if_usb_setup_firmware(cardp->priv); |
| 730 | |
| 731 | done: |
| 732 | return ret; |
| 733 | } |
| 734 | EXPORT_SYMBOL_GPL(if_usb_prog_firmware); |
| 735 | |
| 736 | |
| 737 | #define if_usb_suspend NULL |
| 738 | #define if_usb_resume NULL |
| 739 | |
| 740 | static struct usb_driver if_usb_driver = { |
| 741 | .name = DRV_NAME, |
| 742 | .probe = if_usb_probe, |
| 743 | .disconnect = if_usb_disconnect, |
| 744 | .id_table = if_usb_table, |
| 745 | .suspend = if_usb_suspend, |
| 746 | .resume = if_usb_resume, |
| 747 | }; |
| 748 | |
| 749 | static int __init if_usb_init_module(void) |
| 750 | { |
| 751 | int ret = 0; |
| 752 | |
| 753 | ret = usb_register(&if_usb_driver); |
| 754 | return ret; |
| 755 | } |
| 756 | |
| 757 | static void __exit if_usb_exit_module(void) |
| 758 | { |
| 759 | usb_deregister(&if_usb_driver); |
| 760 | } |
| 761 | |
| 762 | module_init(if_usb_init_module); |
| 763 | module_exit(if_usb_exit_module); |
| 764 | |
| 765 | MODULE_DESCRIPTION("8388 USB WLAN Thinfirm Driver"); |
| 766 | MODULE_AUTHOR("Cozybit Inc."); |
| 767 | MODULE_LICENSE("GPL"); |