xref: /linux/drivers/bluetooth/btusb.c (revision 40d3057ac036f2501c1930728a6179be4fca577b)
1 /*
2  *
3  *  Generic Bluetooth USB driver
4  *
5  *  Copyright (C) 2005-2008  Marcel Holtmann <marcel@holtmann.org>
6  *
7  *
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.
12  *
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.
17  *
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
21  *
22  */
23 
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>
32 
33 #include <linux/usb.h>
34 
35 #include <net/bluetooth/bluetooth.h>
36 #include <net/bluetooth/hci_core.h>
37 
38 //#define CONFIG_BT_HCIBTUSB_DEBUG
39 #ifndef CONFIG_BT_HCIBTUSB_DEBUG
40 #undef  BT_DBG
41 #define BT_DBG(D...)
42 #endif
43 
44 #define VERSION "0.3"
45 
46 static int ignore_dga;
47 static int ignore_csr;
48 static int ignore_sniffer;
49 static int disable_scofix;
50 static int force_scofix;
51 static int reset;
52 
53 static struct usb_driver btusb_driver;
54 
55 #define BTUSB_IGNORE		0x01
56 #define BTUSB_RESET		0x02
57 #define BTUSB_DIGIANSWER	0x04
58 #define BTUSB_CSR		0x08
59 #define BTUSB_SNIFFER		0x10
60 #define BTUSB_BCM92035		0x20
61 #define BTUSB_BROKEN_ISOC	0x40
62 #define BTUSB_WRONG_SCO_MTU	0x80
63 
64 static struct usb_device_id btusb_table[] = {
65 	/* Generic Bluetooth USB device */
66 	{ USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
67 
68 	/* AVM BlueFRITZ! USB v2.0 */
69 	{ USB_DEVICE(0x057c, 0x3800) },
70 
71 	/* Bluetooth Ultraport Module from IBM */
72 	{ USB_DEVICE(0x04bf, 0x030a) },
73 
74 	/* ALPS Modules with non-standard id */
75 	{ USB_DEVICE(0x044e, 0x3001) },
76 	{ USB_DEVICE(0x044e, 0x3002) },
77 
78 	/* Ericsson with non-standard id */
79 	{ USB_DEVICE(0x0bdb, 0x1002) },
80 
81 	/* Canyon CN-BTU1 with HID interfaces */
82 	{ USB_DEVICE(0x0c10, 0x0000), .driver_info = BTUSB_RESET },
83 
84 	{ }	/* Terminating entry */
85 };
86 
87 MODULE_DEVICE_TABLE(usb, btusb_table);
88 
89 static struct usb_device_id blacklist_table[] = {
90 	/* CSR BlueCore devices */
91 	{ USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
92 
93 	/* Broadcom BCM2033 without firmware */
94 	{ USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
95 
96 	/* Broadcom BCM2035 */
97 	{ USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_RESET | BTUSB_WRONG_SCO_MTU },
98 	{ USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_RESET | BTUSB_WRONG_SCO_MTU },
99 
100 	/* Broadcom BCM2045 */
101 	{ USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_RESET | BTUSB_WRONG_SCO_MTU },
102 	{ USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_RESET | BTUSB_WRONG_SCO_MTU },
103 
104 	/* Broadcom BCM2046 */
105 	{ USB_DEVICE(0x0a5c, 0x2151), .driver_info = BTUSB_RESET },
106 
107 	/* IBM/Lenovo ThinkPad with Broadcom chip */
108 	{ USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_RESET | BTUSB_WRONG_SCO_MTU },
109 	{ USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_RESET | BTUSB_WRONG_SCO_MTU },
110 
111 	/* Targus ACB10US */
112 	{ USB_DEVICE(0x0a5c, 0x2100), .driver_info = BTUSB_RESET },
113 
114 	/* ANYCOM Bluetooth USB-200 and USB-250 */
115 	{ USB_DEVICE(0x0a5c, 0x2111), .driver_info = BTUSB_RESET },
116 
117 	/* HP laptop with Broadcom chip */
118 	{ USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_RESET | BTUSB_WRONG_SCO_MTU },
119 
120 	/* Dell laptop with Broadcom chip */
121 	{ USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_RESET | BTUSB_WRONG_SCO_MTU },
122 
123 	/* Dell Wireless 370 */
124 	{ USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_RESET | BTUSB_WRONG_SCO_MTU },
125 
126 	/* Dell Wireless 410 */
127 	{ USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_RESET | BTUSB_WRONG_SCO_MTU },
128 
129 	/* Microsoft Wireless Transceiver for Bluetooth 2.0 */
130 	{ USB_DEVICE(0x045e, 0x009c), .driver_info = BTUSB_RESET },
131 
132 	/* Kensington Bluetooth USB adapter */
133 	{ USB_DEVICE(0x047d, 0x105d), .driver_info = BTUSB_RESET },
134 	{ USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_RESET | BTUSB_WRONG_SCO_MTU },
135 
136 	/* ISSC Bluetooth Adapter v3.1 */
137 	{ USB_DEVICE(0x1131, 0x1001), .driver_info = BTUSB_RESET },
138 
139 	/* RTX Telecom based adapters with buggy SCO support */
140 	{ USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
141 	{ USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
142 
143 	/* CONWISE Technology based adapters with buggy SCO support */
144 	{ USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
145 
146 	/* Belkin F8T012 and F8T013 devices */
147 	{ USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_RESET | BTUSB_WRONG_SCO_MTU },
148 	{ USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_RESET | BTUSB_WRONG_SCO_MTU },
149 
150 	/* Digianswer devices */
151 	{ USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
152 	{ USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
153 
154 	/* CSR BlueCore Bluetooth Sniffer */
155 	{ USB_DEVICE(0x0a12, 0x0002), .driver_info = BTUSB_SNIFFER },
156 
157 	/* Frontline ComProbe Bluetooth Sniffer */
158 	{ USB_DEVICE(0x16d3, 0x0002), .driver_info = BTUSB_SNIFFER },
159 
160 	{ }	/* Terminating entry */
161 };
162 
163 #define BTUSB_MAX_ISOC_FRAMES	10
164 
165 #define BTUSB_INTR_RUNNING	0
166 #define BTUSB_BULK_RUNNING	1
167 #define BTUSB_ISOC_RUNNING	2
168 
169 struct btusb_data {
170 	struct hci_dev       *hdev;
171 	struct usb_device    *udev;
172 	struct usb_interface *isoc;
173 
174 	spinlock_t lock;
175 
176 	unsigned long flags;
177 
178 	struct work_struct work;
179 
180 	struct usb_anchor tx_anchor;
181 	struct usb_anchor intr_anchor;
182 	struct usb_anchor bulk_anchor;
183 	struct usb_anchor isoc_anchor;
184 
185 	struct usb_endpoint_descriptor *intr_ep;
186 	struct usb_endpoint_descriptor *bulk_tx_ep;
187 	struct usb_endpoint_descriptor *bulk_rx_ep;
188 	struct usb_endpoint_descriptor *isoc_tx_ep;
189 	struct usb_endpoint_descriptor *isoc_rx_ep;
190 
191 	int isoc_altsetting;
192 };
193 
194 static void btusb_intr_complete(struct urb *urb)
195 {
196 	struct hci_dev *hdev = urb->context;
197 	struct btusb_data *data = hdev->driver_data;
198 	int err;
199 
200 	BT_DBG("%s urb %p status %d count %d", hdev->name,
201 					urb, urb->status, urb->actual_length);
202 
203 	if (!test_bit(HCI_RUNNING, &hdev->flags))
204 		return;
205 
206 	if (urb->status == 0) {
207 		hdev->stat.byte_rx += urb->actual_length;
208 
209 		if (hci_recv_fragment(hdev, HCI_EVENT_PKT,
210 						urb->transfer_buffer,
211 						urb->actual_length) < 0) {
212 			BT_ERR("%s corrupted event packet", hdev->name);
213 			hdev->stat.err_rx++;
214 		}
215 	}
216 
217 	if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
218 		return;
219 
220 	usb_anchor_urb(urb, &data->intr_anchor);
221 
222 	err = usb_submit_urb(urb, GFP_ATOMIC);
223 	if (err < 0) {
224 		BT_ERR("%s urb %p failed to resubmit (%d)",
225 						hdev->name, urb, -err);
226 		usb_unanchor_urb(urb);
227 	}
228 }
229 
230 static int btusb_submit_intr_urb(struct hci_dev *hdev)
231 {
232 	struct btusb_data *data = hdev->driver_data;
233 	struct urb *urb;
234 	unsigned char *buf;
235 	unsigned int pipe;
236 	int err, size;
237 
238 	BT_DBG("%s", hdev->name);
239 
240 	if (!data->intr_ep)
241 		return -ENODEV;
242 
243 	urb = usb_alloc_urb(0, GFP_ATOMIC);
244 	if (!urb)
245 		return -ENOMEM;
246 
247 	size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
248 
249 	buf = kmalloc(size, GFP_ATOMIC);
250 	if (!buf) {
251 		usb_free_urb(urb);
252 		return -ENOMEM;
253 	}
254 
255 	pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
256 
257 	usb_fill_int_urb(urb, data->udev, pipe, buf, size,
258 						btusb_intr_complete, hdev,
259 						data->intr_ep->bInterval);
260 
261 	urb->transfer_flags |= URB_FREE_BUFFER;
262 
263 	usb_anchor_urb(urb, &data->intr_anchor);
264 
265 	err = usb_submit_urb(urb, GFP_ATOMIC);
266 	if (err < 0) {
267 		BT_ERR("%s urb %p submission failed (%d)",
268 						hdev->name, urb, -err);
269 		usb_unanchor_urb(urb);
270 		kfree(buf);
271 	}
272 
273 	usb_free_urb(urb);
274 
275 	return err;
276 }
277 
278 static void btusb_bulk_complete(struct urb *urb)
279 {
280 	struct hci_dev *hdev = urb->context;
281 	struct btusb_data *data = hdev->driver_data;
282 	int err;
283 
284 	BT_DBG("%s urb %p status %d count %d", hdev->name,
285 					urb, urb->status, urb->actual_length);
286 
287 	if (!test_bit(HCI_RUNNING, &hdev->flags))
288 		return;
289 
290 	if (urb->status == 0) {
291 		hdev->stat.byte_rx += urb->actual_length;
292 
293 		if (hci_recv_fragment(hdev, HCI_ACLDATA_PKT,
294 						urb->transfer_buffer,
295 						urb->actual_length) < 0) {
296 			BT_ERR("%s corrupted ACL packet", hdev->name);
297 			hdev->stat.err_rx++;
298 		}
299 	}
300 
301 	if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
302 		return;
303 
304 	usb_anchor_urb(urb, &data->bulk_anchor);
305 
306 	err = usb_submit_urb(urb, GFP_ATOMIC);
307 	if (err < 0) {
308 		BT_ERR("%s urb %p failed to resubmit (%d)",
309 						hdev->name, urb, -err);
310 		usb_unanchor_urb(urb);
311 	}
312 }
313 
314 static int btusb_submit_bulk_urb(struct hci_dev *hdev)
315 {
316 	struct btusb_data *data = hdev->driver_data;
317 	struct urb *urb;
318 	unsigned char *buf;
319 	unsigned int pipe;
320 	int err, size;
321 
322 	BT_DBG("%s", hdev->name);
323 
324 	if (!data->bulk_rx_ep)
325 		return -ENODEV;
326 
327 	urb = usb_alloc_urb(0, GFP_KERNEL);
328 	if (!urb)
329 		return -ENOMEM;
330 
331 	size = le16_to_cpu(data->bulk_rx_ep->wMaxPacketSize);
332 
333 	buf = kmalloc(size, GFP_KERNEL);
334 	if (!buf) {
335 		usb_free_urb(urb);
336 		return -ENOMEM;
337 	}
338 
339 	pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
340 
341 	usb_fill_bulk_urb(urb, data->udev, pipe,
342 					buf, size, btusb_bulk_complete, hdev);
343 
344 	urb->transfer_flags |= URB_FREE_BUFFER;
345 
346 	usb_anchor_urb(urb, &data->bulk_anchor);
347 
348 	err = usb_submit_urb(urb, GFP_KERNEL);
349 	if (err < 0) {
350 		BT_ERR("%s urb %p submission failed (%d)",
351 						hdev->name, urb, -err);
352 		usb_unanchor_urb(urb);
353 		kfree(buf);
354 	}
355 
356 	usb_free_urb(urb);
357 
358 	return err;
359 }
360 
361 static void btusb_isoc_complete(struct urb *urb)
362 {
363 	struct hci_dev *hdev = urb->context;
364 	struct btusb_data *data = hdev->driver_data;
365 	int i, err;
366 
367 	BT_DBG("%s urb %p status %d count %d", hdev->name,
368 					urb, urb->status, urb->actual_length);
369 
370 	if (!test_bit(HCI_RUNNING, &hdev->flags))
371 		return;
372 
373 	if (urb->status == 0) {
374 		for (i = 0; i < urb->number_of_packets; i++) {
375 			unsigned int offset = urb->iso_frame_desc[i].offset;
376 			unsigned int length = urb->iso_frame_desc[i].actual_length;
377 
378 			if (urb->iso_frame_desc[i].status)
379 				continue;
380 
381 			hdev->stat.byte_rx += length;
382 
383 			if (hci_recv_fragment(hdev, HCI_SCODATA_PKT,
384 						urb->transfer_buffer + offset,
385 								length) < 0) {
386 				BT_ERR("%s corrupted SCO packet", hdev->name);
387 				hdev->stat.err_rx++;
388 			}
389 		}
390 	}
391 
392 	if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
393 		return;
394 
395 	usb_anchor_urb(urb, &data->isoc_anchor);
396 
397 	err = usb_submit_urb(urb, GFP_ATOMIC);
398 	if (err < 0) {
399 		BT_ERR("%s urb %p failed to resubmit (%d)",
400 						hdev->name, urb, -err);
401 		usb_unanchor_urb(urb);
402 	}
403 }
404 
405 static void inline __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
406 {
407 	int i, offset = 0;
408 
409 	BT_DBG("len %d mtu %d", len, mtu);
410 
411 	for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
412 					i++, offset += mtu, len -= mtu) {
413 		urb->iso_frame_desc[i].offset = offset;
414 		urb->iso_frame_desc[i].length = mtu;
415 	}
416 
417 	if (len && i < BTUSB_MAX_ISOC_FRAMES) {
418 		urb->iso_frame_desc[i].offset = offset;
419 		urb->iso_frame_desc[i].length = len;
420 		i++;
421 	}
422 
423 	urb->number_of_packets = i;
424 }
425 
426 static int btusb_submit_isoc_urb(struct hci_dev *hdev)
427 {
428 	struct btusb_data *data = hdev->driver_data;
429 	struct urb *urb;
430 	unsigned char *buf;
431 	unsigned int pipe;
432 	int err, size;
433 
434 	BT_DBG("%s", hdev->name);
435 
436 	if (!data->isoc_rx_ep)
437 		return -ENODEV;
438 
439 	urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
440 	if (!urb)
441 		return -ENOMEM;
442 
443 	size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
444 						BTUSB_MAX_ISOC_FRAMES;
445 
446 	buf = kmalloc(size, GFP_KERNEL);
447 	if (!buf) {
448 		usb_free_urb(urb);
449 		return -ENOMEM;
450 	}
451 
452 	pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
453 
454 	urb->dev      = data->udev;
455 	urb->pipe     = pipe;
456 	urb->context  = hdev;
457 	urb->complete = btusb_isoc_complete;
458 	urb->interval = data->isoc_rx_ep->bInterval;
459 
460 	urb->transfer_flags  = URB_FREE_BUFFER | URB_ISO_ASAP;
461 	urb->transfer_buffer = buf;
462 	urb->transfer_buffer_length = size;
463 
464 	__fill_isoc_descriptor(urb, size,
465 			le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
466 
467 	usb_anchor_urb(urb, &data->isoc_anchor);
468 
469 	err = usb_submit_urb(urb, GFP_KERNEL);
470 	if (err < 0) {
471 		BT_ERR("%s urb %p submission failed (%d)",
472 						hdev->name, urb, -err);
473 		usb_unanchor_urb(urb);
474 		kfree(buf);
475 	}
476 
477 	usb_free_urb(urb);
478 
479 	return err;
480 }
481 
482 static void btusb_tx_complete(struct urb *urb)
483 {
484 	struct sk_buff *skb = urb->context;
485 	struct hci_dev *hdev = (struct hci_dev *) skb->dev;
486 
487 	BT_DBG("%s urb %p status %d count %d", hdev->name,
488 					urb, urb->status, urb->actual_length);
489 
490 	if (!test_bit(HCI_RUNNING, &hdev->flags))
491 		goto done;
492 
493 	if (!urb->status)
494 		hdev->stat.byte_tx += urb->transfer_buffer_length;
495 	else
496 		hdev->stat.err_tx++;
497 
498 done:
499 	kfree(urb->setup_packet);
500 
501 	kfree_skb(skb);
502 }
503 
504 static int btusb_open(struct hci_dev *hdev)
505 {
506 	struct btusb_data *data = hdev->driver_data;
507 	int err;
508 
509 	BT_DBG("%s", hdev->name);
510 
511 	if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
512 		return 0;
513 
514 	if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
515 		return 0;
516 
517 	err = btusb_submit_intr_urb(hdev);
518 	if (err < 0) {
519 		clear_bit(BTUSB_INTR_RUNNING, &hdev->flags);
520 		clear_bit(HCI_RUNNING, &hdev->flags);
521 	}
522 
523 	return err;
524 }
525 
526 static int btusb_close(struct hci_dev *hdev)
527 {
528 	struct btusb_data *data = hdev->driver_data;
529 
530 	BT_DBG("%s", hdev->name);
531 
532 	if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
533 		return 0;
534 
535 	clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
536 	usb_kill_anchored_urbs(&data->intr_anchor);
537 
538 	clear_bit(BTUSB_BULK_RUNNING, &data->flags);
539 	usb_kill_anchored_urbs(&data->bulk_anchor);
540 
541 	clear_bit(BTUSB_INTR_RUNNING, &data->flags);
542 	usb_kill_anchored_urbs(&data->intr_anchor);
543 
544 	return 0;
545 }
546 
547 static int btusb_flush(struct hci_dev *hdev)
548 {
549 	struct btusb_data *data = hdev->driver_data;
550 
551 	BT_DBG("%s", hdev->name);
552 
553 	usb_kill_anchored_urbs(&data->tx_anchor);
554 
555 	return 0;
556 }
557 
558 static int btusb_send_frame(struct sk_buff *skb)
559 {
560 	struct hci_dev *hdev = (struct hci_dev *) skb->dev;
561 	struct btusb_data *data = hdev->driver_data;
562 	struct usb_ctrlrequest *dr;
563 	struct urb *urb;
564 	unsigned int pipe;
565 	int err;
566 
567 	BT_DBG("%s", hdev->name);
568 
569 	if (!test_bit(HCI_RUNNING, &hdev->flags))
570 		return -EBUSY;
571 
572 	switch (bt_cb(skb)->pkt_type) {
573 	case HCI_COMMAND_PKT:
574 		urb = usb_alloc_urb(0, GFP_ATOMIC);
575 		if (!urb)
576 			return -ENOMEM;
577 
578 		dr = kmalloc(sizeof(*dr), GFP_ATOMIC);
579 		if (!dr) {
580 			usb_free_urb(urb);
581 			return -ENOMEM;
582 		}
583 
584 		dr->bRequestType = USB_TYPE_CLASS;
585 		dr->bRequest     = 0;
586 		dr->wIndex       = 0;
587 		dr->wValue       = 0;
588 		dr->wLength      = __cpu_to_le16(skb->len);
589 
590 		pipe = usb_sndctrlpipe(data->udev, 0x00);
591 
592 		usb_fill_control_urb(urb, data->udev, pipe, (void *) dr,
593 				skb->data, skb->len, btusb_tx_complete, skb);
594 
595 		hdev->stat.cmd_tx++;
596 		break;
597 
598 	case HCI_ACLDATA_PKT:
599 		if (!data->bulk_tx_ep || hdev->conn_hash.acl_num < 1)
600 			return -ENODEV;
601 
602 		urb = usb_alloc_urb(0, GFP_ATOMIC);
603 		if (!urb)
604 			return -ENOMEM;
605 
606 		pipe = usb_sndbulkpipe(data->udev,
607 					data->bulk_tx_ep->bEndpointAddress);
608 
609 		usb_fill_bulk_urb(urb, data->udev, pipe,
610 				skb->data, skb->len, btusb_tx_complete, skb);
611 
612 		hdev->stat.acl_tx++;
613 		break;
614 
615 	case HCI_SCODATA_PKT:
616 		if (!data->isoc_tx_ep || hdev->conn_hash.sco_num < 1)
617 			return -ENODEV;
618 
619 		urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_ATOMIC);
620 		if (!urb)
621 			return -ENOMEM;
622 
623 		pipe = usb_sndisocpipe(data->udev,
624 					data->isoc_tx_ep->bEndpointAddress);
625 
626 		urb->dev      = data->udev;
627 		urb->pipe     = pipe;
628 		urb->context  = skb;
629 		urb->complete = btusb_tx_complete;
630 		urb->interval = data->isoc_tx_ep->bInterval;
631 
632 		urb->transfer_flags  = URB_ISO_ASAP;
633 		urb->transfer_buffer = skb->data;
634 		urb->transfer_buffer_length = skb->len;
635 
636 		__fill_isoc_descriptor(urb, skb->len,
637 				le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
638 
639 		hdev->stat.sco_tx++;
640 		break;
641 
642 	default:
643 		return -EILSEQ;
644 	}
645 
646 	usb_anchor_urb(urb, &data->tx_anchor);
647 
648 	err = usb_submit_urb(urb, GFP_ATOMIC);
649 	if (err < 0) {
650 		BT_ERR("%s urb %p submission failed", hdev->name, urb);
651 		kfree(urb->setup_packet);
652 		usb_unanchor_urb(urb);
653 	}
654 
655 	usb_free_urb(urb);
656 
657 	return err;
658 }
659 
660 static void btusb_destruct(struct hci_dev *hdev)
661 {
662 	struct btusb_data *data = hdev->driver_data;
663 
664 	BT_DBG("%s", hdev->name);
665 
666 	kfree(data);
667 }
668 
669 static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
670 {
671 	struct btusb_data *data = hdev->driver_data;
672 
673 	BT_DBG("%s evt %d", hdev->name, evt);
674 
675 	if (evt == HCI_NOTIFY_CONN_ADD || evt == HCI_NOTIFY_CONN_DEL)
676 		schedule_work(&data->work);
677 }
678 
679 static int inline __set_isoc_interface(struct hci_dev *hdev, int altsetting)
680 {
681 	struct btusb_data *data = hdev->driver_data;
682 	struct usb_interface *intf = data->isoc;
683 	struct usb_endpoint_descriptor *ep_desc;
684 	int i, err;
685 
686 	if (!data->isoc)
687 		return -ENODEV;
688 
689 	err = usb_set_interface(data->udev, 1, altsetting);
690 	if (err < 0) {
691 		BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
692 		return err;
693 	}
694 
695 	data->isoc_altsetting = altsetting;
696 
697 	data->isoc_tx_ep = NULL;
698 	data->isoc_rx_ep = NULL;
699 
700 	for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
701 		ep_desc = &intf->cur_altsetting->endpoint[i].desc;
702 
703 		if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
704 			data->isoc_tx_ep = ep_desc;
705 			continue;
706 		}
707 
708 		if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
709 			data->isoc_rx_ep = ep_desc;
710 			continue;
711 		}
712 	}
713 
714 	if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
715 		BT_ERR("%s invalid SCO descriptors", hdev->name);
716 		return -ENODEV;
717 	}
718 
719 	return 0;
720 }
721 
722 static void btusb_work(struct work_struct *work)
723 {
724 	struct btusb_data *data = container_of(work, struct btusb_data, work);
725 	struct hci_dev *hdev = data->hdev;
726 
727 	if (hdev->conn_hash.acl_num > 0) {
728 		if (!test_and_set_bit(BTUSB_BULK_RUNNING, &data->flags)) {
729 			if (btusb_submit_bulk_urb(hdev) < 0)
730 				clear_bit(BTUSB_BULK_RUNNING, &data->flags);
731 			else
732 				btusb_submit_bulk_urb(hdev);
733 		}
734 	} else {
735 		clear_bit(BTUSB_BULK_RUNNING, &data->flags);
736 		usb_kill_anchored_urbs(&data->bulk_anchor);
737 	}
738 
739 	if (hdev->conn_hash.sco_num > 0) {
740 		if (data->isoc_altsetting != 2) {
741 			clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
742 			usb_kill_anchored_urbs(&data->isoc_anchor);
743 
744 			if (__set_isoc_interface(hdev, 2) < 0)
745 				return;
746 		}
747 
748 		if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
749 			if (btusb_submit_isoc_urb(hdev) < 0)
750 				clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
751 			else
752 				btusb_submit_isoc_urb(hdev);
753 		}
754 	} else {
755 		clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
756 		usb_kill_anchored_urbs(&data->isoc_anchor);
757 
758 		__set_isoc_interface(hdev, 0);
759 	}
760 }
761 
762 static int btusb_probe(struct usb_interface *intf,
763 				const struct usb_device_id *id)
764 {
765 	struct usb_endpoint_descriptor *ep_desc;
766 	struct btusb_data *data;
767 	struct hci_dev *hdev;
768 	int i, err;
769 
770 	BT_DBG("intf %p id %p", intf, id);
771 
772 	/* interface numbers are hardcoded in the spec */
773 	if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
774 		return -ENODEV;
775 
776 	if (!id->driver_info) {
777 		const struct usb_device_id *match;
778 		match = usb_match_id(intf, blacklist_table);
779 		if (match)
780 			id = match;
781 	}
782 
783 	if (id->driver_info == BTUSB_IGNORE)
784 		return -ENODEV;
785 
786 	if (ignore_dga && id->driver_info & BTUSB_DIGIANSWER)
787 		return -ENODEV;
788 
789 	if (ignore_csr && id->driver_info & BTUSB_CSR)
790 		return -ENODEV;
791 
792 	if (ignore_sniffer && id->driver_info & BTUSB_SNIFFER)
793 		return -ENODEV;
794 
795 	data = kzalloc(sizeof(*data), GFP_KERNEL);
796 	if (!data)
797 		return -ENOMEM;
798 
799 	for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
800 		ep_desc = &intf->cur_altsetting->endpoint[i].desc;
801 
802 		if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
803 			data->intr_ep = ep_desc;
804 			continue;
805 		}
806 
807 		if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
808 			data->bulk_tx_ep = ep_desc;
809 			continue;
810 		}
811 
812 		if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
813 			data->bulk_rx_ep = ep_desc;
814 			continue;
815 		}
816 	}
817 
818 	if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep) {
819 		kfree(data);
820 		return -ENODEV;
821 	}
822 
823 	data->udev = interface_to_usbdev(intf);
824 
825 	spin_lock_init(&data->lock);
826 
827 	INIT_WORK(&data->work, btusb_work);
828 
829 	init_usb_anchor(&data->tx_anchor);
830 	init_usb_anchor(&data->intr_anchor);
831 	init_usb_anchor(&data->bulk_anchor);
832 	init_usb_anchor(&data->isoc_anchor);
833 
834 	hdev = hci_alloc_dev();
835 	if (!hdev) {
836 		kfree(data);
837 		return -ENOMEM;
838 	}
839 
840 	hdev->type = HCI_USB;
841 	hdev->driver_data = data;
842 
843 	data->hdev = hdev;
844 
845 	SET_HCIDEV_DEV(hdev, &intf->dev);
846 
847 	hdev->open     = btusb_open;
848 	hdev->close    = btusb_close;
849 	hdev->flush    = btusb_flush;
850 	hdev->send     = btusb_send_frame;
851 	hdev->destruct = btusb_destruct;
852 	hdev->notify   = btusb_notify;
853 
854 	hdev->owner = THIS_MODULE;
855 
856 	/* interface numbers are hardcoded in the spec */
857 	data->isoc = usb_ifnum_to_if(data->udev, 1);
858 
859 	if (reset || id->driver_info & BTUSB_RESET)
860 		set_bit(HCI_QUIRK_RESET_ON_INIT, &hdev->quirks);
861 
862 	if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
863 		if (!disable_scofix)
864 			set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
865 	}
866 
867 	if (id->driver_info & BTUSB_BROKEN_ISOC)
868 		data->isoc = NULL;
869 
870 	if (id->driver_info & BTUSB_SNIFFER) {
871 		struct usb_device *udev = data->udev;
872 
873 		if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
874 			set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
875 
876 		data->isoc = NULL;
877 	}
878 
879 	if (id->driver_info & BTUSB_BCM92035) {
880 		unsigned char cmd[] = { 0x3b, 0xfc, 0x01, 0x00 };
881 		struct sk_buff *skb;
882 
883 		skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
884 		if (skb) {
885 			memcpy(skb_put(skb, sizeof(cmd)), cmd, sizeof(cmd));
886 			skb_queue_tail(&hdev->driver_init, skb);
887 		}
888 	}
889 
890 	if (data->isoc) {
891 		err = usb_driver_claim_interface(&btusb_driver,
892 							data->isoc, NULL);
893 		if (err < 0) {
894 			hci_free_dev(hdev);
895 			kfree(data);
896 			return err;
897 		}
898 	}
899 
900 	err = hci_register_dev(hdev);
901 	if (err < 0) {
902 		hci_free_dev(hdev);
903 		kfree(data);
904 		return err;
905 	}
906 
907 	usb_set_intfdata(intf, data);
908 
909 	return 0;
910 }
911 
912 static void btusb_disconnect(struct usb_interface *intf)
913 {
914 	struct btusb_data *data = usb_get_intfdata(intf);
915 	struct hci_dev *hdev;
916 
917 	BT_DBG("intf %p", intf);
918 
919 	if (!data)
920 		return;
921 
922 	hdev = data->hdev;
923 
924 	if (data->isoc)
925 		usb_driver_release_interface(&btusb_driver, data->isoc);
926 
927 	usb_set_intfdata(intf, NULL);
928 
929 	hci_unregister_dev(hdev);
930 
931 	hci_free_dev(hdev);
932 }
933 
934 static struct usb_driver btusb_driver = {
935 	.name		= "btusb",
936 	.probe		= btusb_probe,
937 	.disconnect	= btusb_disconnect,
938 	.id_table	= btusb_table,
939 };
940 
941 static int __init btusb_init(void)
942 {
943 	BT_INFO("Generic Bluetooth USB driver ver %s", VERSION);
944 
945 	return usb_register(&btusb_driver);
946 }
947 
948 static void __exit btusb_exit(void)
949 {
950 	usb_deregister(&btusb_driver);
951 }
952 
953 module_init(btusb_init);
954 module_exit(btusb_exit);
955 
956 module_param(ignore_dga, bool, 0644);
957 MODULE_PARM_DESC(ignore_dga, "Ignore devices with id 08fd:0001");
958 
959 module_param(ignore_csr, bool, 0644);
960 MODULE_PARM_DESC(ignore_csr, "Ignore devices with id 0a12:0001");
961 
962 module_param(ignore_sniffer, bool, 0644);
963 MODULE_PARM_DESC(ignore_sniffer, "Ignore devices with id 0a12:0002");
964 
965 module_param(disable_scofix, bool, 0644);
966 MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
967 
968 module_param(force_scofix, bool, 0644);
969 MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
970 
971 module_param(reset, bool, 0644);
972 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
973 
974 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
975 MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
976 MODULE_VERSION(VERSION);
977 MODULE_LICENSE("GPL");
978