btusb.c 24 KB

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