btusb.c 86 KB

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