btusb.c 42 KB

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