btusb.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648
  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/module.h>
  24. #include <linux/usb.h>
  25. #include <linux/firmware.h>
  26. #include <net/bluetooth/bluetooth.h>
  27. #include <net/bluetooth/hci_core.h>
  28. #define VERSION "0.6"
  29. static bool ignore_dga;
  30. static bool ignore_csr;
  31. static bool ignore_sniffer;
  32. static bool disable_scofix;
  33. static bool force_scofix;
  34. static bool reset = 1;
  35. static struct usb_driver btusb_driver;
  36. #define BTUSB_IGNORE 0x01
  37. #define BTUSB_DIGIANSWER 0x02
  38. #define BTUSB_CSR 0x04
  39. #define BTUSB_SNIFFER 0x08
  40. #define BTUSB_BCM92035 0x10
  41. #define BTUSB_BROKEN_ISOC 0x20
  42. #define BTUSB_WRONG_SCO_MTU 0x40
  43. #define BTUSB_ATH3012 0x80
  44. #define BTUSB_INTEL 0x100
  45. static struct usb_device_id btusb_table[] = {
  46. /* Generic Bluetooth USB device */
  47. { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
  48. /* Apple-specific (Broadcom) devices */
  49. { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01) },
  50. /* Broadcom SoftSailing reporting vendor specific */
  51. { USB_DEVICE(0x0a5c, 0x21e1) },
  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 MacBookAir4,1 */
  61. { USB_DEVICE(0x05ac, 0x821f) },
  62. /* Apple MacBookPro8,2 */
  63. { USB_DEVICE(0x05ac, 0x821a) },
  64. /* Apple MacMini5,1 */
  65. { USB_DEVICE(0x05ac, 0x8281) },
  66. /* AVM BlueFRITZ! USB v2.0 */
  67. { USB_DEVICE(0x057c, 0x3800) },
  68. /* Bluetooth Ultraport Module from IBM */
  69. { USB_DEVICE(0x04bf, 0x030a) },
  70. /* ALPS Modules with non-standard id */
  71. { USB_DEVICE(0x044e, 0x3001) },
  72. { USB_DEVICE(0x044e, 0x3002) },
  73. /* Ericsson with non-standard id */
  74. { USB_DEVICE(0x0bdb, 0x1002) },
  75. /* Canyon CN-BTU1 with HID interfaces */
  76. { USB_DEVICE(0x0c10, 0x0000) },
  77. /* Broadcom BCM20702A0 */
  78. { USB_DEVICE(0x0b05, 0x17b5) },
  79. { USB_DEVICE(0x04ca, 0x2003) },
  80. { USB_DEVICE(0x0489, 0xe042) },
  81. { USB_DEVICE(0x413c, 0x8197) },
  82. /* Foxconn - Hon Hai */
  83. { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01) },
  84. /*Broadcom devices with vendor specific id */
  85. { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01) },
  86. { } /* Terminating entry */
  87. };
  88. MODULE_DEVICE_TABLE(usb, btusb_table);
  89. static struct usb_device_id blacklist_table[] = {
  90. /* CSR BlueCore devices */
  91. { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
  92. /* Broadcom BCM2033 without firmware */
  93. { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
  94. /* Atheros 3011 with sflash firmware */
  95. { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
  96. { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
  97. { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
  98. { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
  99. { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
  100. { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
  101. /* Atheros AR9285 Malbec with sflash firmware */
  102. { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
  103. /* Atheros 3012 with sflash firmware */
  104. { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
  105. { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
  106. { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
  107. { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
  108. { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
  109. { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
  110. { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
  111. { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
  112. { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
  113. { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
  114. { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
  115. { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
  116. { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
  117. { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
  118. { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
  119. { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
  120. { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
  121. { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
  122. /* Atheros AR5BBU12 with sflash firmware */
  123. { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
  124. /* Atheros AR5BBU12 with sflash firmware */
  125. { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
  126. { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
  127. /* Broadcom BCM2035 */
  128. { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
  129. { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
  130. { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
  131. /* Broadcom BCM2045 */
  132. { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
  133. { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
  134. /* IBM/Lenovo ThinkPad with Broadcom chip */
  135. { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
  136. { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
  137. /* HP laptop with Broadcom chip */
  138. { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
  139. /* Dell laptop with Broadcom chip */
  140. { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
  141. /* Dell Wireless 370 and 410 devices */
  142. { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
  143. { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
  144. /* Belkin F8T012 and F8T013 devices */
  145. { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
  146. { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
  147. /* Asus WL-BTD202 device */
  148. { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
  149. /* Kensington Bluetooth USB adapter */
  150. { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
  151. /* RTX Telecom based adapters with buggy SCO support */
  152. { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
  153. { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
  154. /* CONWISE Technology based adapters with buggy SCO support */
  155. { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
  156. /* Digianswer devices */
  157. { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
  158. { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
  159. /* CSR BlueCore Bluetooth Sniffer */
  160. { USB_DEVICE(0x0a12, 0x0002), .driver_info = BTUSB_SNIFFER },
  161. /* Frontline ComProbe Bluetooth Sniffer */
  162. { USB_DEVICE(0x16d3, 0x0002), .driver_info = BTUSB_SNIFFER },
  163. /* Intel Bluetooth device */
  164. { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL },
  165. { } /* Terminating entry */
  166. };
  167. #define BTUSB_MAX_ISOC_FRAMES 10
  168. #define BTUSB_INTR_RUNNING 0
  169. #define BTUSB_BULK_RUNNING 1
  170. #define BTUSB_ISOC_RUNNING 2
  171. #define BTUSB_SUSPENDING 3
  172. #define BTUSB_DID_ISO_RESUME 4
  173. struct btusb_data {
  174. struct hci_dev *hdev;
  175. struct usb_device *udev;
  176. struct usb_interface *intf;
  177. struct usb_interface *isoc;
  178. spinlock_t lock;
  179. unsigned long flags;
  180. struct work_struct work;
  181. struct work_struct waker;
  182. struct usb_anchor tx_anchor;
  183. struct usb_anchor intr_anchor;
  184. struct usb_anchor bulk_anchor;
  185. struct usb_anchor isoc_anchor;
  186. struct usb_anchor deferred;
  187. int tx_in_flight;
  188. spinlock_t txlock;
  189. struct usb_endpoint_descriptor *intr_ep;
  190. struct usb_endpoint_descriptor *bulk_tx_ep;
  191. struct usb_endpoint_descriptor *bulk_rx_ep;
  192. struct usb_endpoint_descriptor *isoc_tx_ep;
  193. struct usb_endpoint_descriptor *isoc_rx_ep;
  194. __u8 cmdreq_type;
  195. unsigned int sco_num;
  196. int isoc_altsetting;
  197. int suspend_count;
  198. };
  199. static int inc_tx(struct btusb_data *data)
  200. {
  201. unsigned long flags;
  202. int rv;
  203. spin_lock_irqsave(&data->txlock, flags);
  204. rv = test_bit(BTUSB_SUSPENDING, &data->flags);
  205. if (!rv)
  206. data->tx_in_flight++;
  207. spin_unlock_irqrestore(&data->txlock, flags);
  208. return rv;
  209. }
  210. static void btusb_intr_complete(struct urb *urb)
  211. {
  212. struct hci_dev *hdev = urb->context;
  213. struct btusb_data *data = hci_get_drvdata(hdev);
  214. int err;
  215. BT_DBG("%s urb %p status %d count %d", hdev->name,
  216. urb, urb->status, urb->actual_length);
  217. if (!test_bit(HCI_RUNNING, &hdev->flags))
  218. return;
  219. if (urb->status == 0) {
  220. hdev->stat.byte_rx += urb->actual_length;
  221. if (hci_recv_fragment(hdev, HCI_EVENT_PKT,
  222. urb->transfer_buffer,
  223. urb->actual_length) < 0) {
  224. BT_ERR("%s corrupted event packet", hdev->name);
  225. hdev->stat.err_rx++;
  226. }
  227. }
  228. if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
  229. return;
  230. usb_mark_last_busy(data->udev);
  231. usb_anchor_urb(urb, &data->intr_anchor);
  232. err = usb_submit_urb(urb, GFP_ATOMIC);
  233. if (err < 0) {
  234. /* -EPERM: urb is being killed;
  235. * -ENODEV: device got disconnected */
  236. if (err != -EPERM && err != -ENODEV)
  237. BT_ERR("%s urb %p failed to resubmit (%d)",
  238. hdev->name, urb, -err);
  239. usb_unanchor_urb(urb);
  240. }
  241. }
  242. static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
  243. {
  244. struct btusb_data *data = hci_get_drvdata(hdev);
  245. struct urb *urb;
  246. unsigned char *buf;
  247. unsigned int pipe;
  248. int err, size;
  249. BT_DBG("%s", hdev->name);
  250. if (!data->intr_ep)
  251. return -ENODEV;
  252. urb = usb_alloc_urb(0, mem_flags);
  253. if (!urb)
  254. return -ENOMEM;
  255. size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
  256. buf = kmalloc(size, mem_flags);
  257. if (!buf) {
  258. usb_free_urb(urb);
  259. return -ENOMEM;
  260. }
  261. pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
  262. usb_fill_int_urb(urb, data->udev, pipe, buf, size,
  263. btusb_intr_complete, hdev,
  264. data->intr_ep->bInterval);
  265. urb->transfer_flags |= URB_FREE_BUFFER;
  266. usb_anchor_urb(urb, &data->intr_anchor);
  267. err = usb_submit_urb(urb, mem_flags);
  268. if (err < 0) {
  269. if (err != -EPERM && err != -ENODEV)
  270. BT_ERR("%s urb %p submission failed (%d)",
  271. hdev->name, urb, -err);
  272. usb_unanchor_urb(urb);
  273. }
  274. usb_free_urb(urb);
  275. return err;
  276. }
  277. static void btusb_bulk_complete(struct urb *urb)
  278. {
  279. struct hci_dev *hdev = urb->context;
  280. struct btusb_data *data = hci_get_drvdata(hdev);
  281. int err;
  282. BT_DBG("%s urb %p status %d count %d", hdev->name,
  283. urb, urb->status, urb->actual_length);
  284. if (!test_bit(HCI_RUNNING, &hdev->flags))
  285. return;
  286. if (urb->status == 0) {
  287. hdev->stat.byte_rx += urb->actual_length;
  288. if (hci_recv_fragment(hdev, HCI_ACLDATA_PKT,
  289. urb->transfer_buffer,
  290. urb->actual_length) < 0) {
  291. BT_ERR("%s corrupted ACL packet", hdev->name);
  292. hdev->stat.err_rx++;
  293. }
  294. }
  295. if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
  296. return;
  297. usb_anchor_urb(urb, &data->bulk_anchor);
  298. usb_mark_last_busy(data->udev);
  299. err = usb_submit_urb(urb, GFP_ATOMIC);
  300. if (err < 0) {
  301. /* -EPERM: urb is being killed;
  302. * -ENODEV: device got disconnected */
  303. if (err != -EPERM && err != -ENODEV)
  304. BT_ERR("%s urb %p failed to resubmit (%d)",
  305. hdev->name, urb, -err);
  306. usb_unanchor_urb(urb);
  307. }
  308. }
  309. static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
  310. {
  311. struct btusb_data *data = hci_get_drvdata(hdev);
  312. struct urb *urb;
  313. unsigned char *buf;
  314. unsigned int pipe;
  315. int err, size = HCI_MAX_FRAME_SIZE;
  316. BT_DBG("%s", hdev->name);
  317. if (!data->bulk_rx_ep)
  318. return -ENODEV;
  319. urb = usb_alloc_urb(0, mem_flags);
  320. if (!urb)
  321. return -ENOMEM;
  322. buf = kmalloc(size, mem_flags);
  323. if (!buf) {
  324. usb_free_urb(urb);
  325. return -ENOMEM;
  326. }
  327. pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
  328. usb_fill_bulk_urb(urb, data->udev, pipe,
  329. buf, size, btusb_bulk_complete, hdev);
  330. urb->transfer_flags |= URB_FREE_BUFFER;
  331. usb_mark_last_busy(data->udev);
  332. usb_anchor_urb(urb, &data->bulk_anchor);
  333. err = usb_submit_urb(urb, mem_flags);
  334. if (err < 0) {
  335. if (err != -EPERM && err != -ENODEV)
  336. BT_ERR("%s urb %p submission failed (%d)",
  337. hdev->name, urb, -err);
  338. usb_unanchor_urb(urb);
  339. }
  340. usb_free_urb(urb);
  341. return err;
  342. }
  343. static void btusb_isoc_complete(struct urb *urb)
  344. {
  345. struct hci_dev *hdev = urb->context;
  346. struct btusb_data *data = hci_get_drvdata(hdev);
  347. int i, err;
  348. BT_DBG("%s urb %p status %d count %d", hdev->name,
  349. urb, urb->status, urb->actual_length);
  350. if (!test_bit(HCI_RUNNING, &hdev->flags))
  351. return;
  352. if (urb->status == 0) {
  353. for (i = 0; i < urb->number_of_packets; i++) {
  354. unsigned int offset = urb->iso_frame_desc[i].offset;
  355. unsigned int length = urb->iso_frame_desc[i].actual_length;
  356. if (urb->iso_frame_desc[i].status)
  357. continue;
  358. hdev->stat.byte_rx += length;
  359. if (hci_recv_fragment(hdev, HCI_SCODATA_PKT,
  360. urb->transfer_buffer + offset,
  361. length) < 0) {
  362. BT_ERR("%s corrupted SCO packet", hdev->name);
  363. hdev->stat.err_rx++;
  364. }
  365. }
  366. }
  367. if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
  368. return;
  369. usb_anchor_urb(urb, &data->isoc_anchor);
  370. err = usb_submit_urb(urb, GFP_ATOMIC);
  371. if (err < 0) {
  372. /* -EPERM: urb is being killed;
  373. * -ENODEV: device got disconnected */
  374. if (err != -EPERM && err != -ENODEV)
  375. BT_ERR("%s urb %p failed to resubmit (%d)",
  376. hdev->name, urb, -err);
  377. usb_unanchor_urb(urb);
  378. }
  379. }
  380. static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
  381. {
  382. int i, offset = 0;
  383. BT_DBG("len %d mtu %d", len, mtu);
  384. for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
  385. i++, offset += mtu, len -= mtu) {
  386. urb->iso_frame_desc[i].offset = offset;
  387. urb->iso_frame_desc[i].length = mtu;
  388. }
  389. if (len && i < BTUSB_MAX_ISOC_FRAMES) {
  390. urb->iso_frame_desc[i].offset = offset;
  391. urb->iso_frame_desc[i].length = len;
  392. i++;
  393. }
  394. urb->number_of_packets = i;
  395. }
  396. static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
  397. {
  398. struct btusb_data *data = hci_get_drvdata(hdev);
  399. struct urb *urb;
  400. unsigned char *buf;
  401. unsigned int pipe;
  402. int err, size;
  403. BT_DBG("%s", hdev->name);
  404. if (!data->isoc_rx_ep)
  405. return -ENODEV;
  406. urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
  407. if (!urb)
  408. return -ENOMEM;
  409. size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
  410. BTUSB_MAX_ISOC_FRAMES;
  411. buf = kmalloc(size, mem_flags);
  412. if (!buf) {
  413. usb_free_urb(urb);
  414. return -ENOMEM;
  415. }
  416. pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
  417. usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
  418. hdev, data->isoc_rx_ep->bInterval);
  419. urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
  420. __fill_isoc_descriptor(urb, size,
  421. le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
  422. usb_anchor_urb(urb, &data->isoc_anchor);
  423. err = usb_submit_urb(urb, mem_flags);
  424. if (err < 0) {
  425. if (err != -EPERM && err != -ENODEV)
  426. BT_ERR("%s urb %p submission failed (%d)",
  427. hdev->name, urb, -err);
  428. usb_unanchor_urb(urb);
  429. }
  430. usb_free_urb(urb);
  431. return err;
  432. }
  433. static void btusb_tx_complete(struct urb *urb)
  434. {
  435. struct sk_buff *skb = urb->context;
  436. struct hci_dev *hdev = (struct hci_dev *) skb->dev;
  437. struct btusb_data *data = hci_get_drvdata(hdev);
  438. BT_DBG("%s urb %p status %d count %d", hdev->name,
  439. urb, urb->status, urb->actual_length);
  440. if (!test_bit(HCI_RUNNING, &hdev->flags))
  441. goto done;
  442. if (!urb->status)
  443. hdev->stat.byte_tx += urb->transfer_buffer_length;
  444. else
  445. hdev->stat.err_tx++;
  446. done:
  447. spin_lock(&data->txlock);
  448. data->tx_in_flight--;
  449. spin_unlock(&data->txlock);
  450. kfree(urb->setup_packet);
  451. kfree_skb(skb);
  452. }
  453. static void btusb_isoc_tx_complete(struct urb *urb)
  454. {
  455. struct sk_buff *skb = urb->context;
  456. struct hci_dev *hdev = (struct hci_dev *) skb->dev;
  457. BT_DBG("%s urb %p status %d count %d", hdev->name,
  458. urb, urb->status, urb->actual_length);
  459. if (!test_bit(HCI_RUNNING, &hdev->flags))
  460. goto done;
  461. if (!urb->status)
  462. hdev->stat.byte_tx += urb->transfer_buffer_length;
  463. else
  464. hdev->stat.err_tx++;
  465. done:
  466. kfree(urb->setup_packet);
  467. kfree_skb(skb);
  468. }
  469. static int btusb_open(struct hci_dev *hdev)
  470. {
  471. struct btusb_data *data = hci_get_drvdata(hdev);
  472. int err;
  473. BT_DBG("%s", hdev->name);
  474. err = usb_autopm_get_interface(data->intf);
  475. if (err < 0)
  476. return err;
  477. data->intf->needs_remote_wakeup = 1;
  478. if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
  479. goto done;
  480. if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
  481. goto done;
  482. err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
  483. if (err < 0)
  484. goto failed;
  485. err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
  486. if (err < 0) {
  487. usb_kill_anchored_urbs(&data->intr_anchor);
  488. goto failed;
  489. }
  490. set_bit(BTUSB_BULK_RUNNING, &data->flags);
  491. btusb_submit_bulk_urb(hdev, GFP_KERNEL);
  492. done:
  493. usb_autopm_put_interface(data->intf);
  494. return 0;
  495. failed:
  496. clear_bit(BTUSB_INTR_RUNNING, &data->flags);
  497. clear_bit(HCI_RUNNING, &hdev->flags);
  498. usb_autopm_put_interface(data->intf);
  499. return err;
  500. }
  501. static void btusb_stop_traffic(struct btusb_data *data)
  502. {
  503. usb_kill_anchored_urbs(&data->intr_anchor);
  504. usb_kill_anchored_urbs(&data->bulk_anchor);
  505. usb_kill_anchored_urbs(&data->isoc_anchor);
  506. }
  507. static int btusb_close(struct hci_dev *hdev)
  508. {
  509. struct btusb_data *data = hci_get_drvdata(hdev);
  510. int err;
  511. BT_DBG("%s", hdev->name);
  512. if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
  513. return 0;
  514. cancel_work_sync(&data->work);
  515. cancel_work_sync(&data->waker);
  516. clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
  517. clear_bit(BTUSB_BULK_RUNNING, &data->flags);
  518. clear_bit(BTUSB_INTR_RUNNING, &data->flags);
  519. btusb_stop_traffic(data);
  520. err = usb_autopm_get_interface(data->intf);
  521. if (err < 0)
  522. goto failed;
  523. data->intf->needs_remote_wakeup = 0;
  524. usb_autopm_put_interface(data->intf);
  525. failed:
  526. usb_scuttle_anchored_urbs(&data->deferred);
  527. return 0;
  528. }
  529. static int btusb_flush(struct hci_dev *hdev)
  530. {
  531. struct btusb_data *data = hci_get_drvdata(hdev);
  532. BT_DBG("%s", hdev->name);
  533. usb_kill_anchored_urbs(&data->tx_anchor);
  534. return 0;
  535. }
  536. static int btusb_send_frame(struct sk_buff *skb)
  537. {
  538. struct hci_dev *hdev = (struct hci_dev *) skb->dev;
  539. struct btusb_data *data = hci_get_drvdata(hdev);
  540. struct usb_ctrlrequest *dr;
  541. struct urb *urb;
  542. unsigned int pipe;
  543. int err;
  544. BT_DBG("%s", hdev->name);
  545. if (!test_bit(HCI_RUNNING, &hdev->flags))
  546. return -EBUSY;
  547. switch (bt_cb(skb)->pkt_type) {
  548. case HCI_COMMAND_PKT:
  549. urb = usb_alloc_urb(0, GFP_ATOMIC);
  550. if (!urb)
  551. return -ENOMEM;
  552. dr = kmalloc(sizeof(*dr), GFP_ATOMIC);
  553. if (!dr) {
  554. usb_free_urb(urb);
  555. return -ENOMEM;
  556. }
  557. dr->bRequestType = data->cmdreq_type;
  558. dr->bRequest = 0;
  559. dr->wIndex = 0;
  560. dr->wValue = 0;
  561. dr->wLength = __cpu_to_le16(skb->len);
  562. pipe = usb_sndctrlpipe(data->udev, 0x00);
  563. usb_fill_control_urb(urb, data->udev, pipe, (void *) dr,
  564. skb->data, skb->len, btusb_tx_complete, skb);
  565. hdev->stat.cmd_tx++;
  566. break;
  567. case HCI_ACLDATA_PKT:
  568. if (!data->bulk_tx_ep)
  569. return -ENODEV;
  570. urb = usb_alloc_urb(0, GFP_ATOMIC);
  571. if (!urb)
  572. return -ENOMEM;
  573. pipe = usb_sndbulkpipe(data->udev,
  574. data->bulk_tx_ep->bEndpointAddress);
  575. usb_fill_bulk_urb(urb, data->udev, pipe,
  576. skb->data, skb->len, btusb_tx_complete, skb);
  577. hdev->stat.acl_tx++;
  578. break;
  579. case HCI_SCODATA_PKT:
  580. if (!data->isoc_tx_ep || hdev->conn_hash.sco_num < 1)
  581. return -ENODEV;
  582. urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_ATOMIC);
  583. if (!urb)
  584. return -ENOMEM;
  585. pipe = usb_sndisocpipe(data->udev,
  586. data->isoc_tx_ep->bEndpointAddress);
  587. usb_fill_int_urb(urb, data->udev, pipe,
  588. skb->data, skb->len, btusb_isoc_tx_complete,
  589. skb, data->isoc_tx_ep->bInterval);
  590. urb->transfer_flags = URB_ISO_ASAP;
  591. __fill_isoc_descriptor(urb, skb->len,
  592. le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
  593. hdev->stat.sco_tx++;
  594. goto skip_waking;
  595. default:
  596. return -EILSEQ;
  597. }
  598. err = inc_tx(data);
  599. if (err) {
  600. usb_anchor_urb(urb, &data->deferred);
  601. schedule_work(&data->waker);
  602. err = 0;
  603. goto done;
  604. }
  605. skip_waking:
  606. usb_anchor_urb(urb, &data->tx_anchor);
  607. err = usb_submit_urb(urb, GFP_ATOMIC);
  608. if (err < 0) {
  609. if (err != -EPERM && err != -ENODEV)
  610. BT_ERR("%s urb %p submission failed (%d)",
  611. hdev->name, urb, -err);
  612. kfree(urb->setup_packet);
  613. usb_unanchor_urb(urb);
  614. } else {
  615. usb_mark_last_busy(data->udev);
  616. }
  617. done:
  618. usb_free_urb(urb);
  619. return err;
  620. }
  621. static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
  622. {
  623. struct btusb_data *data = hci_get_drvdata(hdev);
  624. BT_DBG("%s evt %d", hdev->name, evt);
  625. if (hdev->conn_hash.sco_num != data->sco_num) {
  626. data->sco_num = hdev->conn_hash.sco_num;
  627. schedule_work(&data->work);
  628. }
  629. }
  630. static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
  631. {
  632. struct btusb_data *data = hci_get_drvdata(hdev);
  633. struct usb_interface *intf = data->isoc;
  634. struct usb_endpoint_descriptor *ep_desc;
  635. int i, err;
  636. if (!data->isoc)
  637. return -ENODEV;
  638. err = usb_set_interface(data->udev, 1, altsetting);
  639. if (err < 0) {
  640. BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
  641. return err;
  642. }
  643. data->isoc_altsetting = altsetting;
  644. data->isoc_tx_ep = NULL;
  645. data->isoc_rx_ep = NULL;
  646. for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
  647. ep_desc = &intf->cur_altsetting->endpoint[i].desc;
  648. if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
  649. data->isoc_tx_ep = ep_desc;
  650. continue;
  651. }
  652. if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
  653. data->isoc_rx_ep = ep_desc;
  654. continue;
  655. }
  656. }
  657. if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
  658. BT_ERR("%s invalid SCO descriptors", hdev->name);
  659. return -ENODEV;
  660. }
  661. return 0;
  662. }
  663. static void btusb_work(struct work_struct *work)
  664. {
  665. struct btusb_data *data = container_of(work, struct btusb_data, work);
  666. struct hci_dev *hdev = data->hdev;
  667. int new_alts;
  668. int err;
  669. if (hdev->conn_hash.sco_num > 0) {
  670. if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
  671. err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
  672. if (err < 0) {
  673. clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
  674. usb_kill_anchored_urbs(&data->isoc_anchor);
  675. return;
  676. }
  677. set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
  678. }
  679. if (hdev->voice_setting & 0x0020) {
  680. static const int alts[3] = { 2, 4, 5 };
  681. new_alts = alts[hdev->conn_hash.sco_num - 1];
  682. } else {
  683. new_alts = hdev->conn_hash.sco_num;
  684. }
  685. if (data->isoc_altsetting != new_alts) {
  686. clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
  687. usb_kill_anchored_urbs(&data->isoc_anchor);
  688. if (__set_isoc_interface(hdev, new_alts) < 0)
  689. return;
  690. }
  691. if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
  692. if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
  693. clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
  694. else
  695. btusb_submit_isoc_urb(hdev, GFP_KERNEL);
  696. }
  697. } else {
  698. clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
  699. usb_kill_anchored_urbs(&data->isoc_anchor);
  700. __set_isoc_interface(hdev, 0);
  701. if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
  702. usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
  703. }
  704. }
  705. static void btusb_waker(struct work_struct *work)
  706. {
  707. struct btusb_data *data = container_of(work, struct btusb_data, waker);
  708. int err;
  709. err = usb_autopm_get_interface(data->intf);
  710. if (err < 0)
  711. return;
  712. usb_autopm_put_interface(data->intf);
  713. }
  714. static int btusb_setup_bcm92035(struct hci_dev *hdev)
  715. {
  716. struct sk_buff *skb;
  717. u8 val = 0x00;
  718. BT_DBG("%s", hdev->name);
  719. skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
  720. if (IS_ERR(skb))
  721. BT_ERR("BCM92035 command failed (%ld)", -PTR_ERR(skb));
  722. else
  723. kfree_skb(skb);
  724. return 0;
  725. }
  726. struct intel_version {
  727. u8 status;
  728. u8 hw_platform;
  729. u8 hw_variant;
  730. u8 hw_revision;
  731. u8 fw_variant;
  732. u8 fw_revision;
  733. u8 fw_build_num;
  734. u8 fw_build_ww;
  735. u8 fw_build_yy;
  736. u8 fw_patch_num;
  737. } __packed;
  738. static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
  739. struct intel_version *ver)
  740. {
  741. const struct firmware *fw;
  742. char fwname[64];
  743. int ret;
  744. snprintf(fwname, sizeof(fwname),
  745. "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
  746. ver->hw_platform, ver->hw_variant, ver->hw_revision,
  747. ver->fw_variant, ver->fw_revision, ver->fw_build_num,
  748. ver->fw_build_ww, ver->fw_build_yy);
  749. ret = request_firmware(&fw, fwname, &hdev->dev);
  750. if (ret < 0) {
  751. if (ret == -EINVAL) {
  752. BT_ERR("%s Intel firmware file request failed (%d)",
  753. hdev->name, ret);
  754. return NULL;
  755. }
  756. BT_ERR("%s failed to open Intel firmware file: %s(%d)",
  757. hdev->name, fwname, ret);
  758. /* If the correct firmware patch file is not found, use the
  759. * default firmware patch file instead
  760. */
  761. snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
  762. ver->hw_platform, ver->hw_variant);
  763. if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
  764. BT_ERR("%s failed to open default Intel fw file: %s",
  765. hdev->name, fwname);
  766. return NULL;
  767. }
  768. }
  769. BT_INFO("%s: Intel Bluetooth firmware file: %s", hdev->name, fwname);
  770. return fw;
  771. }
  772. static int btusb_setup_intel_patching(struct hci_dev *hdev,
  773. const struct firmware *fw,
  774. const u8 **fw_ptr, int *disable_patch)
  775. {
  776. struct sk_buff *skb;
  777. struct hci_command_hdr *cmd;
  778. const u8 *cmd_param;
  779. struct hci_event_hdr *evt = NULL;
  780. const u8 *evt_param = NULL;
  781. int remain = fw->size - (*fw_ptr - fw->data);
  782. /* The first byte indicates the types of the patch command or event.
  783. * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
  784. * in the current firmware buffer doesn't start with 0x01 or
  785. * the size of remain buffer is smaller than HCI command header,
  786. * the firmware file is corrupted and it should stop the patching
  787. * process.
  788. */
  789. if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
  790. BT_ERR("%s Intel fw corrupted: invalid cmd read", hdev->name);
  791. return -EINVAL;
  792. }
  793. (*fw_ptr)++;
  794. remain--;
  795. cmd = (struct hci_command_hdr *)(*fw_ptr);
  796. *fw_ptr += sizeof(*cmd);
  797. remain -= sizeof(*cmd);
  798. /* Ensure that the remain firmware data is long enough than the length
  799. * of command parameter. If not, the firmware file is corrupted.
  800. */
  801. if (remain < cmd->plen) {
  802. BT_ERR("%s Intel fw corrupted: invalid cmd len", hdev->name);
  803. return -EFAULT;
  804. }
  805. /* If there is a command that loads a patch in the firmware
  806. * file, then enable the patch upon success, otherwise just
  807. * disable the manufacturer mode, for example patch activation
  808. * is not required when the default firmware patch file is used
  809. * because there are no patch data to load.
  810. */
  811. if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
  812. *disable_patch = 0;
  813. cmd_param = *fw_ptr;
  814. *fw_ptr += cmd->plen;
  815. remain -= cmd->plen;
  816. /* This reads the expected events when the above command is sent to the
  817. * device. Some vendor commands expects more than one events, for
  818. * example command status event followed by vendor specific event.
  819. * For this case, it only keeps the last expected event. so the command
  820. * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
  821. * last expected event.
  822. */
  823. while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
  824. (*fw_ptr)++;
  825. remain--;
  826. evt = (struct hci_event_hdr *)(*fw_ptr);
  827. *fw_ptr += sizeof(*evt);
  828. remain -= sizeof(*evt);
  829. if (remain < evt->plen) {
  830. BT_ERR("%s Intel fw corrupted: invalid evt len",
  831. hdev->name);
  832. return -EFAULT;
  833. }
  834. evt_param = *fw_ptr;
  835. *fw_ptr += evt->plen;
  836. remain -= evt->plen;
  837. }
  838. /* Every HCI commands in the firmware file has its correspond event.
  839. * If event is not found or remain is smaller than zero, the firmware
  840. * file is corrupted.
  841. */
  842. if (!evt || !evt_param || remain < 0) {
  843. BT_ERR("%s Intel fw corrupted: invalid evt read", hdev->name);
  844. return -EFAULT;
  845. }
  846. skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
  847. cmd_param, evt->evt, HCI_INIT_TIMEOUT);
  848. if (IS_ERR(skb)) {
  849. BT_ERR("%s sending Intel patch command (0x%4.4x) failed (%ld)",
  850. hdev->name, cmd->opcode, PTR_ERR(skb));
  851. return -PTR_ERR(skb);
  852. }
  853. /* It ensures that the returned event matches the event data read from
  854. * the firmware file. At fist, it checks the length and then
  855. * the contents of the event.
  856. */
  857. if (skb->len != evt->plen) {
  858. BT_ERR("%s mismatch event length (opcode 0x%4.4x)", hdev->name,
  859. le16_to_cpu(cmd->opcode));
  860. kfree_skb(skb);
  861. return -EFAULT;
  862. }
  863. if (memcmp(skb->data, evt_param, evt->plen)) {
  864. BT_ERR("%s mismatch event parameter (opcode 0x%4.4x)",
  865. hdev->name, le16_to_cpu(cmd->opcode));
  866. kfree_skb(skb);
  867. return -EFAULT;
  868. }
  869. kfree_skb(skb);
  870. return 0;
  871. }
  872. static int btusb_setup_intel(struct hci_dev *hdev)
  873. {
  874. struct sk_buff *skb;
  875. const struct firmware *fw;
  876. const u8 *fw_ptr;
  877. int disable_patch;
  878. struct intel_version *ver;
  879. const u8 mfg_enable[] = { 0x01, 0x00 };
  880. const u8 mfg_disable[] = { 0x00, 0x00 };
  881. const u8 mfg_reset_deactivate[] = { 0x00, 0x01 };
  882. const u8 mfg_reset_activate[] = { 0x00, 0x02 };
  883. BT_DBG("%s", hdev->name);
  884. /* The controller has a bug with the first HCI command sent to it
  885. * returning number of completed commands as zero. This would stall the
  886. * command processing in the Bluetooth core.
  887. *
  888. * As a workaround, send HCI Reset command first which will reset the
  889. * number of completed commands and allow normal command processing
  890. * from now on.
  891. */
  892. skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
  893. if (IS_ERR(skb)) {
  894. BT_ERR("%s sending initial HCI reset command failed (%ld)",
  895. hdev->name, PTR_ERR(skb));
  896. return -PTR_ERR(skb);
  897. }
  898. kfree_skb(skb);
  899. /* Read Intel specific controller version first to allow selection of
  900. * which firmware file to load.
  901. *
  902. * The returned information are hardware variant and revision plus
  903. * firmware variant, revision and build number.
  904. */
  905. skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
  906. if (IS_ERR(skb)) {
  907. BT_ERR("%s reading Intel fw version command failed (%ld)",
  908. hdev->name, PTR_ERR(skb));
  909. return -PTR_ERR(skb);
  910. }
  911. if (skb->len != sizeof(*ver)) {
  912. BT_ERR("%s Intel version event length mismatch", hdev->name);
  913. kfree_skb(skb);
  914. return -EIO;
  915. }
  916. ver = (struct intel_version *)skb->data;
  917. if (ver->status) {
  918. BT_ERR("%s Intel fw version event failed (%02x)", hdev->name,
  919. ver->status);
  920. kfree_skb(skb);
  921. return -bt_to_errno(ver->status);
  922. }
  923. BT_INFO("%s: read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
  924. hdev->name, ver->hw_platform, ver->hw_variant,
  925. ver->hw_revision, ver->fw_variant, ver->fw_revision,
  926. ver->fw_build_num, ver->fw_build_ww, ver->fw_build_yy,
  927. ver->fw_patch_num);
  928. /* fw_patch_num indicates the version of patch the device currently
  929. * have. If there is no patch data in the device, it is always 0x00.
  930. * So, if it is other than 0x00, no need to patch the deivce again.
  931. */
  932. if (ver->fw_patch_num) {
  933. BT_INFO("%s: Intel device is already patched. patch num: %02x",
  934. hdev->name, ver->fw_patch_num);
  935. kfree_skb(skb);
  936. return 0;
  937. }
  938. /* Opens the firmware patch file based on the firmware version read
  939. * from the controller. If it fails to open the matching firmware
  940. * patch file, it tries to open the default firmware patch file.
  941. * If no patch file is found, allow the device to operate without
  942. * a patch.
  943. */
  944. fw = btusb_setup_intel_get_fw(hdev, ver);
  945. if (!fw) {
  946. kfree_skb(skb);
  947. return 0;
  948. }
  949. fw_ptr = fw->data;
  950. /* This Intel specific command enables the manufacturer mode of the
  951. * controller.
  952. *
  953. * Only while this mode is enabled, the driver can download the
  954. * firmware patch data and configuration parameters.
  955. */
  956. skb = __hci_cmd_sync(hdev, 0xfc11, 2, mfg_enable, HCI_INIT_TIMEOUT);
  957. if (IS_ERR(skb)) {
  958. BT_ERR("%s entering Intel manufacturer mode failed (%ld)",
  959. hdev->name, PTR_ERR(skb));
  960. release_firmware(fw);
  961. return -PTR_ERR(skb);
  962. }
  963. if (skb->data[0]) {
  964. u8 evt_status = skb->data[0];
  965. BT_ERR("%s enable Intel manufacturer mode event failed (%02x)",
  966. hdev->name, evt_status);
  967. kfree_skb(skb);
  968. release_firmware(fw);
  969. return -bt_to_errno(evt_status);
  970. }
  971. kfree_skb(skb);
  972. disable_patch = 1;
  973. /* The firmware data file consists of list of Intel specific HCI
  974. * commands and its expected events. The first byte indicates the
  975. * type of the message, either HCI command or HCI event.
  976. *
  977. * It reads the command and its expected event from the firmware file,
  978. * and send to the controller. Once __hci_cmd_sync_ev() returns,
  979. * the returned event is compared with the event read from the firmware
  980. * file and it will continue until all the messages are downloaded to
  981. * the controller.
  982. *
  983. * Once the firmware patching is completed successfully,
  984. * the manufacturer mode is disabled with reset and activating the
  985. * downloaded patch.
  986. *
  987. * If the firmware patching fails, the manufacturer mode is
  988. * disabled with reset and deactivating the patch.
  989. *
  990. * If the default patch file is used, no reset is done when disabling
  991. * the manufacturer.
  992. */
  993. while (fw->size > fw_ptr - fw->data) {
  994. int ret;
  995. ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
  996. &disable_patch);
  997. if (ret < 0)
  998. goto exit_mfg_deactivate;
  999. }
  1000. release_firmware(fw);
  1001. if (disable_patch)
  1002. goto exit_mfg_disable;
  1003. /* Patching completed successfully and disable the manufacturer mode
  1004. * with reset and activate the downloaded firmware patches.
  1005. */
  1006. skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_activate),
  1007. mfg_reset_activate, HCI_INIT_TIMEOUT);
  1008. if (IS_ERR(skb)) {
  1009. BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
  1010. hdev->name, PTR_ERR(skb));
  1011. return -PTR_ERR(skb);
  1012. }
  1013. kfree_skb(skb);
  1014. BT_INFO("%s: Intel Bluetooth firmware patch completed and activated",
  1015. hdev->name);
  1016. return 0;
  1017. exit_mfg_disable:
  1018. /* Disable the manufacturer mode without reset */
  1019. skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_disable), mfg_disable,
  1020. HCI_INIT_TIMEOUT);
  1021. if (IS_ERR(skb)) {
  1022. BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
  1023. hdev->name, PTR_ERR(skb));
  1024. return -PTR_ERR(skb);
  1025. }
  1026. kfree_skb(skb);
  1027. BT_INFO("%s: Intel Bluetooth firmware patch completed", hdev->name);
  1028. return 0;
  1029. exit_mfg_deactivate:
  1030. release_firmware(fw);
  1031. /* Patching failed. Disable the manufacturer mode with reset and
  1032. * deactivate the downloaded firmware patches.
  1033. */
  1034. skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_deactivate),
  1035. mfg_reset_deactivate, HCI_INIT_TIMEOUT);
  1036. if (IS_ERR(skb)) {
  1037. BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
  1038. hdev->name, PTR_ERR(skb));
  1039. return -PTR_ERR(skb);
  1040. }
  1041. kfree_skb(skb);
  1042. BT_INFO("%s: Intel Bluetooth firmware patch completed and deactivated",
  1043. hdev->name);
  1044. return 0;
  1045. }
  1046. static int btusb_probe(struct usb_interface *intf,
  1047. const struct usb_device_id *id)
  1048. {
  1049. struct usb_endpoint_descriptor *ep_desc;
  1050. struct btusb_data *data;
  1051. struct hci_dev *hdev;
  1052. int i, err;
  1053. BT_DBG("intf %p id %p", intf, id);
  1054. /* interface numbers are hardcoded in the spec */
  1055. if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
  1056. return -ENODEV;
  1057. if (!id->driver_info) {
  1058. const struct usb_device_id *match;
  1059. match = usb_match_id(intf, blacklist_table);
  1060. if (match)
  1061. id = match;
  1062. }
  1063. if (id->driver_info == BTUSB_IGNORE)
  1064. return -ENODEV;
  1065. if (ignore_dga && id->driver_info & BTUSB_DIGIANSWER)
  1066. return -ENODEV;
  1067. if (ignore_csr && id->driver_info & BTUSB_CSR)
  1068. return -ENODEV;
  1069. if (ignore_sniffer && id->driver_info & BTUSB_SNIFFER)
  1070. return -ENODEV;
  1071. if (id->driver_info & BTUSB_ATH3012) {
  1072. struct usb_device *udev = interface_to_usbdev(intf);
  1073. /* Old firmware would otherwise let ath3k driver load
  1074. * patch and sysconfig files */
  1075. if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
  1076. return -ENODEV;
  1077. }
  1078. data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
  1079. if (!data)
  1080. return -ENOMEM;
  1081. for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
  1082. ep_desc = &intf->cur_altsetting->endpoint[i].desc;
  1083. if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
  1084. data->intr_ep = ep_desc;
  1085. continue;
  1086. }
  1087. if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
  1088. data->bulk_tx_ep = ep_desc;
  1089. continue;
  1090. }
  1091. if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
  1092. data->bulk_rx_ep = ep_desc;
  1093. continue;
  1094. }
  1095. }
  1096. if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
  1097. return -ENODEV;
  1098. data->cmdreq_type = USB_TYPE_CLASS;
  1099. data->udev = interface_to_usbdev(intf);
  1100. data->intf = intf;
  1101. spin_lock_init(&data->lock);
  1102. INIT_WORK(&data->work, btusb_work);
  1103. INIT_WORK(&data->waker, btusb_waker);
  1104. spin_lock_init(&data->txlock);
  1105. init_usb_anchor(&data->tx_anchor);
  1106. init_usb_anchor(&data->intr_anchor);
  1107. init_usb_anchor(&data->bulk_anchor);
  1108. init_usb_anchor(&data->isoc_anchor);
  1109. init_usb_anchor(&data->deferred);
  1110. hdev = hci_alloc_dev();
  1111. if (!hdev)
  1112. return -ENOMEM;
  1113. hdev->bus = HCI_USB;
  1114. hci_set_drvdata(hdev, data);
  1115. data->hdev = hdev;
  1116. SET_HCIDEV_DEV(hdev, &intf->dev);
  1117. hdev->open = btusb_open;
  1118. hdev->close = btusb_close;
  1119. hdev->flush = btusb_flush;
  1120. hdev->send = btusb_send_frame;
  1121. hdev->notify = btusb_notify;
  1122. if (id->driver_info & BTUSB_BCM92035)
  1123. hdev->setup = btusb_setup_bcm92035;
  1124. if (id->driver_info & BTUSB_INTEL)
  1125. hdev->setup = btusb_setup_intel;
  1126. /* Interface numbers are hardcoded in the specification */
  1127. data->isoc = usb_ifnum_to_if(data->udev, 1);
  1128. if (!reset)
  1129. set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
  1130. if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
  1131. if (!disable_scofix)
  1132. set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
  1133. }
  1134. if (id->driver_info & BTUSB_BROKEN_ISOC)
  1135. data->isoc = NULL;
  1136. if (id->driver_info & BTUSB_DIGIANSWER) {
  1137. data->cmdreq_type = USB_TYPE_VENDOR;
  1138. set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
  1139. }
  1140. if (id->driver_info & BTUSB_CSR) {
  1141. struct usb_device *udev = data->udev;
  1142. /* Old firmware would otherwise execute USB reset */
  1143. if (le16_to_cpu(udev->descriptor.bcdDevice) < 0x117)
  1144. set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
  1145. }
  1146. if (id->driver_info & BTUSB_SNIFFER) {
  1147. struct usb_device *udev = data->udev;
  1148. /* New sniffer firmware has crippled HCI interface */
  1149. if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
  1150. set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
  1151. data->isoc = NULL;
  1152. }
  1153. if (data->isoc) {
  1154. err = usb_driver_claim_interface(&btusb_driver,
  1155. data->isoc, data);
  1156. if (err < 0) {
  1157. hci_free_dev(hdev);
  1158. return err;
  1159. }
  1160. }
  1161. err = hci_register_dev(hdev);
  1162. if (err < 0) {
  1163. hci_free_dev(hdev);
  1164. return err;
  1165. }
  1166. usb_set_intfdata(intf, data);
  1167. return 0;
  1168. }
  1169. static void btusb_disconnect(struct usb_interface *intf)
  1170. {
  1171. struct btusb_data *data = usb_get_intfdata(intf);
  1172. struct hci_dev *hdev;
  1173. BT_DBG("intf %p", intf);
  1174. if (!data)
  1175. return;
  1176. hdev = data->hdev;
  1177. usb_set_intfdata(data->intf, NULL);
  1178. if (data->isoc)
  1179. usb_set_intfdata(data->isoc, NULL);
  1180. hci_unregister_dev(hdev);
  1181. if (intf == data->isoc)
  1182. usb_driver_release_interface(&btusb_driver, data->intf);
  1183. else if (data->isoc)
  1184. usb_driver_release_interface(&btusb_driver, data->isoc);
  1185. hci_free_dev(hdev);
  1186. }
  1187. #ifdef CONFIG_PM
  1188. static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
  1189. {
  1190. struct btusb_data *data = usb_get_intfdata(intf);
  1191. BT_DBG("intf %p", intf);
  1192. if (data->suspend_count++)
  1193. return 0;
  1194. spin_lock_irq(&data->txlock);
  1195. if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
  1196. set_bit(BTUSB_SUSPENDING, &data->flags);
  1197. spin_unlock_irq(&data->txlock);
  1198. } else {
  1199. spin_unlock_irq(&data->txlock);
  1200. data->suspend_count--;
  1201. return -EBUSY;
  1202. }
  1203. cancel_work_sync(&data->work);
  1204. btusb_stop_traffic(data);
  1205. usb_kill_anchored_urbs(&data->tx_anchor);
  1206. return 0;
  1207. }
  1208. static void play_deferred(struct btusb_data *data)
  1209. {
  1210. struct urb *urb;
  1211. int err;
  1212. while ((urb = usb_get_from_anchor(&data->deferred))) {
  1213. err = usb_submit_urb(urb, GFP_ATOMIC);
  1214. if (err < 0)
  1215. break;
  1216. data->tx_in_flight++;
  1217. }
  1218. usb_scuttle_anchored_urbs(&data->deferred);
  1219. }
  1220. static int btusb_resume(struct usb_interface *intf)
  1221. {
  1222. struct btusb_data *data = usb_get_intfdata(intf);
  1223. struct hci_dev *hdev = data->hdev;
  1224. int err = 0;
  1225. BT_DBG("intf %p", intf);
  1226. if (--data->suspend_count)
  1227. return 0;
  1228. if (!test_bit(HCI_RUNNING, &hdev->flags))
  1229. goto done;
  1230. if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
  1231. err = btusb_submit_intr_urb(hdev, GFP_NOIO);
  1232. if (err < 0) {
  1233. clear_bit(BTUSB_INTR_RUNNING, &data->flags);
  1234. goto failed;
  1235. }
  1236. }
  1237. if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
  1238. err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
  1239. if (err < 0) {
  1240. clear_bit(BTUSB_BULK_RUNNING, &data->flags);
  1241. goto failed;
  1242. }
  1243. btusb_submit_bulk_urb(hdev, GFP_NOIO);
  1244. }
  1245. if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
  1246. if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
  1247. clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
  1248. else
  1249. btusb_submit_isoc_urb(hdev, GFP_NOIO);
  1250. }
  1251. spin_lock_irq(&data->txlock);
  1252. play_deferred(data);
  1253. clear_bit(BTUSB_SUSPENDING, &data->flags);
  1254. spin_unlock_irq(&data->txlock);
  1255. schedule_work(&data->work);
  1256. return 0;
  1257. failed:
  1258. usb_scuttle_anchored_urbs(&data->deferred);
  1259. done:
  1260. spin_lock_irq(&data->txlock);
  1261. clear_bit(BTUSB_SUSPENDING, &data->flags);
  1262. spin_unlock_irq(&data->txlock);
  1263. return err;
  1264. }
  1265. #endif
  1266. static struct usb_driver btusb_driver = {
  1267. .name = "btusb",
  1268. .probe = btusb_probe,
  1269. .disconnect = btusb_disconnect,
  1270. #ifdef CONFIG_PM
  1271. .suspend = btusb_suspend,
  1272. .resume = btusb_resume,
  1273. #endif
  1274. .id_table = btusb_table,
  1275. .supports_autosuspend = 1,
  1276. .disable_hub_initiated_lpm = 1,
  1277. };
  1278. module_usb_driver(btusb_driver);
  1279. module_param(ignore_dga, bool, 0644);
  1280. MODULE_PARM_DESC(ignore_dga, "Ignore devices with id 08fd:0001");
  1281. module_param(ignore_csr, bool, 0644);
  1282. MODULE_PARM_DESC(ignore_csr, "Ignore devices with id 0a12:0001");
  1283. module_param(ignore_sniffer, bool, 0644);
  1284. MODULE_PARM_DESC(ignore_sniffer, "Ignore devices with id 0a12:0002");
  1285. module_param(disable_scofix, bool, 0644);
  1286. MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
  1287. module_param(force_scofix, bool, 0644);
  1288. MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
  1289. module_param(reset, bool, 0644);
  1290. MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
  1291. MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
  1292. MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
  1293. MODULE_VERSION(VERSION);
  1294. MODULE_LICENSE("GPL");