btusb.c 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008
  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 <asm/unaligned.h>
  27. #include <net/bluetooth/bluetooth.h>
  28. #include <net/bluetooth/hci_core.h>
  29. #include "btintel.h"
  30. #include "btbcm.h"
  31. #include "btrtl.h"
  32. #define VERSION "0.8"
  33. static bool disable_scofix;
  34. static bool force_scofix;
  35. static bool reset = true;
  36. static struct usb_driver btusb_driver;
  37. #define BTUSB_IGNORE 0x01
  38. #define BTUSB_DIGIANSWER 0x02
  39. #define BTUSB_CSR 0x04
  40. #define BTUSB_SNIFFER 0x08
  41. #define BTUSB_BCM92035 0x10
  42. #define BTUSB_BROKEN_ISOC 0x20
  43. #define BTUSB_WRONG_SCO_MTU 0x40
  44. #define BTUSB_ATH3012 0x80
  45. #define BTUSB_INTEL 0x100
  46. #define BTUSB_INTEL_BOOT 0x200
  47. #define BTUSB_BCM_PATCHRAM 0x400
  48. #define BTUSB_MARVELL 0x800
  49. #define BTUSB_SWAVE 0x1000
  50. #define BTUSB_INTEL_NEW 0x2000
  51. #define BTUSB_AMP 0x4000
  52. #define BTUSB_QCA_ROME 0x8000
  53. #define BTUSB_BCM_APPLE 0x10000
  54. #define BTUSB_REALTEK 0x20000
  55. static const struct usb_device_id btusb_table[] = {
  56. /* Generic Bluetooth USB device */
  57. { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
  58. /* Generic Bluetooth AMP device */
  59. { USB_DEVICE_INFO(0xe0, 0x01, 0x04), .driver_info = BTUSB_AMP },
  60. /* Apple-specific (Broadcom) devices */
  61. { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01),
  62. .driver_info = BTUSB_BCM_APPLE },
  63. /* MediaTek MT76x0E */
  64. { USB_DEVICE(0x0e8d, 0x763f) },
  65. /* Broadcom SoftSailing reporting vendor specific */
  66. { USB_DEVICE(0x0a5c, 0x21e1) },
  67. /* Apple MacBookPro 7,1 */
  68. { USB_DEVICE(0x05ac, 0x8213) },
  69. /* Apple iMac11,1 */
  70. { USB_DEVICE(0x05ac, 0x8215) },
  71. /* Apple MacBookPro6,2 */
  72. { USB_DEVICE(0x05ac, 0x8218) },
  73. /* Apple MacBookAir3,1, MacBookAir3,2 */
  74. { USB_DEVICE(0x05ac, 0x821b) },
  75. /* Apple MacBookAir4,1 */
  76. { USB_DEVICE(0x05ac, 0x821f) },
  77. /* Apple MacBookPro8,2 */
  78. { USB_DEVICE(0x05ac, 0x821a) },
  79. /* Apple MacMini5,1 */
  80. { USB_DEVICE(0x05ac, 0x8281) },
  81. /* AVM BlueFRITZ! USB v2.0 */
  82. { USB_DEVICE(0x057c, 0x3800), .driver_info = BTUSB_SWAVE },
  83. /* Bluetooth Ultraport Module from IBM */
  84. { USB_DEVICE(0x04bf, 0x030a) },
  85. /* ALPS Modules with non-standard id */
  86. { USB_DEVICE(0x044e, 0x3001) },
  87. { USB_DEVICE(0x044e, 0x3002) },
  88. /* Ericsson with non-standard id */
  89. { USB_DEVICE(0x0bdb, 0x1002) },
  90. /* Canyon CN-BTU1 with HID interfaces */
  91. { USB_DEVICE(0x0c10, 0x0000) },
  92. /* Broadcom BCM20702A0 */
  93. { USB_DEVICE(0x413c, 0x8197) },
  94. /* Broadcom BCM20702B0 (Dynex/Insignia) */
  95. { USB_DEVICE(0x19ff, 0x0239), .driver_info = BTUSB_BCM_PATCHRAM },
  96. /* Foxconn - Hon Hai */
  97. { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
  98. .driver_info = BTUSB_BCM_PATCHRAM },
  99. /* Lite-On Technology - Broadcom based */
  100. { USB_VENDOR_AND_INTERFACE_INFO(0x04ca, 0xff, 0x01, 0x01),
  101. .driver_info = BTUSB_BCM_PATCHRAM },
  102. /* Broadcom devices with vendor specific id */
  103. { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
  104. .driver_info = BTUSB_BCM_PATCHRAM },
  105. /* ASUSTek Computer - Broadcom based */
  106. { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01),
  107. .driver_info = BTUSB_BCM_PATCHRAM },
  108. /* Belkin F8065bf - Broadcom based */
  109. { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01),
  110. .driver_info = BTUSB_BCM_PATCHRAM },
  111. /* IMC Networks - Broadcom based */
  112. { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01),
  113. .driver_info = BTUSB_BCM_PATCHRAM },
  114. /* Intel Bluetooth USB Bootloader (RAM module) */
  115. { USB_DEVICE(0x8087, 0x0a5a),
  116. .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
  117. { } /* Terminating entry */
  118. };
  119. MODULE_DEVICE_TABLE(usb, btusb_table);
  120. static const struct usb_device_id blacklist_table[] = {
  121. /* CSR BlueCore devices */
  122. { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
  123. /* Broadcom BCM2033 without firmware */
  124. { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
  125. /* Atheros 3011 with sflash firmware */
  126. { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
  127. { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
  128. { USB_DEVICE(0x04f2, 0xaff1), .driver_info = BTUSB_IGNORE },
  129. { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
  130. { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
  131. { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
  132. { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
  133. /* Atheros AR9285 Malbec with sflash firmware */
  134. { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
  135. /* Atheros 3012 with sflash firmware */
  136. { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
  137. { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
  138. { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
  139. { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
  140. { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
  141. { USB_DEVICE(0x0489, 0xe076), .driver_info = BTUSB_ATH3012 },
  142. { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
  143. { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
  144. { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
  145. { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
  146. { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
  147. { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
  148. { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
  149. { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
  150. { USB_DEVICE(0x04ca, 0x300d), .driver_info = BTUSB_ATH3012 },
  151. { USB_DEVICE(0x04ca, 0x300f), .driver_info = BTUSB_ATH3012 },
  152. { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
  153. { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
  154. { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
  155. { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
  156. { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
  157. { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
  158. { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
  159. { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
  160. { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
  161. { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
  162. { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
  163. { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
  164. { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
  165. { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
  166. { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
  167. { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
  168. { USB_DEVICE(0x0cf3, 0xe006), .driver_info = BTUSB_ATH3012 },
  169. { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
  170. { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
  171. { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
  172. { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
  173. { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
  174. { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
  175. { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
  176. { USB_DEVICE(0x13d3, 0x3474), .driver_info = BTUSB_ATH3012 },
  177. /* Atheros AR5BBU12 with sflash firmware */
  178. { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
  179. /* Atheros AR5BBU12 with sflash firmware */
  180. { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
  181. { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
  182. /* QCA ROME chipset */
  183. { USB_DEVICE(0x0cf3, 0xe007), .driver_info = BTUSB_QCA_ROME },
  184. { USB_DEVICE(0x0cf3, 0xe300), .driver_info = BTUSB_QCA_ROME },
  185. { USB_DEVICE(0x0cf3, 0xe360), .driver_info = BTUSB_QCA_ROME },
  186. /* Broadcom BCM2035 */
  187. { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
  188. { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
  189. { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
  190. /* Broadcom BCM2045 */
  191. { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
  192. { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
  193. /* IBM/Lenovo ThinkPad with Broadcom chip */
  194. { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
  195. { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
  196. /* HP laptop with Broadcom chip */
  197. { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
  198. /* Dell laptop with Broadcom chip */
  199. { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
  200. /* Dell Wireless 370 and 410 devices */
  201. { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
  202. { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
  203. /* Belkin F8T012 and F8T013 devices */
  204. { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
  205. { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
  206. /* Asus WL-BTD202 device */
  207. { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
  208. /* Kensington Bluetooth USB adapter */
  209. { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
  210. /* RTX Telecom based adapters with buggy SCO support */
  211. { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
  212. { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
  213. /* CONWISE Technology based adapters with buggy SCO support */
  214. { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
  215. /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
  216. { USB_DEVICE(0x1310, 0x0001), .driver_info = BTUSB_SWAVE },
  217. /* Digianswer devices */
  218. { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
  219. { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
  220. /* CSR BlueCore Bluetooth Sniffer */
  221. { USB_DEVICE(0x0a12, 0x0002),
  222. .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
  223. /* Frontline ComProbe Bluetooth Sniffer */
  224. { USB_DEVICE(0x16d3, 0x0002),
  225. .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
  226. /* Marvell Bluetooth devices */
  227. { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
  228. { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
  229. /* Intel Bluetooth devices */
  230. { USB_DEVICE(0x8087, 0x07da), .driver_info = BTUSB_CSR },
  231. { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL },
  232. { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL },
  233. { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_NEW },
  234. /* Other Intel Bluetooth devices */
  235. { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
  236. .driver_info = BTUSB_IGNORE },
  237. /* Realtek Bluetooth devices */
  238. { USB_VENDOR_AND_INTERFACE_INFO(0x0bda, 0xe0, 0x01, 0x01),
  239. .driver_info = BTUSB_REALTEK },
  240. /* Additional Realtek 8723AE Bluetooth devices */
  241. { USB_DEVICE(0x0930, 0x021d), .driver_info = BTUSB_REALTEK },
  242. { USB_DEVICE(0x13d3, 0x3394), .driver_info = BTUSB_REALTEK },
  243. /* Additional Realtek 8723BE Bluetooth devices */
  244. { USB_DEVICE(0x0489, 0xe085), .driver_info = BTUSB_REALTEK },
  245. { USB_DEVICE(0x0489, 0xe08b), .driver_info = BTUSB_REALTEK },
  246. { USB_DEVICE(0x13d3, 0x3410), .driver_info = BTUSB_REALTEK },
  247. { USB_DEVICE(0x13d3, 0x3416), .driver_info = BTUSB_REALTEK },
  248. { USB_DEVICE(0x13d3, 0x3459), .driver_info = BTUSB_REALTEK },
  249. /* Additional Realtek 8821AE Bluetooth devices */
  250. { USB_DEVICE(0x0b05, 0x17dc), .driver_info = BTUSB_REALTEK },
  251. { USB_DEVICE(0x13d3, 0x3414), .driver_info = BTUSB_REALTEK },
  252. { USB_DEVICE(0x13d3, 0x3458), .driver_info = BTUSB_REALTEK },
  253. { USB_DEVICE(0x13d3, 0x3461), .driver_info = BTUSB_REALTEK },
  254. { USB_DEVICE(0x13d3, 0x3462), .driver_info = BTUSB_REALTEK },
  255. { } /* Terminating entry */
  256. };
  257. #define BTUSB_MAX_ISOC_FRAMES 10
  258. #define BTUSB_INTR_RUNNING 0
  259. #define BTUSB_BULK_RUNNING 1
  260. #define BTUSB_ISOC_RUNNING 2
  261. #define BTUSB_SUSPENDING 3
  262. #define BTUSB_DID_ISO_RESUME 4
  263. #define BTUSB_BOOTLOADER 5
  264. #define BTUSB_DOWNLOADING 6
  265. #define BTUSB_FIRMWARE_LOADED 7
  266. #define BTUSB_FIRMWARE_FAILED 8
  267. #define BTUSB_BOOTING 9
  268. #define BTUSB_RESET_RESUME 10
  269. struct btusb_data {
  270. struct hci_dev *hdev;
  271. struct usb_device *udev;
  272. struct usb_interface *intf;
  273. struct usb_interface *isoc;
  274. unsigned long flags;
  275. struct work_struct work;
  276. struct work_struct waker;
  277. struct usb_anchor deferred;
  278. struct usb_anchor tx_anchor;
  279. int tx_in_flight;
  280. spinlock_t txlock;
  281. struct usb_anchor intr_anchor;
  282. struct usb_anchor bulk_anchor;
  283. struct usb_anchor isoc_anchor;
  284. spinlock_t rxlock;
  285. struct sk_buff *evt_skb;
  286. struct sk_buff *acl_skb;
  287. struct sk_buff *sco_skb;
  288. struct usb_endpoint_descriptor *intr_ep;
  289. struct usb_endpoint_descriptor *bulk_tx_ep;
  290. struct usb_endpoint_descriptor *bulk_rx_ep;
  291. struct usb_endpoint_descriptor *isoc_tx_ep;
  292. struct usb_endpoint_descriptor *isoc_rx_ep;
  293. __u8 cmdreq_type;
  294. __u8 cmdreq;
  295. unsigned int sco_num;
  296. int isoc_altsetting;
  297. int suspend_count;
  298. int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
  299. int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
  300. int (*setup_on_usb)(struct hci_dev *hdev);
  301. };
  302. static inline void btusb_free_frags(struct btusb_data *data)
  303. {
  304. unsigned long flags;
  305. spin_lock_irqsave(&data->rxlock, flags);
  306. kfree_skb(data->evt_skb);
  307. data->evt_skb = NULL;
  308. kfree_skb(data->acl_skb);
  309. data->acl_skb = NULL;
  310. kfree_skb(data->sco_skb);
  311. data->sco_skb = NULL;
  312. spin_unlock_irqrestore(&data->rxlock, flags);
  313. }
  314. static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
  315. {
  316. struct sk_buff *skb;
  317. int err = 0;
  318. spin_lock(&data->rxlock);
  319. skb = data->evt_skb;
  320. while (count) {
  321. int len;
  322. if (!skb) {
  323. skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
  324. if (!skb) {
  325. err = -ENOMEM;
  326. break;
  327. }
  328. bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
  329. bt_cb(skb)->expect = HCI_EVENT_HDR_SIZE;
  330. }
  331. len = min_t(uint, bt_cb(skb)->expect, count);
  332. memcpy(skb_put(skb, len), buffer, len);
  333. count -= len;
  334. buffer += len;
  335. bt_cb(skb)->expect -= len;
  336. if (skb->len == HCI_EVENT_HDR_SIZE) {
  337. /* Complete event header */
  338. bt_cb(skb)->expect = hci_event_hdr(skb)->plen;
  339. if (skb_tailroom(skb) < bt_cb(skb)->expect) {
  340. kfree_skb(skb);
  341. skb = NULL;
  342. err = -EILSEQ;
  343. break;
  344. }
  345. }
  346. if (bt_cb(skb)->expect == 0) {
  347. /* Complete frame */
  348. data->recv_event(data->hdev, skb);
  349. skb = NULL;
  350. }
  351. }
  352. data->evt_skb = skb;
  353. spin_unlock(&data->rxlock);
  354. return err;
  355. }
  356. static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
  357. {
  358. struct sk_buff *skb;
  359. int err = 0;
  360. spin_lock(&data->rxlock);
  361. skb = data->acl_skb;
  362. while (count) {
  363. int len;
  364. if (!skb) {
  365. skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
  366. if (!skb) {
  367. err = -ENOMEM;
  368. break;
  369. }
  370. bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT;
  371. bt_cb(skb)->expect = HCI_ACL_HDR_SIZE;
  372. }
  373. len = min_t(uint, bt_cb(skb)->expect, count);
  374. memcpy(skb_put(skb, len), buffer, len);
  375. count -= len;
  376. buffer += len;
  377. bt_cb(skb)->expect -= len;
  378. if (skb->len == HCI_ACL_HDR_SIZE) {
  379. __le16 dlen = hci_acl_hdr(skb)->dlen;
  380. /* Complete ACL header */
  381. bt_cb(skb)->expect = __le16_to_cpu(dlen);
  382. if (skb_tailroom(skb) < bt_cb(skb)->expect) {
  383. kfree_skb(skb);
  384. skb = NULL;
  385. err = -EILSEQ;
  386. break;
  387. }
  388. }
  389. if (bt_cb(skb)->expect == 0) {
  390. /* Complete frame */
  391. hci_recv_frame(data->hdev, skb);
  392. skb = NULL;
  393. }
  394. }
  395. data->acl_skb = skb;
  396. spin_unlock(&data->rxlock);
  397. return err;
  398. }
  399. static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
  400. {
  401. struct sk_buff *skb;
  402. int err = 0;
  403. spin_lock(&data->rxlock);
  404. skb = data->sco_skb;
  405. while (count) {
  406. int len;
  407. if (!skb) {
  408. skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
  409. if (!skb) {
  410. err = -ENOMEM;
  411. break;
  412. }
  413. bt_cb(skb)->pkt_type = HCI_SCODATA_PKT;
  414. bt_cb(skb)->expect = HCI_SCO_HDR_SIZE;
  415. }
  416. len = min_t(uint, bt_cb(skb)->expect, count);
  417. memcpy(skb_put(skb, len), buffer, len);
  418. count -= len;
  419. buffer += len;
  420. bt_cb(skb)->expect -= len;
  421. if (skb->len == HCI_SCO_HDR_SIZE) {
  422. /* Complete SCO header */
  423. bt_cb(skb)->expect = hci_sco_hdr(skb)->dlen;
  424. if (skb_tailroom(skb) < bt_cb(skb)->expect) {
  425. kfree_skb(skb);
  426. skb = NULL;
  427. err = -EILSEQ;
  428. break;
  429. }
  430. }
  431. if (bt_cb(skb)->expect == 0) {
  432. /* Complete frame */
  433. hci_recv_frame(data->hdev, skb);
  434. skb = NULL;
  435. }
  436. }
  437. data->sco_skb = skb;
  438. spin_unlock(&data->rxlock);
  439. return err;
  440. }
  441. static void btusb_intr_complete(struct urb *urb)
  442. {
  443. struct hci_dev *hdev = urb->context;
  444. struct btusb_data *data = hci_get_drvdata(hdev);
  445. int err;
  446. BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
  447. urb->actual_length);
  448. if (!test_bit(HCI_RUNNING, &hdev->flags))
  449. return;
  450. if (urb->status == 0) {
  451. hdev->stat.byte_rx += urb->actual_length;
  452. if (btusb_recv_intr(data, urb->transfer_buffer,
  453. urb->actual_length) < 0) {
  454. BT_ERR("%s corrupted event packet", hdev->name);
  455. hdev->stat.err_rx++;
  456. }
  457. } else if (urb->status == -ENOENT) {
  458. /* Avoid suspend failed when usb_kill_urb */
  459. return;
  460. }
  461. if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
  462. return;
  463. usb_mark_last_busy(data->udev);
  464. usb_anchor_urb(urb, &data->intr_anchor);
  465. err = usb_submit_urb(urb, GFP_ATOMIC);
  466. if (err < 0) {
  467. /* -EPERM: urb is being killed;
  468. * -ENODEV: device got disconnected */
  469. if (err != -EPERM && err != -ENODEV)
  470. BT_ERR("%s urb %p failed to resubmit (%d)",
  471. hdev->name, urb, -err);
  472. usb_unanchor_urb(urb);
  473. }
  474. }
  475. static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
  476. {
  477. struct btusb_data *data = hci_get_drvdata(hdev);
  478. struct urb *urb;
  479. unsigned char *buf;
  480. unsigned int pipe;
  481. int err, size;
  482. BT_DBG("%s", hdev->name);
  483. if (!data->intr_ep)
  484. return -ENODEV;
  485. urb = usb_alloc_urb(0, mem_flags);
  486. if (!urb)
  487. return -ENOMEM;
  488. size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
  489. buf = kmalloc(size, mem_flags);
  490. if (!buf) {
  491. usb_free_urb(urb);
  492. return -ENOMEM;
  493. }
  494. pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
  495. usb_fill_int_urb(urb, data->udev, pipe, buf, size,
  496. btusb_intr_complete, hdev, data->intr_ep->bInterval);
  497. urb->transfer_flags |= URB_FREE_BUFFER;
  498. usb_anchor_urb(urb, &data->intr_anchor);
  499. err = usb_submit_urb(urb, mem_flags);
  500. if (err < 0) {
  501. if (err != -EPERM && err != -ENODEV)
  502. BT_ERR("%s urb %p submission failed (%d)",
  503. hdev->name, urb, -err);
  504. usb_unanchor_urb(urb);
  505. }
  506. usb_free_urb(urb);
  507. return err;
  508. }
  509. static void btusb_bulk_complete(struct urb *urb)
  510. {
  511. struct hci_dev *hdev = urb->context;
  512. struct btusb_data *data = hci_get_drvdata(hdev);
  513. int err;
  514. BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
  515. urb->actual_length);
  516. if (!test_bit(HCI_RUNNING, &hdev->flags))
  517. return;
  518. if (urb->status == 0) {
  519. hdev->stat.byte_rx += urb->actual_length;
  520. if (data->recv_bulk(data, urb->transfer_buffer,
  521. urb->actual_length) < 0) {
  522. BT_ERR("%s corrupted ACL packet", hdev->name);
  523. hdev->stat.err_rx++;
  524. }
  525. } else if (urb->status == -ENOENT) {
  526. /* Avoid suspend failed when usb_kill_urb */
  527. return;
  528. }
  529. if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
  530. return;
  531. usb_anchor_urb(urb, &data->bulk_anchor);
  532. usb_mark_last_busy(data->udev);
  533. err = usb_submit_urb(urb, GFP_ATOMIC);
  534. if (err < 0) {
  535. /* -EPERM: urb is being killed;
  536. * -ENODEV: device got disconnected */
  537. if (err != -EPERM && err != -ENODEV)
  538. BT_ERR("%s urb %p failed to resubmit (%d)",
  539. hdev->name, urb, -err);
  540. usb_unanchor_urb(urb);
  541. }
  542. }
  543. static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
  544. {
  545. struct btusb_data *data = hci_get_drvdata(hdev);
  546. struct urb *urb;
  547. unsigned char *buf;
  548. unsigned int pipe;
  549. int err, size = HCI_MAX_FRAME_SIZE;
  550. BT_DBG("%s", hdev->name);
  551. if (!data->bulk_rx_ep)
  552. return -ENODEV;
  553. urb = usb_alloc_urb(0, mem_flags);
  554. if (!urb)
  555. return -ENOMEM;
  556. buf = kmalloc(size, mem_flags);
  557. if (!buf) {
  558. usb_free_urb(urb);
  559. return -ENOMEM;
  560. }
  561. pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
  562. usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
  563. btusb_bulk_complete, hdev);
  564. urb->transfer_flags |= URB_FREE_BUFFER;
  565. usb_mark_last_busy(data->udev);
  566. usb_anchor_urb(urb, &data->bulk_anchor);
  567. err = usb_submit_urb(urb, mem_flags);
  568. if (err < 0) {
  569. if (err != -EPERM && err != -ENODEV)
  570. BT_ERR("%s urb %p submission failed (%d)",
  571. hdev->name, urb, -err);
  572. usb_unanchor_urb(urb);
  573. }
  574. usb_free_urb(urb);
  575. return err;
  576. }
  577. static void btusb_isoc_complete(struct urb *urb)
  578. {
  579. struct hci_dev *hdev = urb->context;
  580. struct btusb_data *data = hci_get_drvdata(hdev);
  581. int i, err;
  582. BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
  583. urb->actual_length);
  584. if (!test_bit(HCI_RUNNING, &hdev->flags))
  585. return;
  586. if (urb->status == 0) {
  587. for (i = 0; i < urb->number_of_packets; i++) {
  588. unsigned int offset = urb->iso_frame_desc[i].offset;
  589. unsigned int length = urb->iso_frame_desc[i].actual_length;
  590. if (urb->iso_frame_desc[i].status)
  591. continue;
  592. hdev->stat.byte_rx += length;
  593. if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
  594. length) < 0) {
  595. BT_ERR("%s corrupted SCO packet", hdev->name);
  596. hdev->stat.err_rx++;
  597. }
  598. }
  599. } else if (urb->status == -ENOENT) {
  600. /* Avoid suspend failed when usb_kill_urb */
  601. return;
  602. }
  603. if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
  604. return;
  605. usb_anchor_urb(urb, &data->isoc_anchor);
  606. err = usb_submit_urb(urb, GFP_ATOMIC);
  607. if (err < 0) {
  608. /* -EPERM: urb is being killed;
  609. * -ENODEV: device got disconnected */
  610. if (err != -EPERM && err != -ENODEV)
  611. BT_ERR("%s urb %p failed to resubmit (%d)",
  612. hdev->name, urb, -err);
  613. usb_unanchor_urb(urb);
  614. }
  615. }
  616. static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
  617. {
  618. int i, offset = 0;
  619. BT_DBG("len %d mtu %d", len, mtu);
  620. for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
  621. i++, offset += mtu, len -= mtu) {
  622. urb->iso_frame_desc[i].offset = offset;
  623. urb->iso_frame_desc[i].length = mtu;
  624. }
  625. if (len && i < BTUSB_MAX_ISOC_FRAMES) {
  626. urb->iso_frame_desc[i].offset = offset;
  627. urb->iso_frame_desc[i].length = len;
  628. i++;
  629. }
  630. urb->number_of_packets = i;
  631. }
  632. static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
  633. {
  634. struct btusb_data *data = hci_get_drvdata(hdev);
  635. struct urb *urb;
  636. unsigned char *buf;
  637. unsigned int pipe;
  638. int err, size;
  639. BT_DBG("%s", hdev->name);
  640. if (!data->isoc_rx_ep)
  641. return -ENODEV;
  642. urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
  643. if (!urb)
  644. return -ENOMEM;
  645. size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
  646. BTUSB_MAX_ISOC_FRAMES;
  647. buf = kmalloc(size, mem_flags);
  648. if (!buf) {
  649. usb_free_urb(urb);
  650. return -ENOMEM;
  651. }
  652. pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
  653. usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
  654. hdev, data->isoc_rx_ep->bInterval);
  655. urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
  656. __fill_isoc_descriptor(urb, size,
  657. le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
  658. usb_anchor_urb(urb, &data->isoc_anchor);
  659. err = usb_submit_urb(urb, mem_flags);
  660. if (err < 0) {
  661. if (err != -EPERM && err != -ENODEV)
  662. BT_ERR("%s urb %p submission failed (%d)",
  663. hdev->name, urb, -err);
  664. usb_unanchor_urb(urb);
  665. }
  666. usb_free_urb(urb);
  667. return err;
  668. }
  669. static void btusb_tx_complete(struct urb *urb)
  670. {
  671. struct sk_buff *skb = urb->context;
  672. struct hci_dev *hdev = (struct hci_dev *)skb->dev;
  673. struct btusb_data *data = hci_get_drvdata(hdev);
  674. BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
  675. urb->actual_length);
  676. if (!test_bit(HCI_RUNNING, &hdev->flags))
  677. goto done;
  678. if (!urb->status)
  679. hdev->stat.byte_tx += urb->transfer_buffer_length;
  680. else
  681. hdev->stat.err_tx++;
  682. done:
  683. spin_lock(&data->txlock);
  684. data->tx_in_flight--;
  685. spin_unlock(&data->txlock);
  686. kfree(urb->setup_packet);
  687. kfree_skb(skb);
  688. }
  689. static void btusb_isoc_tx_complete(struct urb *urb)
  690. {
  691. struct sk_buff *skb = urb->context;
  692. struct hci_dev *hdev = (struct hci_dev *)skb->dev;
  693. BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
  694. urb->actual_length);
  695. if (!test_bit(HCI_RUNNING, &hdev->flags))
  696. goto done;
  697. if (!urb->status)
  698. hdev->stat.byte_tx += urb->transfer_buffer_length;
  699. else
  700. hdev->stat.err_tx++;
  701. done:
  702. kfree(urb->setup_packet);
  703. kfree_skb(skb);
  704. }
  705. static int btusb_open(struct hci_dev *hdev)
  706. {
  707. struct btusb_data *data = hci_get_drvdata(hdev);
  708. int err;
  709. BT_DBG("%s", hdev->name);
  710. /* Patching USB firmware files prior to starting any URBs of HCI path
  711. * It is more safe to use USB bulk channel for downloading USB patch
  712. */
  713. if (data->setup_on_usb) {
  714. err = data->setup_on_usb(hdev);
  715. if (err < 0)
  716. return err;
  717. }
  718. err = usb_autopm_get_interface(data->intf);
  719. if (err < 0)
  720. return err;
  721. data->intf->needs_remote_wakeup = 1;
  722. if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
  723. goto done;
  724. if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
  725. goto done;
  726. err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
  727. if (err < 0)
  728. goto failed;
  729. err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
  730. if (err < 0) {
  731. usb_kill_anchored_urbs(&data->intr_anchor);
  732. goto failed;
  733. }
  734. set_bit(BTUSB_BULK_RUNNING, &data->flags);
  735. btusb_submit_bulk_urb(hdev, GFP_KERNEL);
  736. done:
  737. usb_autopm_put_interface(data->intf);
  738. return 0;
  739. failed:
  740. clear_bit(BTUSB_INTR_RUNNING, &data->flags);
  741. clear_bit(HCI_RUNNING, &hdev->flags);
  742. usb_autopm_put_interface(data->intf);
  743. return err;
  744. }
  745. static void btusb_stop_traffic(struct btusb_data *data)
  746. {
  747. usb_kill_anchored_urbs(&data->intr_anchor);
  748. usb_kill_anchored_urbs(&data->bulk_anchor);
  749. usb_kill_anchored_urbs(&data->isoc_anchor);
  750. }
  751. static int btusb_close(struct hci_dev *hdev)
  752. {
  753. struct btusb_data *data = hci_get_drvdata(hdev);
  754. int err;
  755. BT_DBG("%s", hdev->name);
  756. if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
  757. return 0;
  758. cancel_work_sync(&data->work);
  759. cancel_work_sync(&data->waker);
  760. clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
  761. clear_bit(BTUSB_BULK_RUNNING, &data->flags);
  762. clear_bit(BTUSB_INTR_RUNNING, &data->flags);
  763. btusb_stop_traffic(data);
  764. btusb_free_frags(data);
  765. err = usb_autopm_get_interface(data->intf);
  766. if (err < 0)
  767. goto failed;
  768. data->intf->needs_remote_wakeup = 0;
  769. usb_autopm_put_interface(data->intf);
  770. failed:
  771. usb_scuttle_anchored_urbs(&data->deferred);
  772. return 0;
  773. }
  774. static int btusb_flush(struct hci_dev *hdev)
  775. {
  776. struct btusb_data *data = hci_get_drvdata(hdev);
  777. BT_DBG("%s", hdev->name);
  778. usb_kill_anchored_urbs(&data->tx_anchor);
  779. btusb_free_frags(data);
  780. return 0;
  781. }
  782. static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
  783. {
  784. struct btusb_data *data = hci_get_drvdata(hdev);
  785. struct usb_ctrlrequest *dr;
  786. struct urb *urb;
  787. unsigned int pipe;
  788. urb = usb_alloc_urb(0, GFP_KERNEL);
  789. if (!urb)
  790. return ERR_PTR(-ENOMEM);
  791. dr = kmalloc(sizeof(*dr), GFP_KERNEL);
  792. if (!dr) {
  793. usb_free_urb(urb);
  794. return ERR_PTR(-ENOMEM);
  795. }
  796. dr->bRequestType = data->cmdreq_type;
  797. dr->bRequest = data->cmdreq;
  798. dr->wIndex = 0;
  799. dr->wValue = 0;
  800. dr->wLength = __cpu_to_le16(skb->len);
  801. pipe = usb_sndctrlpipe(data->udev, 0x00);
  802. usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
  803. skb->data, skb->len, btusb_tx_complete, skb);
  804. skb->dev = (void *)hdev;
  805. return urb;
  806. }
  807. static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
  808. {
  809. struct btusb_data *data = hci_get_drvdata(hdev);
  810. struct urb *urb;
  811. unsigned int pipe;
  812. if (!data->bulk_tx_ep)
  813. return ERR_PTR(-ENODEV);
  814. urb = usb_alloc_urb(0, GFP_KERNEL);
  815. if (!urb)
  816. return ERR_PTR(-ENOMEM);
  817. pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
  818. usb_fill_bulk_urb(urb, data->udev, pipe,
  819. skb->data, skb->len, btusb_tx_complete, skb);
  820. skb->dev = (void *)hdev;
  821. return urb;
  822. }
  823. static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
  824. {
  825. struct btusb_data *data = hci_get_drvdata(hdev);
  826. struct urb *urb;
  827. unsigned int pipe;
  828. if (!data->isoc_tx_ep)
  829. return ERR_PTR(-ENODEV);
  830. urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
  831. if (!urb)
  832. return ERR_PTR(-ENOMEM);
  833. pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
  834. usb_fill_int_urb(urb, data->udev, pipe,
  835. skb->data, skb->len, btusb_isoc_tx_complete,
  836. skb, data->isoc_tx_ep->bInterval);
  837. urb->transfer_flags = URB_ISO_ASAP;
  838. __fill_isoc_descriptor(urb, skb->len,
  839. le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
  840. skb->dev = (void *)hdev;
  841. return urb;
  842. }
  843. static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
  844. {
  845. struct btusb_data *data = hci_get_drvdata(hdev);
  846. int err;
  847. usb_anchor_urb(urb, &data->tx_anchor);
  848. err = usb_submit_urb(urb, GFP_KERNEL);
  849. if (err < 0) {
  850. if (err != -EPERM && err != -ENODEV)
  851. BT_ERR("%s urb %p submission failed (%d)",
  852. hdev->name, urb, -err);
  853. kfree(urb->setup_packet);
  854. usb_unanchor_urb(urb);
  855. } else {
  856. usb_mark_last_busy(data->udev);
  857. }
  858. usb_free_urb(urb);
  859. return err;
  860. }
  861. static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
  862. {
  863. struct btusb_data *data = hci_get_drvdata(hdev);
  864. unsigned long flags;
  865. bool suspending;
  866. spin_lock_irqsave(&data->txlock, flags);
  867. suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
  868. if (!suspending)
  869. data->tx_in_flight++;
  870. spin_unlock_irqrestore(&data->txlock, flags);
  871. if (!suspending)
  872. return submit_tx_urb(hdev, urb);
  873. usb_anchor_urb(urb, &data->deferred);
  874. schedule_work(&data->waker);
  875. usb_free_urb(urb);
  876. return 0;
  877. }
  878. static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
  879. {
  880. struct urb *urb;
  881. BT_DBG("%s", hdev->name);
  882. if (!test_bit(HCI_RUNNING, &hdev->flags))
  883. return -EBUSY;
  884. switch (bt_cb(skb)->pkt_type) {
  885. case HCI_COMMAND_PKT:
  886. urb = alloc_ctrl_urb(hdev, skb);
  887. if (IS_ERR(urb))
  888. return PTR_ERR(urb);
  889. hdev->stat.cmd_tx++;
  890. return submit_or_queue_tx_urb(hdev, urb);
  891. case HCI_ACLDATA_PKT:
  892. urb = alloc_bulk_urb(hdev, skb);
  893. if (IS_ERR(urb))
  894. return PTR_ERR(urb);
  895. hdev->stat.acl_tx++;
  896. return submit_or_queue_tx_urb(hdev, urb);
  897. case HCI_SCODATA_PKT:
  898. if (hci_conn_num(hdev, SCO_LINK) < 1)
  899. return -ENODEV;
  900. urb = alloc_isoc_urb(hdev, skb);
  901. if (IS_ERR(urb))
  902. return PTR_ERR(urb);
  903. hdev->stat.sco_tx++;
  904. return submit_tx_urb(hdev, urb);
  905. }
  906. return -EILSEQ;
  907. }
  908. static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
  909. {
  910. struct btusb_data *data = hci_get_drvdata(hdev);
  911. BT_DBG("%s evt %d", hdev->name, evt);
  912. if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
  913. data->sco_num = hci_conn_num(hdev, SCO_LINK);
  914. schedule_work(&data->work);
  915. }
  916. }
  917. static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
  918. {
  919. struct btusb_data *data = hci_get_drvdata(hdev);
  920. struct usb_interface *intf = data->isoc;
  921. struct usb_endpoint_descriptor *ep_desc;
  922. int i, err;
  923. if (!data->isoc)
  924. return -ENODEV;
  925. err = usb_set_interface(data->udev, 1, altsetting);
  926. if (err < 0) {
  927. BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
  928. return err;
  929. }
  930. data->isoc_altsetting = altsetting;
  931. data->isoc_tx_ep = NULL;
  932. data->isoc_rx_ep = NULL;
  933. for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
  934. ep_desc = &intf->cur_altsetting->endpoint[i].desc;
  935. if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
  936. data->isoc_tx_ep = ep_desc;
  937. continue;
  938. }
  939. if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
  940. data->isoc_rx_ep = ep_desc;
  941. continue;
  942. }
  943. }
  944. if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
  945. BT_ERR("%s invalid SCO descriptors", hdev->name);
  946. return -ENODEV;
  947. }
  948. return 0;
  949. }
  950. static void btusb_work(struct work_struct *work)
  951. {
  952. struct btusb_data *data = container_of(work, struct btusb_data, work);
  953. struct hci_dev *hdev = data->hdev;
  954. int new_alts;
  955. int err;
  956. if (data->sco_num > 0) {
  957. if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
  958. err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
  959. if (err < 0) {
  960. clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
  961. usb_kill_anchored_urbs(&data->isoc_anchor);
  962. return;
  963. }
  964. set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
  965. }
  966. if (hdev->voice_setting & 0x0020) {
  967. static const int alts[3] = { 2, 4, 5 };
  968. new_alts = alts[data->sco_num - 1];
  969. } else {
  970. new_alts = data->sco_num;
  971. }
  972. if (data->isoc_altsetting != new_alts) {
  973. clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
  974. usb_kill_anchored_urbs(&data->isoc_anchor);
  975. if (__set_isoc_interface(hdev, new_alts) < 0)
  976. return;
  977. }
  978. if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
  979. if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
  980. clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
  981. else
  982. btusb_submit_isoc_urb(hdev, GFP_KERNEL);
  983. }
  984. } else {
  985. clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
  986. usb_kill_anchored_urbs(&data->isoc_anchor);
  987. __set_isoc_interface(hdev, 0);
  988. if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
  989. usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
  990. }
  991. }
  992. static void btusb_waker(struct work_struct *work)
  993. {
  994. struct btusb_data *data = container_of(work, struct btusb_data, waker);
  995. int err;
  996. err = usb_autopm_get_interface(data->intf);
  997. if (err < 0)
  998. return;
  999. usb_autopm_put_interface(data->intf);
  1000. }
  1001. static int btusb_setup_bcm92035(struct hci_dev *hdev)
  1002. {
  1003. struct sk_buff *skb;
  1004. u8 val = 0x00;
  1005. BT_DBG("%s", hdev->name);
  1006. skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
  1007. if (IS_ERR(skb))
  1008. BT_ERR("BCM92035 command failed (%ld)", -PTR_ERR(skb));
  1009. else
  1010. kfree_skb(skb);
  1011. return 0;
  1012. }
  1013. static int btusb_setup_csr(struct hci_dev *hdev)
  1014. {
  1015. struct hci_rp_read_local_version *rp;
  1016. struct sk_buff *skb;
  1017. BT_DBG("%s", hdev->name);
  1018. skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
  1019. HCI_INIT_TIMEOUT);
  1020. if (IS_ERR(skb)) {
  1021. int err = PTR_ERR(skb);
  1022. BT_ERR("%s: CSR: Local version failed (%d)", hdev->name, err);
  1023. return err;
  1024. }
  1025. if (skb->len != sizeof(struct hci_rp_read_local_version)) {
  1026. BT_ERR("%s: CSR: Local version length mismatch", hdev->name);
  1027. kfree_skb(skb);
  1028. return -EIO;
  1029. }
  1030. rp = (struct hci_rp_read_local_version *)skb->data;
  1031. if (le16_to_cpu(rp->manufacturer) != 10) {
  1032. /* Clear the reset quirk since this is not an actual
  1033. * early Bluetooth 1.1 device from CSR.
  1034. */
  1035. clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
  1036. /* These fake CSR controllers have all a broken
  1037. * stored link key handling and so just disable it.
  1038. */
  1039. set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
  1040. }
  1041. kfree_skb(skb);
  1042. return 0;
  1043. }
  1044. static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
  1045. struct intel_version *ver)
  1046. {
  1047. const struct firmware *fw;
  1048. char fwname[64];
  1049. int ret;
  1050. snprintf(fwname, sizeof(fwname),
  1051. "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
  1052. ver->hw_platform, ver->hw_variant, ver->hw_revision,
  1053. ver->fw_variant, ver->fw_revision, ver->fw_build_num,
  1054. ver->fw_build_ww, ver->fw_build_yy);
  1055. ret = request_firmware(&fw, fwname, &hdev->dev);
  1056. if (ret < 0) {
  1057. if (ret == -EINVAL) {
  1058. BT_ERR("%s Intel firmware file request failed (%d)",
  1059. hdev->name, ret);
  1060. return NULL;
  1061. }
  1062. BT_ERR("%s failed to open Intel firmware file: %s(%d)",
  1063. hdev->name, fwname, ret);
  1064. /* If the correct firmware patch file is not found, use the
  1065. * default firmware patch file instead
  1066. */
  1067. snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
  1068. ver->hw_platform, ver->hw_variant);
  1069. if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
  1070. BT_ERR("%s failed to open default Intel fw file: %s",
  1071. hdev->name, fwname);
  1072. return NULL;
  1073. }
  1074. }
  1075. BT_INFO("%s: Intel Bluetooth firmware file: %s", hdev->name, fwname);
  1076. return fw;
  1077. }
  1078. static int btusb_setup_intel_patching(struct hci_dev *hdev,
  1079. const struct firmware *fw,
  1080. const u8 **fw_ptr, int *disable_patch)
  1081. {
  1082. struct sk_buff *skb;
  1083. struct hci_command_hdr *cmd;
  1084. const u8 *cmd_param;
  1085. struct hci_event_hdr *evt = NULL;
  1086. const u8 *evt_param = NULL;
  1087. int remain = fw->size - (*fw_ptr - fw->data);
  1088. /* The first byte indicates the types of the patch command or event.
  1089. * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
  1090. * in the current firmware buffer doesn't start with 0x01 or
  1091. * the size of remain buffer is smaller than HCI command header,
  1092. * the firmware file is corrupted and it should stop the patching
  1093. * process.
  1094. */
  1095. if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
  1096. BT_ERR("%s Intel fw corrupted: invalid cmd read", hdev->name);
  1097. return -EINVAL;
  1098. }
  1099. (*fw_ptr)++;
  1100. remain--;
  1101. cmd = (struct hci_command_hdr *)(*fw_ptr);
  1102. *fw_ptr += sizeof(*cmd);
  1103. remain -= sizeof(*cmd);
  1104. /* Ensure that the remain firmware data is long enough than the length
  1105. * of command parameter. If not, the firmware file is corrupted.
  1106. */
  1107. if (remain < cmd->plen) {
  1108. BT_ERR("%s Intel fw corrupted: invalid cmd len", hdev->name);
  1109. return -EFAULT;
  1110. }
  1111. /* If there is a command that loads a patch in the firmware
  1112. * file, then enable the patch upon success, otherwise just
  1113. * disable the manufacturer mode, for example patch activation
  1114. * is not required when the default firmware patch file is used
  1115. * because there are no patch data to load.
  1116. */
  1117. if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
  1118. *disable_patch = 0;
  1119. cmd_param = *fw_ptr;
  1120. *fw_ptr += cmd->plen;
  1121. remain -= cmd->plen;
  1122. /* This reads the expected events when the above command is sent to the
  1123. * device. Some vendor commands expects more than one events, for
  1124. * example command status event followed by vendor specific event.
  1125. * For this case, it only keeps the last expected event. so the command
  1126. * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
  1127. * last expected event.
  1128. */
  1129. while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
  1130. (*fw_ptr)++;
  1131. remain--;
  1132. evt = (struct hci_event_hdr *)(*fw_ptr);
  1133. *fw_ptr += sizeof(*evt);
  1134. remain -= sizeof(*evt);
  1135. if (remain < evt->plen) {
  1136. BT_ERR("%s Intel fw corrupted: invalid evt len",
  1137. hdev->name);
  1138. return -EFAULT;
  1139. }
  1140. evt_param = *fw_ptr;
  1141. *fw_ptr += evt->plen;
  1142. remain -= evt->plen;
  1143. }
  1144. /* Every HCI commands in the firmware file has its correspond event.
  1145. * If event is not found or remain is smaller than zero, the firmware
  1146. * file is corrupted.
  1147. */
  1148. if (!evt || !evt_param || remain < 0) {
  1149. BT_ERR("%s Intel fw corrupted: invalid evt read", hdev->name);
  1150. return -EFAULT;
  1151. }
  1152. skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
  1153. cmd_param, evt->evt, HCI_INIT_TIMEOUT);
  1154. if (IS_ERR(skb)) {
  1155. BT_ERR("%s sending Intel patch command (0x%4.4x) failed (%ld)",
  1156. hdev->name, cmd->opcode, PTR_ERR(skb));
  1157. return PTR_ERR(skb);
  1158. }
  1159. /* It ensures that the returned event matches the event data read from
  1160. * the firmware file. At fist, it checks the length and then
  1161. * the contents of the event.
  1162. */
  1163. if (skb->len != evt->plen) {
  1164. BT_ERR("%s mismatch event length (opcode 0x%4.4x)", hdev->name,
  1165. le16_to_cpu(cmd->opcode));
  1166. kfree_skb(skb);
  1167. return -EFAULT;
  1168. }
  1169. if (memcmp(skb->data, evt_param, evt->plen)) {
  1170. BT_ERR("%s mismatch event parameter (opcode 0x%4.4x)",
  1171. hdev->name, le16_to_cpu(cmd->opcode));
  1172. kfree_skb(skb);
  1173. return -EFAULT;
  1174. }
  1175. kfree_skb(skb);
  1176. return 0;
  1177. }
  1178. static int btusb_setup_intel(struct hci_dev *hdev)
  1179. {
  1180. struct sk_buff *skb;
  1181. const struct firmware *fw;
  1182. const u8 *fw_ptr;
  1183. int disable_patch;
  1184. struct intel_version *ver;
  1185. const u8 mfg_enable[] = { 0x01, 0x00 };
  1186. const u8 mfg_disable[] = { 0x00, 0x00 };
  1187. const u8 mfg_reset_deactivate[] = { 0x00, 0x01 };
  1188. const u8 mfg_reset_activate[] = { 0x00, 0x02 };
  1189. BT_DBG("%s", hdev->name);
  1190. /* The controller has a bug with the first HCI command sent to it
  1191. * returning number of completed commands as zero. This would stall the
  1192. * command processing in the Bluetooth core.
  1193. *
  1194. * As a workaround, send HCI Reset command first which will reset the
  1195. * number of completed commands and allow normal command processing
  1196. * from now on.
  1197. */
  1198. skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
  1199. if (IS_ERR(skb)) {
  1200. BT_ERR("%s sending initial HCI reset command failed (%ld)",
  1201. hdev->name, PTR_ERR(skb));
  1202. return PTR_ERR(skb);
  1203. }
  1204. kfree_skb(skb);
  1205. /* Read Intel specific controller version first to allow selection of
  1206. * which firmware file to load.
  1207. *
  1208. * The returned information are hardware variant and revision plus
  1209. * firmware variant, revision and build number.
  1210. */
  1211. skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
  1212. if (IS_ERR(skb)) {
  1213. BT_ERR("%s reading Intel fw version command failed (%ld)",
  1214. hdev->name, PTR_ERR(skb));
  1215. return PTR_ERR(skb);
  1216. }
  1217. if (skb->len != sizeof(*ver)) {
  1218. BT_ERR("%s Intel version event length mismatch", hdev->name);
  1219. kfree_skb(skb);
  1220. return -EIO;
  1221. }
  1222. ver = (struct intel_version *)skb->data;
  1223. BT_INFO("%s: read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
  1224. hdev->name, ver->hw_platform, ver->hw_variant,
  1225. ver->hw_revision, ver->fw_variant, ver->fw_revision,
  1226. ver->fw_build_num, ver->fw_build_ww, ver->fw_build_yy,
  1227. ver->fw_patch_num);
  1228. /* fw_patch_num indicates the version of patch the device currently
  1229. * have. If there is no patch data in the device, it is always 0x00.
  1230. * So, if it is other than 0x00, no need to patch the deivce again.
  1231. */
  1232. if (ver->fw_patch_num) {
  1233. BT_INFO("%s: Intel device is already patched. patch num: %02x",
  1234. hdev->name, ver->fw_patch_num);
  1235. kfree_skb(skb);
  1236. btintel_check_bdaddr(hdev);
  1237. return 0;
  1238. }
  1239. /* Opens the firmware patch file based on the firmware version read
  1240. * from the controller. If it fails to open the matching firmware
  1241. * patch file, it tries to open the default firmware patch file.
  1242. * If no patch file is found, allow the device to operate without
  1243. * a patch.
  1244. */
  1245. fw = btusb_setup_intel_get_fw(hdev, ver);
  1246. if (!fw) {
  1247. kfree_skb(skb);
  1248. btintel_check_bdaddr(hdev);
  1249. return 0;
  1250. }
  1251. fw_ptr = fw->data;
  1252. kfree_skb(skb);
  1253. /* This Intel specific command enables the manufacturer mode of the
  1254. * controller.
  1255. *
  1256. * Only while this mode is enabled, the driver can download the
  1257. * firmware patch data and configuration parameters.
  1258. */
  1259. skb = __hci_cmd_sync(hdev, 0xfc11, 2, mfg_enable, HCI_INIT_TIMEOUT);
  1260. if (IS_ERR(skb)) {
  1261. BT_ERR("%s entering Intel manufacturer mode failed (%ld)",
  1262. hdev->name, PTR_ERR(skb));
  1263. release_firmware(fw);
  1264. return PTR_ERR(skb);
  1265. }
  1266. kfree_skb(skb);
  1267. disable_patch = 1;
  1268. /* The firmware data file consists of list of Intel specific HCI
  1269. * commands and its expected events. The first byte indicates the
  1270. * type of the message, either HCI command or HCI event.
  1271. *
  1272. * It reads the command and its expected event from the firmware file,
  1273. * and send to the controller. Once __hci_cmd_sync_ev() returns,
  1274. * the returned event is compared with the event read from the firmware
  1275. * file and it will continue until all the messages are downloaded to
  1276. * the controller.
  1277. *
  1278. * Once the firmware patching is completed successfully,
  1279. * the manufacturer mode is disabled with reset and activating the
  1280. * downloaded patch.
  1281. *
  1282. * If the firmware patching fails, the manufacturer mode is
  1283. * disabled with reset and deactivating the patch.
  1284. *
  1285. * If the default patch file is used, no reset is done when disabling
  1286. * the manufacturer.
  1287. */
  1288. while (fw->size > fw_ptr - fw->data) {
  1289. int ret;
  1290. ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
  1291. &disable_patch);
  1292. if (ret < 0)
  1293. goto exit_mfg_deactivate;
  1294. }
  1295. release_firmware(fw);
  1296. if (disable_patch)
  1297. goto exit_mfg_disable;
  1298. /* Patching completed successfully and disable the manufacturer mode
  1299. * with reset and activate the downloaded firmware patches.
  1300. */
  1301. skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_activate),
  1302. mfg_reset_activate, HCI_INIT_TIMEOUT);
  1303. if (IS_ERR(skb)) {
  1304. BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
  1305. hdev->name, PTR_ERR(skb));
  1306. return PTR_ERR(skb);
  1307. }
  1308. kfree_skb(skb);
  1309. BT_INFO("%s: Intel Bluetooth firmware patch completed and activated",
  1310. hdev->name);
  1311. btintel_check_bdaddr(hdev);
  1312. return 0;
  1313. exit_mfg_disable:
  1314. /* Disable the manufacturer mode without reset */
  1315. skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_disable), mfg_disable,
  1316. HCI_INIT_TIMEOUT);
  1317. if (IS_ERR(skb)) {
  1318. BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
  1319. hdev->name, PTR_ERR(skb));
  1320. return PTR_ERR(skb);
  1321. }
  1322. kfree_skb(skb);
  1323. BT_INFO("%s: Intel Bluetooth firmware patch completed", hdev->name);
  1324. btintel_check_bdaddr(hdev);
  1325. return 0;
  1326. exit_mfg_deactivate:
  1327. release_firmware(fw);
  1328. /* Patching failed. Disable the manufacturer mode with reset and
  1329. * deactivate the downloaded firmware patches.
  1330. */
  1331. skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_deactivate),
  1332. mfg_reset_deactivate, HCI_INIT_TIMEOUT);
  1333. if (IS_ERR(skb)) {
  1334. BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
  1335. hdev->name, PTR_ERR(skb));
  1336. return PTR_ERR(skb);
  1337. }
  1338. kfree_skb(skb);
  1339. BT_INFO("%s: Intel Bluetooth firmware patch completed and deactivated",
  1340. hdev->name);
  1341. btintel_check_bdaddr(hdev);
  1342. return 0;
  1343. }
  1344. static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
  1345. {
  1346. struct sk_buff *skb;
  1347. struct hci_event_hdr *hdr;
  1348. struct hci_ev_cmd_complete *evt;
  1349. skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_ATOMIC);
  1350. if (!skb)
  1351. return -ENOMEM;
  1352. hdr = (struct hci_event_hdr *)skb_put(skb, sizeof(*hdr));
  1353. hdr->evt = HCI_EV_CMD_COMPLETE;
  1354. hdr->plen = sizeof(*evt) + 1;
  1355. evt = (struct hci_ev_cmd_complete *)skb_put(skb, sizeof(*evt));
  1356. evt->ncmd = 0x01;
  1357. evt->opcode = cpu_to_le16(opcode);
  1358. *skb_put(skb, 1) = 0x00;
  1359. bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
  1360. return hci_recv_frame(hdev, skb);
  1361. }
  1362. static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
  1363. int count)
  1364. {
  1365. /* When the device is in bootloader mode, then it can send
  1366. * events via the bulk endpoint. These events are treated the
  1367. * same way as the ones received from the interrupt endpoint.
  1368. */
  1369. if (test_bit(BTUSB_BOOTLOADER, &data->flags))
  1370. return btusb_recv_intr(data, buffer, count);
  1371. return btusb_recv_bulk(data, buffer, count);
  1372. }
  1373. static void btusb_intel_bootup(struct btusb_data *data, const void *ptr,
  1374. unsigned int len)
  1375. {
  1376. const struct intel_bootup *evt = ptr;
  1377. if (len != sizeof(*evt))
  1378. return;
  1379. if (test_and_clear_bit(BTUSB_BOOTING, &data->flags)) {
  1380. smp_mb__after_atomic();
  1381. wake_up_bit(&data->flags, BTUSB_BOOTING);
  1382. }
  1383. }
  1384. static void btusb_intel_secure_send_result(struct btusb_data *data,
  1385. const void *ptr, unsigned int len)
  1386. {
  1387. const struct intel_secure_send_result *evt = ptr;
  1388. if (len != sizeof(*evt))
  1389. return;
  1390. if (evt->result)
  1391. set_bit(BTUSB_FIRMWARE_FAILED, &data->flags);
  1392. if (test_and_clear_bit(BTUSB_DOWNLOADING, &data->flags) &&
  1393. test_bit(BTUSB_FIRMWARE_LOADED, &data->flags)) {
  1394. smp_mb__after_atomic();
  1395. wake_up_bit(&data->flags, BTUSB_DOWNLOADING);
  1396. }
  1397. }
  1398. static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
  1399. {
  1400. struct btusb_data *data = hci_get_drvdata(hdev);
  1401. if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
  1402. struct hci_event_hdr *hdr = (void *)skb->data;
  1403. if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
  1404. hdr->plen > 0) {
  1405. const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
  1406. unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
  1407. switch (skb->data[2]) {
  1408. case 0x02:
  1409. /* When switching to the operational firmware
  1410. * the device sends a vendor specific event
  1411. * indicating that the bootup completed.
  1412. */
  1413. btusb_intel_bootup(data, ptr, len);
  1414. break;
  1415. case 0x06:
  1416. /* When the firmware loading completes the
  1417. * device sends out a vendor specific event
  1418. * indicating the result of the firmware
  1419. * loading.
  1420. */
  1421. btusb_intel_secure_send_result(data, ptr, len);
  1422. break;
  1423. }
  1424. }
  1425. }
  1426. return hci_recv_frame(hdev, skb);
  1427. }
  1428. static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
  1429. {
  1430. struct btusb_data *data = hci_get_drvdata(hdev);
  1431. struct urb *urb;
  1432. BT_DBG("%s", hdev->name);
  1433. if (!test_bit(HCI_RUNNING, &hdev->flags))
  1434. return -EBUSY;
  1435. switch (bt_cb(skb)->pkt_type) {
  1436. case HCI_COMMAND_PKT:
  1437. if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
  1438. struct hci_command_hdr *cmd = (void *)skb->data;
  1439. __u16 opcode = le16_to_cpu(cmd->opcode);
  1440. /* When in bootloader mode and the command 0xfc09
  1441. * is received, it needs to be send down the
  1442. * bulk endpoint. So allocate a bulk URB instead.
  1443. */
  1444. if (opcode == 0xfc09)
  1445. urb = alloc_bulk_urb(hdev, skb);
  1446. else
  1447. urb = alloc_ctrl_urb(hdev, skb);
  1448. /* When the 0xfc01 command is issued to boot into
  1449. * the operational firmware, it will actually not
  1450. * send a command complete event. To keep the flow
  1451. * control working inject that event here.
  1452. */
  1453. if (opcode == 0xfc01)
  1454. inject_cmd_complete(hdev, opcode);
  1455. } else {
  1456. urb = alloc_ctrl_urb(hdev, skb);
  1457. }
  1458. if (IS_ERR(urb))
  1459. return PTR_ERR(urb);
  1460. hdev->stat.cmd_tx++;
  1461. return submit_or_queue_tx_urb(hdev, urb);
  1462. case HCI_ACLDATA_PKT:
  1463. urb = alloc_bulk_urb(hdev, skb);
  1464. if (IS_ERR(urb))
  1465. return PTR_ERR(urb);
  1466. hdev->stat.acl_tx++;
  1467. return submit_or_queue_tx_urb(hdev, urb);
  1468. case HCI_SCODATA_PKT:
  1469. if (hci_conn_num(hdev, SCO_LINK) < 1)
  1470. return -ENODEV;
  1471. urb = alloc_isoc_urb(hdev, skb);
  1472. if (IS_ERR(urb))
  1473. return PTR_ERR(urb);
  1474. hdev->stat.sco_tx++;
  1475. return submit_tx_urb(hdev, urb);
  1476. }
  1477. return -EILSEQ;
  1478. }
  1479. static void btusb_intel_version_info(struct hci_dev *hdev,
  1480. struct intel_version *ver)
  1481. {
  1482. const char *variant;
  1483. switch (ver->fw_variant) {
  1484. case 0x06:
  1485. variant = "Bootloader";
  1486. break;
  1487. case 0x23:
  1488. variant = "Firmware";
  1489. break;
  1490. default:
  1491. return;
  1492. }
  1493. BT_INFO("%s: %s revision %u.%u build %u week %u %u", hdev->name,
  1494. variant, ver->fw_revision >> 4, ver->fw_revision & 0x0f,
  1495. ver->fw_build_num, ver->fw_build_ww, 2000 + ver->fw_build_yy);
  1496. }
  1497. static int btusb_setup_intel_new(struct hci_dev *hdev)
  1498. {
  1499. static const u8 reset_param[] = { 0x00, 0x01, 0x00, 0x01,
  1500. 0x00, 0x08, 0x04, 0x00 };
  1501. struct btusb_data *data = hci_get_drvdata(hdev);
  1502. struct sk_buff *skb;
  1503. struct intel_version *ver;
  1504. struct intel_boot_params *params;
  1505. const struct firmware *fw;
  1506. const u8 *fw_ptr;
  1507. u32 frag_len;
  1508. char fwname[64];
  1509. ktime_t calltime, delta, rettime;
  1510. unsigned long long duration;
  1511. int err;
  1512. BT_DBG("%s", hdev->name);
  1513. calltime = ktime_get();
  1514. /* Read the Intel version information to determine if the device
  1515. * is in bootloader mode or if it already has operational firmware
  1516. * loaded.
  1517. */
  1518. skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
  1519. if (IS_ERR(skb)) {
  1520. BT_ERR("%s: Reading Intel version information failed (%ld)",
  1521. hdev->name, PTR_ERR(skb));
  1522. return PTR_ERR(skb);
  1523. }
  1524. if (skb->len != sizeof(*ver)) {
  1525. BT_ERR("%s: Intel version event size mismatch", hdev->name);
  1526. kfree_skb(skb);
  1527. return -EILSEQ;
  1528. }
  1529. ver = (struct intel_version *)skb->data;
  1530. /* The hardware platform number has a fixed value of 0x37 and
  1531. * for now only accept this single value.
  1532. */
  1533. if (ver->hw_platform != 0x37) {
  1534. BT_ERR("%s: Unsupported Intel hardware platform (%u)",
  1535. hdev->name, ver->hw_platform);
  1536. kfree_skb(skb);
  1537. return -EINVAL;
  1538. }
  1539. /* At the moment only the hardware variant iBT 3.0 (LnP/SfP) is
  1540. * supported by this firmware loading method. This check has been
  1541. * put in place to ensure correct forward compatibility options
  1542. * when newer hardware variants come along.
  1543. */
  1544. if (ver->hw_variant != 0x0b) {
  1545. BT_ERR("%s: Unsupported Intel hardware variant (%u)",
  1546. hdev->name, ver->hw_variant);
  1547. kfree_skb(skb);
  1548. return -EINVAL;
  1549. }
  1550. btusb_intel_version_info(hdev, ver);
  1551. /* The firmware variant determines if the device is in bootloader
  1552. * mode or is running operational firmware. The value 0x06 identifies
  1553. * the bootloader and the value 0x23 identifies the operational
  1554. * firmware.
  1555. *
  1556. * When the operational firmware is already present, then only
  1557. * the check for valid Bluetooth device address is needed. This
  1558. * determines if the device will be added as configured or
  1559. * unconfigured controller.
  1560. *
  1561. * It is not possible to use the Secure Boot Parameters in this
  1562. * case since that command is only available in bootloader mode.
  1563. */
  1564. if (ver->fw_variant == 0x23) {
  1565. kfree_skb(skb);
  1566. clear_bit(BTUSB_BOOTLOADER, &data->flags);
  1567. btintel_check_bdaddr(hdev);
  1568. return 0;
  1569. }
  1570. /* If the device is not in bootloader mode, then the only possible
  1571. * choice is to return an error and abort the device initialization.
  1572. */
  1573. if (ver->fw_variant != 0x06) {
  1574. BT_ERR("%s: Unsupported Intel firmware variant (%u)",
  1575. hdev->name, ver->fw_variant);
  1576. kfree_skb(skb);
  1577. return -ENODEV;
  1578. }
  1579. kfree_skb(skb);
  1580. /* Read the secure boot parameters to identify the operating
  1581. * details of the bootloader.
  1582. */
  1583. skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
  1584. if (IS_ERR(skb)) {
  1585. BT_ERR("%s: Reading Intel boot parameters failed (%ld)",
  1586. hdev->name, PTR_ERR(skb));
  1587. return PTR_ERR(skb);
  1588. }
  1589. if (skb->len != sizeof(*params)) {
  1590. BT_ERR("%s: Intel boot parameters size mismatch", hdev->name);
  1591. kfree_skb(skb);
  1592. return -EILSEQ;
  1593. }
  1594. params = (struct intel_boot_params *)skb->data;
  1595. BT_INFO("%s: Device revision is %u", hdev->name,
  1596. le16_to_cpu(params->dev_revid));
  1597. BT_INFO("%s: Secure boot is %s", hdev->name,
  1598. params->secure_boot ? "enabled" : "disabled");
  1599. BT_INFO("%s: Minimum firmware build %u week %u %u", hdev->name,
  1600. params->min_fw_build_nn, params->min_fw_build_cw,
  1601. 2000 + params->min_fw_build_yy);
  1602. /* It is required that every single firmware fragment is acknowledged
  1603. * with a command complete event. If the boot parameters indicate
  1604. * that this bootloader does not send them, then abort the setup.
  1605. */
  1606. if (params->limited_cce != 0x00) {
  1607. BT_ERR("%s: Unsupported Intel firmware loading method (%u)",
  1608. hdev->name, params->limited_cce);
  1609. kfree_skb(skb);
  1610. return -EINVAL;
  1611. }
  1612. /* If the OTP has no valid Bluetooth device address, then there will
  1613. * also be no valid address for the operational firmware.
  1614. */
  1615. if (!bacmp(&params->otp_bdaddr, BDADDR_ANY)) {
  1616. BT_INFO("%s: No device address configured", hdev->name);
  1617. set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
  1618. }
  1619. /* With this Intel bootloader only the hardware variant and device
  1620. * revision information are used to select the right firmware.
  1621. *
  1622. * Currently this bootloader support is limited to hardware variant
  1623. * iBT 3.0 (LnP/SfP) which is identified by the value 11 (0x0b).
  1624. */
  1625. snprintf(fwname, sizeof(fwname), "intel/ibt-11-%u.sfi",
  1626. le16_to_cpu(params->dev_revid));
  1627. err = request_firmware(&fw, fwname, &hdev->dev);
  1628. if (err < 0) {
  1629. BT_ERR("%s: Failed to load Intel firmware file (%d)",
  1630. hdev->name, err);
  1631. kfree_skb(skb);
  1632. return err;
  1633. }
  1634. BT_INFO("%s: Found device firmware: %s", hdev->name, fwname);
  1635. /* Save the DDC file name for later use to apply once the firmware
  1636. * downloading is done.
  1637. */
  1638. snprintf(fwname, sizeof(fwname), "intel/ibt-11-%u.ddc",
  1639. le16_to_cpu(params->dev_revid));
  1640. kfree_skb(skb);
  1641. if (fw->size < 644) {
  1642. BT_ERR("%s: Invalid size of firmware file (%zu)",
  1643. hdev->name, fw->size);
  1644. err = -EBADF;
  1645. goto done;
  1646. }
  1647. set_bit(BTUSB_DOWNLOADING, &data->flags);
  1648. /* Start the firmware download transaction with the Init fragment
  1649. * represented by the 128 bytes of CSS header.
  1650. */
  1651. err = btintel_secure_send(hdev, 0x00, 128, fw->data);
  1652. if (err < 0) {
  1653. BT_ERR("%s: Failed to send firmware header (%d)",
  1654. hdev->name, err);
  1655. goto done;
  1656. }
  1657. /* Send the 256 bytes of public key information from the firmware
  1658. * as the PKey fragment.
  1659. */
  1660. err = btintel_secure_send(hdev, 0x03, 256, fw->data + 128);
  1661. if (err < 0) {
  1662. BT_ERR("%s: Failed to send firmware public key (%d)",
  1663. hdev->name, err);
  1664. goto done;
  1665. }
  1666. /* Send the 256 bytes of signature information from the firmware
  1667. * as the Sign fragment.
  1668. */
  1669. err = btintel_secure_send(hdev, 0x02, 256, fw->data + 388);
  1670. if (err < 0) {
  1671. BT_ERR("%s: Failed to send firmware signature (%d)",
  1672. hdev->name, err);
  1673. goto done;
  1674. }
  1675. fw_ptr = fw->data + 644;
  1676. frag_len = 0;
  1677. while (fw_ptr - fw->data < fw->size) {
  1678. struct hci_command_hdr *cmd = (void *)(fw_ptr + frag_len);
  1679. frag_len += sizeof(*cmd) + cmd->plen;
  1680. /* The paramter length of the secure send command requires
  1681. * a 4 byte alignment. It happens so that the firmware file
  1682. * contains proper Intel_NOP commands to align the fragments
  1683. * as needed.
  1684. *
  1685. * Send set of commands with 4 byte alignment from the
  1686. * firmware data buffer as a single Data fragement.
  1687. */
  1688. if (!(frag_len % 4)) {
  1689. err = btintel_secure_send(hdev, 0x01, frag_len, fw_ptr);
  1690. if (err < 0) {
  1691. BT_ERR("%s: Failed to send firmware data (%d)",
  1692. hdev->name, err);
  1693. goto done;
  1694. }
  1695. fw_ptr += frag_len;
  1696. frag_len = 0;
  1697. }
  1698. }
  1699. set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
  1700. BT_INFO("%s: Waiting for firmware download to complete", hdev->name);
  1701. /* Before switching the device into operational mode and with that
  1702. * booting the loaded firmware, wait for the bootloader notification
  1703. * that all fragments have been successfully received.
  1704. *
  1705. * When the event processing receives the notification, then the
  1706. * BTUSB_DOWNLOADING flag will be cleared.
  1707. *
  1708. * The firmware loading should not take longer than 5 seconds
  1709. * and thus just timeout if that happens and fail the setup
  1710. * of this device.
  1711. */
  1712. err = wait_on_bit_timeout(&data->flags, BTUSB_DOWNLOADING,
  1713. TASK_INTERRUPTIBLE,
  1714. msecs_to_jiffies(5000));
  1715. if (err == 1) {
  1716. BT_ERR("%s: Firmware loading interrupted", hdev->name);
  1717. err = -EINTR;
  1718. goto done;
  1719. }
  1720. if (err) {
  1721. BT_ERR("%s: Firmware loading timeout", hdev->name);
  1722. err = -ETIMEDOUT;
  1723. goto done;
  1724. }
  1725. if (test_bit(BTUSB_FIRMWARE_FAILED, &data->flags)) {
  1726. BT_ERR("%s: Firmware loading failed", hdev->name);
  1727. err = -ENOEXEC;
  1728. goto done;
  1729. }
  1730. rettime = ktime_get();
  1731. delta = ktime_sub(rettime, calltime);
  1732. duration = (unsigned long long) ktime_to_ns(delta) >> 10;
  1733. BT_INFO("%s: Firmware loaded in %llu usecs", hdev->name, duration);
  1734. done:
  1735. release_firmware(fw);
  1736. if (err < 0)
  1737. return err;
  1738. calltime = ktime_get();
  1739. set_bit(BTUSB_BOOTING, &data->flags);
  1740. skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(reset_param), reset_param,
  1741. HCI_INIT_TIMEOUT);
  1742. if (IS_ERR(skb))
  1743. return PTR_ERR(skb);
  1744. kfree_skb(skb);
  1745. /* The bootloader will not indicate when the device is ready. This
  1746. * is done by the operational firmware sending bootup notification.
  1747. *
  1748. * Booting into operational firmware should not take longer than
  1749. * 1 second. However if that happens, then just fail the setup
  1750. * since something went wrong.
  1751. */
  1752. BT_INFO("%s: Waiting for device to boot", hdev->name);
  1753. err = wait_on_bit_timeout(&data->flags, BTUSB_BOOTING,
  1754. TASK_INTERRUPTIBLE,
  1755. msecs_to_jiffies(1000));
  1756. if (err == 1) {
  1757. BT_ERR("%s: Device boot interrupted", hdev->name);
  1758. return -EINTR;
  1759. }
  1760. if (err) {
  1761. BT_ERR("%s: Device boot timeout", hdev->name);
  1762. return -ETIMEDOUT;
  1763. }
  1764. rettime = ktime_get();
  1765. delta = ktime_sub(rettime, calltime);
  1766. duration = (unsigned long long) ktime_to_ns(delta) >> 10;
  1767. BT_INFO("%s: Device booted in %llu usecs", hdev->name, duration);
  1768. clear_bit(BTUSB_BOOTLOADER, &data->flags);
  1769. /* Once the device is running in operational mode, it needs to apply
  1770. * the device configuration (DDC) parameters.
  1771. *
  1772. * The device can work without DDC parameters, so even if it fails
  1773. * to load the file, no need to fail the setup.
  1774. */
  1775. err = request_firmware_direct(&fw, fwname, &hdev->dev);
  1776. if (err < 0)
  1777. return 0;
  1778. BT_INFO("%s: Found Intel DDC parameters: %s", hdev->name, fwname);
  1779. fw_ptr = fw->data;
  1780. /* DDC file contains one or more DDC structure which has
  1781. * Length (1 byte), DDC ID (2 bytes), and DDC value (Length - 2).
  1782. */
  1783. while (fw->size > fw_ptr - fw->data) {
  1784. u8 cmd_plen = fw_ptr[0] + sizeof(u8);
  1785. skb = __hci_cmd_sync(hdev, 0xfc8b, cmd_plen, fw_ptr,
  1786. HCI_INIT_TIMEOUT);
  1787. if (IS_ERR(skb)) {
  1788. BT_ERR("%s: Failed to send Intel_Write_DDC (%ld)",
  1789. hdev->name, PTR_ERR(skb));
  1790. release_firmware(fw);
  1791. return PTR_ERR(skb);
  1792. }
  1793. fw_ptr += cmd_plen;
  1794. kfree_skb(skb);
  1795. }
  1796. release_firmware(fw);
  1797. BT_INFO("%s: Applying Intel DDC parameters completed", hdev->name);
  1798. return 0;
  1799. }
  1800. static int btusb_shutdown_intel(struct hci_dev *hdev)
  1801. {
  1802. struct sk_buff *skb;
  1803. long ret;
  1804. /* Some platforms have an issue with BT LED when the interface is
  1805. * down or BT radio is turned off, which takes 5 seconds to BT LED
  1806. * goes off. This command turns off the BT LED immediately.
  1807. */
  1808. skb = __hci_cmd_sync(hdev, 0xfc3f, 0, NULL, HCI_INIT_TIMEOUT);
  1809. if (IS_ERR(skb)) {
  1810. ret = PTR_ERR(skb);
  1811. BT_ERR("%s: turning off Intel device LED failed (%ld)",
  1812. hdev->name, ret);
  1813. return ret;
  1814. }
  1815. kfree_skb(skb);
  1816. return 0;
  1817. }
  1818. static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
  1819. const bdaddr_t *bdaddr)
  1820. {
  1821. struct sk_buff *skb;
  1822. u8 buf[8];
  1823. long ret;
  1824. buf[0] = 0xfe;
  1825. buf[1] = sizeof(bdaddr_t);
  1826. memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
  1827. skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
  1828. if (IS_ERR(skb)) {
  1829. ret = PTR_ERR(skb);
  1830. BT_ERR("%s: changing Marvell device address failed (%ld)",
  1831. hdev->name, ret);
  1832. return ret;
  1833. }
  1834. kfree_skb(skb);
  1835. return 0;
  1836. }
  1837. static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
  1838. const bdaddr_t *bdaddr)
  1839. {
  1840. struct sk_buff *skb;
  1841. u8 buf[10];
  1842. long ret;
  1843. buf[0] = 0x01;
  1844. buf[1] = 0x01;
  1845. buf[2] = 0x00;
  1846. buf[3] = sizeof(bdaddr_t);
  1847. memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
  1848. skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
  1849. if (IS_ERR(skb)) {
  1850. ret = PTR_ERR(skb);
  1851. BT_ERR("%s: Change address command failed (%ld)",
  1852. hdev->name, ret);
  1853. return ret;
  1854. }
  1855. kfree_skb(skb);
  1856. return 0;
  1857. }
  1858. #define QCA_DFU_PACKET_LEN 4096
  1859. #define QCA_GET_TARGET_VERSION 0x09
  1860. #define QCA_CHECK_STATUS 0x05
  1861. #define QCA_DFU_DOWNLOAD 0x01
  1862. #define QCA_SYSCFG_UPDATED 0x40
  1863. #define QCA_PATCH_UPDATED 0x80
  1864. #define QCA_DFU_TIMEOUT 3000
  1865. struct qca_version {
  1866. __le32 rom_version;
  1867. __le32 patch_version;
  1868. __le32 ram_version;
  1869. __le32 ref_clock;
  1870. __u8 reserved[4];
  1871. } __packed;
  1872. struct qca_rampatch_version {
  1873. __le16 rom_version;
  1874. __le16 patch_version;
  1875. } __packed;
  1876. struct qca_device_info {
  1877. u32 rom_version;
  1878. u8 rampatch_hdr; /* length of header in rampatch */
  1879. u8 nvm_hdr; /* length of header in NVM */
  1880. u8 ver_offset; /* offset of version structure in rampatch */
  1881. };
  1882. static const struct qca_device_info qca_devices_table[] = {
  1883. { 0x00000100, 20, 4, 10 }, /* Rome 1.0 */
  1884. { 0x00000101, 20, 4, 10 }, /* Rome 1.1 */
  1885. { 0x00000200, 28, 4, 18 }, /* Rome 2.0 */
  1886. { 0x00000201, 28, 4, 18 }, /* Rome 2.1 */
  1887. { 0x00000300, 28, 4, 18 }, /* Rome 3.0 */
  1888. { 0x00000302, 28, 4, 18 }, /* Rome 3.2 */
  1889. };
  1890. static int btusb_qca_send_vendor_req(struct hci_dev *hdev, u8 request,
  1891. void *data, u16 size)
  1892. {
  1893. struct btusb_data *btdata = hci_get_drvdata(hdev);
  1894. struct usb_device *udev = btdata->udev;
  1895. int pipe, err;
  1896. u8 *buf;
  1897. buf = kmalloc(size, GFP_KERNEL);
  1898. if (!buf)
  1899. return -ENOMEM;
  1900. /* Found some of USB hosts have IOT issues with ours so that we should
  1901. * not wait until HCI layer is ready.
  1902. */
  1903. pipe = usb_rcvctrlpipe(udev, 0);
  1904. err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
  1905. 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
  1906. if (err < 0) {
  1907. BT_ERR("%s: Failed to access otp area (%d)", hdev->name, err);
  1908. goto done;
  1909. }
  1910. memcpy(data, buf, size);
  1911. done:
  1912. kfree(buf);
  1913. return err;
  1914. }
  1915. static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
  1916. const struct firmware *firmware,
  1917. size_t hdr_size)
  1918. {
  1919. struct btusb_data *btdata = hci_get_drvdata(hdev);
  1920. struct usb_device *udev = btdata->udev;
  1921. size_t count, size, sent = 0;
  1922. int pipe, len, err;
  1923. u8 *buf;
  1924. buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
  1925. if (!buf)
  1926. return -ENOMEM;
  1927. count = firmware->size;
  1928. size = min_t(size_t, count, hdr_size);
  1929. memcpy(buf, firmware->data, size);
  1930. /* USB patches should go down to controller through USB path
  1931. * because binary format fits to go down through USB channel.
  1932. * USB control path is for patching headers and USB bulk is for
  1933. * patch body.
  1934. */
  1935. pipe = usb_sndctrlpipe(udev, 0);
  1936. err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
  1937. 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
  1938. if (err < 0) {
  1939. BT_ERR("%s: Failed to send headers (%d)", hdev->name, err);
  1940. goto done;
  1941. }
  1942. sent += size;
  1943. count -= size;
  1944. while (count) {
  1945. size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
  1946. memcpy(buf, firmware->data + sent, size);
  1947. pipe = usb_sndbulkpipe(udev, 0x02);
  1948. err = usb_bulk_msg(udev, pipe, buf, size, &len,
  1949. QCA_DFU_TIMEOUT);
  1950. if (err < 0) {
  1951. BT_ERR("%s: Failed to send body at %zd of %zd (%d)",
  1952. hdev->name, sent, firmware->size, err);
  1953. break;
  1954. }
  1955. if (size != len) {
  1956. BT_ERR("%s: Failed to get bulk buffer", hdev->name);
  1957. err = -EILSEQ;
  1958. break;
  1959. }
  1960. sent += size;
  1961. count -= size;
  1962. }
  1963. done:
  1964. kfree(buf);
  1965. return err;
  1966. }
  1967. static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
  1968. struct qca_version *ver,
  1969. const struct qca_device_info *info)
  1970. {
  1971. struct qca_rampatch_version *rver;
  1972. const struct firmware *fw;
  1973. u32 ver_rom, ver_patch;
  1974. u16 rver_rom, rver_patch;
  1975. char fwname[64];
  1976. int err;
  1977. ver_rom = le32_to_cpu(ver->rom_version);
  1978. ver_patch = le32_to_cpu(ver->patch_version);
  1979. snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin", ver_rom);
  1980. err = request_firmware(&fw, fwname, &hdev->dev);
  1981. if (err) {
  1982. BT_ERR("%s: failed to request rampatch file: %s (%d)",
  1983. hdev->name, fwname, err);
  1984. return err;
  1985. }
  1986. BT_INFO("%s: using rampatch file: %s", hdev->name, fwname);
  1987. rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
  1988. rver_rom = le16_to_cpu(rver->rom_version);
  1989. rver_patch = le16_to_cpu(rver->patch_version);
  1990. BT_INFO("%s: QCA: patch rome 0x%x build 0x%x, firmware rome 0x%x "
  1991. "build 0x%x", hdev->name, rver_rom, rver_patch, ver_rom,
  1992. ver_patch);
  1993. if (rver_rom != ver_rom || rver_patch <= ver_patch) {
  1994. BT_ERR("%s: rampatch file version did not match with firmware",
  1995. hdev->name);
  1996. err = -EINVAL;
  1997. goto done;
  1998. }
  1999. err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
  2000. done:
  2001. release_firmware(fw);
  2002. return err;
  2003. }
  2004. static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
  2005. struct qca_version *ver,
  2006. const struct qca_device_info *info)
  2007. {
  2008. const struct firmware *fw;
  2009. char fwname[64];
  2010. int err;
  2011. snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x.bin",
  2012. le32_to_cpu(ver->rom_version));
  2013. err = request_firmware(&fw, fwname, &hdev->dev);
  2014. if (err) {
  2015. BT_ERR("%s: failed to request NVM file: %s (%d)",
  2016. hdev->name, fwname, err);
  2017. return err;
  2018. }
  2019. BT_INFO("%s: using NVM file: %s", hdev->name, fwname);
  2020. err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
  2021. release_firmware(fw);
  2022. return err;
  2023. }
  2024. static int btusb_setup_qca(struct hci_dev *hdev)
  2025. {
  2026. const struct qca_device_info *info = NULL;
  2027. struct qca_version ver;
  2028. u32 ver_rom;
  2029. u8 status;
  2030. int i, err;
  2031. err = btusb_qca_send_vendor_req(hdev, QCA_GET_TARGET_VERSION, &ver,
  2032. sizeof(ver));
  2033. if (err < 0)
  2034. return err;
  2035. ver_rom = le32_to_cpu(ver.rom_version);
  2036. for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
  2037. if (ver_rom == qca_devices_table[i].rom_version)
  2038. info = &qca_devices_table[i];
  2039. }
  2040. if (!info) {
  2041. BT_ERR("%s: don't support firmware rome 0x%x", hdev->name,
  2042. ver_rom);
  2043. return -ENODEV;
  2044. }
  2045. err = btusb_qca_send_vendor_req(hdev, QCA_CHECK_STATUS, &status,
  2046. sizeof(status));
  2047. if (err < 0)
  2048. return err;
  2049. if (!(status & QCA_PATCH_UPDATED)) {
  2050. err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
  2051. if (err < 0)
  2052. return err;
  2053. }
  2054. if (!(status & QCA_SYSCFG_UPDATED)) {
  2055. err = btusb_setup_qca_load_nvm(hdev, &ver, info);
  2056. if (err < 0)
  2057. return err;
  2058. }
  2059. return 0;
  2060. }
  2061. static int btusb_probe(struct usb_interface *intf,
  2062. const struct usb_device_id *id)
  2063. {
  2064. struct usb_endpoint_descriptor *ep_desc;
  2065. struct btusb_data *data;
  2066. struct hci_dev *hdev;
  2067. int i, err;
  2068. BT_DBG("intf %p id %p", intf, id);
  2069. /* interface numbers are hardcoded in the spec */
  2070. if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
  2071. return -ENODEV;
  2072. if (!id->driver_info) {
  2073. const struct usb_device_id *match;
  2074. match = usb_match_id(intf, blacklist_table);
  2075. if (match)
  2076. id = match;
  2077. }
  2078. if (id->driver_info == BTUSB_IGNORE)
  2079. return -ENODEV;
  2080. if (id->driver_info & BTUSB_ATH3012) {
  2081. struct usb_device *udev = interface_to_usbdev(intf);
  2082. /* Old firmware would otherwise let ath3k driver load
  2083. * patch and sysconfig files */
  2084. if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
  2085. return -ENODEV;
  2086. }
  2087. data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
  2088. if (!data)
  2089. return -ENOMEM;
  2090. for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
  2091. ep_desc = &intf->cur_altsetting->endpoint[i].desc;
  2092. if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
  2093. data->intr_ep = ep_desc;
  2094. continue;
  2095. }
  2096. if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
  2097. data->bulk_tx_ep = ep_desc;
  2098. continue;
  2099. }
  2100. if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
  2101. data->bulk_rx_ep = ep_desc;
  2102. continue;
  2103. }
  2104. }
  2105. if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
  2106. return -ENODEV;
  2107. if (id->driver_info & BTUSB_AMP) {
  2108. data->cmdreq_type = USB_TYPE_CLASS | 0x01;
  2109. data->cmdreq = 0x2b;
  2110. } else {
  2111. data->cmdreq_type = USB_TYPE_CLASS;
  2112. data->cmdreq = 0x00;
  2113. }
  2114. data->udev = interface_to_usbdev(intf);
  2115. data->intf = intf;
  2116. INIT_WORK(&data->work, btusb_work);
  2117. INIT_WORK(&data->waker, btusb_waker);
  2118. init_usb_anchor(&data->deferred);
  2119. init_usb_anchor(&data->tx_anchor);
  2120. spin_lock_init(&data->txlock);
  2121. init_usb_anchor(&data->intr_anchor);
  2122. init_usb_anchor(&data->bulk_anchor);
  2123. init_usb_anchor(&data->isoc_anchor);
  2124. spin_lock_init(&data->rxlock);
  2125. if (id->driver_info & BTUSB_INTEL_NEW) {
  2126. data->recv_event = btusb_recv_event_intel;
  2127. data->recv_bulk = btusb_recv_bulk_intel;
  2128. set_bit(BTUSB_BOOTLOADER, &data->flags);
  2129. } else {
  2130. data->recv_event = hci_recv_frame;
  2131. data->recv_bulk = btusb_recv_bulk;
  2132. }
  2133. hdev = hci_alloc_dev();
  2134. if (!hdev)
  2135. return -ENOMEM;
  2136. hdev->bus = HCI_USB;
  2137. hci_set_drvdata(hdev, data);
  2138. if (id->driver_info & BTUSB_AMP)
  2139. hdev->dev_type = HCI_AMP;
  2140. else
  2141. hdev->dev_type = HCI_BREDR;
  2142. data->hdev = hdev;
  2143. SET_HCIDEV_DEV(hdev, &intf->dev);
  2144. hdev->open = btusb_open;
  2145. hdev->close = btusb_close;
  2146. hdev->flush = btusb_flush;
  2147. hdev->send = btusb_send_frame;
  2148. hdev->notify = btusb_notify;
  2149. if (id->driver_info & BTUSB_BCM92035)
  2150. hdev->setup = btusb_setup_bcm92035;
  2151. #ifdef CONFIG_BT_HCIBTUSB_BCM
  2152. if (id->driver_info & BTUSB_BCM_PATCHRAM) {
  2153. hdev->setup = btbcm_setup_patchram;
  2154. hdev->set_bdaddr = btbcm_set_bdaddr;
  2155. }
  2156. if (id->driver_info & BTUSB_BCM_APPLE)
  2157. hdev->setup = btbcm_setup_apple;
  2158. #endif
  2159. if (id->driver_info & BTUSB_INTEL) {
  2160. hdev->setup = btusb_setup_intel;
  2161. hdev->shutdown = btusb_shutdown_intel;
  2162. hdev->set_bdaddr = btintel_set_bdaddr;
  2163. set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
  2164. set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
  2165. }
  2166. if (id->driver_info & BTUSB_INTEL_NEW) {
  2167. hdev->send = btusb_send_frame_intel;
  2168. hdev->setup = btusb_setup_intel_new;
  2169. hdev->hw_error = btintel_hw_error;
  2170. hdev->set_bdaddr = btintel_set_bdaddr;
  2171. set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
  2172. }
  2173. if (id->driver_info & BTUSB_MARVELL)
  2174. hdev->set_bdaddr = btusb_set_bdaddr_marvell;
  2175. if (id->driver_info & BTUSB_SWAVE) {
  2176. set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
  2177. set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
  2178. }
  2179. if (id->driver_info & BTUSB_INTEL_BOOT)
  2180. set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
  2181. if (id->driver_info & BTUSB_ATH3012) {
  2182. hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
  2183. set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
  2184. set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
  2185. }
  2186. if (id->driver_info & BTUSB_QCA_ROME) {
  2187. data->setup_on_usb = btusb_setup_qca;
  2188. hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
  2189. }
  2190. #ifdef CONFIG_BT_HCIBTUSB_RTL
  2191. if (id->driver_info & BTUSB_REALTEK) {
  2192. hdev->setup = btrtl_setup_realtek;
  2193. /* Realtek devices lose their updated firmware over suspend,
  2194. * but the USB hub doesn't notice any status change.
  2195. * Explicitly request a device reset on resume.
  2196. */
  2197. set_bit(BTUSB_RESET_RESUME, &data->flags);
  2198. }
  2199. #endif
  2200. if (id->driver_info & BTUSB_AMP) {
  2201. /* AMP controllers do not support SCO packets */
  2202. data->isoc = NULL;
  2203. } else {
  2204. /* Interface numbers are hardcoded in the specification */
  2205. data->isoc = usb_ifnum_to_if(data->udev, 1);
  2206. }
  2207. if (!reset)
  2208. set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
  2209. if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
  2210. if (!disable_scofix)
  2211. set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
  2212. }
  2213. if (id->driver_info & BTUSB_BROKEN_ISOC)
  2214. data->isoc = NULL;
  2215. if (id->driver_info & BTUSB_DIGIANSWER) {
  2216. data->cmdreq_type = USB_TYPE_VENDOR;
  2217. set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
  2218. }
  2219. if (id->driver_info & BTUSB_CSR) {
  2220. struct usb_device *udev = data->udev;
  2221. u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
  2222. /* Old firmware would otherwise execute USB reset */
  2223. if (bcdDevice < 0x117)
  2224. set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
  2225. /* Fake CSR devices with broken commands */
  2226. if (bcdDevice <= 0x100)
  2227. hdev->setup = btusb_setup_csr;
  2228. set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
  2229. }
  2230. if (id->driver_info & BTUSB_SNIFFER) {
  2231. struct usb_device *udev = data->udev;
  2232. /* New sniffer firmware has crippled HCI interface */
  2233. if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
  2234. set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
  2235. }
  2236. if (id->driver_info & BTUSB_INTEL_BOOT) {
  2237. /* A bug in the bootloader causes that interrupt interface is
  2238. * only enabled after receiving SetInterface(0, AltSetting=0).
  2239. */
  2240. err = usb_set_interface(data->udev, 0, 0);
  2241. if (err < 0) {
  2242. BT_ERR("failed to set interface 0, alt 0 %d", err);
  2243. hci_free_dev(hdev);
  2244. return err;
  2245. }
  2246. }
  2247. if (data->isoc) {
  2248. err = usb_driver_claim_interface(&btusb_driver,
  2249. data->isoc, data);
  2250. if (err < 0) {
  2251. hci_free_dev(hdev);
  2252. return err;
  2253. }
  2254. }
  2255. err = hci_register_dev(hdev);
  2256. if (err < 0) {
  2257. hci_free_dev(hdev);
  2258. return err;
  2259. }
  2260. usb_set_intfdata(intf, data);
  2261. return 0;
  2262. }
  2263. static void btusb_disconnect(struct usb_interface *intf)
  2264. {
  2265. struct btusb_data *data = usb_get_intfdata(intf);
  2266. struct hci_dev *hdev;
  2267. BT_DBG("intf %p", intf);
  2268. if (!data)
  2269. return;
  2270. hdev = data->hdev;
  2271. usb_set_intfdata(data->intf, NULL);
  2272. if (data->isoc)
  2273. usb_set_intfdata(data->isoc, NULL);
  2274. hci_unregister_dev(hdev);
  2275. if (intf == data->isoc)
  2276. usb_driver_release_interface(&btusb_driver, data->intf);
  2277. else if (data->isoc)
  2278. usb_driver_release_interface(&btusb_driver, data->isoc);
  2279. hci_free_dev(hdev);
  2280. }
  2281. #ifdef CONFIG_PM
  2282. static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
  2283. {
  2284. struct btusb_data *data = usb_get_intfdata(intf);
  2285. BT_DBG("intf %p", intf);
  2286. if (data->suspend_count++)
  2287. return 0;
  2288. spin_lock_irq(&data->txlock);
  2289. if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
  2290. set_bit(BTUSB_SUSPENDING, &data->flags);
  2291. spin_unlock_irq(&data->txlock);
  2292. } else {
  2293. spin_unlock_irq(&data->txlock);
  2294. data->suspend_count--;
  2295. return -EBUSY;
  2296. }
  2297. cancel_work_sync(&data->work);
  2298. btusb_stop_traffic(data);
  2299. usb_kill_anchored_urbs(&data->tx_anchor);
  2300. /* Optionally request a device reset on resume, but only when
  2301. * wakeups are disabled. If wakeups are enabled we assume the
  2302. * device will stay powered up throughout suspend.
  2303. */
  2304. if (test_bit(BTUSB_RESET_RESUME, &data->flags) &&
  2305. !device_may_wakeup(&data->udev->dev))
  2306. data->udev->reset_resume = 1;
  2307. return 0;
  2308. }
  2309. static void play_deferred(struct btusb_data *data)
  2310. {
  2311. struct urb *urb;
  2312. int err;
  2313. while ((urb = usb_get_from_anchor(&data->deferred))) {
  2314. err = usb_submit_urb(urb, GFP_ATOMIC);
  2315. if (err < 0)
  2316. break;
  2317. data->tx_in_flight++;
  2318. }
  2319. usb_scuttle_anchored_urbs(&data->deferred);
  2320. }
  2321. static int btusb_resume(struct usb_interface *intf)
  2322. {
  2323. struct btusb_data *data = usb_get_intfdata(intf);
  2324. struct hci_dev *hdev = data->hdev;
  2325. int err = 0;
  2326. BT_DBG("intf %p", intf);
  2327. if (--data->suspend_count)
  2328. return 0;
  2329. if (!test_bit(HCI_RUNNING, &hdev->flags))
  2330. goto done;
  2331. if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
  2332. err = btusb_submit_intr_urb(hdev, GFP_NOIO);
  2333. if (err < 0) {
  2334. clear_bit(BTUSB_INTR_RUNNING, &data->flags);
  2335. goto failed;
  2336. }
  2337. }
  2338. if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
  2339. err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
  2340. if (err < 0) {
  2341. clear_bit(BTUSB_BULK_RUNNING, &data->flags);
  2342. goto failed;
  2343. }
  2344. btusb_submit_bulk_urb(hdev, GFP_NOIO);
  2345. }
  2346. if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
  2347. if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
  2348. clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
  2349. else
  2350. btusb_submit_isoc_urb(hdev, GFP_NOIO);
  2351. }
  2352. spin_lock_irq(&data->txlock);
  2353. play_deferred(data);
  2354. clear_bit(BTUSB_SUSPENDING, &data->flags);
  2355. spin_unlock_irq(&data->txlock);
  2356. schedule_work(&data->work);
  2357. return 0;
  2358. failed:
  2359. usb_scuttle_anchored_urbs(&data->deferred);
  2360. done:
  2361. spin_lock_irq(&data->txlock);
  2362. clear_bit(BTUSB_SUSPENDING, &data->flags);
  2363. spin_unlock_irq(&data->txlock);
  2364. return err;
  2365. }
  2366. #endif
  2367. static struct usb_driver btusb_driver = {
  2368. .name = "btusb",
  2369. .probe = btusb_probe,
  2370. .disconnect = btusb_disconnect,
  2371. #ifdef CONFIG_PM
  2372. .suspend = btusb_suspend,
  2373. .resume = btusb_resume,
  2374. #endif
  2375. .id_table = btusb_table,
  2376. .supports_autosuspend = 1,
  2377. .disable_hub_initiated_lpm = 1,
  2378. };
  2379. module_usb_driver(btusb_driver);
  2380. module_param(disable_scofix, bool, 0644);
  2381. MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
  2382. module_param(force_scofix, bool, 0644);
  2383. MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
  2384. module_param(reset, bool, 0644);
  2385. MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
  2386. MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
  2387. MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
  2388. MODULE_VERSION(VERSION);
  2389. MODULE_LICENSE("GPL");