btusb.c 83 KB

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