btusb.c 42 KB

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