btusb.c 44 KB

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