btusb.c 28 KB

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