btusb.c 86 KB

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