0527b29c3954be02db3693b4d338d0cbe59d7960
[deliverable/linux.git] / drivers / bluetooth / btusb.c
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/module.h>
25 #include <linux/usb.h>
26 #include <linux/firmware.h>
27
28 #include <net/bluetooth/bluetooth.h>
29 #include <net/bluetooth/hci_core.h>
30
31 #define VERSION "0.6"
32
33 static bool disable_scofix;
34 static bool force_scofix;
35
36 static bool reset = 1;
37
38 static struct usb_driver btusb_driver;
39
40 #define BTUSB_IGNORE 0x01
41 #define BTUSB_DIGIANSWER 0x02
42 #define BTUSB_CSR 0x04
43 #define BTUSB_SNIFFER 0x08
44 #define BTUSB_BCM92035 0x10
45 #define BTUSB_BROKEN_ISOC 0x20
46 #define BTUSB_WRONG_SCO_MTU 0x40
47 #define BTUSB_ATH3012 0x80
48 #define BTUSB_INTEL 0x100
49 #define BTUSB_INTEL_BOOT 0x200
50 #define BTUSB_BCM_PATCHRAM 0x400
51 #define BTUSB_MARVELL 0x800
52
53 static const struct usb_device_id btusb_table[] = {
54 /* Generic Bluetooth USB device */
55 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
56
57 /* Apple-specific (Broadcom) devices */
58 { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01) },
59
60 /* MediaTek MT76x0E */
61 { USB_DEVICE(0x0e8d, 0x763f) },
62
63 /* Broadcom SoftSailing reporting vendor specific */
64 { USB_DEVICE(0x0a5c, 0x21e1) },
65
66 /* Apple MacBookPro 7,1 */
67 { USB_DEVICE(0x05ac, 0x8213) },
68
69 /* Apple iMac11,1 */
70 { USB_DEVICE(0x05ac, 0x8215) },
71
72 /* Apple MacBookPro6,2 */
73 { USB_DEVICE(0x05ac, 0x8218) },
74
75 /* Apple MacBookAir3,1, MacBookAir3,2 */
76 { USB_DEVICE(0x05ac, 0x821b) },
77
78 /* Apple MacBookAir4,1 */
79 { USB_DEVICE(0x05ac, 0x821f) },
80
81 /* Apple MacBookPro8,2 */
82 { USB_DEVICE(0x05ac, 0x821a) },
83
84 /* Apple MacMini5,1 */
85 { USB_DEVICE(0x05ac, 0x8281) },
86
87 /* AVM BlueFRITZ! USB v2.0 */
88 { USB_DEVICE(0x057c, 0x3800) },
89
90 /* Bluetooth Ultraport Module from IBM */
91 { USB_DEVICE(0x04bf, 0x030a) },
92
93 /* ALPS Modules with non-standard id */
94 { USB_DEVICE(0x044e, 0x3001) },
95 { USB_DEVICE(0x044e, 0x3002) },
96
97 /* Ericsson with non-standard id */
98 { USB_DEVICE(0x0bdb, 0x1002) },
99
100 /* Canyon CN-BTU1 with HID interfaces */
101 { USB_DEVICE(0x0c10, 0x0000) },
102
103 /* Broadcom BCM20702A0 */
104 { USB_DEVICE(0x0489, 0xe042) },
105 { USB_DEVICE(0x04ca, 0x2003) },
106 { USB_DEVICE(0x0b05, 0x17b5) },
107 { USB_DEVICE(0x0b05, 0x17cb) },
108 { USB_DEVICE(0x413c, 0x8197) },
109
110 /* Foxconn - Hon Hai */
111 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01) },
112
113 /* Broadcom devices with vendor specific id */
114 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
115 .driver_info = BTUSB_BCM_PATCHRAM },
116
117 /* ASUSTek Computer - Broadcom based */
118 { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01) },
119
120 /* Belkin F8065bf - Broadcom based */
121 { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01) },
122
123 /* IMC Networks - Broadcom based */
124 { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01) },
125
126 /* Intel Bluetooth USB Bootloader (RAM module) */
127 { USB_DEVICE(0x8087, 0x0a5a),
128 .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
129
130 { } /* Terminating entry */
131 };
132
133 MODULE_DEVICE_TABLE(usb, btusb_table);
134
135 static const struct usb_device_id blacklist_table[] = {
136 /* CSR BlueCore devices */
137 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
138
139 /* Broadcom BCM2033 without firmware */
140 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
141
142 /* Atheros 3011 with sflash firmware */
143 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
144 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
145 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
146 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
147 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
148 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
149
150 /* Atheros AR9285 Malbec with sflash firmware */
151 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
152
153 /* Atheros 3012 with sflash firmware */
154 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
155 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
156 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
157 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
158 { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
159 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
160 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
161 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
162 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
163 { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
164 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
165 { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
166 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
167 { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
168 { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
169 { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
170 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
171 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
172 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
173 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
174 { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
175 { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
176 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
177 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
178 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
179 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
180 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
181 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
182 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
183 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
184 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
185 { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
186
187 /* Atheros AR5BBU12 with sflash firmware */
188 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
189
190 /* Atheros AR5BBU12 with sflash firmware */
191 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
192 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
193
194 /* Broadcom BCM2035 */
195 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
196 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
197 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
198
199 /* Broadcom BCM2045 */
200 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
201 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
202
203 /* IBM/Lenovo ThinkPad with Broadcom chip */
204 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
205 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
206
207 /* HP laptop with Broadcom chip */
208 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
209
210 /* Dell laptop with Broadcom chip */
211 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
212
213 /* Dell Wireless 370 and 410 devices */
214 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
215 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
216
217 /* Belkin F8T012 and F8T013 devices */
218 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
219 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
220
221 /* Asus WL-BTD202 device */
222 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
223
224 /* Kensington Bluetooth USB adapter */
225 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
226
227 /* RTX Telecom based adapters with buggy SCO support */
228 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
229 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
230
231 /* CONWISE Technology based adapters with buggy SCO support */
232 { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
233
234 /* Digianswer devices */
235 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
236 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
237
238 /* CSR BlueCore Bluetooth Sniffer */
239 { USB_DEVICE(0x0a12, 0x0002),
240 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
241
242 /* Frontline ComProbe Bluetooth Sniffer */
243 { USB_DEVICE(0x16d3, 0x0002),
244 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
245
246 /* Intel Bluetooth device */
247 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL },
248 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL },
249
250 /* Marvell device */
251 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
252 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
253
254 { } /* Terminating entry */
255 };
256
257 #define BTUSB_MAX_ISOC_FRAMES 10
258
259 #define BTUSB_INTR_RUNNING 0
260 #define BTUSB_BULK_RUNNING 1
261 #define BTUSB_ISOC_RUNNING 2
262 #define BTUSB_SUSPENDING 3
263 #define BTUSB_DID_ISO_RESUME 4
264
265 struct btusb_data {
266 struct hci_dev *hdev;
267 struct usb_device *udev;
268 struct usb_interface *intf;
269 struct usb_interface *isoc;
270
271 spinlock_t lock;
272
273 unsigned long flags;
274
275 struct work_struct work;
276 struct work_struct waker;
277
278 struct usb_anchor tx_anchor;
279 struct usb_anchor intr_anchor;
280 struct usb_anchor bulk_anchor;
281 struct usb_anchor isoc_anchor;
282 struct usb_anchor deferred;
283 int tx_in_flight;
284 spinlock_t txlock;
285
286 struct usb_endpoint_descriptor *intr_ep;
287 struct usb_endpoint_descriptor *bulk_tx_ep;
288 struct usb_endpoint_descriptor *bulk_rx_ep;
289 struct usb_endpoint_descriptor *isoc_tx_ep;
290 struct usb_endpoint_descriptor *isoc_rx_ep;
291
292 __u8 cmdreq_type;
293
294 unsigned int sco_num;
295 int isoc_altsetting;
296 int suspend_count;
297 };
298
299 static int inc_tx(struct btusb_data *data)
300 {
301 unsigned long flags;
302 int rv;
303
304 spin_lock_irqsave(&data->txlock, flags);
305 rv = test_bit(BTUSB_SUSPENDING, &data->flags);
306 if (!rv)
307 data->tx_in_flight++;
308 spin_unlock_irqrestore(&data->txlock, flags);
309
310 return rv;
311 }
312
313 static void btusb_intr_complete(struct urb *urb)
314 {
315 struct hci_dev *hdev = urb->context;
316 struct btusb_data *data = hci_get_drvdata(hdev);
317 int err;
318
319 BT_DBG("%s urb %p status %d count %d", hdev->name,
320 urb, urb->status, urb->actual_length);
321
322 if (!test_bit(HCI_RUNNING, &hdev->flags))
323 return;
324
325 if (urb->status == 0) {
326 hdev->stat.byte_rx += urb->actual_length;
327
328 if (hci_recv_fragment(hdev, HCI_EVENT_PKT,
329 urb->transfer_buffer,
330 urb->actual_length) < 0) {
331 BT_ERR("%s corrupted event packet", hdev->name);
332 hdev->stat.err_rx++;
333 }
334 }
335
336 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
337 return;
338
339 usb_mark_last_busy(data->udev);
340 usb_anchor_urb(urb, &data->intr_anchor);
341
342 err = usb_submit_urb(urb, GFP_ATOMIC);
343 if (err < 0) {
344 /* -EPERM: urb is being killed;
345 * -ENODEV: device got disconnected */
346 if (err != -EPERM && err != -ENODEV)
347 BT_ERR("%s urb %p failed to resubmit (%d)",
348 hdev->name, urb, -err);
349 usb_unanchor_urb(urb);
350 }
351 }
352
353 static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
354 {
355 struct btusb_data *data = hci_get_drvdata(hdev);
356 struct urb *urb;
357 unsigned char *buf;
358 unsigned int pipe;
359 int err, size;
360
361 BT_DBG("%s", hdev->name);
362
363 if (!data->intr_ep)
364 return -ENODEV;
365
366 urb = usb_alloc_urb(0, mem_flags);
367 if (!urb)
368 return -ENOMEM;
369
370 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
371
372 buf = kmalloc(size, mem_flags);
373 if (!buf) {
374 usb_free_urb(urb);
375 return -ENOMEM;
376 }
377
378 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
379
380 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
381 btusb_intr_complete, hdev,
382 data->intr_ep->bInterval);
383
384 urb->transfer_flags |= URB_FREE_BUFFER;
385
386 usb_anchor_urb(urb, &data->intr_anchor);
387
388 err = usb_submit_urb(urb, mem_flags);
389 if (err < 0) {
390 if (err != -EPERM && err != -ENODEV)
391 BT_ERR("%s urb %p submission failed (%d)",
392 hdev->name, urb, -err);
393 usb_unanchor_urb(urb);
394 }
395
396 usb_free_urb(urb);
397
398 return err;
399 }
400
401 static void btusb_bulk_complete(struct urb *urb)
402 {
403 struct hci_dev *hdev = urb->context;
404 struct btusb_data *data = hci_get_drvdata(hdev);
405 int err;
406
407 BT_DBG("%s urb %p status %d count %d", hdev->name,
408 urb, urb->status, urb->actual_length);
409
410 if (!test_bit(HCI_RUNNING, &hdev->flags))
411 return;
412
413 if (urb->status == 0) {
414 hdev->stat.byte_rx += urb->actual_length;
415
416 if (hci_recv_fragment(hdev, HCI_ACLDATA_PKT,
417 urb->transfer_buffer,
418 urb->actual_length) < 0) {
419 BT_ERR("%s corrupted ACL packet", hdev->name);
420 hdev->stat.err_rx++;
421 }
422 }
423
424 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
425 return;
426
427 usb_anchor_urb(urb, &data->bulk_anchor);
428 usb_mark_last_busy(data->udev);
429
430 err = usb_submit_urb(urb, GFP_ATOMIC);
431 if (err < 0) {
432 /* -EPERM: urb is being killed;
433 * -ENODEV: device got disconnected */
434 if (err != -EPERM && err != -ENODEV)
435 BT_ERR("%s urb %p failed to resubmit (%d)",
436 hdev->name, urb, -err);
437 usb_unanchor_urb(urb);
438 }
439 }
440
441 static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
442 {
443 struct btusb_data *data = hci_get_drvdata(hdev);
444 struct urb *urb;
445 unsigned char *buf;
446 unsigned int pipe;
447 int err, size = HCI_MAX_FRAME_SIZE;
448
449 BT_DBG("%s", hdev->name);
450
451 if (!data->bulk_rx_ep)
452 return -ENODEV;
453
454 urb = usb_alloc_urb(0, mem_flags);
455 if (!urb)
456 return -ENOMEM;
457
458 buf = kmalloc(size, mem_flags);
459 if (!buf) {
460 usb_free_urb(urb);
461 return -ENOMEM;
462 }
463
464 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
465
466 usb_fill_bulk_urb(urb, data->udev, pipe,
467 buf, size, btusb_bulk_complete, hdev);
468
469 urb->transfer_flags |= URB_FREE_BUFFER;
470
471 usb_mark_last_busy(data->udev);
472 usb_anchor_urb(urb, &data->bulk_anchor);
473
474 err = usb_submit_urb(urb, mem_flags);
475 if (err < 0) {
476 if (err != -EPERM && err != -ENODEV)
477 BT_ERR("%s urb %p submission failed (%d)",
478 hdev->name, urb, -err);
479 usb_unanchor_urb(urb);
480 }
481
482 usb_free_urb(urb);
483
484 return err;
485 }
486
487 static void btusb_isoc_complete(struct urb *urb)
488 {
489 struct hci_dev *hdev = urb->context;
490 struct btusb_data *data = hci_get_drvdata(hdev);
491 int i, err;
492
493 BT_DBG("%s urb %p status %d count %d", hdev->name,
494 urb, urb->status, urb->actual_length);
495
496 if (!test_bit(HCI_RUNNING, &hdev->flags))
497 return;
498
499 if (urb->status == 0) {
500 for (i = 0; i < urb->number_of_packets; i++) {
501 unsigned int offset = urb->iso_frame_desc[i].offset;
502 unsigned int length = urb->iso_frame_desc[i].actual_length;
503
504 if (urb->iso_frame_desc[i].status)
505 continue;
506
507 hdev->stat.byte_rx += length;
508
509 if (hci_recv_fragment(hdev, HCI_SCODATA_PKT,
510 urb->transfer_buffer + offset,
511 length) < 0) {
512 BT_ERR("%s corrupted SCO packet", hdev->name);
513 hdev->stat.err_rx++;
514 }
515 }
516 }
517
518 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
519 return;
520
521 usb_anchor_urb(urb, &data->isoc_anchor);
522
523 err = usb_submit_urb(urb, GFP_ATOMIC);
524 if (err < 0) {
525 /* -EPERM: urb is being killed;
526 * -ENODEV: device got disconnected */
527 if (err != -EPERM && err != -ENODEV)
528 BT_ERR("%s urb %p failed to resubmit (%d)",
529 hdev->name, urb, -err);
530 usb_unanchor_urb(urb);
531 }
532 }
533
534 static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
535 {
536 int i, offset = 0;
537
538 BT_DBG("len %d mtu %d", len, mtu);
539
540 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
541 i++, offset += mtu, len -= mtu) {
542 urb->iso_frame_desc[i].offset = offset;
543 urb->iso_frame_desc[i].length = mtu;
544 }
545
546 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
547 urb->iso_frame_desc[i].offset = offset;
548 urb->iso_frame_desc[i].length = len;
549 i++;
550 }
551
552 urb->number_of_packets = i;
553 }
554
555 static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
556 {
557 struct btusb_data *data = hci_get_drvdata(hdev);
558 struct urb *urb;
559 unsigned char *buf;
560 unsigned int pipe;
561 int err, size;
562
563 BT_DBG("%s", hdev->name);
564
565 if (!data->isoc_rx_ep)
566 return -ENODEV;
567
568 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
569 if (!urb)
570 return -ENOMEM;
571
572 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
573 BTUSB_MAX_ISOC_FRAMES;
574
575 buf = kmalloc(size, mem_flags);
576 if (!buf) {
577 usb_free_urb(urb);
578 return -ENOMEM;
579 }
580
581 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
582
583 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
584 hdev, data->isoc_rx_ep->bInterval);
585
586 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
587
588 __fill_isoc_descriptor(urb, size,
589 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
590
591 usb_anchor_urb(urb, &data->isoc_anchor);
592
593 err = usb_submit_urb(urb, mem_flags);
594 if (err < 0) {
595 if (err != -EPERM && err != -ENODEV)
596 BT_ERR("%s urb %p submission failed (%d)",
597 hdev->name, urb, -err);
598 usb_unanchor_urb(urb);
599 }
600
601 usb_free_urb(urb);
602
603 return err;
604 }
605
606 static void btusb_tx_complete(struct urb *urb)
607 {
608 struct sk_buff *skb = urb->context;
609 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
610 struct btusb_data *data = hci_get_drvdata(hdev);
611
612 BT_DBG("%s urb %p status %d count %d", hdev->name,
613 urb, urb->status, urb->actual_length);
614
615 if (!test_bit(HCI_RUNNING, &hdev->flags))
616 goto done;
617
618 if (!urb->status)
619 hdev->stat.byte_tx += urb->transfer_buffer_length;
620 else
621 hdev->stat.err_tx++;
622
623 done:
624 spin_lock(&data->txlock);
625 data->tx_in_flight--;
626 spin_unlock(&data->txlock);
627
628 kfree(urb->setup_packet);
629
630 kfree_skb(skb);
631 }
632
633 static void btusb_isoc_tx_complete(struct urb *urb)
634 {
635 struct sk_buff *skb = urb->context;
636 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
637
638 BT_DBG("%s urb %p status %d count %d", hdev->name,
639 urb, urb->status, urb->actual_length);
640
641 if (!test_bit(HCI_RUNNING, &hdev->flags))
642 goto done;
643
644 if (!urb->status)
645 hdev->stat.byte_tx += urb->transfer_buffer_length;
646 else
647 hdev->stat.err_tx++;
648
649 done:
650 kfree(urb->setup_packet);
651
652 kfree_skb(skb);
653 }
654
655 static int btusb_open(struct hci_dev *hdev)
656 {
657 struct btusb_data *data = hci_get_drvdata(hdev);
658 int err;
659
660 BT_DBG("%s", hdev->name);
661
662 err = usb_autopm_get_interface(data->intf);
663 if (err < 0)
664 return err;
665
666 data->intf->needs_remote_wakeup = 1;
667
668 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
669 goto done;
670
671 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
672 goto done;
673
674 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
675 if (err < 0)
676 goto failed;
677
678 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
679 if (err < 0) {
680 usb_kill_anchored_urbs(&data->intr_anchor);
681 goto failed;
682 }
683
684 set_bit(BTUSB_BULK_RUNNING, &data->flags);
685 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
686
687 done:
688 usb_autopm_put_interface(data->intf);
689 return 0;
690
691 failed:
692 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
693 clear_bit(HCI_RUNNING, &hdev->flags);
694 usb_autopm_put_interface(data->intf);
695 return err;
696 }
697
698 static void btusb_stop_traffic(struct btusb_data *data)
699 {
700 usb_kill_anchored_urbs(&data->intr_anchor);
701 usb_kill_anchored_urbs(&data->bulk_anchor);
702 usb_kill_anchored_urbs(&data->isoc_anchor);
703 }
704
705 static int btusb_close(struct hci_dev *hdev)
706 {
707 struct btusb_data *data = hci_get_drvdata(hdev);
708 int err;
709
710 BT_DBG("%s", hdev->name);
711
712 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
713 return 0;
714
715 cancel_work_sync(&data->work);
716 cancel_work_sync(&data->waker);
717
718 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
719 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
720 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
721
722 btusb_stop_traffic(data);
723 err = usb_autopm_get_interface(data->intf);
724 if (err < 0)
725 goto failed;
726
727 data->intf->needs_remote_wakeup = 0;
728 usb_autopm_put_interface(data->intf);
729
730 failed:
731 usb_scuttle_anchored_urbs(&data->deferred);
732 return 0;
733 }
734
735 static int btusb_flush(struct hci_dev *hdev)
736 {
737 struct btusb_data *data = hci_get_drvdata(hdev);
738
739 BT_DBG("%s", hdev->name);
740
741 usb_kill_anchored_urbs(&data->tx_anchor);
742
743 return 0;
744 }
745
746 static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
747 {
748 struct btusb_data *data = hci_get_drvdata(hdev);
749 struct usb_ctrlrequest *dr;
750 struct urb *urb;
751 unsigned int pipe;
752 int err;
753
754 BT_DBG("%s", hdev->name);
755
756 if (!test_bit(HCI_RUNNING, &hdev->flags))
757 return -EBUSY;
758
759 skb->dev = (void *) hdev;
760
761 switch (bt_cb(skb)->pkt_type) {
762 case HCI_COMMAND_PKT:
763 urb = usb_alloc_urb(0, GFP_ATOMIC);
764 if (!urb)
765 return -ENOMEM;
766
767 dr = kmalloc(sizeof(*dr), GFP_ATOMIC);
768 if (!dr) {
769 usb_free_urb(urb);
770 return -ENOMEM;
771 }
772
773 dr->bRequestType = data->cmdreq_type;
774 dr->bRequest = 0;
775 dr->wIndex = 0;
776 dr->wValue = 0;
777 dr->wLength = __cpu_to_le16(skb->len);
778
779 pipe = usb_sndctrlpipe(data->udev, 0x00);
780
781 usb_fill_control_urb(urb, data->udev, pipe, (void *) dr,
782 skb->data, skb->len, btusb_tx_complete, skb);
783
784 hdev->stat.cmd_tx++;
785 break;
786
787 case HCI_ACLDATA_PKT:
788 if (!data->bulk_tx_ep)
789 return -ENODEV;
790
791 urb = usb_alloc_urb(0, GFP_ATOMIC);
792 if (!urb)
793 return -ENOMEM;
794
795 pipe = usb_sndbulkpipe(data->udev,
796 data->bulk_tx_ep->bEndpointAddress);
797
798 usb_fill_bulk_urb(urb, data->udev, pipe,
799 skb->data, skb->len, btusb_tx_complete, skb);
800
801 hdev->stat.acl_tx++;
802 break;
803
804 case HCI_SCODATA_PKT:
805 if (!data->isoc_tx_ep || hci_conn_num(hdev, SCO_LINK) < 1)
806 return -ENODEV;
807
808 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_ATOMIC);
809 if (!urb)
810 return -ENOMEM;
811
812 pipe = usb_sndisocpipe(data->udev,
813 data->isoc_tx_ep->bEndpointAddress);
814
815 usb_fill_int_urb(urb, data->udev, pipe,
816 skb->data, skb->len, btusb_isoc_tx_complete,
817 skb, data->isoc_tx_ep->bInterval);
818
819 urb->transfer_flags = URB_ISO_ASAP;
820
821 __fill_isoc_descriptor(urb, skb->len,
822 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
823
824 hdev->stat.sco_tx++;
825 goto skip_waking;
826
827 default:
828 return -EILSEQ;
829 }
830
831 err = inc_tx(data);
832 if (err) {
833 usb_anchor_urb(urb, &data->deferred);
834 schedule_work(&data->waker);
835 err = 0;
836 goto done;
837 }
838
839 skip_waking:
840 usb_anchor_urb(urb, &data->tx_anchor);
841
842 err = usb_submit_urb(urb, GFP_ATOMIC);
843 if (err < 0) {
844 if (err != -EPERM && err != -ENODEV)
845 BT_ERR("%s urb %p submission failed (%d)",
846 hdev->name, urb, -err);
847 kfree(urb->setup_packet);
848 usb_unanchor_urb(urb);
849 } else {
850 usb_mark_last_busy(data->udev);
851 }
852
853 done:
854 usb_free_urb(urb);
855 return err;
856 }
857
858 static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
859 {
860 struct btusb_data *data = hci_get_drvdata(hdev);
861
862 BT_DBG("%s evt %d", hdev->name, evt);
863
864 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
865 data->sco_num = hci_conn_num(hdev, SCO_LINK);
866 schedule_work(&data->work);
867 }
868 }
869
870 static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
871 {
872 struct btusb_data *data = hci_get_drvdata(hdev);
873 struct usb_interface *intf = data->isoc;
874 struct usb_endpoint_descriptor *ep_desc;
875 int i, err;
876
877 if (!data->isoc)
878 return -ENODEV;
879
880 err = usb_set_interface(data->udev, 1, altsetting);
881 if (err < 0) {
882 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
883 return err;
884 }
885
886 data->isoc_altsetting = altsetting;
887
888 data->isoc_tx_ep = NULL;
889 data->isoc_rx_ep = NULL;
890
891 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
892 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
893
894 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
895 data->isoc_tx_ep = ep_desc;
896 continue;
897 }
898
899 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
900 data->isoc_rx_ep = ep_desc;
901 continue;
902 }
903 }
904
905 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
906 BT_ERR("%s invalid SCO descriptors", hdev->name);
907 return -ENODEV;
908 }
909
910 return 0;
911 }
912
913 static void btusb_work(struct work_struct *work)
914 {
915 struct btusb_data *data = container_of(work, struct btusb_data, work);
916 struct hci_dev *hdev = data->hdev;
917 int new_alts;
918 int err;
919
920 if (data->sco_num > 0) {
921 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
922 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
923 if (err < 0) {
924 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
925 usb_kill_anchored_urbs(&data->isoc_anchor);
926 return;
927 }
928
929 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
930 }
931
932 if (hdev->voice_setting & 0x0020) {
933 static const int alts[3] = { 2, 4, 5 };
934 new_alts = alts[data->sco_num - 1];
935 } else {
936 new_alts = data->sco_num;
937 }
938
939 if (data->isoc_altsetting != new_alts) {
940 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
941 usb_kill_anchored_urbs(&data->isoc_anchor);
942
943 if (__set_isoc_interface(hdev, new_alts) < 0)
944 return;
945 }
946
947 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
948 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
949 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
950 else
951 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
952 }
953 } else {
954 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
955 usb_kill_anchored_urbs(&data->isoc_anchor);
956
957 __set_isoc_interface(hdev, 0);
958 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
959 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
960 }
961 }
962
963 static void btusb_waker(struct work_struct *work)
964 {
965 struct btusb_data *data = container_of(work, struct btusb_data, waker);
966 int err;
967
968 err = usb_autopm_get_interface(data->intf);
969 if (err < 0)
970 return;
971
972 usb_autopm_put_interface(data->intf);
973 }
974
975 static int btusb_setup_bcm92035(struct hci_dev *hdev)
976 {
977 struct sk_buff *skb;
978 u8 val = 0x00;
979
980 BT_DBG("%s", hdev->name);
981
982 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
983 if (IS_ERR(skb))
984 BT_ERR("BCM92035 command failed (%ld)", -PTR_ERR(skb));
985 else
986 kfree_skb(skb);
987
988 return 0;
989 }
990
991 static int btusb_setup_csr(struct hci_dev *hdev)
992 {
993 struct hci_rp_read_local_version *rp;
994 struct sk_buff *skb;
995 int ret;
996
997 BT_DBG("%s", hdev->name);
998
999 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1000 HCI_INIT_TIMEOUT);
1001 if (IS_ERR(skb)) {
1002 BT_ERR("Reading local version failed (%ld)", -PTR_ERR(skb));
1003 return -PTR_ERR(skb);
1004 }
1005
1006 rp = (struct hci_rp_read_local_version *) skb->data;
1007
1008 if (!rp->status) {
1009 if (le16_to_cpu(rp->manufacturer) != 10) {
1010 /* Clear the reset quirk since this is not an actual
1011 * early Bluetooth 1.1 device from CSR.
1012 */
1013 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
1014
1015 /* These fake CSR controllers have all a broken
1016 * stored link key handling and so just disable it.
1017 */
1018 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY,
1019 &hdev->quirks);
1020 }
1021 }
1022
1023 ret = -bt_to_errno(rp->status);
1024
1025 kfree_skb(skb);
1026
1027 return ret;
1028 }
1029
1030 struct intel_version {
1031 u8 status;
1032 u8 hw_platform;
1033 u8 hw_variant;
1034 u8 hw_revision;
1035 u8 fw_variant;
1036 u8 fw_revision;
1037 u8 fw_build_num;
1038 u8 fw_build_ww;
1039 u8 fw_build_yy;
1040 u8 fw_patch_num;
1041 } __packed;
1042
1043 static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
1044 struct intel_version *ver)
1045 {
1046 const struct firmware *fw;
1047 char fwname[64];
1048 int ret;
1049
1050 snprintf(fwname, sizeof(fwname),
1051 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1052 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1053 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
1054 ver->fw_build_ww, ver->fw_build_yy);
1055
1056 ret = request_firmware(&fw, fwname, &hdev->dev);
1057 if (ret < 0) {
1058 if (ret == -EINVAL) {
1059 BT_ERR("%s Intel firmware file request failed (%d)",
1060 hdev->name, ret);
1061 return NULL;
1062 }
1063
1064 BT_ERR("%s failed to open Intel firmware file: %s(%d)",
1065 hdev->name, fwname, ret);
1066
1067 /* If the correct firmware patch file is not found, use the
1068 * default firmware patch file instead
1069 */
1070 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1071 ver->hw_platform, ver->hw_variant);
1072 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1073 BT_ERR("%s failed to open default Intel fw file: %s",
1074 hdev->name, fwname);
1075 return NULL;
1076 }
1077 }
1078
1079 BT_INFO("%s: Intel Bluetooth firmware file: %s", hdev->name, fwname);
1080
1081 return fw;
1082 }
1083
1084 static int btusb_setup_intel_patching(struct hci_dev *hdev,
1085 const struct firmware *fw,
1086 const u8 **fw_ptr, int *disable_patch)
1087 {
1088 struct sk_buff *skb;
1089 struct hci_command_hdr *cmd;
1090 const u8 *cmd_param;
1091 struct hci_event_hdr *evt = NULL;
1092 const u8 *evt_param = NULL;
1093 int remain = fw->size - (*fw_ptr - fw->data);
1094
1095 /* The first byte indicates the types of the patch command or event.
1096 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1097 * in the current firmware buffer doesn't start with 0x01 or
1098 * the size of remain buffer is smaller than HCI command header,
1099 * the firmware file is corrupted and it should stop the patching
1100 * process.
1101 */
1102 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1103 BT_ERR("%s Intel fw corrupted: invalid cmd read", hdev->name);
1104 return -EINVAL;
1105 }
1106 (*fw_ptr)++;
1107 remain--;
1108
1109 cmd = (struct hci_command_hdr *)(*fw_ptr);
1110 *fw_ptr += sizeof(*cmd);
1111 remain -= sizeof(*cmd);
1112
1113 /* Ensure that the remain firmware data is long enough than the length
1114 * of command parameter. If not, the firmware file is corrupted.
1115 */
1116 if (remain < cmd->plen) {
1117 BT_ERR("%s Intel fw corrupted: invalid cmd len", hdev->name);
1118 return -EFAULT;
1119 }
1120
1121 /* If there is a command that loads a patch in the firmware
1122 * file, then enable the patch upon success, otherwise just
1123 * disable the manufacturer mode, for example patch activation
1124 * is not required when the default firmware patch file is used
1125 * because there are no patch data to load.
1126 */
1127 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1128 *disable_patch = 0;
1129
1130 cmd_param = *fw_ptr;
1131 *fw_ptr += cmd->plen;
1132 remain -= cmd->plen;
1133
1134 /* This reads the expected events when the above command is sent to the
1135 * device. Some vendor commands expects more than one events, for
1136 * example command status event followed by vendor specific event.
1137 * For this case, it only keeps the last expected event. so the command
1138 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1139 * last expected event.
1140 */
1141 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1142 (*fw_ptr)++;
1143 remain--;
1144
1145 evt = (struct hci_event_hdr *)(*fw_ptr);
1146 *fw_ptr += sizeof(*evt);
1147 remain -= sizeof(*evt);
1148
1149 if (remain < evt->plen) {
1150 BT_ERR("%s Intel fw corrupted: invalid evt len",
1151 hdev->name);
1152 return -EFAULT;
1153 }
1154
1155 evt_param = *fw_ptr;
1156 *fw_ptr += evt->plen;
1157 remain -= evt->plen;
1158 }
1159
1160 /* Every HCI commands in the firmware file has its correspond event.
1161 * If event is not found or remain is smaller than zero, the firmware
1162 * file is corrupted.
1163 */
1164 if (!evt || !evt_param || remain < 0) {
1165 BT_ERR("%s Intel fw corrupted: invalid evt read", hdev->name);
1166 return -EFAULT;
1167 }
1168
1169 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
1170 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
1171 if (IS_ERR(skb)) {
1172 BT_ERR("%s sending Intel patch command (0x%4.4x) failed (%ld)",
1173 hdev->name, cmd->opcode, PTR_ERR(skb));
1174 return PTR_ERR(skb);
1175 }
1176
1177 /* It ensures that the returned event matches the event data read from
1178 * the firmware file. At fist, it checks the length and then
1179 * the contents of the event.
1180 */
1181 if (skb->len != evt->plen) {
1182 BT_ERR("%s mismatch event length (opcode 0x%4.4x)", hdev->name,
1183 le16_to_cpu(cmd->opcode));
1184 kfree_skb(skb);
1185 return -EFAULT;
1186 }
1187
1188 if (memcmp(skb->data, evt_param, evt->plen)) {
1189 BT_ERR("%s mismatch event parameter (opcode 0x%4.4x)",
1190 hdev->name, le16_to_cpu(cmd->opcode));
1191 kfree_skb(skb);
1192 return -EFAULT;
1193 }
1194 kfree_skb(skb);
1195
1196 return 0;
1197 }
1198
1199 #define BDADDR_INTEL (&(bdaddr_t) {{0x00, 0x8b, 0x9e, 0x19, 0x03, 0x00}})
1200
1201 static int btusb_check_bdaddr_intel(struct hci_dev *hdev)
1202 {
1203 struct sk_buff *skb;
1204 struct hci_rp_read_bd_addr *rp;
1205
1206 skb = __hci_cmd_sync(hdev, HCI_OP_READ_BD_ADDR, 0, NULL,
1207 HCI_INIT_TIMEOUT);
1208 if (IS_ERR(skb)) {
1209 BT_ERR("%s reading Intel device address failed (%ld)",
1210 hdev->name, PTR_ERR(skb));
1211 return PTR_ERR(skb);
1212 }
1213
1214 if (skb->len != sizeof(*rp)) {
1215 BT_ERR("%s Intel device address length mismatch", hdev->name);
1216 kfree_skb(skb);
1217 return -EIO;
1218 }
1219
1220 rp = (struct hci_rp_read_bd_addr *) skb->data;
1221 if (rp->status) {
1222 BT_ERR("%s Intel device address result failed (%02x)",
1223 hdev->name, rp->status);
1224 kfree_skb(skb);
1225 return -bt_to_errno(rp->status);
1226 }
1227
1228 /* For some Intel based controllers, the default Bluetooth device
1229 * address 00:03:19:9E:8B:00 can be found. These controllers are
1230 * fully operational, but have the danger of duplicate addresses
1231 * and that in turn can cause problems with Bluetooth operation.
1232 */
1233 if (!bacmp(&rp->bdaddr, BDADDR_INTEL)) {
1234 BT_ERR("%s found Intel default device address (%pMR)",
1235 hdev->name, &rp->bdaddr);
1236 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
1237 }
1238
1239 kfree_skb(skb);
1240
1241 return 0;
1242 }
1243
1244 static int btusb_setup_intel(struct hci_dev *hdev)
1245 {
1246 struct sk_buff *skb;
1247 const struct firmware *fw;
1248 const u8 *fw_ptr;
1249 int disable_patch;
1250 struct intel_version *ver;
1251
1252 const u8 mfg_enable[] = { 0x01, 0x00 };
1253 const u8 mfg_disable[] = { 0x00, 0x00 };
1254 const u8 mfg_reset_deactivate[] = { 0x00, 0x01 };
1255 const u8 mfg_reset_activate[] = { 0x00, 0x02 };
1256
1257 BT_DBG("%s", hdev->name);
1258
1259 /* The controller has a bug with the first HCI command sent to it
1260 * returning number of completed commands as zero. This would stall the
1261 * command processing in the Bluetooth core.
1262 *
1263 * As a workaround, send HCI Reset command first which will reset the
1264 * number of completed commands and allow normal command processing
1265 * from now on.
1266 */
1267 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
1268 if (IS_ERR(skb)) {
1269 BT_ERR("%s sending initial HCI reset command failed (%ld)",
1270 hdev->name, PTR_ERR(skb));
1271 return PTR_ERR(skb);
1272 }
1273 kfree_skb(skb);
1274
1275 /* Read Intel specific controller version first to allow selection of
1276 * which firmware file to load.
1277 *
1278 * The returned information are hardware variant and revision plus
1279 * firmware variant, revision and build number.
1280 */
1281 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
1282 if (IS_ERR(skb)) {
1283 BT_ERR("%s reading Intel fw version command failed (%ld)",
1284 hdev->name, PTR_ERR(skb));
1285 return PTR_ERR(skb);
1286 }
1287
1288 if (skb->len != sizeof(*ver)) {
1289 BT_ERR("%s Intel version event length mismatch", hdev->name);
1290 kfree_skb(skb);
1291 return -EIO;
1292 }
1293
1294 ver = (struct intel_version *)skb->data;
1295 if (ver->status) {
1296 BT_ERR("%s Intel fw version event failed (%02x)", hdev->name,
1297 ver->status);
1298 kfree_skb(skb);
1299 return -bt_to_errno(ver->status);
1300 }
1301
1302 BT_INFO("%s: read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
1303 hdev->name, ver->hw_platform, ver->hw_variant,
1304 ver->hw_revision, ver->fw_variant, ver->fw_revision,
1305 ver->fw_build_num, ver->fw_build_ww, ver->fw_build_yy,
1306 ver->fw_patch_num);
1307
1308 /* fw_patch_num indicates the version of patch the device currently
1309 * have. If there is no patch data in the device, it is always 0x00.
1310 * So, if it is other than 0x00, no need to patch the deivce again.
1311 */
1312 if (ver->fw_patch_num) {
1313 BT_INFO("%s: Intel device is already patched. patch num: %02x",
1314 hdev->name, ver->fw_patch_num);
1315 kfree_skb(skb);
1316 btusb_check_bdaddr_intel(hdev);
1317 return 0;
1318 }
1319
1320 /* Opens the firmware patch file based on the firmware version read
1321 * from the controller. If it fails to open the matching firmware
1322 * patch file, it tries to open the default firmware patch file.
1323 * If no patch file is found, allow the device to operate without
1324 * a patch.
1325 */
1326 fw = btusb_setup_intel_get_fw(hdev, ver);
1327 if (!fw) {
1328 kfree_skb(skb);
1329 btusb_check_bdaddr_intel(hdev);
1330 return 0;
1331 }
1332 fw_ptr = fw->data;
1333
1334 /* This Intel specific command enables the manufacturer mode of the
1335 * controller.
1336 *
1337 * Only while this mode is enabled, the driver can download the
1338 * firmware patch data and configuration parameters.
1339 */
1340 skb = __hci_cmd_sync(hdev, 0xfc11, 2, mfg_enable, HCI_INIT_TIMEOUT);
1341 if (IS_ERR(skb)) {
1342 BT_ERR("%s entering Intel manufacturer mode failed (%ld)",
1343 hdev->name, PTR_ERR(skb));
1344 release_firmware(fw);
1345 return PTR_ERR(skb);
1346 }
1347
1348 if (skb->data[0]) {
1349 u8 evt_status = skb->data[0];
1350 BT_ERR("%s enable Intel manufacturer mode event failed (%02x)",
1351 hdev->name, evt_status);
1352 kfree_skb(skb);
1353 release_firmware(fw);
1354 return -bt_to_errno(evt_status);
1355 }
1356 kfree_skb(skb);
1357
1358 disable_patch = 1;
1359
1360 /* The firmware data file consists of list of Intel specific HCI
1361 * commands and its expected events. The first byte indicates the
1362 * type of the message, either HCI command or HCI event.
1363 *
1364 * It reads the command and its expected event from the firmware file,
1365 * and send to the controller. Once __hci_cmd_sync_ev() returns,
1366 * the returned event is compared with the event read from the firmware
1367 * file and it will continue until all the messages are downloaded to
1368 * the controller.
1369 *
1370 * Once the firmware patching is completed successfully,
1371 * the manufacturer mode is disabled with reset and activating the
1372 * downloaded patch.
1373 *
1374 * If the firmware patching fails, the manufacturer mode is
1375 * disabled with reset and deactivating the patch.
1376 *
1377 * If the default patch file is used, no reset is done when disabling
1378 * the manufacturer.
1379 */
1380 while (fw->size > fw_ptr - fw->data) {
1381 int ret;
1382
1383 ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
1384 &disable_patch);
1385 if (ret < 0)
1386 goto exit_mfg_deactivate;
1387 }
1388
1389 release_firmware(fw);
1390
1391 if (disable_patch)
1392 goto exit_mfg_disable;
1393
1394 /* Patching completed successfully and disable the manufacturer mode
1395 * with reset and activate the downloaded firmware patches.
1396 */
1397 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_activate),
1398 mfg_reset_activate, HCI_INIT_TIMEOUT);
1399 if (IS_ERR(skb)) {
1400 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1401 hdev->name, PTR_ERR(skb));
1402 return PTR_ERR(skb);
1403 }
1404 kfree_skb(skb);
1405
1406 BT_INFO("%s: Intel Bluetooth firmware patch completed and activated",
1407 hdev->name);
1408
1409 btusb_check_bdaddr_intel(hdev);
1410 return 0;
1411
1412 exit_mfg_disable:
1413 /* Disable the manufacturer mode without reset */
1414 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_disable), mfg_disable,
1415 HCI_INIT_TIMEOUT);
1416 if (IS_ERR(skb)) {
1417 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1418 hdev->name, PTR_ERR(skb));
1419 return PTR_ERR(skb);
1420 }
1421 kfree_skb(skb);
1422
1423 BT_INFO("%s: Intel Bluetooth firmware patch completed", hdev->name);
1424
1425 btusb_check_bdaddr_intel(hdev);
1426 return 0;
1427
1428 exit_mfg_deactivate:
1429 release_firmware(fw);
1430
1431 /* Patching failed. Disable the manufacturer mode with reset and
1432 * deactivate the downloaded firmware patches.
1433 */
1434 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_deactivate),
1435 mfg_reset_deactivate, HCI_INIT_TIMEOUT);
1436 if (IS_ERR(skb)) {
1437 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1438 hdev->name, PTR_ERR(skb));
1439 return PTR_ERR(skb);
1440 }
1441 kfree_skb(skb);
1442
1443 BT_INFO("%s: Intel Bluetooth firmware patch completed and deactivated",
1444 hdev->name);
1445
1446 btusb_check_bdaddr_intel(hdev);
1447 return 0;
1448 }
1449
1450 static int btusb_set_bdaddr_intel(struct hci_dev *hdev, const bdaddr_t *bdaddr)
1451 {
1452 struct sk_buff *skb;
1453 long ret;
1454
1455 skb = __hci_cmd_sync(hdev, 0xfc31, 6, bdaddr, HCI_INIT_TIMEOUT);
1456 if (IS_ERR(skb)) {
1457 ret = PTR_ERR(skb);
1458 BT_ERR("%s: changing Intel device address failed (%ld)",
1459 hdev->name, ret);
1460 return ret;
1461 }
1462 kfree_skb(skb);
1463
1464 return 0;
1465 }
1466
1467 static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
1468 const bdaddr_t *bdaddr)
1469 {
1470 struct sk_buff *skb;
1471 u8 buf[8];
1472 long ret;
1473
1474 buf[0] = 0xfe;
1475 buf[1] = sizeof(bdaddr_t);
1476 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
1477
1478 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
1479 if (IS_ERR(skb)) {
1480 ret = PTR_ERR(skb);
1481 BT_ERR("%s: changing Marvell device address failed (%ld)",
1482 hdev->name, ret);
1483 return ret;
1484 }
1485 kfree_skb(skb);
1486
1487 return 0;
1488 }
1489
1490 #define BDADDR_BCM20702A0 (&(bdaddr_t) {{0x00, 0xa0, 0x02, 0x70, 0x20, 0x00}})
1491
1492 static int btusb_setup_bcm_patchram(struct hci_dev *hdev)
1493 {
1494 struct btusb_data *data = hci_get_drvdata(hdev);
1495 struct usb_device *udev = data->udev;
1496 char fw_name[64];
1497 const struct firmware *fw;
1498 const u8 *fw_ptr;
1499 size_t fw_size;
1500 const struct hci_command_hdr *cmd;
1501 const u8 *cmd_param;
1502 u16 opcode;
1503 struct sk_buff *skb;
1504 struct hci_rp_read_local_version *ver;
1505 struct hci_rp_read_bd_addr *bda;
1506 long ret;
1507
1508 snprintf(fw_name, sizeof(fw_name), "brcm/%s-%04x-%04x.hcd",
1509 udev->product ? udev->product : "BCM",
1510 le16_to_cpu(udev->descriptor.idVendor),
1511 le16_to_cpu(udev->descriptor.idProduct));
1512
1513 ret = request_firmware(&fw, fw_name, &hdev->dev);
1514 if (ret < 0) {
1515 BT_INFO("%s: BCM: patch %s not found", hdev->name, fw_name);
1516 return 0;
1517 }
1518
1519 /* Reset */
1520 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
1521 if (IS_ERR(skb)) {
1522 ret = PTR_ERR(skb);
1523 BT_ERR("%s: HCI_OP_RESET failed (%ld)", hdev->name, ret);
1524 goto done;
1525 }
1526 kfree_skb(skb);
1527
1528 /* Read Local Version Info */
1529 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1530 HCI_INIT_TIMEOUT);
1531 if (IS_ERR(skb)) {
1532 ret = PTR_ERR(skb);
1533 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION failed (%ld)",
1534 hdev->name, ret);
1535 goto done;
1536 }
1537
1538 if (skb->len != sizeof(*ver)) {
1539 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION event length mismatch",
1540 hdev->name);
1541 kfree_skb(skb);
1542 ret = -EIO;
1543 goto done;
1544 }
1545
1546 ver = (struct hci_rp_read_local_version *) skb->data;
1547 BT_INFO("%s: BCM: patching hci_ver=%02x hci_rev=%04x lmp_ver=%02x "
1548 "lmp_subver=%04x", hdev->name, ver->hci_ver, ver->hci_rev,
1549 ver->lmp_ver, ver->lmp_subver);
1550 kfree_skb(skb);
1551
1552 /* Start Download */
1553 skb = __hci_cmd_sync(hdev, 0xfc2e, 0, NULL, HCI_INIT_TIMEOUT);
1554 if (IS_ERR(skb)) {
1555 ret = PTR_ERR(skb);
1556 BT_ERR("%s: BCM: Download Minidrv command failed (%ld)",
1557 hdev->name, ret);
1558 goto reset_fw;
1559 }
1560 kfree_skb(skb);
1561
1562 /* 50 msec delay after Download Minidrv completes */
1563 msleep(50);
1564
1565 fw_ptr = fw->data;
1566 fw_size = fw->size;
1567
1568 while (fw_size >= sizeof(*cmd)) {
1569 cmd = (struct hci_command_hdr *) fw_ptr;
1570 fw_ptr += sizeof(*cmd);
1571 fw_size -= sizeof(*cmd);
1572
1573 if (fw_size < cmd->plen) {
1574 BT_ERR("%s: BCM: patch %s is corrupted",
1575 hdev->name, fw_name);
1576 ret = -EINVAL;
1577 goto reset_fw;
1578 }
1579
1580 cmd_param = fw_ptr;
1581 fw_ptr += cmd->plen;
1582 fw_size -= cmd->plen;
1583
1584 opcode = le16_to_cpu(cmd->opcode);
1585
1586 skb = __hci_cmd_sync(hdev, opcode, cmd->plen, cmd_param,
1587 HCI_INIT_TIMEOUT);
1588 if (IS_ERR(skb)) {
1589 ret = PTR_ERR(skb);
1590 BT_ERR("%s: BCM: patch command %04x failed (%ld)",
1591 hdev->name, opcode, ret);
1592 goto reset_fw;
1593 }
1594 kfree_skb(skb);
1595 }
1596
1597 /* 250 msec delay after Launch Ram completes */
1598 msleep(250);
1599
1600 reset_fw:
1601 /* Reset */
1602 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
1603 if (IS_ERR(skb)) {
1604 ret = PTR_ERR(skb);
1605 BT_ERR("%s: HCI_OP_RESET failed (%ld)", hdev->name, ret);
1606 goto done;
1607 }
1608 kfree_skb(skb);
1609
1610 /* Read Local Version Info */
1611 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1612 HCI_INIT_TIMEOUT);
1613 if (IS_ERR(skb)) {
1614 ret = PTR_ERR(skb);
1615 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION failed (%ld)",
1616 hdev->name, ret);
1617 goto done;
1618 }
1619
1620 if (skb->len != sizeof(*ver)) {
1621 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION event length mismatch",
1622 hdev->name);
1623 kfree_skb(skb);
1624 ret = -EIO;
1625 goto done;
1626 }
1627
1628 ver = (struct hci_rp_read_local_version *) skb->data;
1629 BT_INFO("%s: BCM: firmware hci_ver=%02x hci_rev=%04x lmp_ver=%02x "
1630 "lmp_subver=%04x", hdev->name, ver->hci_ver, ver->hci_rev,
1631 ver->lmp_ver, ver->lmp_subver);
1632 kfree_skb(skb);
1633
1634 /* Read BD Address */
1635 skb = __hci_cmd_sync(hdev, HCI_OP_READ_BD_ADDR, 0, NULL,
1636 HCI_INIT_TIMEOUT);
1637 if (IS_ERR(skb)) {
1638 ret = PTR_ERR(skb);
1639 BT_ERR("%s: HCI_OP_READ_BD_ADDR failed (%ld)",
1640 hdev->name, ret);
1641 goto done;
1642 }
1643
1644 if (skb->len != sizeof(*bda)) {
1645 BT_ERR("%s: HCI_OP_READ_BD_ADDR event length mismatch",
1646 hdev->name);
1647 kfree_skb(skb);
1648 ret = -EIO;
1649 goto done;
1650 }
1651
1652 bda = (struct hci_rp_read_bd_addr *) skb->data;
1653 if (bda->status) {
1654 BT_ERR("%s: HCI_OP_READ_BD_ADDR error status (%02x)",
1655 hdev->name, bda->status);
1656 kfree_skb(skb);
1657 ret = -bt_to_errno(bda->status);
1658 goto done;
1659 }
1660
1661 /* The address 00:20:70:02:A0:00 indicates a BCM20702A0 controller
1662 * with no configured address.
1663 */
1664 if (!bacmp(&bda->bdaddr, BDADDR_BCM20702A0)) {
1665 BT_INFO("%s: BCM: using default device address (%pMR)",
1666 hdev->name, &bda->bdaddr);
1667 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
1668 }
1669
1670 kfree_skb(skb);
1671
1672 done:
1673 release_firmware(fw);
1674
1675 return ret;
1676 }
1677
1678 static int btusb_set_bdaddr_bcm(struct hci_dev *hdev, const bdaddr_t *bdaddr)
1679 {
1680 struct sk_buff *skb;
1681 long ret;
1682
1683 skb = __hci_cmd_sync(hdev, 0xfc01, 6, bdaddr, HCI_INIT_TIMEOUT);
1684 if (IS_ERR(skb)) {
1685 ret = PTR_ERR(skb);
1686 BT_ERR("%s: BCM: Change address command failed (%ld)",
1687 hdev->name, ret);
1688 return ret;
1689 }
1690 kfree_skb(skb);
1691
1692 return 0;
1693 }
1694
1695 static int btusb_probe(struct usb_interface *intf,
1696 const struct usb_device_id *id)
1697 {
1698 struct usb_endpoint_descriptor *ep_desc;
1699 struct btusb_data *data;
1700 struct hci_dev *hdev;
1701 int i, err;
1702
1703 BT_DBG("intf %p id %p", intf, id);
1704
1705 /* interface numbers are hardcoded in the spec */
1706 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
1707 return -ENODEV;
1708
1709 if (!id->driver_info) {
1710 const struct usb_device_id *match;
1711 match = usb_match_id(intf, blacklist_table);
1712 if (match)
1713 id = match;
1714 }
1715
1716 if (id->driver_info == BTUSB_IGNORE)
1717 return -ENODEV;
1718
1719 if (id->driver_info & BTUSB_ATH3012) {
1720 struct usb_device *udev = interface_to_usbdev(intf);
1721
1722 /* Old firmware would otherwise let ath3k driver load
1723 * patch and sysconfig files */
1724 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
1725 return -ENODEV;
1726 }
1727
1728 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
1729 if (!data)
1730 return -ENOMEM;
1731
1732 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1733 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1734
1735 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
1736 data->intr_ep = ep_desc;
1737 continue;
1738 }
1739
1740 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
1741 data->bulk_tx_ep = ep_desc;
1742 continue;
1743 }
1744
1745 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
1746 data->bulk_rx_ep = ep_desc;
1747 continue;
1748 }
1749 }
1750
1751 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
1752 return -ENODEV;
1753
1754 data->cmdreq_type = USB_TYPE_CLASS;
1755
1756 data->udev = interface_to_usbdev(intf);
1757 data->intf = intf;
1758
1759 spin_lock_init(&data->lock);
1760
1761 INIT_WORK(&data->work, btusb_work);
1762 INIT_WORK(&data->waker, btusb_waker);
1763 spin_lock_init(&data->txlock);
1764
1765 init_usb_anchor(&data->tx_anchor);
1766 init_usb_anchor(&data->intr_anchor);
1767 init_usb_anchor(&data->bulk_anchor);
1768 init_usb_anchor(&data->isoc_anchor);
1769 init_usb_anchor(&data->deferred);
1770
1771 hdev = hci_alloc_dev();
1772 if (!hdev)
1773 return -ENOMEM;
1774
1775 hdev->bus = HCI_USB;
1776 hci_set_drvdata(hdev, data);
1777
1778 data->hdev = hdev;
1779
1780 SET_HCIDEV_DEV(hdev, &intf->dev);
1781
1782 hdev->open = btusb_open;
1783 hdev->close = btusb_close;
1784 hdev->flush = btusb_flush;
1785 hdev->send = btusb_send_frame;
1786 hdev->notify = btusb_notify;
1787
1788 if (id->driver_info & BTUSB_BCM92035)
1789 hdev->setup = btusb_setup_bcm92035;
1790
1791 if (id->driver_info & BTUSB_BCM_PATCHRAM) {
1792 hdev->setup = btusb_setup_bcm_patchram;
1793 hdev->set_bdaddr = btusb_set_bdaddr_bcm;
1794 }
1795
1796 if (id->driver_info & BTUSB_INTEL) {
1797 hdev->setup = btusb_setup_intel;
1798 hdev->set_bdaddr = btusb_set_bdaddr_intel;
1799 }
1800
1801 if (id->driver_info & BTUSB_MARVELL)
1802 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
1803
1804 if (id->driver_info & BTUSB_INTEL_BOOT)
1805 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
1806
1807 /* Interface numbers are hardcoded in the specification */
1808 data->isoc = usb_ifnum_to_if(data->udev, 1);
1809
1810 if (!reset)
1811 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
1812
1813 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
1814 if (!disable_scofix)
1815 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
1816 }
1817
1818 if (id->driver_info & BTUSB_BROKEN_ISOC)
1819 data->isoc = NULL;
1820
1821 if (id->driver_info & BTUSB_DIGIANSWER) {
1822 data->cmdreq_type = USB_TYPE_VENDOR;
1823 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
1824 }
1825
1826 if (id->driver_info & BTUSB_CSR) {
1827 struct usb_device *udev = data->udev;
1828 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
1829
1830 /* Old firmware would otherwise execute USB reset */
1831 if (bcdDevice < 0x117)
1832 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
1833
1834 /* Fake CSR devices with broken commands */
1835 if (bcdDevice <= 0x100)
1836 hdev->setup = btusb_setup_csr;
1837 }
1838
1839 if (id->driver_info & BTUSB_SNIFFER) {
1840 struct usb_device *udev = data->udev;
1841
1842 /* New sniffer firmware has crippled HCI interface */
1843 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
1844 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
1845 }
1846
1847 if (id->driver_info & BTUSB_INTEL_BOOT) {
1848 /* A bug in the bootloader causes that interrupt interface is
1849 * only enabled after receiving SetInterface(0, AltSetting=0).
1850 */
1851 err = usb_set_interface(data->udev, 0, 0);
1852 if (err < 0) {
1853 BT_ERR("failed to set interface 0, alt 0 %d", err);
1854 hci_free_dev(hdev);
1855 return err;
1856 }
1857 }
1858
1859 if (data->isoc) {
1860 err = usb_driver_claim_interface(&btusb_driver,
1861 data->isoc, data);
1862 if (err < 0) {
1863 hci_free_dev(hdev);
1864 return err;
1865 }
1866 }
1867
1868 err = hci_register_dev(hdev);
1869 if (err < 0) {
1870 hci_free_dev(hdev);
1871 return err;
1872 }
1873
1874 usb_set_intfdata(intf, data);
1875
1876 return 0;
1877 }
1878
1879 static void btusb_disconnect(struct usb_interface *intf)
1880 {
1881 struct btusb_data *data = usb_get_intfdata(intf);
1882 struct hci_dev *hdev;
1883
1884 BT_DBG("intf %p", intf);
1885
1886 if (!data)
1887 return;
1888
1889 hdev = data->hdev;
1890 usb_set_intfdata(data->intf, NULL);
1891
1892 if (data->isoc)
1893 usb_set_intfdata(data->isoc, NULL);
1894
1895 hci_unregister_dev(hdev);
1896
1897 if (intf == data->isoc)
1898 usb_driver_release_interface(&btusb_driver, data->intf);
1899 else if (data->isoc)
1900 usb_driver_release_interface(&btusb_driver, data->isoc);
1901
1902 hci_free_dev(hdev);
1903 }
1904
1905 #ifdef CONFIG_PM
1906 static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
1907 {
1908 struct btusb_data *data = usb_get_intfdata(intf);
1909
1910 BT_DBG("intf %p", intf);
1911
1912 if (data->suspend_count++)
1913 return 0;
1914
1915 spin_lock_irq(&data->txlock);
1916 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
1917 set_bit(BTUSB_SUSPENDING, &data->flags);
1918 spin_unlock_irq(&data->txlock);
1919 } else {
1920 spin_unlock_irq(&data->txlock);
1921 data->suspend_count--;
1922 return -EBUSY;
1923 }
1924
1925 cancel_work_sync(&data->work);
1926
1927 btusb_stop_traffic(data);
1928 usb_kill_anchored_urbs(&data->tx_anchor);
1929
1930 return 0;
1931 }
1932
1933 static void play_deferred(struct btusb_data *data)
1934 {
1935 struct urb *urb;
1936 int err;
1937
1938 while ((urb = usb_get_from_anchor(&data->deferred))) {
1939 err = usb_submit_urb(urb, GFP_ATOMIC);
1940 if (err < 0)
1941 break;
1942
1943 data->tx_in_flight++;
1944 }
1945 usb_scuttle_anchored_urbs(&data->deferred);
1946 }
1947
1948 static int btusb_resume(struct usb_interface *intf)
1949 {
1950 struct btusb_data *data = usb_get_intfdata(intf);
1951 struct hci_dev *hdev = data->hdev;
1952 int err = 0;
1953
1954 BT_DBG("intf %p", intf);
1955
1956 if (--data->suspend_count)
1957 return 0;
1958
1959 if (!test_bit(HCI_RUNNING, &hdev->flags))
1960 goto done;
1961
1962 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
1963 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
1964 if (err < 0) {
1965 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1966 goto failed;
1967 }
1968 }
1969
1970 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
1971 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
1972 if (err < 0) {
1973 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
1974 goto failed;
1975 }
1976
1977 btusb_submit_bulk_urb(hdev, GFP_NOIO);
1978 }
1979
1980 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1981 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
1982 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1983 else
1984 btusb_submit_isoc_urb(hdev, GFP_NOIO);
1985 }
1986
1987 spin_lock_irq(&data->txlock);
1988 play_deferred(data);
1989 clear_bit(BTUSB_SUSPENDING, &data->flags);
1990 spin_unlock_irq(&data->txlock);
1991 schedule_work(&data->work);
1992
1993 return 0;
1994
1995 failed:
1996 usb_scuttle_anchored_urbs(&data->deferred);
1997 done:
1998 spin_lock_irq(&data->txlock);
1999 clear_bit(BTUSB_SUSPENDING, &data->flags);
2000 spin_unlock_irq(&data->txlock);
2001
2002 return err;
2003 }
2004 #endif
2005
2006 static struct usb_driver btusb_driver = {
2007 .name = "btusb",
2008 .probe = btusb_probe,
2009 .disconnect = btusb_disconnect,
2010 #ifdef CONFIG_PM
2011 .suspend = btusb_suspend,
2012 .resume = btusb_resume,
2013 #endif
2014 .id_table = btusb_table,
2015 .supports_autosuspend = 1,
2016 .disable_hub_initiated_lpm = 1,
2017 };
2018
2019 module_usb_driver(btusb_driver);
2020
2021 module_param(disable_scofix, bool, 0644);
2022 MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
2023
2024 module_param(force_scofix, bool, 0644);
2025 MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
2026
2027 module_param(reset, bool, 0644);
2028 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
2029
2030 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
2031 MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
2032 MODULE_VERSION(VERSION);
2033 MODULE_LICENSE("GPL");
This page took 0.079137 seconds and 4 git commands to generate.