btusb.c 28 KB

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