btusb.c 23 KB

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