btusb.c 43 KB

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