btusb.c 32 KB

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