btusb.c 28 KB

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