Bluetooth: hidp: verify l2cap sockets
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / bluetooth / btusb.c
CommitLineData
5e23b923
MH
1/*
2 *
3 * Generic Bluetooth USB driver
4 *
9bfa35fe 5 * Copyright (C) 2005-2008 Marcel Holtmann <marcel@holtmann.org>
5e23b923
MH
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
5e23b923 24#include <linux/module.h>
5e23b923
MH
25#include <linux/usb.h>
26
27#include <net/bluetooth/bluetooth.h>
28#include <net/bluetooth/hci_core.h>
29
7bee549e 30#define VERSION "0.6"
cfeb4145 31
90ab5ee9
RR
32static bool ignore_dga;
33static bool ignore_csr;
34static bool ignore_sniffer;
35static bool disable_scofix;
36static bool force_scofix;
7a9d4020 37
90ab5ee9 38static bool reset = 1;
cfeb4145
MH
39
40static struct usb_driver btusb_driver;
41
42#define BTUSB_IGNORE 0x01
7a9d4020
MH
43#define BTUSB_DIGIANSWER 0x02
44#define BTUSB_CSR 0x04
45#define BTUSB_SNIFFER 0x08
46#define BTUSB_BCM92035 0x10
47#define BTUSB_BROKEN_ISOC 0x20
48#define BTUSB_WRONG_SCO_MTU 0x40
2d25f8b4 49#define BTUSB_ATH3012 0x80
5e23b923
MH
50
51static struct usb_device_id btusb_table[] = {
52 /* Generic Bluetooth USB device */
53 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
54
1fa6535f
HR
55 /* Apple-specific (Broadcom) devices */
56 { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01) },
57
c510eae3 58 /* Broadcom SoftSailing reporting vendor specific */
2e8b5063 59 { USB_DEVICE(0x0a5c, 0x21e1) },
c510eae3 60
3cd01976
NI
61 /* Apple MacBookPro 7,1 */
62 { USB_DEVICE(0x05ac, 0x8213) },
63
0a79f674
CL
64 /* Apple iMac11,1 */
65 { USB_DEVICE(0x05ac, 0x8215) },
66
9c047157
NI
67 /* Apple MacBookPro6,2 */
68 { USB_DEVICE(0x05ac, 0x8218) },
69
3e3ede7d
EH
70 /* Apple MacBookAir3,1, MacBookAir3,2 */
71 { USB_DEVICE(0x05ac, 0x821b) },
72
a63b723d
PAVM
73 /* Apple MacBookAir4,1 */
74 { USB_DEVICE(0x05ac, 0x821f) },
75
88d377b6
MAP
76 /* Apple MacBookPro8,2 */
77 { USB_DEVICE(0x05ac, 0x821a) },
78
f78b6826
JK
79 /* Apple MacMini5,1 */
80 { USB_DEVICE(0x05ac, 0x8281) },
81
cfeb4145
MH
82 /* AVM BlueFRITZ! USB v2.0 */
83 { USB_DEVICE(0x057c, 0x3800) },
84
85 /* Bluetooth Ultraport Module from IBM */
86 { USB_DEVICE(0x04bf, 0x030a) },
87
88 /* ALPS Modules with non-standard id */
89 { USB_DEVICE(0x044e, 0x3001) },
90 { USB_DEVICE(0x044e, 0x3002) },
91
92 /* Ericsson with non-standard id */
93 { USB_DEVICE(0x0bdb, 0x1002) },
94
95 /* Canyon CN-BTU1 with HID interfaces */
7a9d4020 96 { USB_DEVICE(0x0c10, 0x0000) },
cfeb4145 97
d13431ca 98 /* Broadcom BCM20702A0 */
1ee3ff61 99 { USB_DEVICE(0x0b05, 0x17b5) },
0c1abbd1 100 { USB_DEVICE(0x04ca, 0x2003) },
79cd7602 101 { USB_DEVICE(0x0489, 0xe042) },
d13431ca
WJS
102 { USB_DEVICE(0x413c, 0x8197) },
103
98514036 104 /* Foxconn - Hon Hai */
ee66401b 105 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01) },
98514036 106
92c385f4
GP
107 /*Broadcom devices with vendor specific id */
108 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01) },
109
5e23b923
MH
110 { } /* Terminating entry */
111};
112
113MODULE_DEVICE_TABLE(usb, btusb_table);
114
115static struct usb_device_id blacklist_table[] = {
cfeb4145
MH
116 /* CSR BlueCore devices */
117 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
118
119 /* Broadcom BCM2033 without firmware */
120 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
121
be93112a
BS
122 /* Atheros 3011 with sflash firmware */
123 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
6eda541d 124 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
2a7bcccc 125 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
8e7c3d2e 126 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
6b6ba88b 127 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
acd94544 128 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
be93112a 129
509e7861
CYC
130 /* Atheros AR9285 Malbec with sflash firmware */
131 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
132
d9f51b51 133 /* Atheros 3012 with sflash firmware */
d66629c1 134 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
2d25f8b4 135 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
94a32d10 136 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
07c0ea87 137 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
ebaf5795 138 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
9498ba7a 139 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
3f63c340 140 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
55ed7d4d 141 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
2c262b2a 142 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
f4d6f7dc 143 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
87522a43 144 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
ac71311e 145 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
6c4ae5c2 146 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
2096ae6c 147 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
eed307e2 148 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
3a61eda8 149 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
2582d529 150 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
0fc110f4 151 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
d9f51b51 152
e9036e33
CYC
153 /* Atheros AR5BBU12 with sflash firmware */
154 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
155
85d59726
MG
156 /* Atheros AR5BBU12 with sflash firmware */
157 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
bc21fde2 158 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
85d59726 159
cfeb4145 160 /* Broadcom BCM2035 */
7a9d4020
MH
161 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
162 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
163 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
cfeb4145
MH
164
165 /* Broadcom BCM2045 */
7a9d4020
MH
166 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
167 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
bdbef3d6 168
cfeb4145 169 /* IBM/Lenovo ThinkPad with Broadcom chip */
7a9d4020
MH
170 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
171 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
cfeb4145
MH
172
173 /* HP laptop with Broadcom chip */
7a9d4020 174 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
cfeb4145
MH
175
176 /* Dell laptop with Broadcom chip */
7a9d4020 177 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
cfeb4145 178
5ddd4a60 179 /* Dell Wireless 370 and 410 devices */
7a9d4020 180 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
5ddd4a60 181 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
cfeb4145 182
7a9d4020
MH
183 /* Belkin F8T012 and F8T013 devices */
184 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
185 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
cfeb4145 186
5ddd4a60
MH
187 /* Asus WL-BTD202 device */
188 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
189
190 /* Kensington Bluetooth USB adapter */
191 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
192
cfeb4145
MH
193 /* RTX Telecom based adapters with buggy SCO support */
194 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
195 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
196
197 /* CONWISE Technology based adapters with buggy SCO support */
198 { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
199
cfeb4145
MH
200 /* Digianswer devices */
201 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
202 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
203
204 /* CSR BlueCore Bluetooth Sniffer */
205 { USB_DEVICE(0x0a12, 0x0002), .driver_info = BTUSB_SNIFFER },
206
207 /* Frontline ComProbe Bluetooth Sniffer */
208 { USB_DEVICE(0x16d3, 0x0002), .driver_info = BTUSB_SNIFFER },
209
5e23b923
MH
210 { } /* Terminating entry */
211};
212
9bfa35fe
MH
213#define BTUSB_MAX_ISOC_FRAMES 10
214
5e23b923
MH
215#define BTUSB_INTR_RUNNING 0
216#define BTUSB_BULK_RUNNING 1
9bfa35fe 217#define BTUSB_ISOC_RUNNING 2
7bee549e 218#define BTUSB_SUSPENDING 3
08b8b6c4 219#define BTUSB_DID_ISO_RESUME 4
5e23b923
MH
220
221struct btusb_data {
222 struct hci_dev *hdev;
223 struct usb_device *udev;
5fbcd260 224 struct usb_interface *intf;
9bfa35fe 225 struct usb_interface *isoc;
5e23b923
MH
226
227 spinlock_t lock;
228
229 unsigned long flags;
230
231 struct work_struct work;
7bee549e 232 struct work_struct waker;
5e23b923
MH
233
234 struct usb_anchor tx_anchor;
235 struct usb_anchor intr_anchor;
236 struct usb_anchor bulk_anchor;
9bfa35fe 237 struct usb_anchor isoc_anchor;
7bee549e
ON
238 struct usb_anchor deferred;
239 int tx_in_flight;
240 spinlock_t txlock;
5e23b923
MH
241
242 struct usb_endpoint_descriptor *intr_ep;
243 struct usb_endpoint_descriptor *bulk_tx_ep;
244 struct usb_endpoint_descriptor *bulk_rx_ep;
9bfa35fe
MH
245 struct usb_endpoint_descriptor *isoc_tx_ep;
246 struct usb_endpoint_descriptor *isoc_rx_ep;
247
7a9d4020 248 __u8 cmdreq_type;
93600997 249 unsigned long driver_info;
7a9d4020 250
43c2e57f 251 unsigned int sco_num;
9bfa35fe 252 int isoc_altsetting;
6a88adf2 253 int suspend_count;
5e23b923
MH
254};
255
7bee549e
ON
256static int inc_tx(struct btusb_data *data)
257{
258 unsigned long flags;
259 int rv;
260
261 spin_lock_irqsave(&data->txlock, flags);
262 rv = test_bit(BTUSB_SUSPENDING, &data->flags);
263 if (!rv)
264 data->tx_in_flight++;
265 spin_unlock_irqrestore(&data->txlock, flags);
266
267 return rv;
268}
269
5e23b923
MH
270static void btusb_intr_complete(struct urb *urb)
271{
272 struct hci_dev *hdev = urb->context;
155961e8 273 struct btusb_data *data = hci_get_drvdata(hdev);
5e23b923
MH
274 int err;
275
276 BT_DBG("%s urb %p status %d count %d", hdev->name,
277 urb, urb->status, urb->actual_length);
278
279 if (!test_bit(HCI_RUNNING, &hdev->flags))
280 return;
281
282 if (urb->status == 0) {
9bfa35fe
MH
283 hdev->stat.byte_rx += urb->actual_length;
284
5e23b923
MH
285 if (hci_recv_fragment(hdev, HCI_EVENT_PKT,
286 urb->transfer_buffer,
287 urb->actual_length) < 0) {
288 BT_ERR("%s corrupted event packet", hdev->name);
289 hdev->stat.err_rx++;
290 }
291 }
292
293 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
294 return;
295
7bee549e 296 usb_mark_last_busy(data->udev);
5e23b923
MH
297 usb_anchor_urb(urb, &data->intr_anchor);
298
299 err = usb_submit_urb(urb, GFP_ATOMIC);
300 if (err < 0) {
4935f1c1
PB
301 /* -EPERM: urb is being killed;
302 * -ENODEV: device got disconnected */
303 if (err != -EPERM && err != -ENODEV)
61faddf6 304 BT_ERR("%s urb %p failed to resubmit (%d)",
5e23b923
MH
305 hdev->name, urb, -err);
306 usb_unanchor_urb(urb);
307 }
308}
309
2eda66f4 310static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
5e23b923 311{
155961e8 312 struct btusb_data *data = hci_get_drvdata(hdev);
5e23b923
MH
313 struct urb *urb;
314 unsigned char *buf;
315 unsigned int pipe;
316 int err, size;
317
318 BT_DBG("%s", hdev->name);
319
9bfa35fe
MH
320 if (!data->intr_ep)
321 return -ENODEV;
322
2eda66f4 323 urb = usb_alloc_urb(0, mem_flags);
5e23b923
MH
324 if (!urb)
325 return -ENOMEM;
326
327 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
328
2eda66f4 329 buf = kmalloc(size, mem_flags);
5e23b923
MH
330 if (!buf) {
331 usb_free_urb(urb);
332 return -ENOMEM;
333 }
334
335 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
336
337 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
338 btusb_intr_complete, hdev,
339 data->intr_ep->bInterval);
340
341 urb->transfer_flags |= URB_FREE_BUFFER;
342
343 usb_anchor_urb(urb, &data->intr_anchor);
344
2eda66f4 345 err = usb_submit_urb(urb, mem_flags);
5e23b923 346 if (err < 0) {
d4b8d1c9
PB
347 if (err != -EPERM && err != -ENODEV)
348 BT_ERR("%s urb %p submission failed (%d)",
5e23b923
MH
349 hdev->name, urb, -err);
350 usb_unanchor_urb(urb);
5e23b923
MH
351 }
352
353 usb_free_urb(urb);
354
355 return err;
356}
357
358static void btusb_bulk_complete(struct urb *urb)
359{
360 struct hci_dev *hdev = urb->context;
155961e8 361 struct btusb_data *data = hci_get_drvdata(hdev);
5e23b923
MH
362 int err;
363
364 BT_DBG("%s urb %p status %d count %d", hdev->name,
365 urb, urb->status, urb->actual_length);
366
367 if (!test_bit(HCI_RUNNING, &hdev->flags))
368 return;
369
370 if (urb->status == 0) {
9bfa35fe
MH
371 hdev->stat.byte_rx += urb->actual_length;
372
5e23b923
MH
373 if (hci_recv_fragment(hdev, HCI_ACLDATA_PKT,
374 urb->transfer_buffer,
375 urb->actual_length) < 0) {
376 BT_ERR("%s corrupted ACL packet", hdev->name);
377 hdev->stat.err_rx++;
378 }
379 }
380
381 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
382 return;
383
384 usb_anchor_urb(urb, &data->bulk_anchor);
652fd781 385 usb_mark_last_busy(data->udev);
5e23b923
MH
386
387 err = usb_submit_urb(urb, GFP_ATOMIC);
388 if (err < 0) {
4935f1c1
PB
389 /* -EPERM: urb is being killed;
390 * -ENODEV: device got disconnected */
391 if (err != -EPERM && err != -ENODEV)
61faddf6 392 BT_ERR("%s urb %p failed to resubmit (%d)",
5e23b923
MH
393 hdev->name, urb, -err);
394 usb_unanchor_urb(urb);
395 }
396}
397
2eda66f4 398static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
5e23b923 399{
155961e8 400 struct btusb_data *data = hci_get_drvdata(hdev);
5e23b923
MH
401 struct urb *urb;
402 unsigned char *buf;
403 unsigned int pipe;
290ba200 404 int err, size = HCI_MAX_FRAME_SIZE;
5e23b923
MH
405
406 BT_DBG("%s", hdev->name);
407
9bfa35fe
MH
408 if (!data->bulk_rx_ep)
409 return -ENODEV;
410
2eda66f4 411 urb = usb_alloc_urb(0, mem_flags);
5e23b923
MH
412 if (!urb)
413 return -ENOMEM;
414
2eda66f4 415 buf = kmalloc(size, mem_flags);
5e23b923
MH
416 if (!buf) {
417 usb_free_urb(urb);
418 return -ENOMEM;
419 }
420
421 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
422
423 usb_fill_bulk_urb(urb, data->udev, pipe,
424 buf, size, btusb_bulk_complete, hdev);
425
426 urb->transfer_flags |= URB_FREE_BUFFER;
427
7bee549e 428 usb_mark_last_busy(data->udev);
5e23b923
MH
429 usb_anchor_urb(urb, &data->bulk_anchor);
430
2eda66f4 431 err = usb_submit_urb(urb, mem_flags);
5e23b923 432 if (err < 0) {
d4b8d1c9
PB
433 if (err != -EPERM && err != -ENODEV)
434 BT_ERR("%s urb %p submission failed (%d)",
5e23b923
MH
435 hdev->name, urb, -err);
436 usb_unanchor_urb(urb);
5e23b923
MH
437 }
438
439 usb_free_urb(urb);
440
441 return err;
442}
443
9bfa35fe
MH
444static void btusb_isoc_complete(struct urb *urb)
445{
446 struct hci_dev *hdev = urb->context;
155961e8 447 struct btusb_data *data = hci_get_drvdata(hdev);
9bfa35fe
MH
448 int i, err;
449
450 BT_DBG("%s urb %p status %d count %d", hdev->name,
451 urb, urb->status, urb->actual_length);
452
453 if (!test_bit(HCI_RUNNING, &hdev->flags))
454 return;
455
456 if (urb->status == 0) {
457 for (i = 0; i < urb->number_of_packets; i++) {
458 unsigned int offset = urb->iso_frame_desc[i].offset;
459 unsigned int length = urb->iso_frame_desc[i].actual_length;
460
461 if (urb->iso_frame_desc[i].status)
462 continue;
463
464 hdev->stat.byte_rx += length;
465
466 if (hci_recv_fragment(hdev, HCI_SCODATA_PKT,
467 urb->transfer_buffer + offset,
468 length) < 0) {
469 BT_ERR("%s corrupted SCO packet", hdev->name);
470 hdev->stat.err_rx++;
471 }
472 }
473 }
474
475 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
476 return;
477
478 usb_anchor_urb(urb, &data->isoc_anchor);
479
480 err = usb_submit_urb(urb, GFP_ATOMIC);
481 if (err < 0) {
4935f1c1
PB
482 /* -EPERM: urb is being killed;
483 * -ENODEV: device got disconnected */
484 if (err != -EPERM && err != -ENODEV)
61faddf6 485 BT_ERR("%s urb %p failed to resubmit (%d)",
9bfa35fe
MH
486 hdev->name, urb, -err);
487 usb_unanchor_urb(urb);
488 }
489}
490
42b16b3f 491static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
9bfa35fe
MH
492{
493 int i, offset = 0;
494
495 BT_DBG("len %d mtu %d", len, mtu);
496
497 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
498 i++, offset += mtu, len -= mtu) {
499 urb->iso_frame_desc[i].offset = offset;
500 urb->iso_frame_desc[i].length = mtu;
501 }
502
503 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
504 urb->iso_frame_desc[i].offset = offset;
505 urb->iso_frame_desc[i].length = len;
506 i++;
507 }
508
509 urb->number_of_packets = i;
510}
511
2eda66f4 512static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
9bfa35fe 513{
155961e8 514 struct btusb_data *data = hci_get_drvdata(hdev);
9bfa35fe
MH
515 struct urb *urb;
516 unsigned char *buf;
517 unsigned int pipe;
518 int err, size;
519
520 BT_DBG("%s", hdev->name);
521
522 if (!data->isoc_rx_ep)
523 return -ENODEV;
524
2eda66f4 525 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
9bfa35fe
MH
526 if (!urb)
527 return -ENOMEM;
528
529 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
530 BTUSB_MAX_ISOC_FRAMES;
531
2eda66f4 532 buf = kmalloc(size, mem_flags);
9bfa35fe
MH
533 if (!buf) {
534 usb_free_urb(urb);
535 return -ENOMEM;
536 }
537
538 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
539
fa0fb93f
BZ
540 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
541 hdev, data->isoc_rx_ep->bInterval);
9bfa35fe
MH
542
543 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
9bfa35fe
MH
544
545 __fill_isoc_descriptor(urb, size,
546 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
547
548 usb_anchor_urb(urb, &data->isoc_anchor);
549
2eda66f4 550 err = usb_submit_urb(urb, mem_flags);
9bfa35fe 551 if (err < 0) {
d4b8d1c9
PB
552 if (err != -EPERM && err != -ENODEV)
553 BT_ERR("%s urb %p submission failed (%d)",
9bfa35fe
MH
554 hdev->name, urb, -err);
555 usb_unanchor_urb(urb);
9bfa35fe
MH
556 }
557
558 usb_free_urb(urb);
559
560 return err;
561}
562
5e23b923 563static void btusb_tx_complete(struct urb *urb)
7bee549e
ON
564{
565 struct sk_buff *skb = urb->context;
566 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
155961e8 567 struct btusb_data *data = hci_get_drvdata(hdev);
7bee549e
ON
568
569 BT_DBG("%s urb %p status %d count %d", hdev->name,
570 urb, urb->status, urb->actual_length);
571
572 if (!test_bit(HCI_RUNNING, &hdev->flags))
573 goto done;
574
575 if (!urb->status)
576 hdev->stat.byte_tx += urb->transfer_buffer_length;
577 else
578 hdev->stat.err_tx++;
579
580done:
581 spin_lock(&data->txlock);
582 data->tx_in_flight--;
583 spin_unlock(&data->txlock);
584
585 kfree(urb->setup_packet);
586
587 kfree_skb(skb);
588}
589
590static void btusb_isoc_tx_complete(struct urb *urb)
5e23b923
MH
591{
592 struct sk_buff *skb = urb->context;
593 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
594
595 BT_DBG("%s urb %p status %d count %d", hdev->name,
596 urb, urb->status, urb->actual_length);
597
598 if (!test_bit(HCI_RUNNING, &hdev->flags))
599 goto done;
600
601 if (!urb->status)
602 hdev->stat.byte_tx += urb->transfer_buffer_length;
603 else
604 hdev->stat.err_tx++;
605
606done:
607 kfree(urb->setup_packet);
608
609 kfree_skb(skb);
610}
611
612static int btusb_open(struct hci_dev *hdev)
613{
155961e8 614 struct btusb_data *data = hci_get_drvdata(hdev);
5e23b923
MH
615 int err;
616
617 BT_DBG("%s", hdev->name);
618
7bee549e
ON
619 err = usb_autopm_get_interface(data->intf);
620 if (err < 0)
621 return err;
622
623 data->intf->needs_remote_wakeup = 1;
624
5e23b923 625 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
7bee549e 626 goto done;
5e23b923
MH
627
628 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
7bee549e 629 goto done;
5e23b923 630
2eda66f4 631 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
43c2e57f
MH
632 if (err < 0)
633 goto failed;
634
635 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
5e23b923 636 if (err < 0) {
43c2e57f
MH
637 usb_kill_anchored_urbs(&data->intr_anchor);
638 goto failed;
5e23b923
MH
639 }
640
43c2e57f
MH
641 set_bit(BTUSB_BULK_RUNNING, &data->flags);
642 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
643
7bee549e
ON
644done:
645 usb_autopm_put_interface(data->intf);
43c2e57f
MH
646 return 0;
647
648failed:
649 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
650 clear_bit(HCI_RUNNING, &hdev->flags);
7bee549e 651 usb_autopm_put_interface(data->intf);
5e23b923
MH
652 return err;
653}
654
7bee549e
ON
655static void btusb_stop_traffic(struct btusb_data *data)
656{
657 usb_kill_anchored_urbs(&data->intr_anchor);
658 usb_kill_anchored_urbs(&data->bulk_anchor);
659 usb_kill_anchored_urbs(&data->isoc_anchor);
660}
661
5e23b923
MH
662static int btusb_close(struct hci_dev *hdev)
663{
155961e8 664 struct btusb_data *data = hci_get_drvdata(hdev);
7bee549e 665 int err;
5e23b923
MH
666
667 BT_DBG("%s", hdev->name);
668
669 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
670 return 0;
671
e8c3c3d2 672 cancel_work_sync(&data->work);
404291ac 673 cancel_work_sync(&data->waker);
e8c3c3d2 674
9bfa35fe 675 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
5e23b923 676 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
5e23b923 677 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
7bee549e
ON
678
679 btusb_stop_traffic(data);
680 err = usb_autopm_get_interface(data->intf);
681 if (err < 0)
7b8e2c1d 682 goto failed;
7bee549e
ON
683
684 data->intf->needs_remote_wakeup = 0;
685 usb_autopm_put_interface(data->intf);
5e23b923 686
7b8e2c1d
ON
687failed:
688 usb_scuttle_anchored_urbs(&data->deferred);
5e23b923
MH
689 return 0;
690}
691
692static int btusb_flush(struct hci_dev *hdev)
693{
155961e8 694 struct btusb_data *data = hci_get_drvdata(hdev);
5e23b923
MH
695
696 BT_DBG("%s", hdev->name);
697
698 usb_kill_anchored_urbs(&data->tx_anchor);
699
700 return 0;
701}
702
93600997
MH
703static int btusb_setup(struct hci_dev *hdev)
704{
705 struct btusb_data *data = hci_get_drvdata(hdev);
706
707 BT_DBG("%s", hdev->name);
708
709 if (data->driver_info & BTUSB_BCM92035) {
710 struct sk_buff *skb;
711 __u8 val = 0x00;
712
713 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
714 if (IS_ERR(skb))
715 BT_ERR("BCM92035 command failed (%ld)", -PTR_ERR(skb));
716 else
717 kfree_skb(skb);
718 }
719
720 return 0;
721}
722
5e23b923
MH
723static int btusb_send_frame(struct sk_buff *skb)
724{
725 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
155961e8 726 struct btusb_data *data = hci_get_drvdata(hdev);
5e23b923
MH
727 struct usb_ctrlrequest *dr;
728 struct urb *urb;
729 unsigned int pipe;
730 int err;
731
732 BT_DBG("%s", hdev->name);
733
734 if (!test_bit(HCI_RUNNING, &hdev->flags))
735 return -EBUSY;
736
737 switch (bt_cb(skb)->pkt_type) {
738 case HCI_COMMAND_PKT:
739 urb = usb_alloc_urb(0, GFP_ATOMIC);
740 if (!urb)
741 return -ENOMEM;
742
743 dr = kmalloc(sizeof(*dr), GFP_ATOMIC);
744 if (!dr) {
745 usb_free_urb(urb);
746 return -ENOMEM;
747 }
748
7a9d4020 749 dr->bRequestType = data->cmdreq_type;
5e23b923
MH
750 dr->bRequest = 0;
751 dr->wIndex = 0;
752 dr->wValue = 0;
753 dr->wLength = __cpu_to_le16(skb->len);
754
755 pipe = usb_sndctrlpipe(data->udev, 0x00);
756
757 usb_fill_control_urb(urb, data->udev, pipe, (void *) dr,
758 skb->data, skb->len, btusb_tx_complete, skb);
759
760 hdev->stat.cmd_tx++;
761 break;
762
763 case HCI_ACLDATA_PKT:
9fd481e0 764 if (!data->bulk_tx_ep)
9bfa35fe
MH
765 return -ENODEV;
766
5e23b923
MH
767 urb = usb_alloc_urb(0, GFP_ATOMIC);
768 if (!urb)
769 return -ENOMEM;
770
771 pipe = usb_sndbulkpipe(data->udev,
772 data->bulk_tx_ep->bEndpointAddress);
773
774 usb_fill_bulk_urb(urb, data->udev, pipe,
775 skb->data, skb->len, btusb_tx_complete, skb);
776
777 hdev->stat.acl_tx++;
778 break;
779
780 case HCI_SCODATA_PKT:
9bfa35fe
MH
781 if (!data->isoc_tx_ep || hdev->conn_hash.sco_num < 1)
782 return -ENODEV;
783
784 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_ATOMIC);
785 if (!urb)
786 return -ENOMEM;
787
788 pipe = usb_sndisocpipe(data->udev,
789 data->isoc_tx_ep->bEndpointAddress);
790
03c2d0e8
GP
791 usb_fill_int_urb(urb, data->udev, pipe,
792 skb->data, skb->len, btusb_isoc_tx_complete,
793 skb, data->isoc_tx_ep->bInterval);
9bfa35fe
MH
794
795 urb->transfer_flags = URB_ISO_ASAP;
9bfa35fe
MH
796
797 __fill_isoc_descriptor(urb, skb->len,
798 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
799
5e23b923 800 hdev->stat.sco_tx++;
7bee549e 801 goto skip_waking;
5e23b923
MH
802
803 default:
804 return -EILSEQ;
805 }
806
7bee549e
ON
807 err = inc_tx(data);
808 if (err) {
809 usb_anchor_urb(urb, &data->deferred);
810 schedule_work(&data->waker);
811 err = 0;
812 goto done;
813 }
814
815skip_waking:
5e23b923
MH
816 usb_anchor_urb(urb, &data->tx_anchor);
817
818 err = usb_submit_urb(urb, GFP_ATOMIC);
819 if (err < 0) {
5a9b80e2
PB
820 if (err != -EPERM && err != -ENODEV)
821 BT_ERR("%s urb %p submission failed (%d)",
822 hdev->name, urb, -err);
5e23b923
MH
823 kfree(urb->setup_packet);
824 usb_unanchor_urb(urb);
7bee549e
ON
825 } else {
826 usb_mark_last_busy(data->udev);
5e23b923
MH
827 }
828
7bee549e 829done:
54a8a79c 830 usb_free_urb(urb);
5e23b923
MH
831 return err;
832}
833
5e23b923
MH
834static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
835{
155961e8 836 struct btusb_data *data = hci_get_drvdata(hdev);
5e23b923
MH
837
838 BT_DBG("%s evt %d", hdev->name, evt);
839
43c2e57f
MH
840 if (hdev->conn_hash.sco_num != data->sco_num) {
841 data->sco_num = hdev->conn_hash.sco_num;
842 schedule_work(&data->work);
a780efa8 843 }
5e23b923
MH
844}
845
42b16b3f 846static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
9bfa35fe 847{
155961e8 848 struct btusb_data *data = hci_get_drvdata(hdev);
9bfa35fe
MH
849 struct usb_interface *intf = data->isoc;
850 struct usb_endpoint_descriptor *ep_desc;
851 int i, err;
852
853 if (!data->isoc)
854 return -ENODEV;
855
856 err = usb_set_interface(data->udev, 1, altsetting);
857 if (err < 0) {
858 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
859 return err;
860 }
861
862 data->isoc_altsetting = altsetting;
863
864 data->isoc_tx_ep = NULL;
865 data->isoc_rx_ep = NULL;
866
867 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
868 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
869
870 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
871 data->isoc_tx_ep = ep_desc;
872 continue;
873 }
874
875 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
876 data->isoc_rx_ep = ep_desc;
877 continue;
878 }
879 }
880
881 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
882 BT_ERR("%s invalid SCO descriptors", hdev->name);
883 return -ENODEV;
884 }
885
886 return 0;
887}
888
5e23b923
MH
889static void btusb_work(struct work_struct *work)
890{
891 struct btusb_data *data = container_of(work, struct btusb_data, work);
892 struct hci_dev *hdev = data->hdev;
f4001d28 893 int new_alts;
7bee549e 894 int err;
5e23b923 895
9bfa35fe 896 if (hdev->conn_hash.sco_num > 0) {
08b8b6c4 897 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
8efdd0cd 898 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
7bee549e
ON
899 if (err < 0) {
900 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
901 usb_kill_anchored_urbs(&data->isoc_anchor);
902 return;
903 }
904
08b8b6c4 905 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
7bee549e 906 }
f4001d28
MA
907
908 if (hdev->voice_setting & 0x0020) {
909 static const int alts[3] = { 2, 4, 5 };
910 new_alts = alts[hdev->conn_hash.sco_num - 1];
911 } else {
912 new_alts = hdev->conn_hash.sco_num;
913 }
914
915 if (data->isoc_altsetting != new_alts) {
9bfa35fe
MH
916 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
917 usb_kill_anchored_urbs(&data->isoc_anchor);
918
f4001d28 919 if (__set_isoc_interface(hdev, new_alts) < 0)
9bfa35fe
MH
920 return;
921 }
922
923 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
2eda66f4 924 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
9bfa35fe
MH
925 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
926 else
2eda66f4 927 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
9bfa35fe
MH
928 }
929 } else {
930 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
931 usb_kill_anchored_urbs(&data->isoc_anchor);
932
933 __set_isoc_interface(hdev, 0);
08b8b6c4 934 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
8efdd0cd 935 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
5e23b923
MH
936 }
937}
938
7bee549e
ON
939static void btusb_waker(struct work_struct *work)
940{
941 struct btusb_data *data = container_of(work, struct btusb_data, waker);
942 int err;
943
944 err = usb_autopm_get_interface(data->intf);
945 if (err < 0)
946 return;
947
948 usb_autopm_put_interface(data->intf);
949}
950
5e23b923
MH
951static int btusb_probe(struct usb_interface *intf,
952 const struct usb_device_id *id)
953{
954 struct usb_endpoint_descriptor *ep_desc;
955 struct btusb_data *data;
956 struct hci_dev *hdev;
957 int i, err;
958
959 BT_DBG("intf %p id %p", intf, id);
960
cfeb4145 961 /* interface numbers are hardcoded in the spec */
5e23b923
MH
962 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
963 return -ENODEV;
964
965 if (!id->driver_info) {
966 const struct usb_device_id *match;
967 match = usb_match_id(intf, blacklist_table);
968 if (match)
969 id = match;
970 }
971
cfeb4145
MH
972 if (id->driver_info == BTUSB_IGNORE)
973 return -ENODEV;
974
975 if (ignore_dga && id->driver_info & BTUSB_DIGIANSWER)
976 return -ENODEV;
977
978 if (ignore_csr && id->driver_info & BTUSB_CSR)
979 return -ENODEV;
980
981 if (ignore_sniffer && id->driver_info & BTUSB_SNIFFER)
982 return -ENODEV;
983
2d25f8b4
SL
984 if (id->driver_info & BTUSB_ATH3012) {
985 struct usb_device *udev = interface_to_usbdev(intf);
986
987 /* Old firmware would otherwise let ath3k driver load
988 * patch and sysconfig files */
989 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
990 return -ENODEV;
991 }
992
98921dbd 993 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
5e23b923
MH
994 if (!data)
995 return -ENOMEM;
996
997 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
998 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
999
1000 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
1001 data->intr_ep = ep_desc;
1002 continue;
1003 }
1004
1005 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
1006 data->bulk_tx_ep = ep_desc;
1007 continue;
1008 }
1009
1010 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
1011 data->bulk_rx_ep = ep_desc;
1012 continue;
1013 }
1014 }
1015
98921dbd 1016 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
5e23b923 1017 return -ENODEV;
5e23b923 1018
7a9d4020 1019 data->cmdreq_type = USB_TYPE_CLASS;
93600997 1020 data->driver_info = id->driver_info;
7a9d4020 1021
5e23b923 1022 data->udev = interface_to_usbdev(intf);
5fbcd260 1023 data->intf = intf;
5e23b923
MH
1024
1025 spin_lock_init(&data->lock);
1026
1027 INIT_WORK(&data->work, btusb_work);
7bee549e
ON
1028 INIT_WORK(&data->waker, btusb_waker);
1029 spin_lock_init(&data->txlock);
5e23b923
MH
1030
1031 init_usb_anchor(&data->tx_anchor);
1032 init_usb_anchor(&data->intr_anchor);
1033 init_usb_anchor(&data->bulk_anchor);
9bfa35fe 1034 init_usb_anchor(&data->isoc_anchor);
7bee549e 1035 init_usb_anchor(&data->deferred);
5e23b923
MH
1036
1037 hdev = hci_alloc_dev();
98921dbd 1038 if (!hdev)
5e23b923 1039 return -ENOMEM;
5e23b923 1040
c13854ce 1041 hdev->bus = HCI_USB;
155961e8 1042 hci_set_drvdata(hdev, data);
5e23b923
MH
1043
1044 data->hdev = hdev;
1045
1046 SET_HCIDEV_DEV(hdev, &intf->dev);
1047
1048 hdev->open = btusb_open;
1049 hdev->close = btusb_close;
1050 hdev->flush = btusb_flush;
93600997 1051 hdev->setup = btusb_setup;
5e23b923 1052 hdev->send = btusb_send_frame;
5e23b923
MH
1053 hdev->notify = btusb_notify;
1054
7a9d4020 1055 /* Interface numbers are hardcoded in the specification */
9bfa35fe
MH
1056 data->isoc = usb_ifnum_to_if(data->udev, 1);
1057
7a9d4020 1058 if (!reset)
a6c511c6 1059 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
cfeb4145
MH
1060
1061 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
1062 if (!disable_scofix)
1063 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
1064 }
1065
9bfa35fe
MH
1066 if (id->driver_info & BTUSB_BROKEN_ISOC)
1067 data->isoc = NULL;
1068
7a9d4020
MH
1069 if (id->driver_info & BTUSB_DIGIANSWER) {
1070 data->cmdreq_type = USB_TYPE_VENDOR;
a6c511c6 1071 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
7a9d4020
MH
1072 }
1073
1074 if (id->driver_info & BTUSB_CSR) {
1075 struct usb_device *udev = data->udev;
1076
1077 /* Old firmware would otherwise execute USB reset */
1078 if (le16_to_cpu(udev->descriptor.bcdDevice) < 0x117)
a6c511c6 1079 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
7a9d4020
MH
1080 }
1081
cfeb4145 1082 if (id->driver_info & BTUSB_SNIFFER) {
9bfa35fe 1083 struct usb_device *udev = data->udev;
cfeb4145 1084
7a9d4020 1085 /* New sniffer firmware has crippled HCI interface */
cfeb4145
MH
1086 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
1087 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
9bfa35fe
MH
1088
1089 data->isoc = NULL;
cfeb4145
MH
1090 }
1091
9bfa35fe
MH
1092 if (data->isoc) {
1093 err = usb_driver_claim_interface(&btusb_driver,
5fbcd260 1094 data->isoc, data);
9bfa35fe
MH
1095 if (err < 0) {
1096 hci_free_dev(hdev);
9bfa35fe
MH
1097 return err;
1098 }
1099 }
1100
5e23b923
MH
1101 err = hci_register_dev(hdev);
1102 if (err < 0) {
1103 hci_free_dev(hdev);
5e23b923
MH
1104 return err;
1105 }
1106
1107 usb_set_intfdata(intf, data);
1108
1109 return 0;
1110}
1111
1112static void btusb_disconnect(struct usb_interface *intf)
1113{
1114 struct btusb_data *data = usb_get_intfdata(intf);
1115 struct hci_dev *hdev;
1116
1117 BT_DBG("intf %p", intf);
1118
1119 if (!data)
1120 return;
1121
1122 hdev = data->hdev;
5fbcd260
MH
1123 usb_set_intfdata(data->intf, NULL);
1124
1125 if (data->isoc)
1126 usb_set_intfdata(data->isoc, NULL);
5e23b923
MH
1127
1128 hci_unregister_dev(hdev);
1129
5fbcd260
MH
1130 if (intf == data->isoc)
1131 usb_driver_release_interface(&btusb_driver, data->intf);
1132 else if (data->isoc)
1133 usb_driver_release_interface(&btusb_driver, data->isoc);
1134
5e23b923
MH
1135 hci_free_dev(hdev);
1136}
1137
7bee549e 1138#ifdef CONFIG_PM
6a88adf2
MH
1139static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
1140{
1141 struct btusb_data *data = usb_get_intfdata(intf);
1142
1143 BT_DBG("intf %p", intf);
1144
1145 if (data->suspend_count++)
1146 return 0;
1147
7bee549e 1148 spin_lock_irq(&data->txlock);
5b1b0b81 1149 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
7bee549e
ON
1150 set_bit(BTUSB_SUSPENDING, &data->flags);
1151 spin_unlock_irq(&data->txlock);
1152 } else {
1153 spin_unlock_irq(&data->txlock);
1154 data->suspend_count--;
1155 return -EBUSY;
1156 }
1157
6a88adf2
MH
1158 cancel_work_sync(&data->work);
1159
7bee549e 1160 btusb_stop_traffic(data);
6a88adf2
MH
1161 usb_kill_anchored_urbs(&data->tx_anchor);
1162
6a88adf2
MH
1163 return 0;
1164}
1165
7bee549e
ON
1166static void play_deferred(struct btusb_data *data)
1167{
1168 struct urb *urb;
1169 int err;
1170
1171 while ((urb = usb_get_from_anchor(&data->deferred))) {
1172 err = usb_submit_urb(urb, GFP_ATOMIC);
1173 if (err < 0)
1174 break;
1175
1176 data->tx_in_flight++;
1177 }
1178 usb_scuttle_anchored_urbs(&data->deferred);
1179}
1180
6a88adf2
MH
1181static int btusb_resume(struct usb_interface *intf)
1182{
1183 struct btusb_data *data = usb_get_intfdata(intf);
1184 struct hci_dev *hdev = data->hdev;
7bee549e 1185 int err = 0;
6a88adf2
MH
1186
1187 BT_DBG("intf %p", intf);
1188
1189 if (--data->suspend_count)
1190 return 0;
1191
1192 if (!test_bit(HCI_RUNNING, &hdev->flags))
7bee549e 1193 goto done;
6a88adf2
MH
1194
1195 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
1196 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
1197 if (err < 0) {
1198 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
7bee549e 1199 goto failed;
6a88adf2
MH
1200 }
1201 }
1202
1203 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
43c2e57f
MH
1204 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
1205 if (err < 0) {
6a88adf2 1206 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
7bee549e
ON
1207 goto failed;
1208 }
1209
1210 btusb_submit_bulk_urb(hdev, GFP_NOIO);
6a88adf2
MH
1211 }
1212
1213 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1214 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
1215 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1216 else
1217 btusb_submit_isoc_urb(hdev, GFP_NOIO);
1218 }
1219
7bee549e
ON
1220 spin_lock_irq(&data->txlock);
1221 play_deferred(data);
1222 clear_bit(BTUSB_SUSPENDING, &data->flags);
1223 spin_unlock_irq(&data->txlock);
1224 schedule_work(&data->work);
1225
6a88adf2 1226 return 0;
7bee549e
ON
1227
1228failed:
1229 usb_scuttle_anchored_urbs(&data->deferred);
1230done:
1231 spin_lock_irq(&data->txlock);
1232 clear_bit(BTUSB_SUSPENDING, &data->flags);
1233 spin_unlock_irq(&data->txlock);
1234
1235 return err;
6a88adf2 1236}
7bee549e 1237#endif
6a88adf2 1238
5e23b923
MH
1239static struct usb_driver btusb_driver = {
1240 .name = "btusb",
1241 .probe = btusb_probe,
1242 .disconnect = btusb_disconnect,
7bee549e 1243#ifdef CONFIG_PM
6a88adf2
MH
1244 .suspend = btusb_suspend,
1245 .resume = btusb_resume,
7bee549e 1246#endif
5e23b923 1247 .id_table = btusb_table,
7bee549e 1248 .supports_autosuspend = 1,
e1f12eb6 1249 .disable_hub_initiated_lpm = 1,
5e23b923
MH
1250};
1251
93f1508c 1252module_usb_driver(btusb_driver);
5e23b923 1253
cfeb4145
MH
1254module_param(ignore_dga, bool, 0644);
1255MODULE_PARM_DESC(ignore_dga, "Ignore devices with id 08fd:0001");
1256
1257module_param(ignore_csr, bool, 0644);
1258MODULE_PARM_DESC(ignore_csr, "Ignore devices with id 0a12:0001");
1259
1260module_param(ignore_sniffer, bool, 0644);
1261MODULE_PARM_DESC(ignore_sniffer, "Ignore devices with id 0a12:0002");
1262
1263module_param(disable_scofix, bool, 0644);
1264MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
1265
1266module_param(force_scofix, bool, 0644);
1267MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
1268
1269module_param(reset, bool, 0644);
1270MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
1271
5e23b923
MH
1272MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
1273MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
1274MODULE_VERSION(VERSION);
1275MODULE_LICENSE("GPL");