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 #define DRV_NAME "lbtf_usb" 11 12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 13 14 #include "libertas_tf.h" 15 #include "if_usb.h" 16 17 #include <linux/delay.h> 18 #include <linux/module.h> 19 #include <linux/firmware.h> 20 #include <linux/netdevice.h> 21 #include <linux/slab.h> 22 #include <linux/usb.h> 23 24 #define INSANEDEBUG 0 25 #define lbtf_deb_usb2(...) do { if (INSANEDEBUG) lbtf_deb_usbd(__VA_ARGS__); } while (0) 26 27 #define MESSAGE_HEADER_LEN 4 28 29 static char *lbtf_fw_name = "lbtf_usb.bin"; 30 module_param_named(fw_name, lbtf_fw_name, charp, 0644); 31 32 MODULE_FIRMWARE("lbtf_usb.bin"); 33 34 static const struct usb_device_id if_usb_table[] = { 35 /* Enter the device signature inside */ 36 { USB_DEVICE(0x1286, 0x2001) }, 37 { USB_DEVICE(0x05a3, 0x8388) }, 38 {} /* Terminating entry */ 39 }; 40 41 MODULE_DEVICE_TABLE(usb, if_usb_table); 42 43 static void if_usb_receive(struct urb *urb); 44 static void if_usb_receive_fwload(struct urb *urb); 45 static int if_usb_prog_firmware(struct lbtf_private *priv); 46 static int if_usb_host_to_card(struct lbtf_private *priv, uint8_t type, 47 uint8_t *payload, uint16_t nb); 48 static int usb_tx_block(struct if_usb_card *cardp, uint8_t *payload, 49 uint16_t nb, u8 data); 50 static void if_usb_free(struct if_usb_card *cardp); 51 static int if_usb_submit_rx_urb(struct if_usb_card *cardp); 52 static int if_usb_reset_device(struct lbtf_private *priv); 53 54 /** 55 * if_usb_wrike_bulk_callback - call back to handle URB status 56 * 57 * @param urb pointer to urb structure 58 */ 59 static void if_usb_write_bulk_callback(struct urb *urb) 60 { 61 if (urb->status != 0) { 62 /* print the failure status number for debug */ 63 pr_info("URB in failure status: %d\n", urb->status); 64 } else { 65 lbtf_deb_usb2(&urb->dev->dev, "URB status is successful\n"); 66 lbtf_deb_usb2(&urb->dev->dev, "Actual length transmitted %d\n", 67 urb->actual_length); 68 } 69 } 70 71 /** 72 * if_usb_free - free tx/rx urb, skb and rx buffer 73 * 74 * @param cardp pointer if_usb_card 75 */ 76 static void if_usb_free(struct if_usb_card *cardp) 77 { 78 lbtf_deb_enter(LBTF_DEB_USB); 79 80 /* Unlink tx & rx urb */ 81 usb_kill_urb(cardp->tx_urb); 82 usb_kill_urb(cardp->rx_urb); 83 usb_kill_urb(cardp->cmd_urb); 84 85 usb_free_urb(cardp->tx_urb); 86 cardp->tx_urb = NULL; 87 88 usb_free_urb(cardp->rx_urb); 89 cardp->rx_urb = NULL; 90 91 usb_free_urb(cardp->cmd_urb); 92 cardp->cmd_urb = NULL; 93 94 kfree(cardp->ep_out_buf); 95 cardp->ep_out_buf = NULL; 96 97 lbtf_deb_leave(LBTF_DEB_USB); 98 } 99 100 static void if_usb_setup_firmware(struct lbtf_private *priv) 101 { 102 struct if_usb_card *cardp = priv->card; 103 struct cmd_ds_set_boot2_ver b2_cmd; 104 105 lbtf_deb_enter(LBTF_DEB_USB); 106 107 if_usb_submit_rx_urb(cardp); 108 b2_cmd.hdr.size = cpu_to_le16(sizeof(b2_cmd)); 109 b2_cmd.action = 0; 110 b2_cmd.version = cardp->boot2_version; 111 112 if (lbtf_cmd_with_response(priv, CMD_SET_BOOT2_VER, &b2_cmd)) 113 lbtf_deb_usb("Setting boot2 version failed\n"); 114 115 lbtf_deb_leave(LBTF_DEB_USB); 116 } 117 118 static void if_usb_fw_timeo(struct timer_list *t) 119 { 120 struct if_usb_card *cardp = from_timer(cardp, t, fw_timeout); 121 122 lbtf_deb_enter(LBTF_DEB_USB); 123 if (!cardp->fwdnldover) { 124 /* Download timed out */ 125 cardp->priv->surpriseremoved = 1; 126 pr_err("Download timed out\n"); 127 } else { 128 lbtf_deb_usb("Download complete, no event. Assuming success\n"); 129 } 130 wake_up(&cardp->fw_wq); 131 lbtf_deb_leave(LBTF_DEB_USB); 132 } 133 134 static const struct lbtf_ops if_usb_ops = { 135 .hw_host_to_card = if_usb_host_to_card, 136 .hw_prog_firmware = if_usb_prog_firmware, 137 .hw_reset_device = if_usb_reset_device, 138 }; 139 140 /** 141 * if_usb_probe - sets the configuration values 142 * 143 * @ifnum interface number 144 * @id pointer to usb_device_id 145 * 146 * Returns: 0 on success, error code on failure 147 */ 148 static int if_usb_probe(struct usb_interface *intf, 149 const struct usb_device_id *id) 150 { 151 struct usb_device *udev; 152 struct usb_host_interface *iface_desc; 153 struct usb_endpoint_descriptor *endpoint; 154 struct lbtf_private *priv; 155 struct if_usb_card *cardp; 156 int i; 157 158 lbtf_deb_enter(LBTF_DEB_USB); 159 udev = interface_to_usbdev(intf); 160 161 cardp = kzalloc(sizeof(struct if_usb_card), GFP_KERNEL); 162 if (!cardp) 163 goto error; 164 165 timer_setup(&cardp->fw_timeout, if_usb_fw_timeo, 0); 166 init_waitqueue_head(&cardp->fw_wq); 167 168 cardp->udev = udev; 169 iface_desc = intf->cur_altsetting; 170 171 lbtf_deb_usbd(&udev->dev, "bcdUSB = 0x%X bDeviceClass = 0x%X" 172 " bDeviceSubClass = 0x%X, bDeviceProtocol = 0x%X\n", 173 le16_to_cpu(udev->descriptor.bcdUSB), 174 udev->descriptor.bDeviceClass, 175 udev->descriptor.bDeviceSubClass, 176 udev->descriptor.bDeviceProtocol); 177 178 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { 179 endpoint = &iface_desc->endpoint[i].desc; 180 if (usb_endpoint_is_bulk_in(endpoint)) { 181 cardp->ep_in_size = 182 le16_to_cpu(endpoint->wMaxPacketSize); 183 cardp->ep_in = usb_endpoint_num(endpoint); 184 185 lbtf_deb_usbd(&udev->dev, "in_endpoint = %d\n", 186 cardp->ep_in); 187 lbtf_deb_usbd(&udev->dev, "Bulk in size is %d\n", 188 cardp->ep_in_size); 189 } else if (usb_endpoint_is_bulk_out(endpoint)) { 190 cardp->ep_out_size = 191 le16_to_cpu(endpoint->wMaxPacketSize); 192 cardp->ep_out = usb_endpoint_num(endpoint); 193 194 lbtf_deb_usbd(&udev->dev, "out_endpoint = %d\n", 195 cardp->ep_out); 196 lbtf_deb_usbd(&udev->dev, "Bulk out size is %d\n", 197 cardp->ep_out_size); 198 } 199 } 200 if (!cardp->ep_out_size || !cardp->ep_in_size) { 201 lbtf_deb_usbd(&udev->dev, "Endpoints not found\n"); 202 /* Endpoints not found */ 203 goto dealloc; 204 } 205 206 cardp->rx_urb = usb_alloc_urb(0, GFP_KERNEL); 207 if (!cardp->rx_urb) 208 goto dealloc; 209 210 cardp->tx_urb = usb_alloc_urb(0, GFP_KERNEL); 211 if (!cardp->tx_urb) 212 goto dealloc; 213 214 cardp->cmd_urb = usb_alloc_urb(0, GFP_KERNEL); 215 if (!cardp->cmd_urb) 216 goto dealloc; 217 218 cardp->ep_out_buf = kmalloc(MRVDRV_ETH_TX_PACKET_BUFFER_SIZE, 219 GFP_KERNEL); 220 if (!cardp->ep_out_buf) { 221 lbtf_deb_usbd(&udev->dev, "Could not allocate buffer\n"); 222 goto dealloc; 223 } 224 225 cardp->boot2_version = udev->descriptor.bcdDevice; 226 priv = lbtf_add_card(cardp, &udev->dev, &if_usb_ops); 227 if (!priv) 228 goto dealloc; 229 230 usb_get_dev(udev); 231 usb_set_intfdata(intf, cardp); 232 233 return 0; 234 235 dealloc: 236 if_usb_free(cardp); 237 error: 238 lbtf_deb_leave(LBTF_DEB_MAIN); 239 return -ENOMEM; 240 } 241 242 /** 243 * if_usb_disconnect - free resource and cleanup 244 * 245 * @intf USB interface structure 246 */ 247 static void if_usb_disconnect(struct usb_interface *intf) 248 { 249 struct if_usb_card *cardp = usb_get_intfdata(intf); 250 struct lbtf_private *priv = cardp->priv; 251 252 lbtf_deb_enter(LBTF_DEB_MAIN); 253 254 if_usb_reset_device(priv); 255 256 if (priv) 257 lbtf_remove_card(priv); 258 259 /* Unlink and free urb */ 260 if_usb_free(cardp); 261 262 usb_set_intfdata(intf, NULL); 263 usb_put_dev(interface_to_usbdev(intf)); 264 265 lbtf_deb_leave(LBTF_DEB_MAIN); 266 } 267 268 /** 269 * if_usb_send_fw_pkt - This function downloads the FW 270 * 271 * @priv pointer to struct lbtf_private 272 * 273 * Returns: 0 274 */ 275 static int if_usb_send_fw_pkt(struct if_usb_card *cardp) 276 { 277 struct fwdata *fwdata = cardp->ep_out_buf; 278 u8 *firmware = (u8 *) cardp->fw->data; 279 280 lbtf_deb_enter(LBTF_DEB_FW); 281 282 /* If we got a CRC failure on the last block, back 283 up and retry it */ 284 if (!cardp->CRC_OK) { 285 cardp->totalbytes = cardp->fwlastblksent; 286 cardp->fwseqnum--; 287 } 288 289 lbtf_deb_usb2(&cardp->udev->dev, "totalbytes = %d\n", 290 cardp->totalbytes); 291 292 /* struct fwdata (which we sent to the card) has an 293 extra __le32 field in between the header and the data, 294 which is not in the struct fwheader in the actual 295 firmware binary. Insert the seqnum in the middle... */ 296 memcpy(&fwdata->hdr, &firmware[cardp->totalbytes], 297 sizeof(struct fwheader)); 298 299 cardp->fwlastblksent = cardp->totalbytes; 300 cardp->totalbytes += sizeof(struct fwheader); 301 302 memcpy(fwdata->data, &firmware[cardp->totalbytes], 303 le32_to_cpu(fwdata->hdr.datalength)); 304 305 lbtf_deb_usb2(&cardp->udev->dev, "Data length = %d\n", 306 le32_to_cpu(fwdata->hdr.datalength)); 307 308 fwdata->seqnum = cpu_to_le32(++cardp->fwseqnum); 309 cardp->totalbytes += le32_to_cpu(fwdata->hdr.datalength); 310 311 usb_tx_block(cardp, cardp->ep_out_buf, sizeof(struct fwdata) + 312 le32_to_cpu(fwdata->hdr.datalength), 0); 313 314 if (fwdata->hdr.dnldcmd == cpu_to_le32(FW_HAS_DATA_TO_RECV)) { 315 lbtf_deb_usb2(&cardp->udev->dev, "There are data to follow\n"); 316 lbtf_deb_usb2(&cardp->udev->dev, 317 "seqnum = %d totalbytes = %d\n", 318 cardp->fwseqnum, cardp->totalbytes); 319 } else if (fwdata->hdr.dnldcmd == cpu_to_le32(FW_HAS_LAST_BLOCK)) { 320 lbtf_deb_usb2(&cardp->udev->dev, 321 "Host has finished FW downloading\n"); 322 lbtf_deb_usb2(&cardp->udev->dev, "Donwloading FW JUMP BLOCK\n"); 323 324 /* Host has finished FW downloading 325 * Donwloading FW JUMP BLOCK 326 */ 327 cardp->fwfinalblk = 1; 328 } 329 330 lbtf_deb_usb2(&cardp->udev->dev, "Firmware download done; size %d\n", 331 cardp->totalbytes); 332 333 lbtf_deb_leave(LBTF_DEB_FW); 334 return 0; 335 } 336 337 static int if_usb_reset_device(struct lbtf_private *priv) 338 { 339 struct if_usb_card *cardp = priv->card; 340 struct cmd_ds_802_11_reset *cmd = cardp->ep_out_buf + 4; 341 int ret; 342 343 lbtf_deb_enter(LBTF_DEB_USB); 344 345 *(__le32 *)cardp->ep_out_buf = cpu_to_le32(CMD_TYPE_REQUEST); 346 347 cmd->hdr.command = cpu_to_le16(CMD_802_11_RESET); 348 cmd->hdr.size = cpu_to_le16(sizeof(struct cmd_ds_802_11_reset)); 349 cmd->hdr.result = cpu_to_le16(0); 350 cmd->hdr.seqnum = cpu_to_le16(0x5a5a); 351 cmd->action = cpu_to_le16(CMD_ACT_HALT); 352 usb_tx_block(cardp, cardp->ep_out_buf, 353 4 + sizeof(struct cmd_ds_802_11_reset), 0); 354 355 msleep(100); 356 ret = usb_reset_device(cardp->udev); 357 msleep(100); 358 359 lbtf_deb_leave_args(LBTF_DEB_USB, "ret %d", ret); 360 361 return ret; 362 } 363 364 /** 365 * usb_tx_block - transfer data to the device 366 * 367 * @priv pointer to struct lbtf_private 368 * @payload pointer to payload data 369 * @nb data length 370 * @data non-zero for data, zero for commands 371 * 372 * Returns: 0 on success, nonzero otherwise. 373 */ 374 static int usb_tx_block(struct if_usb_card *cardp, uint8_t *payload, 375 uint16_t nb, u8 data) 376 { 377 int ret = -1; 378 struct urb *urb; 379 380 lbtf_deb_enter(LBTF_DEB_USB); 381 /* check if device is removed */ 382 if (cardp->priv->surpriseremoved) { 383 lbtf_deb_usbd(&cardp->udev->dev, "Device removed\n"); 384 goto tx_ret; 385 } 386 387 if (data) 388 urb = cardp->tx_urb; 389 else 390 urb = cardp->cmd_urb; 391 392 usb_fill_bulk_urb(urb, cardp->udev, 393 usb_sndbulkpipe(cardp->udev, 394 cardp->ep_out), 395 payload, nb, if_usb_write_bulk_callback, cardp); 396 397 urb->transfer_flags |= URB_ZERO_PACKET; 398 399 if (usb_submit_urb(urb, GFP_ATOMIC)) { 400 lbtf_deb_usbd(&cardp->udev->dev, 401 "usb_submit_urb failed: %d\n", ret); 402 goto tx_ret; 403 } 404 405 lbtf_deb_usb2(&cardp->udev->dev, "usb_submit_urb success\n"); 406 407 ret = 0; 408 409 tx_ret: 410 lbtf_deb_leave(LBTF_DEB_USB); 411 return ret; 412 } 413 414 static int __if_usb_submit_rx_urb(struct if_usb_card *cardp, 415 void (*callbackfn)(struct urb *urb)) 416 { 417 struct sk_buff *skb; 418 int ret = -1; 419 420 lbtf_deb_enter(LBTF_DEB_USB); 421 422 skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE); 423 if (!skb) { 424 pr_err("No free skb\n"); 425 lbtf_deb_leave(LBTF_DEB_USB); 426 return -1; 427 } 428 429 cardp->rx_skb = skb; 430 431 /* Fill the receive configuration URB and initialise the Rx call back */ 432 usb_fill_bulk_urb(cardp->rx_urb, cardp->udev, 433 usb_rcvbulkpipe(cardp->udev, cardp->ep_in), 434 skb_tail_pointer(skb), 435 MRVDRV_ETH_RX_PACKET_BUFFER_SIZE, callbackfn, cardp); 436 437 lbtf_deb_usb2(&cardp->udev->dev, "Pointer for rx_urb %p\n", 438 cardp->rx_urb); 439 ret = usb_submit_urb(cardp->rx_urb, GFP_ATOMIC); 440 if (ret) { 441 lbtf_deb_usbd(&cardp->udev->dev, 442 "Submit Rx URB failed: %d\n", ret); 443 kfree_skb(skb); 444 cardp->rx_skb = NULL; 445 lbtf_deb_leave(LBTF_DEB_USB); 446 return -1; 447 } else { 448 lbtf_deb_usb2(&cardp->udev->dev, "Submit Rx URB success\n"); 449 lbtf_deb_leave(LBTF_DEB_USB); 450 return 0; 451 } 452 } 453 454 static int if_usb_submit_rx_urb_fwload(struct if_usb_card *cardp) 455 { 456 return __if_usb_submit_rx_urb(cardp, &if_usb_receive_fwload); 457 } 458 459 static int if_usb_submit_rx_urb(struct if_usb_card *cardp) 460 { 461 return __if_usb_submit_rx_urb(cardp, &if_usb_receive); 462 } 463 464 static void if_usb_receive_fwload(struct urb *urb) 465 { 466 struct if_usb_card *cardp = urb->context; 467 struct sk_buff *skb = cardp->rx_skb; 468 struct fwsyncheader *syncfwheader; 469 struct bootcmdresp bcmdresp; 470 471 lbtf_deb_enter(LBTF_DEB_USB); 472 if (urb->status) { 473 lbtf_deb_usbd(&cardp->udev->dev, 474 "URB status is failed during fw load\n"); 475 kfree_skb(skb); 476 lbtf_deb_leave(LBTF_DEB_USB); 477 return; 478 } 479 480 if (cardp->fwdnldover) { 481 __le32 *tmp = (__le32 *)(skb->data); 482 483 if (tmp[0] == cpu_to_le32(CMD_TYPE_INDICATION) && 484 tmp[1] == cpu_to_le32(MACREG_INT_CODE_FIRMWARE_READY)) { 485 /* Firmware ready event received */ 486 pr_info("Firmware ready event received\n"); 487 wake_up(&cardp->fw_wq); 488 } else { 489 lbtf_deb_usb("Waiting for confirmation; got %x %x\n", 490 le32_to_cpu(tmp[0]), le32_to_cpu(tmp[1])); 491 if_usb_submit_rx_urb_fwload(cardp); 492 } 493 kfree_skb(skb); 494 lbtf_deb_leave(LBTF_DEB_USB); 495 return; 496 } 497 if (cardp->bootcmdresp <= 0) { 498 memcpy(&bcmdresp, skb->data, sizeof(bcmdresp)); 499 500 if (le16_to_cpu(cardp->udev->descriptor.bcdDevice) < 0x3106) { 501 kfree_skb(skb); 502 if_usb_submit_rx_urb_fwload(cardp); 503 cardp->bootcmdresp = 1; 504 /* Received valid boot command response */ 505 lbtf_deb_usbd(&cardp->udev->dev, 506 "Received valid boot command response\n"); 507 lbtf_deb_leave(LBTF_DEB_USB); 508 return; 509 } 510 if (bcmdresp.magic != cpu_to_le32(BOOT_CMD_MAGIC_NUMBER)) { 511 if (bcmdresp.magic == cpu_to_le32(CMD_TYPE_REQUEST) || 512 bcmdresp.magic == cpu_to_le32(CMD_TYPE_DATA) || 513 bcmdresp.magic == cpu_to_le32(CMD_TYPE_INDICATION)) { 514 if (!cardp->bootcmdresp) 515 pr_info("Firmware already seems alive; resetting\n"); 516 cardp->bootcmdresp = -1; 517 } else { 518 pr_info("boot cmd response wrong magic number (0x%x)\n", 519 le32_to_cpu(bcmdresp.magic)); 520 } 521 } else if (bcmdresp.cmd != BOOT_CMD_FW_BY_USB) { 522 pr_info("boot cmd response cmd_tag error (%d)\n", 523 bcmdresp.cmd); 524 } else if (bcmdresp.result != BOOT_CMD_RESP_OK) { 525 pr_info("boot cmd response result error (%d)\n", 526 bcmdresp.result); 527 } else { 528 cardp->bootcmdresp = 1; 529 lbtf_deb_usbd(&cardp->udev->dev, 530 "Received valid boot command response\n"); 531 } 532 533 kfree_skb(skb); 534 if_usb_submit_rx_urb_fwload(cardp); 535 lbtf_deb_leave(LBTF_DEB_USB); 536 return; 537 } 538 539 syncfwheader = kmemdup(skb->data, sizeof(struct fwsyncheader), 540 GFP_ATOMIC); 541 if (!syncfwheader) { 542 lbtf_deb_usbd(&cardp->udev->dev, 543 "Failure to allocate syncfwheader\n"); 544 kfree_skb(skb); 545 lbtf_deb_leave(LBTF_DEB_USB); 546 return; 547 } 548 549 if (!syncfwheader->cmd) { 550 lbtf_deb_usb2(&cardp->udev->dev, 551 "FW received Blk with correct CRC\n"); 552 lbtf_deb_usb2(&cardp->udev->dev, 553 "FW received Blk seqnum = %d\n", 554 le32_to_cpu(syncfwheader->seqnum)); 555 cardp->CRC_OK = 1; 556 } else { 557 lbtf_deb_usbd(&cardp->udev->dev, 558 "FW received Blk with CRC error\n"); 559 cardp->CRC_OK = 0; 560 } 561 562 kfree_skb(skb); 563 564 /* reschedule timer for 200ms hence */ 565 mod_timer(&cardp->fw_timeout, jiffies + (HZ/5)); 566 567 if (cardp->fwfinalblk) { 568 cardp->fwdnldover = 1; 569 goto exit; 570 } 571 572 if_usb_send_fw_pkt(cardp); 573 574 exit: 575 if_usb_submit_rx_urb_fwload(cardp); 576 577 kfree(syncfwheader); 578 579 lbtf_deb_leave(LBTF_DEB_USB); 580 } 581 582 #define MRVDRV_MIN_PKT_LEN 30 583 584 static inline void process_cmdtypedata(int recvlength, struct sk_buff *skb, 585 struct if_usb_card *cardp, 586 struct lbtf_private *priv) 587 { 588 if (recvlength > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + MESSAGE_HEADER_LEN 589 || recvlength < MRVDRV_MIN_PKT_LEN) { 590 lbtf_deb_usbd(&cardp->udev->dev, "Packet length is Invalid\n"); 591 kfree_skb(skb); 592 return; 593 } 594 595 skb_put(skb, recvlength); 596 skb_pull(skb, MESSAGE_HEADER_LEN); 597 lbtf_rx(priv, skb); 598 } 599 600 static inline void process_cmdrequest(int recvlength, uint8_t *recvbuff, 601 struct sk_buff *skb, 602 struct if_usb_card *cardp, 603 struct lbtf_private *priv) 604 { 605 unsigned long flags; 606 607 if (recvlength < MESSAGE_HEADER_LEN || 608 recvlength > LBS_CMD_BUFFER_SIZE) { 609 lbtf_deb_usbd(&cardp->udev->dev, 610 "The receive buffer is invalid: %d\n", recvlength); 611 kfree_skb(skb); 612 return; 613 } 614 615 spin_lock_irqsave(&priv->driver_lock, flags); 616 memcpy(priv->cmd_resp_buff, recvbuff + MESSAGE_HEADER_LEN, 617 recvlength - MESSAGE_HEADER_LEN); 618 kfree_skb(skb); 619 lbtf_cmd_response_rx(priv); 620 spin_unlock_irqrestore(&priv->driver_lock, flags); 621 } 622 623 /** 624 * if_usb_receive - read data received from the device. 625 * 626 * @urb pointer to struct urb 627 */ 628 static void if_usb_receive(struct urb *urb) 629 { 630 struct if_usb_card *cardp = urb->context; 631 struct sk_buff *skb = cardp->rx_skb; 632 struct lbtf_private *priv = cardp->priv; 633 int recvlength = urb->actual_length; 634 uint8_t *recvbuff = NULL; 635 uint32_t recvtype = 0; 636 __le32 *pkt = (__le32 *) skb->data; 637 638 lbtf_deb_enter(LBTF_DEB_USB); 639 640 if (recvlength) { 641 if (urb->status) { 642 lbtf_deb_usbd(&cardp->udev->dev, "RX URB failed: %d\n", 643 urb->status); 644 kfree_skb(skb); 645 goto setup_for_next; 646 } 647 648 recvbuff = skb->data; 649 recvtype = le32_to_cpu(pkt[0]); 650 lbtf_deb_usbd(&cardp->udev->dev, 651 "Recv length = 0x%x, Recv type = 0x%X\n", 652 recvlength, recvtype); 653 } else if (urb->status) { 654 kfree_skb(skb); 655 lbtf_deb_leave(LBTF_DEB_USB); 656 return; 657 } 658 659 switch (recvtype) { 660 case CMD_TYPE_DATA: 661 process_cmdtypedata(recvlength, skb, cardp, priv); 662 break; 663 664 case CMD_TYPE_REQUEST: 665 process_cmdrequest(recvlength, recvbuff, skb, cardp, priv); 666 break; 667 668 case CMD_TYPE_INDICATION: 669 { 670 /* Event cause handling */ 671 u32 event_cause = le32_to_cpu(pkt[1]); 672 lbtf_deb_usbd(&cardp->udev->dev, "**EVENT** 0x%X\n", 673 event_cause); 674 675 /* Icky undocumented magic special case */ 676 if (event_cause & 0xffff0000) { 677 u16 tmp; 678 u8 retrycnt; 679 u8 failure; 680 681 tmp = event_cause >> 16; 682 retrycnt = tmp & 0x00ff; 683 failure = (tmp & 0xff00) >> 8; 684 lbtf_send_tx_feedback(priv, retrycnt, failure); 685 } else if (event_cause == LBTF_EVENT_BCN_SENT) 686 lbtf_bcn_sent(priv); 687 else 688 lbtf_deb_usbd(&cardp->udev->dev, 689 "Unsupported notification %d received\n", 690 event_cause); 691 kfree_skb(skb); 692 break; 693 } 694 default: 695 lbtf_deb_usbd(&cardp->udev->dev, 696 "libertastf: unknown command type 0x%X\n", recvtype); 697 kfree_skb(skb); 698 break; 699 } 700 701 setup_for_next: 702 if_usb_submit_rx_urb(cardp); 703 lbtf_deb_leave(LBTF_DEB_USB); 704 } 705 706 /** 707 * if_usb_host_to_card - Download data to the device 708 * 709 * @priv pointer to struct lbtf_private structure 710 * @type type of data 711 * @buf pointer to data buffer 712 * @len number of bytes 713 * 714 * Returns: 0 on success, nonzero otherwise 715 */ 716 static int if_usb_host_to_card(struct lbtf_private *priv, uint8_t type, 717 uint8_t *payload, uint16_t nb) 718 { 719 struct if_usb_card *cardp = priv->card; 720 u8 data = 0; 721 722 lbtf_deb_usbd(&cardp->udev->dev, "*** type = %u\n", type); 723 lbtf_deb_usbd(&cardp->udev->dev, "size after = %d\n", nb); 724 725 if (type == MVMS_CMD) { 726 *(__le32 *)cardp->ep_out_buf = cpu_to_le32(CMD_TYPE_REQUEST); 727 } else { 728 *(__le32 *)cardp->ep_out_buf = cpu_to_le32(CMD_TYPE_DATA); 729 data = 1; 730 } 731 732 memcpy((cardp->ep_out_buf + MESSAGE_HEADER_LEN), payload, nb); 733 734 return usb_tx_block(cardp, cardp->ep_out_buf, nb + MESSAGE_HEADER_LEN, 735 data); 736 } 737 738 /** 739 * if_usb_issue_boot_command - Issue boot command to Boot2. 740 * 741 * @ivalue 1 boots from FW by USB-Download, 2 boots from FW in EEPROM. 742 * 743 * Returns: 0 744 */ 745 static int if_usb_issue_boot_command(struct if_usb_card *cardp, int ivalue) 746 { 747 struct bootcmd *bootcmd = cardp->ep_out_buf; 748 749 /* Prepare command */ 750 bootcmd->magic = cpu_to_le32(BOOT_CMD_MAGIC_NUMBER); 751 bootcmd->cmd = ivalue; 752 memset(bootcmd->pad, 0, sizeof(bootcmd->pad)); 753 754 /* Issue command */ 755 usb_tx_block(cardp, cardp->ep_out_buf, sizeof(*bootcmd), 0); 756 757 return 0; 758 } 759 760 761 /** 762 * check_fwfile_format - Check the validity of Boot2/FW image. 763 * 764 * @data pointer to image 765 * @totlen image length 766 * 767 * Returns: 0 if the image is valid, nonzero otherwise. 768 */ 769 static int check_fwfile_format(const u8 *data, u32 totlen) 770 { 771 u32 bincmd, exit; 772 u32 blksize, offset, len; 773 int ret; 774 775 ret = 1; 776 exit = len = 0; 777 778 do { 779 struct fwheader *fwh = (void *) data; 780 781 bincmd = le32_to_cpu(fwh->dnldcmd); 782 blksize = le32_to_cpu(fwh->datalength); 783 switch (bincmd) { 784 case FW_HAS_DATA_TO_RECV: 785 offset = sizeof(struct fwheader) + blksize; 786 data += offset; 787 len += offset; 788 if (len >= totlen) 789 exit = 1; 790 break; 791 case FW_HAS_LAST_BLOCK: 792 exit = 1; 793 ret = 0; 794 break; 795 default: 796 exit = 1; 797 break; 798 } 799 } while (!exit); 800 801 if (ret) 802 pr_err("firmware file format check FAIL\n"); 803 else 804 lbtf_deb_fw("firmware file format check PASS\n"); 805 806 return ret; 807 } 808 809 810 static int if_usb_prog_firmware(struct lbtf_private *priv) 811 { 812 struct if_usb_card *cardp = priv->card; 813 int i = 0; 814 static int reset_count = 10; 815 int ret = 0; 816 817 lbtf_deb_enter(LBTF_DEB_USB); 818 819 cardp->priv = priv; 820 821 kernel_param_lock(THIS_MODULE); 822 ret = request_firmware(&cardp->fw, lbtf_fw_name, &cardp->udev->dev); 823 if (ret < 0) { 824 pr_err("request_firmware() failed with %#x\n", ret); 825 pr_err("firmware %s not found\n", lbtf_fw_name); 826 kernel_param_unlock(THIS_MODULE); 827 goto done; 828 } 829 kernel_param_unlock(THIS_MODULE); 830 831 if (check_fwfile_format(cardp->fw->data, cardp->fw->size)) 832 goto release_fw; 833 834 restart: 835 if (if_usb_submit_rx_urb_fwload(cardp) < 0) { 836 lbtf_deb_usbd(&cardp->udev->dev, "URB submission is failed\n"); 837 ret = -1; 838 goto release_fw; 839 } 840 841 cardp->bootcmdresp = 0; 842 do { 843 int j = 0; 844 i++; 845 /* Issue Boot command = 1, Boot from Download-FW */ 846 if_usb_issue_boot_command(cardp, BOOT_CMD_FW_BY_USB); 847 /* wait for command response */ 848 do { 849 j++; 850 msleep_interruptible(100); 851 } while (cardp->bootcmdresp == 0 && j < 10); 852 } while (cardp->bootcmdresp == 0 && i < 5); 853 854 if (cardp->bootcmdresp <= 0) { 855 if (--reset_count >= 0) { 856 if_usb_reset_device(priv); 857 goto restart; 858 } 859 return -1; 860 } 861 862 i = 0; 863 864 cardp->totalbytes = 0; 865 cardp->fwlastblksent = 0; 866 cardp->CRC_OK = 1; 867 cardp->fwdnldover = 0; 868 cardp->fwseqnum = -1; 869 cardp->totalbytes = 0; 870 cardp->fwfinalblk = 0; 871 872 /* Send the first firmware packet... */ 873 if_usb_send_fw_pkt(cardp); 874 875 /* ... and wait for the process to complete */ 876 wait_event_interruptible(cardp->fw_wq, cardp->priv->surpriseremoved || 877 cardp->fwdnldover); 878 879 del_timer_sync(&cardp->fw_timeout); 880 usb_kill_urb(cardp->rx_urb); 881 882 if (!cardp->fwdnldover) { 883 pr_info("failed to load fw, resetting device!\n"); 884 if (--reset_count >= 0) { 885 if_usb_reset_device(priv); 886 goto restart; 887 } 888 889 pr_info("FW download failure, time = %d ms\n", i * 100); 890 ret = -1; 891 goto release_fw; 892 } 893 894 release_fw: 895 release_firmware(cardp->fw); 896 cardp->fw = NULL; 897 898 if_usb_setup_firmware(cardp->priv); 899 900 done: 901 lbtf_deb_leave_args(LBTF_DEB_USB, "ret %d", ret); 902 return ret; 903 } 904 905 906 #define if_usb_suspend NULL 907 #define if_usb_resume NULL 908 909 static struct usb_driver if_usb_driver = { 910 .name = DRV_NAME, 911 .probe = if_usb_probe, 912 .disconnect = if_usb_disconnect, 913 .id_table = if_usb_table, 914 .suspend = if_usb_suspend, 915 .resume = if_usb_resume, 916 .disable_hub_initiated_lpm = 1, 917 }; 918 919 module_usb_driver(if_usb_driver); 920 921 MODULE_DESCRIPTION("8388 USB WLAN Thinfirm Driver"); 922 MODULE_AUTHOR("Cozybit Inc."); 923 MODULE_LICENSE("GPL"); 924