1 /* 2 * Marvell Wireless LAN device driver: USB specific handling 3 * 4 * Copyright (C) 2012-2014, Marvell International Ltd. 5 * 6 * This software file (the "File") is distributed by Marvell International 7 * Ltd. under the terms of the GNU General Public License Version 2, June 1991 8 * (the "License"). You may use, redistribute and/or modify this File in 9 * accordance with the terms and conditions of the License, a copy of which 10 * is available by writing to the Free Software Foundation, Inc., 11 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the 12 * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. 13 * 14 * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE 16 * ARE EXPRESSLY DISCLAIMED. The License provides additional details about 17 * this warranty disclaimer. 18 */ 19 20 #include "main.h" 21 #include "usb.h" 22 23 #define USB_VERSION "1.0" 24 25 static struct mwifiex_if_ops usb_ops; 26 27 static struct usb_device_id mwifiex_usb_table[] = { 28 /* 8766 */ 29 {USB_DEVICE(USB8XXX_VID, USB8766_PID_1)}, 30 {USB_DEVICE_AND_INTERFACE_INFO(USB8XXX_VID, USB8766_PID_2, 31 USB_CLASS_VENDOR_SPEC, 32 USB_SUBCLASS_VENDOR_SPEC, 0xff)}, 33 /* 8797 */ 34 {USB_DEVICE(USB8XXX_VID, USB8797_PID_1)}, 35 {USB_DEVICE_AND_INTERFACE_INFO(USB8XXX_VID, USB8797_PID_2, 36 USB_CLASS_VENDOR_SPEC, 37 USB_SUBCLASS_VENDOR_SPEC, 0xff)}, 38 /* 8801 */ 39 {USB_DEVICE(USB8XXX_VID, USB8801_PID_1)}, 40 {USB_DEVICE_AND_INTERFACE_INFO(USB8XXX_VID, USB8801_PID_2, 41 USB_CLASS_VENDOR_SPEC, 42 USB_SUBCLASS_VENDOR_SPEC, 0xff)}, 43 /* 8997 */ 44 {USB_DEVICE(USB8XXX_VID, USB8997_PID_1)}, 45 {USB_DEVICE_AND_INTERFACE_INFO(USB8XXX_VID, USB8997_PID_2, 46 USB_CLASS_VENDOR_SPEC, 47 USB_SUBCLASS_VENDOR_SPEC, 0xff)}, 48 { } /* Terminating entry */ 49 }; 50 51 MODULE_DEVICE_TABLE(usb, mwifiex_usb_table); 52 53 static int mwifiex_usb_submit_rx_urb(struct urb_context *ctx, int size); 54 55 /* This function handles received packet. Necessary action is taken based on 56 * cmd/event/data. 57 */ 58 static int mwifiex_usb_recv(struct mwifiex_adapter *adapter, 59 struct sk_buff *skb, u8 ep) 60 { 61 u32 recv_type; 62 __le32 tmp; 63 int ret; 64 65 if (adapter->hs_activated) 66 mwifiex_process_hs_config(adapter); 67 68 if (skb->len < INTF_HEADER_LEN) { 69 mwifiex_dbg(adapter, ERROR, 70 "%s: invalid skb->len\n", __func__); 71 return -1; 72 } 73 74 switch (ep) { 75 case MWIFIEX_USB_EP_CMD_EVENT: 76 mwifiex_dbg(adapter, EVENT, 77 "%s: EP_CMD_EVENT\n", __func__); 78 skb_copy_from_linear_data(skb, &tmp, INTF_HEADER_LEN); 79 recv_type = le32_to_cpu(tmp); 80 skb_pull(skb, INTF_HEADER_LEN); 81 82 switch (recv_type) { 83 case MWIFIEX_USB_TYPE_CMD: 84 if (skb->len > MWIFIEX_SIZE_OF_CMD_BUFFER) { 85 mwifiex_dbg(adapter, ERROR, 86 "CMD: skb->len too large\n"); 87 ret = -1; 88 goto exit_restore_skb; 89 } else if (!adapter->curr_cmd) { 90 mwifiex_dbg(adapter, WARN, "CMD: no curr_cmd\n"); 91 if (adapter->ps_state == PS_STATE_SLEEP_CFM) { 92 mwifiex_process_sleep_confirm_resp( 93 adapter, skb->data, 94 skb->len); 95 ret = 0; 96 goto exit_restore_skb; 97 } 98 ret = -1; 99 goto exit_restore_skb; 100 } 101 102 adapter->curr_cmd->resp_skb = skb; 103 adapter->cmd_resp_received = true; 104 break; 105 case MWIFIEX_USB_TYPE_EVENT: 106 if (skb->len < sizeof(u32)) { 107 mwifiex_dbg(adapter, ERROR, 108 "EVENT: skb->len too small\n"); 109 ret = -1; 110 goto exit_restore_skb; 111 } 112 skb_copy_from_linear_data(skb, &tmp, sizeof(u32)); 113 adapter->event_cause = le32_to_cpu(tmp); 114 mwifiex_dbg(adapter, EVENT, 115 "event_cause %#x\n", adapter->event_cause); 116 117 if (skb->len > MAX_EVENT_SIZE) { 118 mwifiex_dbg(adapter, ERROR, 119 "EVENT: event body too large\n"); 120 ret = -1; 121 goto exit_restore_skb; 122 } 123 124 memcpy(adapter->event_body, skb->data + 125 MWIFIEX_EVENT_HEADER_LEN, skb->len); 126 127 adapter->event_received = true; 128 adapter->event_skb = skb; 129 break; 130 default: 131 mwifiex_dbg(adapter, ERROR, 132 "unknown recv_type %#x\n", recv_type); 133 return -1; 134 } 135 break; 136 case MWIFIEX_USB_EP_DATA: 137 mwifiex_dbg(adapter, DATA, "%s: EP_DATA\n", __func__); 138 if (skb->len > MWIFIEX_RX_DATA_BUF_SIZE) { 139 mwifiex_dbg(adapter, ERROR, 140 "DATA: skb->len too large\n"); 141 return -1; 142 } 143 144 skb_queue_tail(&adapter->rx_data_q, skb); 145 adapter->data_received = true; 146 atomic_inc(&adapter->rx_pending); 147 break; 148 default: 149 mwifiex_dbg(adapter, ERROR, 150 "%s: unknown endport %#x\n", __func__, ep); 151 return -1; 152 } 153 154 return -EINPROGRESS; 155 156 exit_restore_skb: 157 /* The buffer will be reused for further cmds/events */ 158 skb_push(skb, INTF_HEADER_LEN); 159 160 return ret; 161 } 162 163 static void mwifiex_usb_rx_complete(struct urb *urb) 164 { 165 struct urb_context *context = (struct urb_context *)urb->context; 166 struct mwifiex_adapter *adapter = context->adapter; 167 struct sk_buff *skb = context->skb; 168 struct usb_card_rec *card; 169 int recv_length = urb->actual_length; 170 int size, status; 171 172 if (!adapter || !adapter->card) { 173 pr_err("mwifiex adapter or card structure is not valid\n"); 174 return; 175 } 176 177 card = (struct usb_card_rec *)adapter->card; 178 if (card->rx_cmd_ep == context->ep) 179 atomic_dec(&card->rx_cmd_urb_pending); 180 else 181 atomic_dec(&card->rx_data_urb_pending); 182 183 if (recv_length) { 184 if (urb->status || (adapter->surprise_removed)) { 185 mwifiex_dbg(adapter, ERROR, 186 "URB status is failed: %d\n", urb->status); 187 /* Do not free skb in case of command ep */ 188 if (card->rx_cmd_ep != context->ep) 189 dev_kfree_skb_any(skb); 190 goto setup_for_next; 191 } 192 if (skb->len > recv_length) 193 skb_trim(skb, recv_length); 194 else 195 skb_put(skb, recv_length - skb->len); 196 197 status = mwifiex_usb_recv(adapter, skb, context->ep); 198 199 mwifiex_dbg(adapter, INFO, 200 "info: recv_length=%d, status=%d\n", 201 recv_length, status); 202 if (status == -EINPROGRESS) { 203 mwifiex_queue_main_work(adapter); 204 205 /* urb for data_ep is re-submitted now; 206 * urb for cmd_ep will be re-submitted in callback 207 * mwifiex_usb_recv_complete 208 */ 209 if (card->rx_cmd_ep == context->ep) 210 return; 211 } else { 212 if (status == -1) 213 mwifiex_dbg(adapter, ERROR, 214 "received data processing failed!\n"); 215 216 /* Do not free skb in case of command ep */ 217 if (card->rx_cmd_ep != context->ep) 218 dev_kfree_skb_any(skb); 219 } 220 } else if (urb->status) { 221 if (!adapter->is_suspended) { 222 mwifiex_dbg(adapter, FATAL, 223 "Card is removed: %d\n", urb->status); 224 adapter->surprise_removed = true; 225 } 226 dev_kfree_skb_any(skb); 227 return; 228 } else { 229 /* Do not free skb in case of command ep */ 230 if (card->rx_cmd_ep != context->ep) 231 dev_kfree_skb_any(skb); 232 233 /* fall through setup_for_next */ 234 } 235 236 setup_for_next: 237 if (card->rx_cmd_ep == context->ep) 238 size = MWIFIEX_RX_CMD_BUF_SIZE; 239 else 240 size = MWIFIEX_RX_DATA_BUF_SIZE; 241 242 if (card->rx_cmd_ep == context->ep) { 243 mwifiex_usb_submit_rx_urb(context, size); 244 } else { 245 if (atomic_read(&adapter->rx_pending) <= HIGH_RX_PENDING) { 246 mwifiex_usb_submit_rx_urb(context, size); 247 } else { 248 context->skb = NULL; 249 } 250 } 251 252 return; 253 } 254 255 static void mwifiex_usb_tx_complete(struct urb *urb) 256 { 257 struct urb_context *context = (struct urb_context *)(urb->context); 258 struct mwifiex_adapter *adapter = context->adapter; 259 struct usb_card_rec *card = adapter->card; 260 struct usb_tx_data_port *port; 261 int i; 262 263 mwifiex_dbg(adapter, INFO, 264 "%s: status: %d\n", __func__, urb->status); 265 266 if (context->ep == card->tx_cmd_ep) { 267 mwifiex_dbg(adapter, CMD, 268 "%s: CMD\n", __func__); 269 atomic_dec(&card->tx_cmd_urb_pending); 270 adapter->cmd_sent = false; 271 } else { 272 mwifiex_dbg(adapter, DATA, 273 "%s: DATA\n", __func__); 274 mwifiex_write_data_complete(adapter, context->skb, 0, 275 urb->status ? -1 : 0); 276 for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) { 277 port = &card->port[i]; 278 if (context->ep == port->tx_data_ep) { 279 atomic_dec(&port->tx_data_urb_pending); 280 port->block_status = false; 281 break; 282 } 283 } 284 adapter->data_sent = false; 285 } 286 287 if (card->mc_resync_flag) 288 mwifiex_multi_chan_resync(adapter); 289 290 mwifiex_queue_main_work(adapter); 291 292 return; 293 } 294 295 static int mwifiex_usb_submit_rx_urb(struct urb_context *ctx, int size) 296 { 297 struct mwifiex_adapter *adapter = ctx->adapter; 298 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card; 299 300 if (card->rx_cmd_ep != ctx->ep) { 301 ctx->skb = dev_alloc_skb(size); 302 if (!ctx->skb) { 303 mwifiex_dbg(adapter, ERROR, 304 "%s: dev_alloc_skb failed\n", __func__); 305 return -ENOMEM; 306 } 307 } 308 309 usb_fill_bulk_urb(ctx->urb, card->udev, 310 usb_rcvbulkpipe(card->udev, ctx->ep), ctx->skb->data, 311 size, mwifiex_usb_rx_complete, (void *)ctx); 312 313 if (card->rx_cmd_ep == ctx->ep) 314 atomic_inc(&card->rx_cmd_urb_pending); 315 else 316 atomic_inc(&card->rx_data_urb_pending); 317 318 if (usb_submit_urb(ctx->urb, GFP_ATOMIC)) { 319 mwifiex_dbg(adapter, ERROR, "usb_submit_urb failed\n"); 320 dev_kfree_skb_any(ctx->skb); 321 ctx->skb = NULL; 322 323 if (card->rx_cmd_ep == ctx->ep) 324 atomic_dec(&card->rx_cmd_urb_pending); 325 else 326 atomic_dec(&card->rx_data_urb_pending); 327 328 return -1; 329 } 330 331 return 0; 332 } 333 334 static void mwifiex_usb_free(struct usb_card_rec *card) 335 { 336 struct usb_tx_data_port *port; 337 int i, j; 338 339 if (atomic_read(&card->rx_cmd_urb_pending) && card->rx_cmd.urb) 340 usb_kill_urb(card->rx_cmd.urb); 341 342 usb_free_urb(card->rx_cmd.urb); 343 card->rx_cmd.urb = NULL; 344 345 if (atomic_read(&card->rx_data_urb_pending)) 346 for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) 347 if (card->rx_data_list[i].urb) 348 usb_kill_urb(card->rx_data_list[i].urb); 349 350 for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) { 351 usb_free_urb(card->rx_data_list[i].urb); 352 card->rx_data_list[i].urb = NULL; 353 } 354 355 for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) { 356 port = &card->port[i]; 357 for (j = 0; j < MWIFIEX_TX_DATA_URB; j++) { 358 usb_free_urb(port->tx_data_list[j].urb); 359 port->tx_data_list[j].urb = NULL; 360 } 361 } 362 363 usb_free_urb(card->tx_cmd.urb); 364 card->tx_cmd.urb = NULL; 365 366 return; 367 } 368 369 /* This function probes an mwifiex device and registers it. It allocates 370 * the card structure, initiates the device registration and initialization 371 * procedure by adding a logical interface. 372 */ 373 static int mwifiex_usb_probe(struct usb_interface *intf, 374 const struct usb_device_id *id) 375 { 376 struct usb_device *udev = interface_to_usbdev(intf); 377 struct usb_host_interface *iface_desc = intf->cur_altsetting; 378 struct usb_endpoint_descriptor *epd; 379 int ret, i; 380 struct usb_card_rec *card; 381 u16 id_vendor, id_product, bcd_device; 382 383 card = devm_kzalloc(&intf->dev, sizeof(*card), GFP_KERNEL); 384 if (!card) 385 return -ENOMEM; 386 387 init_completion(&card->fw_done); 388 389 id_vendor = le16_to_cpu(udev->descriptor.idVendor); 390 id_product = le16_to_cpu(udev->descriptor.idProduct); 391 bcd_device = le16_to_cpu(udev->descriptor.bcdDevice); 392 pr_debug("info: VID/PID = %X/%X, Boot2 version = %X\n", 393 id_vendor, id_product, bcd_device); 394 395 /* PID_1 is used for firmware downloading only */ 396 switch (id_product) { 397 case USB8766_PID_1: 398 case USB8797_PID_1: 399 case USB8801_PID_1: 400 case USB8997_PID_1: 401 card->usb_boot_state = USB8XXX_FW_DNLD; 402 break; 403 case USB8766_PID_2: 404 case USB8797_PID_2: 405 case USB8801_PID_2: 406 case USB8997_PID_2: 407 card->usb_boot_state = USB8XXX_FW_READY; 408 break; 409 default: 410 pr_warn("unknown id_product %#x\n", id_product); 411 card->usb_boot_state = USB8XXX_FW_DNLD; 412 break; 413 } 414 415 card->udev = udev; 416 card->intf = intf; 417 418 pr_debug("info: bcdUSB=%#x Device Class=%#x SubClass=%#x Protocol=%#x\n", 419 udev->descriptor.bcdUSB, udev->descriptor.bDeviceClass, 420 udev->descriptor.bDeviceSubClass, 421 udev->descriptor.bDeviceProtocol); 422 423 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { 424 epd = &iface_desc->endpoint[i].desc; 425 if (usb_endpoint_dir_in(epd) && 426 usb_endpoint_num(epd) == MWIFIEX_USB_EP_CMD_EVENT && 427 usb_endpoint_xfer_bulk(epd)) { 428 pr_debug("info: bulk IN: max pkt size: %d, addr: %d\n", 429 le16_to_cpu(epd->wMaxPacketSize), 430 epd->bEndpointAddress); 431 card->rx_cmd_ep = usb_endpoint_num(epd); 432 atomic_set(&card->rx_cmd_urb_pending, 0); 433 } 434 if (usb_endpoint_dir_in(epd) && 435 usb_endpoint_num(epd) == MWIFIEX_USB_EP_DATA && 436 usb_endpoint_xfer_bulk(epd)) { 437 pr_debug("info: bulk IN: max pkt size: %d, addr: %d\n", 438 le16_to_cpu(epd->wMaxPacketSize), 439 epd->bEndpointAddress); 440 card->rx_data_ep = usb_endpoint_num(epd); 441 atomic_set(&card->rx_data_urb_pending, 0); 442 } 443 if (usb_endpoint_dir_out(epd) && 444 usb_endpoint_num(epd) == MWIFIEX_USB_EP_DATA && 445 usb_endpoint_xfer_bulk(epd)) { 446 pr_debug("info: bulk OUT: max pkt size: %d, addr: %d\n", 447 le16_to_cpu(epd->wMaxPacketSize), 448 epd->bEndpointAddress); 449 card->port[0].tx_data_ep = usb_endpoint_num(epd); 450 atomic_set(&card->port[0].tx_data_urb_pending, 0); 451 } 452 if (usb_endpoint_dir_out(epd) && 453 usb_endpoint_num(epd) == MWIFIEX_USB_EP_DATA_CH2 && 454 usb_endpoint_xfer_bulk(epd)) { 455 pr_debug("info: bulk OUT chan2:\t" 456 "max pkt size: %d, addr: %d\n", 457 le16_to_cpu(epd->wMaxPacketSize), 458 epd->bEndpointAddress); 459 card->port[1].tx_data_ep = usb_endpoint_num(epd); 460 atomic_set(&card->port[1].tx_data_urb_pending, 0); 461 } 462 if (usb_endpoint_dir_out(epd) && 463 usb_endpoint_num(epd) == MWIFIEX_USB_EP_CMD_EVENT && 464 usb_endpoint_xfer_bulk(epd)) { 465 pr_debug("info: bulk OUT: max pkt size: %d, addr: %d\n", 466 le16_to_cpu(epd->wMaxPacketSize), 467 epd->bEndpointAddress); 468 card->tx_cmd_ep = usb_endpoint_num(epd); 469 atomic_set(&card->tx_cmd_urb_pending, 0); 470 card->bulk_out_maxpktsize = 471 le16_to_cpu(epd->wMaxPacketSize); 472 } 473 } 474 475 usb_set_intfdata(intf, card); 476 477 ret = mwifiex_add_card(card, &card->fw_done, &usb_ops, 478 MWIFIEX_USB, &card->udev->dev); 479 if (ret) { 480 pr_err("%s: mwifiex_add_card failed: %d\n", __func__, ret); 481 usb_reset_device(udev); 482 return ret; 483 } 484 485 usb_get_dev(udev); 486 487 return 0; 488 } 489 490 /* Kernel needs to suspend all functions separately. Therefore all 491 * registered functions must have drivers with suspend and resume 492 * methods. Failing that the kernel simply removes the whole card. 493 * 494 * If already not suspended, this function allocates and sends a 495 * 'host sleep activate' request to the firmware and turns off the traffic. 496 */ 497 static int mwifiex_usb_suspend(struct usb_interface *intf, pm_message_t message) 498 { 499 struct usb_card_rec *card = usb_get_intfdata(intf); 500 struct mwifiex_adapter *adapter; 501 struct usb_tx_data_port *port; 502 int i, j; 503 504 /* Might still be loading firmware */ 505 wait_for_completion(&card->fw_done); 506 507 adapter = card->adapter; 508 if (!adapter) { 509 dev_err(&intf->dev, "card is not valid\n"); 510 return 0; 511 } 512 513 if (unlikely(adapter->is_suspended)) 514 mwifiex_dbg(adapter, WARN, 515 "Device already suspended\n"); 516 517 /* Enable the Host Sleep */ 518 if (!mwifiex_enable_hs(adapter)) { 519 mwifiex_dbg(adapter, ERROR, 520 "cmd: failed to suspend\n"); 521 adapter->hs_enabling = false; 522 return -EFAULT; 523 } 524 525 526 /* 'is_suspended' flag indicates device is suspended. 527 * It must be set here before the usb_kill_urb() calls. Reason 528 * is in the complete handlers, urb->status(= -ENOENT) and 529 * this flag is used in combination to distinguish between a 530 * 'suspended' state and a 'disconnect' one. 531 */ 532 adapter->is_suspended = true; 533 adapter->hs_enabling = false; 534 535 if (atomic_read(&card->rx_cmd_urb_pending) && card->rx_cmd.urb) 536 usb_kill_urb(card->rx_cmd.urb); 537 538 if (atomic_read(&card->rx_data_urb_pending)) 539 for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) 540 if (card->rx_data_list[i].urb) 541 usb_kill_urb(card->rx_data_list[i].urb); 542 543 for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) { 544 port = &card->port[i]; 545 for (j = 0; j < MWIFIEX_TX_DATA_URB; j++) { 546 if (port->tx_data_list[j].urb) 547 usb_kill_urb(port->tx_data_list[j].urb); 548 } 549 } 550 551 if (card->tx_cmd.urb) 552 usb_kill_urb(card->tx_cmd.urb); 553 554 return 0; 555 } 556 557 /* Kernel needs to suspend all functions separately. Therefore all 558 * registered functions must have drivers with suspend and resume 559 * methods. Failing that the kernel simply removes the whole card. 560 * 561 * If already not resumed, this function turns on the traffic and 562 * sends a 'host sleep cancel' request to the firmware. 563 */ 564 static int mwifiex_usb_resume(struct usb_interface *intf) 565 { 566 struct usb_card_rec *card = usb_get_intfdata(intf); 567 struct mwifiex_adapter *adapter; 568 int i; 569 570 if (!card->adapter) { 571 dev_err(&intf->dev, "%s: card->adapter is NULL\n", 572 __func__); 573 return 0; 574 } 575 adapter = card->adapter; 576 577 if (unlikely(!adapter->is_suspended)) { 578 mwifiex_dbg(adapter, WARN, 579 "Device already resumed\n"); 580 return 0; 581 } 582 583 /* Indicate device resumed. The netdev queue will be resumed only 584 * after the urbs have been re-submitted 585 */ 586 adapter->is_suspended = false; 587 588 if (!atomic_read(&card->rx_data_urb_pending)) 589 for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) 590 mwifiex_usb_submit_rx_urb(&card->rx_data_list[i], 591 MWIFIEX_RX_DATA_BUF_SIZE); 592 593 if (!atomic_read(&card->rx_cmd_urb_pending)) { 594 card->rx_cmd.skb = dev_alloc_skb(MWIFIEX_RX_CMD_BUF_SIZE); 595 if (card->rx_cmd.skb) 596 mwifiex_usb_submit_rx_urb(&card->rx_cmd, 597 MWIFIEX_RX_CMD_BUF_SIZE); 598 } 599 600 /* Disable Host Sleep */ 601 if (adapter->hs_activated) 602 mwifiex_cancel_hs(mwifiex_get_priv(adapter, 603 MWIFIEX_BSS_ROLE_ANY), 604 MWIFIEX_ASYNC_CMD); 605 606 return 0; 607 } 608 609 static void mwifiex_usb_disconnect(struct usb_interface *intf) 610 { 611 struct usb_card_rec *card = usb_get_intfdata(intf); 612 struct mwifiex_adapter *adapter; 613 614 wait_for_completion(&card->fw_done); 615 616 adapter = card->adapter; 617 if (!adapter || !adapter->priv_num) 618 return; 619 620 if (card->udev->state != USB_STATE_NOTATTACHED && !adapter->mfg_mode) { 621 mwifiex_deauthenticate_all(adapter); 622 623 mwifiex_init_shutdown_fw(mwifiex_get_priv(adapter, 624 MWIFIEX_BSS_ROLE_ANY), 625 MWIFIEX_FUNC_SHUTDOWN); 626 } 627 628 mwifiex_usb_free(card); 629 630 mwifiex_dbg(adapter, FATAL, 631 "%s: removing card\n", __func__); 632 mwifiex_remove_card(adapter); 633 634 usb_put_dev(interface_to_usbdev(intf)); 635 } 636 637 static struct usb_driver mwifiex_usb_driver = { 638 .name = "mwifiex_usb", 639 .probe = mwifiex_usb_probe, 640 .disconnect = mwifiex_usb_disconnect, 641 .id_table = mwifiex_usb_table, 642 .suspend = mwifiex_usb_suspend, 643 .resume = mwifiex_usb_resume, 644 .soft_unbind = 1, 645 }; 646 647 static int mwifiex_usb_tx_init(struct mwifiex_adapter *adapter) 648 { 649 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card; 650 struct usb_tx_data_port *port; 651 int i, j; 652 653 card->tx_cmd.adapter = adapter; 654 card->tx_cmd.ep = card->tx_cmd_ep; 655 656 card->tx_cmd.urb = usb_alloc_urb(0, GFP_KERNEL); 657 if (!card->tx_cmd.urb) 658 return -ENOMEM; 659 660 for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) { 661 port = &card->port[i]; 662 if (!port->tx_data_ep) 663 continue; 664 port->tx_data_ix = 0; 665 if (port->tx_data_ep == MWIFIEX_USB_EP_DATA) 666 port->block_status = false; 667 else 668 port->block_status = true; 669 for (j = 0; j < MWIFIEX_TX_DATA_URB; j++) { 670 port->tx_data_list[j].adapter = adapter; 671 port->tx_data_list[j].ep = port->tx_data_ep; 672 port->tx_data_list[j].urb = 673 usb_alloc_urb(0, GFP_KERNEL); 674 if (!port->tx_data_list[j].urb) 675 return -ENOMEM; 676 } 677 } 678 679 return 0; 680 } 681 682 static int mwifiex_usb_rx_init(struct mwifiex_adapter *adapter) 683 { 684 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card; 685 int i; 686 687 card->rx_cmd.adapter = adapter; 688 card->rx_cmd.ep = card->rx_cmd_ep; 689 690 card->rx_cmd.urb = usb_alloc_urb(0, GFP_KERNEL); 691 if (!card->rx_cmd.urb) 692 return -ENOMEM; 693 694 card->rx_cmd.skb = dev_alloc_skb(MWIFIEX_RX_CMD_BUF_SIZE); 695 if (!card->rx_cmd.skb) 696 return -ENOMEM; 697 698 if (mwifiex_usb_submit_rx_urb(&card->rx_cmd, MWIFIEX_RX_CMD_BUF_SIZE)) 699 return -1; 700 701 for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) { 702 card->rx_data_list[i].adapter = adapter; 703 card->rx_data_list[i].ep = card->rx_data_ep; 704 705 card->rx_data_list[i].urb = usb_alloc_urb(0, GFP_KERNEL); 706 if (!card->rx_data_list[i].urb) 707 return -1; 708 if (mwifiex_usb_submit_rx_urb(&card->rx_data_list[i], 709 MWIFIEX_RX_DATA_BUF_SIZE)) 710 return -1; 711 } 712 713 return 0; 714 } 715 716 static int mwifiex_write_data_sync(struct mwifiex_adapter *adapter, u8 *pbuf, 717 u32 *len, u8 ep, u32 timeout) 718 { 719 struct usb_card_rec *card = adapter->card; 720 int actual_length, ret; 721 722 if (!(*len % card->bulk_out_maxpktsize)) 723 (*len)++; 724 725 /* Send the data block */ 726 ret = usb_bulk_msg(card->udev, usb_sndbulkpipe(card->udev, ep), pbuf, 727 *len, &actual_length, timeout); 728 if (ret) { 729 mwifiex_dbg(adapter, ERROR, 730 "usb_bulk_msg for tx failed: %d\n", ret); 731 return ret; 732 } 733 734 *len = actual_length; 735 736 return ret; 737 } 738 739 static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *pbuf, 740 u32 *len, u8 ep, u32 timeout) 741 { 742 struct usb_card_rec *card = adapter->card; 743 int actual_length, ret; 744 745 /* Receive the data response */ 746 ret = usb_bulk_msg(card->udev, usb_rcvbulkpipe(card->udev, ep), pbuf, 747 *len, &actual_length, timeout); 748 if (ret) { 749 mwifiex_dbg(adapter, ERROR, 750 "usb_bulk_msg for rx failed: %d\n", ret); 751 return ret; 752 } 753 754 *len = actual_length; 755 756 return ret; 757 } 758 759 static void mwifiex_usb_port_resync(struct mwifiex_adapter *adapter) 760 { 761 struct usb_card_rec *card = adapter->card; 762 u8 active_port = MWIFIEX_USB_EP_DATA; 763 struct mwifiex_private *priv = NULL; 764 int i; 765 766 if (adapter->usb_mc_status) { 767 for (i = 0; i < adapter->priv_num; i++) { 768 priv = adapter->priv[i]; 769 if (!priv) 770 continue; 771 if ((priv->bss_role == MWIFIEX_BSS_ROLE_UAP && 772 !priv->bss_started) || 773 (priv->bss_role == MWIFIEX_BSS_ROLE_STA && 774 !priv->media_connected)) 775 priv->usb_port = MWIFIEX_USB_EP_DATA; 776 } 777 for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) 778 card->port[i].block_status = false; 779 } else { 780 for (i = 0; i < adapter->priv_num; i++) { 781 priv = adapter->priv[i]; 782 if (!priv) 783 continue; 784 if ((priv->bss_role == MWIFIEX_BSS_ROLE_UAP && 785 priv->bss_started) || 786 (priv->bss_role == MWIFIEX_BSS_ROLE_STA && 787 priv->media_connected)) { 788 active_port = priv->usb_port; 789 break; 790 } 791 } 792 for (i = 0; i < adapter->priv_num; i++) { 793 priv = adapter->priv[i]; 794 if (priv) 795 priv->usb_port = active_port; 796 } 797 for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) { 798 if (active_port == card->port[i].tx_data_ep) 799 card->port[i].block_status = false; 800 else 801 card->port[i].block_status = true; 802 } 803 } 804 } 805 806 static bool mwifiex_usb_is_port_ready(struct mwifiex_private *priv) 807 { 808 struct usb_card_rec *card = priv->adapter->card; 809 int idx; 810 811 for (idx = 0; idx < MWIFIEX_TX_DATA_PORT; idx++) { 812 if (priv->usb_port == card->port[idx].tx_data_ep) 813 return !card->port[idx].block_status; 814 } 815 816 return false; 817 } 818 819 static inline u8 mwifiex_usb_data_sent(struct mwifiex_adapter *adapter) 820 { 821 struct usb_card_rec *card = adapter->card; 822 int i; 823 824 for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) 825 if (!card->port[i].block_status) 826 return false; 827 828 return true; 829 } 830 831 /* This function write a command/data packet to card. */ 832 static int mwifiex_usb_host_to_card(struct mwifiex_adapter *adapter, u8 ep, 833 struct sk_buff *skb, 834 struct mwifiex_tx_param *tx_param) 835 { 836 struct usb_card_rec *card = adapter->card; 837 struct urb_context *context = NULL; 838 struct usb_tx_data_port *port = NULL; 839 u8 *data = (u8 *)skb->data; 840 struct urb *tx_urb; 841 int idx, ret = -EINPROGRESS; 842 843 if (adapter->is_suspended) { 844 mwifiex_dbg(adapter, ERROR, 845 "%s: not allowed while suspended\n", __func__); 846 return -1; 847 } 848 849 if (adapter->surprise_removed) { 850 mwifiex_dbg(adapter, ERROR, "%s: device removed\n", __func__); 851 return -1; 852 } 853 854 mwifiex_dbg(adapter, INFO, "%s: ep=%d\n", __func__, ep); 855 856 if (ep == card->tx_cmd_ep) { 857 context = &card->tx_cmd; 858 } else { 859 for (idx = 0; idx < MWIFIEX_TX_DATA_PORT; idx++) { 860 if (ep == card->port[idx].tx_data_ep) { 861 port = &card->port[idx]; 862 if (atomic_read(&port->tx_data_urb_pending) 863 >= MWIFIEX_TX_DATA_URB) { 864 port->block_status = true; 865 adapter->data_sent = 866 mwifiex_usb_data_sent(adapter); 867 return -EBUSY; 868 } 869 if (port->tx_data_ix >= MWIFIEX_TX_DATA_URB) 870 port->tx_data_ix = 0; 871 context = 872 &port->tx_data_list[port->tx_data_ix++]; 873 break; 874 } 875 } 876 if (!port) { 877 mwifiex_dbg(adapter, ERROR, "Wrong usb tx data port\n"); 878 return -1; 879 } 880 } 881 882 context->adapter = adapter; 883 context->ep = ep; 884 context->skb = skb; 885 tx_urb = context->urb; 886 887 usb_fill_bulk_urb(tx_urb, card->udev, usb_sndbulkpipe(card->udev, ep), 888 data, skb->len, mwifiex_usb_tx_complete, 889 (void *)context); 890 891 tx_urb->transfer_flags |= URB_ZERO_PACKET; 892 893 if (ep == card->tx_cmd_ep) 894 atomic_inc(&card->tx_cmd_urb_pending); 895 else 896 atomic_inc(&port->tx_data_urb_pending); 897 898 if (ep != card->tx_cmd_ep && 899 atomic_read(&port->tx_data_urb_pending) == 900 MWIFIEX_TX_DATA_URB) { 901 port->block_status = true; 902 adapter->data_sent = mwifiex_usb_data_sent(adapter); 903 ret = -ENOSR; 904 } 905 906 if (usb_submit_urb(tx_urb, GFP_ATOMIC)) { 907 mwifiex_dbg(adapter, ERROR, 908 "%s: usb_submit_urb failed\n", __func__); 909 if (ep == card->tx_cmd_ep) { 910 atomic_dec(&card->tx_cmd_urb_pending); 911 } else { 912 atomic_dec(&port->tx_data_urb_pending); 913 port->block_status = false; 914 adapter->data_sent = false; 915 if (port->tx_data_ix) 916 port->tx_data_ix--; 917 else 918 port->tx_data_ix = MWIFIEX_TX_DATA_URB; 919 } 920 ret = -1; 921 } 922 923 return ret; 924 } 925 926 /* This function register usb device and initialize parameter. */ 927 static int mwifiex_register_dev(struct mwifiex_adapter *adapter) 928 { 929 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card; 930 931 card->adapter = adapter; 932 933 switch (le16_to_cpu(card->udev->descriptor.idProduct)) { 934 case USB8997_PID_1: 935 case USB8997_PID_2: 936 adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_4K; 937 strcpy(adapter->fw_name, USB8997_DEFAULT_FW_NAME); 938 adapter->ext_scan = true; 939 break; 940 case USB8766_PID_1: 941 case USB8766_PID_2: 942 adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K; 943 strcpy(adapter->fw_name, USB8766_DEFAULT_FW_NAME); 944 adapter->ext_scan = true; 945 break; 946 case USB8801_PID_1: 947 case USB8801_PID_2: 948 adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K; 949 strcpy(adapter->fw_name, USB8801_DEFAULT_FW_NAME); 950 adapter->ext_scan = false; 951 break; 952 case USB8797_PID_1: 953 case USB8797_PID_2: 954 default: 955 adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K; 956 strcpy(adapter->fw_name, USB8797_DEFAULT_FW_NAME); 957 break; 958 } 959 960 adapter->usb_mc_status = false; 961 adapter->usb_mc_setup = false; 962 963 return 0; 964 } 965 966 static void mwifiex_unregister_dev(struct mwifiex_adapter *adapter) 967 { 968 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card; 969 970 card->adapter = NULL; 971 } 972 973 static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter, 974 struct mwifiex_fw_image *fw) 975 { 976 int ret = 0; 977 u8 *firmware = fw->fw_buf, *recv_buff; 978 u32 retries = USB8XXX_FW_MAX_RETRY + 1; 979 u32 dlen; 980 u32 fw_seqnum = 0, tlen = 0, dnld_cmd = 0; 981 struct fw_data *fwdata; 982 struct fw_sync_header sync_fw; 983 u8 check_winner = 1; 984 985 if (!firmware) { 986 mwifiex_dbg(adapter, ERROR, 987 "No firmware image found! Terminating download\n"); 988 ret = -1; 989 goto fw_exit; 990 } 991 992 /* Allocate memory for transmit */ 993 fwdata = kzalloc(FW_DNLD_TX_BUF_SIZE, GFP_KERNEL); 994 if (!fwdata) { 995 ret = -ENOMEM; 996 goto fw_exit; 997 } 998 999 /* Allocate memory for receive */ 1000 recv_buff = kzalloc(FW_DNLD_RX_BUF_SIZE, GFP_KERNEL); 1001 if (!recv_buff) { 1002 ret = -ENOMEM; 1003 goto cleanup; 1004 } 1005 1006 do { 1007 /* Send pseudo data to check winner status first */ 1008 if (check_winner) { 1009 memset(&fwdata->fw_hdr, 0, sizeof(struct fw_header)); 1010 dlen = 0; 1011 } else { 1012 /* copy the header of the fw_data to get the length */ 1013 memcpy(&fwdata->fw_hdr, &firmware[tlen], 1014 sizeof(struct fw_header)); 1015 1016 dlen = le32_to_cpu(fwdata->fw_hdr.data_len); 1017 dnld_cmd = le32_to_cpu(fwdata->fw_hdr.dnld_cmd); 1018 tlen += sizeof(struct fw_header); 1019 1020 /* Command 7 doesn't have data length field */ 1021 if (dnld_cmd == FW_CMD_7) 1022 dlen = 0; 1023 1024 memcpy(fwdata->data, &firmware[tlen], dlen); 1025 1026 fwdata->seq_num = cpu_to_le32(fw_seqnum); 1027 tlen += dlen; 1028 } 1029 1030 /* If the send/receive fails or CRC occurs then retry */ 1031 while (--retries) { 1032 u8 *buf = (u8 *)fwdata; 1033 u32 len = FW_DATA_XMIT_SIZE; 1034 1035 /* send the firmware block */ 1036 ret = mwifiex_write_data_sync(adapter, buf, &len, 1037 MWIFIEX_USB_EP_CMD_EVENT, 1038 MWIFIEX_USB_TIMEOUT); 1039 if (ret) { 1040 mwifiex_dbg(adapter, ERROR, 1041 "write_data_sync: failed: %d\n", 1042 ret); 1043 continue; 1044 } 1045 1046 buf = recv_buff; 1047 len = FW_DNLD_RX_BUF_SIZE; 1048 1049 /* Receive the firmware block response */ 1050 ret = mwifiex_read_data_sync(adapter, buf, &len, 1051 MWIFIEX_USB_EP_CMD_EVENT, 1052 MWIFIEX_USB_TIMEOUT); 1053 if (ret) { 1054 mwifiex_dbg(adapter, ERROR, 1055 "read_data_sync: failed: %d\n", 1056 ret); 1057 continue; 1058 } 1059 1060 memcpy(&sync_fw, recv_buff, 1061 sizeof(struct fw_sync_header)); 1062 1063 /* check 1st firmware block resp for highest bit set */ 1064 if (check_winner) { 1065 if (le32_to_cpu(sync_fw.cmd) & 0x80000000) { 1066 mwifiex_dbg(adapter, WARN, 1067 "USB is not the winner %#x\n", 1068 sync_fw.cmd); 1069 1070 /* returning success */ 1071 ret = 0; 1072 goto cleanup; 1073 } 1074 1075 mwifiex_dbg(adapter, MSG, 1076 "start to download FW...\n"); 1077 1078 check_winner = 0; 1079 break; 1080 } 1081 1082 /* check the firmware block response for CRC errors */ 1083 if (sync_fw.cmd) { 1084 mwifiex_dbg(adapter, ERROR, 1085 "FW received block with CRC %#x\n", 1086 sync_fw.cmd); 1087 ret = -1; 1088 continue; 1089 } 1090 1091 retries = USB8XXX_FW_MAX_RETRY + 1; 1092 break; 1093 } 1094 fw_seqnum++; 1095 } while ((dnld_cmd != FW_HAS_LAST_BLOCK) && retries); 1096 1097 cleanup: 1098 mwifiex_dbg(adapter, MSG, 1099 "info: FW download over, size %d bytes\n", tlen); 1100 1101 kfree(recv_buff); 1102 kfree(fwdata); 1103 1104 if (retries) 1105 ret = 0; 1106 fw_exit: 1107 return ret; 1108 } 1109 1110 static int mwifiex_usb_dnld_fw(struct mwifiex_adapter *adapter, 1111 struct mwifiex_fw_image *fw) 1112 { 1113 int ret; 1114 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card; 1115 1116 if (card->usb_boot_state == USB8XXX_FW_DNLD) { 1117 ret = mwifiex_prog_fw_w_helper(adapter, fw); 1118 if (ret) 1119 return -1; 1120 1121 /* Boot state changes after successful firmware download */ 1122 if (card->usb_boot_state == USB8XXX_FW_DNLD) 1123 return -1; 1124 } 1125 1126 ret = mwifiex_usb_rx_init(adapter); 1127 if (!ret) 1128 ret = mwifiex_usb_tx_init(adapter); 1129 1130 return ret; 1131 } 1132 1133 static void mwifiex_submit_rx_urb(struct mwifiex_adapter *adapter, u8 ep) 1134 { 1135 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card; 1136 1137 skb_push(card->rx_cmd.skb, INTF_HEADER_LEN); 1138 if ((ep == card->rx_cmd_ep) && 1139 (!atomic_read(&card->rx_cmd_urb_pending))) 1140 mwifiex_usb_submit_rx_urb(&card->rx_cmd, 1141 MWIFIEX_RX_CMD_BUF_SIZE); 1142 1143 return; 1144 } 1145 1146 static int mwifiex_usb_cmd_event_complete(struct mwifiex_adapter *adapter, 1147 struct sk_buff *skb) 1148 { 1149 mwifiex_submit_rx_urb(adapter, MWIFIEX_USB_EP_CMD_EVENT); 1150 1151 return 0; 1152 } 1153 1154 /* This function wakes up the card. */ 1155 static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter) 1156 { 1157 /* Simulation of HS_AWAKE event */ 1158 adapter->pm_wakeup_fw_try = false; 1159 del_timer(&adapter->wakeup_timer); 1160 adapter->pm_wakeup_card_req = false; 1161 adapter->ps_state = PS_STATE_AWAKE; 1162 1163 return 0; 1164 } 1165 1166 static void mwifiex_usb_submit_rem_rx_urbs(struct mwifiex_adapter *adapter) 1167 { 1168 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card; 1169 int i; 1170 struct urb_context *ctx; 1171 1172 for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) { 1173 if (card->rx_data_list[i].skb) 1174 continue; 1175 ctx = &card->rx_data_list[i]; 1176 mwifiex_usb_submit_rx_urb(ctx, MWIFIEX_RX_DATA_BUF_SIZE); 1177 } 1178 } 1179 1180 /* This function is called after the card has woken up. */ 1181 static inline int 1182 mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter) 1183 { 1184 return 0; 1185 } 1186 1187 static struct mwifiex_if_ops usb_ops = { 1188 .register_dev = mwifiex_register_dev, 1189 .unregister_dev = mwifiex_unregister_dev, 1190 .wakeup = mwifiex_pm_wakeup_card, 1191 .wakeup_complete = mwifiex_pm_wakeup_card_complete, 1192 1193 /* USB specific */ 1194 .dnld_fw = mwifiex_usb_dnld_fw, 1195 .cmdrsp_complete = mwifiex_usb_cmd_event_complete, 1196 .event_complete = mwifiex_usb_cmd_event_complete, 1197 .host_to_card = mwifiex_usb_host_to_card, 1198 .submit_rem_rx_urbs = mwifiex_usb_submit_rem_rx_urbs, 1199 .multi_port_resync = mwifiex_usb_port_resync, 1200 .is_port_ready = mwifiex_usb_is_port_ready, 1201 }; 1202 1203 module_usb_driver(mwifiex_usb_driver); 1204 1205 MODULE_AUTHOR("Marvell International Ltd."); 1206 MODULE_DESCRIPTION("Marvell WiFi-Ex USB Driver version" USB_VERSION); 1207 MODULE_VERSION(USB_VERSION); 1208 MODULE_LICENSE("GPL v2"); 1209 MODULE_FIRMWARE(USB8766_DEFAULT_FW_NAME); 1210 MODULE_FIRMWARE(USB8797_DEFAULT_FW_NAME); 1211 MODULE_FIRMWARE(USB8801_DEFAULT_FW_NAME); 1212 MODULE_FIRMWARE(USB8997_DEFAULT_FW_NAME); 1213