btusb.c 23 KB

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