blob: e5fa4377444e91f05ba4677dcb1fd453d3e4f264 [file] [log] [blame]
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001/*
2 *
3 * Generic Bluetooth USB driver
4 *
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02005 * Copyright (C) 2005-2008 Marcel Holtmann <marcel@holtmann.org>
Marcel Holtmann5e23b922007-10-20 14:12:34 +02006 *
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
24#include <linux/kernel.h>
25#include <linux/module.h>
26#include <linux/init.h>
27#include <linux/slab.h>
28#include <linux/types.h>
29#include <linux/sched.h>
30#include <linux/errno.h>
31#include <linux/skbuff.h>
32
33#include <linux/usb.h>
34
35#include <net/bluetooth/bluetooth.h>
36#include <net/bluetooth/hci_core.h>
37
Oliver Neukum7bee5492009-08-24 23:44:59 +020038#define VERSION "0.6"
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020039
40static int ignore_dga;
41static int ignore_csr;
42static int ignore_sniffer;
43static int disable_scofix;
44static int force_scofix;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +010045
46static int reset = 1;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020047
48static struct usb_driver btusb_driver;
49
50#define BTUSB_IGNORE 0x01
Marcel Holtmann7a9d4022008-11-30 12:17:26 +010051#define BTUSB_DIGIANSWER 0x02
52#define BTUSB_CSR 0x04
53#define BTUSB_SNIFFER 0x08
54#define BTUSB_BCM92035 0x10
55#define BTUSB_BROKEN_ISOC 0x20
56#define BTUSB_WRONG_SCO_MTU 0x40
Marcel Holtmann5e23b922007-10-20 14:12:34 +020057
58static struct usb_device_id btusb_table[] = {
59 /* Generic Bluetooth USB device */
60 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
61
Nobuhiro Iwamatsu3cd01972010-08-20 16:24:07 +090062 /* Apple MacBookPro 7,1 */
63 { USB_DEVICE(0x05ac, 0x8213) },
64
Cyril Lacoux0a79f672010-07-14 10:29:27 +040065 /* Apple iMac11,1 */
66 { USB_DEVICE(0x05ac, 0x8215) },
67
Nobuhiro Iwamatsu9c047152010-08-20 16:24:06 +090068 /* Apple MacBookPro6,2 */
69 { USB_DEVICE(0x05ac, 0x8218) },
70
Edgar (gimli) Hucek3e3ede72010-11-04 08:04:33 +010071 /* Apple MacBookAir3,1, MacBookAir3,2 */
72 { USB_DEVICE(0x05ac, 0x821b) },
73
Pieter-Augustijn Van Malleghemefb14972011-09-07 02:28:10 -040074 /* Apple MacBookAir4,1 */
75 { USB_DEVICE(0x05ac, 0x821f) },
76
Marc-Antoine Perennou88d377b2011-03-24 14:51:21 -030077 /* Apple MacBookPro8,2 */
78 { USB_DEVICE(0x05ac, 0x821a) },
79
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020080 /* AVM BlueFRITZ! USB v2.0 */
81 { USB_DEVICE(0x057c, 0x3800) },
82
83 /* Bluetooth Ultraport Module from IBM */
84 { USB_DEVICE(0x04bf, 0x030a) },
85
86 /* ALPS Modules with non-standard id */
87 { USB_DEVICE(0x044e, 0x3001) },
88 { USB_DEVICE(0x044e, 0x3002) },
89
90 /* Ericsson with non-standard id */
91 { USB_DEVICE(0x0bdb, 0x1002) },
92
93 /* Canyon CN-BTU1 with HID interfaces */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +010094 { USB_DEVICE(0x0c10, 0x0000) },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020095
Marcel Holtmann5e23b922007-10-20 14:12:34 +020096 { } /* Terminating entry */
97};
98
99MODULE_DEVICE_TABLE(usb, btusb_table);
100
101static struct usb_device_id blacklist_table[] = {
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200102 /* CSR BlueCore devices */
103 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
104
105 /* Broadcom BCM2033 without firmware */
106 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
107
Bala Shanmugambe931122010-11-26 17:35:46 +0530108 /* Atheros 3011 with sflash firmware */
109 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
Andy Ross2a7bccc2011-05-09 16:11:16 -0700110 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530111
Cho, Yu-Chen509e7862011-01-26 17:10:59 +0800112 /* Atheros AR9285 Malbec with sflash firmware */
113 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
114
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530115 /* Atheros 3012 with sflash firmware */
116 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_IGNORE },
117
Cho, Yu-Chene9036e32011-02-15 10:20:07 +0800118 /* Atheros AR5BBU12 with sflash firmware */
119 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
120
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200121 /* Broadcom BCM2035 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100122 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
123 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
124 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200125
126 /* Broadcom BCM2045 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100127 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
128 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmannbdbef3d2008-09-23 00:16:35 +0200129
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200130 /* IBM/Lenovo ThinkPad with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100131 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
132 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200133
134 /* HP laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100135 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200136
137 /* Dell laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100138 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200139
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100140 /* Dell Wireless 370 and 410 devices */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100141 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100142 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200143
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100144 /* Belkin F8T012 and F8T013 devices */
145 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
146 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200147
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100148 /* Asus WL-BTD202 device */
149 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
150
151 /* Kensington Bluetooth USB adapter */
152 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
153
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200154 /* RTX Telecom based adapters with buggy SCO support */
155 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
156 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
157
158 /* CONWISE Technology based adapters with buggy SCO support */
159 { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
160
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200161 /* Digianswer devices */
162 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
163 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
164
165 /* CSR BlueCore Bluetooth Sniffer */
166 { USB_DEVICE(0x0a12, 0x0002), .driver_info = BTUSB_SNIFFER },
167
168 /* Frontline ComProbe Bluetooth Sniffer */
169 { USB_DEVICE(0x16d3, 0x0002), .driver_info = BTUSB_SNIFFER },
170
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200171 { } /* Terminating entry */
172};
173
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200174#define BTUSB_MAX_ISOC_FRAMES 10
175
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200176#define BTUSB_INTR_RUNNING 0
177#define BTUSB_BULK_RUNNING 1
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200178#define BTUSB_ISOC_RUNNING 2
Oliver Neukum7bee5492009-08-24 23:44:59 +0200179#define BTUSB_SUSPENDING 3
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300180#define BTUSB_DID_ISO_RESUME 4
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200181
182struct btusb_data {
183 struct hci_dev *hdev;
184 struct usb_device *udev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200185 struct usb_interface *intf;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200186 struct usb_interface *isoc;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200187
188 spinlock_t lock;
189
190 unsigned long flags;
191
192 struct work_struct work;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200193 struct work_struct waker;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200194
195 struct usb_anchor tx_anchor;
196 struct usb_anchor intr_anchor;
197 struct usb_anchor bulk_anchor;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200198 struct usb_anchor isoc_anchor;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200199 struct usb_anchor deferred;
200 int tx_in_flight;
201 spinlock_t txlock;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200202
203 struct usb_endpoint_descriptor *intr_ep;
204 struct usb_endpoint_descriptor *bulk_tx_ep;
205 struct usb_endpoint_descriptor *bulk_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200206 struct usb_endpoint_descriptor *isoc_tx_ep;
207 struct usb_endpoint_descriptor *isoc_rx_ep;
208
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100209 __u8 cmdreq_type;
210
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100211 unsigned int sco_num;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200212 int isoc_altsetting;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +0100213 int suspend_count;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200214};
215
Oliver Neukum7bee5492009-08-24 23:44:59 +0200216static int inc_tx(struct btusb_data *data)
217{
218 unsigned long flags;
219 int rv;
220
221 spin_lock_irqsave(&data->txlock, flags);
222 rv = test_bit(BTUSB_SUSPENDING, &data->flags);
223 if (!rv)
224 data->tx_in_flight++;
225 spin_unlock_irqrestore(&data->txlock, flags);
226
227 return rv;
228}
229
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200230static void btusb_intr_complete(struct urb *urb)
231{
232 struct hci_dev *hdev = urb->context;
233 struct btusb_data *data = hdev->driver_data;
234 int err;
235
236 BT_DBG("%s urb %p status %d count %d", hdev->name,
237 urb, urb->status, urb->actual_length);
238
239 if (!test_bit(HCI_RUNNING, &hdev->flags))
240 return;
241
242 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200243 hdev->stat.byte_rx += urb->actual_length;
244
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200245 if (hci_recv_fragment(hdev, HCI_EVENT_PKT,
246 urb->transfer_buffer,
247 urb->actual_length) < 0) {
248 BT_ERR("%s corrupted event packet", hdev->name);
249 hdev->stat.err_rx++;
250 }
251 }
252
253 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
254 return;
255
Oliver Neukum7bee5492009-08-24 23:44:59 +0200256 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200257 usb_anchor_urb(urb, &data->intr_anchor);
258
259 err = usb_submit_urb(urb, GFP_ATOMIC);
260 if (err < 0) {
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100261 if (err != -EPERM)
262 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200263 hdev->name, urb, -err);
264 usb_unanchor_urb(urb);
265 }
266}
267
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100268static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200269{
270 struct btusb_data *data = hdev->driver_data;
271 struct urb *urb;
272 unsigned char *buf;
273 unsigned int pipe;
274 int err, size;
275
276 BT_DBG("%s", hdev->name);
277
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200278 if (!data->intr_ep)
279 return -ENODEV;
280
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100281 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200282 if (!urb)
283 return -ENOMEM;
284
285 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
286
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100287 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200288 if (!buf) {
289 usb_free_urb(urb);
290 return -ENOMEM;
291 }
292
293 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
294
295 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
296 btusb_intr_complete, hdev,
297 data->intr_ep->bInterval);
298
299 urb->transfer_flags |= URB_FREE_BUFFER;
300
301 usb_anchor_urb(urb, &data->intr_anchor);
302
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100303 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200304 if (err < 0) {
305 BT_ERR("%s urb %p submission failed (%d)",
306 hdev->name, urb, -err);
307 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200308 }
309
310 usb_free_urb(urb);
311
312 return err;
313}
314
315static void btusb_bulk_complete(struct urb *urb)
316{
317 struct hci_dev *hdev = urb->context;
318 struct btusb_data *data = hdev->driver_data;
319 int err;
320
321 BT_DBG("%s urb %p status %d count %d", hdev->name,
322 urb, urb->status, urb->actual_length);
323
324 if (!test_bit(HCI_RUNNING, &hdev->flags))
325 return;
326
327 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200328 hdev->stat.byte_rx += urb->actual_length;
329
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200330 if (hci_recv_fragment(hdev, HCI_ACLDATA_PKT,
331 urb->transfer_buffer,
332 urb->actual_length) < 0) {
333 BT_ERR("%s corrupted ACL packet", hdev->name);
334 hdev->stat.err_rx++;
335 }
336 }
337
338 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
339 return;
340
341 usb_anchor_urb(urb, &data->bulk_anchor);
Oliver Neukum652fd782009-12-16 19:23:43 +0100342 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200343
344 err = usb_submit_urb(urb, GFP_ATOMIC);
345 if (err < 0) {
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100346 if (err != -EPERM)
347 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200348 hdev->name, urb, -err);
349 usb_unanchor_urb(urb);
350 }
351}
352
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100353static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200354{
355 struct btusb_data *data = hdev->driver_data;
356 struct urb *urb;
357 unsigned char *buf;
358 unsigned int pipe;
Vikram Kandukuri290ba202009-07-02 14:31:59 +0530359 int err, size = HCI_MAX_FRAME_SIZE;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200360
361 BT_DBG("%s", hdev->name);
362
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200363 if (!data->bulk_rx_ep)
364 return -ENODEV;
365
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100366 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200367 if (!urb)
368 return -ENOMEM;
369
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100370 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200371 if (!buf) {
372 usb_free_urb(urb);
373 return -ENOMEM;
374 }
375
376 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
377
378 usb_fill_bulk_urb(urb, data->udev, pipe,
379 buf, size, btusb_bulk_complete, hdev);
380
381 urb->transfer_flags |= URB_FREE_BUFFER;
382
Oliver Neukum7bee5492009-08-24 23:44:59 +0200383 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200384 usb_anchor_urb(urb, &data->bulk_anchor);
385
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100386 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200387 if (err < 0) {
388 BT_ERR("%s urb %p submission failed (%d)",
389 hdev->name, urb, -err);
390 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200391 }
392
393 usb_free_urb(urb);
394
395 return err;
396}
397
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200398static void btusb_isoc_complete(struct urb *urb)
399{
400 struct hci_dev *hdev = urb->context;
401 struct btusb_data *data = hdev->driver_data;
402 int i, err;
403
404 BT_DBG("%s urb %p status %d count %d", hdev->name,
405 urb, urb->status, urb->actual_length);
406
407 if (!test_bit(HCI_RUNNING, &hdev->flags))
408 return;
409
410 if (urb->status == 0) {
411 for (i = 0; i < urb->number_of_packets; i++) {
412 unsigned int offset = urb->iso_frame_desc[i].offset;
413 unsigned int length = urb->iso_frame_desc[i].actual_length;
414
415 if (urb->iso_frame_desc[i].status)
416 continue;
417
418 hdev->stat.byte_rx += length;
419
420 if (hci_recv_fragment(hdev, HCI_SCODATA_PKT,
421 urb->transfer_buffer + offset,
422 length) < 0) {
423 BT_ERR("%s corrupted SCO packet", hdev->name);
424 hdev->stat.err_rx++;
425 }
426 }
427 }
428
429 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
430 return;
431
432 usb_anchor_urb(urb, &data->isoc_anchor);
433
434 err = usb_submit_urb(urb, GFP_ATOMIC);
435 if (err < 0) {
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100436 if (err != -EPERM)
437 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200438 hdev->name, urb, -err);
439 usb_unanchor_urb(urb);
440 }
441}
442
Jesper Juhl42b16b32011-01-17 00:09:38 +0100443static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200444{
445 int i, offset = 0;
446
447 BT_DBG("len %d mtu %d", len, mtu);
448
449 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
450 i++, offset += mtu, len -= mtu) {
451 urb->iso_frame_desc[i].offset = offset;
452 urb->iso_frame_desc[i].length = mtu;
453 }
454
455 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
456 urb->iso_frame_desc[i].offset = offset;
457 urb->iso_frame_desc[i].length = len;
458 i++;
459 }
460
461 urb->number_of_packets = i;
462}
463
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100464static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200465{
466 struct btusb_data *data = hdev->driver_data;
467 struct urb *urb;
468 unsigned char *buf;
469 unsigned int pipe;
470 int err, size;
471
472 BT_DBG("%s", hdev->name);
473
474 if (!data->isoc_rx_ep)
475 return -ENODEV;
476
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100477 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200478 if (!urb)
479 return -ENOMEM;
480
481 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
482 BTUSB_MAX_ISOC_FRAMES;
483
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100484 buf = kmalloc(size, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200485 if (!buf) {
486 usb_free_urb(urb);
487 return -ENOMEM;
488 }
489
490 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
491
492 urb->dev = data->udev;
493 urb->pipe = pipe;
494 urb->context = hdev;
495 urb->complete = btusb_isoc_complete;
496 urb->interval = data->isoc_rx_ep->bInterval;
497
498 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
499 urb->transfer_buffer = buf;
500 urb->transfer_buffer_length = size;
501
502 __fill_isoc_descriptor(urb, size,
503 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
504
505 usb_anchor_urb(urb, &data->isoc_anchor);
506
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100507 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200508 if (err < 0) {
509 BT_ERR("%s urb %p submission failed (%d)",
510 hdev->name, urb, -err);
511 usb_unanchor_urb(urb);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200512 }
513
514 usb_free_urb(urb);
515
516 return err;
517}
518
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200519static void btusb_tx_complete(struct urb *urb)
520{
521 struct sk_buff *skb = urb->context;
522 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200523 struct btusb_data *data = hdev->driver_data;
524
525 BT_DBG("%s urb %p status %d count %d", hdev->name,
526 urb, urb->status, urb->actual_length);
527
528 if (!test_bit(HCI_RUNNING, &hdev->flags))
529 goto done;
530
531 if (!urb->status)
532 hdev->stat.byte_tx += urb->transfer_buffer_length;
533 else
534 hdev->stat.err_tx++;
535
536done:
537 spin_lock(&data->txlock);
538 data->tx_in_flight--;
539 spin_unlock(&data->txlock);
540
541 kfree(urb->setup_packet);
542
543 kfree_skb(skb);
544}
545
546static void btusb_isoc_tx_complete(struct urb *urb)
547{
548 struct sk_buff *skb = urb->context;
549 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200550
551 BT_DBG("%s urb %p status %d count %d", hdev->name,
552 urb, urb->status, urb->actual_length);
553
554 if (!test_bit(HCI_RUNNING, &hdev->flags))
555 goto done;
556
557 if (!urb->status)
558 hdev->stat.byte_tx += urb->transfer_buffer_length;
559 else
560 hdev->stat.err_tx++;
561
562done:
563 kfree(urb->setup_packet);
564
565 kfree_skb(skb);
566}
567
568static int btusb_open(struct hci_dev *hdev)
569{
570 struct btusb_data *data = hdev->driver_data;
571 int err;
572
573 BT_DBG("%s", hdev->name);
574
Oliver Neukum7bee5492009-08-24 23:44:59 +0200575 err = usb_autopm_get_interface(data->intf);
576 if (err < 0)
577 return err;
578
579 data->intf->needs_remote_wakeup = 1;
580
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200581 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +0200582 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200583
584 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +0200585 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200586
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100587 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100588 if (err < 0)
589 goto failed;
590
591 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200592 if (err < 0) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100593 usb_kill_anchored_urbs(&data->intr_anchor);
594 goto failed;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200595 }
596
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100597 set_bit(BTUSB_BULK_RUNNING, &data->flags);
598 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
599
Oliver Neukum7bee5492009-08-24 23:44:59 +0200600done:
601 usb_autopm_put_interface(data->intf);
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100602 return 0;
603
604failed:
605 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
606 clear_bit(HCI_RUNNING, &hdev->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200607 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200608 return err;
609}
610
Oliver Neukum7bee5492009-08-24 23:44:59 +0200611static void btusb_stop_traffic(struct btusb_data *data)
612{
613 usb_kill_anchored_urbs(&data->intr_anchor);
614 usb_kill_anchored_urbs(&data->bulk_anchor);
615 usb_kill_anchored_urbs(&data->isoc_anchor);
616}
617
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200618static int btusb_close(struct hci_dev *hdev)
619{
620 struct btusb_data *data = hdev->driver_data;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200621 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200622
623 BT_DBG("%s", hdev->name);
624
625 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
626 return 0;
627
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200628 cancel_work_sync(&data->work);
Linus Torvalds404291a2009-11-11 13:32:29 -0800629 cancel_work_sync(&data->waker);
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200630
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200631 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200632 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200633 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200634
635 btusb_stop_traffic(data);
636 err = usb_autopm_get_interface(data->intf);
637 if (err < 0)
Oliver Neukum7b8e2c12009-11-13 14:26:23 +0100638 goto failed;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200639
640 data->intf->needs_remote_wakeup = 0;
641 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200642
Oliver Neukum7b8e2c12009-11-13 14:26:23 +0100643failed:
644 usb_scuttle_anchored_urbs(&data->deferred);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200645 return 0;
646}
647
648static int btusb_flush(struct hci_dev *hdev)
649{
650 struct btusb_data *data = hdev->driver_data;
651
652 BT_DBG("%s", hdev->name);
653
654 usb_kill_anchored_urbs(&data->tx_anchor);
655
656 return 0;
657}
658
659static int btusb_send_frame(struct sk_buff *skb)
660{
661 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
662 struct btusb_data *data = hdev->driver_data;
663 struct usb_ctrlrequest *dr;
664 struct urb *urb;
665 unsigned int pipe;
666 int err;
667
668 BT_DBG("%s", hdev->name);
669
670 if (!test_bit(HCI_RUNNING, &hdev->flags))
671 return -EBUSY;
672
673 switch (bt_cb(skb)->pkt_type) {
674 case HCI_COMMAND_PKT:
675 urb = usb_alloc_urb(0, GFP_ATOMIC);
676 if (!urb)
677 return -ENOMEM;
678
679 dr = kmalloc(sizeof(*dr), GFP_ATOMIC);
680 if (!dr) {
681 usb_free_urb(urb);
682 return -ENOMEM;
683 }
684
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100685 dr->bRequestType = data->cmdreq_type;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200686 dr->bRequest = 0;
687 dr->wIndex = 0;
688 dr->wValue = 0;
689 dr->wLength = __cpu_to_le16(skb->len);
690
691 pipe = usb_sndctrlpipe(data->udev, 0x00);
692
693 usb_fill_control_urb(urb, data->udev, pipe, (void *) dr,
694 skb->data, skb->len, btusb_tx_complete, skb);
695
696 hdev->stat.cmd_tx++;
697 break;
698
699 case HCI_ACLDATA_PKT:
Vinicius Costa Gomes8693ac92011-03-14 18:20:33 -0300700 if (!data->bulk_tx_ep || (hdev->conn_hash.acl_num < 1 &&
701 hdev->conn_hash.le_num < 1))
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200702 return -ENODEV;
703
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200704 urb = usb_alloc_urb(0, GFP_ATOMIC);
705 if (!urb)
706 return -ENOMEM;
707
708 pipe = usb_sndbulkpipe(data->udev,
709 data->bulk_tx_ep->bEndpointAddress);
710
711 usb_fill_bulk_urb(urb, data->udev, pipe,
712 skb->data, skb->len, btusb_tx_complete, skb);
713
714 hdev->stat.acl_tx++;
715 break;
716
717 case HCI_SCODATA_PKT:
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200718 if (!data->isoc_tx_ep || hdev->conn_hash.sco_num < 1)
719 return -ENODEV;
720
721 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_ATOMIC);
722 if (!urb)
723 return -ENOMEM;
724
725 pipe = usb_sndisocpipe(data->udev,
726 data->isoc_tx_ep->bEndpointAddress);
727
Gustavo F. Padovan03c2d0e2011-02-14 18:53:43 -0300728 usb_fill_int_urb(urb, data->udev, pipe,
729 skb->data, skb->len, btusb_isoc_tx_complete,
730 skb, data->isoc_tx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200731
732 urb->transfer_flags = URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200733
734 __fill_isoc_descriptor(urb, skb->len,
735 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
736
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200737 hdev->stat.sco_tx++;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200738 goto skip_waking;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200739
740 default:
741 return -EILSEQ;
742 }
743
Oliver Neukum7bee5492009-08-24 23:44:59 +0200744 err = inc_tx(data);
745 if (err) {
746 usb_anchor_urb(urb, &data->deferred);
747 schedule_work(&data->waker);
748 err = 0;
749 goto done;
750 }
751
752skip_waking:
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200753 usb_anchor_urb(urb, &data->tx_anchor);
754
755 err = usb_submit_urb(urb, GFP_ATOMIC);
756 if (err < 0) {
757 BT_ERR("%s urb %p submission failed", hdev->name, urb);
758 kfree(urb->setup_packet);
759 usb_unanchor_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200760 } else {
761 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200762 }
763
764 usb_free_urb(urb);
765
Oliver Neukum7bee5492009-08-24 23:44:59 +0200766done:
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200767 return err;
768}
769
770static void btusb_destruct(struct hci_dev *hdev)
771{
772 struct btusb_data *data = hdev->driver_data;
773
774 BT_DBG("%s", hdev->name);
775
776 kfree(data);
777}
778
779static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
780{
781 struct btusb_data *data = hdev->driver_data;
782
783 BT_DBG("%s evt %d", hdev->name, evt);
784
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100785 if (hdev->conn_hash.sco_num != data->sco_num) {
786 data->sco_num = hdev->conn_hash.sco_num;
787 schedule_work(&data->work);
Marcel Holtmanna780efa2008-11-30 12:17:12 +0100788 }
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200789}
790
Jesper Juhl42b16b32011-01-17 00:09:38 +0100791static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200792{
793 struct btusb_data *data = hdev->driver_data;
794 struct usb_interface *intf = data->isoc;
795 struct usb_endpoint_descriptor *ep_desc;
796 int i, err;
797
798 if (!data->isoc)
799 return -ENODEV;
800
801 err = usb_set_interface(data->udev, 1, altsetting);
802 if (err < 0) {
803 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
804 return err;
805 }
806
807 data->isoc_altsetting = altsetting;
808
809 data->isoc_tx_ep = NULL;
810 data->isoc_rx_ep = NULL;
811
812 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
813 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
814
815 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
816 data->isoc_tx_ep = ep_desc;
817 continue;
818 }
819
820 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
821 data->isoc_rx_ep = ep_desc;
822 continue;
823 }
824 }
825
826 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
827 BT_ERR("%s invalid SCO descriptors", hdev->name);
828 return -ENODEV;
829 }
830
831 return 0;
832}
833
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200834static void btusb_work(struct work_struct *work)
835{
836 struct btusb_data *data = container_of(work, struct btusb_data, work);
837 struct hci_dev *hdev = data->hdev;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200838 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200839
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200840 if (hdev->conn_hash.sco_num > 0) {
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300841 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
Oliver Neukum8efdd0c2011-02-11 13:00:06 +0100842 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200843 if (err < 0) {
844 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
845 usb_kill_anchored_urbs(&data->isoc_anchor);
846 return;
847 }
848
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300849 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200850 }
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200851 if (data->isoc_altsetting != 2) {
852 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
853 usb_kill_anchored_urbs(&data->isoc_anchor);
854
855 if (__set_isoc_interface(hdev, 2) < 0)
856 return;
857 }
858
859 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100860 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200861 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
862 else
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100863 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200864 }
865 } else {
866 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
867 usb_kill_anchored_urbs(&data->isoc_anchor);
868
869 __set_isoc_interface(hdev, 0);
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300870 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
Oliver Neukum8efdd0c2011-02-11 13:00:06 +0100871 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200872 }
873}
874
Oliver Neukum7bee5492009-08-24 23:44:59 +0200875static void btusb_waker(struct work_struct *work)
876{
877 struct btusb_data *data = container_of(work, struct btusb_data, waker);
878 int err;
879
880 err = usb_autopm_get_interface(data->intf);
881 if (err < 0)
882 return;
883
884 usb_autopm_put_interface(data->intf);
885}
886
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200887static int btusb_probe(struct usb_interface *intf,
888 const struct usb_device_id *id)
889{
890 struct usb_endpoint_descriptor *ep_desc;
891 struct btusb_data *data;
892 struct hci_dev *hdev;
893 int i, err;
894
895 BT_DBG("intf %p id %p", intf, id);
896
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200897 /* interface numbers are hardcoded in the spec */
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200898 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
899 return -ENODEV;
900
901 if (!id->driver_info) {
902 const struct usb_device_id *match;
903 match = usb_match_id(intf, blacklist_table);
904 if (match)
905 id = match;
906 }
907
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200908 if (id->driver_info == BTUSB_IGNORE)
909 return -ENODEV;
910
911 if (ignore_dga && id->driver_info & BTUSB_DIGIANSWER)
912 return -ENODEV;
913
914 if (ignore_csr && id->driver_info & BTUSB_CSR)
915 return -ENODEV;
916
917 if (ignore_sniffer && id->driver_info & BTUSB_SNIFFER)
918 return -ENODEV;
919
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200920 data = kzalloc(sizeof(*data), GFP_KERNEL);
921 if (!data)
922 return -ENOMEM;
923
924 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
925 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
926
927 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
928 data->intr_ep = ep_desc;
929 continue;
930 }
931
932 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
933 data->bulk_tx_ep = ep_desc;
934 continue;
935 }
936
937 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
938 data->bulk_rx_ep = ep_desc;
939 continue;
940 }
941 }
942
943 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep) {
944 kfree(data);
945 return -ENODEV;
946 }
947
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100948 data->cmdreq_type = USB_TYPE_CLASS;
949
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200950 data->udev = interface_to_usbdev(intf);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200951 data->intf = intf;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200952
953 spin_lock_init(&data->lock);
954
955 INIT_WORK(&data->work, btusb_work);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200956 INIT_WORK(&data->waker, btusb_waker);
957 spin_lock_init(&data->txlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200958
959 init_usb_anchor(&data->tx_anchor);
960 init_usb_anchor(&data->intr_anchor);
961 init_usb_anchor(&data->bulk_anchor);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200962 init_usb_anchor(&data->isoc_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200963 init_usb_anchor(&data->deferred);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200964
965 hdev = hci_alloc_dev();
966 if (!hdev) {
967 kfree(data);
968 return -ENOMEM;
969 }
970
Marcel Holtmannc13854ce2010-02-08 15:27:07 +0100971 hdev->bus = HCI_USB;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200972 hdev->driver_data = data;
973
974 data->hdev = hdev;
975
976 SET_HCIDEV_DEV(hdev, &intf->dev);
977
978 hdev->open = btusb_open;
979 hdev->close = btusb_close;
980 hdev->flush = btusb_flush;
981 hdev->send = btusb_send_frame;
982 hdev->destruct = btusb_destruct;
983 hdev->notify = btusb_notify;
984
985 hdev->owner = THIS_MODULE;
986
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100987 /* Interface numbers are hardcoded in the specification */
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200988 data->isoc = usb_ifnum_to_if(data->udev, 1);
989
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100990 if (!reset)
991 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200992
993 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
994 if (!disable_scofix)
995 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
996 }
997
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200998 if (id->driver_info & BTUSB_BROKEN_ISOC)
999 data->isoc = NULL;
1000
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01001001 if (id->driver_info & BTUSB_DIGIANSWER) {
1002 data->cmdreq_type = USB_TYPE_VENDOR;
1003 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
1004 }
1005
1006 if (id->driver_info & BTUSB_CSR) {
1007 struct usb_device *udev = data->udev;
1008
1009 /* Old firmware would otherwise execute USB reset */
1010 if (le16_to_cpu(udev->descriptor.bcdDevice) < 0x117)
1011 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
1012 }
1013
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001014 if (id->driver_info & BTUSB_SNIFFER) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001015 struct usb_device *udev = data->udev;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001016
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01001017 /* New sniffer firmware has crippled HCI interface */
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001018 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
1019 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001020
1021 data->isoc = NULL;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001022 }
1023
1024 if (id->driver_info & BTUSB_BCM92035) {
1025 unsigned char cmd[] = { 0x3b, 0xfc, 0x01, 0x00 };
1026 struct sk_buff *skb;
1027
1028 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
1029 if (skb) {
1030 memcpy(skb_put(skb, sizeof(cmd)), cmd, sizeof(cmd));
1031 skb_queue_tail(&hdev->driver_init, skb);
1032 }
1033 }
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001034
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001035 if (data->isoc) {
1036 err = usb_driver_claim_interface(&btusb_driver,
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02001037 data->isoc, data);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001038 if (err < 0) {
1039 hci_free_dev(hdev);
1040 kfree(data);
1041 return err;
1042 }
1043 }
1044
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001045 err = hci_register_dev(hdev);
1046 if (err < 0) {
1047 hci_free_dev(hdev);
1048 kfree(data);
1049 return err;
1050 }
1051
1052 usb_set_intfdata(intf, data);
1053
1054 return 0;
1055}
1056
1057static void btusb_disconnect(struct usb_interface *intf)
1058{
1059 struct btusb_data *data = usb_get_intfdata(intf);
1060 struct hci_dev *hdev;
1061
1062 BT_DBG("intf %p", intf);
1063
1064 if (!data)
1065 return;
1066
1067 hdev = data->hdev;
1068
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02001069 __hci_dev_hold(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001070
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02001071 usb_set_intfdata(data->intf, NULL);
1072
1073 if (data->isoc)
1074 usb_set_intfdata(data->isoc, NULL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001075
1076 hci_unregister_dev(hdev);
1077
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02001078 if (intf == data->isoc)
1079 usb_driver_release_interface(&btusb_driver, data->intf);
1080 else if (data->isoc)
1081 usb_driver_release_interface(&btusb_driver, data->isoc);
1082
1083 __hci_dev_put(hdev);
1084
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001085 hci_free_dev(hdev);
1086}
1087
Oliver Neukum7bee5492009-08-24 23:44:59 +02001088#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001089static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
1090{
1091 struct btusb_data *data = usb_get_intfdata(intf);
1092
1093 BT_DBG("intf %p", intf);
1094
1095 if (data->suspend_count++)
1096 return 0;
1097
Oliver Neukum7bee5492009-08-24 23:44:59 +02001098 spin_lock_irq(&data->txlock);
Alan Sternfb34d532009-11-13 11:53:59 -05001099 if (!((message.event & PM_EVENT_AUTO) && data->tx_in_flight)) {
Oliver Neukum7bee5492009-08-24 23:44:59 +02001100 set_bit(BTUSB_SUSPENDING, &data->flags);
1101 spin_unlock_irq(&data->txlock);
1102 } else {
1103 spin_unlock_irq(&data->txlock);
1104 data->suspend_count--;
1105 return -EBUSY;
1106 }
1107
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001108 cancel_work_sync(&data->work);
1109
Oliver Neukum7bee5492009-08-24 23:44:59 +02001110 btusb_stop_traffic(data);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001111 usb_kill_anchored_urbs(&data->tx_anchor);
1112
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001113 return 0;
1114}
1115
Oliver Neukum7bee5492009-08-24 23:44:59 +02001116static void play_deferred(struct btusb_data *data)
1117{
1118 struct urb *urb;
1119 int err;
1120
1121 while ((urb = usb_get_from_anchor(&data->deferred))) {
1122 err = usb_submit_urb(urb, GFP_ATOMIC);
1123 if (err < 0)
1124 break;
1125
1126 data->tx_in_flight++;
1127 }
1128 usb_scuttle_anchored_urbs(&data->deferred);
1129}
1130
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001131static int btusb_resume(struct usb_interface *intf)
1132{
1133 struct btusb_data *data = usb_get_intfdata(intf);
1134 struct hci_dev *hdev = data->hdev;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001135 int err = 0;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001136
1137 BT_DBG("intf %p", intf);
1138
1139 if (--data->suspend_count)
1140 return 0;
1141
1142 if (!test_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02001143 goto done;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001144
1145 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
1146 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
1147 if (err < 0) {
1148 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001149 goto failed;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001150 }
1151 }
1152
1153 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001154 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
1155 if (err < 0) {
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001156 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001157 goto failed;
1158 }
1159
1160 btusb_submit_bulk_urb(hdev, GFP_NOIO);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001161 }
1162
1163 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1164 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
1165 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1166 else
1167 btusb_submit_isoc_urb(hdev, GFP_NOIO);
1168 }
1169
Oliver Neukum7bee5492009-08-24 23:44:59 +02001170 spin_lock_irq(&data->txlock);
1171 play_deferred(data);
1172 clear_bit(BTUSB_SUSPENDING, &data->flags);
1173 spin_unlock_irq(&data->txlock);
1174 schedule_work(&data->work);
1175
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001176 return 0;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001177
1178failed:
1179 usb_scuttle_anchored_urbs(&data->deferred);
1180done:
1181 spin_lock_irq(&data->txlock);
1182 clear_bit(BTUSB_SUSPENDING, &data->flags);
1183 spin_unlock_irq(&data->txlock);
1184
1185 return err;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001186}
Oliver Neukum7bee5492009-08-24 23:44:59 +02001187#endif
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001188
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001189static struct usb_driver btusb_driver = {
1190 .name = "btusb",
1191 .probe = btusb_probe,
1192 .disconnect = btusb_disconnect,
Oliver Neukum7bee5492009-08-24 23:44:59 +02001193#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001194 .suspend = btusb_suspend,
1195 .resume = btusb_resume,
Oliver Neukum7bee5492009-08-24 23:44:59 +02001196#endif
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001197 .id_table = btusb_table,
Oliver Neukum7bee5492009-08-24 23:44:59 +02001198 .supports_autosuspend = 1,
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001199};
1200
1201static int __init btusb_init(void)
1202{
1203 BT_INFO("Generic Bluetooth USB driver ver %s", VERSION);
1204
1205 return usb_register(&btusb_driver);
1206}
1207
1208static void __exit btusb_exit(void)
1209{
1210 usb_deregister(&btusb_driver);
1211}
1212
1213module_init(btusb_init);
1214module_exit(btusb_exit);
1215
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001216module_param(ignore_dga, bool, 0644);
1217MODULE_PARM_DESC(ignore_dga, "Ignore devices with id 08fd:0001");
1218
1219module_param(ignore_csr, bool, 0644);
1220MODULE_PARM_DESC(ignore_csr, "Ignore devices with id 0a12:0001");
1221
1222module_param(ignore_sniffer, bool, 0644);
1223MODULE_PARM_DESC(ignore_sniffer, "Ignore devices with id 0a12:0002");
1224
1225module_param(disable_scofix, bool, 0644);
1226MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
1227
1228module_param(force_scofix, bool, 0644);
1229MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
1230
1231module_param(reset, bool, 0644);
1232MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
1233
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001234MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
1235MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
1236MODULE_VERSION(VERSION);
1237MODULE_LICENSE("GPL");