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 if (card->rx_cmd_ep == ctx->ep && 310 card->rx_cmd_ep_type == USB_ENDPOINT_XFER_INT) 311 usb_fill_int_urb(ctx->urb, card->udev, 312 usb_rcvintpipe(card->udev, ctx->ep), 313 ctx->skb->data, size, mwifiex_usb_rx_complete, 314 (void *)ctx, card->rx_cmd_interval); 315 else 316 usb_fill_bulk_urb(ctx->urb, card->udev, 317 usb_rcvbulkpipe(card->udev, ctx->ep), 318 ctx->skb->data, size, mwifiex_usb_rx_complete, 319 (void *)ctx); 320 321 if (card->rx_cmd_ep == ctx->ep) 322 atomic_inc(&card->rx_cmd_urb_pending); 323 else 324 atomic_inc(&card->rx_data_urb_pending); 325 326 if (usb_submit_urb(ctx->urb, GFP_ATOMIC)) { 327 mwifiex_dbg(adapter, ERROR, "usb_submit_urb failed\n"); 328 dev_kfree_skb_any(ctx->skb); 329 ctx->skb = NULL; 330 331 if (card->rx_cmd_ep == ctx->ep) 332 atomic_dec(&card->rx_cmd_urb_pending); 333 else 334 atomic_dec(&card->rx_data_urb_pending); 335 336 return -1; 337 } 338 339 return 0; 340 } 341 342 static void mwifiex_usb_free(struct usb_card_rec *card) 343 { 344 struct usb_tx_data_port *port; 345 int i, j; 346 347 if (atomic_read(&card->rx_cmd_urb_pending) && card->rx_cmd.urb) 348 usb_kill_urb(card->rx_cmd.urb); 349 350 usb_free_urb(card->rx_cmd.urb); 351 card->rx_cmd.urb = NULL; 352 353 if (atomic_read(&card->rx_data_urb_pending)) 354 for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) 355 if (card->rx_data_list[i].urb) 356 usb_kill_urb(card->rx_data_list[i].urb); 357 358 for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) { 359 usb_free_urb(card->rx_data_list[i].urb); 360 card->rx_data_list[i].urb = NULL; 361 } 362 363 for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) { 364 port = &card->port[i]; 365 for (j = 0; j < MWIFIEX_TX_DATA_URB; j++) { 366 usb_free_urb(port->tx_data_list[j].urb); 367 port->tx_data_list[j].urb = NULL; 368 } 369 } 370 371 usb_free_urb(card->tx_cmd.urb); 372 card->tx_cmd.urb = NULL; 373 374 return; 375 } 376 377 /* This function probes an mwifiex device and registers it. It allocates 378 * the card structure, initiates the device registration and initialization 379 * procedure by adding a logical interface. 380 */ 381 static int mwifiex_usb_probe(struct usb_interface *intf, 382 const struct usb_device_id *id) 383 { 384 struct usb_device *udev = interface_to_usbdev(intf); 385 struct usb_host_interface *iface_desc = intf->cur_altsetting; 386 struct usb_endpoint_descriptor *epd; 387 int ret, i; 388 struct usb_card_rec *card; 389 u16 id_vendor, id_product, bcd_device; 390 391 card = devm_kzalloc(&intf->dev, sizeof(*card), GFP_KERNEL); 392 if (!card) 393 return -ENOMEM; 394 395 init_completion(&card->fw_done); 396 397 id_vendor = le16_to_cpu(udev->descriptor.idVendor); 398 id_product = le16_to_cpu(udev->descriptor.idProduct); 399 bcd_device = le16_to_cpu(udev->descriptor.bcdDevice); 400 pr_debug("info: VID/PID = %X/%X, Boot2 version = %X\n", 401 id_vendor, id_product, bcd_device); 402 403 /* PID_1 is used for firmware downloading only */ 404 switch (id_product) { 405 case USB8766_PID_1: 406 case USB8797_PID_1: 407 case USB8801_PID_1: 408 case USB8997_PID_1: 409 card->usb_boot_state = USB8XXX_FW_DNLD; 410 break; 411 case USB8766_PID_2: 412 case USB8797_PID_2: 413 case USB8801_PID_2: 414 case USB8997_PID_2: 415 card->usb_boot_state = USB8XXX_FW_READY; 416 break; 417 default: 418 pr_warn("unknown id_product %#x\n", id_product); 419 card->usb_boot_state = USB8XXX_FW_DNLD; 420 break; 421 } 422 423 card->udev = udev; 424 card->intf = intf; 425 426 pr_debug("info: bcdUSB=%#x Device Class=%#x SubClass=%#x Protocol=%#x\n", 427 udev->descriptor.bcdUSB, udev->descriptor.bDeviceClass, 428 udev->descriptor.bDeviceSubClass, 429 udev->descriptor.bDeviceProtocol); 430 431 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { 432 epd = &iface_desc->endpoint[i].desc; 433 if (usb_endpoint_dir_in(epd) && 434 usb_endpoint_num(epd) == MWIFIEX_USB_EP_CMD_EVENT && 435 (usb_endpoint_xfer_bulk(epd) || 436 usb_endpoint_xfer_int(epd))) { 437 card->rx_cmd_ep_type = usb_endpoint_type(epd); 438 card->rx_cmd_interval = epd->bInterval; 439 pr_debug("info: Rx CMD/EVT:: max pkt size: %d, addr: %d, ep_type: %d\n", 440 le16_to_cpu(epd->wMaxPacketSize), 441 epd->bEndpointAddress, card->rx_cmd_ep_type); 442 card->rx_cmd_ep = usb_endpoint_num(epd); 443 atomic_set(&card->rx_cmd_urb_pending, 0); 444 } 445 if (usb_endpoint_dir_in(epd) && 446 usb_endpoint_num(epd) == MWIFIEX_USB_EP_DATA && 447 usb_endpoint_xfer_bulk(epd)) { 448 pr_debug("info: bulk IN: max pkt size: %d, addr: %d\n", 449 le16_to_cpu(epd->wMaxPacketSize), 450 epd->bEndpointAddress); 451 card->rx_data_ep = usb_endpoint_num(epd); 452 atomic_set(&card->rx_data_urb_pending, 0); 453 } 454 if (usb_endpoint_dir_out(epd) && 455 usb_endpoint_num(epd) == MWIFIEX_USB_EP_DATA && 456 usb_endpoint_xfer_bulk(epd)) { 457 pr_debug("info: bulk OUT: max pkt size: %d, addr: %d\n", 458 le16_to_cpu(epd->wMaxPacketSize), 459 epd->bEndpointAddress); 460 card->port[0].tx_data_ep = usb_endpoint_num(epd); 461 atomic_set(&card->port[0].tx_data_urb_pending, 0); 462 } 463 if (usb_endpoint_dir_out(epd) && 464 usb_endpoint_num(epd) == MWIFIEX_USB_EP_DATA_CH2 && 465 usb_endpoint_xfer_bulk(epd)) { 466 pr_debug("info: bulk OUT chan2:\t" 467 "max pkt size: %d, addr: %d\n", 468 le16_to_cpu(epd->wMaxPacketSize), 469 epd->bEndpointAddress); 470 card->port[1].tx_data_ep = usb_endpoint_num(epd); 471 atomic_set(&card->port[1].tx_data_urb_pending, 0); 472 } 473 if (usb_endpoint_dir_out(epd) && 474 usb_endpoint_num(epd) == MWIFIEX_USB_EP_CMD_EVENT && 475 (usb_endpoint_xfer_bulk(epd) || 476 usb_endpoint_xfer_int(epd))) { 477 card->tx_cmd_ep_type = usb_endpoint_type(epd); 478 card->tx_cmd_interval = epd->bInterval; 479 pr_debug("info: bulk OUT: max pkt size: %d, addr: %d\n", 480 le16_to_cpu(epd->wMaxPacketSize), 481 epd->bEndpointAddress); 482 pr_debug("info: Tx CMD:: max pkt size: %d, addr: %d, ep_type: %d\n", 483 le16_to_cpu(epd->wMaxPacketSize), 484 epd->bEndpointAddress, card->tx_cmd_ep_type); 485 card->tx_cmd_ep = usb_endpoint_num(epd); 486 atomic_set(&card->tx_cmd_urb_pending, 0); 487 card->bulk_out_maxpktsize = 488 le16_to_cpu(epd->wMaxPacketSize); 489 } 490 } 491 492 usb_set_intfdata(intf, card); 493 494 ret = mwifiex_add_card(card, &card->fw_done, &usb_ops, 495 MWIFIEX_USB, &card->udev->dev); 496 if (ret) { 497 pr_err("%s: mwifiex_add_card failed: %d\n", __func__, ret); 498 usb_reset_device(udev); 499 return ret; 500 } 501 502 usb_get_dev(udev); 503 504 return 0; 505 } 506 507 /* Kernel needs to suspend all functions separately. Therefore all 508 * registered functions must have drivers with suspend and resume 509 * methods. Failing that the kernel simply removes the whole card. 510 * 511 * If already not suspended, this function allocates and sends a 512 * 'host sleep activate' request to the firmware and turns off the traffic. 513 */ 514 static int mwifiex_usb_suspend(struct usb_interface *intf, pm_message_t message) 515 { 516 struct usb_card_rec *card = usb_get_intfdata(intf); 517 struct mwifiex_adapter *adapter; 518 struct usb_tx_data_port *port; 519 int i, j; 520 521 /* Might still be loading firmware */ 522 wait_for_completion(&card->fw_done); 523 524 adapter = card->adapter; 525 if (!adapter) { 526 dev_err(&intf->dev, "card is not valid\n"); 527 return 0; 528 } 529 530 if (unlikely(adapter->is_suspended)) 531 mwifiex_dbg(adapter, WARN, 532 "Device already suspended\n"); 533 534 /* Enable the Host Sleep */ 535 if (!mwifiex_enable_hs(adapter)) { 536 mwifiex_dbg(adapter, ERROR, 537 "cmd: failed to suspend\n"); 538 adapter->hs_enabling = false; 539 return -EFAULT; 540 } 541 542 543 /* 'is_suspended' flag indicates device is suspended. 544 * It must be set here before the usb_kill_urb() calls. Reason 545 * is in the complete handlers, urb->status(= -ENOENT) and 546 * this flag is used in combination to distinguish between a 547 * 'suspended' state and a 'disconnect' one. 548 */ 549 adapter->is_suspended = true; 550 adapter->hs_enabling = false; 551 552 if (atomic_read(&card->rx_cmd_urb_pending) && card->rx_cmd.urb) 553 usb_kill_urb(card->rx_cmd.urb); 554 555 if (atomic_read(&card->rx_data_urb_pending)) 556 for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) 557 if (card->rx_data_list[i].urb) 558 usb_kill_urb(card->rx_data_list[i].urb); 559 560 for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) { 561 port = &card->port[i]; 562 for (j = 0; j < MWIFIEX_TX_DATA_URB; j++) { 563 if (port->tx_data_list[j].urb) 564 usb_kill_urb(port->tx_data_list[j].urb); 565 } 566 } 567 568 if (card->tx_cmd.urb) 569 usb_kill_urb(card->tx_cmd.urb); 570 571 return 0; 572 } 573 574 /* Kernel needs to suspend all functions separately. Therefore all 575 * registered functions must have drivers with suspend and resume 576 * methods. Failing that the kernel simply removes the whole card. 577 * 578 * If already not resumed, this function turns on the traffic and 579 * sends a 'host sleep cancel' request to the firmware. 580 */ 581 static int mwifiex_usb_resume(struct usb_interface *intf) 582 { 583 struct usb_card_rec *card = usb_get_intfdata(intf); 584 struct mwifiex_adapter *adapter; 585 int i; 586 587 if (!card->adapter) { 588 dev_err(&intf->dev, "%s: card->adapter is NULL\n", 589 __func__); 590 return 0; 591 } 592 adapter = card->adapter; 593 594 if (unlikely(!adapter->is_suspended)) { 595 mwifiex_dbg(adapter, WARN, 596 "Device already resumed\n"); 597 return 0; 598 } 599 600 /* Indicate device resumed. The netdev queue will be resumed only 601 * after the urbs have been re-submitted 602 */ 603 adapter->is_suspended = false; 604 605 if (!atomic_read(&card->rx_data_urb_pending)) 606 for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) 607 mwifiex_usb_submit_rx_urb(&card->rx_data_list[i], 608 MWIFIEX_RX_DATA_BUF_SIZE); 609 610 if (!atomic_read(&card->rx_cmd_urb_pending)) { 611 card->rx_cmd.skb = dev_alloc_skb(MWIFIEX_RX_CMD_BUF_SIZE); 612 if (card->rx_cmd.skb) 613 mwifiex_usb_submit_rx_urb(&card->rx_cmd, 614 MWIFIEX_RX_CMD_BUF_SIZE); 615 } 616 617 /* Disable Host Sleep */ 618 if (adapter->hs_activated) 619 mwifiex_cancel_hs(mwifiex_get_priv(adapter, 620 MWIFIEX_BSS_ROLE_ANY), 621 MWIFIEX_ASYNC_CMD); 622 623 return 0; 624 } 625 626 static void mwifiex_usb_disconnect(struct usb_interface *intf) 627 { 628 struct usb_card_rec *card = usb_get_intfdata(intf); 629 struct mwifiex_adapter *adapter; 630 631 wait_for_completion(&card->fw_done); 632 633 adapter = card->adapter; 634 if (!adapter || !adapter->priv_num) 635 return; 636 637 if (card->udev->state != USB_STATE_NOTATTACHED && !adapter->mfg_mode) { 638 mwifiex_deauthenticate_all(adapter); 639 640 mwifiex_init_shutdown_fw(mwifiex_get_priv(adapter, 641 MWIFIEX_BSS_ROLE_ANY), 642 MWIFIEX_FUNC_SHUTDOWN); 643 } 644 645 mwifiex_usb_free(card); 646 647 mwifiex_dbg(adapter, FATAL, 648 "%s: removing card\n", __func__); 649 mwifiex_remove_card(adapter); 650 651 usb_put_dev(interface_to_usbdev(intf)); 652 } 653 654 static struct usb_driver mwifiex_usb_driver = { 655 .name = "mwifiex_usb", 656 .probe = mwifiex_usb_probe, 657 .disconnect = mwifiex_usb_disconnect, 658 .id_table = mwifiex_usb_table, 659 .suspend = mwifiex_usb_suspend, 660 .resume = mwifiex_usb_resume, 661 .soft_unbind = 1, 662 }; 663 664 static int mwifiex_usb_tx_init(struct mwifiex_adapter *adapter) 665 { 666 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card; 667 struct usb_tx_data_port *port; 668 int i, j; 669 670 card->tx_cmd.adapter = adapter; 671 card->tx_cmd.ep = card->tx_cmd_ep; 672 673 card->tx_cmd.urb = usb_alloc_urb(0, GFP_KERNEL); 674 if (!card->tx_cmd.urb) 675 return -ENOMEM; 676 677 for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) { 678 port = &card->port[i]; 679 if (!port->tx_data_ep) 680 continue; 681 port->tx_data_ix = 0; 682 if (port->tx_data_ep == MWIFIEX_USB_EP_DATA) 683 port->block_status = false; 684 else 685 port->block_status = true; 686 for (j = 0; j < MWIFIEX_TX_DATA_URB; j++) { 687 port->tx_data_list[j].adapter = adapter; 688 port->tx_data_list[j].ep = port->tx_data_ep; 689 port->tx_data_list[j].urb = 690 usb_alloc_urb(0, GFP_KERNEL); 691 if (!port->tx_data_list[j].urb) 692 return -ENOMEM; 693 } 694 } 695 696 return 0; 697 } 698 699 static int mwifiex_usb_rx_init(struct mwifiex_adapter *adapter) 700 { 701 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card; 702 int i; 703 704 card->rx_cmd.adapter = adapter; 705 card->rx_cmd.ep = card->rx_cmd_ep; 706 707 card->rx_cmd.urb = usb_alloc_urb(0, GFP_KERNEL); 708 if (!card->rx_cmd.urb) 709 return -ENOMEM; 710 711 card->rx_cmd.skb = dev_alloc_skb(MWIFIEX_RX_CMD_BUF_SIZE); 712 if (!card->rx_cmd.skb) 713 return -ENOMEM; 714 715 if (mwifiex_usb_submit_rx_urb(&card->rx_cmd, MWIFIEX_RX_CMD_BUF_SIZE)) 716 return -1; 717 718 for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) { 719 card->rx_data_list[i].adapter = adapter; 720 card->rx_data_list[i].ep = card->rx_data_ep; 721 722 card->rx_data_list[i].urb = usb_alloc_urb(0, GFP_KERNEL); 723 if (!card->rx_data_list[i].urb) 724 return -1; 725 if (mwifiex_usb_submit_rx_urb(&card->rx_data_list[i], 726 MWIFIEX_RX_DATA_BUF_SIZE)) 727 return -1; 728 } 729 730 return 0; 731 } 732 733 static int mwifiex_write_data_sync(struct mwifiex_adapter *adapter, u8 *pbuf, 734 u32 *len, u8 ep, u32 timeout) 735 { 736 struct usb_card_rec *card = adapter->card; 737 int actual_length, ret; 738 739 if (!(*len % card->bulk_out_maxpktsize)) 740 (*len)++; 741 742 /* Send the data block */ 743 ret = usb_bulk_msg(card->udev, usb_sndbulkpipe(card->udev, ep), pbuf, 744 *len, &actual_length, timeout); 745 if (ret) { 746 mwifiex_dbg(adapter, ERROR, 747 "usb_bulk_msg for tx failed: %d\n", ret); 748 return ret; 749 } 750 751 *len = actual_length; 752 753 return ret; 754 } 755 756 static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *pbuf, 757 u32 *len, u8 ep, u32 timeout) 758 { 759 struct usb_card_rec *card = adapter->card; 760 int actual_length, ret; 761 762 /* Receive the data response */ 763 ret = usb_bulk_msg(card->udev, usb_rcvbulkpipe(card->udev, ep), pbuf, 764 *len, &actual_length, timeout); 765 if (ret) { 766 mwifiex_dbg(adapter, ERROR, 767 "usb_bulk_msg for rx failed: %d\n", ret); 768 return ret; 769 } 770 771 *len = actual_length; 772 773 return ret; 774 } 775 776 static void mwifiex_usb_port_resync(struct mwifiex_adapter *adapter) 777 { 778 struct usb_card_rec *card = adapter->card; 779 u8 active_port = MWIFIEX_USB_EP_DATA; 780 struct mwifiex_private *priv = NULL; 781 int i; 782 783 if (adapter->usb_mc_status) { 784 for (i = 0; i < adapter->priv_num; i++) { 785 priv = adapter->priv[i]; 786 if (!priv) 787 continue; 788 if ((priv->bss_role == MWIFIEX_BSS_ROLE_UAP && 789 !priv->bss_started) || 790 (priv->bss_role == MWIFIEX_BSS_ROLE_STA && 791 !priv->media_connected)) 792 priv->usb_port = MWIFIEX_USB_EP_DATA; 793 } 794 for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) 795 card->port[i].block_status = false; 796 } else { 797 for (i = 0; i < adapter->priv_num; i++) { 798 priv = adapter->priv[i]; 799 if (!priv) 800 continue; 801 if ((priv->bss_role == MWIFIEX_BSS_ROLE_UAP && 802 priv->bss_started) || 803 (priv->bss_role == MWIFIEX_BSS_ROLE_STA && 804 priv->media_connected)) { 805 active_port = priv->usb_port; 806 break; 807 } 808 } 809 for (i = 0; i < adapter->priv_num; i++) { 810 priv = adapter->priv[i]; 811 if (priv) 812 priv->usb_port = active_port; 813 } 814 for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) { 815 if (active_port == card->port[i].tx_data_ep) 816 card->port[i].block_status = false; 817 else 818 card->port[i].block_status = true; 819 } 820 } 821 } 822 823 static bool mwifiex_usb_is_port_ready(struct mwifiex_private *priv) 824 { 825 struct usb_card_rec *card = priv->adapter->card; 826 int idx; 827 828 for (idx = 0; idx < MWIFIEX_TX_DATA_PORT; idx++) { 829 if (priv->usb_port == card->port[idx].tx_data_ep) 830 return !card->port[idx].block_status; 831 } 832 833 return false; 834 } 835 836 static inline u8 mwifiex_usb_data_sent(struct mwifiex_adapter *adapter) 837 { 838 struct usb_card_rec *card = adapter->card; 839 int i; 840 841 for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) 842 if (!card->port[i].block_status) 843 return false; 844 845 return true; 846 } 847 848 /* This function write a command/data packet to card. */ 849 static int mwifiex_usb_host_to_card(struct mwifiex_adapter *adapter, u8 ep, 850 struct sk_buff *skb, 851 struct mwifiex_tx_param *tx_param) 852 { 853 struct usb_card_rec *card = adapter->card; 854 struct urb_context *context = NULL; 855 struct usb_tx_data_port *port = NULL; 856 u8 *data = (u8 *)skb->data; 857 struct urb *tx_urb; 858 int idx, ret = -EINPROGRESS; 859 860 if (adapter->is_suspended) { 861 mwifiex_dbg(adapter, ERROR, 862 "%s: not allowed while suspended\n", __func__); 863 return -1; 864 } 865 866 if (adapter->surprise_removed) { 867 mwifiex_dbg(adapter, ERROR, "%s: device removed\n", __func__); 868 return -1; 869 } 870 871 mwifiex_dbg(adapter, INFO, "%s: ep=%d\n", __func__, ep); 872 873 if (ep == card->tx_cmd_ep) { 874 context = &card->tx_cmd; 875 } else { 876 for (idx = 0; idx < MWIFIEX_TX_DATA_PORT; idx++) { 877 if (ep == card->port[idx].tx_data_ep) { 878 port = &card->port[idx]; 879 if (atomic_read(&port->tx_data_urb_pending) 880 >= MWIFIEX_TX_DATA_URB) { 881 port->block_status = true; 882 adapter->data_sent = 883 mwifiex_usb_data_sent(adapter); 884 return -EBUSY; 885 } 886 if (port->tx_data_ix >= MWIFIEX_TX_DATA_URB) 887 port->tx_data_ix = 0; 888 context = 889 &port->tx_data_list[port->tx_data_ix++]; 890 break; 891 } 892 } 893 if (!port) { 894 mwifiex_dbg(adapter, ERROR, "Wrong usb tx data port\n"); 895 return -1; 896 } 897 } 898 899 context->adapter = adapter; 900 context->ep = ep; 901 context->skb = skb; 902 tx_urb = context->urb; 903 904 if (ep == card->tx_cmd_ep && 905 card->tx_cmd_ep_type == USB_ENDPOINT_XFER_INT) 906 usb_fill_int_urb(tx_urb, card->udev, 907 usb_sndintpipe(card->udev, ep), data, 908 skb->len, mwifiex_usb_tx_complete, 909 (void *)context, card->tx_cmd_interval); 910 else 911 usb_fill_bulk_urb(tx_urb, card->udev, 912 usb_sndbulkpipe(card->udev, ep), data, 913 skb->len, mwifiex_usb_tx_complete, 914 (void *)context); 915 916 tx_urb->transfer_flags |= URB_ZERO_PACKET; 917 918 if (ep == card->tx_cmd_ep) 919 atomic_inc(&card->tx_cmd_urb_pending); 920 else 921 atomic_inc(&port->tx_data_urb_pending); 922 923 if (ep != card->tx_cmd_ep && 924 atomic_read(&port->tx_data_urb_pending) == 925 MWIFIEX_TX_DATA_URB) { 926 port->block_status = true; 927 adapter->data_sent = mwifiex_usb_data_sent(adapter); 928 ret = -ENOSR; 929 } 930 931 if (usb_submit_urb(tx_urb, GFP_ATOMIC)) { 932 mwifiex_dbg(adapter, ERROR, 933 "%s: usb_submit_urb failed\n", __func__); 934 if (ep == card->tx_cmd_ep) { 935 atomic_dec(&card->tx_cmd_urb_pending); 936 } else { 937 atomic_dec(&port->tx_data_urb_pending); 938 port->block_status = false; 939 adapter->data_sent = false; 940 if (port->tx_data_ix) 941 port->tx_data_ix--; 942 else 943 port->tx_data_ix = MWIFIEX_TX_DATA_URB; 944 } 945 ret = -1; 946 } 947 948 return ret; 949 } 950 951 /* This function register usb device and initialize parameter. */ 952 static int mwifiex_register_dev(struct mwifiex_adapter *adapter) 953 { 954 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card; 955 956 card->adapter = adapter; 957 958 switch (le16_to_cpu(card->udev->descriptor.idProduct)) { 959 case USB8997_PID_1: 960 case USB8997_PID_2: 961 adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_4K; 962 strcpy(adapter->fw_name, USB8997_DEFAULT_FW_NAME); 963 adapter->ext_scan = true; 964 break; 965 case USB8766_PID_1: 966 case USB8766_PID_2: 967 adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K; 968 strcpy(adapter->fw_name, USB8766_DEFAULT_FW_NAME); 969 adapter->ext_scan = true; 970 break; 971 case USB8801_PID_1: 972 case USB8801_PID_2: 973 adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K; 974 strcpy(adapter->fw_name, USB8801_DEFAULT_FW_NAME); 975 adapter->ext_scan = false; 976 break; 977 case USB8797_PID_1: 978 case USB8797_PID_2: 979 default: 980 adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K; 981 strcpy(adapter->fw_name, USB8797_DEFAULT_FW_NAME); 982 break; 983 } 984 985 adapter->usb_mc_status = false; 986 adapter->usb_mc_setup = false; 987 988 return 0; 989 } 990 991 static void mwifiex_unregister_dev(struct mwifiex_adapter *adapter) 992 { 993 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card; 994 995 card->adapter = NULL; 996 } 997 998 static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter, 999 struct mwifiex_fw_image *fw) 1000 { 1001 int ret = 0; 1002 u8 *firmware = fw->fw_buf, *recv_buff; 1003 u32 retries = USB8XXX_FW_MAX_RETRY + 1; 1004 u32 dlen; 1005 u32 fw_seqnum = 0, tlen = 0, dnld_cmd = 0; 1006 struct fw_data *fwdata; 1007 struct fw_sync_header sync_fw; 1008 u8 check_winner = 1; 1009 1010 if (!firmware) { 1011 mwifiex_dbg(adapter, ERROR, 1012 "No firmware image found! Terminating download\n"); 1013 ret = -1; 1014 goto fw_exit; 1015 } 1016 1017 /* Allocate memory for transmit */ 1018 fwdata = kzalloc(FW_DNLD_TX_BUF_SIZE, GFP_KERNEL); 1019 if (!fwdata) { 1020 ret = -ENOMEM; 1021 goto fw_exit; 1022 } 1023 1024 /* Allocate memory for receive */ 1025 recv_buff = kzalloc(FW_DNLD_RX_BUF_SIZE, GFP_KERNEL); 1026 if (!recv_buff) { 1027 ret = -ENOMEM; 1028 goto cleanup; 1029 } 1030 1031 do { 1032 /* Send pseudo data to check winner status first */ 1033 if (check_winner) { 1034 memset(&fwdata->fw_hdr, 0, sizeof(struct fw_header)); 1035 dlen = 0; 1036 } else { 1037 /* copy the header of the fw_data to get the length */ 1038 memcpy(&fwdata->fw_hdr, &firmware[tlen], 1039 sizeof(struct fw_header)); 1040 1041 dlen = le32_to_cpu(fwdata->fw_hdr.data_len); 1042 dnld_cmd = le32_to_cpu(fwdata->fw_hdr.dnld_cmd); 1043 tlen += sizeof(struct fw_header); 1044 1045 /* Command 7 doesn't have data length field */ 1046 if (dnld_cmd == FW_CMD_7) 1047 dlen = 0; 1048 1049 memcpy(fwdata->data, &firmware[tlen], dlen); 1050 1051 fwdata->seq_num = cpu_to_le32(fw_seqnum); 1052 tlen += dlen; 1053 } 1054 1055 /* If the send/receive fails or CRC occurs then retry */ 1056 while (--retries) { 1057 u8 *buf = (u8 *)fwdata; 1058 u32 len = FW_DATA_XMIT_SIZE; 1059 1060 /* send the firmware block */ 1061 ret = mwifiex_write_data_sync(adapter, buf, &len, 1062 MWIFIEX_USB_EP_CMD_EVENT, 1063 MWIFIEX_USB_TIMEOUT); 1064 if (ret) { 1065 mwifiex_dbg(adapter, ERROR, 1066 "write_data_sync: failed: %d\n", 1067 ret); 1068 continue; 1069 } 1070 1071 buf = recv_buff; 1072 len = FW_DNLD_RX_BUF_SIZE; 1073 1074 /* Receive the firmware block response */ 1075 ret = mwifiex_read_data_sync(adapter, buf, &len, 1076 MWIFIEX_USB_EP_CMD_EVENT, 1077 MWIFIEX_USB_TIMEOUT); 1078 if (ret) { 1079 mwifiex_dbg(adapter, ERROR, 1080 "read_data_sync: failed: %d\n", 1081 ret); 1082 continue; 1083 } 1084 1085 memcpy(&sync_fw, recv_buff, 1086 sizeof(struct fw_sync_header)); 1087 1088 /* check 1st firmware block resp for highest bit set */ 1089 if (check_winner) { 1090 if (le32_to_cpu(sync_fw.cmd) & 0x80000000) { 1091 mwifiex_dbg(adapter, WARN, 1092 "USB is not the winner %#x\n", 1093 sync_fw.cmd); 1094 1095 /* returning success */ 1096 ret = 0; 1097 goto cleanup; 1098 } 1099 1100 mwifiex_dbg(adapter, MSG, 1101 "start to download FW...\n"); 1102 1103 check_winner = 0; 1104 break; 1105 } 1106 1107 /* check the firmware block response for CRC errors */ 1108 if (sync_fw.cmd) { 1109 mwifiex_dbg(adapter, ERROR, 1110 "FW received block with CRC %#x\n", 1111 sync_fw.cmd); 1112 ret = -1; 1113 continue; 1114 } 1115 1116 retries = USB8XXX_FW_MAX_RETRY + 1; 1117 break; 1118 } 1119 fw_seqnum++; 1120 } while ((dnld_cmd != FW_HAS_LAST_BLOCK) && retries); 1121 1122 cleanup: 1123 mwifiex_dbg(adapter, MSG, 1124 "info: FW download over, size %d bytes\n", tlen); 1125 1126 kfree(recv_buff); 1127 kfree(fwdata); 1128 1129 if (retries) 1130 ret = 0; 1131 fw_exit: 1132 return ret; 1133 } 1134 1135 static int mwifiex_usb_dnld_fw(struct mwifiex_adapter *adapter, 1136 struct mwifiex_fw_image *fw) 1137 { 1138 int ret; 1139 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card; 1140 1141 if (card->usb_boot_state == USB8XXX_FW_DNLD) { 1142 ret = mwifiex_prog_fw_w_helper(adapter, fw); 1143 if (ret) 1144 return -1; 1145 1146 /* Boot state changes after successful firmware download */ 1147 if (card->usb_boot_state == USB8XXX_FW_DNLD) 1148 return -1; 1149 } 1150 1151 ret = mwifiex_usb_rx_init(adapter); 1152 if (!ret) 1153 ret = mwifiex_usb_tx_init(adapter); 1154 1155 return ret; 1156 } 1157 1158 static void mwifiex_submit_rx_urb(struct mwifiex_adapter *adapter, u8 ep) 1159 { 1160 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card; 1161 1162 skb_push(card->rx_cmd.skb, INTF_HEADER_LEN); 1163 if ((ep == card->rx_cmd_ep) && 1164 (!atomic_read(&card->rx_cmd_urb_pending))) 1165 mwifiex_usb_submit_rx_urb(&card->rx_cmd, 1166 MWIFIEX_RX_CMD_BUF_SIZE); 1167 1168 return; 1169 } 1170 1171 static int mwifiex_usb_cmd_event_complete(struct mwifiex_adapter *adapter, 1172 struct sk_buff *skb) 1173 { 1174 mwifiex_submit_rx_urb(adapter, MWIFIEX_USB_EP_CMD_EVENT); 1175 1176 return 0; 1177 } 1178 1179 /* This function wakes up the card. */ 1180 static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter) 1181 { 1182 /* Simulation of HS_AWAKE event */ 1183 adapter->pm_wakeup_fw_try = false; 1184 del_timer(&adapter->wakeup_timer); 1185 adapter->pm_wakeup_card_req = false; 1186 adapter->ps_state = PS_STATE_AWAKE; 1187 1188 return 0; 1189 } 1190 1191 static void mwifiex_usb_submit_rem_rx_urbs(struct mwifiex_adapter *adapter) 1192 { 1193 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card; 1194 int i; 1195 struct urb_context *ctx; 1196 1197 for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) { 1198 if (card->rx_data_list[i].skb) 1199 continue; 1200 ctx = &card->rx_data_list[i]; 1201 mwifiex_usb_submit_rx_urb(ctx, MWIFIEX_RX_DATA_BUF_SIZE); 1202 } 1203 } 1204 1205 /* This function is called after the card has woken up. */ 1206 static inline int 1207 mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter) 1208 { 1209 return 0; 1210 } 1211 1212 static struct mwifiex_if_ops usb_ops = { 1213 .register_dev = mwifiex_register_dev, 1214 .unregister_dev = mwifiex_unregister_dev, 1215 .wakeup = mwifiex_pm_wakeup_card, 1216 .wakeup_complete = mwifiex_pm_wakeup_card_complete, 1217 1218 /* USB specific */ 1219 .dnld_fw = mwifiex_usb_dnld_fw, 1220 .cmdrsp_complete = mwifiex_usb_cmd_event_complete, 1221 .event_complete = mwifiex_usb_cmd_event_complete, 1222 .host_to_card = mwifiex_usb_host_to_card, 1223 .submit_rem_rx_urbs = mwifiex_usb_submit_rem_rx_urbs, 1224 .multi_port_resync = mwifiex_usb_port_resync, 1225 .is_port_ready = mwifiex_usb_is_port_ready, 1226 }; 1227 1228 module_usb_driver(mwifiex_usb_driver); 1229 1230 MODULE_AUTHOR("Marvell International Ltd."); 1231 MODULE_DESCRIPTION("Marvell WiFi-Ex USB Driver version" USB_VERSION); 1232 MODULE_VERSION(USB_VERSION); 1233 MODULE_LICENSE("GPL v2"); 1234 MODULE_FIRMWARE(USB8766_DEFAULT_FW_NAME); 1235 MODULE_FIRMWARE(USB8797_DEFAULT_FW_NAME); 1236 MODULE_FIRMWARE(USB8801_DEFAULT_FW_NAME); 1237 MODULE_FIRMWARE(USB8997_DEFAULT_FW_NAME); 1238