btusb.c 42 KB

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