btusb.c 118 KB

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