btusb.c 43 KB

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