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