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