btusb.c 23 KB

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