ARM: cpu topology: Add debugfs interface for cpu_power
[cmplus.git] / drivers / bluetooth / btusb.c
blobf4585b90841d8adb465fcd1ba92d9e0e53c6e4ef
1 /*
3 * Generic Bluetooth USB driver
5 * Copyright (C) 2005-2008 Marcel Holtmann <marcel@holtmann.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/slab.h>
28 #include <linux/types.h>
29 #include <linux/sched.h>
30 #include <linux/errno.h>
31 #include <linux/skbuff.h>
33 #include <linux/usb.h>
35 #include <net/bluetooth/bluetooth.h>
36 #include <net/bluetooth/hci_core.h>
38 #define VERSION "0.6"
40 static int ignore_dga;
41 static int ignore_csr;
42 static int ignore_sniffer;
43 static int disable_scofix;
44 static int force_scofix;
46 static int reset = 1;
48 static struct usb_driver btusb_driver;
50 #define BTUSB_IGNORE 0x01
51 #define BTUSB_DIGIANSWER 0x02
52 #define BTUSB_CSR 0x04
53 #define BTUSB_SNIFFER 0x08
54 #define BTUSB_BCM92035 0x10
55 #define BTUSB_BROKEN_ISOC 0x20
56 #define BTUSB_WRONG_SCO_MTU 0x40
57 #define BTUSB_ATH3012 0x80
59 static struct usb_device_id btusb_table[] = {
60 /* Generic Bluetooth USB device */
61 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
63 /* Broadcom SoftSailing reporting vendor specific */
64 { USB_DEVICE(0x0a5c, 0x21e1) },
66 /* Apple MacBookPro 7,1 */
67 { USB_DEVICE(0x05ac, 0x8213) },
69 /* Apple iMac11,1 */
70 { USB_DEVICE(0x05ac, 0x8215) },
72 /* Apple MacBookPro6,2 */
73 { USB_DEVICE(0x05ac, 0x8218) },
75 /* Apple MacBookAir3,1, MacBookAir3,2 */
76 { USB_DEVICE(0x05ac, 0x821b) },
78 /* Apple MacBookAir4,1 */
79 { USB_DEVICE(0x05ac, 0x821f) },
81 /* Apple MacBookPro8,2 */
82 { USB_DEVICE(0x05ac, 0x821a) },
84 /* Apple MacMini5,1 */
85 { USB_DEVICE(0x05ac, 0x8281) },
87 /* AVM BlueFRITZ! USB v2.0 */
88 { USB_DEVICE(0x057c, 0x3800) },
90 /* Bluetooth Ultraport Module from IBM */
91 { USB_DEVICE(0x04bf, 0x030a) },
93 /* ALPS Modules with non-standard id */
94 { USB_DEVICE(0x044e, 0x3001) },
95 { USB_DEVICE(0x044e, 0x3002) },
97 /* Ericsson with non-standard id */
98 { USB_DEVICE(0x0bdb, 0x1002) },
100 /* Canyon CN-BTU1 with HID interfaces */
101 { USB_DEVICE(0x0c10, 0x0000) },
103 /* Broadcom BCM20702A0 */
104 { USB_DEVICE(0x0489, 0xe042) },
105 { USB_DEVICE(0x0a5c, 0x21e3) },
106 { USB_DEVICE(0x0a5c, 0x21e6) },
107 { USB_DEVICE(0x0a5c, 0x21e8) },
108 { USB_DEVICE(0x0a5c, 0x21f3) },
109 { USB_DEVICE(0x413c, 0x8197) },
111 /* Foxconn - Hon Hai */
112 { USB_DEVICE(0x0489, 0xe033) },
114 { } /* Terminating entry */
117 MODULE_DEVICE_TABLE(usb, btusb_table);
119 static struct usb_device_id blacklist_table[] = {
120 /* CSR BlueCore devices */
121 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
123 /* Broadcom BCM2033 without firmware */
124 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
126 /* Atheros 3011 with sflash firmware */
127 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
128 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
129 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
130 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
132 /* Atheros AR9285 Malbec with sflash firmware */
133 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
135 /* Atheros 3012 with sflash firmware */
136 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
137 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
138 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
139 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
141 /* Atheros AR5BBU12 with sflash firmware */
142 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
144 /* Atheros AR5BBU12 with sflash firmware */
145 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
147 /* Broadcom BCM2035 */
148 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
149 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
150 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
152 /* Broadcom BCM2045 */
153 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
154 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
156 /* IBM/Lenovo ThinkPad with Broadcom chip */
157 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
158 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
160 /* HP laptop with Broadcom chip */
161 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
163 /* Dell laptop with Broadcom chip */
164 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
166 /* Dell Wireless 370 and 410 devices */
167 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
168 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
170 /* Belkin F8T012 and F8T013 devices */
171 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
172 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
174 /* Asus WL-BTD202 device */
175 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
177 /* Kensington Bluetooth USB adapter */
178 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
180 /* RTX Telecom based adapters with buggy SCO support */
181 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
182 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
184 /* CONWISE Technology based adapters with buggy SCO support */
185 { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
187 /* Digianswer devices */
188 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
189 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
191 /* CSR BlueCore Bluetooth Sniffer */
192 { USB_DEVICE(0x0a12, 0x0002), .driver_info = BTUSB_SNIFFER },
194 /* Frontline ComProbe Bluetooth Sniffer */
195 { USB_DEVICE(0x16d3, 0x0002), .driver_info = BTUSB_SNIFFER },
197 { } /* Terminating entry */
200 #define BTUSB_MAX_ISOC_FRAMES 10
202 #define BTUSB_INTR_RUNNING 0
203 #define BTUSB_BULK_RUNNING 1
204 #define BTUSB_ISOC_RUNNING 2
205 #define BTUSB_SUSPENDING 3
206 #define BTUSB_DID_ISO_RESUME 4
208 struct btusb_data {
209 struct hci_dev *hdev;
210 struct usb_device *udev;
211 struct usb_interface *intf;
212 struct usb_interface *isoc;
214 spinlock_t lock;
216 unsigned long flags;
218 struct work_struct work;
219 struct work_struct waker;
221 struct usb_anchor tx_anchor;
222 struct usb_anchor intr_anchor;
223 struct usb_anchor bulk_anchor;
224 struct usb_anchor isoc_anchor;
225 struct usb_anchor deferred;
226 int tx_in_flight;
227 spinlock_t txlock;
229 struct usb_endpoint_descriptor *intr_ep;
230 struct usb_endpoint_descriptor *bulk_tx_ep;
231 struct usb_endpoint_descriptor *bulk_rx_ep;
232 struct usb_endpoint_descriptor *isoc_tx_ep;
233 struct usb_endpoint_descriptor *isoc_rx_ep;
235 __u8 cmdreq_type;
237 unsigned int sco_num;
238 int isoc_altsetting;
239 int suspend_count;
242 static int inc_tx(struct btusb_data *data)
244 unsigned long flags;
245 int rv;
247 spin_lock_irqsave(&data->txlock, flags);
248 rv = test_bit(BTUSB_SUSPENDING, &data->flags);
249 if (!rv)
250 data->tx_in_flight++;
251 spin_unlock_irqrestore(&data->txlock, flags);
253 return rv;
256 static void btusb_intr_complete(struct urb *urb)
258 struct hci_dev *hdev = urb->context;
259 struct btusb_data *data = hdev->driver_data;
260 int err;
262 BT_DBG("%s urb %p status %d count %d", hdev->name,
263 urb, urb->status, urb->actual_length);
265 if (!test_bit(HCI_RUNNING, &hdev->flags))
266 return;
268 if (urb->status == 0) {
269 hdev->stat.byte_rx += urb->actual_length;
271 if (hci_recv_fragment(hdev, HCI_EVENT_PKT,
272 urb->transfer_buffer,
273 urb->actual_length) < 0) {
274 BT_ERR("%s corrupted event packet", hdev->name);
275 hdev->stat.err_rx++;
279 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
280 return;
282 usb_mark_last_busy(data->udev);
283 usb_anchor_urb(urb, &data->intr_anchor);
285 err = usb_submit_urb(urb, GFP_ATOMIC);
286 if (err < 0) {
287 if (err != -EPERM)
288 BT_ERR("%s urb %p failed to resubmit (%d)",
289 hdev->name, urb, -err);
290 usb_unanchor_urb(urb);
294 static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
296 struct btusb_data *data = hdev->driver_data;
297 struct urb *urb;
298 unsigned char *buf;
299 unsigned int pipe;
300 int err, size;
302 BT_DBG("%s", hdev->name);
304 if (!data->intr_ep)
305 return -ENODEV;
307 urb = usb_alloc_urb(0, mem_flags);
308 if (!urb)
309 return -ENOMEM;
311 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
313 buf = kmalloc(size, mem_flags);
314 if (!buf) {
315 usb_free_urb(urb);
316 return -ENOMEM;
319 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
321 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
322 btusb_intr_complete, hdev,
323 data->intr_ep->bInterval);
325 urb->transfer_flags |= URB_FREE_BUFFER;
327 usb_anchor_urb(urb, &data->intr_anchor);
329 err = usb_submit_urb(urb, mem_flags);
330 if (err < 0) {
331 BT_ERR("%s urb %p submission failed (%d)",
332 hdev->name, urb, -err);
333 usb_unanchor_urb(urb);
336 usb_free_urb(urb);
338 return err;
341 static void btusb_bulk_complete(struct urb *urb)
343 struct hci_dev *hdev = urb->context;
344 struct btusb_data *data = hdev->driver_data;
345 int err;
347 BT_DBG("%s urb %p status %d count %d", hdev->name,
348 urb, urb->status, urb->actual_length);
350 if (!test_bit(HCI_RUNNING, &hdev->flags))
351 return;
353 if (urb->status == 0) {
354 hdev->stat.byte_rx += urb->actual_length;
356 if (hci_recv_fragment(hdev, HCI_ACLDATA_PKT,
357 urb->transfer_buffer,
358 urb->actual_length) < 0) {
359 BT_ERR("%s corrupted ACL packet", hdev->name);
360 hdev->stat.err_rx++;
364 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
365 return;
367 usb_anchor_urb(urb, &data->bulk_anchor);
368 usb_mark_last_busy(data->udev);
370 err = usb_submit_urb(urb, GFP_ATOMIC);
371 if (err < 0) {
372 if (err != -EPERM)
373 BT_ERR("%s urb %p failed to resubmit (%d)",
374 hdev->name, urb, -err);
375 usb_unanchor_urb(urb);
379 static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
381 struct btusb_data *data = hdev->driver_data;
382 struct urb *urb;
383 unsigned char *buf;
384 unsigned int pipe;
385 int err, size = HCI_MAX_FRAME_SIZE;
387 BT_DBG("%s", hdev->name);
389 if (!data->bulk_rx_ep)
390 return -ENODEV;
392 urb = usb_alloc_urb(0, mem_flags);
393 if (!urb)
394 return -ENOMEM;
396 buf = kmalloc(size, mem_flags);
397 if (!buf) {
398 usb_free_urb(urb);
399 return -ENOMEM;
402 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
404 usb_fill_bulk_urb(urb, data->udev, pipe,
405 buf, size, btusb_bulk_complete, hdev);
407 urb->transfer_flags |= URB_FREE_BUFFER;
409 usb_mark_last_busy(data->udev);
410 usb_anchor_urb(urb, &data->bulk_anchor);
412 err = usb_submit_urb(urb, mem_flags);
413 if (err < 0) {
414 BT_ERR("%s urb %p submission failed (%d)",
415 hdev->name, urb, -err);
416 usb_unanchor_urb(urb);
419 usb_free_urb(urb);
421 return err;
424 static void btusb_isoc_complete(struct urb *urb)
426 struct hci_dev *hdev = urb->context;
427 struct btusb_data *data = hdev->driver_data;
428 int i, err;
430 BT_DBG("%s urb %p status %d count %d", hdev->name,
431 urb, urb->status, urb->actual_length);
433 if (!test_bit(HCI_RUNNING, &hdev->flags))
434 return;
436 if (urb->status == 0) {
437 for (i = 0; i < urb->number_of_packets; i++) {
438 unsigned int offset = urb->iso_frame_desc[i].offset;
439 unsigned int length = urb->iso_frame_desc[i].actual_length;
441 if (urb->iso_frame_desc[i].status)
442 continue;
444 hdev->stat.byte_rx += length;
446 if (hci_recv_fragment(hdev, HCI_SCODATA_PKT,
447 urb->transfer_buffer + offset,
448 length) < 0) {
449 BT_ERR("%s corrupted SCO packet", hdev->name);
450 hdev->stat.err_rx++;
455 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
456 return;
458 usb_anchor_urb(urb, &data->isoc_anchor);
460 err = usb_submit_urb(urb, GFP_ATOMIC);
461 if (err < 0) {
462 if (err != -EPERM)
463 BT_ERR("%s urb %p failed to resubmit (%d)",
464 hdev->name, urb, -err);
465 usb_unanchor_urb(urb);
469 static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
471 int i, offset = 0;
473 BT_DBG("len %d mtu %d", len, mtu);
475 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
476 i++, offset += mtu, len -= mtu) {
477 urb->iso_frame_desc[i].offset = offset;
478 urb->iso_frame_desc[i].length = mtu;
481 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
482 urb->iso_frame_desc[i].offset = offset;
483 urb->iso_frame_desc[i].length = len;
484 i++;
487 urb->number_of_packets = i;
490 static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
492 struct btusb_data *data = hdev->driver_data;
493 struct urb *urb;
494 unsigned char *buf;
495 unsigned int pipe;
496 int err, size;
498 BT_DBG("%s", hdev->name);
500 if (!data->isoc_rx_ep)
501 return -ENODEV;
503 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
504 if (!urb)
505 return -ENOMEM;
507 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
508 BTUSB_MAX_ISOC_FRAMES;
510 buf = kmalloc(size, mem_flags);
511 if (!buf) {
512 usb_free_urb(urb);
513 return -ENOMEM;
516 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
518 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
519 hdev, data->isoc_rx_ep->bInterval);
521 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
523 __fill_isoc_descriptor(urb, size,
524 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
526 usb_anchor_urb(urb, &data->isoc_anchor);
528 err = usb_submit_urb(urb, mem_flags);
529 if (err < 0) {
530 BT_ERR("%s urb %p submission failed (%d)",
531 hdev->name, urb, -err);
532 usb_unanchor_urb(urb);
535 usb_free_urb(urb);
537 return err;
540 static void btusb_tx_complete(struct urb *urb)
542 struct sk_buff *skb = urb->context;
543 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
544 struct btusb_data *data = hdev->driver_data;
546 BT_DBG("%s urb %p status %d count %d", hdev->name,
547 urb, urb->status, urb->actual_length);
549 if (!test_bit(HCI_RUNNING, &hdev->flags))
550 goto done;
552 if (!urb->status)
553 hdev->stat.byte_tx += urb->transfer_buffer_length;
554 else
555 hdev->stat.err_tx++;
557 done:
558 spin_lock(&data->txlock);
559 data->tx_in_flight--;
560 spin_unlock(&data->txlock);
562 kfree(urb->setup_packet);
564 kfree_skb(skb);
567 static void btusb_isoc_tx_complete(struct urb *urb)
569 struct sk_buff *skb = urb->context;
570 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
572 BT_DBG("%s urb %p status %d count %d", hdev->name,
573 urb, urb->status, urb->actual_length);
575 if (!test_bit(HCI_RUNNING, &hdev->flags))
576 goto done;
578 if (!urb->status)
579 hdev->stat.byte_tx += urb->transfer_buffer_length;
580 else
581 hdev->stat.err_tx++;
583 done:
584 kfree(urb->setup_packet);
586 kfree_skb(skb);
589 static int btusb_open(struct hci_dev *hdev)
591 struct btusb_data *data = hdev->driver_data;
592 int err;
594 BT_DBG("%s", hdev->name);
596 err = usb_autopm_get_interface(data->intf);
597 if (err < 0)
598 return err;
600 data->intf->needs_remote_wakeup = 1;
602 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
603 goto done;
605 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
606 goto done;
608 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
609 if (err < 0)
610 goto failed;
612 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
613 if (err < 0) {
614 usb_kill_anchored_urbs(&data->intr_anchor);
615 goto failed;
618 set_bit(BTUSB_BULK_RUNNING, &data->flags);
619 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
621 done:
622 usb_autopm_put_interface(data->intf);
623 return 0;
625 failed:
626 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
627 clear_bit(HCI_RUNNING, &hdev->flags);
628 usb_autopm_put_interface(data->intf);
629 return err;
632 static void btusb_stop_traffic(struct btusb_data *data)
634 usb_kill_anchored_urbs(&data->intr_anchor);
635 usb_kill_anchored_urbs(&data->bulk_anchor);
636 usb_kill_anchored_urbs(&data->isoc_anchor);
639 static int btusb_close(struct hci_dev *hdev)
641 struct btusb_data *data = hdev->driver_data;
642 int err;
644 BT_DBG("%s", hdev->name);
646 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
647 return 0;
649 cancel_work_sync(&data->work);
650 cancel_work_sync(&data->waker);
652 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
653 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
654 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
656 btusb_stop_traffic(data);
657 err = usb_autopm_get_interface(data->intf);
658 if (err < 0)
659 goto failed;
661 data->intf->needs_remote_wakeup = 0;
662 usb_autopm_put_interface(data->intf);
664 failed:
665 usb_scuttle_anchored_urbs(&data->deferred);
666 return 0;
669 static int btusb_flush(struct hci_dev *hdev)
671 struct btusb_data *data = hdev->driver_data;
673 BT_DBG("%s", hdev->name);
675 usb_kill_anchored_urbs(&data->tx_anchor);
677 return 0;
680 static int btusb_send_frame(struct sk_buff *skb)
682 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
683 struct btusb_data *data = hdev->driver_data;
684 struct usb_ctrlrequest *dr;
685 struct urb *urb;
686 unsigned int pipe;
687 int err;
689 BT_DBG("%s", hdev->name);
691 if (!test_bit(HCI_RUNNING, &hdev->flags))
692 return -EBUSY;
694 switch (bt_cb(skb)->pkt_type) {
695 case HCI_COMMAND_PKT:
696 urb = usb_alloc_urb(0, GFP_ATOMIC);
697 if (!urb)
698 return -ENOMEM;
700 dr = kmalloc(sizeof(*dr), GFP_ATOMIC);
701 if (!dr) {
702 usb_free_urb(urb);
703 return -ENOMEM;
706 dr->bRequestType = data->cmdreq_type;
707 dr->bRequest = 0;
708 dr->wIndex = 0;
709 dr->wValue = 0;
710 dr->wLength = __cpu_to_le16(skb->len);
712 pipe = usb_sndctrlpipe(data->udev, 0x00);
714 usb_fill_control_urb(urb, data->udev, pipe, (void *) dr,
715 skb->data, skb->len, btusb_tx_complete, skb);
717 hdev->stat.cmd_tx++;
718 break;
720 case HCI_ACLDATA_PKT:
721 if (!data->bulk_tx_ep || (hdev->conn_hash.acl_num < 1 &&
722 hdev->conn_hash.le_num < 1))
723 return -ENODEV;
725 urb = usb_alloc_urb(0, GFP_ATOMIC);
726 if (!urb)
727 return -ENOMEM;
729 pipe = usb_sndbulkpipe(data->udev,
730 data->bulk_tx_ep->bEndpointAddress);
732 usb_fill_bulk_urb(urb, data->udev, pipe,
733 skb->data, skb->len, btusb_tx_complete, skb);
735 hdev->stat.acl_tx++;
736 break;
738 case HCI_SCODATA_PKT:
739 if (!data->isoc_tx_ep || hdev->conn_hash.sco_num < 1)
740 return -ENODEV;
742 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_ATOMIC);
743 if (!urb)
744 return -ENOMEM;
746 pipe = usb_sndisocpipe(data->udev,
747 data->isoc_tx_ep->bEndpointAddress);
749 usb_fill_int_urb(urb, data->udev, pipe,
750 skb->data, skb->len, btusb_isoc_tx_complete,
751 skb, data->isoc_tx_ep->bInterval);
753 urb->transfer_flags = URB_ISO_ASAP;
755 __fill_isoc_descriptor(urb, skb->len,
756 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
758 hdev->stat.sco_tx++;
759 goto skip_waking;
761 default:
762 return -EILSEQ;
765 err = inc_tx(data);
766 if (err) {
767 usb_anchor_urb(urb, &data->deferred);
768 schedule_work(&data->waker);
769 err = 0;
770 goto done;
773 skip_waking:
774 usb_anchor_urb(urb, &data->tx_anchor);
776 err = usb_submit_urb(urb, GFP_ATOMIC);
777 if (err < 0) {
778 BT_ERR("%s urb %p submission failed", hdev->name, urb);
779 kfree(urb->setup_packet);
780 usb_unanchor_urb(urb);
781 } else {
782 usb_mark_last_busy(data->udev);
785 usb_free_urb(urb);
787 done:
788 return err;
791 static void btusb_destruct(struct hci_dev *hdev)
793 struct btusb_data *data = hdev->driver_data;
795 BT_DBG("%s", hdev->name);
797 kfree(data);
800 static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
802 struct btusb_data *data = hdev->driver_data;
804 BT_DBG("%s evt %d", hdev->name, evt);
806 if (hdev->conn_hash.sco_num != data->sco_num) {
807 data->sco_num = hdev->conn_hash.sco_num;
808 schedule_work(&data->work);
812 static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
814 struct btusb_data *data = hdev->driver_data;
815 struct usb_interface *intf = data->isoc;
816 struct usb_endpoint_descriptor *ep_desc;
817 int i, err;
819 if (!data->isoc)
820 return -ENODEV;
822 err = usb_set_interface(data->udev, 1, altsetting);
823 if (err < 0) {
824 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
825 return err;
828 data->isoc_altsetting = altsetting;
830 data->isoc_tx_ep = NULL;
831 data->isoc_rx_ep = NULL;
833 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
834 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
836 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
837 data->isoc_tx_ep = ep_desc;
838 continue;
841 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
842 data->isoc_rx_ep = ep_desc;
843 continue;
847 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
848 BT_ERR("%s invalid SCO descriptors", hdev->name);
849 return -ENODEV;
852 return 0;
855 static void btusb_work(struct work_struct *work)
857 struct btusb_data *data = container_of(work, struct btusb_data, work);
858 struct hci_dev *hdev = data->hdev;
859 int err;
861 if (hdev->conn_hash.sco_num > 0) {
862 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
863 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
864 if (err < 0) {
865 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
866 usb_kill_anchored_urbs(&data->isoc_anchor);
867 return;
870 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
872 if (data->isoc_altsetting != 2) {
873 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
874 usb_kill_anchored_urbs(&data->isoc_anchor);
876 if (__set_isoc_interface(hdev, 2) < 0)
877 return;
880 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
881 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
882 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
883 else
884 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
886 } else {
887 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
888 usb_kill_anchored_urbs(&data->isoc_anchor);
890 __set_isoc_interface(hdev, 0);
891 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
892 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
896 static void btusb_waker(struct work_struct *work)
898 struct btusb_data *data = container_of(work, struct btusb_data, waker);
899 int err;
901 err = usb_autopm_get_interface(data->intf);
902 if (err < 0)
903 return;
905 usb_autopm_put_interface(data->intf);
908 static int btusb_probe(struct usb_interface *intf,
909 const struct usb_device_id *id)
911 struct usb_endpoint_descriptor *ep_desc;
912 struct btusb_data *data;
913 struct hci_dev *hdev;
914 int i, err;
916 BT_DBG("intf %p id %p", intf, id);
918 /* interface numbers are hardcoded in the spec */
919 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
920 return -ENODEV;
922 if (!id->driver_info) {
923 const struct usb_device_id *match;
924 match = usb_match_id(intf, blacklist_table);
925 if (match)
926 id = match;
929 if (id->driver_info == BTUSB_IGNORE)
930 return -ENODEV;
932 if (ignore_dga && id->driver_info & BTUSB_DIGIANSWER)
933 return -ENODEV;
935 if (ignore_csr && id->driver_info & BTUSB_CSR)
936 return -ENODEV;
938 if (ignore_sniffer && id->driver_info & BTUSB_SNIFFER)
939 return -ENODEV;
941 if (id->driver_info & BTUSB_ATH3012) {
942 struct usb_device *udev = interface_to_usbdev(intf);
944 /* Old firmware would otherwise let ath3k driver load
945 * patch and sysconfig files */
946 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
947 return -ENODEV;
950 data = kzalloc(sizeof(*data), GFP_KERNEL);
951 if (!data)
952 return -ENOMEM;
954 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
955 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
957 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
958 data->intr_ep = ep_desc;
959 continue;
962 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
963 data->bulk_tx_ep = ep_desc;
964 continue;
967 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
968 data->bulk_rx_ep = ep_desc;
969 continue;
973 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep) {
974 kfree(data);
975 return -ENODEV;
978 data->cmdreq_type = USB_TYPE_CLASS;
980 data->udev = interface_to_usbdev(intf);
981 data->intf = intf;
983 spin_lock_init(&data->lock);
985 INIT_WORK(&data->work, btusb_work);
986 INIT_WORK(&data->waker, btusb_waker);
987 spin_lock_init(&data->txlock);
989 init_usb_anchor(&data->tx_anchor);
990 init_usb_anchor(&data->intr_anchor);
991 init_usb_anchor(&data->bulk_anchor);
992 init_usb_anchor(&data->isoc_anchor);
993 init_usb_anchor(&data->deferred);
995 hdev = hci_alloc_dev();
996 if (!hdev) {
997 kfree(data);
998 return -ENOMEM;
1001 hdev->bus = HCI_USB;
1002 hdev->driver_data = data;
1004 data->hdev = hdev;
1006 SET_HCIDEV_DEV(hdev, &intf->dev);
1008 hdev->open = btusb_open;
1009 hdev->close = btusb_close;
1010 hdev->flush = btusb_flush;
1011 hdev->send = btusb_send_frame;
1012 hdev->destruct = btusb_destruct;
1013 hdev->notify = btusb_notify;
1015 hdev->owner = THIS_MODULE;
1017 /* Interface numbers are hardcoded in the specification */
1018 data->isoc = usb_ifnum_to_if(data->udev, 1);
1020 if (!reset)
1021 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
1023 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
1024 if (!disable_scofix)
1025 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
1028 if (id->driver_info & BTUSB_BROKEN_ISOC)
1029 data->isoc = NULL;
1031 if (id->driver_info & BTUSB_DIGIANSWER) {
1032 data->cmdreq_type = USB_TYPE_VENDOR;
1033 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
1036 if (id->driver_info & BTUSB_CSR) {
1037 struct usb_device *udev = data->udev;
1039 /* Old firmware would otherwise execute USB reset */
1040 if (le16_to_cpu(udev->descriptor.bcdDevice) < 0x117)
1041 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
1044 if (id->driver_info & BTUSB_SNIFFER) {
1045 struct usb_device *udev = data->udev;
1047 /* New sniffer firmware has crippled HCI interface */
1048 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
1049 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
1051 data->isoc = NULL;
1054 if (id->driver_info & BTUSB_BCM92035) {
1055 unsigned char cmd[] = { 0x3b, 0xfc, 0x01, 0x00 };
1056 struct sk_buff *skb;
1058 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
1059 if (skb) {
1060 memcpy(skb_put(skb, sizeof(cmd)), cmd, sizeof(cmd));
1061 skb_queue_tail(&hdev->driver_init, skb);
1065 if (data->isoc) {
1066 err = usb_driver_claim_interface(&btusb_driver,
1067 data->isoc, data);
1068 if (err < 0) {
1069 hci_free_dev(hdev);
1070 kfree(data);
1071 return err;
1075 err = hci_register_dev(hdev);
1076 if (err < 0) {
1077 hci_free_dev(hdev);
1078 kfree(data);
1079 return err;
1082 usb_set_intfdata(intf, data);
1084 return 0;
1087 static void btusb_disconnect(struct usb_interface *intf)
1089 struct btusb_data *data = usb_get_intfdata(intf);
1090 struct hci_dev *hdev;
1092 BT_DBG("intf %p", intf);
1094 if (!data)
1095 return;
1097 hdev = data->hdev;
1099 __hci_dev_hold(hdev);
1101 usb_set_intfdata(data->intf, NULL);
1103 if (data->isoc)
1104 usb_set_intfdata(data->isoc, NULL);
1106 hci_unregister_dev(hdev);
1108 if (intf == data->isoc)
1109 usb_driver_release_interface(&btusb_driver, data->intf);
1110 else if (data->isoc)
1111 usb_driver_release_interface(&btusb_driver, data->isoc);
1113 __hci_dev_put(hdev);
1115 hci_free_dev(hdev);
1118 #ifdef CONFIG_PM
1119 static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
1121 struct btusb_data *data = usb_get_intfdata(intf);
1123 BT_DBG("intf %p", intf);
1125 if (data->suspend_count++)
1126 return 0;
1128 spin_lock_irq(&data->txlock);
1129 if (!((message.event & PM_EVENT_AUTO) && data->tx_in_flight)) {
1130 set_bit(BTUSB_SUSPENDING, &data->flags);
1131 spin_unlock_irq(&data->txlock);
1132 } else {
1133 spin_unlock_irq(&data->txlock);
1134 data->suspend_count--;
1135 return -EBUSY;
1138 cancel_work_sync(&data->work);
1140 btusb_stop_traffic(data);
1141 usb_kill_anchored_urbs(&data->tx_anchor);
1143 return 0;
1146 static void play_deferred(struct btusb_data *data)
1148 struct urb *urb;
1149 int err;
1151 while ((urb = usb_get_from_anchor(&data->deferred))) {
1152 err = usb_submit_urb(urb, GFP_ATOMIC);
1153 if (err < 0)
1154 break;
1156 data->tx_in_flight++;
1158 usb_scuttle_anchored_urbs(&data->deferred);
1161 static int btusb_resume(struct usb_interface *intf)
1163 struct btusb_data *data = usb_get_intfdata(intf);
1164 struct hci_dev *hdev = data->hdev;
1165 int err = 0;
1167 BT_DBG("intf %p", intf);
1169 if (--data->suspend_count)
1170 return 0;
1172 if (!test_bit(HCI_RUNNING, &hdev->flags))
1173 goto done;
1175 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
1176 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
1177 if (err < 0) {
1178 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1179 goto failed;
1183 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
1184 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
1185 if (err < 0) {
1186 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
1187 goto failed;
1190 btusb_submit_bulk_urb(hdev, GFP_NOIO);
1193 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1194 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
1195 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1196 else
1197 btusb_submit_isoc_urb(hdev, GFP_NOIO);
1200 spin_lock_irq(&data->txlock);
1201 play_deferred(data);
1202 clear_bit(BTUSB_SUSPENDING, &data->flags);
1203 spin_unlock_irq(&data->txlock);
1204 schedule_work(&data->work);
1206 return 0;
1208 failed:
1209 usb_scuttle_anchored_urbs(&data->deferred);
1210 done:
1211 spin_lock_irq(&data->txlock);
1212 clear_bit(BTUSB_SUSPENDING, &data->flags);
1213 spin_unlock_irq(&data->txlock);
1215 return err;
1217 #endif
1219 static struct usb_driver btusb_driver = {
1220 .name = "btusb",
1221 .probe = btusb_probe,
1222 .disconnect = btusb_disconnect,
1223 #ifdef CONFIG_PM
1224 .suspend = btusb_suspend,
1225 .resume = btusb_resume,
1226 #endif
1227 .id_table = btusb_table,
1228 .supports_autosuspend = 1,
1231 static int __init btusb_init(void)
1233 BT_INFO("Generic Bluetooth USB driver ver %s", VERSION);
1235 return usb_register(&btusb_driver);
1238 static void __exit btusb_exit(void)
1240 usb_deregister(&btusb_driver);
1243 module_init(btusb_init);
1244 module_exit(btusb_exit);
1246 module_param(ignore_dga, bool, 0644);
1247 MODULE_PARM_DESC(ignore_dga, "Ignore devices with id 08fd:0001");
1249 module_param(ignore_csr, bool, 0644);
1250 MODULE_PARM_DESC(ignore_csr, "Ignore devices with id 0a12:0001");
1252 module_param(ignore_sniffer, bool, 0644);
1253 MODULE_PARM_DESC(ignore_sniffer, "Ignore devices with id 0a12:0002");
1255 module_param(disable_scofix, bool, 0644);
1256 MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
1258 module_param(force_scofix, bool, 0644);
1259 MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
1261 module_param(reset, bool, 0644);
1262 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
1264 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
1265 MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
1266 MODULE_VERSION(VERSION);
1267 MODULE_LICENSE("GPL");