btusb.c 86 KB

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