aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/bluetooth
diff options
context:
space:
mode:
authorOliver Neukum <oliver@neukum.org>2009-08-24 17:44:59 -0400
committerMarcel Holtmann <marcel@holtmann.org>2009-08-24 19:31:24 -0400
commit7bee549e197c9c0e92b89857a409675c1d5e9dff (patch)
treede18e51c34faf4c53fd2bd6b7f0c8ab2b2caf6e5 /drivers/bluetooth
parentd2e353f7c3c5fbb3add0341c10ae167ee745d23b (diff)
Bluetooth: Add USB autosuspend support to btusb driver
This patch adds support of USB autosuspend to the btusb driver. If the device doesn't support remote wakeup, simple support based on up/down is provided. If the device supports remote wakeup, additional support for autosuspend while the interface is up is provided. This is done by queueing URBs in an anchor structure and waking the device up from a work queue on sending. Reception triggers remote wakeup. The last busy facility of the USB autosuspend code is used. To close a race between autosuspend and transmission, a counter of ongoing transmissions is maintained. Add #ifdefs for CONFIG_PM as necessary. Signed-off-by: Oliver Neukum <oliver@neukum.org> Tested-by: Sarah Sharp <sarah.a.sharp@linux.intel.com> Signed-off-by: Marcel Holtmann <marcel@holtmann.org>
Diffstat (limited to 'drivers/bluetooth')
-rw-r--r--drivers/bluetooth/btusb.c194
1 files changed, 174 insertions, 20 deletions
diff --git a/drivers/bluetooth/btusb.c b/drivers/bluetooth/btusb.c
index 124db8cd144c..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);
@@ -325,6 +346,7 @@ static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
325 346
326 urb->transfer_flags |= URB_FREE_BUFFER; 347 urb->transfer_flags |= URB_FREE_BUFFER;
327 348
349 usb_mark_last_busy(data->udev);
328 usb_anchor_urb(urb, &data->bulk_anchor); 350 usb_anchor_urb(urb, &data->bulk_anchor);
329 351
330 err = usb_submit_urb(urb, mem_flags); 352 err = usb_submit_urb(urb, mem_flags);
@@ -463,6 +485,33 @@ static void btusb_tx_complete(struct urb *urb)
463{ 485{
464 struct sk_buff *skb = urb->context; 486 struct sk_buff *skb = urb->context;
465 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;
466 515
467 BT_DBG("%s urb %p status %d count %d", hdev->name, 516 BT_DBG("%s urb %p status %d count %d", hdev->name,
468 urb, urb->status, urb->actual_length); 517 urb, urb->status, urb->actual_length);
@@ -488,11 +537,17 @@ static int btusb_open(struct hci_dev *hdev)
488 537
489 BT_DBG("%s", hdev->name); 538 BT_DBG("%s", hdev->name);
490 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
491 if (test_and_set_bit(HCI_RUNNING, &hdev->flags)) 546 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
492 return 0; 547 goto done;
493 548
494 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags)) 549 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
495 return 0; 550 goto done;
496 551
497 err = btusb_submit_intr_urb(hdev, GFP_KERNEL); 552 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
498 if (err < 0) 553 if (err < 0)
@@ -507,17 +562,28 @@ static int btusb_open(struct hci_dev *hdev)
507 set_bit(BTUSB_BULK_RUNNING, &data->flags); 562 set_bit(BTUSB_BULK_RUNNING, &data->flags);
508 btusb_submit_bulk_urb(hdev, GFP_KERNEL); 563 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
509 564
565done:
566 usb_autopm_put_interface(data->intf);
510 return 0; 567 return 0;
511 568
512failed: 569failed:
513 clear_bit(BTUSB_INTR_RUNNING, &data->flags); 570 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
514 clear_bit(HCI_RUNNING, &hdev->flags); 571 clear_bit(HCI_RUNNING, &hdev->flags);
572 usb_autopm_put_interface(data->intf);
515 return err; 573 return err;
516} 574}
517 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
518static int btusb_close(struct hci_dev *hdev) 583static int btusb_close(struct hci_dev *hdev)
519{ 584{
520 struct btusb_data *data = hdev->driver_data; 585 struct btusb_data *data = hdev->driver_data;
586 int err;
521 587
522 BT_DBG("%s", hdev->name); 588 BT_DBG("%s", hdev->name);
523 589
@@ -527,13 +593,16 @@ static int btusb_close(struct hci_dev *hdev)
527 cancel_work_sync(&data->work); 593 cancel_work_sync(&data->work);
528 594
529 clear_bit(BTUSB_ISOC_RUNNING, &data->flags); 595 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
530 usb_kill_anchored_urbs(&data->isoc_anchor);
531
532 clear_bit(BTUSB_BULK_RUNNING, &data->flags); 596 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
533 usb_kill_anchored_urbs(&data->bulk_anchor);
534
535 clear_bit(BTUSB_INTR_RUNNING, &data->flags); 597 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
536 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);
537 606
538 return 0; 607 return 0;
539} 608}
@@ -620,7 +689,7 @@ static int btusb_send_frame(struct sk_buff *skb)
620 urb->dev = data->udev; 689 urb->dev = data->udev;
621 urb->pipe = pipe; 690 urb->pipe = pipe;
622 urb->context = skb; 691 urb->context = skb;
623 urb->complete = btusb_tx_complete; 692 urb->complete = btusb_isoc_tx_complete;
624 urb->interval = data->isoc_tx_ep->bInterval; 693 urb->interval = data->isoc_tx_ep->bInterval;
625 694
626 urb->transfer_flags = URB_ISO_ASAP; 695 urb->transfer_flags = URB_ISO_ASAP;
@@ -631,12 +700,21 @@ static int btusb_send_frame(struct sk_buff *skb)
631 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize)); 700 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
632 701
633 hdev->stat.sco_tx++; 702 hdev->stat.sco_tx++;
634 break; 703 goto skip_waking;
635 704
636 default: 705 default:
637 return -EILSEQ; 706 return -EILSEQ;
638 } 707 }
639 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:
640 usb_anchor_urb(urb, &data->tx_anchor); 718 usb_anchor_urb(urb, &data->tx_anchor);
641 719
642 err = usb_submit_urb(urb, GFP_ATOMIC); 720 err = usb_submit_urb(urb, GFP_ATOMIC);
@@ -644,10 +722,13 @@ static int btusb_send_frame(struct sk_buff *skb)
644 BT_ERR("%s urb %p submission failed", hdev->name, urb); 722 BT_ERR("%s urb %p submission failed", hdev->name, urb);
645 kfree(urb->setup_packet); 723 kfree(urb->setup_packet);
646 usb_unanchor_urb(urb); 724 usb_unanchor_urb(urb);
725 } else {
726 usb_mark_last_busy(data->udev);
647 } 727 }
648 728
649 usb_free_urb(urb); 729 usb_free_urb(urb);
650 730
731done:
651 return err; 732 return err;
652} 733}
653 734
@@ -719,8 +800,19 @@ static void btusb_work(struct work_struct *work)
719{ 800{
720 struct btusb_data *data = container_of(work, struct btusb_data, work); 801 struct btusb_data *data = container_of(work, struct btusb_data, work);
721 struct hci_dev *hdev = data->hdev; 802 struct hci_dev *hdev = data->hdev;
803 int err;
722 804
723 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 }
724 if (data->isoc_altsetting != 2) { 816 if (data->isoc_altsetting != 2) {
725 clear_bit(BTUSB_ISOC_RUNNING, &data->flags); 817 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
726 usb_kill_anchored_urbs(&data->isoc_anchor); 818 usb_kill_anchored_urbs(&data->isoc_anchor);
@@ -740,9 +832,25 @@ static void btusb_work(struct work_struct *work)
740 usb_kill_anchored_urbs(&data->isoc_anchor); 832 usb_kill_anchored_urbs(&data->isoc_anchor);
741 833
742 __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 }
743 } 839 }
744} 840}
745 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
746static int btusb_probe(struct usb_interface *intf, 854static int btusb_probe(struct usb_interface *intf,
747 const struct usb_device_id *id) 855 const struct usb_device_id *id)
748{ 856{
@@ -812,11 +920,14 @@ static int btusb_probe(struct usb_interface *intf,
812 spin_lock_init(&data->lock); 920 spin_lock_init(&data->lock);
813 921
814 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);
815 925
816 init_usb_anchor(&data->tx_anchor); 926 init_usb_anchor(&data->tx_anchor);
817 init_usb_anchor(&data->intr_anchor); 927 init_usb_anchor(&data->intr_anchor);
818 init_usb_anchor(&data->bulk_anchor); 928 init_usb_anchor(&data->bulk_anchor);
819 init_usb_anchor(&data->isoc_anchor); 929 init_usb_anchor(&data->isoc_anchor);
930 init_usb_anchor(&data->deferred);
820 931
821 hdev = hci_alloc_dev(); 932 hdev = hci_alloc_dev();
822 if (!hdev) { 933 if (!hdev) {
@@ -941,6 +1052,7 @@ static void btusb_disconnect(struct usb_interface *intf)
941 hci_free_dev(hdev); 1052 hci_free_dev(hdev);
942} 1053}
943 1054
1055#ifdef CONFIG_PM
944static int btusb_suspend(struct usb_interface *intf, pm_message_t message) 1056static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
945{ 1057{
946 struct btusb_data *data = usb_get_intfdata(intf); 1058 struct btusb_data *data = usb_get_intfdata(intf);
@@ -950,22 +1062,44 @@ static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
950 if (data->suspend_count++) 1062 if (data->suspend_count++)
951 return 0; 1063 return 0;
952 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
953 cancel_work_sync(&data->work); 1075 cancel_work_sync(&data->work);
954 1076
1077 btusb_stop_traffic(data);
955 usb_kill_anchored_urbs(&data->tx_anchor); 1078 usb_kill_anchored_urbs(&data->tx_anchor);
956 1079
957 usb_kill_anchored_urbs(&data->isoc_anchor);
958 usb_kill_anchored_urbs(&data->bulk_anchor);
959 usb_kill_anchored_urbs(&data->intr_anchor);
960
961 return 0; 1080 return 0;
962} 1081}
963 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
964static int btusb_resume(struct usb_interface *intf) 1098static int btusb_resume(struct usb_interface *intf)
965{ 1099{
966 struct btusb_data *data = usb_get_intfdata(intf); 1100 struct btusb_data *data = usb_get_intfdata(intf);
967 struct hci_dev *hdev = data->hdev; 1101 struct hci_dev *hdev = data->hdev;
968 int err; 1102 int err = 0;
969 1103
970 BT_DBG("intf %p", intf); 1104 BT_DBG("intf %p", intf);
971 1105
@@ -973,13 +1107,13 @@ static int btusb_resume(struct usb_interface *intf)
973 return 0; 1107 return 0;
974 1108
975 if (!test_bit(HCI_RUNNING, &hdev->flags)) 1109 if (!test_bit(HCI_RUNNING, &hdev->flags))
976 return 0; 1110 goto done;
977 1111
978 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) { 1112 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
979 err = btusb_submit_intr_urb(hdev, GFP_NOIO); 1113 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
980 if (err < 0) { 1114 if (err < 0) {
981 clear_bit(BTUSB_INTR_RUNNING, &data->flags); 1115 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
982 return err; 1116 goto failed;
983 } 1117 }
984 } 1118 }
985 1119
@@ -987,9 +1121,10 @@ static int btusb_resume(struct usb_interface *intf)
987 err = btusb_submit_bulk_urb(hdev, GFP_NOIO); 1121 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
988 if (err < 0) { 1122 if (err < 0) {
989 clear_bit(BTUSB_BULK_RUNNING, &data->flags); 1123 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
990 return err; 1124 goto failed;
991 } else 1125 }
992 btusb_submit_bulk_urb(hdev, GFP_NOIO); 1126
1127 btusb_submit_bulk_urb(hdev, GFP_NOIO);
993 } 1128 }
994 1129
995 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) { 1130 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
@@ -999,16 +1134,35 @@ static int btusb_resume(struct usb_interface *intf)
999 btusb_submit_isoc_urb(hdev, GFP_NOIO); 1134 btusb_submit_isoc_urb(hdev, GFP_NOIO);
1000 } 1135 }
1001 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
1002 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;
1003} 1153}
1154#endif
1004 1155
1005static struct usb_driver btusb_driver = { 1156static struct usb_driver btusb_driver = {
1006 .name = "btusb", 1157 .name = "btusb",
1007 .probe = btusb_probe, 1158 .probe = btusb_probe,
1008 .disconnect = btusb_disconnect, 1159 .disconnect = btusb_disconnect,
1160#ifdef CONFIG_PM
1009 .suspend = btusb_suspend, 1161 .suspend = btusb_suspend,
1010 .resume = btusb_resume, 1162 .resume = btusb_resume,
1163#endif
1011 .id_table = btusb_table, 1164 .id_table = btusb_table,
1165 .supports_autosuspend = 1,
1012}; 1166};
1013 1167
1014static int __init btusb_init(void) 1168static int __init btusb_init(void)