aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/bluetooth/btusb.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/bluetooth/btusb.c')
-rw-r--r--drivers/bluetooth/btusb.c198
1 files changed, 175 insertions, 23 deletions
diff --git a/drivers/bluetooth/btusb.c b/drivers/bluetooth/btusb.c
index e70c57ee4221..7ba91aa3fe8b 100644
--- a/drivers/bluetooth/btusb.c
+++ b/drivers/bluetooth/btusb.c
@@ -35,7 +35,7 @@
35#include <net/bluetooth/bluetooth.h> 35#include <net/bluetooth/bluetooth.h>
36#include <net/bluetooth/hci_core.h> 36#include <net/bluetooth/hci_core.h>
37 37
38#define VERSION "0.5" 38#define VERSION "0.6"
39 39
40static int ignore_dga; 40static int ignore_dga;
41static int ignore_csr; 41static int ignore_csr;
@@ -145,6 +145,7 @@ static struct usb_device_id blacklist_table[] = {
145#define BTUSB_INTR_RUNNING 0 145#define BTUSB_INTR_RUNNING 0
146#define BTUSB_BULK_RUNNING 1 146#define BTUSB_BULK_RUNNING 1
147#define BTUSB_ISOC_RUNNING 2 147#define BTUSB_ISOC_RUNNING 2
148#define BTUSB_SUSPENDING 3
148 149
149struct btusb_data { 150struct btusb_data {
150 struct hci_dev *hdev; 151 struct hci_dev *hdev;
@@ -157,11 +158,15 @@ struct btusb_data {
157 unsigned long flags; 158 unsigned long flags;
158 159
159 struct work_struct work; 160 struct work_struct work;
161 struct work_struct waker;
160 162
161 struct usb_anchor tx_anchor; 163 struct usb_anchor tx_anchor;
162 struct usb_anchor intr_anchor; 164 struct usb_anchor intr_anchor;
163 struct usb_anchor bulk_anchor; 165 struct usb_anchor bulk_anchor;
164 struct usb_anchor isoc_anchor; 166 struct usb_anchor isoc_anchor;
167 struct usb_anchor deferred;
168 int tx_in_flight;
169 spinlock_t txlock;
165 170
166 struct usb_endpoint_descriptor *intr_ep; 171 struct usb_endpoint_descriptor *intr_ep;
167 struct usb_endpoint_descriptor *bulk_tx_ep; 172 struct usb_endpoint_descriptor *bulk_tx_ep;
@@ -174,8 +179,23 @@ struct btusb_data {
174 unsigned int sco_num; 179 unsigned int sco_num;
175 int isoc_altsetting; 180 int isoc_altsetting;
176 int suspend_count; 181 int suspend_count;
182 int did_iso_resume:1;
177}; 183};
178 184
185static int inc_tx(struct btusb_data *data)
186{
187 unsigned long flags;
188 int rv;
189
190 spin_lock_irqsave(&data->txlock, flags);
191 rv = test_bit(BTUSB_SUSPENDING, &data->flags);
192 if (!rv)
193 data->tx_in_flight++;
194 spin_unlock_irqrestore(&data->txlock, flags);
195
196 return rv;
197}
198
179static void btusb_intr_complete(struct urb *urb) 199static void btusb_intr_complete(struct urb *urb)
180{ 200{
181 struct hci_dev *hdev = urb->context; 201 struct hci_dev *hdev = urb->context;
@@ -202,6 +222,7 @@ static void btusb_intr_complete(struct urb *urb)
202 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags)) 222 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
203 return; 223 return;
204 224
225 usb_mark_last_busy(data->udev);
205 usb_anchor_urb(urb, &data->intr_anchor); 226 usb_anchor_urb(urb, &data->intr_anchor);
206 227
207 err = usb_submit_urb(urb, GFP_ATOMIC); 228 err = usb_submit_urb(urb, GFP_ATOMIC);
@@ -301,7 +322,7 @@ static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
301 struct urb *urb; 322 struct urb *urb;
302 unsigned char *buf; 323 unsigned char *buf;
303 unsigned int pipe; 324 unsigned int pipe;
304 int err, size; 325 int err, size = HCI_MAX_FRAME_SIZE;
305 326
306 BT_DBG("%s", hdev->name); 327 BT_DBG("%s", hdev->name);
307 328
@@ -312,8 +333,6 @@ static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
312 if (!urb) 333 if (!urb)
313 return -ENOMEM; 334 return -ENOMEM;
314 335
315 size = le16_to_cpu(data->bulk_rx_ep->wMaxPacketSize);
316
317 buf = kmalloc(size, mem_flags); 336 buf = kmalloc(size, mem_flags);
318 if (!buf) { 337 if (!buf) {
319 usb_free_urb(urb); 338 usb_free_urb(urb);
@@ -327,6 +346,7 @@ static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
327 346
328 urb->transfer_flags |= URB_FREE_BUFFER; 347 urb->transfer_flags |= URB_FREE_BUFFER;
329 348
349 usb_mark_last_busy(data->udev);
330 usb_anchor_urb(urb, &data->bulk_anchor); 350 usb_anchor_urb(urb, &data->bulk_anchor);
331 351
332 err = usb_submit_urb(urb, mem_flags); 352 err = usb_submit_urb(urb, mem_flags);
@@ -465,6 +485,33 @@ static void btusb_tx_complete(struct urb *urb)
465{ 485{
466 struct sk_buff *skb = urb->context; 486 struct sk_buff *skb = urb->context;
467 struct hci_dev *hdev = (struct hci_dev *) skb->dev; 487 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
488 struct btusb_data *data = hdev->driver_data;
489
490 BT_DBG("%s urb %p status %d count %d", hdev->name,
491 urb, urb->status, urb->actual_length);
492
493 if (!test_bit(HCI_RUNNING, &hdev->flags))
494 goto done;
495
496 if (!urb->status)
497 hdev->stat.byte_tx += urb->transfer_buffer_length;
498 else
499 hdev->stat.err_tx++;
500
501done:
502 spin_lock(&data->txlock);
503 data->tx_in_flight--;
504 spin_unlock(&data->txlock);
505
506 kfree(urb->setup_packet);
507
508 kfree_skb(skb);
509}
510
511static void btusb_isoc_tx_complete(struct urb *urb)
512{
513 struct sk_buff *skb = urb->context;
514 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
468 515
469 BT_DBG("%s urb %p status %d count %d", hdev->name, 516 BT_DBG("%s urb %p status %d count %d", hdev->name,
470 urb, urb->status, urb->actual_length); 517 urb, urb->status, urb->actual_length);
@@ -490,11 +537,17 @@ static int btusb_open(struct hci_dev *hdev)
490 537
491 BT_DBG("%s", hdev->name); 538 BT_DBG("%s", hdev->name);
492 539
540 err = usb_autopm_get_interface(data->intf);
541 if (err < 0)
542 return err;
543
544 data->intf->needs_remote_wakeup = 1;
545
493 if (test_and_set_bit(HCI_RUNNING, &hdev->flags)) 546 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
494 return 0; 547 goto done;
495 548
496 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags)) 549 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
497 return 0; 550 goto done;
498 551
499 err = btusb_submit_intr_urb(hdev, GFP_KERNEL); 552 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
500 if (err < 0) 553 if (err < 0)
@@ -509,17 +562,28 @@ static int btusb_open(struct hci_dev *hdev)
509 set_bit(BTUSB_BULK_RUNNING, &data->flags); 562 set_bit(BTUSB_BULK_RUNNING, &data->flags);
510 btusb_submit_bulk_urb(hdev, GFP_KERNEL); 563 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
511 564
565done:
566 usb_autopm_put_interface(data->intf);
512 return 0; 567 return 0;
513 568
514failed: 569failed:
515 clear_bit(BTUSB_INTR_RUNNING, &data->flags); 570 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
516 clear_bit(HCI_RUNNING, &hdev->flags); 571 clear_bit(HCI_RUNNING, &hdev->flags);
572 usb_autopm_put_interface(data->intf);
517 return err; 573 return err;
518} 574}
519 575
576static void btusb_stop_traffic(struct btusb_data *data)
577{
578 usb_kill_anchored_urbs(&data->intr_anchor);
579 usb_kill_anchored_urbs(&data->bulk_anchor);
580 usb_kill_anchored_urbs(&data->isoc_anchor);
581}
582
520static int btusb_close(struct hci_dev *hdev) 583static int btusb_close(struct hci_dev *hdev)
521{ 584{
522 struct btusb_data *data = hdev->driver_data; 585 struct btusb_data *data = hdev->driver_data;
586 int err;
523 587
524 BT_DBG("%s", hdev->name); 588 BT_DBG("%s", hdev->name);
525 589
@@ -529,13 +593,16 @@ static int btusb_close(struct hci_dev *hdev)
529 cancel_work_sync(&data->work); 593 cancel_work_sync(&data->work);
530 594
531 clear_bit(BTUSB_ISOC_RUNNING, &data->flags); 595 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
532 usb_kill_anchored_urbs(&data->isoc_anchor);
533
534 clear_bit(BTUSB_BULK_RUNNING, &data->flags); 596 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
535 usb_kill_anchored_urbs(&data->bulk_anchor);
536
537 clear_bit(BTUSB_INTR_RUNNING, &data->flags); 597 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
538 usb_kill_anchored_urbs(&data->intr_anchor); 598
599 btusb_stop_traffic(data);
600 err = usb_autopm_get_interface(data->intf);
601 if (err < 0)
602 return 0;
603
604 data->intf->needs_remote_wakeup = 0;
605 usb_autopm_put_interface(data->intf);
539 606
540 return 0; 607 return 0;
541} 608}
@@ -622,7 +689,7 @@ static int btusb_send_frame(struct sk_buff *skb)
622 urb->dev = data->udev; 689 urb->dev = data->udev;
623 urb->pipe = pipe; 690 urb->pipe = pipe;
624 urb->context = skb; 691 urb->context = skb;
625 urb->complete = btusb_tx_complete; 692 urb->complete = btusb_isoc_tx_complete;
626 urb->interval = data->isoc_tx_ep->bInterval; 693 urb->interval = data->isoc_tx_ep->bInterval;
627 694
628 urb->transfer_flags = URB_ISO_ASAP; 695 urb->transfer_flags = URB_ISO_ASAP;
@@ -633,12 +700,21 @@ static int btusb_send_frame(struct sk_buff *skb)
633 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize)); 700 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
634 701
635 hdev->stat.sco_tx++; 702 hdev->stat.sco_tx++;
636 break; 703 goto skip_waking;
637 704
638 default: 705 default:
639 return -EILSEQ; 706 return -EILSEQ;
640 } 707 }
641 708
709 err = inc_tx(data);
710 if (err) {
711 usb_anchor_urb(urb, &data->deferred);
712 schedule_work(&data->waker);
713 err = 0;
714 goto done;
715 }
716
717skip_waking:
642 usb_anchor_urb(urb, &data->tx_anchor); 718 usb_anchor_urb(urb, &data->tx_anchor);
643 719
644 err = usb_submit_urb(urb, GFP_ATOMIC); 720 err = usb_submit_urb(urb, GFP_ATOMIC);
@@ -646,10 +722,13 @@ static int btusb_send_frame(struct sk_buff *skb)
646 BT_ERR("%s urb %p submission failed", hdev->name, urb); 722 BT_ERR("%s urb %p submission failed", hdev->name, urb);
647 kfree(urb->setup_packet); 723 kfree(urb->setup_packet);
648 usb_unanchor_urb(urb); 724 usb_unanchor_urb(urb);
725 } else {
726 usb_mark_last_busy(data->udev);
649 } 727 }
650 728
651 usb_free_urb(urb); 729 usb_free_urb(urb);
652 730
731done:
653 return err; 732 return err;
654} 733}
655 734
@@ -721,8 +800,19 @@ static void btusb_work(struct work_struct *work)
721{ 800{
722 struct btusb_data *data = container_of(work, struct btusb_data, work); 801 struct btusb_data *data = container_of(work, struct btusb_data, work);
723 struct hci_dev *hdev = data->hdev; 802 struct hci_dev *hdev = data->hdev;
803 int err;
724 804
725 if (hdev->conn_hash.sco_num > 0) { 805 if (hdev->conn_hash.sco_num > 0) {
806 if (!data->did_iso_resume) {
807 err = usb_autopm_get_interface(data->isoc);
808 if (err < 0) {
809 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
810 usb_kill_anchored_urbs(&data->isoc_anchor);
811 return;
812 }
813
814 data->did_iso_resume = 1;
815 }
726 if (data->isoc_altsetting != 2) { 816 if (data->isoc_altsetting != 2) {
727 clear_bit(BTUSB_ISOC_RUNNING, &data->flags); 817 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
728 usb_kill_anchored_urbs(&data->isoc_anchor); 818 usb_kill_anchored_urbs(&data->isoc_anchor);
@@ -742,9 +832,25 @@ static void btusb_work(struct work_struct *work)
742 usb_kill_anchored_urbs(&data->isoc_anchor); 832 usb_kill_anchored_urbs(&data->isoc_anchor);
743 833
744 __set_isoc_interface(hdev, 0); 834 __set_isoc_interface(hdev, 0);
835 if (data->did_iso_resume) {
836 data->did_iso_resume = 0;
837 usb_autopm_put_interface(data->isoc);
838 }
745 } 839 }
746} 840}
747 841
842static void btusb_waker(struct work_struct *work)
843{
844 struct btusb_data *data = container_of(work, struct btusb_data, waker);
845 int err;
846
847 err = usb_autopm_get_interface(data->intf);
848 if (err < 0)
849 return;
850
851 usb_autopm_put_interface(data->intf);
852}
853
748static int btusb_probe(struct usb_interface *intf, 854static int btusb_probe(struct usb_interface *intf,
749 const struct usb_device_id *id) 855 const struct usb_device_id *id)
750{ 856{
@@ -814,11 +920,14 @@ static int btusb_probe(struct usb_interface *intf,
814 spin_lock_init(&data->lock); 920 spin_lock_init(&data->lock);
815 921
816 INIT_WORK(&data->work, btusb_work); 922 INIT_WORK(&data->work, btusb_work);
923 INIT_WORK(&data->waker, btusb_waker);
924 spin_lock_init(&data->txlock);
817 925
818 init_usb_anchor(&data->tx_anchor); 926 init_usb_anchor(&data->tx_anchor);
819 init_usb_anchor(&data->intr_anchor); 927 init_usb_anchor(&data->intr_anchor);
820 init_usb_anchor(&data->bulk_anchor); 928 init_usb_anchor(&data->bulk_anchor);
821 init_usb_anchor(&data->isoc_anchor); 929 init_usb_anchor(&data->isoc_anchor);
930 init_usb_anchor(&data->deferred);
822 931
823 hdev = hci_alloc_dev(); 932 hdev = hci_alloc_dev();
824 if (!hdev) { 933 if (!hdev) {
@@ -943,6 +1052,7 @@ static void btusb_disconnect(struct usb_interface *intf)
943 hci_free_dev(hdev); 1052 hci_free_dev(hdev);
944} 1053}
945 1054
1055#ifdef CONFIG_PM
946static int btusb_suspend(struct usb_interface *intf, pm_message_t message) 1056static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
947{ 1057{
948 struct btusb_data *data = usb_get_intfdata(intf); 1058 struct btusb_data *data = usb_get_intfdata(intf);
@@ -952,22 +1062,44 @@ static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
952 if (data->suspend_count++) 1062 if (data->suspend_count++)
953 return 0; 1063 return 0;
954 1064
1065 spin_lock_irq(&data->txlock);
1066 if (!(interface_to_usbdev(intf)->auto_pm && data->tx_in_flight)) {
1067 set_bit(BTUSB_SUSPENDING, &data->flags);
1068 spin_unlock_irq(&data->txlock);
1069 } else {
1070 spin_unlock_irq(&data->txlock);
1071 data->suspend_count--;
1072 return -EBUSY;
1073 }
1074
955 cancel_work_sync(&data->work); 1075 cancel_work_sync(&data->work);
956 1076
1077 btusb_stop_traffic(data);
957 usb_kill_anchored_urbs(&data->tx_anchor); 1078 usb_kill_anchored_urbs(&data->tx_anchor);
958 1079
959 usb_kill_anchored_urbs(&data->isoc_anchor);
960 usb_kill_anchored_urbs(&data->bulk_anchor);
961 usb_kill_anchored_urbs(&data->intr_anchor);
962
963 return 0; 1080 return 0;
964} 1081}
965 1082
1083static void play_deferred(struct btusb_data *data)
1084{
1085 struct urb *urb;
1086 int err;
1087
1088 while ((urb = usb_get_from_anchor(&data->deferred))) {
1089 err = usb_submit_urb(urb, GFP_ATOMIC);
1090 if (err < 0)
1091 break;
1092
1093 data->tx_in_flight++;
1094 }
1095 usb_scuttle_anchored_urbs(&data->deferred);
1096}
1097
966static int btusb_resume(struct usb_interface *intf) 1098static int btusb_resume(struct usb_interface *intf)
967{ 1099{
968 struct btusb_data *data = usb_get_intfdata(intf); 1100 struct btusb_data *data = usb_get_intfdata(intf);
969 struct hci_dev *hdev = data->hdev; 1101 struct hci_dev *hdev = data->hdev;
970 int err; 1102 int err = 0;
971 1103
972 BT_DBG("intf %p", intf); 1104 BT_DBG("intf %p", intf);
973 1105
@@ -975,13 +1107,13 @@ static int btusb_resume(struct usb_interface *intf)
975 return 0; 1107 return 0;
976 1108
977 if (!test_bit(HCI_RUNNING, &hdev->flags)) 1109 if (!test_bit(HCI_RUNNING, &hdev->flags))
978 return 0; 1110 goto done;
979 1111
980 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) { 1112 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
981 err = btusb_submit_intr_urb(hdev, GFP_NOIO); 1113 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
982 if (err < 0) { 1114 if (err < 0) {
983 clear_bit(BTUSB_INTR_RUNNING, &data->flags); 1115 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
984 return err; 1116 goto failed;
985 } 1117 }
986 } 1118 }
987 1119
@@ -989,9 +1121,10 @@ static int btusb_resume(struct usb_interface *intf)
989 err = btusb_submit_bulk_urb(hdev, GFP_NOIO); 1121 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
990 if (err < 0) { 1122 if (err < 0) {
991 clear_bit(BTUSB_BULK_RUNNING, &data->flags); 1123 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
992 return err; 1124 goto failed;
993 } else 1125 }
994 btusb_submit_bulk_urb(hdev, GFP_NOIO); 1126
1127 btusb_submit_bulk_urb(hdev, GFP_NOIO);
995 } 1128 }
996 1129
997 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) { 1130 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
@@ -1001,16 +1134,35 @@ static int btusb_resume(struct usb_interface *intf)
1001 btusb_submit_isoc_urb(hdev, GFP_NOIO); 1134 btusb_submit_isoc_urb(hdev, GFP_NOIO);
1002 } 1135 }
1003 1136
1137 spin_lock_irq(&data->txlock);
1138 play_deferred(data);
1139 clear_bit(BTUSB_SUSPENDING, &data->flags);
1140 spin_unlock_irq(&data->txlock);
1141 schedule_work(&data->work);
1142
1004 return 0; 1143 return 0;
1144
1145failed:
1146 usb_scuttle_anchored_urbs(&data->deferred);
1147done:
1148 spin_lock_irq(&data->txlock);
1149 clear_bit(BTUSB_SUSPENDING, &data->flags);
1150 spin_unlock_irq(&data->txlock);
1151
1152 return err;
1005} 1153}
1154#endif
1006 1155
1007static struct usb_driver btusb_driver = { 1156static struct usb_driver btusb_driver = {
1008 .name = "btusb", 1157 .name = "btusb",
1009 .probe = btusb_probe, 1158 .probe = btusb_probe,
1010 .disconnect = btusb_disconnect, 1159 .disconnect = btusb_disconnect,
1160#ifdef CONFIG_PM
1011 .suspend = btusb_suspend, 1161 .suspend = btusb_suspend,
1012 .resume = btusb_resume, 1162 .resume = btusb_resume,
1163#endif
1013 .id_table = btusb_table, 1164 .id_table = btusb_table,
1165 .supports_autosuspend = 1,
1014}; 1166};
1015 1167
1016static int __init btusb_init(void) 1168static int __init btusb_init(void)