btusb.c 44 KB

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