|
4 | 4 | */
|
5 | 5 | #include <linux/module.h>
|
6 | 6 | #include <linux/firmware.h>
|
| 7 | +#include <linux/usb.h> |
7 | 8 |
|
8 | 9 | #include <net/bluetooth/bluetooth.h>
|
9 | 10 | #include <net/bluetooth/hci_core.h>
|
@@ -431,6 +432,270 @@ int btmtk_process_coredump(struct hci_dev *hdev, struct sk_buff *skb)
|
431 | 432 | }
|
432 | 433 | EXPORT_SYMBOL_GPL(btmtk_process_coredump);
|
433 | 434 |
|
| 435 | +static void btmtk_usb_wmt_recv(struct urb *urb) |
| 436 | +{ |
| 437 | + struct hci_dev *hdev = urb->context; |
| 438 | + struct btmtk_data *data = hci_get_priv(hdev); |
| 439 | + struct sk_buff *skb; |
| 440 | + int err; |
| 441 | + |
| 442 | + if (urb->status == 0 && urb->actual_length > 0) { |
| 443 | + hdev->stat.byte_rx += urb->actual_length; |
| 444 | + |
| 445 | + /* WMT event shouldn't be fragmented and the size should be |
| 446 | + * less than HCI_WMT_MAX_EVENT_SIZE. |
| 447 | + */ |
| 448 | + skb = bt_skb_alloc(HCI_WMT_MAX_EVENT_SIZE, GFP_ATOMIC); |
| 449 | + if (!skb) { |
| 450 | + hdev->stat.err_rx++; |
| 451 | + kfree(urb->setup_packet); |
| 452 | + return; |
| 453 | + } |
| 454 | + |
| 455 | + hci_skb_pkt_type(skb) = HCI_EVENT_PKT; |
| 456 | + skb_put_data(skb, urb->transfer_buffer, urb->actual_length); |
| 457 | + |
| 458 | + /* When someone waits for the WMT event, the skb is being cloned |
| 459 | + * and being processed the events from there then. |
| 460 | + */ |
| 461 | + if (test_bit(BTMTK_TX_WAIT_VND_EVT, &data->flags)) { |
| 462 | + data->evt_skb = skb_clone(skb, GFP_ATOMIC); |
| 463 | + if (!data->evt_skb) { |
| 464 | + kfree_skb(skb); |
| 465 | + kfree(urb->setup_packet); |
| 466 | + return; |
| 467 | + } |
| 468 | + } |
| 469 | + |
| 470 | + err = hci_recv_frame(hdev, skb); |
| 471 | + if (err < 0) { |
| 472 | + kfree_skb(data->evt_skb); |
| 473 | + data->evt_skb = NULL; |
| 474 | + kfree(urb->setup_packet); |
| 475 | + return; |
| 476 | + } |
| 477 | + |
| 478 | + if (test_and_clear_bit(BTMTK_TX_WAIT_VND_EVT, |
| 479 | + &data->flags)) { |
| 480 | + /* Barrier to sync with other CPUs */ |
| 481 | + smp_mb__after_atomic(); |
| 482 | + wake_up_bit(&data->flags, |
| 483 | + BTMTK_TX_WAIT_VND_EVT); |
| 484 | + } |
| 485 | + kfree(urb->setup_packet); |
| 486 | + return; |
| 487 | + } else if (urb->status == -ENOENT) { |
| 488 | + /* Avoid suspend failed when usb_kill_urb */ |
| 489 | + return; |
| 490 | + } |
| 491 | + |
| 492 | + usb_mark_last_busy(data->udev); |
| 493 | + |
| 494 | + /* The URB complete handler is still called with urb->actual_length = 0 |
| 495 | + * when the event is not available, so we should keep re-submitting |
| 496 | + * URB until WMT event returns, Also, It's necessary to wait some time |
| 497 | + * between the two consecutive control URBs to relax the target device |
| 498 | + * to generate the event. Otherwise, the WMT event cannot return from |
| 499 | + * the device successfully. |
| 500 | + */ |
| 501 | + udelay(500); |
| 502 | + |
| 503 | + usb_anchor_urb(urb, data->ctrl_anchor); |
| 504 | + err = usb_submit_urb(urb, GFP_ATOMIC); |
| 505 | + if (err < 0) { |
| 506 | + kfree(urb->setup_packet); |
| 507 | + /* -EPERM: urb is being killed; |
| 508 | + * -ENODEV: device got disconnected |
| 509 | + */ |
| 510 | + if (err != -EPERM && err != -ENODEV) |
| 511 | + bt_dev_err(hdev, "urb %p failed to resubmit (%d)", |
| 512 | + urb, -err); |
| 513 | + usb_unanchor_urb(urb); |
| 514 | + } |
| 515 | +} |
| 516 | + |
| 517 | +static int btmtk_usb_submit_wmt_recv_urb(struct hci_dev *hdev) |
| 518 | +{ |
| 519 | + struct btmtk_data *data = hci_get_priv(hdev); |
| 520 | + struct usb_ctrlrequest *dr; |
| 521 | + unsigned char *buf; |
| 522 | + int err, size = 64; |
| 523 | + unsigned int pipe; |
| 524 | + struct urb *urb; |
| 525 | + |
| 526 | + urb = usb_alloc_urb(0, GFP_KERNEL); |
| 527 | + if (!urb) |
| 528 | + return -ENOMEM; |
| 529 | + |
| 530 | + dr = kmalloc(sizeof(*dr), GFP_KERNEL); |
| 531 | + if (!dr) { |
| 532 | + usb_free_urb(urb); |
| 533 | + return -ENOMEM; |
| 534 | + } |
| 535 | + |
| 536 | + dr->bRequestType = USB_TYPE_VENDOR | USB_DIR_IN; |
| 537 | + dr->bRequest = 1; |
| 538 | + dr->wIndex = cpu_to_le16(0); |
| 539 | + dr->wValue = cpu_to_le16(48); |
| 540 | + dr->wLength = cpu_to_le16(size); |
| 541 | + |
| 542 | + buf = kmalloc(size, GFP_KERNEL); |
| 543 | + if (!buf) { |
| 544 | + kfree(dr); |
| 545 | + usb_free_urb(urb); |
| 546 | + return -ENOMEM; |
| 547 | + } |
| 548 | + |
| 549 | + pipe = usb_rcvctrlpipe(data->udev, 0); |
| 550 | + |
| 551 | + usb_fill_control_urb(urb, data->udev, pipe, (void *)dr, |
| 552 | + buf, size, btmtk_usb_wmt_recv, hdev); |
| 553 | + |
| 554 | + urb->transfer_flags |= URB_FREE_BUFFER; |
| 555 | + |
| 556 | + usb_anchor_urb(urb, data->ctrl_anchor); |
| 557 | + err = usb_submit_urb(urb, GFP_KERNEL); |
| 558 | + if (err < 0) { |
| 559 | + if (err != -EPERM && err != -ENODEV) |
| 560 | + bt_dev_err(hdev, "urb %p submission failed (%d)", |
| 561 | + urb, -err); |
| 562 | + usb_unanchor_urb(urb); |
| 563 | + } |
| 564 | + |
| 565 | + usb_free_urb(urb); |
| 566 | + |
| 567 | + return err; |
| 568 | +} |
| 569 | + |
| 570 | +int btmtk_usb_hci_wmt_sync(struct hci_dev *hdev, |
| 571 | + struct btmtk_hci_wmt_params *wmt_params) |
| 572 | +{ |
| 573 | + struct btmtk_data *data = hci_get_priv(hdev); |
| 574 | + struct btmtk_hci_wmt_evt_funcc *wmt_evt_funcc; |
| 575 | + u32 hlen, status = BTMTK_WMT_INVALID; |
| 576 | + struct btmtk_hci_wmt_evt *wmt_evt; |
| 577 | + struct btmtk_hci_wmt_cmd *wc; |
| 578 | + struct btmtk_wmt_hdr *hdr; |
| 579 | + int err; |
| 580 | + |
| 581 | + /* Send the WMT command and wait until the WMT event returns */ |
| 582 | + hlen = sizeof(*hdr) + wmt_params->dlen; |
| 583 | + if (hlen > 255) |
| 584 | + return -EINVAL; |
| 585 | + |
| 586 | + wc = kzalloc(hlen, GFP_KERNEL); |
| 587 | + if (!wc) |
| 588 | + return -ENOMEM; |
| 589 | + |
| 590 | + hdr = &wc->hdr; |
| 591 | + hdr->dir = 1; |
| 592 | + hdr->op = wmt_params->op; |
| 593 | + hdr->dlen = cpu_to_le16(wmt_params->dlen + 1); |
| 594 | + hdr->flag = wmt_params->flag; |
| 595 | + memcpy(wc->data, wmt_params->data, wmt_params->dlen); |
| 596 | + |
| 597 | + set_bit(BTMTK_TX_WAIT_VND_EVT, &data->flags); |
| 598 | + |
| 599 | + /* WMT cmd/event doesn't follow up the generic HCI cmd/event handling, |
| 600 | + * it needs constantly polling control pipe until the host received the |
| 601 | + * WMT event, thus, we should require to specifically acquire PM counter |
| 602 | + * on the USB to prevent the interface from entering auto suspended |
| 603 | + * while WMT cmd/event in progress. |
| 604 | + */ |
| 605 | + err = usb_autopm_get_interface(data->intf); |
| 606 | + if (err < 0) |
| 607 | + goto err_free_wc; |
| 608 | + |
| 609 | + err = __hci_cmd_send(hdev, 0xfc6f, hlen, wc); |
| 610 | + |
| 611 | + if (err < 0) { |
| 612 | + clear_bit(BTMTK_TX_WAIT_VND_EVT, &data->flags); |
| 613 | + usb_autopm_put_interface(data->intf); |
| 614 | + goto err_free_wc; |
| 615 | + } |
| 616 | + |
| 617 | + /* Submit control IN URB on demand to process the WMT event */ |
| 618 | + err = btmtk_usb_submit_wmt_recv_urb(hdev); |
| 619 | + |
| 620 | + usb_autopm_put_interface(data->intf); |
| 621 | + |
| 622 | + if (err < 0) |
| 623 | + goto err_free_wc; |
| 624 | + |
| 625 | + /* The vendor specific WMT commands are all answered by a vendor |
| 626 | + * specific event and will have the Command Status or Command |
| 627 | + * Complete as with usual HCI command flow control. |
| 628 | + * |
| 629 | + * After sending the command, wait for BTUSB_TX_WAIT_VND_EVT |
| 630 | + * state to be cleared. The driver specific event receive routine |
| 631 | + * will clear that state and with that indicate completion of the |
| 632 | + * WMT command. |
| 633 | + */ |
| 634 | + err = wait_on_bit_timeout(&data->flags, BTMTK_TX_WAIT_VND_EVT, |
| 635 | + TASK_INTERRUPTIBLE, HCI_INIT_TIMEOUT); |
| 636 | + if (err == -EINTR) { |
| 637 | + bt_dev_err(hdev, "Execution of wmt command interrupted"); |
| 638 | + clear_bit(BTMTK_TX_WAIT_VND_EVT, &data->flags); |
| 639 | + goto err_free_wc; |
| 640 | + } |
| 641 | + |
| 642 | + if (err) { |
| 643 | + bt_dev_err(hdev, "Execution of wmt command timed out"); |
| 644 | + clear_bit(BTMTK_TX_WAIT_VND_EVT, &data->flags); |
| 645 | + err = -ETIMEDOUT; |
| 646 | + goto err_free_wc; |
| 647 | + } |
| 648 | + |
| 649 | + if (data->evt_skb == NULL) |
| 650 | + goto err_free_wc; |
| 651 | + |
| 652 | + /* Parse and handle the return WMT event */ |
| 653 | + wmt_evt = (struct btmtk_hci_wmt_evt *)data->evt_skb->data; |
| 654 | + if (wmt_evt->whdr.op != hdr->op) { |
| 655 | + bt_dev_err(hdev, "Wrong op received %d expected %d", |
| 656 | + wmt_evt->whdr.op, hdr->op); |
| 657 | + err = -EIO; |
| 658 | + goto err_free_skb; |
| 659 | + } |
| 660 | + |
| 661 | + switch (wmt_evt->whdr.op) { |
| 662 | + case BTMTK_WMT_SEMAPHORE: |
| 663 | + if (wmt_evt->whdr.flag == 2) |
| 664 | + status = BTMTK_WMT_PATCH_UNDONE; |
| 665 | + else |
| 666 | + status = BTMTK_WMT_PATCH_DONE; |
| 667 | + break; |
| 668 | + case BTMTK_WMT_FUNC_CTRL: |
| 669 | + wmt_evt_funcc = (struct btmtk_hci_wmt_evt_funcc *)wmt_evt; |
| 670 | + if (be16_to_cpu(wmt_evt_funcc->status) == 0x404) |
| 671 | + status = BTMTK_WMT_ON_DONE; |
| 672 | + else if (be16_to_cpu(wmt_evt_funcc->status) == 0x420) |
| 673 | + status = BTMTK_WMT_ON_PROGRESS; |
| 674 | + else |
| 675 | + status = BTMTK_WMT_ON_UNDONE; |
| 676 | + break; |
| 677 | + case BTMTK_WMT_PATCH_DWNLD: |
| 678 | + if (wmt_evt->whdr.flag == 2) |
| 679 | + status = BTMTK_WMT_PATCH_DONE; |
| 680 | + else if (wmt_evt->whdr.flag == 1) |
| 681 | + status = BTMTK_WMT_PATCH_PROGRESS; |
| 682 | + else |
| 683 | + status = BTMTK_WMT_PATCH_UNDONE; |
| 684 | + break; |
| 685 | + } |
| 686 | + |
| 687 | + if (wmt_params->status) |
| 688 | + *wmt_params->status = status; |
| 689 | + |
| 690 | +err_free_skb: |
| 691 | + kfree_skb(data->evt_skb); |
| 692 | + data->evt_skb = NULL; |
| 693 | +err_free_wc: |
| 694 | + kfree(wc); |
| 695 | + return err; |
| 696 | +} |
| 697 | +EXPORT_SYMBOL_GPL(btmtk_usb_hci_wmt_sync); |
| 698 | + |
434 | 699 | MODULE_AUTHOR( "Sean Wang <[email protected]>");
|
435 | 700 | MODULE_AUTHOR( "Mark Chen <[email protected]>");
|
436 | 701 | MODULE_DESCRIPTION("Bluetooth support for MediaTek devices ver " VERSION);
|
|
0 commit comments