btusb.c 42 KB

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