btusb.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  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. { USB_DEVICE(0x0a5c, 0x2154), .driver_info = BTUSB_RESET },
  93. /* ANYCOM Bluetooth USB-200 and USB-250 */
  94. { USB_DEVICE(0x0a5c, 0x2111), .driver_info = BTUSB_RESET },
  95. /* HP laptop with Broadcom chip */
  96. { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_RESET | BTUSB_WRONG_SCO_MTU },
  97. /* Dell laptop with Broadcom chip */
  98. { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_RESET | BTUSB_WRONG_SCO_MTU },
  99. /* Dell Wireless 370 */
  100. { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_RESET | BTUSB_WRONG_SCO_MTU },
  101. /* Dell Wireless 410 */
  102. { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_RESET | BTUSB_WRONG_SCO_MTU },
  103. /* Microsoft Wireless Transceiver for Bluetooth 2.0 */
  104. { USB_DEVICE(0x045e, 0x009c), .driver_info = BTUSB_RESET },
  105. /* Kensington Bluetooth USB adapter */
  106. { USB_DEVICE(0x047d, 0x105d), .driver_info = BTUSB_RESET },
  107. { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_RESET | BTUSB_WRONG_SCO_MTU },
  108. /* ISSC Bluetooth Adapter v3.1 */
  109. { USB_DEVICE(0x1131, 0x1001), .driver_info = BTUSB_RESET },
  110. /* RTX Telecom based adapters with buggy SCO support */
  111. { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
  112. { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
  113. /* CONWISE Technology based adapters with buggy SCO support */
  114. { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
  115. /* Belkin F8T012 and F8T013 devices */
  116. { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_RESET | BTUSB_WRONG_SCO_MTU },
  117. { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_RESET | BTUSB_WRONG_SCO_MTU },
  118. /* Belkin F8T016 device */
  119. { USB_DEVICE(0x050d, 0x016a), .driver_info = BTUSB_RESET },
  120. /* Digianswer devices */
  121. { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
  122. { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
  123. /* CSR BlueCore Bluetooth Sniffer */
  124. { USB_DEVICE(0x0a12, 0x0002), .driver_info = BTUSB_SNIFFER },
  125. /* Frontline ComProbe Bluetooth Sniffer */
  126. { USB_DEVICE(0x16d3, 0x0002), .driver_info = BTUSB_SNIFFER },
  127. { } /* Terminating entry */
  128. };
  129. #define BTUSB_MAX_ISOC_FRAMES 10
  130. #define BTUSB_INTR_RUNNING 0
  131. #define BTUSB_BULK_RUNNING 1
  132. #define BTUSB_ISOC_RUNNING 2
  133. struct btusb_data {
  134. struct hci_dev *hdev;
  135. struct usb_device *udev;
  136. struct usb_interface *intf;
  137. struct usb_interface *isoc;
  138. spinlock_t lock;
  139. unsigned long flags;
  140. struct work_struct work;
  141. struct usb_anchor tx_anchor;
  142. struct usb_anchor intr_anchor;
  143. struct usb_anchor bulk_anchor;
  144. struct usb_anchor isoc_anchor;
  145. struct usb_endpoint_descriptor *intr_ep;
  146. struct usb_endpoint_descriptor *bulk_tx_ep;
  147. struct usb_endpoint_descriptor *bulk_rx_ep;
  148. struct usb_endpoint_descriptor *isoc_tx_ep;
  149. struct usb_endpoint_descriptor *isoc_rx_ep;
  150. int isoc_altsetting;
  151. };
  152. static void btusb_intr_complete(struct urb *urb)
  153. {
  154. struct hci_dev *hdev = urb->context;
  155. struct btusb_data *data = hdev->driver_data;
  156. int err;
  157. BT_DBG("%s urb %p status %d count %d", hdev->name,
  158. urb, urb->status, urb->actual_length);
  159. if (!test_bit(HCI_RUNNING, &hdev->flags))
  160. return;
  161. if (urb->status == 0) {
  162. hdev->stat.byte_rx += urb->actual_length;
  163. if (hci_recv_fragment(hdev, HCI_EVENT_PKT,
  164. urb->transfer_buffer,
  165. urb->actual_length) < 0) {
  166. BT_ERR("%s corrupted event packet", hdev->name);
  167. hdev->stat.err_rx++;
  168. }
  169. }
  170. if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
  171. return;
  172. usb_anchor_urb(urb, &data->intr_anchor);
  173. err = usb_submit_urb(urb, GFP_ATOMIC);
  174. if (err < 0) {
  175. BT_ERR("%s urb %p failed to resubmit (%d)",
  176. hdev->name, urb, -err);
  177. usb_unanchor_urb(urb);
  178. }
  179. }
  180. static int btusb_submit_intr_urb(struct hci_dev *hdev)
  181. {
  182. struct btusb_data *data = hdev->driver_data;
  183. struct urb *urb;
  184. unsigned char *buf;
  185. unsigned int pipe;
  186. int err, size;
  187. BT_DBG("%s", hdev->name);
  188. if (!data->intr_ep)
  189. return -ENODEV;
  190. urb = usb_alloc_urb(0, GFP_ATOMIC);
  191. if (!urb)
  192. return -ENOMEM;
  193. size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
  194. buf = kmalloc(size, GFP_ATOMIC);
  195. if (!buf) {
  196. usb_free_urb(urb);
  197. return -ENOMEM;
  198. }
  199. pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
  200. usb_fill_int_urb(urb, data->udev, pipe, buf, size,
  201. btusb_intr_complete, hdev,
  202. data->intr_ep->bInterval);
  203. urb->transfer_flags |= URB_FREE_BUFFER;
  204. usb_anchor_urb(urb, &data->intr_anchor);
  205. err = usb_submit_urb(urb, GFP_ATOMIC);
  206. if (err < 0) {
  207. BT_ERR("%s urb %p submission failed (%d)",
  208. hdev->name, urb, -err);
  209. usb_unanchor_urb(urb);
  210. }
  211. usb_free_urb(urb);
  212. return err;
  213. }
  214. static void btusb_bulk_complete(struct urb *urb)
  215. {
  216. struct hci_dev *hdev = urb->context;
  217. struct btusb_data *data = hdev->driver_data;
  218. int err;
  219. BT_DBG("%s urb %p status %d count %d", hdev->name,
  220. urb, urb->status, urb->actual_length);
  221. if (!test_bit(HCI_RUNNING, &hdev->flags))
  222. return;
  223. if (urb->status == 0) {
  224. hdev->stat.byte_rx += urb->actual_length;
  225. if (hci_recv_fragment(hdev, HCI_ACLDATA_PKT,
  226. urb->transfer_buffer,
  227. urb->actual_length) < 0) {
  228. BT_ERR("%s corrupted ACL packet", hdev->name);
  229. hdev->stat.err_rx++;
  230. }
  231. }
  232. if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
  233. return;
  234. usb_anchor_urb(urb, &data->bulk_anchor);
  235. err = usb_submit_urb(urb, GFP_ATOMIC);
  236. if (err < 0) {
  237. BT_ERR("%s urb %p failed to resubmit (%d)",
  238. hdev->name, urb, -err);
  239. usb_unanchor_urb(urb);
  240. }
  241. }
  242. static int btusb_submit_bulk_urb(struct hci_dev *hdev)
  243. {
  244. struct btusb_data *data = hdev->driver_data;
  245. struct urb *urb;
  246. unsigned char *buf;
  247. unsigned int pipe;
  248. int err, size;
  249. BT_DBG("%s", hdev->name);
  250. if (!data->bulk_rx_ep)
  251. return -ENODEV;
  252. urb = usb_alloc_urb(0, GFP_KERNEL);
  253. if (!urb)
  254. return -ENOMEM;
  255. size = le16_to_cpu(data->bulk_rx_ep->wMaxPacketSize);
  256. buf = kmalloc(size, GFP_KERNEL);
  257. if (!buf) {
  258. usb_free_urb(urb);
  259. return -ENOMEM;
  260. }
  261. pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
  262. usb_fill_bulk_urb(urb, data->udev, pipe,
  263. buf, size, btusb_bulk_complete, hdev);
  264. urb->transfer_flags |= URB_FREE_BUFFER;
  265. usb_anchor_urb(urb, &data->bulk_anchor);
  266. err = usb_submit_urb(urb, GFP_KERNEL);
  267. if (err < 0) {
  268. BT_ERR("%s urb %p submission failed (%d)",
  269. hdev->name, urb, -err);
  270. usb_unanchor_urb(urb);
  271. }
  272. usb_free_urb(urb);
  273. return err;
  274. }
  275. static void btusb_isoc_complete(struct urb *urb)
  276. {
  277. struct hci_dev *hdev = urb->context;
  278. struct btusb_data *data = hdev->driver_data;
  279. int i, err;
  280. BT_DBG("%s urb %p status %d count %d", hdev->name,
  281. urb, urb->status, urb->actual_length);
  282. if (!test_bit(HCI_RUNNING, &hdev->flags))
  283. return;
  284. if (urb->status == 0) {
  285. for (i = 0; i < urb->number_of_packets; i++) {
  286. unsigned int offset = urb->iso_frame_desc[i].offset;
  287. unsigned int length = urb->iso_frame_desc[i].actual_length;
  288. if (urb->iso_frame_desc[i].status)
  289. continue;
  290. hdev->stat.byte_rx += length;
  291. if (hci_recv_fragment(hdev, HCI_SCODATA_PKT,
  292. urb->transfer_buffer + offset,
  293. length) < 0) {
  294. BT_ERR("%s corrupted SCO packet", hdev->name);
  295. hdev->stat.err_rx++;
  296. }
  297. }
  298. }
  299. if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
  300. return;
  301. usb_anchor_urb(urb, &data->isoc_anchor);
  302. err = usb_submit_urb(urb, GFP_ATOMIC);
  303. if (err < 0) {
  304. BT_ERR("%s urb %p failed to resubmit (%d)",
  305. hdev->name, urb, -err);
  306. usb_unanchor_urb(urb);
  307. }
  308. }
  309. static void inline __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
  310. {
  311. int i, offset = 0;
  312. BT_DBG("len %d mtu %d", len, mtu);
  313. for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
  314. i++, offset += mtu, len -= mtu) {
  315. urb->iso_frame_desc[i].offset = offset;
  316. urb->iso_frame_desc[i].length = mtu;
  317. }
  318. if (len && i < BTUSB_MAX_ISOC_FRAMES) {
  319. urb->iso_frame_desc[i].offset = offset;
  320. urb->iso_frame_desc[i].length = len;
  321. i++;
  322. }
  323. urb->number_of_packets = i;
  324. }
  325. static int btusb_submit_isoc_urb(struct hci_dev *hdev)
  326. {
  327. struct btusb_data *data = hdev->driver_data;
  328. struct urb *urb;
  329. unsigned char *buf;
  330. unsigned int pipe;
  331. int err, size;
  332. BT_DBG("%s", hdev->name);
  333. if (!data->isoc_rx_ep)
  334. return -ENODEV;
  335. urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
  336. if (!urb)
  337. return -ENOMEM;
  338. size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
  339. BTUSB_MAX_ISOC_FRAMES;
  340. buf = kmalloc(size, GFP_KERNEL);
  341. if (!buf) {
  342. usb_free_urb(urb);
  343. return -ENOMEM;
  344. }
  345. pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
  346. urb->dev = data->udev;
  347. urb->pipe = pipe;
  348. urb->context = hdev;
  349. urb->complete = btusb_isoc_complete;
  350. urb->interval = data->isoc_rx_ep->bInterval;
  351. urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
  352. urb->transfer_buffer = buf;
  353. urb->transfer_buffer_length = size;
  354. __fill_isoc_descriptor(urb, size,
  355. le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
  356. usb_anchor_urb(urb, &data->isoc_anchor);
  357. err = usb_submit_urb(urb, GFP_KERNEL);
  358. if (err < 0) {
  359. BT_ERR("%s urb %p submission failed (%d)",
  360. hdev->name, urb, -err);
  361. usb_unanchor_urb(urb);
  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");