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 const 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 || 185 test_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags)) { 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 (!test_bit(MWIFIEX_IS_SUSPENDED, &adapter->work_flags)) { 223 mwifiex_dbg(adapter, FATAL, 224 "Card is removed: %d\n", urb->status); 225 set_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags); 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 (test_bit(MWIFIEX_IS_SUSPENDED, &adapter->work_flags)) { 302 if (card->rx_cmd_ep == ctx->ep) { 303 mwifiex_dbg(adapter, INFO, "%s: free rx_cmd skb\n", 304 __func__); 305 dev_kfree_skb_any(ctx->skb); 306 ctx->skb = NULL; 307 } 308 mwifiex_dbg(adapter, ERROR, 309 "%s: card removed/suspended, EP %d rx_cmd URB submit skipped\n", 310 __func__, ctx->ep); 311 return -1; 312 } 313 314 if (card->rx_cmd_ep != ctx->ep) { 315 ctx->skb = dev_alloc_skb(size); 316 if (!ctx->skb) { 317 mwifiex_dbg(adapter, ERROR, 318 "%s: dev_alloc_skb failed\n", __func__); 319 return -ENOMEM; 320 } 321 } 322 323 if (card->rx_cmd_ep == ctx->ep && 324 card->rx_cmd_ep_type == USB_ENDPOINT_XFER_INT) 325 usb_fill_int_urb(ctx->urb, card->udev, 326 usb_rcvintpipe(card->udev, ctx->ep), 327 ctx->skb->data, size, mwifiex_usb_rx_complete, 328 (void *)ctx, card->rx_cmd_interval); 329 else 330 usb_fill_bulk_urb(ctx->urb, card->udev, 331 usb_rcvbulkpipe(card->udev, ctx->ep), 332 ctx->skb->data, size, mwifiex_usb_rx_complete, 333 (void *)ctx); 334 335 if (card->rx_cmd_ep == ctx->ep) 336 atomic_inc(&card->rx_cmd_urb_pending); 337 else 338 atomic_inc(&card->rx_data_urb_pending); 339 340 if (usb_submit_urb(ctx->urb, GFP_ATOMIC)) { 341 mwifiex_dbg(adapter, ERROR, "usb_submit_urb failed\n"); 342 dev_kfree_skb_any(ctx->skb); 343 ctx->skb = NULL; 344 345 if (card->rx_cmd_ep == ctx->ep) 346 atomic_dec(&card->rx_cmd_urb_pending); 347 else 348 atomic_dec(&card->rx_data_urb_pending); 349 350 return -1; 351 } 352 353 return 0; 354 } 355 356 static void mwifiex_usb_free(struct usb_card_rec *card) 357 { 358 struct usb_tx_data_port *port; 359 int i, j; 360 361 if (atomic_read(&card->rx_cmd_urb_pending) && card->rx_cmd.urb) 362 usb_kill_urb(card->rx_cmd.urb); 363 364 usb_free_urb(card->rx_cmd.urb); 365 card->rx_cmd.urb = NULL; 366 367 if (atomic_read(&card->rx_data_urb_pending)) 368 for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) 369 if (card->rx_data_list[i].urb) 370 usb_kill_urb(card->rx_data_list[i].urb); 371 372 for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) { 373 usb_free_urb(card->rx_data_list[i].urb); 374 card->rx_data_list[i].urb = NULL; 375 } 376 377 for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) { 378 port = &card->port[i]; 379 for (j = 0; j < MWIFIEX_TX_DATA_URB; j++) { 380 usb_kill_urb(port->tx_data_list[j].urb); 381 usb_free_urb(port->tx_data_list[j].urb); 382 port->tx_data_list[j].urb = NULL; 383 } 384 } 385 386 usb_free_urb(card->tx_cmd.urb); 387 card->tx_cmd.urb = NULL; 388 389 return; 390 } 391 392 /* This function probes an mwifiex device and registers it. It allocates 393 * the card structure, initiates the device registration and initialization 394 * procedure by adding a logical interface. 395 */ 396 static int mwifiex_usb_probe(struct usb_interface *intf, 397 const struct usb_device_id *id) 398 { 399 struct usb_device *udev = interface_to_usbdev(intf); 400 struct usb_host_interface *iface_desc = intf->cur_altsetting; 401 struct usb_endpoint_descriptor *epd; 402 int ret, i; 403 struct usb_card_rec *card; 404 u16 id_vendor, id_product, bcd_device; 405 406 card = devm_kzalloc(&intf->dev, sizeof(*card), GFP_KERNEL); 407 if (!card) 408 return -ENOMEM; 409 410 init_completion(&card->fw_done); 411 412 id_vendor = le16_to_cpu(udev->descriptor.idVendor); 413 id_product = le16_to_cpu(udev->descriptor.idProduct); 414 bcd_device = le16_to_cpu(udev->descriptor.bcdDevice); 415 pr_debug("info: VID/PID = %X/%X, Boot2 version = %X\n", 416 id_vendor, id_product, bcd_device); 417 418 /* PID_1 is used for firmware downloading only */ 419 switch (id_product) { 420 case USB8766_PID_1: 421 case USB8797_PID_1: 422 case USB8801_PID_1: 423 case USB8997_PID_1: 424 card->usb_boot_state = USB8XXX_FW_DNLD; 425 break; 426 case USB8766_PID_2: 427 case USB8797_PID_2: 428 case USB8801_PID_2: 429 case USB8997_PID_2: 430 card->usb_boot_state = USB8XXX_FW_READY; 431 break; 432 default: 433 pr_warn("unknown id_product %#x\n", id_product); 434 card->usb_boot_state = USB8XXX_FW_DNLD; 435 break; 436 } 437 438 card->udev = udev; 439 card->intf = intf; 440 441 pr_debug("info: bcdUSB=%#x Device Class=%#x SubClass=%#x Protocol=%#x\n", 442 le16_to_cpu(udev->descriptor.bcdUSB), 443 udev->descriptor.bDeviceClass, 444 udev->descriptor.bDeviceSubClass, 445 udev->descriptor.bDeviceProtocol); 446 447 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { 448 epd = &iface_desc->endpoint[i].desc; 449 if (usb_endpoint_dir_in(epd) && 450 usb_endpoint_num(epd) == MWIFIEX_USB_EP_CMD_EVENT && 451 (usb_endpoint_xfer_bulk(epd) || 452 usb_endpoint_xfer_int(epd))) { 453 card->rx_cmd_ep_type = usb_endpoint_type(epd); 454 card->rx_cmd_interval = epd->bInterval; 455 pr_debug("info: Rx CMD/EVT:: max pkt size: %d, addr: %d, ep_type: %d\n", 456 le16_to_cpu(epd->wMaxPacketSize), 457 epd->bEndpointAddress, card->rx_cmd_ep_type); 458 card->rx_cmd_ep = usb_endpoint_num(epd); 459 atomic_set(&card->rx_cmd_urb_pending, 0); 460 } 461 if (usb_endpoint_dir_in(epd) && 462 usb_endpoint_num(epd) == MWIFIEX_USB_EP_DATA && 463 usb_endpoint_xfer_bulk(epd)) { 464 pr_debug("info: bulk IN: max pkt size: %d, addr: %d\n", 465 le16_to_cpu(epd->wMaxPacketSize), 466 epd->bEndpointAddress); 467 card->rx_data_ep = usb_endpoint_num(epd); 468 atomic_set(&card->rx_data_urb_pending, 0); 469 } 470 if (usb_endpoint_dir_out(epd) && 471 usb_endpoint_num(epd) == MWIFIEX_USB_EP_DATA && 472 usb_endpoint_xfer_bulk(epd)) { 473 pr_debug("info: bulk OUT: max pkt size: %d, addr: %d\n", 474 le16_to_cpu(epd->wMaxPacketSize), 475 epd->bEndpointAddress); 476 card->port[0].tx_data_ep = usb_endpoint_num(epd); 477 atomic_set(&card->port[0].tx_data_urb_pending, 0); 478 } 479 if (usb_endpoint_dir_out(epd) && 480 usb_endpoint_num(epd) == MWIFIEX_USB_EP_DATA_CH2 && 481 usb_endpoint_xfer_bulk(epd)) { 482 pr_debug("info: bulk OUT chan2:\t" 483 "max pkt size: %d, addr: %d\n", 484 le16_to_cpu(epd->wMaxPacketSize), 485 epd->bEndpointAddress); 486 card->port[1].tx_data_ep = usb_endpoint_num(epd); 487 atomic_set(&card->port[1].tx_data_urb_pending, 0); 488 } 489 if (usb_endpoint_dir_out(epd) && 490 usb_endpoint_num(epd) == MWIFIEX_USB_EP_CMD_EVENT && 491 (usb_endpoint_xfer_bulk(epd) || 492 usb_endpoint_xfer_int(epd))) { 493 card->tx_cmd_ep_type = usb_endpoint_type(epd); 494 card->tx_cmd_interval = epd->bInterval; 495 pr_debug("info: bulk OUT: max pkt size: %d, addr: %d\n", 496 le16_to_cpu(epd->wMaxPacketSize), 497 epd->bEndpointAddress); 498 pr_debug("info: Tx CMD:: max pkt size: %d, addr: %d, ep_type: %d\n", 499 le16_to_cpu(epd->wMaxPacketSize), 500 epd->bEndpointAddress, card->tx_cmd_ep_type); 501 card->tx_cmd_ep = usb_endpoint_num(epd); 502 atomic_set(&card->tx_cmd_urb_pending, 0); 503 card->bulk_out_maxpktsize = 504 le16_to_cpu(epd->wMaxPacketSize); 505 } 506 } 507 508 usb_set_intfdata(intf, card); 509 510 ret = mwifiex_add_card(card, &card->fw_done, &usb_ops, 511 MWIFIEX_USB, &card->udev->dev); 512 if (ret) { 513 pr_err("%s: mwifiex_add_card failed: %d\n", __func__, ret); 514 usb_reset_device(udev); 515 return ret; 516 } 517 518 usb_get_dev(udev); 519 520 return 0; 521 } 522 523 /* Kernel needs to suspend all functions separately. Therefore all 524 * registered functions must have drivers with suspend and resume 525 * methods. Failing that the kernel simply removes the whole card. 526 * 527 * If already not suspended, this function allocates and sends a 528 * 'host sleep activate' request to the firmware and turns off the traffic. 529 */ 530 static int mwifiex_usb_suspend(struct usb_interface *intf, pm_message_t message) 531 { 532 struct usb_card_rec *card = usb_get_intfdata(intf); 533 struct mwifiex_adapter *adapter; 534 struct usb_tx_data_port *port; 535 int i, j; 536 537 /* Might still be loading firmware */ 538 wait_for_completion(&card->fw_done); 539 540 adapter = card->adapter; 541 if (!adapter) { 542 dev_err(&intf->dev, "card is not valid\n"); 543 return 0; 544 } 545 546 if (unlikely(test_bit(MWIFIEX_IS_SUSPENDED, &adapter->work_flags))) 547 mwifiex_dbg(adapter, WARN, 548 "Device already suspended\n"); 549 550 /* Enable the Host Sleep */ 551 if (!mwifiex_enable_hs(adapter)) { 552 mwifiex_dbg(adapter, ERROR, 553 "cmd: failed to suspend\n"); 554 clear_bit(MWIFIEX_IS_HS_ENABLING, &adapter->work_flags); 555 return -EFAULT; 556 } 557 558 559 /* 'MWIFIEX_IS_SUSPENDED' bit indicates device is suspended. 560 * It must be set here before the usb_kill_urb() calls. Reason 561 * is in the complete handlers, urb->status(= -ENOENT) and 562 * this flag is used in combination to distinguish between a 563 * 'suspended' state and a 'disconnect' one. 564 */ 565 set_bit(MWIFIEX_IS_SUSPENDED, &adapter->work_flags); 566 clear_bit(MWIFIEX_IS_HS_ENABLING, &adapter->work_flags); 567 568 if (atomic_read(&card->rx_cmd_urb_pending) && card->rx_cmd.urb) 569 usb_kill_urb(card->rx_cmd.urb); 570 571 if (atomic_read(&card->rx_data_urb_pending)) 572 for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) 573 if (card->rx_data_list[i].urb) 574 usb_kill_urb(card->rx_data_list[i].urb); 575 576 for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) { 577 port = &card->port[i]; 578 for (j = 0; j < MWIFIEX_TX_DATA_URB; j++) { 579 if (port->tx_data_list[j].urb) 580 usb_kill_urb(port->tx_data_list[j].urb); 581 } 582 } 583 584 if (card->tx_cmd.urb) 585 usb_kill_urb(card->tx_cmd.urb); 586 587 return 0; 588 } 589 590 /* Kernel needs to suspend all functions separately. Therefore all 591 * registered functions must have drivers with suspend and resume 592 * methods. Failing that the kernel simply removes the whole card. 593 * 594 * If already not resumed, this function turns on the traffic and 595 * sends a 'host sleep cancel' request to the firmware. 596 */ 597 static int mwifiex_usb_resume(struct usb_interface *intf) 598 { 599 struct usb_card_rec *card = usb_get_intfdata(intf); 600 struct mwifiex_adapter *adapter; 601 int i; 602 603 if (!card->adapter) { 604 dev_err(&intf->dev, "%s: card->adapter is NULL\n", 605 __func__); 606 return 0; 607 } 608 adapter = card->adapter; 609 610 if (unlikely(!test_bit(MWIFIEX_IS_SUSPENDED, &adapter->work_flags))) { 611 mwifiex_dbg(adapter, WARN, 612 "Device already resumed\n"); 613 return 0; 614 } 615 616 /* Indicate device resumed. The netdev queue will be resumed only 617 * after the urbs have been re-submitted 618 */ 619 clear_bit(MWIFIEX_IS_SUSPENDED, &adapter->work_flags); 620 621 if (!atomic_read(&card->rx_data_urb_pending)) 622 for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) 623 mwifiex_usb_submit_rx_urb(&card->rx_data_list[i], 624 MWIFIEX_RX_DATA_BUF_SIZE); 625 626 if (!atomic_read(&card->rx_cmd_urb_pending)) { 627 card->rx_cmd.skb = dev_alloc_skb(MWIFIEX_RX_CMD_BUF_SIZE); 628 if (card->rx_cmd.skb) 629 mwifiex_usb_submit_rx_urb(&card->rx_cmd, 630 MWIFIEX_RX_CMD_BUF_SIZE); 631 } 632 633 /* Disable Host Sleep */ 634 if (adapter->hs_activated) 635 mwifiex_cancel_hs(mwifiex_get_priv(adapter, 636 MWIFIEX_BSS_ROLE_ANY), 637 MWIFIEX_ASYNC_CMD); 638 639 return 0; 640 } 641 642 static void mwifiex_usb_disconnect(struct usb_interface *intf) 643 { 644 struct usb_card_rec *card = usb_get_intfdata(intf); 645 struct mwifiex_adapter *adapter; 646 647 wait_for_completion(&card->fw_done); 648 649 adapter = card->adapter; 650 if (!adapter || !adapter->priv_num) 651 return; 652 653 if (card->udev->state != USB_STATE_NOTATTACHED && !adapter->mfg_mode) { 654 mwifiex_deauthenticate_all(adapter); 655 656 mwifiex_init_shutdown_fw(mwifiex_get_priv(adapter, 657 MWIFIEX_BSS_ROLE_ANY), 658 MWIFIEX_FUNC_SHUTDOWN); 659 } 660 661 mwifiex_dbg(adapter, FATAL, 662 "%s: removing card\n", __func__); 663 mwifiex_remove_card(adapter); 664 665 usb_put_dev(interface_to_usbdev(intf)); 666 } 667 668 static void mwifiex_usb_coredump(struct device *dev) 669 { 670 struct usb_interface *intf = to_usb_interface(dev); 671 struct usb_card_rec *card = usb_get_intfdata(intf); 672 673 mwifiex_fw_dump_event(mwifiex_get_priv(card->adapter, 674 MWIFIEX_BSS_ROLE_ANY)); 675 } 676 677 static struct usb_driver mwifiex_usb_driver = { 678 .name = "mwifiex_usb", 679 .probe = mwifiex_usb_probe, 680 .disconnect = mwifiex_usb_disconnect, 681 .id_table = mwifiex_usb_table, 682 .suspend = mwifiex_usb_suspend, 683 .resume = mwifiex_usb_resume, 684 .soft_unbind = 1, 685 .drvwrap.driver = { 686 .coredump = mwifiex_usb_coredump, 687 }, 688 }; 689 690 static int mwifiex_write_data_sync(struct mwifiex_adapter *adapter, u8 *pbuf, 691 u32 *len, u8 ep, u32 timeout) 692 { 693 struct usb_card_rec *card = adapter->card; 694 int actual_length, ret; 695 696 if (!(*len % card->bulk_out_maxpktsize)) 697 (*len)++; 698 699 /* Send the data block */ 700 ret = usb_bulk_msg(card->udev, usb_sndbulkpipe(card->udev, ep), pbuf, 701 *len, &actual_length, timeout); 702 if (ret) { 703 mwifiex_dbg(adapter, ERROR, 704 "usb_bulk_msg for tx failed: %d\n", ret); 705 return ret; 706 } 707 708 *len = actual_length; 709 710 return ret; 711 } 712 713 static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *pbuf, 714 u32 *len, u8 ep, u32 timeout) 715 { 716 struct usb_card_rec *card = adapter->card; 717 int actual_length, ret; 718 719 /* Receive the data response */ 720 ret = usb_bulk_msg(card->udev, usb_rcvbulkpipe(card->udev, ep), pbuf, 721 *len, &actual_length, timeout); 722 if (ret) { 723 mwifiex_dbg(adapter, ERROR, 724 "usb_bulk_msg for rx failed: %d\n", ret); 725 return ret; 726 } 727 728 *len = actual_length; 729 730 return ret; 731 } 732 733 static void mwifiex_usb_port_resync(struct mwifiex_adapter *adapter) 734 { 735 struct usb_card_rec *card = adapter->card; 736 u8 active_port = MWIFIEX_USB_EP_DATA; 737 struct mwifiex_private *priv = NULL; 738 int i; 739 740 if (adapter->usb_mc_status) { 741 for (i = 0; i < adapter->priv_num; i++) { 742 priv = adapter->priv[i]; 743 if (!priv) 744 continue; 745 if ((priv->bss_role == MWIFIEX_BSS_ROLE_UAP && 746 !priv->bss_started) || 747 (priv->bss_role == MWIFIEX_BSS_ROLE_STA && 748 !priv->media_connected)) 749 priv->usb_port = MWIFIEX_USB_EP_DATA; 750 } 751 for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) 752 card->port[i].block_status = false; 753 } else { 754 for (i = 0; i < adapter->priv_num; i++) { 755 priv = adapter->priv[i]; 756 if (!priv) 757 continue; 758 if ((priv->bss_role == MWIFIEX_BSS_ROLE_UAP && 759 priv->bss_started) || 760 (priv->bss_role == MWIFIEX_BSS_ROLE_STA && 761 priv->media_connected)) { 762 active_port = priv->usb_port; 763 break; 764 } 765 } 766 for (i = 0; i < adapter->priv_num; i++) { 767 priv = adapter->priv[i]; 768 if (priv) 769 priv->usb_port = active_port; 770 } 771 for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) { 772 if (active_port == card->port[i].tx_data_ep) 773 card->port[i].block_status = false; 774 else 775 card->port[i].block_status = true; 776 } 777 } 778 } 779 780 static bool mwifiex_usb_is_port_ready(struct mwifiex_private *priv) 781 { 782 struct usb_card_rec *card = priv->adapter->card; 783 int idx; 784 785 for (idx = 0; idx < MWIFIEX_TX_DATA_PORT; idx++) { 786 if (priv->usb_port == card->port[idx].tx_data_ep) 787 return !card->port[idx].block_status; 788 } 789 790 return false; 791 } 792 793 static inline u8 mwifiex_usb_data_sent(struct mwifiex_adapter *adapter) 794 { 795 struct usb_card_rec *card = adapter->card; 796 int i; 797 798 for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) 799 if (!card->port[i].block_status) 800 return false; 801 802 return true; 803 } 804 805 static int mwifiex_usb_construct_send_urb(struct mwifiex_adapter *adapter, 806 struct usb_tx_data_port *port, u8 ep, 807 struct urb_context *context, 808 struct sk_buff *skb_send) 809 { 810 struct usb_card_rec *card = adapter->card; 811 int ret = -EINPROGRESS; 812 struct urb *tx_urb; 813 814 context->adapter = adapter; 815 context->ep = ep; 816 context->skb = skb_send; 817 tx_urb = context->urb; 818 819 if (ep == card->tx_cmd_ep && 820 card->tx_cmd_ep_type == USB_ENDPOINT_XFER_INT) 821 usb_fill_int_urb(tx_urb, card->udev, 822 usb_sndintpipe(card->udev, ep), skb_send->data, 823 skb_send->len, mwifiex_usb_tx_complete, 824 (void *)context, card->tx_cmd_interval); 825 else 826 usb_fill_bulk_urb(tx_urb, card->udev, 827 usb_sndbulkpipe(card->udev, ep), 828 skb_send->data, skb_send->len, 829 mwifiex_usb_tx_complete, (void *)context); 830 831 tx_urb->transfer_flags |= URB_ZERO_PACKET; 832 833 if (ep == card->tx_cmd_ep) 834 atomic_inc(&card->tx_cmd_urb_pending); 835 else 836 atomic_inc(&port->tx_data_urb_pending); 837 838 if (ep != card->tx_cmd_ep && 839 atomic_read(&port->tx_data_urb_pending) == 840 MWIFIEX_TX_DATA_URB) { 841 port->block_status = true; 842 adapter->data_sent = mwifiex_usb_data_sent(adapter); 843 ret = -ENOSR; 844 } 845 846 if (usb_submit_urb(tx_urb, GFP_ATOMIC)) { 847 mwifiex_dbg(adapter, ERROR, 848 "%s: usb_submit_urb failed\n", __func__); 849 if (ep == card->tx_cmd_ep) { 850 atomic_dec(&card->tx_cmd_urb_pending); 851 } else { 852 atomic_dec(&port->tx_data_urb_pending); 853 port->block_status = false; 854 adapter->data_sent = false; 855 if (port->tx_data_ix) 856 port->tx_data_ix--; 857 else 858 port->tx_data_ix = MWIFIEX_TX_DATA_URB; 859 } 860 ret = -1; 861 } 862 863 return ret; 864 } 865 866 static int mwifiex_usb_prepare_tx_aggr_skb(struct mwifiex_adapter *adapter, 867 struct usb_tx_data_port *port, 868 struct sk_buff **skb_send) 869 { 870 struct sk_buff *skb_aggr, *skb_tmp; 871 u8 *payload, pad; 872 u16 align = adapter->bus_aggr.tx_aggr_align; 873 struct mwifiex_txinfo *tx_info = NULL; 874 bool is_txinfo_set = false; 875 876 /* Packets in aggr_list will be send in either skb_aggr or 877 * write complete, delete the tx_aggr timer 878 */ 879 if (port->tx_aggr.timer_cnxt.is_hold_timer_set) { 880 del_timer(&port->tx_aggr.timer_cnxt.hold_timer); 881 port->tx_aggr.timer_cnxt.is_hold_timer_set = false; 882 port->tx_aggr.timer_cnxt.hold_tmo_msecs = 0; 883 } 884 885 skb_aggr = mwifiex_alloc_dma_align_buf(port->tx_aggr.aggr_len, 886 GFP_ATOMIC); 887 if (!skb_aggr) { 888 mwifiex_dbg(adapter, ERROR, 889 "%s: alloc skb_aggr failed\n", __func__); 890 891 while ((skb_tmp = skb_dequeue(&port->tx_aggr.aggr_list))) 892 mwifiex_write_data_complete(adapter, skb_tmp, 0, -1); 893 894 port->tx_aggr.aggr_num = 0; 895 port->tx_aggr.aggr_len = 0; 896 return -EBUSY; 897 } 898 899 tx_info = MWIFIEX_SKB_TXCB(skb_aggr); 900 memset(tx_info, 0, sizeof(*tx_info)); 901 902 while ((skb_tmp = skb_dequeue(&port->tx_aggr.aggr_list))) { 903 /* padding for aligning next packet header*/ 904 pad = (align - (skb_tmp->len & (align - 1))) % align; 905 payload = skb_put(skb_aggr, skb_tmp->len + pad); 906 memcpy(payload, skb_tmp->data, skb_tmp->len); 907 if (skb_queue_empty(&port->tx_aggr.aggr_list)) { 908 /* do not padding for last packet*/ 909 *(u16 *)payload = cpu_to_le16(skb_tmp->len); 910 *(u16 *)&payload[2] = 911 cpu_to_le16(MWIFIEX_TYPE_AGGR_DATA_V2 | 0x80); 912 skb_trim(skb_aggr, skb_aggr->len - pad); 913 } else { 914 /* add aggregation interface header */ 915 *(u16 *)payload = cpu_to_le16(skb_tmp->len + pad); 916 *(u16 *)&payload[2] = 917 cpu_to_le16(MWIFIEX_TYPE_AGGR_DATA_V2); 918 } 919 920 if (!is_txinfo_set) { 921 tx_info->bss_num = MWIFIEX_SKB_TXCB(skb_tmp)->bss_num; 922 tx_info->bss_type = MWIFIEX_SKB_TXCB(skb_tmp)->bss_type; 923 is_txinfo_set = true; 924 } 925 926 port->tx_aggr.aggr_num--; 927 port->tx_aggr.aggr_len -= (skb_tmp->len + pad); 928 mwifiex_write_data_complete(adapter, skb_tmp, 0, 0); 929 } 930 931 tx_info->pkt_len = skb_aggr->len - 932 (sizeof(struct txpd) + adapter->intf_hdr_len); 933 tx_info->flags |= MWIFIEX_BUF_FLAG_AGGR_PKT; 934 935 port->tx_aggr.aggr_num = 0; 936 port->tx_aggr.aggr_len = 0; 937 *skb_send = skb_aggr; 938 939 return 0; 940 } 941 942 /* This function prepare data packet to be send under usb tx aggregation 943 * protocol, check current usb aggregation status, link packet to aggrgation 944 * list if possible, work flow as below: 945 * (1) if only 1 packet available, add usb tx aggregation header and send. 946 * (2) if packet is able to aggregated, link it to current aggregation list. 947 * (3) if packet is not able to aggregated, aggregate and send exist packets 948 * in aggrgation list. Then, link packet in the list if there is more 949 * packet in transmit queue, otherwise try to transmit single packet. 950 */ 951 static int mwifiex_usb_aggr_tx_data(struct mwifiex_adapter *adapter, u8 ep, 952 struct sk_buff *skb, 953 struct mwifiex_tx_param *tx_param, 954 struct usb_tx_data_port *port) 955 { 956 u8 *payload, pad; 957 u16 align = adapter->bus_aggr.tx_aggr_align; 958 struct sk_buff *skb_send = NULL; 959 struct urb_context *context = NULL; 960 struct txpd *local_tx_pd = 961 (struct txpd *)((u8 *)skb->data + adapter->intf_hdr_len); 962 u8 f_send_aggr_buf = 0; 963 u8 f_send_cur_buf = 0; 964 u8 f_precopy_cur_buf = 0; 965 u8 f_postcopy_cur_buf = 0; 966 u32 timeout; 967 int ret; 968 969 /* padding to ensure each packet alginment */ 970 pad = (align - (skb->len & (align - 1))) % align; 971 972 if (tx_param && tx_param->next_pkt_len) { 973 /* next packet available in tx queue*/ 974 if (port->tx_aggr.aggr_len + skb->len + pad > 975 adapter->bus_aggr.tx_aggr_max_size) { 976 f_send_aggr_buf = 1; 977 f_postcopy_cur_buf = 1; 978 } else { 979 /* current packet could be aggregated*/ 980 f_precopy_cur_buf = 1; 981 982 if (port->tx_aggr.aggr_len + skb->len + pad + 983 tx_param->next_pkt_len > 984 adapter->bus_aggr.tx_aggr_max_size || 985 port->tx_aggr.aggr_num + 2 > 986 adapter->bus_aggr.tx_aggr_max_num) { 987 /* next packet could not be aggregated 988 * send current aggregation buffer 989 */ 990 f_send_aggr_buf = 1; 991 } 992 } 993 } else { 994 /* last packet in tx queue */ 995 if (port->tx_aggr.aggr_num > 0) { 996 /* pending packets in aggregation buffer*/ 997 if (port->tx_aggr.aggr_len + skb->len + pad > 998 adapter->bus_aggr.tx_aggr_max_size) { 999 /* current packet not be able to aggregated, 1000 * send aggr buffer first, then send packet. 1001 */ 1002 f_send_cur_buf = 1; 1003 } else { 1004 /* last packet, Aggregation and send */ 1005 f_precopy_cur_buf = 1; 1006 } 1007 1008 f_send_aggr_buf = 1; 1009 } else { 1010 /* no pending packets in aggregation buffer, 1011 * send current packet immediately 1012 */ 1013 f_send_cur_buf = 1; 1014 } 1015 } 1016 1017 if (local_tx_pd->flags & MWIFIEX_TxPD_POWER_MGMT_NULL_PACKET) { 1018 /* Send NULL packet immediately*/ 1019 if (f_precopy_cur_buf) { 1020 if (skb_queue_empty(&port->tx_aggr.aggr_list)) { 1021 f_precopy_cur_buf = 0; 1022 f_send_aggr_buf = 0; 1023 f_send_cur_buf = 1; 1024 } else { 1025 f_send_aggr_buf = 1; 1026 } 1027 } else if (f_postcopy_cur_buf) { 1028 f_send_cur_buf = 1; 1029 f_postcopy_cur_buf = 0; 1030 } 1031 } 1032 1033 if (f_precopy_cur_buf) { 1034 skb_queue_tail(&port->tx_aggr.aggr_list, skb); 1035 port->tx_aggr.aggr_len += (skb->len + pad); 1036 port->tx_aggr.aggr_num++; 1037 if (f_send_aggr_buf) 1038 goto send_aggr_buf; 1039 1040 /* packet will not been send immediately, 1041 * set a timer to make sure it will be sent under 1042 * strict time limit. Dynamically fit the timeout 1043 * value, according to packets number in aggr_list 1044 */ 1045 if (!port->tx_aggr.timer_cnxt.is_hold_timer_set) { 1046 port->tx_aggr.timer_cnxt.hold_tmo_msecs = 1047 MWIFIEX_USB_TX_AGGR_TMO_MIN; 1048 timeout = 1049 port->tx_aggr.timer_cnxt.hold_tmo_msecs; 1050 mod_timer(&port->tx_aggr.timer_cnxt.hold_timer, 1051 jiffies + msecs_to_jiffies(timeout)); 1052 port->tx_aggr.timer_cnxt.is_hold_timer_set = true; 1053 } else { 1054 if (port->tx_aggr.timer_cnxt.hold_tmo_msecs < 1055 MWIFIEX_USB_TX_AGGR_TMO_MAX) { 1056 /* Dyanmic fit timeout */ 1057 timeout = 1058 ++port->tx_aggr.timer_cnxt.hold_tmo_msecs; 1059 mod_timer(&port->tx_aggr.timer_cnxt.hold_timer, 1060 jiffies + msecs_to_jiffies(timeout)); 1061 } 1062 } 1063 } 1064 1065 send_aggr_buf: 1066 if (f_send_aggr_buf) { 1067 ret = mwifiex_usb_prepare_tx_aggr_skb(adapter, port, &skb_send); 1068 if (!ret) { 1069 context = &port->tx_data_list[port->tx_data_ix++]; 1070 ret = mwifiex_usb_construct_send_urb(adapter, port, ep, 1071 context, skb_send); 1072 if (ret == -1) 1073 mwifiex_write_data_complete(adapter, skb_send, 1074 0, -1); 1075 } 1076 } 1077 1078 if (f_send_cur_buf) { 1079 if (f_send_aggr_buf) { 1080 if (atomic_read(&port->tx_data_urb_pending) >= 1081 MWIFIEX_TX_DATA_URB) { 1082 port->block_status = true; 1083 adapter->data_sent = 1084 mwifiex_usb_data_sent(adapter); 1085 /* no available urb, postcopy packet*/ 1086 f_postcopy_cur_buf = 1; 1087 goto postcopy_cur_buf; 1088 } 1089 1090 if (port->tx_data_ix >= MWIFIEX_TX_DATA_URB) 1091 port->tx_data_ix = 0; 1092 } 1093 1094 payload = skb->data; 1095 *(u16 *)&payload[2] = 1096 cpu_to_le16(MWIFIEX_TYPE_AGGR_DATA_V2 | 0x80); 1097 *(u16 *)payload = cpu_to_le16(skb->len); 1098 skb_send = skb; 1099 context = &port->tx_data_list[port->tx_data_ix++]; 1100 return mwifiex_usb_construct_send_urb(adapter, port, ep, 1101 context, skb_send); 1102 } 1103 1104 postcopy_cur_buf: 1105 if (f_postcopy_cur_buf) { 1106 skb_queue_tail(&port->tx_aggr.aggr_list, skb); 1107 port->tx_aggr.aggr_len += (skb->len + pad); 1108 port->tx_aggr.aggr_num++; 1109 /* New aggregation begin, start timer */ 1110 if (!port->tx_aggr.timer_cnxt.is_hold_timer_set) { 1111 port->tx_aggr.timer_cnxt.hold_tmo_msecs = 1112 MWIFIEX_USB_TX_AGGR_TMO_MIN; 1113 timeout = port->tx_aggr.timer_cnxt.hold_tmo_msecs; 1114 mod_timer(&port->tx_aggr.timer_cnxt.hold_timer, 1115 jiffies + msecs_to_jiffies(timeout)); 1116 port->tx_aggr.timer_cnxt.is_hold_timer_set = true; 1117 } 1118 } 1119 1120 return -EINPROGRESS; 1121 } 1122 1123 static void mwifiex_usb_tx_aggr_tmo(struct timer_list *t) 1124 { 1125 struct urb_context *urb_cnxt = NULL; 1126 struct sk_buff *skb_send = NULL; 1127 struct tx_aggr_tmr_cnxt *timer_context = 1128 from_timer(timer_context, t, hold_timer); 1129 struct mwifiex_adapter *adapter = timer_context->adapter; 1130 struct usb_tx_data_port *port = timer_context->port; 1131 unsigned long flags; 1132 int err = 0; 1133 1134 spin_lock_irqsave(&port->tx_aggr_lock, flags); 1135 err = mwifiex_usb_prepare_tx_aggr_skb(adapter, port, &skb_send); 1136 if (err) { 1137 mwifiex_dbg(adapter, ERROR, 1138 "prepare tx aggr skb failed, err=%d\n", err); 1139 goto unlock; 1140 } 1141 1142 if (atomic_read(&port->tx_data_urb_pending) >= 1143 MWIFIEX_TX_DATA_URB) { 1144 port->block_status = true; 1145 adapter->data_sent = 1146 mwifiex_usb_data_sent(adapter); 1147 err = -1; 1148 goto done; 1149 } 1150 1151 if (port->tx_data_ix >= MWIFIEX_TX_DATA_URB) 1152 port->tx_data_ix = 0; 1153 1154 urb_cnxt = &port->tx_data_list[port->tx_data_ix++]; 1155 err = mwifiex_usb_construct_send_urb(adapter, port, port->tx_data_ep, 1156 urb_cnxt, skb_send); 1157 done: 1158 if (err == -1) 1159 mwifiex_write_data_complete(adapter, skb_send, 0, -1); 1160 unlock: 1161 spin_unlock_irqrestore(&port->tx_aggr_lock, flags); 1162 } 1163 1164 /* This function write a command/data packet to card. */ 1165 static int mwifiex_usb_host_to_card(struct mwifiex_adapter *adapter, u8 ep, 1166 struct sk_buff *skb, 1167 struct mwifiex_tx_param *tx_param) 1168 { 1169 struct usb_card_rec *card = adapter->card; 1170 struct urb_context *context = NULL; 1171 struct usb_tx_data_port *port = NULL; 1172 unsigned long flags; 1173 int idx, ret; 1174 1175 if (test_bit(MWIFIEX_IS_SUSPENDED, &adapter->work_flags)) { 1176 mwifiex_dbg(adapter, ERROR, 1177 "%s: not allowed while suspended\n", __func__); 1178 return -1; 1179 } 1180 1181 if (test_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags)) { 1182 mwifiex_dbg(adapter, ERROR, "%s: device removed\n", __func__); 1183 return -1; 1184 } 1185 1186 mwifiex_dbg(adapter, INFO, "%s: ep=%d\n", __func__, ep); 1187 1188 if (ep == card->tx_cmd_ep) { 1189 context = &card->tx_cmd; 1190 } else { 1191 /* get the data port structure for endpoint */ 1192 for (idx = 0; idx < MWIFIEX_TX_DATA_PORT; idx++) { 1193 if (ep == card->port[idx].tx_data_ep) { 1194 port = &card->port[idx]; 1195 if (atomic_read(&port->tx_data_urb_pending) 1196 >= MWIFIEX_TX_DATA_URB) { 1197 port->block_status = true; 1198 adapter->data_sent = 1199 mwifiex_usb_data_sent(adapter); 1200 return -EBUSY; 1201 } 1202 if (port->tx_data_ix >= MWIFIEX_TX_DATA_URB) 1203 port->tx_data_ix = 0; 1204 break; 1205 } 1206 } 1207 1208 if (!port) { 1209 mwifiex_dbg(adapter, ERROR, "Wrong usb tx data port\n"); 1210 return -1; 1211 } 1212 1213 if (adapter->bus_aggr.enable) { 1214 spin_lock_irqsave(&port->tx_aggr_lock, flags); 1215 ret = mwifiex_usb_aggr_tx_data(adapter, ep, skb, 1216 tx_param, port); 1217 spin_unlock_irqrestore(&port->tx_aggr_lock, flags); 1218 return ret; 1219 } 1220 1221 context = &port->tx_data_list[port->tx_data_ix++]; 1222 } 1223 1224 return mwifiex_usb_construct_send_urb(adapter, port, ep, context, skb); 1225 } 1226 1227 static int mwifiex_usb_tx_init(struct mwifiex_adapter *adapter) 1228 { 1229 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card; 1230 struct usb_tx_data_port *port; 1231 int i, j; 1232 1233 card->tx_cmd.adapter = adapter; 1234 card->tx_cmd.ep = card->tx_cmd_ep; 1235 1236 card->tx_cmd.urb = usb_alloc_urb(0, GFP_KERNEL); 1237 if (!card->tx_cmd.urb) 1238 return -ENOMEM; 1239 1240 for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) { 1241 port = &card->port[i]; 1242 if (!port->tx_data_ep) 1243 continue; 1244 port->tx_data_ix = 0; 1245 skb_queue_head_init(&port->tx_aggr.aggr_list); 1246 if (port->tx_data_ep == MWIFIEX_USB_EP_DATA) 1247 port->block_status = false; 1248 else 1249 port->block_status = true; 1250 for (j = 0; j < MWIFIEX_TX_DATA_URB; j++) { 1251 port->tx_data_list[j].adapter = adapter; 1252 port->tx_data_list[j].ep = port->tx_data_ep; 1253 port->tx_data_list[j].urb = 1254 usb_alloc_urb(0, GFP_KERNEL); 1255 if (!port->tx_data_list[j].urb) 1256 return -ENOMEM; 1257 } 1258 1259 port->tx_aggr.timer_cnxt.adapter = adapter; 1260 port->tx_aggr.timer_cnxt.port = port; 1261 port->tx_aggr.timer_cnxt.is_hold_timer_set = false; 1262 port->tx_aggr.timer_cnxt.hold_tmo_msecs = 0; 1263 timer_setup(&port->tx_aggr.timer_cnxt.hold_timer, 1264 mwifiex_usb_tx_aggr_tmo, 0); 1265 } 1266 1267 return 0; 1268 } 1269 1270 static int mwifiex_usb_rx_init(struct mwifiex_adapter *adapter) 1271 { 1272 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card; 1273 int i; 1274 1275 card->rx_cmd.adapter = adapter; 1276 card->rx_cmd.ep = card->rx_cmd_ep; 1277 1278 card->rx_cmd.urb = usb_alloc_urb(0, GFP_KERNEL); 1279 if (!card->rx_cmd.urb) 1280 return -ENOMEM; 1281 1282 card->rx_cmd.skb = dev_alloc_skb(MWIFIEX_RX_CMD_BUF_SIZE); 1283 if (!card->rx_cmd.skb) 1284 return -ENOMEM; 1285 1286 if (mwifiex_usb_submit_rx_urb(&card->rx_cmd, MWIFIEX_RX_CMD_BUF_SIZE)) 1287 return -1; 1288 1289 for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) { 1290 card->rx_data_list[i].adapter = adapter; 1291 card->rx_data_list[i].ep = card->rx_data_ep; 1292 1293 card->rx_data_list[i].urb = usb_alloc_urb(0, GFP_KERNEL); 1294 if (!card->rx_data_list[i].urb) 1295 return -1; 1296 if (mwifiex_usb_submit_rx_urb(&card->rx_data_list[i], 1297 MWIFIEX_RX_DATA_BUF_SIZE)) 1298 return -1; 1299 } 1300 1301 return 0; 1302 } 1303 1304 /* This function register usb device and initialize parameter. */ 1305 static int mwifiex_register_dev(struct mwifiex_adapter *adapter) 1306 { 1307 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card; 1308 1309 card->adapter = adapter; 1310 1311 switch (le16_to_cpu(card->udev->descriptor.idProduct)) { 1312 case USB8997_PID_1: 1313 case USB8997_PID_2: 1314 adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_4K; 1315 strcpy(adapter->fw_name, USB8997_DEFAULT_FW_NAME); 1316 adapter->ext_scan = true; 1317 break; 1318 case USB8766_PID_1: 1319 case USB8766_PID_2: 1320 adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K; 1321 strcpy(adapter->fw_name, USB8766_DEFAULT_FW_NAME); 1322 adapter->ext_scan = true; 1323 break; 1324 case USB8801_PID_1: 1325 case USB8801_PID_2: 1326 adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K; 1327 strcpy(adapter->fw_name, USB8801_DEFAULT_FW_NAME); 1328 adapter->ext_scan = false; 1329 break; 1330 case USB8797_PID_1: 1331 case USB8797_PID_2: 1332 default: 1333 adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K; 1334 strcpy(adapter->fw_name, USB8797_DEFAULT_FW_NAME); 1335 break; 1336 } 1337 1338 adapter->usb_mc_status = false; 1339 adapter->usb_mc_setup = false; 1340 1341 return 0; 1342 } 1343 1344 static void mwifiex_usb_cleanup_tx_aggr(struct mwifiex_adapter *adapter) 1345 { 1346 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card; 1347 struct usb_tx_data_port *port; 1348 struct sk_buff *skb_tmp; 1349 int idx; 1350 1351 for (idx = 0; idx < MWIFIEX_TX_DATA_PORT; idx++) { 1352 port = &card->port[idx]; 1353 if (adapter->bus_aggr.enable) 1354 while ((skb_tmp = 1355 skb_dequeue(&port->tx_aggr.aggr_list))) 1356 mwifiex_write_data_complete(adapter, skb_tmp, 1357 0, -1); 1358 del_timer_sync(&port->tx_aggr.timer_cnxt.hold_timer); 1359 port->tx_aggr.timer_cnxt.is_hold_timer_set = false; 1360 port->tx_aggr.timer_cnxt.hold_tmo_msecs = 0; 1361 } 1362 } 1363 1364 static void mwifiex_unregister_dev(struct mwifiex_adapter *adapter) 1365 { 1366 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card; 1367 1368 mwifiex_usb_free(card); 1369 1370 mwifiex_usb_cleanup_tx_aggr(adapter); 1371 1372 card->adapter = NULL; 1373 } 1374 1375 static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter, 1376 struct mwifiex_fw_image *fw) 1377 { 1378 int ret = 0; 1379 u8 *firmware = fw->fw_buf, *recv_buff; 1380 u32 retries = USB8XXX_FW_MAX_RETRY + 1; 1381 u32 dlen; 1382 u32 fw_seqnum = 0, tlen = 0, dnld_cmd = 0; 1383 struct fw_data *fwdata; 1384 struct fw_sync_header sync_fw; 1385 u8 check_winner = 1; 1386 1387 if (!firmware) { 1388 mwifiex_dbg(adapter, ERROR, 1389 "No firmware image found! Terminating download\n"); 1390 ret = -1; 1391 goto fw_exit; 1392 } 1393 1394 /* Allocate memory for transmit */ 1395 fwdata = kzalloc(FW_DNLD_TX_BUF_SIZE, GFP_KERNEL); 1396 if (!fwdata) { 1397 ret = -ENOMEM; 1398 goto fw_exit; 1399 } 1400 1401 /* Allocate memory for receive */ 1402 recv_buff = kzalloc(FW_DNLD_RX_BUF_SIZE, GFP_KERNEL); 1403 if (!recv_buff) { 1404 ret = -ENOMEM; 1405 goto cleanup; 1406 } 1407 1408 do { 1409 /* Send pseudo data to check winner status first */ 1410 if (check_winner) { 1411 memset(&fwdata->fw_hdr, 0, sizeof(struct fw_header)); 1412 dlen = 0; 1413 } else { 1414 /* copy the header of the fw_data to get the length */ 1415 memcpy(&fwdata->fw_hdr, &firmware[tlen], 1416 sizeof(struct fw_header)); 1417 1418 dlen = le32_to_cpu(fwdata->fw_hdr.data_len); 1419 dnld_cmd = le32_to_cpu(fwdata->fw_hdr.dnld_cmd); 1420 tlen += sizeof(struct fw_header); 1421 1422 /* Command 7 doesn't have data length field */ 1423 if (dnld_cmd == FW_CMD_7) 1424 dlen = 0; 1425 1426 memcpy(fwdata->data, &firmware[tlen], dlen); 1427 1428 fwdata->seq_num = cpu_to_le32(fw_seqnum); 1429 tlen += dlen; 1430 } 1431 1432 /* If the send/receive fails or CRC occurs then retry */ 1433 while (--retries) { 1434 u8 *buf = (u8 *)fwdata; 1435 u32 len = FW_DATA_XMIT_SIZE; 1436 1437 /* send the firmware block */ 1438 ret = mwifiex_write_data_sync(adapter, buf, &len, 1439 MWIFIEX_USB_EP_CMD_EVENT, 1440 MWIFIEX_USB_TIMEOUT); 1441 if (ret) { 1442 mwifiex_dbg(adapter, ERROR, 1443 "write_data_sync: failed: %d\n", 1444 ret); 1445 continue; 1446 } 1447 1448 buf = recv_buff; 1449 len = FW_DNLD_RX_BUF_SIZE; 1450 1451 /* Receive the firmware block response */ 1452 ret = mwifiex_read_data_sync(adapter, buf, &len, 1453 MWIFIEX_USB_EP_CMD_EVENT, 1454 MWIFIEX_USB_TIMEOUT); 1455 if (ret) { 1456 mwifiex_dbg(adapter, ERROR, 1457 "read_data_sync: failed: %d\n", 1458 ret); 1459 continue; 1460 } 1461 1462 memcpy(&sync_fw, recv_buff, 1463 sizeof(struct fw_sync_header)); 1464 1465 /* check 1st firmware block resp for highest bit set */ 1466 if (check_winner) { 1467 if (le32_to_cpu(sync_fw.cmd) & 0x80000000) { 1468 mwifiex_dbg(adapter, WARN, 1469 "USB is not the winner %#x\n", 1470 sync_fw.cmd); 1471 1472 /* returning success */ 1473 ret = 0; 1474 goto cleanup; 1475 } 1476 1477 mwifiex_dbg(adapter, MSG, 1478 "start to download FW...\n"); 1479 1480 check_winner = 0; 1481 break; 1482 } 1483 1484 /* check the firmware block response for CRC errors */ 1485 if (sync_fw.cmd) { 1486 mwifiex_dbg(adapter, ERROR, 1487 "FW received block with CRC %#x\n", 1488 sync_fw.cmd); 1489 ret = -1; 1490 continue; 1491 } 1492 1493 retries = USB8XXX_FW_MAX_RETRY + 1; 1494 break; 1495 } 1496 fw_seqnum++; 1497 } while ((dnld_cmd != FW_HAS_LAST_BLOCK) && retries); 1498 1499 cleanup: 1500 mwifiex_dbg(adapter, MSG, 1501 "info: FW download over, size %d bytes\n", tlen); 1502 1503 kfree(recv_buff); 1504 kfree(fwdata); 1505 1506 if (retries) 1507 ret = 0; 1508 fw_exit: 1509 return ret; 1510 } 1511 1512 static int mwifiex_usb_dnld_fw(struct mwifiex_adapter *adapter, 1513 struct mwifiex_fw_image *fw) 1514 { 1515 int ret; 1516 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card; 1517 1518 if (card->usb_boot_state == USB8XXX_FW_DNLD) { 1519 ret = mwifiex_prog_fw_w_helper(adapter, fw); 1520 if (ret) 1521 return -1; 1522 1523 /* Boot state changes after successful firmware download */ 1524 if (card->usb_boot_state == USB8XXX_FW_DNLD) 1525 return -1; 1526 } 1527 1528 ret = mwifiex_usb_rx_init(adapter); 1529 if (!ret) 1530 ret = mwifiex_usb_tx_init(adapter); 1531 1532 return ret; 1533 } 1534 1535 static void mwifiex_submit_rx_urb(struct mwifiex_adapter *adapter, u8 ep) 1536 { 1537 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card; 1538 1539 skb_push(card->rx_cmd.skb, INTF_HEADER_LEN); 1540 if ((ep == card->rx_cmd_ep) && 1541 (!atomic_read(&card->rx_cmd_urb_pending))) 1542 mwifiex_usb_submit_rx_urb(&card->rx_cmd, 1543 MWIFIEX_RX_CMD_BUF_SIZE); 1544 1545 return; 1546 } 1547 1548 static int mwifiex_usb_cmd_event_complete(struct mwifiex_adapter *adapter, 1549 struct sk_buff *skb) 1550 { 1551 mwifiex_submit_rx_urb(adapter, MWIFIEX_USB_EP_CMD_EVENT); 1552 1553 return 0; 1554 } 1555 1556 /* This function wakes up the card. */ 1557 static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter) 1558 { 1559 /* Simulation of HS_AWAKE event */ 1560 adapter->pm_wakeup_fw_try = false; 1561 del_timer(&adapter->wakeup_timer); 1562 adapter->pm_wakeup_card_req = false; 1563 adapter->ps_state = PS_STATE_AWAKE; 1564 1565 return 0; 1566 } 1567 1568 static void mwifiex_usb_submit_rem_rx_urbs(struct mwifiex_adapter *adapter) 1569 { 1570 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card; 1571 int i; 1572 struct urb_context *ctx; 1573 1574 for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) { 1575 if (card->rx_data_list[i].skb) 1576 continue; 1577 ctx = &card->rx_data_list[i]; 1578 mwifiex_usb_submit_rx_urb(ctx, MWIFIEX_RX_DATA_BUF_SIZE); 1579 } 1580 } 1581 1582 /* This function is called after the card has woken up. */ 1583 static inline int 1584 mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter) 1585 { 1586 return 0; 1587 } 1588 1589 static struct mwifiex_if_ops usb_ops = { 1590 .register_dev = mwifiex_register_dev, 1591 .unregister_dev = mwifiex_unregister_dev, 1592 .wakeup = mwifiex_pm_wakeup_card, 1593 .wakeup_complete = mwifiex_pm_wakeup_card_complete, 1594 1595 /* USB specific */ 1596 .dnld_fw = mwifiex_usb_dnld_fw, 1597 .cmdrsp_complete = mwifiex_usb_cmd_event_complete, 1598 .event_complete = mwifiex_usb_cmd_event_complete, 1599 .host_to_card = mwifiex_usb_host_to_card, 1600 .submit_rem_rx_urbs = mwifiex_usb_submit_rem_rx_urbs, 1601 .multi_port_resync = mwifiex_usb_port_resync, 1602 .is_port_ready = mwifiex_usb_is_port_ready, 1603 }; 1604 1605 module_usb_driver(mwifiex_usb_driver); 1606 1607 MODULE_AUTHOR("Marvell International Ltd."); 1608 MODULE_DESCRIPTION("Marvell WiFi-Ex USB Driver version" USB_VERSION); 1609 MODULE_VERSION(USB_VERSION); 1610 MODULE_LICENSE("GPL v2"); 1611 MODULE_FIRMWARE(USB8766_DEFAULT_FW_NAME); 1612 MODULE_FIRMWARE(USB8797_DEFAULT_FW_NAME); 1613 MODULE_FIRMWARE(USB8801_DEFAULT_FW_NAME); 1614 MODULE_FIRMWARE(USB8997_DEFAULT_FW_NAME); 1615