btusb.c 118 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * Generic Bluetooth USB driver
  5. *
  6. * Copyright (C) 2005-2008 Marcel Holtmann <[email protected]>
  7. */
  8. #include <linux/dmi.h>
  9. #include <linux/module.h>
  10. #include <linux/usb.h>
  11. #include <linux/usb/quirks.h>
  12. #include <linux/firmware.h>
  13. #include <linux/iopoll.h>
  14. #include <linux/of_device.h>
  15. #include <linux/of_irq.h>
  16. #include <linux/suspend.h>
  17. #include <linux/gpio/consumer.h>
  18. #include <linux/debugfs.h>
  19. #include <asm/unaligned.h>
  20. #include <net/bluetooth/bluetooth.h>
  21. #include <net/bluetooth/hci_core.h>
  22. #include "btintel.h"
  23. #include "btbcm.h"
  24. #include "btrtl.h"
  25. #include "btmtk.h"
  26. #define VERSION "0.8"
  27. static bool disable_scofix;
  28. static bool force_scofix;
  29. static bool enable_autosuspend = IS_ENABLED(CONFIG_BT_HCIBTUSB_AUTOSUSPEND);
  30. static bool reset = true;
  31. static struct usb_driver btusb_driver;
  32. #define BTUSB_IGNORE BIT(0)
  33. #define BTUSB_DIGIANSWER BIT(1)
  34. #define BTUSB_CSR BIT(2)
  35. #define BTUSB_SNIFFER BIT(3)
  36. #define BTUSB_BCM92035 BIT(4)
  37. #define BTUSB_BROKEN_ISOC BIT(5)
  38. #define BTUSB_WRONG_SCO_MTU BIT(6)
  39. #define BTUSB_ATH3012 BIT(7)
  40. #define BTUSB_INTEL_COMBINED BIT(8)
  41. #define BTUSB_INTEL_BOOT BIT(9)
  42. #define BTUSB_BCM_PATCHRAM BIT(10)
  43. #define BTUSB_MARVELL BIT(11)
  44. #define BTUSB_SWAVE BIT(12)
  45. #define BTUSB_AMP BIT(13)
  46. #define BTUSB_QCA_ROME BIT(14)
  47. #define BTUSB_BCM_APPLE BIT(15)
  48. #define BTUSB_REALTEK BIT(16)
  49. #define BTUSB_BCM2045 BIT(17)
  50. #define BTUSB_IFNUM_2 BIT(18)
  51. #define BTUSB_CW6622 BIT(19)
  52. #define BTUSB_MEDIATEK BIT(20)
  53. #define BTUSB_WIDEBAND_SPEECH BIT(21)
  54. #define BTUSB_VALID_LE_STATES BIT(22)
  55. #define BTUSB_QCA_WCN6855 BIT(23)
  56. #define BTUSB_INTEL_BROKEN_SHUTDOWN_LED BIT(24)
  57. #define BTUSB_INTEL_BROKEN_INITIAL_NCMD BIT(25)
  58. #define BTUSB_INTEL_NO_WBS_SUPPORT BIT(26)
  59. #define BTUSB_ACTIONS_SEMI BIT(27)
  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 BCM20702B0 (Dynex/Insignia) */
  100. { USB_DEVICE(0x19ff, 0x0239), .driver_info = BTUSB_BCM_PATCHRAM },
  101. /* Broadcom BCM43142A0 (Foxconn/Lenovo) */
  102. { USB_VENDOR_AND_INTERFACE_INFO(0x105b, 0xff, 0x01, 0x01),
  103. .driver_info = BTUSB_BCM_PATCHRAM },
  104. /* Broadcom BCM920703 (HTC Vive) */
  105. { USB_VENDOR_AND_INTERFACE_INFO(0x0bb4, 0xff, 0x01, 0x01),
  106. .driver_info = BTUSB_BCM_PATCHRAM },
  107. /* Foxconn - Hon Hai */
  108. { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
  109. .driver_info = BTUSB_BCM_PATCHRAM },
  110. /* Lite-On Technology - Broadcom based */
  111. { USB_VENDOR_AND_INTERFACE_INFO(0x04ca, 0xff, 0x01, 0x01),
  112. .driver_info = BTUSB_BCM_PATCHRAM },
  113. /* Broadcom devices with vendor specific id */
  114. { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
  115. .driver_info = BTUSB_BCM_PATCHRAM },
  116. /* ASUSTek Computer - Broadcom based */
  117. { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01),
  118. .driver_info = BTUSB_BCM_PATCHRAM },
  119. /* Belkin F8065bf - Broadcom based */
  120. { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01),
  121. .driver_info = BTUSB_BCM_PATCHRAM },
  122. /* IMC Networks - Broadcom based */
  123. { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01),
  124. .driver_info = BTUSB_BCM_PATCHRAM },
  125. /* Dell Computer - Broadcom based */
  126. { USB_VENDOR_AND_INTERFACE_INFO(0x413c, 0xff, 0x01, 0x01),
  127. .driver_info = BTUSB_BCM_PATCHRAM },
  128. /* Toshiba Corp - Broadcom based */
  129. { USB_VENDOR_AND_INTERFACE_INFO(0x0930, 0xff, 0x01, 0x01),
  130. .driver_info = BTUSB_BCM_PATCHRAM },
  131. /* Intel Bluetooth USB Bootloader (RAM module) */
  132. { USB_DEVICE(0x8087, 0x0a5a),
  133. .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
  134. { } /* Terminating entry */
  135. };
  136. MODULE_DEVICE_TABLE(usb, btusb_table);
  137. static const struct usb_device_id blacklist_table[] = {
  138. /* CSR BlueCore devices */
  139. { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
  140. /* Broadcom BCM2033 without firmware */
  141. { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
  142. /* Broadcom BCM2045 devices */
  143. { USB_DEVICE(0x0a5c, 0x2045), .driver_info = BTUSB_BCM2045 },
  144. /* Atheros 3011 with sflash firmware */
  145. { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
  146. { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
  147. { USB_DEVICE(0x04f2, 0xaff1), .driver_info = BTUSB_IGNORE },
  148. { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
  149. { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
  150. { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
  151. { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
  152. /* Atheros AR9285 Malbec with sflash firmware */
  153. { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
  154. /* Atheros 3012 with sflash firmware */
  155. { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
  156. { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
  157. { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
  158. { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
  159. { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
  160. { USB_DEVICE(0x0489, 0xe076), .driver_info = BTUSB_ATH3012 },
  161. { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
  162. { USB_DEVICE(0x0489, 0xe095), .driver_info = BTUSB_ATH3012 },
  163. { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
  164. { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
  165. { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
  166. { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
  167. { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
  168. { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
  169. { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
  170. { USB_DEVICE(0x04ca, 0x300d), .driver_info = BTUSB_ATH3012 },
  171. { USB_DEVICE(0x04ca, 0x300f), .driver_info = BTUSB_ATH3012 },
  172. { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
  173. { USB_DEVICE(0x04ca, 0x3014), .driver_info = BTUSB_ATH3012 },
  174. { USB_DEVICE(0x04ca, 0x3018), .driver_info = BTUSB_ATH3012 },
  175. { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
  176. { USB_DEVICE(0x0930, 0x021c), .driver_info = BTUSB_ATH3012 },
  177. { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
  178. { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
  179. { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
  180. { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
  181. { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
  182. { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
  183. { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
  184. { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
  185. { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
  186. { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
  187. { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
  188. { USB_DEVICE(0x0cf3, 0x817b), .driver_info = BTUSB_ATH3012 },
  189. { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
  190. { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
  191. { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
  192. { USB_DEVICE(0x0cf3, 0xe006), .driver_info = BTUSB_ATH3012 },
  193. { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
  194. { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
  195. { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
  196. { USB_DEVICE(0x13d3, 0x3395), .driver_info = BTUSB_ATH3012 },
  197. { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
  198. { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
  199. { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
  200. { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
  201. { USB_DEVICE(0x13d3, 0x3472), .driver_info = BTUSB_ATH3012 },
  202. { USB_DEVICE(0x13d3, 0x3474), .driver_info = BTUSB_ATH3012 },
  203. { USB_DEVICE(0x13d3, 0x3487), .driver_info = BTUSB_ATH3012 },
  204. { USB_DEVICE(0x13d3, 0x3490), .driver_info = BTUSB_ATH3012 },
  205. /* Atheros AR5BBU12 with sflash firmware */
  206. { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
  207. /* Atheros AR5BBU12 with sflash firmware */
  208. { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
  209. { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
  210. /* QCA ROME chipset */
  211. { USB_DEVICE(0x0cf3, 0x535b), .driver_info = BTUSB_QCA_ROME |
  212. BTUSB_WIDEBAND_SPEECH },
  213. { USB_DEVICE(0x0cf3, 0xe007), .driver_info = BTUSB_QCA_ROME |
  214. BTUSB_WIDEBAND_SPEECH },
  215. { USB_DEVICE(0x0cf3, 0xe009), .driver_info = BTUSB_QCA_ROME |
  216. BTUSB_WIDEBAND_SPEECH },
  217. { USB_DEVICE(0x0cf3, 0xe010), .driver_info = BTUSB_QCA_ROME |
  218. BTUSB_WIDEBAND_SPEECH },
  219. { USB_DEVICE(0x0cf3, 0xe300), .driver_info = BTUSB_QCA_ROME |
  220. BTUSB_WIDEBAND_SPEECH },
  221. { USB_DEVICE(0x0cf3, 0xe301), .driver_info = BTUSB_QCA_ROME |
  222. BTUSB_WIDEBAND_SPEECH },
  223. { USB_DEVICE(0x0cf3, 0xe360), .driver_info = BTUSB_QCA_ROME |
  224. BTUSB_WIDEBAND_SPEECH },
  225. { USB_DEVICE(0x0cf3, 0xe500), .driver_info = BTUSB_QCA_ROME |
  226. BTUSB_WIDEBAND_SPEECH },
  227. { USB_DEVICE(0x0489, 0xe092), .driver_info = BTUSB_QCA_ROME |
  228. BTUSB_WIDEBAND_SPEECH },
  229. { USB_DEVICE(0x0489, 0xe09f), .driver_info = BTUSB_QCA_ROME |
  230. BTUSB_WIDEBAND_SPEECH },
  231. { USB_DEVICE(0x0489, 0xe0a2), .driver_info = BTUSB_QCA_ROME |
  232. BTUSB_WIDEBAND_SPEECH },
  233. { USB_DEVICE(0x04ca, 0x3011), .driver_info = BTUSB_QCA_ROME |
  234. BTUSB_WIDEBAND_SPEECH },
  235. { USB_DEVICE(0x04ca, 0x3015), .driver_info = BTUSB_QCA_ROME |
  236. BTUSB_WIDEBAND_SPEECH },
  237. { USB_DEVICE(0x04ca, 0x3016), .driver_info = BTUSB_QCA_ROME |
  238. BTUSB_WIDEBAND_SPEECH },
  239. { USB_DEVICE(0x04ca, 0x301a), .driver_info = BTUSB_QCA_ROME |
  240. BTUSB_WIDEBAND_SPEECH },
  241. { USB_DEVICE(0x04ca, 0x3021), .driver_info = BTUSB_QCA_ROME |
  242. BTUSB_WIDEBAND_SPEECH },
  243. { USB_DEVICE(0x13d3, 0x3491), .driver_info = BTUSB_QCA_ROME |
  244. BTUSB_WIDEBAND_SPEECH },
  245. { USB_DEVICE(0x13d3, 0x3496), .driver_info = BTUSB_QCA_ROME |
  246. BTUSB_WIDEBAND_SPEECH },
  247. { USB_DEVICE(0x13d3, 0x3501), .driver_info = BTUSB_QCA_ROME |
  248. BTUSB_WIDEBAND_SPEECH },
  249. /* QCA WCN6855 chipset */
  250. { USB_DEVICE(0x0cf3, 0xe600), .driver_info = BTUSB_QCA_WCN6855 |
  251. BTUSB_WIDEBAND_SPEECH |
  252. BTUSB_VALID_LE_STATES },
  253. { USB_DEVICE(0x0489, 0xe0cc), .driver_info = BTUSB_QCA_WCN6855 |
  254. BTUSB_WIDEBAND_SPEECH |
  255. BTUSB_VALID_LE_STATES },
  256. { USB_DEVICE(0x0489, 0xe0d6), .driver_info = BTUSB_QCA_WCN6855 |
  257. BTUSB_WIDEBAND_SPEECH |
  258. BTUSB_VALID_LE_STATES },
  259. { USB_DEVICE(0x0489, 0xe0e3), .driver_info = BTUSB_QCA_WCN6855 |
  260. BTUSB_WIDEBAND_SPEECH |
  261. BTUSB_VALID_LE_STATES },
  262. { USB_DEVICE(0x10ab, 0x9309), .driver_info = BTUSB_QCA_WCN6855 |
  263. BTUSB_WIDEBAND_SPEECH |
  264. BTUSB_VALID_LE_STATES },
  265. { USB_DEVICE(0x10ab, 0x9409), .driver_info = BTUSB_QCA_WCN6855 |
  266. BTUSB_WIDEBAND_SPEECH |
  267. BTUSB_VALID_LE_STATES },
  268. { USB_DEVICE(0x0489, 0xe0d0), .driver_info = BTUSB_QCA_WCN6855 |
  269. BTUSB_WIDEBAND_SPEECH |
  270. BTUSB_VALID_LE_STATES },
  271. { USB_DEVICE(0x10ab, 0x9108), .driver_info = BTUSB_QCA_WCN6855 |
  272. BTUSB_WIDEBAND_SPEECH |
  273. BTUSB_VALID_LE_STATES },
  274. { USB_DEVICE(0x10ab, 0x9109), .driver_info = BTUSB_QCA_WCN6855 |
  275. BTUSB_WIDEBAND_SPEECH |
  276. BTUSB_VALID_LE_STATES },
  277. { USB_DEVICE(0x10ab, 0x9208), .driver_info = BTUSB_QCA_WCN6855 |
  278. BTUSB_WIDEBAND_SPEECH |
  279. BTUSB_VALID_LE_STATES },
  280. { USB_DEVICE(0x10ab, 0x9209), .driver_info = BTUSB_QCA_WCN6855 |
  281. BTUSB_WIDEBAND_SPEECH |
  282. BTUSB_VALID_LE_STATES },
  283. { USB_DEVICE(0x10ab, 0x9308), .driver_info = BTUSB_QCA_WCN6855 |
  284. BTUSB_WIDEBAND_SPEECH |
  285. BTUSB_VALID_LE_STATES },
  286. { USB_DEVICE(0x10ab, 0x9408), .driver_info = BTUSB_QCA_WCN6855 |
  287. BTUSB_WIDEBAND_SPEECH |
  288. BTUSB_VALID_LE_STATES },
  289. { USB_DEVICE(0x10ab, 0x9508), .driver_info = BTUSB_QCA_WCN6855 |
  290. BTUSB_WIDEBAND_SPEECH |
  291. BTUSB_VALID_LE_STATES },
  292. { USB_DEVICE(0x10ab, 0x9509), .driver_info = BTUSB_QCA_WCN6855 |
  293. BTUSB_WIDEBAND_SPEECH |
  294. BTUSB_VALID_LE_STATES },
  295. { USB_DEVICE(0x10ab, 0x9608), .driver_info = BTUSB_QCA_WCN6855 |
  296. BTUSB_WIDEBAND_SPEECH |
  297. BTUSB_VALID_LE_STATES },
  298. { USB_DEVICE(0x10ab, 0x9609), .driver_info = BTUSB_QCA_WCN6855 |
  299. BTUSB_WIDEBAND_SPEECH |
  300. BTUSB_VALID_LE_STATES },
  301. { USB_DEVICE(0x10ab, 0x9f09), .driver_info = BTUSB_QCA_WCN6855 |
  302. BTUSB_WIDEBAND_SPEECH |
  303. BTUSB_VALID_LE_STATES },
  304. { USB_DEVICE(0x04ca, 0x3022), .driver_info = BTUSB_QCA_WCN6855 |
  305. BTUSB_WIDEBAND_SPEECH |
  306. BTUSB_VALID_LE_STATES },
  307. { USB_DEVICE(0x0489, 0xe0c7), .driver_info = BTUSB_QCA_WCN6855 |
  308. BTUSB_WIDEBAND_SPEECH |
  309. BTUSB_VALID_LE_STATES },
  310. { USB_DEVICE(0x0489, 0xe0c9), .driver_info = BTUSB_QCA_WCN6855 |
  311. BTUSB_WIDEBAND_SPEECH |
  312. BTUSB_VALID_LE_STATES },
  313. { USB_DEVICE(0x0489, 0xe0ca), .driver_info = BTUSB_QCA_WCN6855 |
  314. BTUSB_WIDEBAND_SPEECH |
  315. BTUSB_VALID_LE_STATES },
  316. { USB_DEVICE(0x0489, 0xe0cb), .driver_info = BTUSB_QCA_WCN6855 |
  317. BTUSB_WIDEBAND_SPEECH |
  318. BTUSB_VALID_LE_STATES },
  319. { USB_DEVICE(0x0489, 0xe0ce), .driver_info = BTUSB_QCA_WCN6855 |
  320. BTUSB_WIDEBAND_SPEECH |
  321. BTUSB_VALID_LE_STATES },
  322. { USB_DEVICE(0x0489, 0xe0de), .driver_info = BTUSB_QCA_WCN6855 |
  323. BTUSB_WIDEBAND_SPEECH |
  324. BTUSB_VALID_LE_STATES },
  325. { USB_DEVICE(0x0489, 0xe0df), .driver_info = BTUSB_QCA_WCN6855 |
  326. BTUSB_WIDEBAND_SPEECH |
  327. BTUSB_VALID_LE_STATES },
  328. { USB_DEVICE(0x0489, 0xe0e1), .driver_info = BTUSB_QCA_WCN6855 |
  329. BTUSB_WIDEBAND_SPEECH |
  330. BTUSB_VALID_LE_STATES },
  331. { USB_DEVICE(0x0489, 0xe0ea), .driver_info = BTUSB_QCA_WCN6855 |
  332. BTUSB_WIDEBAND_SPEECH |
  333. BTUSB_VALID_LE_STATES },
  334. { USB_DEVICE(0x0489, 0xe0ec), .driver_info = BTUSB_QCA_WCN6855 |
  335. BTUSB_WIDEBAND_SPEECH |
  336. BTUSB_VALID_LE_STATES },
  337. { USB_DEVICE(0x04ca, 0x3023), .driver_info = BTUSB_QCA_WCN6855 |
  338. BTUSB_WIDEBAND_SPEECH |
  339. BTUSB_VALID_LE_STATES },
  340. { USB_DEVICE(0x04ca, 0x3024), .driver_info = BTUSB_QCA_WCN6855 |
  341. BTUSB_WIDEBAND_SPEECH |
  342. BTUSB_VALID_LE_STATES },
  343. { USB_DEVICE(0x04ca, 0x3a22), .driver_info = BTUSB_QCA_WCN6855 |
  344. BTUSB_WIDEBAND_SPEECH |
  345. BTUSB_VALID_LE_STATES },
  346. { USB_DEVICE(0x04ca, 0x3a24), .driver_info = BTUSB_QCA_WCN6855 |
  347. BTUSB_WIDEBAND_SPEECH |
  348. BTUSB_VALID_LE_STATES },
  349. { USB_DEVICE(0x04ca, 0x3a26), .driver_info = BTUSB_QCA_WCN6855 |
  350. BTUSB_WIDEBAND_SPEECH |
  351. BTUSB_VALID_LE_STATES },
  352. { USB_DEVICE(0x04ca, 0x3a27), .driver_info = BTUSB_QCA_WCN6855 |
  353. BTUSB_WIDEBAND_SPEECH |
  354. BTUSB_VALID_LE_STATES },
  355. /* QCA WCN785x chipset */
  356. { USB_DEVICE(0x0cf3, 0xe700), .driver_info = BTUSB_QCA_WCN6855 |
  357. BTUSB_WIDEBAND_SPEECH |
  358. BTUSB_VALID_LE_STATES },
  359. /* Broadcom BCM2035 */
  360. { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
  361. { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
  362. { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
  363. /* Broadcom BCM2045 */
  364. { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
  365. { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
  366. /* IBM/Lenovo ThinkPad with Broadcom chip */
  367. { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
  368. { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
  369. /* HP laptop with Broadcom chip */
  370. { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
  371. /* Dell laptop with Broadcom chip */
  372. { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
  373. /* Dell Wireless 370 and 410 devices */
  374. { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
  375. { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
  376. /* Belkin F8T012 and F8T013 devices */
  377. { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
  378. { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
  379. /* Asus WL-BTD202 device */
  380. { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
  381. /* Kensington Bluetooth USB adapter */
  382. { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
  383. /* RTX Telecom based adapters with buggy SCO support */
  384. { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
  385. { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
  386. /* CONWISE Technology based adapters with buggy SCO support */
  387. { USB_DEVICE(0x0e5e, 0x6622),
  388. .driver_info = BTUSB_BROKEN_ISOC | BTUSB_CW6622},
  389. /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
  390. { USB_DEVICE(0x1310, 0x0001), .driver_info = BTUSB_SWAVE },
  391. /* Digianswer devices */
  392. { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
  393. { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
  394. /* CSR BlueCore Bluetooth Sniffer */
  395. { USB_DEVICE(0x0a12, 0x0002),
  396. .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
  397. /* Frontline ComProbe Bluetooth Sniffer */
  398. { USB_DEVICE(0x16d3, 0x0002),
  399. .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
  400. /* Marvell Bluetooth devices */
  401. { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
  402. { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
  403. { USB_DEVICE(0x1286, 0x204e), .driver_info = BTUSB_MARVELL },
  404. /* Intel Bluetooth devices */
  405. { USB_DEVICE(0x8087, 0x0025), .driver_info = BTUSB_INTEL_COMBINED },
  406. { USB_DEVICE(0x8087, 0x0026), .driver_info = BTUSB_INTEL_COMBINED },
  407. { USB_DEVICE(0x8087, 0x0029), .driver_info = BTUSB_INTEL_COMBINED },
  408. { USB_DEVICE(0x8087, 0x0032), .driver_info = BTUSB_INTEL_COMBINED },
  409. { USB_DEVICE(0x8087, 0x0033), .driver_info = BTUSB_INTEL_COMBINED },
  410. { USB_DEVICE(0x8087, 0x0035), .driver_info = BTUSB_INTEL_COMBINED },
  411. { USB_DEVICE(0x8087, 0x07da), .driver_info = BTUSB_CSR },
  412. { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL_COMBINED |
  413. BTUSB_INTEL_NO_WBS_SUPPORT |
  414. BTUSB_INTEL_BROKEN_INITIAL_NCMD |
  415. BTUSB_INTEL_BROKEN_SHUTDOWN_LED },
  416. { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL_COMBINED |
  417. BTUSB_INTEL_NO_WBS_SUPPORT |
  418. BTUSB_INTEL_BROKEN_SHUTDOWN_LED },
  419. { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_COMBINED },
  420. { USB_DEVICE(0x8087, 0x0aa7), .driver_info = BTUSB_INTEL_COMBINED |
  421. BTUSB_INTEL_BROKEN_SHUTDOWN_LED },
  422. { USB_DEVICE(0x8087, 0x0aaa), .driver_info = BTUSB_INTEL_COMBINED },
  423. /* Other Intel Bluetooth devices */
  424. { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
  425. .driver_info = BTUSB_IGNORE },
  426. /* Realtek 8821CE Bluetooth devices */
  427. { USB_DEVICE(0x13d3, 0x3529), .driver_info = BTUSB_REALTEK |
  428. BTUSB_WIDEBAND_SPEECH },
  429. /* Realtek 8822CE Bluetooth devices */
  430. { USB_DEVICE(0x0bda, 0xb00c), .driver_info = BTUSB_REALTEK |
  431. BTUSB_WIDEBAND_SPEECH },
  432. { USB_DEVICE(0x0bda, 0xc822), .driver_info = BTUSB_REALTEK |
  433. BTUSB_WIDEBAND_SPEECH },
  434. /* Realtek 8852AE Bluetooth devices */
  435. { USB_DEVICE(0x0bda, 0x2852), .driver_info = BTUSB_REALTEK |
  436. BTUSB_WIDEBAND_SPEECH },
  437. { USB_DEVICE(0x0bda, 0xc852), .driver_info = BTUSB_REALTEK |
  438. BTUSB_WIDEBAND_SPEECH },
  439. { USB_DEVICE(0x0bda, 0x385a), .driver_info = BTUSB_REALTEK |
  440. BTUSB_WIDEBAND_SPEECH },
  441. { USB_DEVICE(0x0bda, 0x4852), .driver_info = BTUSB_REALTEK |
  442. BTUSB_WIDEBAND_SPEECH },
  443. { USB_DEVICE(0x04c5, 0x165c), .driver_info = BTUSB_REALTEK |
  444. BTUSB_WIDEBAND_SPEECH },
  445. { USB_DEVICE(0x04ca, 0x4006), .driver_info = BTUSB_REALTEK |
  446. BTUSB_WIDEBAND_SPEECH },
  447. { USB_DEVICE(0x0cb8, 0xc549), .driver_info = BTUSB_REALTEK |
  448. BTUSB_WIDEBAND_SPEECH },
  449. /* Realtek 8852CE Bluetooth devices */
  450. { USB_DEVICE(0x04ca, 0x4007), .driver_info = BTUSB_REALTEK |
  451. BTUSB_WIDEBAND_SPEECH },
  452. { USB_DEVICE(0x04c5, 0x1675), .driver_info = BTUSB_REALTEK |
  453. BTUSB_WIDEBAND_SPEECH },
  454. { USB_DEVICE(0x0cb8, 0xc558), .driver_info = BTUSB_REALTEK |
  455. BTUSB_WIDEBAND_SPEECH },
  456. { USB_DEVICE(0x13d3, 0x3587), .driver_info = BTUSB_REALTEK |
  457. BTUSB_WIDEBAND_SPEECH },
  458. { USB_DEVICE(0x13d3, 0x3586), .driver_info = BTUSB_REALTEK |
  459. BTUSB_WIDEBAND_SPEECH },
  460. { USB_DEVICE(0x13d3, 0x3592), .driver_info = BTUSB_REALTEK |
  461. BTUSB_WIDEBAND_SPEECH },
  462. /* Realtek 8852BE Bluetooth devices */
  463. { USB_DEVICE(0x0cb8, 0xc559), .driver_info = BTUSB_REALTEK |
  464. BTUSB_WIDEBAND_SPEECH },
  465. { USB_DEVICE(0x0bda, 0x887b), .driver_info = BTUSB_REALTEK |
  466. BTUSB_WIDEBAND_SPEECH },
  467. { USB_DEVICE(0x0bda, 0xb85b), .driver_info = BTUSB_REALTEK |
  468. BTUSB_WIDEBAND_SPEECH },
  469. { USB_DEVICE(0x13d3, 0x3570), .driver_info = BTUSB_REALTEK |
  470. BTUSB_WIDEBAND_SPEECH },
  471. { USB_DEVICE(0x13d3, 0x3571), .driver_info = BTUSB_REALTEK |
  472. BTUSB_WIDEBAND_SPEECH },
  473. /* Realtek Bluetooth devices */
  474. { USB_VENDOR_AND_INTERFACE_INFO(0x0bda, 0xe0, 0x01, 0x01),
  475. .driver_info = BTUSB_REALTEK },
  476. /* MediaTek Bluetooth devices */
  477. { USB_VENDOR_AND_INTERFACE_INFO(0x0e8d, 0xe0, 0x01, 0x01),
  478. .driver_info = BTUSB_MEDIATEK |
  479. BTUSB_WIDEBAND_SPEECH |
  480. BTUSB_VALID_LE_STATES },
  481. /* Additional MediaTek MT7615E Bluetooth devices */
  482. { USB_DEVICE(0x13d3, 0x3560), .driver_info = BTUSB_MEDIATEK},
  483. /* Additional MediaTek MT7663 Bluetooth devices */
  484. { USB_DEVICE(0x043e, 0x310c), .driver_info = BTUSB_MEDIATEK |
  485. BTUSB_WIDEBAND_SPEECH |
  486. BTUSB_VALID_LE_STATES },
  487. { USB_DEVICE(0x04ca, 0x3801), .driver_info = BTUSB_MEDIATEK |
  488. BTUSB_WIDEBAND_SPEECH |
  489. BTUSB_VALID_LE_STATES },
  490. /* Additional MediaTek MT7668 Bluetooth devices */
  491. { USB_DEVICE(0x043e, 0x3109), .driver_info = BTUSB_MEDIATEK |
  492. BTUSB_WIDEBAND_SPEECH |
  493. BTUSB_VALID_LE_STATES },
  494. /* Additional MediaTek MT7921 Bluetooth devices */
  495. { USB_DEVICE(0x0489, 0xe0c8), .driver_info = BTUSB_MEDIATEK |
  496. BTUSB_WIDEBAND_SPEECH |
  497. BTUSB_VALID_LE_STATES },
  498. { USB_DEVICE(0x0489, 0xe0e0), .driver_info = BTUSB_MEDIATEK |
  499. BTUSB_WIDEBAND_SPEECH |
  500. BTUSB_VALID_LE_STATES },
  501. { USB_DEVICE(0x0489, 0xe0f2), .driver_info = BTUSB_MEDIATEK |
  502. BTUSB_WIDEBAND_SPEECH |
  503. BTUSB_VALID_LE_STATES },
  504. { USB_DEVICE(0x04ca, 0x3802), .driver_info = BTUSB_MEDIATEK |
  505. BTUSB_WIDEBAND_SPEECH |
  506. BTUSB_VALID_LE_STATES },
  507. { USB_DEVICE(0x13d3, 0x3563), .driver_info = BTUSB_MEDIATEK |
  508. BTUSB_WIDEBAND_SPEECH |
  509. BTUSB_VALID_LE_STATES },
  510. { USB_DEVICE(0x13d3, 0x3564), .driver_info = BTUSB_MEDIATEK |
  511. BTUSB_WIDEBAND_SPEECH |
  512. BTUSB_VALID_LE_STATES },
  513. { USB_DEVICE(0x13d3, 0x3567), .driver_info = BTUSB_MEDIATEK |
  514. BTUSB_WIDEBAND_SPEECH |
  515. BTUSB_VALID_LE_STATES },
  516. { USB_DEVICE(0x13d3, 0x3578), .driver_info = BTUSB_MEDIATEK |
  517. BTUSB_WIDEBAND_SPEECH |
  518. BTUSB_VALID_LE_STATES },
  519. { USB_DEVICE(0x13d3, 0x3583), .driver_info = BTUSB_MEDIATEK |
  520. BTUSB_WIDEBAND_SPEECH |
  521. BTUSB_VALID_LE_STATES },
  522. { USB_DEVICE(0x0489, 0xe0cd), .driver_info = BTUSB_MEDIATEK |
  523. BTUSB_WIDEBAND_SPEECH |
  524. BTUSB_VALID_LE_STATES },
  525. { USB_DEVICE(0x0e8d, 0x0608), .driver_info = BTUSB_MEDIATEK |
  526. BTUSB_WIDEBAND_SPEECH |
  527. BTUSB_VALID_LE_STATES },
  528. /* MediaTek MT7922A Bluetooth devices */
  529. { USB_DEVICE(0x0489, 0xe0d8), .driver_info = BTUSB_MEDIATEK |
  530. BTUSB_WIDEBAND_SPEECH |
  531. BTUSB_VALID_LE_STATES },
  532. { USB_DEVICE(0x0489, 0xe0d9), .driver_info = BTUSB_MEDIATEK |
  533. BTUSB_WIDEBAND_SPEECH |
  534. BTUSB_VALID_LE_STATES },
  535. { USB_DEVICE(0x0489, 0xe0f5), .driver_info = BTUSB_MEDIATEK |
  536. BTUSB_WIDEBAND_SPEECH |
  537. BTUSB_VALID_LE_STATES },
  538. { USB_DEVICE(0x13d3, 0x3568), .driver_info = BTUSB_MEDIATEK |
  539. BTUSB_WIDEBAND_SPEECH |
  540. BTUSB_VALID_LE_STATES },
  541. { USB_DEVICE(0x0489, 0xe0e2), .driver_info = BTUSB_MEDIATEK |
  542. BTUSB_WIDEBAND_SPEECH |
  543. BTUSB_VALID_LE_STATES },
  544. /* Additional Realtek 8723AE Bluetooth devices */
  545. { USB_DEVICE(0x0930, 0x021d), .driver_info = BTUSB_REALTEK },
  546. { USB_DEVICE(0x13d3, 0x3394), .driver_info = BTUSB_REALTEK },
  547. /* Additional Realtek 8723BE Bluetooth devices */
  548. { USB_DEVICE(0x0489, 0xe085), .driver_info = BTUSB_REALTEK },
  549. { USB_DEVICE(0x0489, 0xe08b), .driver_info = BTUSB_REALTEK },
  550. { USB_DEVICE(0x04f2, 0xb49f), .driver_info = BTUSB_REALTEK },
  551. { USB_DEVICE(0x13d3, 0x3410), .driver_info = BTUSB_REALTEK },
  552. { USB_DEVICE(0x13d3, 0x3416), .driver_info = BTUSB_REALTEK },
  553. { USB_DEVICE(0x13d3, 0x3459), .driver_info = BTUSB_REALTEK },
  554. { USB_DEVICE(0x13d3, 0x3494), .driver_info = BTUSB_REALTEK },
  555. /* Additional Realtek 8723BU Bluetooth devices */
  556. { USB_DEVICE(0x7392, 0xa611), .driver_info = BTUSB_REALTEK },
  557. /* Additional Realtek 8723DE Bluetooth devices */
  558. { USB_DEVICE(0x0bda, 0xb009), .driver_info = BTUSB_REALTEK },
  559. { USB_DEVICE(0x2ff8, 0xb011), .driver_info = BTUSB_REALTEK },
  560. /* Additional Realtek 8761BUV Bluetooth devices */
  561. { USB_DEVICE(0x2357, 0x0604), .driver_info = BTUSB_REALTEK |
  562. BTUSB_WIDEBAND_SPEECH },
  563. { USB_DEVICE(0x0b05, 0x190e), .driver_info = BTUSB_REALTEK |
  564. BTUSB_WIDEBAND_SPEECH },
  565. { USB_DEVICE(0x2550, 0x8761), .driver_info = BTUSB_REALTEK |
  566. BTUSB_WIDEBAND_SPEECH },
  567. { USB_DEVICE(0x0bda, 0x8771), .driver_info = BTUSB_REALTEK |
  568. BTUSB_WIDEBAND_SPEECH },
  569. { USB_DEVICE(0x7392, 0xc611), .driver_info = BTUSB_REALTEK |
  570. BTUSB_WIDEBAND_SPEECH },
  571. /* Additional Realtek 8821AE Bluetooth devices */
  572. { USB_DEVICE(0x0b05, 0x17dc), .driver_info = BTUSB_REALTEK },
  573. { USB_DEVICE(0x13d3, 0x3414), .driver_info = BTUSB_REALTEK },
  574. { USB_DEVICE(0x13d3, 0x3458), .driver_info = BTUSB_REALTEK },
  575. { USB_DEVICE(0x13d3, 0x3461), .driver_info = BTUSB_REALTEK },
  576. { USB_DEVICE(0x13d3, 0x3462), .driver_info = BTUSB_REALTEK },
  577. /* Additional Realtek 8822BE Bluetooth devices */
  578. { USB_DEVICE(0x13d3, 0x3526), .driver_info = BTUSB_REALTEK },
  579. { USB_DEVICE(0x0b05, 0x185c), .driver_info = BTUSB_REALTEK },
  580. /* Additional Realtek 8822CE Bluetooth devices */
  581. { USB_DEVICE(0x04ca, 0x4005), .driver_info = BTUSB_REALTEK |
  582. BTUSB_WIDEBAND_SPEECH },
  583. { USB_DEVICE(0x04c5, 0x161f), .driver_info = BTUSB_REALTEK |
  584. BTUSB_WIDEBAND_SPEECH },
  585. { USB_DEVICE(0x0b05, 0x18ef), .driver_info = BTUSB_REALTEK |
  586. BTUSB_WIDEBAND_SPEECH },
  587. { USB_DEVICE(0x13d3, 0x3548), .driver_info = BTUSB_REALTEK |
  588. BTUSB_WIDEBAND_SPEECH },
  589. { USB_DEVICE(0x13d3, 0x3549), .driver_info = BTUSB_REALTEK |
  590. BTUSB_WIDEBAND_SPEECH },
  591. { USB_DEVICE(0x13d3, 0x3553), .driver_info = BTUSB_REALTEK |
  592. BTUSB_WIDEBAND_SPEECH },
  593. { USB_DEVICE(0x13d3, 0x3555), .driver_info = BTUSB_REALTEK |
  594. BTUSB_WIDEBAND_SPEECH },
  595. { USB_DEVICE(0x2ff8, 0x3051), .driver_info = BTUSB_REALTEK |
  596. BTUSB_WIDEBAND_SPEECH },
  597. { USB_DEVICE(0x1358, 0xc123), .driver_info = BTUSB_REALTEK |
  598. BTUSB_WIDEBAND_SPEECH },
  599. { USB_DEVICE(0x0bda, 0xc123), .driver_info = BTUSB_REALTEK |
  600. BTUSB_WIDEBAND_SPEECH },
  601. { USB_DEVICE(0x0cb5, 0xc547), .driver_info = BTUSB_REALTEK |
  602. BTUSB_WIDEBAND_SPEECH },
  603. /* Actions Semiconductor ATS2851 based devices */
  604. { USB_DEVICE(0x10d7, 0xb012), .driver_info = BTUSB_ACTIONS_SEMI },
  605. /* Silicon Wave based devices */
  606. { USB_DEVICE(0x0c10, 0x0000), .driver_info = BTUSB_SWAVE },
  607. { } /* Terminating entry */
  608. };
  609. /* The Bluetooth USB module build into some devices needs to be reset on resume,
  610. * this is a problem with the platform (likely shutting off all power) not with
  611. * the module itself. So we use a DMI list to match known broken platforms.
  612. */
  613. static const struct dmi_system_id btusb_needs_reset_resume_table[] = {
  614. {
  615. /* Dell OptiPlex 3060 (QCA ROME device 0cf3:e007) */
  616. .matches = {
  617. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  618. DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 3060"),
  619. },
  620. },
  621. {
  622. /* Dell XPS 9360 (QCA ROME device 0cf3:e300) */
  623. .matches = {
  624. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  625. DMI_MATCH(DMI_PRODUCT_NAME, "XPS 13 9360"),
  626. },
  627. },
  628. {
  629. /* Dell Inspiron 5565 (QCA ROME device 0cf3:e009) */
  630. .matches = {
  631. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  632. DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5565"),
  633. },
  634. },
  635. {}
  636. };
  637. #define BTUSB_MAX_ISOC_FRAMES 10
  638. #define BTUSB_INTR_RUNNING 0
  639. #define BTUSB_BULK_RUNNING 1
  640. #define BTUSB_ISOC_RUNNING 2
  641. #define BTUSB_SUSPENDING 3
  642. #define BTUSB_DID_ISO_RESUME 4
  643. #define BTUSB_BOOTLOADER 5
  644. #define BTUSB_DOWNLOADING 6
  645. #define BTUSB_FIRMWARE_LOADED 7
  646. #define BTUSB_FIRMWARE_FAILED 8
  647. #define BTUSB_BOOTING 9
  648. #define BTUSB_DIAG_RUNNING 10
  649. #define BTUSB_OOB_WAKE_ENABLED 11
  650. #define BTUSB_HW_RESET_ACTIVE 12
  651. #define BTUSB_TX_WAIT_VND_EVT 13
  652. #define BTUSB_WAKEUP_AUTOSUSPEND 14
  653. #define BTUSB_USE_ALT3_FOR_WBS 15
  654. struct btusb_data {
  655. struct hci_dev *hdev;
  656. struct usb_device *udev;
  657. struct usb_interface *intf;
  658. struct usb_interface *isoc;
  659. struct usb_interface *diag;
  660. unsigned isoc_ifnum;
  661. unsigned long flags;
  662. bool poll_sync;
  663. int intr_interval;
  664. struct work_struct work;
  665. struct work_struct waker;
  666. struct delayed_work rx_work;
  667. struct sk_buff_head acl_q;
  668. struct usb_anchor deferred;
  669. struct usb_anchor tx_anchor;
  670. int tx_in_flight;
  671. spinlock_t txlock;
  672. struct usb_anchor intr_anchor;
  673. struct usb_anchor bulk_anchor;
  674. struct usb_anchor isoc_anchor;
  675. struct usb_anchor diag_anchor;
  676. struct usb_anchor ctrl_anchor;
  677. spinlock_t rxlock;
  678. struct sk_buff *evt_skb;
  679. struct sk_buff *acl_skb;
  680. struct sk_buff *sco_skb;
  681. struct usb_endpoint_descriptor *intr_ep;
  682. struct usb_endpoint_descriptor *bulk_tx_ep;
  683. struct usb_endpoint_descriptor *bulk_rx_ep;
  684. struct usb_endpoint_descriptor *isoc_tx_ep;
  685. struct usb_endpoint_descriptor *isoc_rx_ep;
  686. struct usb_endpoint_descriptor *diag_tx_ep;
  687. struct usb_endpoint_descriptor *diag_rx_ep;
  688. struct gpio_desc *reset_gpio;
  689. __u8 cmdreq_type;
  690. __u8 cmdreq;
  691. unsigned int sco_num;
  692. unsigned int air_mode;
  693. bool usb_alt6_packet_flow;
  694. int isoc_altsetting;
  695. int suspend_count;
  696. int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
  697. int (*recv_acl)(struct hci_dev *hdev, struct sk_buff *skb);
  698. int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
  699. int (*setup_on_usb)(struct hci_dev *hdev);
  700. int oob_wake_irq; /* irq for out-of-band wake-on-bt */
  701. unsigned cmd_timeout_cnt;
  702. };
  703. static void btusb_intel_cmd_timeout(struct hci_dev *hdev)
  704. {
  705. struct btusb_data *data = hci_get_drvdata(hdev);
  706. struct gpio_desc *reset_gpio = data->reset_gpio;
  707. if (++data->cmd_timeout_cnt < 5)
  708. return;
  709. if (!reset_gpio) {
  710. bt_dev_err(hdev, "No way to reset. Ignoring and continuing");
  711. return;
  712. }
  713. /*
  714. * Toggle the hard reset line if the platform provides one. The reset
  715. * is going to yank the device off the USB and then replug. So doing
  716. * once is enough. The cleanup is handled correctly on the way out
  717. * (standard USB disconnect), and the new device is detected cleanly
  718. * and bound to the driver again like it should be.
  719. */
  720. if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
  721. bt_dev_err(hdev, "last reset failed? Not resetting again");
  722. return;
  723. }
  724. bt_dev_err(hdev, "Initiating HW reset via gpio");
  725. gpiod_set_value_cansleep(reset_gpio, 1);
  726. msleep(100);
  727. gpiod_set_value_cansleep(reset_gpio, 0);
  728. }
  729. static void btusb_rtl_cmd_timeout(struct hci_dev *hdev)
  730. {
  731. struct btusb_data *data = hci_get_drvdata(hdev);
  732. struct gpio_desc *reset_gpio = data->reset_gpio;
  733. if (++data->cmd_timeout_cnt < 5)
  734. return;
  735. if (!reset_gpio) {
  736. bt_dev_err(hdev, "No gpio to reset Realtek device, ignoring");
  737. return;
  738. }
  739. /* Toggle the hard reset line. The Realtek device is going to
  740. * yank itself off the USB and then replug. The cleanup is handled
  741. * correctly on the way out (standard USB disconnect), and the new
  742. * device is detected cleanly and bound to the driver again like
  743. * it should be.
  744. */
  745. if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
  746. bt_dev_err(hdev, "last reset failed? Not resetting again");
  747. return;
  748. }
  749. bt_dev_err(hdev, "Reset Realtek device via gpio");
  750. gpiod_set_value_cansleep(reset_gpio, 1);
  751. msleep(200);
  752. gpiod_set_value_cansleep(reset_gpio, 0);
  753. }
  754. static void btusb_qca_cmd_timeout(struct hci_dev *hdev)
  755. {
  756. struct btusb_data *data = hci_get_drvdata(hdev);
  757. struct gpio_desc *reset_gpio = data->reset_gpio;
  758. int err;
  759. if (++data->cmd_timeout_cnt < 5)
  760. return;
  761. if (reset_gpio) {
  762. bt_dev_err(hdev, "Reset qca device via bt_en gpio");
  763. /* Toggle the hard reset line. The qca bt device is going to
  764. * yank itself off the USB and then replug. The cleanup is handled
  765. * correctly on the way out (standard USB disconnect), and the new
  766. * device is detected cleanly and bound to the driver again like
  767. * it should be.
  768. */
  769. if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
  770. bt_dev_err(hdev, "last reset failed? Not resetting again");
  771. return;
  772. }
  773. gpiod_set_value_cansleep(reset_gpio, 0);
  774. msleep(200);
  775. gpiod_set_value_cansleep(reset_gpio, 1);
  776. return;
  777. }
  778. bt_dev_err(hdev, "Multiple cmd timeouts seen. Resetting usb device.");
  779. /* This is not an unbalanced PM reference since the device will reset */
  780. err = usb_autopm_get_interface(data->intf);
  781. if (!err)
  782. usb_queue_reset_device(data->intf);
  783. else
  784. bt_dev_err(hdev, "Failed usb_autopm_get_interface with %d", err);
  785. }
  786. static inline void btusb_free_frags(struct btusb_data *data)
  787. {
  788. unsigned long flags;
  789. spin_lock_irqsave(&data->rxlock, flags);
  790. dev_kfree_skb_irq(data->evt_skb);
  791. data->evt_skb = NULL;
  792. dev_kfree_skb_irq(data->acl_skb);
  793. data->acl_skb = NULL;
  794. dev_kfree_skb_irq(data->sco_skb);
  795. data->sco_skb = NULL;
  796. spin_unlock_irqrestore(&data->rxlock, flags);
  797. }
  798. static int btusb_recv_event(struct btusb_data *data, struct sk_buff *skb)
  799. {
  800. if (data->intr_interval) {
  801. /* Trigger dequeue immediatelly if an event is received */
  802. schedule_delayed_work(&data->rx_work, 0);
  803. }
  804. return data->recv_event(data->hdev, skb);
  805. }
  806. static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
  807. {
  808. struct sk_buff *skb;
  809. unsigned long flags;
  810. int err = 0;
  811. spin_lock_irqsave(&data->rxlock, flags);
  812. skb = data->evt_skb;
  813. while (count) {
  814. int len;
  815. if (!skb) {
  816. skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
  817. if (!skb) {
  818. err = -ENOMEM;
  819. break;
  820. }
  821. hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
  822. hci_skb_expect(skb) = HCI_EVENT_HDR_SIZE;
  823. }
  824. len = min_t(uint, hci_skb_expect(skb), count);
  825. skb_put_data(skb, buffer, len);
  826. count -= len;
  827. buffer += len;
  828. hci_skb_expect(skb) -= len;
  829. if (skb->len == HCI_EVENT_HDR_SIZE) {
  830. /* Complete event header */
  831. hci_skb_expect(skb) = hci_event_hdr(skb)->plen;
  832. if (skb_tailroom(skb) < hci_skb_expect(skb)) {
  833. kfree_skb(skb);
  834. skb = NULL;
  835. err = -EILSEQ;
  836. break;
  837. }
  838. }
  839. if (!hci_skb_expect(skb)) {
  840. /* Complete frame */
  841. btusb_recv_event(data, skb);
  842. skb = NULL;
  843. }
  844. }
  845. data->evt_skb = skb;
  846. spin_unlock_irqrestore(&data->rxlock, flags);
  847. return err;
  848. }
  849. static int btusb_recv_acl(struct btusb_data *data, struct sk_buff *skb)
  850. {
  851. /* Only queue ACL packet if intr_interval is set as it means
  852. * force_poll_sync has been enabled.
  853. */
  854. if (!data->intr_interval)
  855. return data->recv_acl(data->hdev, skb);
  856. skb_queue_tail(&data->acl_q, skb);
  857. schedule_delayed_work(&data->rx_work, data->intr_interval);
  858. return 0;
  859. }
  860. static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
  861. {
  862. struct sk_buff *skb;
  863. unsigned long flags;
  864. int err = 0;
  865. spin_lock_irqsave(&data->rxlock, flags);
  866. skb = data->acl_skb;
  867. while (count) {
  868. int len;
  869. if (!skb) {
  870. skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
  871. if (!skb) {
  872. err = -ENOMEM;
  873. break;
  874. }
  875. hci_skb_pkt_type(skb) = HCI_ACLDATA_PKT;
  876. hci_skb_expect(skb) = HCI_ACL_HDR_SIZE;
  877. }
  878. len = min_t(uint, hci_skb_expect(skb), count);
  879. skb_put_data(skb, buffer, len);
  880. count -= len;
  881. buffer += len;
  882. hci_skb_expect(skb) -= len;
  883. if (skb->len == HCI_ACL_HDR_SIZE) {
  884. __le16 dlen = hci_acl_hdr(skb)->dlen;
  885. /* Complete ACL header */
  886. hci_skb_expect(skb) = __le16_to_cpu(dlen);
  887. if (skb_tailroom(skb) < hci_skb_expect(skb)) {
  888. kfree_skb(skb);
  889. skb = NULL;
  890. err = -EILSEQ;
  891. break;
  892. }
  893. }
  894. if (!hci_skb_expect(skb)) {
  895. /* Complete frame */
  896. btusb_recv_acl(data, skb);
  897. skb = NULL;
  898. }
  899. }
  900. data->acl_skb = skb;
  901. spin_unlock_irqrestore(&data->rxlock, flags);
  902. return err;
  903. }
  904. static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
  905. {
  906. struct sk_buff *skb;
  907. unsigned long flags;
  908. int err = 0;
  909. spin_lock_irqsave(&data->rxlock, flags);
  910. skb = data->sco_skb;
  911. while (count) {
  912. int len;
  913. if (!skb) {
  914. skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
  915. if (!skb) {
  916. err = -ENOMEM;
  917. break;
  918. }
  919. hci_skb_pkt_type(skb) = HCI_SCODATA_PKT;
  920. hci_skb_expect(skb) = HCI_SCO_HDR_SIZE;
  921. }
  922. len = min_t(uint, hci_skb_expect(skb), count);
  923. skb_put_data(skb, buffer, len);
  924. count -= len;
  925. buffer += len;
  926. hci_skb_expect(skb) -= len;
  927. if (skb->len == HCI_SCO_HDR_SIZE) {
  928. /* Complete SCO header */
  929. hci_skb_expect(skb) = hci_sco_hdr(skb)->dlen;
  930. if (skb_tailroom(skb) < hci_skb_expect(skb)) {
  931. kfree_skb(skb);
  932. skb = NULL;
  933. err = -EILSEQ;
  934. break;
  935. }
  936. }
  937. if (!hci_skb_expect(skb)) {
  938. /* Complete frame */
  939. hci_recv_frame(data->hdev, skb);
  940. skb = NULL;
  941. }
  942. }
  943. data->sco_skb = skb;
  944. spin_unlock_irqrestore(&data->rxlock, flags);
  945. return err;
  946. }
  947. static void btusb_intr_complete(struct urb *urb)
  948. {
  949. struct hci_dev *hdev = urb->context;
  950. struct btusb_data *data = hci_get_drvdata(hdev);
  951. int err;
  952. BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
  953. urb->actual_length);
  954. if (!test_bit(HCI_RUNNING, &hdev->flags))
  955. return;
  956. if (urb->status == 0) {
  957. hdev->stat.byte_rx += urb->actual_length;
  958. if (btusb_recv_intr(data, urb->transfer_buffer,
  959. urb->actual_length) < 0) {
  960. bt_dev_err(hdev, "corrupted event packet");
  961. hdev->stat.err_rx++;
  962. }
  963. } else if (urb->status == -ENOENT) {
  964. /* Avoid suspend failed when usb_kill_urb */
  965. return;
  966. }
  967. if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
  968. return;
  969. usb_mark_last_busy(data->udev);
  970. usb_anchor_urb(urb, &data->intr_anchor);
  971. err = usb_submit_urb(urb, GFP_ATOMIC);
  972. if (err < 0) {
  973. /* -EPERM: urb is being killed;
  974. * -ENODEV: device got disconnected
  975. */
  976. if (err != -EPERM && err != -ENODEV)
  977. bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
  978. urb, -err);
  979. if (err != -EPERM)
  980. hci_cmd_sync_cancel(hdev, -err);
  981. usb_unanchor_urb(urb);
  982. }
  983. }
  984. static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
  985. {
  986. struct btusb_data *data = hci_get_drvdata(hdev);
  987. struct urb *urb;
  988. unsigned char *buf;
  989. unsigned int pipe;
  990. int err, size;
  991. BT_DBG("%s", hdev->name);
  992. if (!data->intr_ep)
  993. return -ENODEV;
  994. urb = usb_alloc_urb(0, mem_flags);
  995. if (!urb)
  996. return -ENOMEM;
  997. size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
  998. buf = kmalloc(size, mem_flags);
  999. if (!buf) {
  1000. usb_free_urb(urb);
  1001. return -ENOMEM;
  1002. }
  1003. pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
  1004. usb_fill_int_urb(urb, data->udev, pipe, buf, size,
  1005. btusb_intr_complete, hdev, data->intr_ep->bInterval);
  1006. urb->transfer_flags |= URB_FREE_BUFFER;
  1007. usb_anchor_urb(urb, &data->intr_anchor);
  1008. err = usb_submit_urb(urb, mem_flags);
  1009. if (err < 0) {
  1010. if (err != -EPERM && err != -ENODEV)
  1011. bt_dev_err(hdev, "urb %p submission failed (%d)",
  1012. urb, -err);
  1013. if (err != -EPERM)
  1014. hci_cmd_sync_cancel(hdev, -err);
  1015. usb_unanchor_urb(urb);
  1016. }
  1017. /* Only initialize intr_interval if URB poll sync is enabled */
  1018. if (!data->poll_sync)
  1019. goto done;
  1020. /* The units are frames (milliseconds) for full and low speed devices,
  1021. * and microframes (1/8 millisecond) for highspeed and SuperSpeed
  1022. * devices.
  1023. *
  1024. * This is done once on open/resume so it shouldn't change even if
  1025. * force_poll_sync changes.
  1026. */
  1027. switch (urb->dev->speed) {
  1028. case USB_SPEED_SUPER_PLUS:
  1029. case USB_SPEED_SUPER: /* units are 125us */
  1030. data->intr_interval = usecs_to_jiffies(urb->interval * 125);
  1031. break;
  1032. default:
  1033. data->intr_interval = msecs_to_jiffies(urb->interval);
  1034. break;
  1035. }
  1036. done:
  1037. usb_free_urb(urb);
  1038. return err;
  1039. }
  1040. static void btusb_bulk_complete(struct urb *urb)
  1041. {
  1042. struct hci_dev *hdev = urb->context;
  1043. struct btusb_data *data = hci_get_drvdata(hdev);
  1044. int err;
  1045. BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
  1046. urb->actual_length);
  1047. if (!test_bit(HCI_RUNNING, &hdev->flags))
  1048. return;
  1049. if (urb->status == 0) {
  1050. hdev->stat.byte_rx += urb->actual_length;
  1051. if (data->recv_bulk(data, urb->transfer_buffer,
  1052. urb->actual_length) < 0) {
  1053. bt_dev_err(hdev, "corrupted ACL packet");
  1054. hdev->stat.err_rx++;
  1055. }
  1056. } else if (urb->status == -ENOENT) {
  1057. /* Avoid suspend failed when usb_kill_urb */
  1058. return;
  1059. }
  1060. if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
  1061. return;
  1062. usb_anchor_urb(urb, &data->bulk_anchor);
  1063. usb_mark_last_busy(data->udev);
  1064. err = usb_submit_urb(urb, GFP_ATOMIC);
  1065. if (err < 0) {
  1066. /* -EPERM: urb is being killed;
  1067. * -ENODEV: device got disconnected
  1068. */
  1069. if (err != -EPERM && err != -ENODEV)
  1070. bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
  1071. urb, -err);
  1072. usb_unanchor_urb(urb);
  1073. }
  1074. }
  1075. static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
  1076. {
  1077. struct btusb_data *data = hci_get_drvdata(hdev);
  1078. struct urb *urb;
  1079. unsigned char *buf;
  1080. unsigned int pipe;
  1081. int err, size = HCI_MAX_FRAME_SIZE;
  1082. BT_DBG("%s", hdev->name);
  1083. if (!data->bulk_rx_ep)
  1084. return -ENODEV;
  1085. urb = usb_alloc_urb(0, mem_flags);
  1086. if (!urb)
  1087. return -ENOMEM;
  1088. buf = kmalloc(size, mem_flags);
  1089. if (!buf) {
  1090. usb_free_urb(urb);
  1091. return -ENOMEM;
  1092. }
  1093. pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
  1094. usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
  1095. btusb_bulk_complete, hdev);
  1096. urb->transfer_flags |= URB_FREE_BUFFER;
  1097. usb_mark_last_busy(data->udev);
  1098. usb_anchor_urb(urb, &data->bulk_anchor);
  1099. err = usb_submit_urb(urb, mem_flags);
  1100. if (err < 0) {
  1101. if (err != -EPERM && err != -ENODEV)
  1102. bt_dev_err(hdev, "urb %p submission failed (%d)",
  1103. urb, -err);
  1104. usb_unanchor_urb(urb);
  1105. }
  1106. usb_free_urb(urb);
  1107. return err;
  1108. }
  1109. static void btusb_isoc_complete(struct urb *urb)
  1110. {
  1111. struct hci_dev *hdev = urb->context;
  1112. struct btusb_data *data = hci_get_drvdata(hdev);
  1113. int i, err;
  1114. BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
  1115. urb->actual_length);
  1116. if (!test_bit(HCI_RUNNING, &hdev->flags))
  1117. return;
  1118. if (urb->status == 0) {
  1119. for (i = 0; i < urb->number_of_packets; i++) {
  1120. unsigned int offset = urb->iso_frame_desc[i].offset;
  1121. unsigned int length = urb->iso_frame_desc[i].actual_length;
  1122. if (urb->iso_frame_desc[i].status)
  1123. continue;
  1124. hdev->stat.byte_rx += length;
  1125. if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
  1126. length) < 0) {
  1127. bt_dev_err(hdev, "corrupted SCO packet");
  1128. hdev->stat.err_rx++;
  1129. }
  1130. }
  1131. } else if (urb->status == -ENOENT) {
  1132. /* Avoid suspend failed when usb_kill_urb */
  1133. return;
  1134. }
  1135. if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
  1136. return;
  1137. usb_anchor_urb(urb, &data->isoc_anchor);
  1138. err = usb_submit_urb(urb, GFP_ATOMIC);
  1139. if (err < 0) {
  1140. /* -EPERM: urb is being killed;
  1141. * -ENODEV: device got disconnected
  1142. */
  1143. if (err != -EPERM && err != -ENODEV)
  1144. bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
  1145. urb, -err);
  1146. usb_unanchor_urb(urb);
  1147. }
  1148. }
  1149. static inline void __fill_isoc_descriptor_msbc(struct urb *urb, int len,
  1150. int mtu, struct btusb_data *data)
  1151. {
  1152. int i, offset = 0;
  1153. unsigned int interval;
  1154. BT_DBG("len %d mtu %d", len, mtu);
  1155. /* For mSBC ALT 6 setting the host will send the packet at continuous
  1156. * flow. As per core spec 5, vol 4, part B, table 2.1. For ALT setting
  1157. * 6 the HCI PACKET INTERVAL should be 7.5ms for every usb packets.
  1158. * To maintain the rate we send 63bytes of usb packets alternatively for
  1159. * 7ms and 8ms to maintain the rate as 7.5ms.
  1160. */
  1161. if (data->usb_alt6_packet_flow) {
  1162. interval = 7;
  1163. data->usb_alt6_packet_flow = false;
  1164. } else {
  1165. interval = 6;
  1166. data->usb_alt6_packet_flow = true;
  1167. }
  1168. for (i = 0; i < interval; i++) {
  1169. urb->iso_frame_desc[i].offset = offset;
  1170. urb->iso_frame_desc[i].length = offset;
  1171. }
  1172. if (len && i < BTUSB_MAX_ISOC_FRAMES) {
  1173. urb->iso_frame_desc[i].offset = offset;
  1174. urb->iso_frame_desc[i].length = len;
  1175. i++;
  1176. }
  1177. urb->number_of_packets = i;
  1178. }
  1179. static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
  1180. {
  1181. int i, offset = 0;
  1182. BT_DBG("len %d mtu %d", len, mtu);
  1183. for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
  1184. i++, offset += mtu, len -= mtu) {
  1185. urb->iso_frame_desc[i].offset = offset;
  1186. urb->iso_frame_desc[i].length = mtu;
  1187. }
  1188. if (len && i < BTUSB_MAX_ISOC_FRAMES) {
  1189. urb->iso_frame_desc[i].offset = offset;
  1190. urb->iso_frame_desc[i].length = len;
  1191. i++;
  1192. }
  1193. urb->number_of_packets = i;
  1194. }
  1195. static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
  1196. {
  1197. struct btusb_data *data = hci_get_drvdata(hdev);
  1198. struct urb *urb;
  1199. unsigned char *buf;
  1200. unsigned int pipe;
  1201. int err, size;
  1202. BT_DBG("%s", hdev->name);
  1203. if (!data->isoc_rx_ep)
  1204. return -ENODEV;
  1205. urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
  1206. if (!urb)
  1207. return -ENOMEM;
  1208. size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
  1209. BTUSB_MAX_ISOC_FRAMES;
  1210. buf = kmalloc(size, mem_flags);
  1211. if (!buf) {
  1212. usb_free_urb(urb);
  1213. return -ENOMEM;
  1214. }
  1215. pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
  1216. usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
  1217. hdev, data->isoc_rx_ep->bInterval);
  1218. urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
  1219. __fill_isoc_descriptor(urb, size,
  1220. le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
  1221. usb_anchor_urb(urb, &data->isoc_anchor);
  1222. err = usb_submit_urb(urb, mem_flags);
  1223. if (err < 0) {
  1224. if (err != -EPERM && err != -ENODEV)
  1225. bt_dev_err(hdev, "urb %p submission failed (%d)",
  1226. urb, -err);
  1227. usb_unanchor_urb(urb);
  1228. }
  1229. usb_free_urb(urb);
  1230. return err;
  1231. }
  1232. static void btusb_diag_complete(struct urb *urb)
  1233. {
  1234. struct hci_dev *hdev = urb->context;
  1235. struct btusb_data *data = hci_get_drvdata(hdev);
  1236. int err;
  1237. BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
  1238. urb->actual_length);
  1239. if (urb->status == 0) {
  1240. struct sk_buff *skb;
  1241. skb = bt_skb_alloc(urb->actual_length, GFP_ATOMIC);
  1242. if (skb) {
  1243. skb_put_data(skb, urb->transfer_buffer,
  1244. urb->actual_length);
  1245. hci_recv_diag(hdev, skb);
  1246. }
  1247. } else if (urb->status == -ENOENT) {
  1248. /* Avoid suspend failed when usb_kill_urb */
  1249. return;
  1250. }
  1251. if (!test_bit(BTUSB_DIAG_RUNNING, &data->flags))
  1252. return;
  1253. usb_anchor_urb(urb, &data->diag_anchor);
  1254. usb_mark_last_busy(data->udev);
  1255. err = usb_submit_urb(urb, GFP_ATOMIC);
  1256. if (err < 0) {
  1257. /* -EPERM: urb is being killed;
  1258. * -ENODEV: device got disconnected
  1259. */
  1260. if (err != -EPERM && err != -ENODEV)
  1261. bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
  1262. urb, -err);
  1263. usb_unanchor_urb(urb);
  1264. }
  1265. }
  1266. static int btusb_submit_diag_urb(struct hci_dev *hdev, gfp_t mem_flags)
  1267. {
  1268. struct btusb_data *data = hci_get_drvdata(hdev);
  1269. struct urb *urb;
  1270. unsigned char *buf;
  1271. unsigned int pipe;
  1272. int err, size = HCI_MAX_FRAME_SIZE;
  1273. BT_DBG("%s", hdev->name);
  1274. if (!data->diag_rx_ep)
  1275. return -ENODEV;
  1276. urb = usb_alloc_urb(0, mem_flags);
  1277. if (!urb)
  1278. return -ENOMEM;
  1279. buf = kmalloc(size, mem_flags);
  1280. if (!buf) {
  1281. usb_free_urb(urb);
  1282. return -ENOMEM;
  1283. }
  1284. pipe = usb_rcvbulkpipe(data->udev, data->diag_rx_ep->bEndpointAddress);
  1285. usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
  1286. btusb_diag_complete, hdev);
  1287. urb->transfer_flags |= URB_FREE_BUFFER;
  1288. usb_mark_last_busy(data->udev);
  1289. usb_anchor_urb(urb, &data->diag_anchor);
  1290. err = usb_submit_urb(urb, mem_flags);
  1291. if (err < 0) {
  1292. if (err != -EPERM && err != -ENODEV)
  1293. bt_dev_err(hdev, "urb %p submission failed (%d)",
  1294. urb, -err);
  1295. usb_unanchor_urb(urb);
  1296. }
  1297. usb_free_urb(urb);
  1298. return err;
  1299. }
  1300. static void btusb_tx_complete(struct urb *urb)
  1301. {
  1302. struct sk_buff *skb = urb->context;
  1303. struct hci_dev *hdev = (struct hci_dev *)skb->dev;
  1304. struct btusb_data *data = hci_get_drvdata(hdev);
  1305. unsigned long flags;
  1306. BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
  1307. urb->actual_length);
  1308. if (!test_bit(HCI_RUNNING, &hdev->flags))
  1309. goto done;
  1310. if (!urb->status) {
  1311. hdev->stat.byte_tx += urb->transfer_buffer_length;
  1312. } else {
  1313. if (hci_skb_pkt_type(skb) == HCI_COMMAND_PKT)
  1314. hci_cmd_sync_cancel(hdev, -urb->status);
  1315. hdev->stat.err_tx++;
  1316. }
  1317. done:
  1318. spin_lock_irqsave(&data->txlock, flags);
  1319. data->tx_in_flight--;
  1320. spin_unlock_irqrestore(&data->txlock, flags);
  1321. kfree(urb->setup_packet);
  1322. kfree_skb(skb);
  1323. }
  1324. static void btusb_isoc_tx_complete(struct urb *urb)
  1325. {
  1326. struct sk_buff *skb = urb->context;
  1327. struct hci_dev *hdev = (struct hci_dev *)skb->dev;
  1328. BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
  1329. urb->actual_length);
  1330. if (!test_bit(HCI_RUNNING, &hdev->flags))
  1331. goto done;
  1332. if (!urb->status)
  1333. hdev->stat.byte_tx += urb->transfer_buffer_length;
  1334. else
  1335. hdev->stat.err_tx++;
  1336. done:
  1337. kfree(urb->setup_packet);
  1338. kfree_skb(skb);
  1339. }
  1340. static int btusb_open(struct hci_dev *hdev)
  1341. {
  1342. struct btusb_data *data = hci_get_drvdata(hdev);
  1343. int err;
  1344. BT_DBG("%s", hdev->name);
  1345. err = usb_autopm_get_interface(data->intf);
  1346. if (err < 0)
  1347. return err;
  1348. /* Patching USB firmware files prior to starting any URBs of HCI path
  1349. * It is more safe to use USB bulk channel for downloading USB patch
  1350. */
  1351. if (data->setup_on_usb) {
  1352. err = data->setup_on_usb(hdev);
  1353. if (err < 0)
  1354. goto setup_fail;
  1355. }
  1356. data->intf->needs_remote_wakeup = 1;
  1357. if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
  1358. goto done;
  1359. err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
  1360. if (err < 0)
  1361. goto failed;
  1362. err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
  1363. if (err < 0) {
  1364. usb_kill_anchored_urbs(&data->intr_anchor);
  1365. goto failed;
  1366. }
  1367. set_bit(BTUSB_BULK_RUNNING, &data->flags);
  1368. btusb_submit_bulk_urb(hdev, GFP_KERNEL);
  1369. if (data->diag) {
  1370. if (!btusb_submit_diag_urb(hdev, GFP_KERNEL))
  1371. set_bit(BTUSB_DIAG_RUNNING, &data->flags);
  1372. }
  1373. done:
  1374. usb_autopm_put_interface(data->intf);
  1375. return 0;
  1376. failed:
  1377. clear_bit(BTUSB_INTR_RUNNING, &data->flags);
  1378. setup_fail:
  1379. usb_autopm_put_interface(data->intf);
  1380. return err;
  1381. }
  1382. static void btusb_stop_traffic(struct btusb_data *data)
  1383. {
  1384. usb_kill_anchored_urbs(&data->intr_anchor);
  1385. usb_kill_anchored_urbs(&data->bulk_anchor);
  1386. usb_kill_anchored_urbs(&data->isoc_anchor);
  1387. usb_kill_anchored_urbs(&data->diag_anchor);
  1388. usb_kill_anchored_urbs(&data->ctrl_anchor);
  1389. }
  1390. static int btusb_close(struct hci_dev *hdev)
  1391. {
  1392. struct btusb_data *data = hci_get_drvdata(hdev);
  1393. int err;
  1394. BT_DBG("%s", hdev->name);
  1395. cancel_delayed_work(&data->rx_work);
  1396. cancel_work_sync(&data->work);
  1397. cancel_work_sync(&data->waker);
  1398. skb_queue_purge(&data->acl_q);
  1399. clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
  1400. clear_bit(BTUSB_BULK_RUNNING, &data->flags);
  1401. clear_bit(BTUSB_INTR_RUNNING, &data->flags);
  1402. clear_bit(BTUSB_DIAG_RUNNING, &data->flags);
  1403. btusb_stop_traffic(data);
  1404. btusb_free_frags(data);
  1405. err = usb_autopm_get_interface(data->intf);
  1406. if (err < 0)
  1407. goto failed;
  1408. data->intf->needs_remote_wakeup = 0;
  1409. /* Enable remote wake up for auto-suspend */
  1410. if (test_bit(BTUSB_WAKEUP_AUTOSUSPEND, &data->flags))
  1411. data->intf->needs_remote_wakeup = 1;
  1412. usb_autopm_put_interface(data->intf);
  1413. failed:
  1414. usb_scuttle_anchored_urbs(&data->deferred);
  1415. return 0;
  1416. }
  1417. static int btusb_flush(struct hci_dev *hdev)
  1418. {
  1419. struct btusb_data *data = hci_get_drvdata(hdev);
  1420. BT_DBG("%s", hdev->name);
  1421. cancel_delayed_work(&data->rx_work);
  1422. skb_queue_purge(&data->acl_q);
  1423. usb_kill_anchored_urbs(&data->tx_anchor);
  1424. btusb_free_frags(data);
  1425. return 0;
  1426. }
  1427. static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
  1428. {
  1429. struct btusb_data *data = hci_get_drvdata(hdev);
  1430. struct usb_ctrlrequest *dr;
  1431. struct urb *urb;
  1432. unsigned int pipe;
  1433. urb = usb_alloc_urb(0, GFP_KERNEL);
  1434. if (!urb)
  1435. return ERR_PTR(-ENOMEM);
  1436. dr = kmalloc(sizeof(*dr), GFP_KERNEL);
  1437. if (!dr) {
  1438. usb_free_urb(urb);
  1439. return ERR_PTR(-ENOMEM);
  1440. }
  1441. dr->bRequestType = data->cmdreq_type;
  1442. dr->bRequest = data->cmdreq;
  1443. dr->wIndex = 0;
  1444. dr->wValue = 0;
  1445. dr->wLength = __cpu_to_le16(skb->len);
  1446. pipe = usb_sndctrlpipe(data->udev, 0x00);
  1447. usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
  1448. skb->data, skb->len, btusb_tx_complete, skb);
  1449. skb->dev = (void *)hdev;
  1450. return urb;
  1451. }
  1452. static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
  1453. {
  1454. struct btusb_data *data = hci_get_drvdata(hdev);
  1455. struct urb *urb;
  1456. unsigned int pipe;
  1457. if (!data->bulk_tx_ep)
  1458. return ERR_PTR(-ENODEV);
  1459. urb = usb_alloc_urb(0, GFP_KERNEL);
  1460. if (!urb)
  1461. return ERR_PTR(-ENOMEM);
  1462. pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
  1463. usb_fill_bulk_urb(urb, data->udev, pipe,
  1464. skb->data, skb->len, btusb_tx_complete, skb);
  1465. skb->dev = (void *)hdev;
  1466. return urb;
  1467. }
  1468. static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
  1469. {
  1470. struct btusb_data *data = hci_get_drvdata(hdev);
  1471. struct urb *urb;
  1472. unsigned int pipe;
  1473. if (!data->isoc_tx_ep)
  1474. return ERR_PTR(-ENODEV);
  1475. urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
  1476. if (!urb)
  1477. return ERR_PTR(-ENOMEM);
  1478. pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
  1479. usb_fill_int_urb(urb, data->udev, pipe,
  1480. skb->data, skb->len, btusb_isoc_tx_complete,
  1481. skb, data->isoc_tx_ep->bInterval);
  1482. urb->transfer_flags = URB_ISO_ASAP;
  1483. if (data->isoc_altsetting == 6)
  1484. __fill_isoc_descriptor_msbc(urb, skb->len,
  1485. le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize),
  1486. data);
  1487. else
  1488. __fill_isoc_descriptor(urb, skb->len,
  1489. le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
  1490. skb->dev = (void *)hdev;
  1491. return urb;
  1492. }
  1493. static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
  1494. {
  1495. struct btusb_data *data = hci_get_drvdata(hdev);
  1496. int err;
  1497. usb_anchor_urb(urb, &data->tx_anchor);
  1498. err = usb_submit_urb(urb, GFP_KERNEL);
  1499. if (err < 0) {
  1500. if (err != -EPERM && err != -ENODEV)
  1501. bt_dev_err(hdev, "urb %p submission failed (%d)",
  1502. urb, -err);
  1503. kfree(urb->setup_packet);
  1504. usb_unanchor_urb(urb);
  1505. } else {
  1506. usb_mark_last_busy(data->udev);
  1507. }
  1508. usb_free_urb(urb);
  1509. return err;
  1510. }
  1511. static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
  1512. {
  1513. struct btusb_data *data = hci_get_drvdata(hdev);
  1514. unsigned long flags;
  1515. bool suspending;
  1516. spin_lock_irqsave(&data->txlock, flags);
  1517. suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
  1518. if (!suspending)
  1519. data->tx_in_flight++;
  1520. spin_unlock_irqrestore(&data->txlock, flags);
  1521. if (!suspending)
  1522. return submit_tx_urb(hdev, urb);
  1523. usb_anchor_urb(urb, &data->deferred);
  1524. schedule_work(&data->waker);
  1525. usb_free_urb(urb);
  1526. return 0;
  1527. }
  1528. static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
  1529. {
  1530. struct urb *urb;
  1531. BT_DBG("%s", hdev->name);
  1532. switch (hci_skb_pkt_type(skb)) {
  1533. case HCI_COMMAND_PKT:
  1534. urb = alloc_ctrl_urb(hdev, skb);
  1535. if (IS_ERR(urb))
  1536. return PTR_ERR(urb);
  1537. hdev->stat.cmd_tx++;
  1538. return submit_or_queue_tx_urb(hdev, urb);
  1539. case HCI_ACLDATA_PKT:
  1540. urb = alloc_bulk_urb(hdev, skb);
  1541. if (IS_ERR(urb))
  1542. return PTR_ERR(urb);
  1543. hdev->stat.acl_tx++;
  1544. return submit_or_queue_tx_urb(hdev, urb);
  1545. case HCI_SCODATA_PKT:
  1546. if (hci_conn_num(hdev, SCO_LINK) < 1)
  1547. return -ENODEV;
  1548. urb = alloc_isoc_urb(hdev, skb);
  1549. if (IS_ERR(urb))
  1550. return PTR_ERR(urb);
  1551. hdev->stat.sco_tx++;
  1552. return submit_tx_urb(hdev, urb);
  1553. case HCI_ISODATA_PKT:
  1554. urb = alloc_bulk_urb(hdev, skb);
  1555. if (IS_ERR(urb))
  1556. return PTR_ERR(urb);
  1557. return submit_or_queue_tx_urb(hdev, urb);
  1558. }
  1559. return -EILSEQ;
  1560. }
  1561. static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
  1562. {
  1563. struct btusb_data *data = hci_get_drvdata(hdev);
  1564. BT_DBG("%s evt %d", hdev->name, evt);
  1565. if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
  1566. data->sco_num = hci_conn_num(hdev, SCO_LINK);
  1567. data->air_mode = evt;
  1568. schedule_work(&data->work);
  1569. }
  1570. }
  1571. static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
  1572. {
  1573. struct btusb_data *data = hci_get_drvdata(hdev);
  1574. struct usb_interface *intf = data->isoc;
  1575. struct usb_endpoint_descriptor *ep_desc;
  1576. int i, err;
  1577. if (!data->isoc)
  1578. return -ENODEV;
  1579. err = usb_set_interface(data->udev, data->isoc_ifnum, altsetting);
  1580. if (err < 0) {
  1581. bt_dev_err(hdev, "setting interface failed (%d)", -err);
  1582. return err;
  1583. }
  1584. data->isoc_altsetting = altsetting;
  1585. data->isoc_tx_ep = NULL;
  1586. data->isoc_rx_ep = NULL;
  1587. for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
  1588. ep_desc = &intf->cur_altsetting->endpoint[i].desc;
  1589. if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
  1590. data->isoc_tx_ep = ep_desc;
  1591. continue;
  1592. }
  1593. if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
  1594. data->isoc_rx_ep = ep_desc;
  1595. continue;
  1596. }
  1597. }
  1598. if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
  1599. bt_dev_err(hdev, "invalid SCO descriptors");
  1600. return -ENODEV;
  1601. }
  1602. return 0;
  1603. }
  1604. static int btusb_switch_alt_setting(struct hci_dev *hdev, int new_alts)
  1605. {
  1606. struct btusb_data *data = hci_get_drvdata(hdev);
  1607. int err;
  1608. if (data->isoc_altsetting != new_alts) {
  1609. unsigned long flags;
  1610. clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
  1611. usb_kill_anchored_urbs(&data->isoc_anchor);
  1612. /* When isochronous alternate setting needs to be
  1613. * changed, because SCO connection has been added
  1614. * or removed, a packet fragment may be left in the
  1615. * reassembling state. This could lead to wrongly
  1616. * assembled fragments.
  1617. *
  1618. * Clear outstanding fragment when selecting a new
  1619. * alternate setting.
  1620. */
  1621. spin_lock_irqsave(&data->rxlock, flags);
  1622. dev_kfree_skb_irq(data->sco_skb);
  1623. data->sco_skb = NULL;
  1624. spin_unlock_irqrestore(&data->rxlock, flags);
  1625. err = __set_isoc_interface(hdev, new_alts);
  1626. if (err < 0)
  1627. return err;
  1628. }
  1629. if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
  1630. if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
  1631. clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
  1632. else
  1633. btusb_submit_isoc_urb(hdev, GFP_KERNEL);
  1634. }
  1635. return 0;
  1636. }
  1637. static struct usb_host_interface *btusb_find_altsetting(struct btusb_data *data,
  1638. int alt)
  1639. {
  1640. struct usb_interface *intf = data->isoc;
  1641. int i;
  1642. BT_DBG("Looking for Alt no :%d", alt);
  1643. if (!intf)
  1644. return NULL;
  1645. for (i = 0; i < intf->num_altsetting; i++) {
  1646. if (intf->altsetting[i].desc.bAlternateSetting == alt)
  1647. return &intf->altsetting[i];
  1648. }
  1649. return NULL;
  1650. }
  1651. static void btusb_work(struct work_struct *work)
  1652. {
  1653. struct btusb_data *data = container_of(work, struct btusb_data, work);
  1654. struct hci_dev *hdev = data->hdev;
  1655. int new_alts = 0;
  1656. int err;
  1657. if (data->sco_num > 0) {
  1658. if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
  1659. err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
  1660. if (err < 0) {
  1661. clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
  1662. usb_kill_anchored_urbs(&data->isoc_anchor);
  1663. return;
  1664. }
  1665. set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
  1666. }
  1667. if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_CVSD) {
  1668. if (hdev->voice_setting & 0x0020) {
  1669. static const int alts[3] = { 2, 4, 5 };
  1670. new_alts = alts[data->sco_num - 1];
  1671. } else {
  1672. new_alts = data->sco_num;
  1673. }
  1674. } else if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_TRANSP) {
  1675. /* Bluetooth USB spec recommends alt 6 (63 bytes), but
  1676. * many adapters do not support it. Alt 1 appears to
  1677. * work for all adapters that do not have alt 6, and
  1678. * which work with WBS at all. Some devices prefer
  1679. * alt 3 (HCI payload >= 60 Bytes let air packet
  1680. * data satisfy 60 bytes), requiring
  1681. * MTU >= 3 (packets) * 25 (size) - 3 (headers) = 72
  1682. * see also Core spec 5, vol 4, B 2.1.1 & Table 2.1.
  1683. */
  1684. if (btusb_find_altsetting(data, 6))
  1685. new_alts = 6;
  1686. else if (btusb_find_altsetting(data, 3) &&
  1687. hdev->sco_mtu >= 72 &&
  1688. test_bit(BTUSB_USE_ALT3_FOR_WBS, &data->flags))
  1689. new_alts = 3;
  1690. else
  1691. new_alts = 1;
  1692. }
  1693. if (btusb_switch_alt_setting(hdev, new_alts) < 0)
  1694. bt_dev_err(hdev, "set USB alt:(%d) failed!", new_alts);
  1695. } else {
  1696. clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
  1697. usb_kill_anchored_urbs(&data->isoc_anchor);
  1698. __set_isoc_interface(hdev, 0);
  1699. if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
  1700. usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
  1701. }
  1702. }
  1703. static void btusb_waker(struct work_struct *work)
  1704. {
  1705. struct btusb_data *data = container_of(work, struct btusb_data, waker);
  1706. int err;
  1707. err = usb_autopm_get_interface(data->intf);
  1708. if (err < 0)
  1709. return;
  1710. usb_autopm_put_interface(data->intf);
  1711. }
  1712. static void btusb_rx_work(struct work_struct *work)
  1713. {
  1714. struct btusb_data *data = container_of(work, struct btusb_data,
  1715. rx_work.work);
  1716. struct sk_buff *skb;
  1717. /* Dequeue ACL data received during the interval */
  1718. while ((skb = skb_dequeue(&data->acl_q)))
  1719. data->recv_acl(data->hdev, skb);
  1720. }
  1721. static int btusb_setup_bcm92035(struct hci_dev *hdev)
  1722. {
  1723. struct sk_buff *skb;
  1724. u8 val = 0x00;
  1725. BT_DBG("%s", hdev->name);
  1726. skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
  1727. if (IS_ERR(skb))
  1728. bt_dev_err(hdev, "BCM92035 command failed (%ld)", PTR_ERR(skb));
  1729. else
  1730. kfree_skb(skb);
  1731. return 0;
  1732. }
  1733. static int btusb_setup_csr(struct hci_dev *hdev)
  1734. {
  1735. struct btusb_data *data = hci_get_drvdata(hdev);
  1736. u16 bcdDevice = le16_to_cpu(data->udev->descriptor.bcdDevice);
  1737. struct hci_rp_read_local_version *rp;
  1738. struct sk_buff *skb;
  1739. bool is_fake = false;
  1740. int ret;
  1741. BT_DBG("%s", hdev->name);
  1742. skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
  1743. HCI_INIT_TIMEOUT);
  1744. if (IS_ERR(skb)) {
  1745. int err = PTR_ERR(skb);
  1746. bt_dev_err(hdev, "CSR: Local version failed (%d)", err);
  1747. return err;
  1748. }
  1749. if (skb->len != sizeof(struct hci_rp_read_local_version)) {
  1750. bt_dev_err(hdev, "CSR: Local version length mismatch");
  1751. kfree_skb(skb);
  1752. return -EIO;
  1753. }
  1754. rp = (struct hci_rp_read_local_version *)skb->data;
  1755. bt_dev_info(hdev, "CSR: Setting up dongle with HCI ver=%u rev=%04x; LMP ver=%u subver=%04x; manufacturer=%u",
  1756. le16_to_cpu(rp->hci_ver), le16_to_cpu(rp->hci_rev),
  1757. le16_to_cpu(rp->lmp_ver), le16_to_cpu(rp->lmp_subver),
  1758. le16_to_cpu(rp->manufacturer));
  1759. /* Detect a wide host of Chinese controllers that aren't CSR.
  1760. *
  1761. * Known fake bcdDevices: 0x0100, 0x0134, 0x1915, 0x2520, 0x7558, 0x8891
  1762. *
  1763. * The main thing they have in common is that these are really popular low-cost
  1764. * options that support newer Bluetooth versions but rely on heavy VID/PID
  1765. * squatting of this poor old Bluetooth 1.1 device. Even sold as such.
  1766. *
  1767. * We detect actual CSR devices by checking that the HCI manufacturer code
  1768. * is Cambridge Silicon Radio (10) and ensuring that LMP sub-version and
  1769. * HCI rev values always match. As they both store the firmware number.
  1770. */
  1771. if (le16_to_cpu(rp->manufacturer) != 10 ||
  1772. le16_to_cpu(rp->hci_rev) != le16_to_cpu(rp->lmp_subver))
  1773. is_fake = true;
  1774. /* Known legit CSR firmware build numbers and their supported BT versions:
  1775. * - 1.1 (0x1) -> 0x0073, 0x020d, 0x033c, 0x034e
  1776. * - 1.2 (0x2) -> 0x04d9, 0x0529
  1777. * - 2.0 (0x3) -> 0x07a6, 0x07ad, 0x0c5c
  1778. * - 2.1 (0x4) -> 0x149c, 0x1735, 0x1899 (0x1899 is a BlueCore4-External)
  1779. * - 4.0 (0x6) -> 0x1d86, 0x2031, 0x22bb
  1780. *
  1781. * e.g. Real CSR dongles with LMP subversion 0x73 are old enough that
  1782. * support BT 1.1 only; so it's a dead giveaway when some
  1783. * third-party BT 4.0 dongle reuses it.
  1784. */
  1785. else if (le16_to_cpu(rp->lmp_subver) <= 0x034e &&
  1786. le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_1_1)
  1787. is_fake = true;
  1788. else if (le16_to_cpu(rp->lmp_subver) <= 0x0529 &&
  1789. le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_1_2)
  1790. is_fake = true;
  1791. else if (le16_to_cpu(rp->lmp_subver) <= 0x0c5c &&
  1792. le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_2_0)
  1793. is_fake = true;
  1794. else if (le16_to_cpu(rp->lmp_subver) <= 0x1899 &&
  1795. le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_2_1)
  1796. is_fake = true;
  1797. else if (le16_to_cpu(rp->lmp_subver) <= 0x22bb &&
  1798. le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_4_0)
  1799. is_fake = true;
  1800. /* Other clones which beat all the above checks */
  1801. else if (bcdDevice == 0x0134 &&
  1802. le16_to_cpu(rp->lmp_subver) == 0x0c5c &&
  1803. le16_to_cpu(rp->hci_ver) == BLUETOOTH_VER_2_0)
  1804. is_fake = true;
  1805. if (is_fake) {
  1806. bt_dev_warn(hdev, "CSR: Unbranded CSR clone detected; adding workarounds and force-suspending once...");
  1807. /* Generally these clones have big discrepancies between
  1808. * advertised features and what's actually supported.
  1809. * Probably will need to be expanded in the future;
  1810. * without these the controller will lock up.
  1811. */
  1812. set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
  1813. set_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks);
  1814. set_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks);
  1815. set_bit(HCI_QUIRK_NO_SUSPEND_NOTIFIER, &hdev->quirks);
  1816. /* Clear the reset quirk since this is not an actual
  1817. * early Bluetooth 1.1 device from CSR.
  1818. */
  1819. clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
  1820. clear_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
  1821. /*
  1822. * Special workaround for these BT 4.0 chip clones, and potentially more:
  1823. *
  1824. * - 0x0134: a Barrot 8041a02 (HCI rev: 0x0810 sub: 0x1012)
  1825. * - 0x7558: IC markings FR3191AHAL 749H15143 (HCI rev/sub-version: 0x0709)
  1826. *
  1827. * These controllers are really messed-up.
  1828. *
  1829. * 1. Their bulk RX endpoint will never report any data unless
  1830. * the device was suspended at least once (yes, really).
  1831. * 2. They will not wakeup when autosuspended and receiving data
  1832. * on their bulk RX endpoint from e.g. a keyboard or mouse
  1833. * (IOW remote-wakeup support is broken for the bulk endpoint).
  1834. *
  1835. * To fix 1. enable runtime-suspend, force-suspend the
  1836. * HCI and then wake-it up by disabling runtime-suspend.
  1837. *
  1838. * To fix 2. clear the HCI's can_wake flag, this way the HCI
  1839. * will still be autosuspended when it is not open.
  1840. *
  1841. * --
  1842. *
  1843. * Because these are widespread problems we prefer generic solutions; so
  1844. * apply this initialization quirk to every controller that gets here,
  1845. * it should be harmless. The alternative is to not work at all.
  1846. */
  1847. pm_runtime_allow(&data->udev->dev);
  1848. ret = pm_runtime_suspend(&data->udev->dev);
  1849. if (ret >= 0)
  1850. msleep(200);
  1851. else
  1852. bt_dev_warn(hdev, "CSR: Couldn't suspend the device for our Barrot 8041a02 receive-issue workaround");
  1853. pm_runtime_forbid(&data->udev->dev);
  1854. device_set_wakeup_capable(&data->udev->dev, false);
  1855. /* Re-enable autosuspend if this was requested */
  1856. if (enable_autosuspend)
  1857. usb_enable_autosuspend(data->udev);
  1858. }
  1859. kfree_skb(skb);
  1860. return 0;
  1861. }
  1862. static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
  1863. {
  1864. struct sk_buff *skb;
  1865. struct hci_event_hdr *hdr;
  1866. struct hci_ev_cmd_complete *evt;
  1867. skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_KERNEL);
  1868. if (!skb)
  1869. return -ENOMEM;
  1870. hdr = skb_put(skb, sizeof(*hdr));
  1871. hdr->evt = HCI_EV_CMD_COMPLETE;
  1872. hdr->plen = sizeof(*evt) + 1;
  1873. evt = skb_put(skb, sizeof(*evt));
  1874. evt->ncmd = 0x01;
  1875. evt->opcode = cpu_to_le16(opcode);
  1876. skb_put_u8(skb, 0x00);
  1877. hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
  1878. return hci_recv_frame(hdev, skb);
  1879. }
  1880. static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
  1881. int count)
  1882. {
  1883. struct hci_dev *hdev = data->hdev;
  1884. /* When the device is in bootloader mode, then it can send
  1885. * events via the bulk endpoint. These events are treated the
  1886. * same way as the ones received from the interrupt endpoint.
  1887. */
  1888. if (btintel_test_flag(hdev, INTEL_BOOTLOADER))
  1889. return btusb_recv_intr(data, buffer, count);
  1890. return btusb_recv_bulk(data, buffer, count);
  1891. }
  1892. static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
  1893. {
  1894. if (btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
  1895. struct hci_event_hdr *hdr = (void *)skb->data;
  1896. if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
  1897. hdr->plen > 0) {
  1898. const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
  1899. unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
  1900. switch (skb->data[2]) {
  1901. case 0x02:
  1902. /* When switching to the operational firmware
  1903. * the device sends a vendor specific event
  1904. * indicating that the bootup completed.
  1905. */
  1906. btintel_bootup(hdev, ptr, len);
  1907. break;
  1908. case 0x06:
  1909. /* When the firmware loading completes the
  1910. * device sends out a vendor specific event
  1911. * indicating the result of the firmware
  1912. * loading.
  1913. */
  1914. btintel_secure_send_result(hdev, ptr, len);
  1915. break;
  1916. }
  1917. }
  1918. }
  1919. return hci_recv_frame(hdev, skb);
  1920. }
  1921. static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
  1922. {
  1923. struct urb *urb;
  1924. BT_DBG("%s", hdev->name);
  1925. switch (hci_skb_pkt_type(skb)) {
  1926. case HCI_COMMAND_PKT:
  1927. if (btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
  1928. struct hci_command_hdr *cmd = (void *)skb->data;
  1929. __u16 opcode = le16_to_cpu(cmd->opcode);
  1930. /* When in bootloader mode and the command 0xfc09
  1931. * is received, it needs to be send down the
  1932. * bulk endpoint. So allocate a bulk URB instead.
  1933. */
  1934. if (opcode == 0xfc09)
  1935. urb = alloc_bulk_urb(hdev, skb);
  1936. else
  1937. urb = alloc_ctrl_urb(hdev, skb);
  1938. /* When the 0xfc01 command is issued to boot into
  1939. * the operational firmware, it will actually not
  1940. * send a command complete event. To keep the flow
  1941. * control working inject that event here.
  1942. */
  1943. if (opcode == 0xfc01)
  1944. inject_cmd_complete(hdev, opcode);
  1945. } else {
  1946. urb = alloc_ctrl_urb(hdev, skb);
  1947. }
  1948. if (IS_ERR(urb))
  1949. return PTR_ERR(urb);
  1950. hdev->stat.cmd_tx++;
  1951. return submit_or_queue_tx_urb(hdev, urb);
  1952. case HCI_ACLDATA_PKT:
  1953. urb = alloc_bulk_urb(hdev, skb);
  1954. if (IS_ERR(urb))
  1955. return PTR_ERR(urb);
  1956. hdev->stat.acl_tx++;
  1957. return submit_or_queue_tx_urb(hdev, urb);
  1958. case HCI_SCODATA_PKT:
  1959. if (hci_conn_num(hdev, SCO_LINK) < 1)
  1960. return -ENODEV;
  1961. urb = alloc_isoc_urb(hdev, skb);
  1962. if (IS_ERR(urb))
  1963. return PTR_ERR(urb);
  1964. hdev->stat.sco_tx++;
  1965. return submit_tx_urb(hdev, urb);
  1966. case HCI_ISODATA_PKT:
  1967. urb = alloc_bulk_urb(hdev, skb);
  1968. if (IS_ERR(urb))
  1969. return PTR_ERR(urb);
  1970. return submit_or_queue_tx_urb(hdev, urb);
  1971. }
  1972. return -EILSEQ;
  1973. }
  1974. /* UHW CR mapping */
  1975. #define MTK_BT_MISC 0x70002510
  1976. #define MTK_BT_SUBSYS_RST 0x70002610
  1977. #define MTK_UDMA_INT_STA_BT 0x74000024
  1978. #define MTK_UDMA_INT_STA_BT1 0x74000308
  1979. #define MTK_BT_WDT_STATUS 0x740003A0
  1980. #define MTK_EP_RST_OPT 0x74011890
  1981. #define MTK_EP_RST_IN_OUT_OPT 0x00010001
  1982. #define MTK_BT_RST_DONE 0x00000100
  1983. #define MTK_BT_RESET_WAIT_MS 100
  1984. #define MTK_BT_RESET_NUM_TRIES 10
  1985. static void btusb_mtk_wmt_recv(struct urb *urb)
  1986. {
  1987. struct hci_dev *hdev = urb->context;
  1988. struct btusb_data *data = hci_get_drvdata(hdev);
  1989. struct sk_buff *skb;
  1990. int err;
  1991. if (urb->status == 0 && urb->actual_length > 0) {
  1992. hdev->stat.byte_rx += urb->actual_length;
  1993. /* WMT event shouldn't be fragmented and the size should be
  1994. * less than HCI_WMT_MAX_EVENT_SIZE.
  1995. */
  1996. skb = bt_skb_alloc(HCI_WMT_MAX_EVENT_SIZE, GFP_ATOMIC);
  1997. if (!skb) {
  1998. hdev->stat.err_rx++;
  1999. kfree(urb->setup_packet);
  2000. return;
  2001. }
  2002. hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
  2003. skb_put_data(skb, urb->transfer_buffer, urb->actual_length);
  2004. /* When someone waits for the WMT event, the skb is being cloned
  2005. * and being processed the events from there then.
  2006. */
  2007. if (test_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags)) {
  2008. data->evt_skb = skb_clone(skb, GFP_ATOMIC);
  2009. if (!data->evt_skb) {
  2010. kfree_skb(skb);
  2011. kfree(urb->setup_packet);
  2012. return;
  2013. }
  2014. }
  2015. err = hci_recv_frame(hdev, skb);
  2016. if (err < 0) {
  2017. kfree_skb(data->evt_skb);
  2018. data->evt_skb = NULL;
  2019. kfree(urb->setup_packet);
  2020. return;
  2021. }
  2022. if (test_and_clear_bit(BTUSB_TX_WAIT_VND_EVT,
  2023. &data->flags)) {
  2024. /* Barrier to sync with other CPUs */
  2025. smp_mb__after_atomic();
  2026. wake_up_bit(&data->flags,
  2027. BTUSB_TX_WAIT_VND_EVT);
  2028. }
  2029. kfree(urb->setup_packet);
  2030. return;
  2031. } else if (urb->status == -ENOENT) {
  2032. /* Avoid suspend failed when usb_kill_urb */
  2033. return;
  2034. }
  2035. usb_mark_last_busy(data->udev);
  2036. /* The URB complete handler is still called with urb->actual_length = 0
  2037. * when the event is not available, so we should keep re-submitting
  2038. * URB until WMT event returns, Also, It's necessary to wait some time
  2039. * between the two consecutive control URBs to relax the target device
  2040. * to generate the event. Otherwise, the WMT event cannot return from
  2041. * the device successfully.
  2042. */
  2043. udelay(500);
  2044. usb_anchor_urb(urb, &data->ctrl_anchor);
  2045. err = usb_submit_urb(urb, GFP_ATOMIC);
  2046. if (err < 0) {
  2047. kfree(urb->setup_packet);
  2048. /* -EPERM: urb is being killed;
  2049. * -ENODEV: device got disconnected
  2050. */
  2051. if (err != -EPERM && err != -ENODEV)
  2052. bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
  2053. urb, -err);
  2054. usb_unanchor_urb(urb);
  2055. }
  2056. }
  2057. static int btusb_mtk_submit_wmt_recv_urb(struct hci_dev *hdev)
  2058. {
  2059. struct btusb_data *data = hci_get_drvdata(hdev);
  2060. struct usb_ctrlrequest *dr;
  2061. unsigned char *buf;
  2062. int err, size = 64;
  2063. unsigned int pipe;
  2064. struct urb *urb;
  2065. urb = usb_alloc_urb(0, GFP_KERNEL);
  2066. if (!urb)
  2067. return -ENOMEM;
  2068. dr = kmalloc(sizeof(*dr), GFP_KERNEL);
  2069. if (!dr) {
  2070. usb_free_urb(urb);
  2071. return -ENOMEM;
  2072. }
  2073. dr->bRequestType = USB_TYPE_VENDOR | USB_DIR_IN;
  2074. dr->bRequest = 1;
  2075. dr->wIndex = cpu_to_le16(0);
  2076. dr->wValue = cpu_to_le16(48);
  2077. dr->wLength = cpu_to_le16(size);
  2078. buf = kmalloc(size, GFP_KERNEL);
  2079. if (!buf) {
  2080. kfree(dr);
  2081. usb_free_urb(urb);
  2082. return -ENOMEM;
  2083. }
  2084. pipe = usb_rcvctrlpipe(data->udev, 0);
  2085. usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
  2086. buf, size, btusb_mtk_wmt_recv, hdev);
  2087. urb->transfer_flags |= URB_FREE_BUFFER;
  2088. usb_anchor_urb(urb, &data->ctrl_anchor);
  2089. err = usb_submit_urb(urb, GFP_KERNEL);
  2090. if (err < 0) {
  2091. if (err != -EPERM && err != -ENODEV)
  2092. bt_dev_err(hdev, "urb %p submission failed (%d)",
  2093. urb, -err);
  2094. usb_unanchor_urb(urb);
  2095. }
  2096. usb_free_urb(urb);
  2097. return err;
  2098. }
  2099. static int btusb_mtk_hci_wmt_sync(struct hci_dev *hdev,
  2100. struct btmtk_hci_wmt_params *wmt_params)
  2101. {
  2102. struct btusb_data *data = hci_get_drvdata(hdev);
  2103. struct btmtk_hci_wmt_evt_funcc *wmt_evt_funcc;
  2104. u32 hlen, status = BTMTK_WMT_INVALID;
  2105. struct btmtk_hci_wmt_evt *wmt_evt;
  2106. struct btmtk_hci_wmt_cmd *wc;
  2107. struct btmtk_wmt_hdr *hdr;
  2108. int err;
  2109. /* Send the WMT command and wait until the WMT event returns */
  2110. hlen = sizeof(*hdr) + wmt_params->dlen;
  2111. if (hlen > 255)
  2112. return -EINVAL;
  2113. wc = kzalloc(hlen, GFP_KERNEL);
  2114. if (!wc)
  2115. return -ENOMEM;
  2116. hdr = &wc->hdr;
  2117. hdr->dir = 1;
  2118. hdr->op = wmt_params->op;
  2119. hdr->dlen = cpu_to_le16(wmt_params->dlen + 1);
  2120. hdr->flag = wmt_params->flag;
  2121. memcpy(wc->data, wmt_params->data, wmt_params->dlen);
  2122. set_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
  2123. /* WMT cmd/event doesn't follow up the generic HCI cmd/event handling,
  2124. * it needs constantly polling control pipe until the host received the
  2125. * WMT event, thus, we should require to specifically acquire PM counter
  2126. * on the USB to prevent the interface from entering auto suspended
  2127. * while WMT cmd/event in progress.
  2128. */
  2129. err = usb_autopm_get_interface(data->intf);
  2130. if (err < 0)
  2131. goto err_free_wc;
  2132. err = __hci_cmd_send(hdev, 0xfc6f, hlen, wc);
  2133. if (err < 0) {
  2134. clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
  2135. usb_autopm_put_interface(data->intf);
  2136. goto err_free_wc;
  2137. }
  2138. /* Submit control IN URB on demand to process the WMT event */
  2139. err = btusb_mtk_submit_wmt_recv_urb(hdev);
  2140. usb_autopm_put_interface(data->intf);
  2141. if (err < 0)
  2142. goto err_free_wc;
  2143. /* The vendor specific WMT commands are all answered by a vendor
  2144. * specific event and will have the Command Status or Command
  2145. * Complete as with usual HCI command flow control.
  2146. *
  2147. * After sending the command, wait for BTUSB_TX_WAIT_VND_EVT
  2148. * state to be cleared. The driver specific event receive routine
  2149. * will clear that state and with that indicate completion of the
  2150. * WMT command.
  2151. */
  2152. err = wait_on_bit_timeout(&data->flags, BTUSB_TX_WAIT_VND_EVT,
  2153. TASK_INTERRUPTIBLE, HCI_INIT_TIMEOUT);
  2154. if (err == -EINTR) {
  2155. bt_dev_err(hdev, "Execution of wmt command interrupted");
  2156. clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
  2157. goto err_free_wc;
  2158. }
  2159. if (err) {
  2160. bt_dev_err(hdev, "Execution of wmt command timed out");
  2161. clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
  2162. err = -ETIMEDOUT;
  2163. goto err_free_wc;
  2164. }
  2165. if (data->evt_skb == NULL)
  2166. goto err_free_wc;
  2167. /* Parse and handle the return WMT event */
  2168. wmt_evt = (struct btmtk_hci_wmt_evt *)data->evt_skb->data;
  2169. if (wmt_evt->whdr.op != hdr->op) {
  2170. bt_dev_err(hdev, "Wrong op received %d expected %d",
  2171. wmt_evt->whdr.op, hdr->op);
  2172. err = -EIO;
  2173. goto err_free_skb;
  2174. }
  2175. switch (wmt_evt->whdr.op) {
  2176. case BTMTK_WMT_SEMAPHORE:
  2177. if (wmt_evt->whdr.flag == 2)
  2178. status = BTMTK_WMT_PATCH_UNDONE;
  2179. else
  2180. status = BTMTK_WMT_PATCH_DONE;
  2181. break;
  2182. case BTMTK_WMT_FUNC_CTRL:
  2183. wmt_evt_funcc = (struct btmtk_hci_wmt_evt_funcc *)wmt_evt;
  2184. if (be16_to_cpu(wmt_evt_funcc->status) == 0x404)
  2185. status = BTMTK_WMT_ON_DONE;
  2186. else if (be16_to_cpu(wmt_evt_funcc->status) == 0x420)
  2187. status = BTMTK_WMT_ON_PROGRESS;
  2188. else
  2189. status = BTMTK_WMT_ON_UNDONE;
  2190. break;
  2191. case BTMTK_WMT_PATCH_DWNLD:
  2192. if (wmt_evt->whdr.flag == 2)
  2193. status = BTMTK_WMT_PATCH_DONE;
  2194. else if (wmt_evt->whdr.flag == 1)
  2195. status = BTMTK_WMT_PATCH_PROGRESS;
  2196. else
  2197. status = BTMTK_WMT_PATCH_UNDONE;
  2198. break;
  2199. }
  2200. if (wmt_params->status)
  2201. *wmt_params->status = status;
  2202. err_free_skb:
  2203. kfree_skb(data->evt_skb);
  2204. data->evt_skb = NULL;
  2205. err_free_wc:
  2206. kfree(wc);
  2207. return err;
  2208. }
  2209. static int btusb_mtk_func_query(struct hci_dev *hdev)
  2210. {
  2211. struct btmtk_hci_wmt_params wmt_params;
  2212. int status, err;
  2213. u8 param = 0;
  2214. /* Query whether the function is enabled */
  2215. wmt_params.op = BTMTK_WMT_FUNC_CTRL;
  2216. wmt_params.flag = 4;
  2217. wmt_params.dlen = sizeof(param);
  2218. wmt_params.data = &param;
  2219. wmt_params.status = &status;
  2220. err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
  2221. if (err < 0) {
  2222. bt_dev_err(hdev, "Failed to query function status (%d)", err);
  2223. return err;
  2224. }
  2225. return status;
  2226. }
  2227. static int btusb_mtk_uhw_reg_write(struct btusb_data *data, u32 reg, u32 val)
  2228. {
  2229. struct hci_dev *hdev = data->hdev;
  2230. int pipe, err;
  2231. void *buf;
  2232. buf = kzalloc(4, GFP_KERNEL);
  2233. if (!buf)
  2234. return -ENOMEM;
  2235. put_unaligned_le32(val, buf);
  2236. pipe = usb_sndctrlpipe(data->udev, 0);
  2237. err = usb_control_msg(data->udev, pipe, 0x02,
  2238. 0x5E,
  2239. reg >> 16, reg & 0xffff,
  2240. buf, 4, USB_CTRL_SET_TIMEOUT);
  2241. if (err < 0) {
  2242. bt_dev_err(hdev, "Failed to write uhw reg(%d)", err);
  2243. goto err_free_buf;
  2244. }
  2245. err_free_buf:
  2246. kfree(buf);
  2247. return err;
  2248. }
  2249. static int btusb_mtk_uhw_reg_read(struct btusb_data *data, u32 reg, u32 *val)
  2250. {
  2251. struct hci_dev *hdev = data->hdev;
  2252. int pipe, err;
  2253. void *buf;
  2254. buf = kzalloc(4, GFP_KERNEL);
  2255. if (!buf)
  2256. return -ENOMEM;
  2257. pipe = usb_rcvctrlpipe(data->udev, 0);
  2258. err = usb_control_msg(data->udev, pipe, 0x01,
  2259. 0xDE,
  2260. reg >> 16, reg & 0xffff,
  2261. buf, 4, USB_CTRL_SET_TIMEOUT);
  2262. if (err < 0) {
  2263. bt_dev_err(hdev, "Failed to read uhw reg(%d)", err);
  2264. goto err_free_buf;
  2265. }
  2266. *val = get_unaligned_le32(buf);
  2267. bt_dev_dbg(hdev, "reg=%x, value=0x%08x", reg, *val);
  2268. err_free_buf:
  2269. kfree(buf);
  2270. return err;
  2271. }
  2272. static int btusb_mtk_reg_read(struct btusb_data *data, u32 reg, u32 *val)
  2273. {
  2274. int pipe, err, size = sizeof(u32);
  2275. void *buf;
  2276. buf = kzalloc(size, GFP_KERNEL);
  2277. if (!buf)
  2278. return -ENOMEM;
  2279. pipe = usb_rcvctrlpipe(data->udev, 0);
  2280. err = usb_control_msg(data->udev, pipe, 0x63,
  2281. USB_TYPE_VENDOR | USB_DIR_IN,
  2282. reg >> 16, reg & 0xffff,
  2283. buf, size, USB_CTRL_SET_TIMEOUT);
  2284. if (err < 0)
  2285. goto err_free_buf;
  2286. *val = get_unaligned_le32(buf);
  2287. err_free_buf:
  2288. kfree(buf);
  2289. return err;
  2290. }
  2291. static int btusb_mtk_id_get(struct btusb_data *data, u32 reg, u32 *id)
  2292. {
  2293. return btusb_mtk_reg_read(data, reg, id);
  2294. }
  2295. static int btusb_mtk_setup(struct hci_dev *hdev)
  2296. {
  2297. struct btusb_data *data = hci_get_drvdata(hdev);
  2298. struct btmtk_hci_wmt_params wmt_params;
  2299. ktime_t calltime, delta, rettime;
  2300. struct btmtk_tci_sleep tci_sleep;
  2301. unsigned long long duration;
  2302. struct sk_buff *skb;
  2303. const char *fwname;
  2304. int err, status;
  2305. u32 dev_id;
  2306. char fw_bin_name[64];
  2307. u32 fw_version = 0;
  2308. u8 param;
  2309. calltime = ktime_get();
  2310. err = btusb_mtk_id_get(data, 0x80000008, &dev_id);
  2311. if (err < 0) {
  2312. bt_dev_err(hdev, "Failed to get device id (%d)", err);
  2313. return err;
  2314. }
  2315. if (!dev_id) {
  2316. err = btusb_mtk_id_get(data, 0x70010200, &dev_id);
  2317. if (err < 0) {
  2318. bt_dev_err(hdev, "Failed to get device id (%d)", err);
  2319. return err;
  2320. }
  2321. err = btusb_mtk_id_get(data, 0x80021004, &fw_version);
  2322. if (err < 0) {
  2323. bt_dev_err(hdev, "Failed to get fw version (%d)", err);
  2324. return err;
  2325. }
  2326. }
  2327. switch (dev_id) {
  2328. case 0x7663:
  2329. fwname = FIRMWARE_MT7663;
  2330. break;
  2331. case 0x7668:
  2332. fwname = FIRMWARE_MT7668;
  2333. break;
  2334. case 0x7922:
  2335. case 0x7961:
  2336. snprintf(fw_bin_name, sizeof(fw_bin_name),
  2337. "mediatek/BT_RAM_CODE_MT%04x_1_%x_hdr.bin",
  2338. dev_id & 0xffff, (fw_version & 0xff) + 1);
  2339. err = btmtk_setup_firmware_79xx(hdev, fw_bin_name,
  2340. btusb_mtk_hci_wmt_sync);
  2341. if (err < 0) {
  2342. bt_dev_err(hdev, "Failed to set up firmware (%d)", err);
  2343. return err;
  2344. }
  2345. /* It's Device EndPoint Reset Option Register */
  2346. btusb_mtk_uhw_reg_write(data, MTK_EP_RST_OPT, MTK_EP_RST_IN_OUT_OPT);
  2347. /* Enable Bluetooth protocol */
  2348. param = 1;
  2349. wmt_params.op = BTMTK_WMT_FUNC_CTRL;
  2350. wmt_params.flag = 0;
  2351. wmt_params.dlen = sizeof(param);
  2352. wmt_params.data = &param;
  2353. wmt_params.status = NULL;
  2354. err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
  2355. if (err < 0) {
  2356. bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
  2357. return err;
  2358. }
  2359. hci_set_msft_opcode(hdev, 0xFD30);
  2360. hci_set_aosp_capable(hdev);
  2361. goto done;
  2362. default:
  2363. bt_dev_err(hdev, "Unsupported hardware variant (%08x)",
  2364. dev_id);
  2365. return -ENODEV;
  2366. }
  2367. /* Query whether the firmware is already download */
  2368. wmt_params.op = BTMTK_WMT_SEMAPHORE;
  2369. wmt_params.flag = 1;
  2370. wmt_params.dlen = 0;
  2371. wmt_params.data = NULL;
  2372. wmt_params.status = &status;
  2373. err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
  2374. if (err < 0) {
  2375. bt_dev_err(hdev, "Failed to query firmware status (%d)", err);
  2376. return err;
  2377. }
  2378. if (status == BTMTK_WMT_PATCH_DONE) {
  2379. bt_dev_info(hdev, "firmware already downloaded");
  2380. goto ignore_setup_fw;
  2381. }
  2382. /* Setup a firmware which the device definitely requires */
  2383. err = btmtk_setup_firmware(hdev, fwname,
  2384. btusb_mtk_hci_wmt_sync);
  2385. if (err < 0)
  2386. return err;
  2387. ignore_setup_fw:
  2388. err = readx_poll_timeout(btusb_mtk_func_query, hdev, status,
  2389. status < 0 || status != BTMTK_WMT_ON_PROGRESS,
  2390. 2000, 5000000);
  2391. /* -ETIMEDOUT happens */
  2392. if (err < 0)
  2393. return err;
  2394. /* The other errors happen in btusb_mtk_func_query */
  2395. if (status < 0)
  2396. return status;
  2397. if (status == BTMTK_WMT_ON_DONE) {
  2398. bt_dev_info(hdev, "function already on");
  2399. goto ignore_func_on;
  2400. }
  2401. /* Enable Bluetooth protocol */
  2402. param = 1;
  2403. wmt_params.op = BTMTK_WMT_FUNC_CTRL;
  2404. wmt_params.flag = 0;
  2405. wmt_params.dlen = sizeof(param);
  2406. wmt_params.data = &param;
  2407. wmt_params.status = NULL;
  2408. err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
  2409. if (err < 0) {
  2410. bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
  2411. return err;
  2412. }
  2413. ignore_func_on:
  2414. /* Apply the low power environment setup */
  2415. tci_sleep.mode = 0x5;
  2416. tci_sleep.duration = cpu_to_le16(0x640);
  2417. tci_sleep.host_duration = cpu_to_le16(0x640);
  2418. tci_sleep.host_wakeup_pin = 0;
  2419. tci_sleep.time_compensation = 0;
  2420. skb = __hci_cmd_sync(hdev, 0xfc7a, sizeof(tci_sleep), &tci_sleep,
  2421. HCI_INIT_TIMEOUT);
  2422. if (IS_ERR(skb)) {
  2423. err = PTR_ERR(skb);
  2424. bt_dev_err(hdev, "Failed to apply low power setting (%d)", err);
  2425. return err;
  2426. }
  2427. kfree_skb(skb);
  2428. done:
  2429. rettime = ktime_get();
  2430. delta = ktime_sub(rettime, calltime);
  2431. duration = (unsigned long long)ktime_to_ns(delta) >> 10;
  2432. bt_dev_info(hdev, "Device setup in %llu usecs", duration);
  2433. return 0;
  2434. }
  2435. static int btusb_mtk_shutdown(struct hci_dev *hdev)
  2436. {
  2437. struct btmtk_hci_wmt_params wmt_params;
  2438. u8 param = 0;
  2439. int err;
  2440. /* Disable the device */
  2441. wmt_params.op = BTMTK_WMT_FUNC_CTRL;
  2442. wmt_params.flag = 0;
  2443. wmt_params.dlen = sizeof(param);
  2444. wmt_params.data = &param;
  2445. wmt_params.status = NULL;
  2446. err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
  2447. if (err < 0) {
  2448. bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
  2449. return err;
  2450. }
  2451. return 0;
  2452. }
  2453. static void btusb_mtk_cmd_timeout(struct hci_dev *hdev)
  2454. {
  2455. struct btusb_data *data = hci_get_drvdata(hdev);
  2456. u32 val;
  2457. int err, retry = 0;
  2458. /* It's MediaTek specific bluetooth reset mechanism via USB */
  2459. if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
  2460. bt_dev_err(hdev, "last reset failed? Not resetting again");
  2461. return;
  2462. }
  2463. err = usb_autopm_get_interface(data->intf);
  2464. if (err < 0)
  2465. return;
  2466. btusb_stop_traffic(data);
  2467. usb_kill_anchored_urbs(&data->tx_anchor);
  2468. /* It's Device EndPoint Reset Option Register */
  2469. bt_dev_dbg(hdev, "Initiating reset mechanism via uhw");
  2470. btusb_mtk_uhw_reg_write(data, MTK_EP_RST_OPT, MTK_EP_RST_IN_OUT_OPT);
  2471. btusb_mtk_uhw_reg_read(data, MTK_BT_WDT_STATUS, &val);
  2472. /* Reset the bluetooth chip via USB interface. */
  2473. btusb_mtk_uhw_reg_write(data, MTK_BT_SUBSYS_RST, 1);
  2474. btusb_mtk_uhw_reg_write(data, MTK_UDMA_INT_STA_BT, 0x000000FF);
  2475. btusb_mtk_uhw_reg_read(data, MTK_UDMA_INT_STA_BT, &val);
  2476. btusb_mtk_uhw_reg_write(data, MTK_UDMA_INT_STA_BT1, 0x000000FF);
  2477. btusb_mtk_uhw_reg_read(data, MTK_UDMA_INT_STA_BT1, &val);
  2478. /* MT7921 need to delay 20ms between toggle reset bit */
  2479. msleep(20);
  2480. btusb_mtk_uhw_reg_write(data, MTK_BT_SUBSYS_RST, 0);
  2481. btusb_mtk_uhw_reg_read(data, MTK_BT_SUBSYS_RST, &val);
  2482. /* Poll the register until reset is completed */
  2483. do {
  2484. btusb_mtk_uhw_reg_read(data, MTK_BT_MISC, &val);
  2485. if (val & MTK_BT_RST_DONE) {
  2486. bt_dev_dbg(hdev, "Bluetooth Reset Successfully");
  2487. break;
  2488. }
  2489. bt_dev_dbg(hdev, "Polling Bluetooth Reset CR");
  2490. retry++;
  2491. msleep(MTK_BT_RESET_WAIT_MS);
  2492. } while (retry < MTK_BT_RESET_NUM_TRIES);
  2493. btusb_mtk_id_get(data, 0x70010200, &val);
  2494. if (!val)
  2495. bt_dev_err(hdev, "Can't get device id, subsys reset fail.");
  2496. usb_queue_reset_device(data->intf);
  2497. clear_bit(BTUSB_HW_RESET_ACTIVE, &data->flags);
  2498. }
  2499. static int btusb_recv_acl_mtk(struct hci_dev *hdev, struct sk_buff *skb)
  2500. {
  2501. struct btusb_data *data = hci_get_drvdata(hdev);
  2502. u16 handle = le16_to_cpu(hci_acl_hdr(skb)->handle);
  2503. switch (handle) {
  2504. case 0xfc6f: /* Firmware dump from device */
  2505. /* When the firmware hangs, the device can no longer
  2506. * suspend and thus disable auto-suspend.
  2507. */
  2508. usb_disable_autosuspend(data->udev);
  2509. fallthrough;
  2510. case 0x05ff: /* Firmware debug logging 1 */
  2511. case 0x05fe: /* Firmware debug logging 2 */
  2512. return hci_recv_diag(hdev, skb);
  2513. }
  2514. return hci_recv_frame(hdev, skb);
  2515. }
  2516. #ifdef CONFIG_PM
  2517. /* Configure an out-of-band gpio as wake-up pin, if specified in device tree */
  2518. static int marvell_config_oob_wake(struct hci_dev *hdev)
  2519. {
  2520. struct sk_buff *skb;
  2521. struct btusb_data *data = hci_get_drvdata(hdev);
  2522. struct device *dev = &data->udev->dev;
  2523. u16 pin, gap, opcode;
  2524. int ret;
  2525. u8 cmd[5];
  2526. /* Move on if no wakeup pin specified */
  2527. if (of_property_read_u16(dev->of_node, "marvell,wakeup-pin", &pin) ||
  2528. of_property_read_u16(dev->of_node, "marvell,wakeup-gap-ms", &gap))
  2529. return 0;
  2530. /* Vendor specific command to configure a GPIO as wake-up pin */
  2531. opcode = hci_opcode_pack(0x3F, 0x59);
  2532. cmd[0] = opcode & 0xFF;
  2533. cmd[1] = opcode >> 8;
  2534. cmd[2] = 2; /* length of parameters that follow */
  2535. cmd[3] = pin;
  2536. cmd[4] = gap; /* time in ms, for which wakeup pin should be asserted */
  2537. skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
  2538. if (!skb) {
  2539. bt_dev_err(hdev, "%s: No memory", __func__);
  2540. return -ENOMEM;
  2541. }
  2542. skb_put_data(skb, cmd, sizeof(cmd));
  2543. hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
  2544. ret = btusb_send_frame(hdev, skb);
  2545. if (ret) {
  2546. bt_dev_err(hdev, "%s: configuration failed", __func__);
  2547. kfree_skb(skb);
  2548. return ret;
  2549. }
  2550. return 0;
  2551. }
  2552. #endif
  2553. static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
  2554. const bdaddr_t *bdaddr)
  2555. {
  2556. struct sk_buff *skb;
  2557. u8 buf[8];
  2558. long ret;
  2559. buf[0] = 0xfe;
  2560. buf[1] = sizeof(bdaddr_t);
  2561. memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
  2562. skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
  2563. if (IS_ERR(skb)) {
  2564. ret = PTR_ERR(skb);
  2565. bt_dev_err(hdev, "changing Marvell device address failed (%ld)",
  2566. ret);
  2567. return ret;
  2568. }
  2569. kfree_skb(skb);
  2570. return 0;
  2571. }
  2572. static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
  2573. const bdaddr_t *bdaddr)
  2574. {
  2575. struct sk_buff *skb;
  2576. u8 buf[10];
  2577. long ret;
  2578. buf[0] = 0x01;
  2579. buf[1] = 0x01;
  2580. buf[2] = 0x00;
  2581. buf[3] = sizeof(bdaddr_t);
  2582. memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
  2583. skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
  2584. if (IS_ERR(skb)) {
  2585. ret = PTR_ERR(skb);
  2586. bt_dev_err(hdev, "Change address command failed (%ld)", ret);
  2587. return ret;
  2588. }
  2589. kfree_skb(skb);
  2590. return 0;
  2591. }
  2592. static int btusb_set_bdaddr_wcn6855(struct hci_dev *hdev,
  2593. const bdaddr_t *bdaddr)
  2594. {
  2595. struct sk_buff *skb;
  2596. u8 buf[6];
  2597. long ret;
  2598. memcpy(buf, bdaddr, sizeof(bdaddr_t));
  2599. skb = __hci_cmd_sync_ev(hdev, 0xfc14, sizeof(buf), buf,
  2600. HCI_EV_CMD_COMPLETE, HCI_INIT_TIMEOUT);
  2601. if (IS_ERR(skb)) {
  2602. ret = PTR_ERR(skb);
  2603. bt_dev_err(hdev, "Change address command failed (%ld)", ret);
  2604. return ret;
  2605. }
  2606. kfree_skb(skb);
  2607. return 0;
  2608. }
  2609. #define QCA_DFU_PACKET_LEN 4096
  2610. #define QCA_GET_TARGET_VERSION 0x09
  2611. #define QCA_CHECK_STATUS 0x05
  2612. #define QCA_DFU_DOWNLOAD 0x01
  2613. #define QCA_SYSCFG_UPDATED 0x40
  2614. #define QCA_PATCH_UPDATED 0x80
  2615. #define QCA_DFU_TIMEOUT 3000
  2616. #define QCA_FLAG_MULTI_NVM 0x80
  2617. #define QCA_BT_RESET_WAIT_MS 100
  2618. #define WCN6855_2_0_RAM_VERSION_GF 0x400c1200
  2619. #define WCN6855_2_1_RAM_VERSION_GF 0x400c1211
  2620. struct qca_version {
  2621. __le32 rom_version;
  2622. __le32 patch_version;
  2623. __le32 ram_version;
  2624. __u8 chip_id;
  2625. __u8 platform_id;
  2626. __le16 flag;
  2627. __u8 reserved[4];
  2628. } __packed;
  2629. struct qca_rampatch_version {
  2630. __le16 rom_version_high;
  2631. __le16 rom_version_low;
  2632. __le16 patch_version;
  2633. } __packed;
  2634. struct qca_device_info {
  2635. u32 rom_version;
  2636. u8 rampatch_hdr; /* length of header in rampatch */
  2637. u8 nvm_hdr; /* length of header in NVM */
  2638. u8 ver_offset; /* offset of version structure in rampatch */
  2639. };
  2640. static const struct qca_device_info qca_devices_table[] = {
  2641. { 0x00000100, 20, 4, 8 }, /* Rome 1.0 */
  2642. { 0x00000101, 20, 4, 8 }, /* Rome 1.1 */
  2643. { 0x00000200, 28, 4, 16 }, /* Rome 2.0 */
  2644. { 0x00000201, 28, 4, 16 }, /* Rome 2.1 */
  2645. { 0x00000300, 28, 4, 16 }, /* Rome 3.0 */
  2646. { 0x00000302, 28, 4, 16 }, /* Rome 3.2 */
  2647. { 0x00130100, 40, 4, 16 }, /* WCN6855 1.0 */
  2648. { 0x00130200, 40, 4, 16 }, /* WCN6855 2.0 */
  2649. { 0x00130201, 40, 4, 16 }, /* WCN6855 2.1 */
  2650. { 0x00190200, 40, 4, 16 }, /* WCN785x 2.0 */
  2651. };
  2652. static int btusb_qca_send_vendor_req(struct usb_device *udev, u8 request,
  2653. void *data, u16 size)
  2654. {
  2655. int pipe, err;
  2656. u8 *buf;
  2657. buf = kmalloc(size, GFP_KERNEL);
  2658. if (!buf)
  2659. return -ENOMEM;
  2660. /* Found some of USB hosts have IOT issues with ours so that we should
  2661. * not wait until HCI layer is ready.
  2662. */
  2663. pipe = usb_rcvctrlpipe(udev, 0);
  2664. err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
  2665. 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
  2666. if (err < 0) {
  2667. dev_err(&udev->dev, "Failed to access otp area (%d)", err);
  2668. goto done;
  2669. }
  2670. memcpy(data, buf, size);
  2671. done:
  2672. kfree(buf);
  2673. return err;
  2674. }
  2675. static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
  2676. const struct firmware *firmware,
  2677. size_t hdr_size)
  2678. {
  2679. struct btusb_data *btdata = hci_get_drvdata(hdev);
  2680. struct usb_device *udev = btdata->udev;
  2681. size_t count, size, sent = 0;
  2682. int pipe, len, err;
  2683. u8 *buf;
  2684. buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
  2685. if (!buf)
  2686. return -ENOMEM;
  2687. count = firmware->size;
  2688. size = min_t(size_t, count, hdr_size);
  2689. memcpy(buf, firmware->data, size);
  2690. /* USB patches should go down to controller through USB path
  2691. * because binary format fits to go down through USB channel.
  2692. * USB control path is for patching headers and USB bulk is for
  2693. * patch body.
  2694. */
  2695. pipe = usb_sndctrlpipe(udev, 0);
  2696. err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
  2697. 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
  2698. if (err < 0) {
  2699. bt_dev_err(hdev, "Failed to send headers (%d)", err);
  2700. goto done;
  2701. }
  2702. sent += size;
  2703. count -= size;
  2704. /* ep2 need time to switch from function acl to function dfu,
  2705. * so we add 20ms delay here.
  2706. */
  2707. msleep(20);
  2708. while (count) {
  2709. size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
  2710. memcpy(buf, firmware->data + sent, size);
  2711. pipe = usb_sndbulkpipe(udev, 0x02);
  2712. err = usb_bulk_msg(udev, pipe, buf, size, &len,
  2713. QCA_DFU_TIMEOUT);
  2714. if (err < 0) {
  2715. bt_dev_err(hdev, "Failed to send body at %zd of %zd (%d)",
  2716. sent, firmware->size, err);
  2717. break;
  2718. }
  2719. if (size != len) {
  2720. bt_dev_err(hdev, "Failed to get bulk buffer");
  2721. err = -EILSEQ;
  2722. break;
  2723. }
  2724. sent += size;
  2725. count -= size;
  2726. }
  2727. done:
  2728. kfree(buf);
  2729. return err;
  2730. }
  2731. static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
  2732. struct qca_version *ver,
  2733. const struct qca_device_info *info)
  2734. {
  2735. struct qca_rampatch_version *rver;
  2736. const struct firmware *fw;
  2737. u32 ver_rom, ver_patch, rver_rom;
  2738. u16 rver_rom_low, rver_rom_high, rver_patch;
  2739. char fwname[64];
  2740. int err;
  2741. ver_rom = le32_to_cpu(ver->rom_version);
  2742. ver_patch = le32_to_cpu(ver->patch_version);
  2743. snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin", ver_rom);
  2744. err = request_firmware(&fw, fwname, &hdev->dev);
  2745. if (err) {
  2746. bt_dev_err(hdev, "failed to request rampatch file: %s (%d)",
  2747. fwname, err);
  2748. return err;
  2749. }
  2750. bt_dev_info(hdev, "using rampatch file: %s", fwname);
  2751. rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
  2752. rver_rom_low = le16_to_cpu(rver->rom_version_low);
  2753. rver_patch = le16_to_cpu(rver->patch_version);
  2754. if (ver_rom & ~0xffffU) {
  2755. rver_rom_high = le16_to_cpu(rver->rom_version_high);
  2756. rver_rom = le32_to_cpu(rver_rom_high << 16 | rver_rom_low);
  2757. } else {
  2758. rver_rom = rver_rom_low;
  2759. }
  2760. bt_dev_info(hdev, "QCA: patch rome 0x%x build 0x%x, "
  2761. "firmware rome 0x%x build 0x%x",
  2762. rver_rom, rver_patch, ver_rom, ver_patch);
  2763. if (rver_rom != ver_rom || rver_patch <= ver_patch) {
  2764. bt_dev_err(hdev, "rampatch file version did not match with firmware");
  2765. err = -EINVAL;
  2766. goto done;
  2767. }
  2768. err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
  2769. done:
  2770. release_firmware(fw);
  2771. return err;
  2772. }
  2773. static void btusb_generate_qca_nvm_name(char *fwname, size_t max_size,
  2774. const struct qca_version *ver)
  2775. {
  2776. u32 rom_version = le32_to_cpu(ver->rom_version);
  2777. u16 flag = le16_to_cpu(ver->flag);
  2778. if (((flag >> 8) & 0xff) == QCA_FLAG_MULTI_NVM) {
  2779. /* The board_id should be split into two bytes
  2780. * The 1st byte is chip ID, and the 2nd byte is platform ID
  2781. * For example, board ID 0x010A, 0x01 is platform ID. 0x0A is chip ID
  2782. * we have several platforms, and platform IDs are continuously added
  2783. * Platform ID:
  2784. * 0x00 is for Mobile
  2785. * 0x01 is for X86
  2786. * 0x02 is for Automotive
  2787. * 0x03 is for Consumer electronic
  2788. */
  2789. u16 board_id = (ver->chip_id << 8) + ver->platform_id;
  2790. const char *variant;
  2791. switch (le32_to_cpu(ver->ram_version)) {
  2792. case WCN6855_2_0_RAM_VERSION_GF:
  2793. case WCN6855_2_1_RAM_VERSION_GF:
  2794. variant = "_gf";
  2795. break;
  2796. default:
  2797. variant = "";
  2798. break;
  2799. }
  2800. if (board_id == 0) {
  2801. snprintf(fwname, max_size, "qca/nvm_usb_%08x%s.bin",
  2802. rom_version, variant);
  2803. } else {
  2804. snprintf(fwname, max_size, "qca/nvm_usb_%08x%s_%04x.bin",
  2805. rom_version, variant, board_id);
  2806. }
  2807. } else {
  2808. snprintf(fwname, max_size, "qca/nvm_usb_%08x.bin",
  2809. rom_version);
  2810. }
  2811. }
  2812. static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
  2813. struct qca_version *ver,
  2814. const struct qca_device_info *info)
  2815. {
  2816. const struct firmware *fw;
  2817. char fwname[64];
  2818. int err;
  2819. btusb_generate_qca_nvm_name(fwname, sizeof(fwname), ver);
  2820. err = request_firmware(&fw, fwname, &hdev->dev);
  2821. if (err) {
  2822. bt_dev_err(hdev, "failed to request NVM file: %s (%d)",
  2823. fwname, err);
  2824. return err;
  2825. }
  2826. bt_dev_info(hdev, "using NVM file: %s", fwname);
  2827. err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
  2828. release_firmware(fw);
  2829. return err;
  2830. }
  2831. /* identify the ROM version and check whether patches are needed */
  2832. static bool btusb_qca_need_patch(struct usb_device *udev)
  2833. {
  2834. struct qca_version ver;
  2835. if (btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
  2836. sizeof(ver)) < 0)
  2837. return false;
  2838. /* only low ROM versions need patches */
  2839. return !(le32_to_cpu(ver.rom_version) & ~0xffffU);
  2840. }
  2841. static int btusb_setup_qca(struct hci_dev *hdev)
  2842. {
  2843. struct btusb_data *btdata = hci_get_drvdata(hdev);
  2844. struct usb_device *udev = btdata->udev;
  2845. const struct qca_device_info *info = NULL;
  2846. struct qca_version ver;
  2847. u32 ver_rom;
  2848. u8 status;
  2849. int i, err;
  2850. err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
  2851. sizeof(ver));
  2852. if (err < 0)
  2853. return err;
  2854. ver_rom = le32_to_cpu(ver.rom_version);
  2855. for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
  2856. if (ver_rom == qca_devices_table[i].rom_version)
  2857. info = &qca_devices_table[i];
  2858. }
  2859. if (!info) {
  2860. /* If the rom_version is not matched in the qca_devices_table
  2861. * and the high ROM version is not zero, we assume this chip no
  2862. * need to load the rampatch and nvm.
  2863. */
  2864. if (ver_rom & ~0xffffU)
  2865. return 0;
  2866. bt_dev_err(hdev, "don't support firmware rome 0x%x", ver_rom);
  2867. return -ENODEV;
  2868. }
  2869. err = btusb_qca_send_vendor_req(udev, QCA_CHECK_STATUS, &status,
  2870. sizeof(status));
  2871. if (err < 0)
  2872. return err;
  2873. if (!(status & QCA_PATCH_UPDATED)) {
  2874. err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
  2875. if (err < 0)
  2876. return err;
  2877. }
  2878. err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
  2879. sizeof(ver));
  2880. if (err < 0)
  2881. return err;
  2882. if (!(status & QCA_SYSCFG_UPDATED)) {
  2883. err = btusb_setup_qca_load_nvm(hdev, &ver, info);
  2884. if (err < 0)
  2885. return err;
  2886. /* WCN6855 2.1 and later will reset to apply firmware downloaded here, so
  2887. * wait ~100ms for reset Done then go ahead, otherwise, it maybe
  2888. * cause potential enable failure.
  2889. */
  2890. if (info->rom_version >= 0x00130201)
  2891. msleep(QCA_BT_RESET_WAIT_MS);
  2892. }
  2893. /* Mark HCI_OP_ENHANCED_SETUP_SYNC_CONN as broken as it doesn't seem to
  2894. * work with the likes of HSP/HFP mSBC.
  2895. */
  2896. set_bit(HCI_QUIRK_BROKEN_ENHANCED_SETUP_SYNC_CONN, &hdev->quirks);
  2897. return 0;
  2898. }
  2899. static inline int __set_diag_interface(struct hci_dev *hdev)
  2900. {
  2901. struct btusb_data *data = hci_get_drvdata(hdev);
  2902. struct usb_interface *intf = data->diag;
  2903. int i;
  2904. if (!data->diag)
  2905. return -ENODEV;
  2906. data->diag_tx_ep = NULL;
  2907. data->diag_rx_ep = NULL;
  2908. for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
  2909. struct usb_endpoint_descriptor *ep_desc;
  2910. ep_desc = &intf->cur_altsetting->endpoint[i].desc;
  2911. if (!data->diag_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
  2912. data->diag_tx_ep = ep_desc;
  2913. continue;
  2914. }
  2915. if (!data->diag_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
  2916. data->diag_rx_ep = ep_desc;
  2917. continue;
  2918. }
  2919. }
  2920. if (!data->diag_tx_ep || !data->diag_rx_ep) {
  2921. bt_dev_err(hdev, "invalid diagnostic descriptors");
  2922. return -ENODEV;
  2923. }
  2924. return 0;
  2925. }
  2926. static struct urb *alloc_diag_urb(struct hci_dev *hdev, bool enable)
  2927. {
  2928. struct btusb_data *data = hci_get_drvdata(hdev);
  2929. struct sk_buff *skb;
  2930. struct urb *urb;
  2931. unsigned int pipe;
  2932. if (!data->diag_tx_ep)
  2933. return ERR_PTR(-ENODEV);
  2934. urb = usb_alloc_urb(0, GFP_KERNEL);
  2935. if (!urb)
  2936. return ERR_PTR(-ENOMEM);
  2937. skb = bt_skb_alloc(2, GFP_KERNEL);
  2938. if (!skb) {
  2939. usb_free_urb(urb);
  2940. return ERR_PTR(-ENOMEM);
  2941. }
  2942. skb_put_u8(skb, 0xf0);
  2943. skb_put_u8(skb, enable);
  2944. pipe = usb_sndbulkpipe(data->udev, data->diag_tx_ep->bEndpointAddress);
  2945. usb_fill_bulk_urb(urb, data->udev, pipe,
  2946. skb->data, skb->len, btusb_tx_complete, skb);
  2947. skb->dev = (void *)hdev;
  2948. return urb;
  2949. }
  2950. static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable)
  2951. {
  2952. struct btusb_data *data = hci_get_drvdata(hdev);
  2953. struct urb *urb;
  2954. if (!data->diag)
  2955. return -ENODEV;
  2956. if (!test_bit(HCI_RUNNING, &hdev->flags))
  2957. return -ENETDOWN;
  2958. urb = alloc_diag_urb(hdev, enable);
  2959. if (IS_ERR(urb))
  2960. return PTR_ERR(urb);
  2961. return submit_or_queue_tx_urb(hdev, urb);
  2962. }
  2963. #ifdef CONFIG_PM
  2964. static irqreturn_t btusb_oob_wake_handler(int irq, void *priv)
  2965. {
  2966. struct btusb_data *data = priv;
  2967. pm_wakeup_event(&data->udev->dev, 0);
  2968. pm_system_wakeup();
  2969. /* Disable only if not already disabled (keep it balanced) */
  2970. if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
  2971. disable_irq_nosync(irq);
  2972. disable_irq_wake(irq);
  2973. }
  2974. return IRQ_HANDLED;
  2975. }
  2976. static const struct of_device_id btusb_match_table[] = {
  2977. { .compatible = "usb1286,204e" },
  2978. { .compatible = "usbcf3,e300" }, /* QCA6174A */
  2979. { .compatible = "usb4ca,301a" }, /* QCA6174A (Lite-On) */
  2980. { }
  2981. };
  2982. MODULE_DEVICE_TABLE(of, btusb_match_table);
  2983. /* Use an oob wakeup pin? */
  2984. static int btusb_config_oob_wake(struct hci_dev *hdev)
  2985. {
  2986. struct btusb_data *data = hci_get_drvdata(hdev);
  2987. struct device *dev = &data->udev->dev;
  2988. int irq, ret;
  2989. clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
  2990. if (!of_match_device(btusb_match_table, dev))
  2991. return 0;
  2992. /* Move on if no IRQ specified */
  2993. irq = of_irq_get_byname(dev->of_node, "wakeup");
  2994. if (irq <= 0) {
  2995. bt_dev_dbg(hdev, "%s: no OOB Wakeup IRQ in DT", __func__);
  2996. return 0;
  2997. }
  2998. irq_set_status_flags(irq, IRQ_NOAUTOEN);
  2999. ret = devm_request_irq(&hdev->dev, irq, btusb_oob_wake_handler,
  3000. 0, "OOB Wake-on-BT", data);
  3001. if (ret) {
  3002. bt_dev_err(hdev, "%s: IRQ request failed", __func__);
  3003. return ret;
  3004. }
  3005. ret = device_init_wakeup(dev, true);
  3006. if (ret) {
  3007. bt_dev_err(hdev, "%s: failed to init_wakeup", __func__);
  3008. return ret;
  3009. }
  3010. data->oob_wake_irq = irq;
  3011. bt_dev_info(hdev, "OOB Wake-on-BT configured at IRQ %u", irq);
  3012. return 0;
  3013. }
  3014. #endif
  3015. static void btusb_check_needs_reset_resume(struct usb_interface *intf)
  3016. {
  3017. if (dmi_check_system(btusb_needs_reset_resume_table))
  3018. interface_to_usbdev(intf)->quirks |= USB_QUIRK_RESET_RESUME;
  3019. }
  3020. static bool btusb_wakeup(struct hci_dev *hdev)
  3021. {
  3022. struct btusb_data *data = hci_get_drvdata(hdev);
  3023. return device_may_wakeup(&data->udev->dev);
  3024. }
  3025. static int btusb_shutdown_qca(struct hci_dev *hdev)
  3026. {
  3027. struct sk_buff *skb;
  3028. skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
  3029. if (IS_ERR(skb)) {
  3030. bt_dev_err(hdev, "HCI reset during shutdown failed");
  3031. return PTR_ERR(skb);
  3032. }
  3033. kfree_skb(skb);
  3034. return 0;
  3035. }
  3036. static ssize_t force_poll_sync_read(struct file *file, char __user *user_buf,
  3037. size_t count, loff_t *ppos)
  3038. {
  3039. struct btusb_data *data = file->private_data;
  3040. char buf[3];
  3041. buf[0] = data->poll_sync ? 'Y' : 'N';
  3042. buf[1] = '\n';
  3043. buf[2] = '\0';
  3044. return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
  3045. }
  3046. static ssize_t force_poll_sync_write(struct file *file,
  3047. const char __user *user_buf,
  3048. size_t count, loff_t *ppos)
  3049. {
  3050. struct btusb_data *data = file->private_data;
  3051. bool enable;
  3052. int err;
  3053. err = kstrtobool_from_user(user_buf, count, &enable);
  3054. if (err)
  3055. return err;
  3056. /* Only allow changes while the adapter is down */
  3057. if (test_bit(HCI_UP, &data->hdev->flags))
  3058. return -EPERM;
  3059. if (data->poll_sync == enable)
  3060. return -EALREADY;
  3061. data->poll_sync = enable;
  3062. return count;
  3063. }
  3064. static const struct file_operations force_poll_sync_fops = {
  3065. .open = simple_open,
  3066. .read = force_poll_sync_read,
  3067. .write = force_poll_sync_write,
  3068. .llseek = default_llseek,
  3069. };
  3070. static int btusb_probe(struct usb_interface *intf,
  3071. const struct usb_device_id *id)
  3072. {
  3073. struct usb_endpoint_descriptor *ep_desc;
  3074. struct gpio_desc *reset_gpio;
  3075. struct btusb_data *data;
  3076. struct hci_dev *hdev;
  3077. unsigned ifnum_base;
  3078. int i, err, priv_size;
  3079. BT_DBG("intf %p id %p", intf, id);
  3080. /* interface numbers are hardcoded in the spec */
  3081. if (intf->cur_altsetting->desc.bInterfaceNumber != 0) {
  3082. if (!(id->driver_info & BTUSB_IFNUM_2))
  3083. return -ENODEV;
  3084. if (intf->cur_altsetting->desc.bInterfaceNumber != 2)
  3085. return -ENODEV;
  3086. }
  3087. ifnum_base = intf->cur_altsetting->desc.bInterfaceNumber;
  3088. if (!id->driver_info) {
  3089. const struct usb_device_id *match;
  3090. match = usb_match_id(intf, blacklist_table);
  3091. if (match)
  3092. id = match;
  3093. }
  3094. if (id->driver_info == BTUSB_IGNORE)
  3095. return -ENODEV;
  3096. if (id->driver_info & BTUSB_ATH3012) {
  3097. struct usb_device *udev = interface_to_usbdev(intf);
  3098. /* Old firmware would otherwise let ath3k driver load
  3099. * patch and sysconfig files
  3100. */
  3101. if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001 &&
  3102. !btusb_qca_need_patch(udev))
  3103. return -ENODEV;
  3104. }
  3105. data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
  3106. if (!data)
  3107. return -ENOMEM;
  3108. for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
  3109. ep_desc = &intf->cur_altsetting->endpoint[i].desc;
  3110. if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
  3111. data->intr_ep = ep_desc;
  3112. continue;
  3113. }
  3114. if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
  3115. data->bulk_tx_ep = ep_desc;
  3116. continue;
  3117. }
  3118. if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
  3119. data->bulk_rx_ep = ep_desc;
  3120. continue;
  3121. }
  3122. }
  3123. if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
  3124. return -ENODEV;
  3125. if (id->driver_info & BTUSB_AMP) {
  3126. data->cmdreq_type = USB_TYPE_CLASS | 0x01;
  3127. data->cmdreq = 0x2b;
  3128. } else {
  3129. data->cmdreq_type = USB_TYPE_CLASS;
  3130. data->cmdreq = 0x00;
  3131. }
  3132. data->udev = interface_to_usbdev(intf);
  3133. data->intf = intf;
  3134. INIT_WORK(&data->work, btusb_work);
  3135. INIT_WORK(&data->waker, btusb_waker);
  3136. INIT_DELAYED_WORK(&data->rx_work, btusb_rx_work);
  3137. skb_queue_head_init(&data->acl_q);
  3138. init_usb_anchor(&data->deferred);
  3139. init_usb_anchor(&data->tx_anchor);
  3140. spin_lock_init(&data->txlock);
  3141. init_usb_anchor(&data->intr_anchor);
  3142. init_usb_anchor(&data->bulk_anchor);
  3143. init_usb_anchor(&data->isoc_anchor);
  3144. init_usb_anchor(&data->diag_anchor);
  3145. init_usb_anchor(&data->ctrl_anchor);
  3146. spin_lock_init(&data->rxlock);
  3147. priv_size = 0;
  3148. data->recv_event = hci_recv_frame;
  3149. data->recv_bulk = btusb_recv_bulk;
  3150. if (id->driver_info & BTUSB_INTEL_COMBINED) {
  3151. /* Allocate extra space for Intel device */
  3152. priv_size += sizeof(struct btintel_data);
  3153. /* Override the rx handlers */
  3154. data->recv_event = btusb_recv_event_intel;
  3155. data->recv_bulk = btusb_recv_bulk_intel;
  3156. }
  3157. data->recv_acl = hci_recv_frame;
  3158. hdev = hci_alloc_dev_priv(priv_size);
  3159. if (!hdev)
  3160. return -ENOMEM;
  3161. hdev->bus = HCI_USB;
  3162. hci_set_drvdata(hdev, data);
  3163. if (id->driver_info & BTUSB_AMP)
  3164. hdev->dev_type = HCI_AMP;
  3165. else
  3166. hdev->dev_type = HCI_PRIMARY;
  3167. data->hdev = hdev;
  3168. SET_HCIDEV_DEV(hdev, &intf->dev);
  3169. reset_gpio = gpiod_get_optional(&data->udev->dev, "reset",
  3170. GPIOD_OUT_LOW);
  3171. if (IS_ERR(reset_gpio)) {
  3172. err = PTR_ERR(reset_gpio);
  3173. goto out_free_dev;
  3174. } else if (reset_gpio) {
  3175. data->reset_gpio = reset_gpio;
  3176. }
  3177. hdev->open = btusb_open;
  3178. hdev->close = btusb_close;
  3179. hdev->flush = btusb_flush;
  3180. hdev->send = btusb_send_frame;
  3181. hdev->notify = btusb_notify;
  3182. hdev->wakeup = btusb_wakeup;
  3183. #ifdef CONFIG_PM
  3184. err = btusb_config_oob_wake(hdev);
  3185. if (err)
  3186. goto out_free_dev;
  3187. /* Marvell devices may need a specific chip configuration */
  3188. if (id->driver_info & BTUSB_MARVELL && data->oob_wake_irq) {
  3189. err = marvell_config_oob_wake(hdev);
  3190. if (err)
  3191. goto out_free_dev;
  3192. }
  3193. #endif
  3194. if (id->driver_info & BTUSB_CW6622)
  3195. set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
  3196. if (id->driver_info & BTUSB_BCM2045)
  3197. set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
  3198. if (id->driver_info & BTUSB_BCM92035)
  3199. hdev->setup = btusb_setup_bcm92035;
  3200. if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
  3201. (id->driver_info & BTUSB_BCM_PATCHRAM)) {
  3202. hdev->manufacturer = 15;
  3203. hdev->setup = btbcm_setup_patchram;
  3204. hdev->set_diag = btusb_bcm_set_diag;
  3205. hdev->set_bdaddr = btbcm_set_bdaddr;
  3206. /* Broadcom LM_DIAG Interface numbers are hardcoded */
  3207. data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
  3208. }
  3209. if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
  3210. (id->driver_info & BTUSB_BCM_APPLE)) {
  3211. hdev->manufacturer = 15;
  3212. hdev->setup = btbcm_setup_apple;
  3213. hdev->set_diag = btusb_bcm_set_diag;
  3214. /* Broadcom LM_DIAG Interface numbers are hardcoded */
  3215. data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
  3216. }
  3217. /* Combined Intel Device setup to support multiple setup routine */
  3218. if (id->driver_info & BTUSB_INTEL_COMBINED) {
  3219. err = btintel_configure_setup(hdev);
  3220. if (err)
  3221. goto out_free_dev;
  3222. /* Transport specific configuration */
  3223. hdev->send = btusb_send_frame_intel;
  3224. hdev->cmd_timeout = btusb_intel_cmd_timeout;
  3225. if (id->driver_info & BTUSB_INTEL_NO_WBS_SUPPORT)
  3226. btintel_set_flag(hdev, INTEL_ROM_LEGACY_NO_WBS_SUPPORT);
  3227. if (id->driver_info & BTUSB_INTEL_BROKEN_INITIAL_NCMD)
  3228. btintel_set_flag(hdev, INTEL_BROKEN_INITIAL_NCMD);
  3229. if (id->driver_info & BTUSB_INTEL_BROKEN_SHUTDOWN_LED)
  3230. btintel_set_flag(hdev, INTEL_BROKEN_SHUTDOWN_LED);
  3231. }
  3232. if (id->driver_info & BTUSB_MARVELL)
  3233. hdev->set_bdaddr = btusb_set_bdaddr_marvell;
  3234. if (IS_ENABLED(CONFIG_BT_HCIBTUSB_MTK) &&
  3235. (id->driver_info & BTUSB_MEDIATEK)) {
  3236. hdev->setup = btusb_mtk_setup;
  3237. hdev->shutdown = btusb_mtk_shutdown;
  3238. hdev->manufacturer = 70;
  3239. hdev->cmd_timeout = btusb_mtk_cmd_timeout;
  3240. hdev->set_bdaddr = btmtk_set_bdaddr;
  3241. set_bit(HCI_QUIRK_BROKEN_ENHANCED_SETUP_SYNC_CONN, &hdev->quirks);
  3242. set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
  3243. data->recv_acl = btusb_recv_acl_mtk;
  3244. }
  3245. if (id->driver_info & BTUSB_SWAVE) {
  3246. set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
  3247. set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
  3248. }
  3249. if (id->driver_info & BTUSB_INTEL_BOOT) {
  3250. hdev->manufacturer = 2;
  3251. set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
  3252. }
  3253. if (id->driver_info & BTUSB_ATH3012) {
  3254. data->setup_on_usb = btusb_setup_qca;
  3255. hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
  3256. set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
  3257. set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
  3258. }
  3259. if (id->driver_info & BTUSB_QCA_ROME) {
  3260. data->setup_on_usb = btusb_setup_qca;
  3261. hdev->shutdown = btusb_shutdown_qca;
  3262. hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
  3263. hdev->cmd_timeout = btusb_qca_cmd_timeout;
  3264. set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
  3265. btusb_check_needs_reset_resume(intf);
  3266. }
  3267. if (id->driver_info & BTUSB_QCA_WCN6855) {
  3268. data->setup_on_usb = btusb_setup_qca;
  3269. hdev->shutdown = btusb_shutdown_qca;
  3270. hdev->set_bdaddr = btusb_set_bdaddr_wcn6855;
  3271. hdev->cmd_timeout = btusb_qca_cmd_timeout;
  3272. set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
  3273. hci_set_msft_opcode(hdev, 0xFD70);
  3274. }
  3275. if (id->driver_info & BTUSB_AMP) {
  3276. /* AMP controllers do not support SCO packets */
  3277. data->isoc = NULL;
  3278. } else {
  3279. /* Interface orders are hardcoded in the specification */
  3280. data->isoc = usb_ifnum_to_if(data->udev, ifnum_base + 1);
  3281. data->isoc_ifnum = ifnum_base + 1;
  3282. }
  3283. if (IS_ENABLED(CONFIG_BT_HCIBTUSB_RTL) &&
  3284. (id->driver_info & BTUSB_REALTEK)) {
  3285. hdev->setup = btrtl_setup_realtek;
  3286. hdev->shutdown = btrtl_shutdown_realtek;
  3287. hdev->cmd_timeout = btusb_rtl_cmd_timeout;
  3288. /* Realtek devices need to set remote wakeup on auto-suspend */
  3289. set_bit(BTUSB_WAKEUP_AUTOSUSPEND, &data->flags);
  3290. set_bit(BTUSB_USE_ALT3_FOR_WBS, &data->flags);
  3291. }
  3292. if (id->driver_info & BTUSB_ACTIONS_SEMI) {
  3293. /* Support is advertised, but not implemented */
  3294. set_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks);
  3295. set_bit(HCI_QUIRK_BROKEN_READ_TRANSMIT_POWER, &hdev->quirks);
  3296. set_bit(HCI_QUIRK_BROKEN_SET_RPA_TIMEOUT, &hdev->quirks);
  3297. set_bit(HCI_QUIRK_BROKEN_EXT_SCAN, &hdev->quirks);
  3298. }
  3299. if (!reset)
  3300. set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
  3301. if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
  3302. if (!disable_scofix)
  3303. set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
  3304. }
  3305. if (id->driver_info & BTUSB_BROKEN_ISOC)
  3306. data->isoc = NULL;
  3307. if (id->driver_info & BTUSB_WIDEBAND_SPEECH)
  3308. set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
  3309. if (id->driver_info & BTUSB_VALID_LE_STATES)
  3310. set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
  3311. if (id->driver_info & BTUSB_DIGIANSWER) {
  3312. data->cmdreq_type = USB_TYPE_VENDOR;
  3313. set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
  3314. }
  3315. if (id->driver_info & BTUSB_CSR) {
  3316. struct usb_device *udev = data->udev;
  3317. u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
  3318. /* Old firmware would otherwise execute USB reset */
  3319. if (bcdDevice < 0x117)
  3320. set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
  3321. /* This must be set first in case we disable it for fakes */
  3322. set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
  3323. /* Fake CSR devices with broken commands */
  3324. if (le16_to_cpu(udev->descriptor.idVendor) == 0x0a12 &&
  3325. le16_to_cpu(udev->descriptor.idProduct) == 0x0001)
  3326. hdev->setup = btusb_setup_csr;
  3327. }
  3328. if (id->driver_info & BTUSB_SNIFFER) {
  3329. struct usb_device *udev = data->udev;
  3330. /* New sniffer firmware has crippled HCI interface */
  3331. if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
  3332. set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
  3333. }
  3334. if (id->driver_info & BTUSB_INTEL_BOOT) {
  3335. /* A bug in the bootloader causes that interrupt interface is
  3336. * only enabled after receiving SetInterface(0, AltSetting=0).
  3337. */
  3338. err = usb_set_interface(data->udev, 0, 0);
  3339. if (err < 0) {
  3340. BT_ERR("failed to set interface 0, alt 0 %d", err);
  3341. goto out_free_dev;
  3342. }
  3343. }
  3344. if (data->isoc) {
  3345. err = usb_driver_claim_interface(&btusb_driver,
  3346. data->isoc, data);
  3347. if (err < 0)
  3348. goto out_free_dev;
  3349. }
  3350. if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) && data->diag) {
  3351. if (!usb_driver_claim_interface(&btusb_driver,
  3352. data->diag, data))
  3353. __set_diag_interface(hdev);
  3354. else
  3355. data->diag = NULL;
  3356. }
  3357. if (enable_autosuspend)
  3358. usb_enable_autosuspend(data->udev);
  3359. err = hci_register_dev(hdev);
  3360. if (err < 0)
  3361. goto out_free_dev;
  3362. usb_set_intfdata(intf, data);
  3363. debugfs_create_file("force_poll_sync", 0644, hdev->debugfs, data,
  3364. &force_poll_sync_fops);
  3365. return 0;
  3366. out_free_dev:
  3367. if (data->reset_gpio)
  3368. gpiod_put(data->reset_gpio);
  3369. hci_free_dev(hdev);
  3370. return err;
  3371. }
  3372. static void btusb_disconnect(struct usb_interface *intf)
  3373. {
  3374. struct btusb_data *data = usb_get_intfdata(intf);
  3375. struct hci_dev *hdev;
  3376. BT_DBG("intf %p", intf);
  3377. if (!data)
  3378. return;
  3379. hdev = data->hdev;
  3380. usb_set_intfdata(data->intf, NULL);
  3381. if (data->isoc)
  3382. usb_set_intfdata(data->isoc, NULL);
  3383. if (data->diag)
  3384. usb_set_intfdata(data->diag, NULL);
  3385. hci_unregister_dev(hdev);
  3386. if (intf == data->intf) {
  3387. if (data->isoc)
  3388. usb_driver_release_interface(&btusb_driver, data->isoc);
  3389. if (data->diag)
  3390. usb_driver_release_interface(&btusb_driver, data->diag);
  3391. } else if (intf == data->isoc) {
  3392. if (data->diag)
  3393. usb_driver_release_interface(&btusb_driver, data->diag);
  3394. usb_driver_release_interface(&btusb_driver, data->intf);
  3395. } else if (intf == data->diag) {
  3396. usb_driver_release_interface(&btusb_driver, data->intf);
  3397. if (data->isoc)
  3398. usb_driver_release_interface(&btusb_driver, data->isoc);
  3399. }
  3400. if (data->oob_wake_irq)
  3401. device_init_wakeup(&data->udev->dev, false);
  3402. if (data->reset_gpio)
  3403. gpiod_put(data->reset_gpio);
  3404. hci_free_dev(hdev);
  3405. }
  3406. #ifdef CONFIG_PM
  3407. static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
  3408. {
  3409. struct btusb_data *data = usb_get_intfdata(intf);
  3410. BT_DBG("intf %p", intf);
  3411. if (data->suspend_count++)
  3412. return 0;
  3413. spin_lock_irq(&data->txlock);
  3414. if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
  3415. set_bit(BTUSB_SUSPENDING, &data->flags);
  3416. spin_unlock_irq(&data->txlock);
  3417. } else {
  3418. spin_unlock_irq(&data->txlock);
  3419. data->suspend_count--;
  3420. return -EBUSY;
  3421. }
  3422. cancel_work_sync(&data->work);
  3423. btusb_stop_traffic(data);
  3424. usb_kill_anchored_urbs(&data->tx_anchor);
  3425. if (data->oob_wake_irq && device_may_wakeup(&data->udev->dev)) {
  3426. set_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
  3427. enable_irq_wake(data->oob_wake_irq);
  3428. enable_irq(data->oob_wake_irq);
  3429. }
  3430. /* For global suspend, Realtek devices lose the loaded fw
  3431. * in them. But for autosuspend, firmware should remain.
  3432. * Actually, it depends on whether the usb host sends
  3433. * set feature (enable wakeup) or not.
  3434. */
  3435. if (test_bit(BTUSB_WAKEUP_AUTOSUSPEND, &data->flags)) {
  3436. if (PMSG_IS_AUTO(message) &&
  3437. device_can_wakeup(&data->udev->dev))
  3438. data->udev->do_remote_wakeup = 1;
  3439. else if (!PMSG_IS_AUTO(message) &&
  3440. !device_may_wakeup(&data->udev->dev)) {
  3441. data->udev->do_remote_wakeup = 0;
  3442. data->udev->reset_resume = 1;
  3443. }
  3444. }
  3445. return 0;
  3446. }
  3447. static void play_deferred(struct btusb_data *data)
  3448. {
  3449. struct urb *urb;
  3450. int err;
  3451. while ((urb = usb_get_from_anchor(&data->deferred))) {
  3452. usb_anchor_urb(urb, &data->tx_anchor);
  3453. err = usb_submit_urb(urb, GFP_ATOMIC);
  3454. if (err < 0) {
  3455. if (err != -EPERM && err != -ENODEV)
  3456. BT_ERR("%s urb %p submission failed (%d)",
  3457. data->hdev->name, urb, -err);
  3458. kfree(urb->setup_packet);
  3459. usb_unanchor_urb(urb);
  3460. usb_free_urb(urb);
  3461. break;
  3462. }
  3463. data->tx_in_flight++;
  3464. usb_free_urb(urb);
  3465. }
  3466. /* Cleanup the rest deferred urbs. */
  3467. while ((urb = usb_get_from_anchor(&data->deferred))) {
  3468. kfree(urb->setup_packet);
  3469. usb_free_urb(urb);
  3470. }
  3471. }
  3472. static int btusb_resume(struct usb_interface *intf)
  3473. {
  3474. struct btusb_data *data = usb_get_intfdata(intf);
  3475. struct hci_dev *hdev = data->hdev;
  3476. int err = 0;
  3477. BT_DBG("intf %p", intf);
  3478. if (--data->suspend_count)
  3479. return 0;
  3480. /* Disable only if not already disabled (keep it balanced) */
  3481. if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
  3482. disable_irq(data->oob_wake_irq);
  3483. disable_irq_wake(data->oob_wake_irq);
  3484. }
  3485. if (!test_bit(HCI_RUNNING, &hdev->flags))
  3486. goto done;
  3487. if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
  3488. err = btusb_submit_intr_urb(hdev, GFP_NOIO);
  3489. if (err < 0) {
  3490. clear_bit(BTUSB_INTR_RUNNING, &data->flags);
  3491. goto failed;
  3492. }
  3493. }
  3494. if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
  3495. err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
  3496. if (err < 0) {
  3497. clear_bit(BTUSB_BULK_RUNNING, &data->flags);
  3498. goto failed;
  3499. }
  3500. btusb_submit_bulk_urb(hdev, GFP_NOIO);
  3501. }
  3502. if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
  3503. if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
  3504. clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
  3505. else
  3506. btusb_submit_isoc_urb(hdev, GFP_NOIO);
  3507. }
  3508. spin_lock_irq(&data->txlock);
  3509. play_deferred(data);
  3510. clear_bit(BTUSB_SUSPENDING, &data->flags);
  3511. spin_unlock_irq(&data->txlock);
  3512. schedule_work(&data->work);
  3513. return 0;
  3514. failed:
  3515. usb_scuttle_anchored_urbs(&data->deferred);
  3516. done:
  3517. spin_lock_irq(&data->txlock);
  3518. clear_bit(BTUSB_SUSPENDING, &data->flags);
  3519. spin_unlock_irq(&data->txlock);
  3520. return err;
  3521. }
  3522. #endif
  3523. static struct usb_driver btusb_driver = {
  3524. .name = "btusb",
  3525. .probe = btusb_probe,
  3526. .disconnect = btusb_disconnect,
  3527. #ifdef CONFIG_PM
  3528. .suspend = btusb_suspend,
  3529. .resume = btusb_resume,
  3530. #endif
  3531. .id_table = btusb_table,
  3532. .supports_autosuspend = 1,
  3533. .disable_hub_initiated_lpm = 1,
  3534. };
  3535. module_usb_driver(btusb_driver);
  3536. module_param(disable_scofix, bool, 0644);
  3537. MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
  3538. module_param(force_scofix, bool, 0644);
  3539. MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
  3540. module_param(enable_autosuspend, bool, 0644);
  3541. MODULE_PARM_DESC(enable_autosuspend, "Enable USB autosuspend by default");
  3542. module_param(reset, bool, 0644);
  3543. MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
  3544. MODULE_AUTHOR("Marcel Holtmann <[email protected]>");
  3545. MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
  3546. MODULE_VERSION(VERSION);
  3547. MODULE_LICENSE("GPL");