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