btusb.c 42 KB

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