btusb.c 42 KB

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