hci_sync.c 197 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * BlueZ - Bluetooth protocol stack for Linux
  4. *
  5. * Copyright (C) 2021 Intel Corporation
  6. * Copyright 2023 NXP
  7. */
  8. #include <linux/property.h>
  9. #include <net/bluetooth/bluetooth.h>
  10. #include <net/bluetooth/hci_core.h>
  11. #include <net/bluetooth/mgmt.h>
  12. #include "hci_codec.h"
  13. #include "hci_debugfs.h"
  14. #include "smp.h"
  15. #include "eir.h"
  16. #include "msft.h"
  17. #include "aosp.h"
  18. #include "leds.h"
  19. static void hci_cmd_sync_complete(struct hci_dev *hdev, u8 result, u16 opcode,
  20. struct sk_buff *skb)
  21. {
  22. bt_dev_dbg(hdev, "result 0x%2.2x", result);
  23. if (READ_ONCE(hdev->req_status) != HCI_REQ_PEND)
  24. return;
  25. hdev->req_result = result;
  26. WRITE_ONCE(hdev->req_status, HCI_REQ_DONE);
  27. /* Free the request command so it is not used as response */
  28. kfree_skb(hdev->req_skb);
  29. hdev->req_skb = NULL;
  30. if (skb) {
  31. struct sock *sk = hci_skb_sk(skb);
  32. /* Drop sk reference if set */
  33. if (sk)
  34. sock_put(sk);
  35. hdev->req_rsp = skb_get(skb);
  36. }
  37. wake_up_interruptible(&hdev->req_wait_q);
  38. }
  39. struct sk_buff *hci_cmd_sync_alloc(struct hci_dev *hdev, u16 opcode, u32 plen,
  40. const void *param, struct sock *sk)
  41. {
  42. int len = HCI_COMMAND_HDR_SIZE + plen;
  43. struct hci_command_hdr *hdr;
  44. struct sk_buff *skb;
  45. skb = bt_skb_alloc(len, GFP_ATOMIC);
  46. if (!skb)
  47. return NULL;
  48. hdr = skb_put(skb, HCI_COMMAND_HDR_SIZE);
  49. hdr->opcode = cpu_to_le16(opcode);
  50. hdr->plen = plen;
  51. if (plen)
  52. skb_put_data(skb, param, plen);
  53. bt_dev_dbg(hdev, "skb len %d", skb->len);
  54. hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
  55. hci_skb_opcode(skb) = opcode;
  56. /* Grab a reference if command needs to be associated with a sock (e.g.
  57. * likely mgmt socket that initiated the command).
  58. */
  59. if (sk) {
  60. hci_skb_sk(skb) = sk;
  61. sock_hold(sk);
  62. }
  63. return skb;
  64. }
  65. static void hci_cmd_sync_add(struct hci_request *req, u16 opcode, u32 plen,
  66. const void *param, u8 event, struct sock *sk)
  67. {
  68. struct hci_dev *hdev = req->hdev;
  69. struct sk_buff *skb;
  70. bt_dev_dbg(hdev, "opcode 0x%4.4x plen %d", opcode, plen);
  71. /* If an error occurred during request building, there is no point in
  72. * queueing the HCI command. We can simply return.
  73. */
  74. if (req->err)
  75. return;
  76. skb = hci_cmd_sync_alloc(hdev, opcode, plen, param, sk);
  77. if (!skb) {
  78. bt_dev_err(hdev, "no memory for command (opcode 0x%4.4x)",
  79. opcode);
  80. req->err = -ENOMEM;
  81. return;
  82. }
  83. if (skb_queue_empty(&req->cmd_q))
  84. bt_cb(skb)->hci.req_flags |= HCI_REQ_START;
  85. hci_skb_event(skb) = event;
  86. skb_queue_tail(&req->cmd_q, skb);
  87. }
  88. static int hci_req_sync_run(struct hci_request *req)
  89. {
  90. struct hci_dev *hdev = req->hdev;
  91. struct sk_buff *skb;
  92. unsigned long flags;
  93. bt_dev_dbg(hdev, "length %u", skb_queue_len(&req->cmd_q));
  94. /* If an error occurred during request building, remove all HCI
  95. * commands queued on the HCI request queue.
  96. */
  97. if (req->err) {
  98. skb_queue_purge(&req->cmd_q);
  99. return req->err;
  100. }
  101. /* Do not allow empty requests */
  102. if (skb_queue_empty(&req->cmd_q))
  103. return -ENODATA;
  104. skb = skb_peek_tail(&req->cmd_q);
  105. bt_cb(skb)->hci.req_complete_skb = hci_cmd_sync_complete;
  106. bt_cb(skb)->hci.req_flags |= HCI_REQ_SKB;
  107. spin_lock_irqsave(&hdev->cmd_q.lock, flags);
  108. skb_queue_splice_tail(&req->cmd_q, &hdev->cmd_q);
  109. spin_unlock_irqrestore(&hdev->cmd_q.lock, flags);
  110. queue_work(hdev->workqueue, &hdev->cmd_work);
  111. return 0;
  112. }
  113. static void hci_request_init(struct hci_request *req, struct hci_dev *hdev)
  114. {
  115. skb_queue_head_init(&req->cmd_q);
  116. req->hdev = hdev;
  117. req->err = 0;
  118. }
  119. /* This function requires the caller holds hdev->req_lock. */
  120. struct sk_buff *__hci_cmd_sync_sk(struct hci_dev *hdev, u16 opcode, u32 plen,
  121. const void *param, u8 event, u32 timeout,
  122. struct sock *sk)
  123. {
  124. struct hci_request req;
  125. struct sk_buff *skb;
  126. int err = 0;
  127. bt_dev_dbg(hdev, "Opcode 0x%4.4x", opcode);
  128. hci_request_init(&req, hdev);
  129. hci_cmd_sync_add(&req, opcode, plen, param, event, sk);
  130. WRITE_ONCE(hdev->req_status, HCI_REQ_PEND);
  131. err = hci_req_sync_run(&req);
  132. if (err < 0)
  133. return ERR_PTR(err);
  134. err = wait_event_interruptible_timeout(hdev->req_wait_q,
  135. READ_ONCE(hdev->req_status) != HCI_REQ_PEND,
  136. timeout);
  137. if (err == -ERESTARTSYS)
  138. return ERR_PTR(-EINTR);
  139. switch (READ_ONCE(hdev->req_status)) {
  140. case HCI_REQ_DONE:
  141. err = -bt_to_errno(hdev->req_result);
  142. break;
  143. case HCI_REQ_CANCELED:
  144. err = -hdev->req_result;
  145. break;
  146. default:
  147. err = -ETIMEDOUT;
  148. break;
  149. }
  150. WRITE_ONCE(hdev->req_status, 0);
  151. hdev->req_result = 0;
  152. skb = hdev->req_rsp;
  153. hdev->req_rsp = NULL;
  154. bt_dev_dbg(hdev, "end: err %d", err);
  155. if (err < 0) {
  156. kfree_skb(skb);
  157. return ERR_PTR(err);
  158. }
  159. /* If command return a status event skb will be set to NULL as there are
  160. * no parameters.
  161. */
  162. if (!skb)
  163. return ERR_PTR(-ENODATA);
  164. return skb;
  165. }
  166. EXPORT_SYMBOL(__hci_cmd_sync_sk);
  167. /* This function requires the caller holds hdev->req_lock. */
  168. struct sk_buff *__hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
  169. const void *param, u32 timeout)
  170. {
  171. return __hci_cmd_sync_sk(hdev, opcode, plen, param, 0, timeout, NULL);
  172. }
  173. EXPORT_SYMBOL(__hci_cmd_sync);
  174. /* Send HCI command and wait for command complete event */
  175. struct sk_buff *hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
  176. const void *param, u32 timeout)
  177. {
  178. struct sk_buff *skb;
  179. if (!test_bit(HCI_UP, &hdev->flags))
  180. return ERR_PTR(-ENETDOWN);
  181. bt_dev_dbg(hdev, "opcode 0x%4.4x plen %d", opcode, plen);
  182. hci_req_sync_lock(hdev);
  183. skb = __hci_cmd_sync(hdev, opcode, plen, param, timeout);
  184. hci_req_sync_unlock(hdev);
  185. return skb;
  186. }
  187. EXPORT_SYMBOL(hci_cmd_sync);
  188. /* This function requires the caller holds hdev->req_lock. */
  189. struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 opcode, u32 plen,
  190. const void *param, u8 event, u32 timeout)
  191. {
  192. return __hci_cmd_sync_sk(hdev, opcode, plen, param, event, timeout,
  193. NULL);
  194. }
  195. EXPORT_SYMBOL(__hci_cmd_sync_ev);
  196. /* This function requires the caller holds hdev->req_lock. */
  197. int __hci_cmd_sync_status_sk(struct hci_dev *hdev, u16 opcode, u32 plen,
  198. const void *param, u8 event, u32 timeout,
  199. struct sock *sk)
  200. {
  201. struct sk_buff *skb;
  202. u8 status;
  203. skb = __hci_cmd_sync_sk(hdev, opcode, plen, param, event, timeout, sk);
  204. /* If command return a status event, skb will be set to -ENODATA */
  205. if (skb == ERR_PTR(-ENODATA))
  206. return 0;
  207. if (IS_ERR(skb)) {
  208. if (!event)
  209. bt_dev_err(hdev, "Opcode 0x%4.4x failed: %ld", opcode,
  210. PTR_ERR(skb));
  211. return PTR_ERR(skb);
  212. }
  213. status = skb->data[0];
  214. kfree_skb(skb);
  215. return status;
  216. }
  217. EXPORT_SYMBOL(__hci_cmd_sync_status_sk);
  218. int __hci_cmd_sync_status(struct hci_dev *hdev, u16 opcode, u32 plen,
  219. const void *param, u32 timeout)
  220. {
  221. return __hci_cmd_sync_status_sk(hdev, opcode, plen, param, 0, timeout,
  222. NULL);
  223. }
  224. EXPORT_SYMBOL(__hci_cmd_sync_status);
  225. int hci_cmd_sync_status(struct hci_dev *hdev, u16 opcode, u32 plen,
  226. const void *param, u32 timeout)
  227. {
  228. int err;
  229. hci_req_sync_lock(hdev);
  230. err = __hci_cmd_sync_status(hdev, opcode, plen, param, timeout);
  231. hci_req_sync_unlock(hdev);
  232. return err;
  233. }
  234. EXPORT_SYMBOL(hci_cmd_sync_status);
  235. static void hci_cmd_sync_work(struct work_struct *work)
  236. {
  237. struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_sync_work);
  238. bt_dev_dbg(hdev, "");
  239. /* Dequeue all entries and run them */
  240. while (1) {
  241. struct hci_cmd_sync_work_entry *entry;
  242. mutex_lock(&hdev->cmd_sync_work_lock);
  243. entry = list_first_entry_or_null(&hdev->cmd_sync_work_list,
  244. struct hci_cmd_sync_work_entry,
  245. list);
  246. if (entry)
  247. list_del(&entry->list);
  248. mutex_unlock(&hdev->cmd_sync_work_lock);
  249. if (!entry)
  250. break;
  251. bt_dev_dbg(hdev, "entry %p", entry);
  252. if (entry->func) {
  253. int err;
  254. hci_req_sync_lock(hdev);
  255. err = entry->func(hdev, entry->data);
  256. if (entry->destroy)
  257. entry->destroy(hdev, entry->data, err);
  258. hci_req_sync_unlock(hdev);
  259. }
  260. kfree(entry);
  261. }
  262. }
  263. static void hci_cmd_sync_cancel_work(struct work_struct *work)
  264. {
  265. struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_sync_cancel_work);
  266. cancel_delayed_work_sync(&hdev->cmd_timer);
  267. cancel_delayed_work_sync(&hdev->ncmd_timer);
  268. atomic_set(&hdev->cmd_cnt, 1);
  269. wake_up_interruptible(&hdev->req_wait_q);
  270. }
  271. static int hci_scan_disable_sync(struct hci_dev *hdev);
  272. static int scan_disable_sync(struct hci_dev *hdev, void *data)
  273. {
  274. return hci_scan_disable_sync(hdev);
  275. }
  276. static int interleaved_inquiry_sync(struct hci_dev *hdev, void *data)
  277. {
  278. return hci_inquiry_sync(hdev, DISCOV_INTERLEAVED_INQUIRY_LEN, 0);
  279. }
  280. static void le_scan_disable(struct work_struct *work)
  281. {
  282. struct hci_dev *hdev = container_of(work, struct hci_dev,
  283. le_scan_disable.work);
  284. int status;
  285. bt_dev_dbg(hdev, "");
  286. hci_dev_lock(hdev);
  287. if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
  288. goto _return;
  289. status = hci_cmd_sync_queue(hdev, scan_disable_sync, NULL, NULL);
  290. if (status) {
  291. bt_dev_err(hdev, "failed to disable LE scan: %d", status);
  292. goto _return;
  293. }
  294. /* If we were running LE only scan, change discovery state. If
  295. * we were running both LE and BR/EDR inquiry simultaneously,
  296. * and BR/EDR inquiry is already finished, stop discovery,
  297. * otherwise BR/EDR inquiry will stop discovery when finished.
  298. * If we will resolve remote device name, do not change
  299. * discovery state.
  300. */
  301. if (hdev->discovery.type == DISCOV_TYPE_LE)
  302. goto discov_stopped;
  303. if (hdev->discovery.type != DISCOV_TYPE_INTERLEAVED)
  304. goto _return;
  305. if (hci_test_quirk(hdev, HCI_QUIRK_SIMULTANEOUS_DISCOVERY)) {
  306. if (!test_bit(HCI_INQUIRY, &hdev->flags) &&
  307. hdev->discovery.state != DISCOVERY_RESOLVING)
  308. goto discov_stopped;
  309. goto _return;
  310. }
  311. status = hci_cmd_sync_queue(hdev, interleaved_inquiry_sync, NULL, NULL);
  312. if (status) {
  313. bt_dev_err(hdev, "inquiry failed: status %d", status);
  314. goto discov_stopped;
  315. }
  316. goto _return;
  317. discov_stopped:
  318. hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
  319. _return:
  320. hci_dev_unlock(hdev);
  321. }
  322. static int hci_le_set_scan_enable_sync(struct hci_dev *hdev, u8 val,
  323. u8 filter_dup);
  324. static int reenable_adv_sync(struct hci_dev *hdev, void *data)
  325. {
  326. bt_dev_dbg(hdev, "");
  327. if (!hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
  328. list_empty(&hdev->adv_instances))
  329. return 0;
  330. if (hdev->cur_adv_instance) {
  331. return hci_schedule_adv_instance_sync(hdev,
  332. hdev->cur_adv_instance,
  333. true);
  334. } else {
  335. if (ext_adv_capable(hdev)) {
  336. hci_start_ext_adv_sync(hdev, 0x00);
  337. } else {
  338. hci_update_adv_data_sync(hdev, 0x00);
  339. hci_update_scan_rsp_data_sync(hdev, 0x00);
  340. hci_enable_advertising_sync(hdev);
  341. }
  342. }
  343. return 0;
  344. }
  345. static void reenable_adv(struct work_struct *work)
  346. {
  347. struct hci_dev *hdev = container_of(work, struct hci_dev,
  348. reenable_adv_work);
  349. int status;
  350. bt_dev_dbg(hdev, "");
  351. hci_dev_lock(hdev);
  352. status = hci_cmd_sync_queue(hdev, reenable_adv_sync, NULL, NULL);
  353. if (status)
  354. bt_dev_err(hdev, "failed to reenable ADV: %d", status);
  355. hci_dev_unlock(hdev);
  356. }
  357. static void cancel_adv_timeout(struct hci_dev *hdev)
  358. {
  359. if (hdev->adv_instance_timeout) {
  360. hdev->adv_instance_timeout = 0;
  361. cancel_delayed_work(&hdev->adv_instance_expire);
  362. }
  363. }
  364. /* For a single instance:
  365. * - force == true: The instance will be removed even when its remaining
  366. * lifetime is not zero.
  367. * - force == false: the instance will be deactivated but kept stored unless
  368. * the remaining lifetime is zero.
  369. *
  370. * For instance == 0x00:
  371. * - force == true: All instances will be removed regardless of their timeout
  372. * setting.
  373. * - force == false: Only instances that have a timeout will be removed.
  374. */
  375. int hci_clear_adv_instance_sync(struct hci_dev *hdev, struct sock *sk,
  376. u8 instance, bool force)
  377. {
  378. struct adv_info *adv_instance, *n, *next_instance = NULL;
  379. int err;
  380. u8 rem_inst;
  381. /* Cancel any timeout concerning the removed instance(s). */
  382. if (!instance || hdev->cur_adv_instance == instance)
  383. cancel_adv_timeout(hdev);
  384. /* Get the next instance to advertise BEFORE we remove
  385. * the current one. This can be the same instance again
  386. * if there is only one instance.
  387. */
  388. if (instance && hdev->cur_adv_instance == instance)
  389. next_instance = hci_get_next_instance(hdev, instance);
  390. if (instance == 0x00) {
  391. list_for_each_entry_safe(adv_instance, n, &hdev->adv_instances,
  392. list) {
  393. if (!(force || adv_instance->timeout))
  394. continue;
  395. rem_inst = adv_instance->instance;
  396. err = hci_remove_adv_instance(hdev, rem_inst);
  397. if (!err)
  398. mgmt_advertising_removed(sk, hdev, rem_inst);
  399. }
  400. } else {
  401. adv_instance = hci_find_adv_instance(hdev, instance);
  402. if (force || (adv_instance && adv_instance->timeout &&
  403. !adv_instance->remaining_time)) {
  404. /* Don't advertise a removed instance. */
  405. if (next_instance &&
  406. next_instance->instance == instance)
  407. next_instance = NULL;
  408. err = hci_remove_adv_instance(hdev, instance);
  409. if (!err)
  410. mgmt_advertising_removed(sk, hdev, instance);
  411. }
  412. }
  413. if (!hdev_is_powered(hdev) || hci_dev_test_flag(hdev, HCI_ADVERTISING))
  414. return 0;
  415. if (next_instance && !ext_adv_capable(hdev))
  416. return hci_schedule_adv_instance_sync(hdev,
  417. next_instance->instance,
  418. false);
  419. return 0;
  420. }
  421. static int adv_timeout_expire_sync(struct hci_dev *hdev, void *data)
  422. {
  423. u8 instance = *(u8 *)data;
  424. kfree(data);
  425. hci_clear_adv_instance_sync(hdev, NULL, instance, false);
  426. if (list_empty(&hdev->adv_instances))
  427. return hci_disable_advertising_sync(hdev);
  428. return 0;
  429. }
  430. static void adv_timeout_expire(struct work_struct *work)
  431. {
  432. u8 *inst_ptr;
  433. struct hci_dev *hdev = container_of(work, struct hci_dev,
  434. adv_instance_expire.work);
  435. bt_dev_dbg(hdev, "");
  436. hci_dev_lock(hdev);
  437. hdev->adv_instance_timeout = 0;
  438. if (hdev->cur_adv_instance == 0x00)
  439. goto unlock;
  440. inst_ptr = kmalloc(1, GFP_KERNEL);
  441. if (!inst_ptr)
  442. goto unlock;
  443. *inst_ptr = hdev->cur_adv_instance;
  444. hci_cmd_sync_queue(hdev, adv_timeout_expire_sync, inst_ptr, NULL);
  445. unlock:
  446. hci_dev_unlock(hdev);
  447. }
  448. static bool is_interleave_scanning(struct hci_dev *hdev)
  449. {
  450. return hdev->interleave_scan_state != INTERLEAVE_SCAN_NONE;
  451. }
  452. static int hci_passive_scan_sync(struct hci_dev *hdev);
  453. static void interleave_scan_work(struct work_struct *work)
  454. {
  455. struct hci_dev *hdev = container_of(work, struct hci_dev,
  456. interleave_scan.work);
  457. unsigned long timeout;
  458. if (hdev->interleave_scan_state == INTERLEAVE_SCAN_ALLOWLIST) {
  459. timeout = msecs_to_jiffies(hdev->advmon_allowlist_duration);
  460. } else if (hdev->interleave_scan_state == INTERLEAVE_SCAN_NO_FILTER) {
  461. timeout = msecs_to_jiffies(hdev->advmon_no_filter_duration);
  462. } else {
  463. bt_dev_err(hdev, "unexpected error");
  464. return;
  465. }
  466. hci_passive_scan_sync(hdev);
  467. hci_dev_lock(hdev);
  468. switch (hdev->interleave_scan_state) {
  469. case INTERLEAVE_SCAN_ALLOWLIST:
  470. bt_dev_dbg(hdev, "next state: allowlist");
  471. hdev->interleave_scan_state = INTERLEAVE_SCAN_NO_FILTER;
  472. break;
  473. case INTERLEAVE_SCAN_NO_FILTER:
  474. bt_dev_dbg(hdev, "next state: no filter");
  475. hdev->interleave_scan_state = INTERLEAVE_SCAN_ALLOWLIST;
  476. break;
  477. case INTERLEAVE_SCAN_NONE:
  478. bt_dev_err(hdev, "unexpected error");
  479. }
  480. hci_dev_unlock(hdev);
  481. /* Don't continue interleaving if it was canceled */
  482. if (is_interleave_scanning(hdev))
  483. queue_delayed_work(hdev->req_workqueue,
  484. &hdev->interleave_scan, timeout);
  485. }
  486. void hci_cmd_sync_init(struct hci_dev *hdev)
  487. {
  488. INIT_WORK(&hdev->cmd_sync_work, hci_cmd_sync_work);
  489. INIT_LIST_HEAD(&hdev->cmd_sync_work_list);
  490. mutex_init(&hdev->cmd_sync_work_lock);
  491. mutex_init(&hdev->unregister_lock);
  492. INIT_WORK(&hdev->cmd_sync_cancel_work, hci_cmd_sync_cancel_work);
  493. INIT_WORK(&hdev->reenable_adv_work, reenable_adv);
  494. INIT_DELAYED_WORK(&hdev->le_scan_disable, le_scan_disable);
  495. INIT_DELAYED_WORK(&hdev->adv_instance_expire, adv_timeout_expire);
  496. INIT_DELAYED_WORK(&hdev->interleave_scan, interleave_scan_work);
  497. }
  498. static void _hci_cmd_sync_cancel_entry(struct hci_dev *hdev,
  499. struct hci_cmd_sync_work_entry *entry,
  500. int err)
  501. {
  502. if (entry->destroy)
  503. entry->destroy(hdev, entry->data, err);
  504. list_del(&entry->list);
  505. kfree(entry);
  506. }
  507. void hci_cmd_sync_clear(struct hci_dev *hdev)
  508. {
  509. struct hci_cmd_sync_work_entry *entry, *tmp;
  510. cancel_work_sync(&hdev->cmd_sync_work);
  511. cancel_work_sync(&hdev->reenable_adv_work);
  512. mutex_lock(&hdev->cmd_sync_work_lock);
  513. list_for_each_entry_safe(entry, tmp, &hdev->cmd_sync_work_list, list)
  514. _hci_cmd_sync_cancel_entry(hdev, entry, -ECANCELED);
  515. mutex_unlock(&hdev->cmd_sync_work_lock);
  516. }
  517. void hci_cmd_sync_cancel(struct hci_dev *hdev, int err)
  518. {
  519. bt_dev_dbg(hdev, "err 0x%2.2x", err);
  520. if (READ_ONCE(hdev->req_status) == HCI_REQ_PEND) {
  521. hdev->req_result = err;
  522. WRITE_ONCE(hdev->req_status, HCI_REQ_CANCELED);
  523. queue_work(hdev->workqueue, &hdev->cmd_sync_cancel_work);
  524. }
  525. }
  526. EXPORT_SYMBOL(hci_cmd_sync_cancel);
  527. /* Cancel ongoing command request synchronously:
  528. *
  529. * - Set result and mark status to HCI_REQ_CANCELED
  530. * - Wakeup command sync thread
  531. */
  532. void hci_cmd_sync_cancel_sync(struct hci_dev *hdev, int err)
  533. {
  534. bt_dev_dbg(hdev, "err 0x%2.2x", err);
  535. if (READ_ONCE(hdev->req_status) == HCI_REQ_PEND) {
  536. /* req_result is __u32 so error must be positive to be properly
  537. * propagated.
  538. */
  539. hdev->req_result = err < 0 ? -err : err;
  540. WRITE_ONCE(hdev->req_status, HCI_REQ_CANCELED);
  541. wake_up_interruptible(&hdev->req_wait_q);
  542. }
  543. }
  544. EXPORT_SYMBOL(hci_cmd_sync_cancel_sync);
  545. /* Submit HCI command to be run in as cmd_sync_work:
  546. *
  547. * - hdev must _not_ be unregistered
  548. */
  549. int hci_cmd_sync_submit(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
  550. void *data, hci_cmd_sync_work_destroy_t destroy)
  551. {
  552. struct hci_cmd_sync_work_entry *entry;
  553. int err = 0;
  554. mutex_lock(&hdev->unregister_lock);
  555. if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) {
  556. err = -ENODEV;
  557. goto unlock;
  558. }
  559. entry = kmalloc_obj(*entry);
  560. if (!entry) {
  561. err = -ENOMEM;
  562. goto unlock;
  563. }
  564. entry->func = func;
  565. entry->data = data;
  566. entry->destroy = destroy;
  567. mutex_lock(&hdev->cmd_sync_work_lock);
  568. list_add_tail(&entry->list, &hdev->cmd_sync_work_list);
  569. mutex_unlock(&hdev->cmd_sync_work_lock);
  570. queue_work(hdev->req_workqueue, &hdev->cmd_sync_work);
  571. unlock:
  572. mutex_unlock(&hdev->unregister_lock);
  573. return err;
  574. }
  575. EXPORT_SYMBOL(hci_cmd_sync_submit);
  576. /* Queue HCI command:
  577. *
  578. * - hdev must be running
  579. */
  580. int hci_cmd_sync_queue(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
  581. void *data, hci_cmd_sync_work_destroy_t destroy)
  582. {
  583. /* Only queue command if hdev is running which means it had been opened
  584. * and is either on init phase or is already up.
  585. */
  586. if (!test_bit(HCI_RUNNING, &hdev->flags))
  587. return -ENETDOWN;
  588. return hci_cmd_sync_submit(hdev, func, data, destroy);
  589. }
  590. EXPORT_SYMBOL(hci_cmd_sync_queue);
  591. static struct hci_cmd_sync_work_entry *
  592. _hci_cmd_sync_lookup_entry(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
  593. void *data, hci_cmd_sync_work_destroy_t destroy)
  594. {
  595. struct hci_cmd_sync_work_entry *entry, *tmp;
  596. list_for_each_entry_safe(entry, tmp, &hdev->cmd_sync_work_list, list) {
  597. if (func && entry->func != func)
  598. continue;
  599. if (data && entry->data != data)
  600. continue;
  601. if (destroy && entry->destroy != destroy)
  602. continue;
  603. return entry;
  604. }
  605. return NULL;
  606. }
  607. /* Queue HCI command entry once:
  608. *
  609. * - Lookup if an entry already exist and only if it doesn't creates a new entry
  610. * and queue it.
  611. */
  612. int hci_cmd_sync_queue_once(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
  613. void *data, hci_cmd_sync_work_destroy_t destroy)
  614. {
  615. if (hci_cmd_sync_lookup_entry(hdev, func, data, destroy))
  616. return -EEXIST;
  617. return hci_cmd_sync_queue(hdev, func, data, destroy);
  618. }
  619. EXPORT_SYMBOL(hci_cmd_sync_queue_once);
  620. /* Run HCI command:
  621. *
  622. * - hdev must be running
  623. * - if on cmd_sync_work then run immediately otherwise queue
  624. */
  625. int hci_cmd_sync_run(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
  626. void *data, hci_cmd_sync_work_destroy_t destroy)
  627. {
  628. /* Only queue command if hdev is running which means it had been opened
  629. * and is either on init phase or is already up.
  630. */
  631. if (!test_bit(HCI_RUNNING, &hdev->flags))
  632. return -ENETDOWN;
  633. /* If on cmd_sync_work then run immediately otherwise queue */
  634. if (current_work() == &hdev->cmd_sync_work) {
  635. int err;
  636. err = func(hdev, data);
  637. if (destroy)
  638. destroy(hdev, data, err);
  639. return 0;
  640. }
  641. return hci_cmd_sync_submit(hdev, func, data, destroy);
  642. }
  643. EXPORT_SYMBOL(hci_cmd_sync_run);
  644. /* Run HCI command entry once:
  645. *
  646. * - Lookup if an entry already exist and only if it doesn't creates a new entry
  647. * and run it.
  648. * - if on cmd_sync_work then run immediately otherwise queue
  649. */
  650. int hci_cmd_sync_run_once(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
  651. void *data, hci_cmd_sync_work_destroy_t destroy)
  652. {
  653. if (hci_cmd_sync_lookup_entry(hdev, func, data, destroy))
  654. return 0;
  655. return hci_cmd_sync_run(hdev, func, data, destroy);
  656. }
  657. EXPORT_SYMBOL(hci_cmd_sync_run_once);
  658. /* Lookup HCI command entry:
  659. *
  660. * - Return first entry that matches by function callback or data or
  661. * destroy callback.
  662. */
  663. struct hci_cmd_sync_work_entry *
  664. hci_cmd_sync_lookup_entry(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
  665. void *data, hci_cmd_sync_work_destroy_t destroy)
  666. {
  667. struct hci_cmd_sync_work_entry *entry;
  668. mutex_lock(&hdev->cmd_sync_work_lock);
  669. entry = _hci_cmd_sync_lookup_entry(hdev, func, data, destroy);
  670. mutex_unlock(&hdev->cmd_sync_work_lock);
  671. return entry;
  672. }
  673. EXPORT_SYMBOL(hci_cmd_sync_lookup_entry);
  674. /* Cancel HCI command entry */
  675. void hci_cmd_sync_cancel_entry(struct hci_dev *hdev,
  676. struct hci_cmd_sync_work_entry *entry)
  677. {
  678. mutex_lock(&hdev->cmd_sync_work_lock);
  679. _hci_cmd_sync_cancel_entry(hdev, entry, -ECANCELED);
  680. mutex_unlock(&hdev->cmd_sync_work_lock);
  681. }
  682. EXPORT_SYMBOL(hci_cmd_sync_cancel_entry);
  683. /* Dequeue one HCI command entry:
  684. *
  685. * - Lookup and cancel first entry that matches.
  686. */
  687. bool hci_cmd_sync_dequeue_once(struct hci_dev *hdev,
  688. hci_cmd_sync_work_func_t func,
  689. void *data, hci_cmd_sync_work_destroy_t destroy)
  690. {
  691. struct hci_cmd_sync_work_entry *entry;
  692. mutex_lock(&hdev->cmd_sync_work_lock);
  693. entry = _hci_cmd_sync_lookup_entry(hdev, func, data, destroy);
  694. if (!entry) {
  695. mutex_unlock(&hdev->cmd_sync_work_lock);
  696. return false;
  697. }
  698. _hci_cmd_sync_cancel_entry(hdev, entry, -ECANCELED);
  699. mutex_unlock(&hdev->cmd_sync_work_lock);
  700. return true;
  701. }
  702. EXPORT_SYMBOL(hci_cmd_sync_dequeue_once);
  703. /* Dequeue HCI command entry:
  704. *
  705. * - Lookup and cancel any entry that matches by function callback or data or
  706. * destroy callback.
  707. */
  708. bool hci_cmd_sync_dequeue(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
  709. void *data, hci_cmd_sync_work_destroy_t destroy)
  710. {
  711. struct hci_cmd_sync_work_entry *entry;
  712. bool ret = false;
  713. mutex_lock(&hdev->cmd_sync_work_lock);
  714. while ((entry = _hci_cmd_sync_lookup_entry(hdev, func, data,
  715. destroy))) {
  716. _hci_cmd_sync_cancel_entry(hdev, entry, -ECANCELED);
  717. ret = true;
  718. }
  719. mutex_unlock(&hdev->cmd_sync_work_lock);
  720. return ret;
  721. }
  722. EXPORT_SYMBOL(hci_cmd_sync_dequeue);
  723. int hci_update_eir_sync(struct hci_dev *hdev)
  724. {
  725. struct hci_cp_write_eir cp;
  726. bt_dev_dbg(hdev, "");
  727. if (!hdev_is_powered(hdev))
  728. return 0;
  729. if (!lmp_ext_inq_capable(hdev))
  730. return 0;
  731. if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
  732. return 0;
  733. if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
  734. return 0;
  735. memset(&cp, 0, sizeof(cp));
  736. eir_create(hdev, cp.data);
  737. if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0)
  738. return 0;
  739. memcpy(hdev->eir, cp.data, sizeof(cp.data));
  740. return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp,
  741. HCI_CMD_TIMEOUT);
  742. }
  743. static u8 get_service_classes(struct hci_dev *hdev)
  744. {
  745. struct bt_uuid *uuid;
  746. u8 val = 0;
  747. list_for_each_entry(uuid, &hdev->uuids, list)
  748. val |= uuid->svc_hint;
  749. return val;
  750. }
  751. int hci_update_class_sync(struct hci_dev *hdev)
  752. {
  753. u8 cod[3];
  754. bt_dev_dbg(hdev, "");
  755. if (!hdev_is_powered(hdev))
  756. return 0;
  757. if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
  758. return 0;
  759. if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
  760. return 0;
  761. cod[0] = hdev->minor_class;
  762. cod[1] = hdev->major_class;
  763. cod[2] = get_service_classes(hdev);
  764. if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE))
  765. cod[1] |= 0x20;
  766. if (memcmp(cod, hdev->dev_class, 3) == 0)
  767. return 0;
  768. return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CLASS_OF_DEV,
  769. sizeof(cod), cod, HCI_CMD_TIMEOUT);
  770. }
  771. static bool is_advertising_allowed(struct hci_dev *hdev, bool connectable)
  772. {
  773. /* If there is no connection we are OK to advertise. */
  774. if (hci_conn_num(hdev, LE_LINK) == 0)
  775. return true;
  776. /* Check le_states if there is any connection in peripheral role. */
  777. if (hdev->conn_hash.le_num_peripheral > 0) {
  778. /* Peripheral connection state and non connectable mode
  779. * bit 20.
  780. */
  781. if (!connectable && !(hdev->le_states[2] & 0x10))
  782. return false;
  783. /* Peripheral connection state and connectable mode bit 38
  784. * and scannable bit 21.
  785. */
  786. if (connectable && (!(hdev->le_states[4] & 0x40) ||
  787. !(hdev->le_states[2] & 0x20)))
  788. return false;
  789. }
  790. /* Check le_states if there is any connection in central role. */
  791. if (hci_conn_num(hdev, LE_LINK) != hdev->conn_hash.le_num_peripheral) {
  792. /* Central connection state and non connectable mode bit 18. */
  793. if (!connectable && !(hdev->le_states[2] & 0x02))
  794. return false;
  795. /* Central connection state and connectable mode bit 35 and
  796. * scannable 19.
  797. */
  798. if (connectable && (!(hdev->le_states[4] & 0x08) ||
  799. !(hdev->le_states[2] & 0x08)))
  800. return false;
  801. }
  802. return true;
  803. }
  804. static bool adv_use_rpa(struct hci_dev *hdev, uint32_t flags)
  805. {
  806. /* If privacy is not enabled don't use RPA */
  807. if (!hci_dev_test_flag(hdev, HCI_PRIVACY))
  808. return false;
  809. /* If basic privacy mode is enabled use RPA */
  810. if (!hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY))
  811. return true;
  812. /* If limited privacy mode is enabled don't use RPA if we're
  813. * both discoverable and bondable.
  814. */
  815. if ((flags & MGMT_ADV_FLAG_DISCOV) &&
  816. hci_dev_test_flag(hdev, HCI_BONDABLE))
  817. return false;
  818. /* We're neither bondable nor discoverable in the limited
  819. * privacy mode, therefore use RPA.
  820. */
  821. return true;
  822. }
  823. static int hci_set_random_addr_sync(struct hci_dev *hdev, bdaddr_t *rpa)
  824. {
  825. /* If a random_addr has been set we're advertising or initiating an LE
  826. * connection we can't go ahead and change the random address at this
  827. * time. This is because the eventual initiator address used for the
  828. * subsequently created connection will be undefined (some
  829. * controllers use the new address and others the one we had
  830. * when the operation started).
  831. *
  832. * In this kind of scenario skip the update and let the random
  833. * address be updated at the next cycle.
  834. */
  835. if (bacmp(&hdev->random_addr, BDADDR_ANY) &&
  836. (hci_dev_test_flag(hdev, HCI_LE_ADV) ||
  837. hci_lookup_le_connect(hdev))) {
  838. bt_dev_dbg(hdev, "Deferring random address update");
  839. hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
  840. return 0;
  841. }
  842. return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_RANDOM_ADDR,
  843. 6, rpa, HCI_CMD_TIMEOUT);
  844. }
  845. int hci_update_random_address_sync(struct hci_dev *hdev, bool require_privacy,
  846. bool rpa, u8 *own_addr_type)
  847. {
  848. int err;
  849. /* If privacy is enabled use a resolvable private address. If
  850. * current RPA has expired or there is something else than
  851. * the current RPA in use, then generate a new one.
  852. */
  853. if (rpa) {
  854. /* If Controller supports LL Privacy use own address type is
  855. * 0x03
  856. */
  857. if (ll_privacy_capable(hdev))
  858. *own_addr_type = ADDR_LE_DEV_RANDOM_RESOLVED;
  859. else
  860. *own_addr_type = ADDR_LE_DEV_RANDOM;
  861. /* Check if RPA is valid */
  862. if (rpa_valid(hdev))
  863. return 0;
  864. err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa);
  865. if (err < 0) {
  866. bt_dev_err(hdev, "failed to generate new RPA");
  867. return err;
  868. }
  869. err = hci_set_random_addr_sync(hdev, &hdev->rpa);
  870. if (err)
  871. return err;
  872. return 0;
  873. }
  874. /* In case of required privacy without resolvable private address,
  875. * use an non-resolvable private address. This is useful for active
  876. * scanning and non-connectable advertising.
  877. */
  878. if (require_privacy) {
  879. bdaddr_t nrpa;
  880. while (true) {
  881. /* The non-resolvable private address is generated
  882. * from random six bytes with the two most significant
  883. * bits cleared.
  884. */
  885. get_random_bytes(&nrpa, 6);
  886. nrpa.b[5] &= 0x3f;
  887. /* The non-resolvable private address shall not be
  888. * equal to the public address.
  889. */
  890. if (bacmp(&hdev->bdaddr, &nrpa))
  891. break;
  892. }
  893. *own_addr_type = ADDR_LE_DEV_RANDOM;
  894. return hci_set_random_addr_sync(hdev, &nrpa);
  895. }
  896. /* If forcing static address is in use or there is no public
  897. * address use the static address as random address (but skip
  898. * the HCI command if the current random address is already the
  899. * static one.
  900. *
  901. * In case BR/EDR has been disabled on a dual-mode controller
  902. * and a static address has been configured, then use that
  903. * address instead of the public BR/EDR address.
  904. */
  905. if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
  906. !bacmp(&hdev->bdaddr, BDADDR_ANY) ||
  907. (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
  908. bacmp(&hdev->static_addr, BDADDR_ANY))) {
  909. *own_addr_type = ADDR_LE_DEV_RANDOM;
  910. if (bacmp(&hdev->static_addr, &hdev->random_addr))
  911. return hci_set_random_addr_sync(hdev,
  912. &hdev->static_addr);
  913. return 0;
  914. }
  915. /* Neither privacy nor static address is being used so use a
  916. * public address.
  917. */
  918. *own_addr_type = ADDR_LE_DEV_PUBLIC;
  919. return 0;
  920. }
  921. static int hci_disable_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance)
  922. {
  923. struct hci_cp_le_set_ext_adv_enable *cp;
  924. struct hci_cp_ext_adv_set *set;
  925. u8 data[sizeof(*cp) + sizeof(*set) * 1];
  926. u8 size;
  927. struct adv_info *adv = NULL;
  928. /* If request specifies an instance that doesn't exist, fail */
  929. if (instance > 0) {
  930. adv = hci_find_adv_instance(hdev, instance);
  931. if (!adv)
  932. return -EINVAL;
  933. /* If not enabled there is nothing to do */
  934. if (!adv->enabled)
  935. return 0;
  936. }
  937. memset(data, 0, sizeof(data));
  938. cp = (void *)data;
  939. set = (void *)cp->data;
  940. /* Instance 0x00 indicates all advertising instances will be disabled */
  941. cp->num_of_sets = !!instance;
  942. cp->enable = 0x00;
  943. set->handle = adv ? adv->handle : instance;
  944. size = sizeof(*cp) + sizeof(*set) * cp->num_of_sets;
  945. return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE,
  946. size, data, HCI_CMD_TIMEOUT);
  947. }
  948. static int hci_set_adv_set_random_addr_sync(struct hci_dev *hdev, u8 instance,
  949. bdaddr_t *random_addr)
  950. {
  951. struct hci_cp_le_set_adv_set_rand_addr cp;
  952. int err;
  953. if (!instance) {
  954. /* Instance 0x00 doesn't have an adv_info, instead it uses
  955. * hdev->random_addr to track its address so whenever it needs
  956. * to be updated this also set the random address since
  957. * hdev->random_addr is shared with scan state machine.
  958. */
  959. err = hci_set_random_addr_sync(hdev, random_addr);
  960. if (err)
  961. return err;
  962. }
  963. memset(&cp, 0, sizeof(cp));
  964. cp.handle = instance;
  965. bacpy(&cp.bdaddr, random_addr);
  966. return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_SET_RAND_ADDR,
  967. sizeof(cp), &cp, HCI_CMD_TIMEOUT);
  968. }
  969. static int
  970. hci_set_ext_adv_params_sync(struct hci_dev *hdev, struct adv_info *adv,
  971. const struct hci_cp_le_set_ext_adv_params *cp,
  972. struct hci_rp_le_set_ext_adv_params *rp)
  973. {
  974. struct sk_buff *skb;
  975. skb = __hci_cmd_sync(hdev, HCI_OP_LE_SET_EXT_ADV_PARAMS, sizeof(*cp),
  976. cp, HCI_CMD_TIMEOUT);
  977. /* If command return a status event, skb will be set to -ENODATA */
  978. if (skb == ERR_PTR(-ENODATA))
  979. return 0;
  980. if (IS_ERR(skb)) {
  981. bt_dev_err(hdev, "Opcode 0x%4.4x failed: %ld",
  982. HCI_OP_LE_SET_EXT_ADV_PARAMS, PTR_ERR(skb));
  983. return PTR_ERR(skb);
  984. }
  985. if (skb->len != sizeof(*rp)) {
  986. bt_dev_err(hdev, "Invalid response length for 0x%4.4x: %u",
  987. HCI_OP_LE_SET_EXT_ADV_PARAMS, skb->len);
  988. kfree_skb(skb);
  989. return -EIO;
  990. }
  991. memcpy(rp, skb->data, sizeof(*rp));
  992. kfree_skb(skb);
  993. if (!rp->status) {
  994. hdev->adv_addr_type = cp->own_addr_type;
  995. if (!cp->handle) {
  996. /* Store in hdev for instance 0 */
  997. hdev->adv_tx_power = rp->tx_power;
  998. } else if (adv) {
  999. adv->tx_power = rp->tx_power;
  1000. }
  1001. }
  1002. return rp->status;
  1003. }
  1004. static int hci_set_ext_adv_data_sync(struct hci_dev *hdev, u8 instance)
  1005. {
  1006. DEFINE_FLEX(struct hci_cp_le_set_ext_adv_data, pdu, data, length,
  1007. HCI_MAX_EXT_AD_LENGTH);
  1008. u8 len;
  1009. struct adv_info *adv = NULL;
  1010. int err;
  1011. if (instance) {
  1012. adv = hci_find_adv_instance(hdev, instance);
  1013. if (!adv || !adv->adv_data_changed)
  1014. return 0;
  1015. }
  1016. len = eir_create_adv_data(hdev, instance, pdu->data,
  1017. HCI_MAX_EXT_AD_LENGTH);
  1018. pdu->length = len;
  1019. pdu->handle = adv ? adv->handle : instance;
  1020. pdu->operation = LE_SET_ADV_DATA_OP_COMPLETE;
  1021. pdu->frag_pref = LE_SET_ADV_DATA_NO_FRAG;
  1022. err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_DATA,
  1023. struct_size(pdu, data, len), pdu,
  1024. HCI_CMD_TIMEOUT);
  1025. if (err)
  1026. return err;
  1027. /* Update data if the command succeed */
  1028. if (adv) {
  1029. adv->adv_data_changed = false;
  1030. } else {
  1031. memcpy(hdev->adv_data, pdu->data, len);
  1032. hdev->adv_data_len = len;
  1033. }
  1034. return 0;
  1035. }
  1036. static int hci_set_adv_data_sync(struct hci_dev *hdev, u8 instance)
  1037. {
  1038. struct hci_cp_le_set_adv_data cp;
  1039. u8 len;
  1040. memset(&cp, 0, sizeof(cp));
  1041. len = eir_create_adv_data(hdev, instance, cp.data, sizeof(cp.data));
  1042. /* There's nothing to do if the data hasn't changed */
  1043. if (hdev->adv_data_len == len &&
  1044. memcmp(cp.data, hdev->adv_data, len) == 0)
  1045. return 0;
  1046. memcpy(hdev->adv_data, cp.data, sizeof(cp.data));
  1047. hdev->adv_data_len = len;
  1048. cp.length = len;
  1049. return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_DATA,
  1050. sizeof(cp), &cp, HCI_CMD_TIMEOUT);
  1051. }
  1052. int hci_update_adv_data_sync(struct hci_dev *hdev, u8 instance)
  1053. {
  1054. if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
  1055. return 0;
  1056. if (ext_adv_capable(hdev))
  1057. return hci_set_ext_adv_data_sync(hdev, instance);
  1058. return hci_set_adv_data_sync(hdev, instance);
  1059. }
  1060. int hci_setup_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance)
  1061. {
  1062. struct hci_cp_le_set_ext_adv_params cp;
  1063. struct hci_rp_le_set_ext_adv_params rp;
  1064. bool connectable, require_privacy;
  1065. u32 flags;
  1066. bdaddr_t random_addr;
  1067. u8 own_addr_type;
  1068. int err;
  1069. struct adv_info *adv;
  1070. bool secondary_adv;
  1071. if (instance > 0) {
  1072. adv = hci_find_adv_instance(hdev, instance);
  1073. if (!adv)
  1074. return -EINVAL;
  1075. } else {
  1076. adv = NULL;
  1077. }
  1078. /* Updating parameters of an active instance will return a
  1079. * Command Disallowed error, so we must first disable the
  1080. * instance if it is active.
  1081. */
  1082. if (adv) {
  1083. err = hci_disable_ext_adv_instance_sync(hdev, instance);
  1084. if (err)
  1085. return err;
  1086. }
  1087. flags = hci_adv_instance_flags(hdev, instance);
  1088. /* If the "connectable" instance flag was not set, then choose between
  1089. * ADV_IND and ADV_NONCONN_IND based on the global connectable setting.
  1090. */
  1091. connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) ||
  1092. mgmt_get_connectable(hdev);
  1093. if (!is_advertising_allowed(hdev, connectable))
  1094. return -EPERM;
  1095. /* Set require_privacy to true only when non-connectable
  1096. * advertising is used and it is not periodic.
  1097. * In that case it is fine to use a non-resolvable private address.
  1098. */
  1099. require_privacy = !connectable && !(adv && adv->periodic);
  1100. err = hci_get_random_address(hdev, require_privacy,
  1101. adv_use_rpa(hdev, flags), adv,
  1102. &own_addr_type, &random_addr);
  1103. if (err < 0)
  1104. return err;
  1105. memset(&cp, 0, sizeof(cp));
  1106. if (adv) {
  1107. hci_cpu_to_le24(adv->min_interval, cp.min_interval);
  1108. hci_cpu_to_le24(adv->max_interval, cp.max_interval);
  1109. cp.tx_power = adv->tx_power;
  1110. cp.sid = adv->sid;
  1111. } else {
  1112. hci_cpu_to_le24(hdev->le_adv_min_interval, cp.min_interval);
  1113. hci_cpu_to_le24(hdev->le_adv_max_interval, cp.max_interval);
  1114. cp.tx_power = HCI_ADV_TX_POWER_NO_PREFERENCE;
  1115. cp.sid = 0x00;
  1116. }
  1117. secondary_adv = (flags & MGMT_ADV_FLAG_SEC_MASK);
  1118. if (connectable) {
  1119. if (secondary_adv)
  1120. cp.evt_properties = cpu_to_le16(LE_EXT_ADV_CONN_IND);
  1121. else
  1122. cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_IND);
  1123. } else if (hci_adv_instance_is_scannable(hdev, instance) ||
  1124. (flags & MGMT_ADV_PARAM_SCAN_RSP)) {
  1125. if (secondary_adv)
  1126. cp.evt_properties = cpu_to_le16(LE_EXT_ADV_SCAN_IND);
  1127. else
  1128. cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_SCAN_IND);
  1129. } else {
  1130. if (secondary_adv)
  1131. cp.evt_properties = cpu_to_le16(LE_EXT_ADV_NON_CONN_IND);
  1132. else
  1133. cp.evt_properties = cpu_to_le16(LE_LEGACY_NONCONN_IND);
  1134. }
  1135. /* If Own_Address_Type equals 0x02 or 0x03, the Peer_Address parameter
  1136. * contains the peer’s Identity Address and the Peer_Address_Type
  1137. * parameter contains the peer’s Identity Type (i.e., 0x00 or 0x01).
  1138. * These parameters are used to locate the corresponding local IRK in
  1139. * the resolving list; this IRK is used to generate their own address
  1140. * used in the advertisement.
  1141. */
  1142. if (own_addr_type == ADDR_LE_DEV_RANDOM_RESOLVED)
  1143. hci_copy_identity_address(hdev, &cp.peer_addr,
  1144. &cp.peer_addr_type);
  1145. cp.own_addr_type = own_addr_type;
  1146. cp.channel_map = hdev->le_adv_channel_map;
  1147. cp.handle = adv ? adv->handle : instance;
  1148. if (flags & MGMT_ADV_FLAG_SEC_2M) {
  1149. cp.primary_phy = HCI_ADV_PHY_1M;
  1150. cp.secondary_phy = HCI_ADV_PHY_2M;
  1151. } else if (flags & MGMT_ADV_FLAG_SEC_CODED) {
  1152. cp.primary_phy = HCI_ADV_PHY_CODED;
  1153. cp.secondary_phy = HCI_ADV_PHY_CODED;
  1154. } else {
  1155. /* In all other cases use 1M */
  1156. cp.primary_phy = HCI_ADV_PHY_1M;
  1157. cp.secondary_phy = HCI_ADV_PHY_1M;
  1158. }
  1159. err = hci_set_ext_adv_params_sync(hdev, adv, &cp, &rp);
  1160. if (err)
  1161. return err;
  1162. /* Update adv data as tx power is known now */
  1163. err = hci_set_ext_adv_data_sync(hdev, cp.handle);
  1164. if (err)
  1165. return err;
  1166. if ((own_addr_type == ADDR_LE_DEV_RANDOM ||
  1167. own_addr_type == ADDR_LE_DEV_RANDOM_RESOLVED) &&
  1168. bacmp(&random_addr, BDADDR_ANY)) {
  1169. /* Check if random address need to be updated */
  1170. if (adv) {
  1171. if (!bacmp(&random_addr, &adv->random_addr))
  1172. return 0;
  1173. } else {
  1174. if (!bacmp(&random_addr, &hdev->random_addr))
  1175. return 0;
  1176. }
  1177. return hci_set_adv_set_random_addr_sync(hdev, instance,
  1178. &random_addr);
  1179. }
  1180. return 0;
  1181. }
  1182. static int hci_set_ext_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
  1183. {
  1184. DEFINE_FLEX(struct hci_cp_le_set_ext_scan_rsp_data, pdu, data, length,
  1185. HCI_MAX_EXT_AD_LENGTH);
  1186. u8 len;
  1187. struct adv_info *adv = NULL;
  1188. int err;
  1189. if (instance) {
  1190. adv = hci_find_adv_instance(hdev, instance);
  1191. if (!adv || !adv->scan_rsp_changed)
  1192. return 0;
  1193. }
  1194. len = eir_create_scan_rsp(hdev, instance, pdu->data);
  1195. pdu->handle = adv ? adv->handle : instance;
  1196. pdu->length = len;
  1197. pdu->operation = LE_SET_ADV_DATA_OP_COMPLETE;
  1198. pdu->frag_pref = LE_SET_ADV_DATA_NO_FRAG;
  1199. err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_RSP_DATA,
  1200. struct_size(pdu, data, len), pdu,
  1201. HCI_CMD_TIMEOUT);
  1202. if (err)
  1203. return err;
  1204. if (adv) {
  1205. adv->scan_rsp_changed = false;
  1206. } else {
  1207. memcpy(hdev->scan_rsp_data, pdu->data, len);
  1208. hdev->scan_rsp_data_len = len;
  1209. }
  1210. return 0;
  1211. }
  1212. static int __hci_set_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
  1213. {
  1214. struct hci_cp_le_set_scan_rsp_data cp;
  1215. u8 len;
  1216. memset(&cp, 0, sizeof(cp));
  1217. len = eir_create_scan_rsp(hdev, instance, cp.data);
  1218. if (hdev->scan_rsp_data_len == len &&
  1219. !memcmp(cp.data, hdev->scan_rsp_data, len))
  1220. return 0;
  1221. memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data));
  1222. hdev->scan_rsp_data_len = len;
  1223. cp.length = len;
  1224. return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_RSP_DATA,
  1225. sizeof(cp), &cp, HCI_CMD_TIMEOUT);
  1226. }
  1227. int hci_update_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
  1228. {
  1229. if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
  1230. return 0;
  1231. if (ext_adv_capable(hdev))
  1232. return hci_set_ext_scan_rsp_data_sync(hdev, instance);
  1233. return __hci_set_scan_rsp_data_sync(hdev, instance);
  1234. }
  1235. int hci_enable_ext_advertising_sync(struct hci_dev *hdev, u8 instance)
  1236. {
  1237. struct hci_cp_le_set_ext_adv_enable *cp;
  1238. struct hci_cp_ext_adv_set *set;
  1239. u8 data[sizeof(*cp) + sizeof(*set) * 1];
  1240. struct adv_info *adv;
  1241. if (instance > 0) {
  1242. adv = hci_find_adv_instance(hdev, instance);
  1243. if (!adv)
  1244. return -EINVAL;
  1245. /* If already enabled there is nothing to do */
  1246. if (adv->enabled)
  1247. return 0;
  1248. } else {
  1249. adv = NULL;
  1250. }
  1251. cp = (void *)data;
  1252. set = (void *)cp->data;
  1253. memset(cp, 0, sizeof(*cp));
  1254. cp->enable = 0x01;
  1255. cp->num_of_sets = 0x01;
  1256. memset(set, 0, sizeof(*set));
  1257. set->handle = adv ? adv->handle : instance;
  1258. /* Set duration per instance since controller is responsible for
  1259. * scheduling it.
  1260. */
  1261. if (adv && adv->timeout) {
  1262. u16 duration = adv->timeout * MSEC_PER_SEC;
  1263. /* Time = N * 10 ms */
  1264. set->duration = cpu_to_le16(duration / 10);
  1265. }
  1266. return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE,
  1267. sizeof(*cp) +
  1268. sizeof(*set) * cp->num_of_sets,
  1269. data, HCI_CMD_TIMEOUT);
  1270. }
  1271. int hci_start_ext_adv_sync(struct hci_dev *hdev, u8 instance)
  1272. {
  1273. int err;
  1274. err = hci_setup_ext_adv_instance_sync(hdev, instance);
  1275. if (err)
  1276. return err;
  1277. err = hci_set_ext_scan_rsp_data_sync(hdev, instance);
  1278. if (err)
  1279. return err;
  1280. return hci_enable_ext_advertising_sync(hdev, instance);
  1281. }
  1282. int hci_disable_per_advertising_sync(struct hci_dev *hdev, u8 instance)
  1283. {
  1284. struct hci_cp_le_set_per_adv_enable cp;
  1285. struct adv_info *adv = NULL;
  1286. /* If periodic advertising already disabled there is nothing to do. */
  1287. adv = hci_find_adv_instance(hdev, instance);
  1288. if (!adv || !adv->periodic_enabled)
  1289. return 0;
  1290. memset(&cp, 0, sizeof(cp));
  1291. cp.enable = 0x00;
  1292. cp.handle = instance;
  1293. return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_ENABLE,
  1294. sizeof(cp), &cp, HCI_CMD_TIMEOUT);
  1295. }
  1296. static int hci_set_per_adv_params_sync(struct hci_dev *hdev, u8 instance,
  1297. u16 min_interval, u16 max_interval)
  1298. {
  1299. struct hci_cp_le_set_per_adv_params cp;
  1300. memset(&cp, 0, sizeof(cp));
  1301. if (!min_interval)
  1302. min_interval = DISCOV_LE_PER_ADV_INT_MIN;
  1303. if (!max_interval)
  1304. max_interval = DISCOV_LE_PER_ADV_INT_MAX;
  1305. cp.handle = instance;
  1306. cp.min_interval = cpu_to_le16(min_interval);
  1307. cp.max_interval = cpu_to_le16(max_interval);
  1308. cp.periodic_properties = 0x0000;
  1309. return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_PARAMS,
  1310. sizeof(cp), &cp, HCI_CMD_TIMEOUT);
  1311. }
  1312. static int hci_set_per_adv_data_sync(struct hci_dev *hdev, u8 instance)
  1313. {
  1314. DEFINE_FLEX(struct hci_cp_le_set_per_adv_data, pdu, data, length,
  1315. HCI_MAX_PER_AD_LENGTH);
  1316. u8 len;
  1317. struct adv_info *adv = NULL;
  1318. if (instance) {
  1319. adv = hci_find_adv_instance(hdev, instance);
  1320. if (!adv || !adv->periodic)
  1321. return 0;
  1322. }
  1323. len = eir_create_per_adv_data(hdev, instance, pdu->data);
  1324. pdu->length = len;
  1325. pdu->handle = adv ? adv->handle : instance;
  1326. pdu->operation = LE_SET_ADV_DATA_OP_COMPLETE;
  1327. return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_DATA,
  1328. struct_size(pdu, data, len), pdu,
  1329. HCI_CMD_TIMEOUT);
  1330. }
  1331. static int hci_enable_per_advertising_sync(struct hci_dev *hdev, u8 instance)
  1332. {
  1333. struct hci_cp_le_set_per_adv_enable cp;
  1334. struct adv_info *adv = NULL;
  1335. /* If periodic advertising already enabled there is nothing to do. */
  1336. adv = hci_find_adv_instance(hdev, instance);
  1337. if (adv && adv->periodic_enabled)
  1338. return 0;
  1339. memset(&cp, 0, sizeof(cp));
  1340. cp.enable = 0x01;
  1341. cp.handle = instance;
  1342. return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_ENABLE,
  1343. sizeof(cp), &cp, HCI_CMD_TIMEOUT);
  1344. }
  1345. /* Checks if periodic advertising data contains a Basic Announcement and if it
  1346. * does generates a Broadcast ID and add Broadcast Announcement.
  1347. */
  1348. static int hci_adv_bcast_annoucement(struct hci_dev *hdev, struct adv_info *adv)
  1349. {
  1350. u8 bid[3];
  1351. u8 ad[HCI_MAX_EXT_AD_LENGTH];
  1352. u8 len;
  1353. /* Skip if NULL adv as instance 0x00 is used for general purpose
  1354. * advertising so it cannot used for the likes of Broadcast Announcement
  1355. * as it can be overwritten at any point.
  1356. */
  1357. if (!adv)
  1358. return 0;
  1359. /* Check if PA data doesn't contains a Basic Audio Announcement then
  1360. * there is nothing to do.
  1361. */
  1362. if (!eir_get_service_data(adv->per_adv_data, adv->per_adv_data_len,
  1363. 0x1851, NULL))
  1364. return 0;
  1365. /* Check if advertising data already has a Broadcast Announcement since
  1366. * the process may want to control the Broadcast ID directly and in that
  1367. * case the kernel shall no interfere.
  1368. */
  1369. if (eir_get_service_data(adv->adv_data, adv->adv_data_len, 0x1852,
  1370. NULL))
  1371. return 0;
  1372. /* Generate Broadcast ID */
  1373. get_random_bytes(bid, sizeof(bid));
  1374. len = eir_append_service_data(ad, 0, 0x1852, bid, sizeof(bid));
  1375. memcpy(ad + len, adv->adv_data, adv->adv_data_len);
  1376. hci_set_adv_instance_data(hdev, adv->instance, len + adv->adv_data_len,
  1377. ad, 0, NULL);
  1378. return hci_update_adv_data_sync(hdev, adv->instance);
  1379. }
  1380. int hci_start_per_adv_sync(struct hci_dev *hdev, u8 instance, u8 sid,
  1381. u8 data_len, u8 *data, u32 flags, u16 min_interval,
  1382. u16 max_interval, u16 sync_interval)
  1383. {
  1384. struct adv_info *adv = NULL;
  1385. int err;
  1386. bool added = false;
  1387. hci_disable_per_advertising_sync(hdev, instance);
  1388. if (instance) {
  1389. adv = hci_find_adv_instance(hdev, instance);
  1390. if (adv) {
  1391. if (sid != HCI_SID_INVALID && adv->sid != sid) {
  1392. /* If the SID don't match attempt to find by
  1393. * SID.
  1394. */
  1395. adv = hci_find_adv_sid(hdev, sid);
  1396. if (!adv) {
  1397. bt_dev_err(hdev,
  1398. "Unable to find adv_info");
  1399. return -EINVAL;
  1400. }
  1401. }
  1402. /* Turn it into periodic advertising */
  1403. adv->periodic = true;
  1404. adv->per_adv_data_len = data_len;
  1405. if (data)
  1406. memcpy(adv->per_adv_data, data, data_len);
  1407. adv->flags = flags;
  1408. } else if (!adv) {
  1409. /* Create an instance if that could not be found */
  1410. adv = hci_add_per_instance(hdev, instance, sid, flags,
  1411. data_len, data,
  1412. sync_interval,
  1413. sync_interval);
  1414. if (IS_ERR(adv))
  1415. return PTR_ERR(adv);
  1416. adv->pending = false;
  1417. added = true;
  1418. }
  1419. }
  1420. /* Start advertising */
  1421. err = hci_start_ext_adv_sync(hdev, instance);
  1422. if (err < 0)
  1423. goto fail;
  1424. err = hci_adv_bcast_annoucement(hdev, adv);
  1425. if (err < 0)
  1426. goto fail;
  1427. err = hci_set_per_adv_params_sync(hdev, instance, min_interval,
  1428. max_interval);
  1429. if (err < 0)
  1430. goto fail;
  1431. err = hci_set_per_adv_data_sync(hdev, instance);
  1432. if (err < 0)
  1433. goto fail;
  1434. err = hci_enable_per_advertising_sync(hdev, instance);
  1435. if (err < 0)
  1436. goto fail;
  1437. return 0;
  1438. fail:
  1439. if (added)
  1440. hci_remove_adv_instance(hdev, instance);
  1441. return err;
  1442. }
  1443. static int hci_start_adv_sync(struct hci_dev *hdev, u8 instance)
  1444. {
  1445. int err;
  1446. if (ext_adv_capable(hdev))
  1447. return hci_start_ext_adv_sync(hdev, instance);
  1448. err = hci_update_adv_data_sync(hdev, instance);
  1449. if (err)
  1450. return err;
  1451. err = hci_update_scan_rsp_data_sync(hdev, instance);
  1452. if (err)
  1453. return err;
  1454. return hci_enable_advertising_sync(hdev);
  1455. }
  1456. int hci_enable_advertising_sync(struct hci_dev *hdev)
  1457. {
  1458. struct adv_info *adv_instance;
  1459. struct hci_cp_le_set_adv_param cp;
  1460. u8 own_addr_type, enable = 0x01;
  1461. bool connectable;
  1462. u16 adv_min_interval, adv_max_interval;
  1463. u32 flags;
  1464. u8 status;
  1465. if (ext_adv_capable(hdev))
  1466. return hci_enable_ext_advertising_sync(hdev,
  1467. hdev->cur_adv_instance);
  1468. flags = hci_adv_instance_flags(hdev, hdev->cur_adv_instance);
  1469. adv_instance = hci_find_adv_instance(hdev, hdev->cur_adv_instance);
  1470. /* If the "connectable" instance flag was not set, then choose between
  1471. * ADV_IND and ADV_NONCONN_IND based on the global connectable setting.
  1472. */
  1473. connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) ||
  1474. mgmt_get_connectable(hdev);
  1475. if (!is_advertising_allowed(hdev, connectable))
  1476. return -EINVAL;
  1477. status = hci_disable_advertising_sync(hdev);
  1478. if (status)
  1479. return status;
  1480. /* Clear the HCI_LE_ADV bit temporarily so that the
  1481. * hci_update_random_address knows that it's safe to go ahead
  1482. * and write a new random address. The flag will be set back on
  1483. * as soon as the SET_ADV_ENABLE HCI command completes.
  1484. */
  1485. hci_dev_clear_flag(hdev, HCI_LE_ADV);
  1486. /* Set require_privacy to true only when non-connectable
  1487. * advertising is used. In that case it is fine to use a
  1488. * non-resolvable private address.
  1489. */
  1490. status = hci_update_random_address_sync(hdev, !connectable,
  1491. adv_use_rpa(hdev, flags),
  1492. &own_addr_type);
  1493. if (status)
  1494. return status;
  1495. memset(&cp, 0, sizeof(cp));
  1496. if (adv_instance) {
  1497. adv_min_interval = adv_instance->min_interval;
  1498. adv_max_interval = adv_instance->max_interval;
  1499. } else {
  1500. adv_min_interval = hdev->le_adv_min_interval;
  1501. adv_max_interval = hdev->le_adv_max_interval;
  1502. }
  1503. if (connectable) {
  1504. cp.type = LE_ADV_IND;
  1505. } else {
  1506. if (hci_adv_instance_is_scannable(hdev, hdev->cur_adv_instance))
  1507. cp.type = LE_ADV_SCAN_IND;
  1508. else
  1509. cp.type = LE_ADV_NONCONN_IND;
  1510. if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE) ||
  1511. hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) {
  1512. adv_min_interval = DISCOV_LE_FAST_ADV_INT_MIN;
  1513. adv_max_interval = DISCOV_LE_FAST_ADV_INT_MAX;
  1514. }
  1515. }
  1516. cp.min_interval = cpu_to_le16(adv_min_interval);
  1517. cp.max_interval = cpu_to_le16(adv_max_interval);
  1518. cp.own_address_type = own_addr_type;
  1519. cp.channel_map = hdev->le_adv_channel_map;
  1520. status = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_PARAM,
  1521. sizeof(cp), &cp, HCI_CMD_TIMEOUT);
  1522. if (status)
  1523. return status;
  1524. return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
  1525. sizeof(enable), &enable, HCI_CMD_TIMEOUT);
  1526. }
  1527. static int enable_advertising_sync(struct hci_dev *hdev, void *data)
  1528. {
  1529. return hci_enable_advertising_sync(hdev);
  1530. }
  1531. int hci_enable_advertising(struct hci_dev *hdev)
  1532. {
  1533. if (!hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
  1534. list_empty(&hdev->adv_instances))
  1535. return 0;
  1536. return hci_cmd_sync_queue(hdev, enable_advertising_sync, NULL, NULL);
  1537. }
  1538. int hci_remove_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance,
  1539. struct sock *sk)
  1540. {
  1541. int err;
  1542. if (!ext_adv_capable(hdev))
  1543. return 0;
  1544. err = hci_disable_ext_adv_instance_sync(hdev, instance);
  1545. if (err)
  1546. return err;
  1547. /* If request specifies an instance that doesn't exist, fail */
  1548. if (instance > 0 && !hci_find_adv_instance(hdev, instance))
  1549. return -EINVAL;
  1550. return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_REMOVE_ADV_SET,
  1551. sizeof(instance), &instance, 0,
  1552. HCI_CMD_TIMEOUT, sk);
  1553. }
  1554. int hci_le_terminate_big_sync(struct hci_dev *hdev, u8 handle, u8 reason)
  1555. {
  1556. struct hci_cp_le_term_big cp;
  1557. memset(&cp, 0, sizeof(cp));
  1558. cp.handle = handle;
  1559. cp.reason = reason;
  1560. return __hci_cmd_sync_status(hdev, HCI_OP_LE_TERM_BIG,
  1561. sizeof(cp), &cp, HCI_CMD_TIMEOUT);
  1562. }
  1563. int hci_schedule_adv_instance_sync(struct hci_dev *hdev, u8 instance,
  1564. bool force)
  1565. {
  1566. struct adv_info *adv = NULL;
  1567. u16 timeout;
  1568. if (hci_dev_test_flag(hdev, HCI_ADVERTISING) && !ext_adv_capable(hdev))
  1569. return -EPERM;
  1570. if (hdev->adv_instance_timeout)
  1571. return -EBUSY;
  1572. adv = hci_find_adv_instance(hdev, instance);
  1573. if (!adv)
  1574. return -ENOENT;
  1575. /* A zero timeout means unlimited advertising. As long as there is
  1576. * only one instance, duration should be ignored. We still set a timeout
  1577. * in case further instances are being added later on.
  1578. *
  1579. * If the remaining lifetime of the instance is more than the duration
  1580. * then the timeout corresponds to the duration, otherwise it will be
  1581. * reduced to the remaining instance lifetime.
  1582. */
  1583. if (adv->timeout == 0 || adv->duration <= adv->remaining_time)
  1584. timeout = adv->duration;
  1585. else
  1586. timeout = adv->remaining_time;
  1587. /* The remaining time is being reduced unless the instance is being
  1588. * advertised without time limit.
  1589. */
  1590. if (adv->timeout)
  1591. adv->remaining_time = adv->remaining_time - timeout;
  1592. /* Only use work for scheduling instances with legacy advertising */
  1593. if (!ext_adv_capable(hdev)) {
  1594. hdev->adv_instance_timeout = timeout;
  1595. queue_delayed_work(hdev->req_workqueue,
  1596. &hdev->adv_instance_expire,
  1597. secs_to_jiffies(timeout));
  1598. }
  1599. /* If we're just re-scheduling the same instance again then do not
  1600. * execute any HCI commands. This happens when a single instance is
  1601. * being advertised.
  1602. */
  1603. if (!force && hdev->cur_adv_instance == instance &&
  1604. hci_dev_test_flag(hdev, HCI_LE_ADV))
  1605. return 0;
  1606. hdev->cur_adv_instance = instance;
  1607. return hci_start_adv_sync(hdev, instance);
  1608. }
  1609. static int hci_clear_adv_sets_sync(struct hci_dev *hdev, struct sock *sk)
  1610. {
  1611. int err;
  1612. if (!ext_adv_capable(hdev))
  1613. return 0;
  1614. /* Disable instance 0x00 to disable all instances */
  1615. err = hci_disable_ext_adv_instance_sync(hdev, 0x00);
  1616. if (err)
  1617. return err;
  1618. return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CLEAR_ADV_SETS,
  1619. 0, NULL, 0, HCI_CMD_TIMEOUT, sk);
  1620. }
  1621. static int hci_clear_adv_sync(struct hci_dev *hdev, struct sock *sk, bool force)
  1622. {
  1623. struct adv_info *adv, *n;
  1624. if (ext_adv_capable(hdev))
  1625. /* Remove all existing sets */
  1626. return hci_clear_adv_sets_sync(hdev, sk);
  1627. /* This is safe as long as there is no command send while the lock is
  1628. * held.
  1629. */
  1630. hci_dev_lock(hdev);
  1631. /* Cleanup non-ext instances */
  1632. list_for_each_entry_safe(adv, n, &hdev->adv_instances, list) {
  1633. u8 instance = adv->instance;
  1634. int err;
  1635. if (!(force || adv->timeout))
  1636. continue;
  1637. err = hci_remove_adv_instance(hdev, instance);
  1638. if (!err)
  1639. mgmt_advertising_removed(sk, hdev, instance);
  1640. }
  1641. hci_dev_unlock(hdev);
  1642. return 0;
  1643. }
  1644. static int hci_remove_adv_sync(struct hci_dev *hdev, u8 instance,
  1645. struct sock *sk)
  1646. {
  1647. int err;
  1648. /* If we use extended advertising, instance has to be removed first. */
  1649. if (ext_adv_capable(hdev))
  1650. return hci_remove_ext_adv_instance_sync(hdev, instance, sk);
  1651. /* This is safe as long as there is no command send while the lock is
  1652. * held.
  1653. */
  1654. hci_dev_lock(hdev);
  1655. err = hci_remove_adv_instance(hdev, instance);
  1656. if (!err)
  1657. mgmt_advertising_removed(sk, hdev, instance);
  1658. hci_dev_unlock(hdev);
  1659. return err;
  1660. }
  1661. /* For a single instance:
  1662. * - force == true: The instance will be removed even when its remaining
  1663. * lifetime is not zero.
  1664. * - force == false: the instance will be deactivated but kept stored unless
  1665. * the remaining lifetime is zero.
  1666. *
  1667. * For instance == 0x00:
  1668. * - force == true: All instances will be removed regardless of their timeout
  1669. * setting.
  1670. * - force == false: Only instances that have a timeout will be removed.
  1671. */
  1672. int hci_remove_advertising_sync(struct hci_dev *hdev, struct sock *sk,
  1673. u8 instance, bool force)
  1674. {
  1675. struct adv_info *next = NULL;
  1676. int err;
  1677. /* Cancel any timeout concerning the removed instance(s). */
  1678. if (!instance || hdev->cur_adv_instance == instance)
  1679. cancel_adv_timeout(hdev);
  1680. /* Get the next instance to advertise BEFORE we remove
  1681. * the current one. This can be the same instance again
  1682. * if there is only one instance.
  1683. */
  1684. if (hdev->cur_adv_instance == instance)
  1685. next = hci_get_next_instance(hdev, instance);
  1686. if (!instance) {
  1687. err = hci_clear_adv_sync(hdev, sk, force);
  1688. if (err)
  1689. return err;
  1690. } else {
  1691. struct adv_info *adv = hci_find_adv_instance(hdev, instance);
  1692. if (force || (adv && adv->timeout && !adv->remaining_time)) {
  1693. /* Don't advertise a removed instance. */
  1694. if (next && next->instance == instance)
  1695. next = NULL;
  1696. err = hci_remove_adv_sync(hdev, instance, sk);
  1697. if (err)
  1698. return err;
  1699. }
  1700. }
  1701. if (!hdev_is_powered(hdev) || hci_dev_test_flag(hdev, HCI_ADVERTISING))
  1702. return 0;
  1703. if (next && !ext_adv_capable(hdev))
  1704. hci_schedule_adv_instance_sync(hdev, next->instance, false);
  1705. return 0;
  1706. }
  1707. int hci_read_rssi_sync(struct hci_dev *hdev, __le16 handle)
  1708. {
  1709. struct hci_cp_read_rssi cp;
  1710. cp.handle = handle;
  1711. return __hci_cmd_sync_status(hdev, HCI_OP_READ_RSSI,
  1712. sizeof(cp), &cp, HCI_CMD_TIMEOUT);
  1713. }
  1714. int hci_read_clock_sync(struct hci_dev *hdev, struct hci_cp_read_clock *cp)
  1715. {
  1716. return __hci_cmd_sync_status(hdev, HCI_OP_READ_CLOCK,
  1717. sizeof(*cp), cp, HCI_CMD_TIMEOUT);
  1718. }
  1719. int hci_read_tx_power_sync(struct hci_dev *hdev, __le16 handle, u8 type)
  1720. {
  1721. struct hci_cp_read_tx_power cp;
  1722. cp.handle = handle;
  1723. cp.type = type;
  1724. return __hci_cmd_sync_status(hdev, HCI_OP_READ_TX_POWER,
  1725. sizeof(cp), &cp, HCI_CMD_TIMEOUT);
  1726. }
  1727. int hci_disable_advertising_sync(struct hci_dev *hdev)
  1728. {
  1729. u8 enable = 0x00;
  1730. /* If controller is not advertising we are done. */
  1731. if (!hci_dev_test_flag(hdev, HCI_LE_ADV))
  1732. return 0;
  1733. if (ext_adv_capable(hdev))
  1734. return hci_disable_ext_adv_instance_sync(hdev, 0x00);
  1735. return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
  1736. sizeof(enable), &enable, HCI_CMD_TIMEOUT);
  1737. }
  1738. static int hci_le_set_ext_scan_enable_sync(struct hci_dev *hdev, u8 val,
  1739. u8 filter_dup)
  1740. {
  1741. struct hci_cp_le_set_ext_scan_enable cp;
  1742. memset(&cp, 0, sizeof(cp));
  1743. cp.enable = val;
  1744. if (hci_dev_test_flag(hdev, HCI_MESH))
  1745. cp.filter_dup = LE_SCAN_FILTER_DUP_DISABLE;
  1746. else
  1747. cp.filter_dup = filter_dup;
  1748. return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_ENABLE,
  1749. sizeof(cp), &cp, HCI_CMD_TIMEOUT);
  1750. }
  1751. static int hci_le_set_scan_enable_sync(struct hci_dev *hdev, u8 val,
  1752. u8 filter_dup)
  1753. {
  1754. struct hci_cp_le_set_scan_enable cp;
  1755. if (use_ext_scan(hdev))
  1756. return hci_le_set_ext_scan_enable_sync(hdev, val, filter_dup);
  1757. memset(&cp, 0, sizeof(cp));
  1758. cp.enable = val;
  1759. if (val && hci_dev_test_flag(hdev, HCI_MESH))
  1760. cp.filter_dup = LE_SCAN_FILTER_DUP_DISABLE;
  1761. else
  1762. cp.filter_dup = filter_dup;
  1763. return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_ENABLE,
  1764. sizeof(cp), &cp, HCI_CMD_TIMEOUT);
  1765. }
  1766. static int hci_le_set_addr_resolution_enable_sync(struct hci_dev *hdev, u8 val)
  1767. {
  1768. if (!ll_privacy_capable(hdev))
  1769. return 0;
  1770. /* If controller is not/already resolving we are done. */
  1771. if (val == hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION))
  1772. return 0;
  1773. return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADDR_RESOLV_ENABLE,
  1774. sizeof(val), &val, HCI_CMD_TIMEOUT);
  1775. }
  1776. static int hci_scan_disable_sync(struct hci_dev *hdev)
  1777. {
  1778. int err;
  1779. /* If controller is not scanning we are done. */
  1780. if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
  1781. return 0;
  1782. if (hdev->scanning_paused) {
  1783. bt_dev_dbg(hdev, "Scanning is paused for suspend");
  1784. return 0;
  1785. }
  1786. err = hci_le_set_scan_enable_sync(hdev, LE_SCAN_DISABLE, 0x00);
  1787. if (err) {
  1788. bt_dev_err(hdev, "Unable to disable scanning: %d", err);
  1789. return err;
  1790. }
  1791. return err;
  1792. }
  1793. static bool scan_use_rpa(struct hci_dev *hdev)
  1794. {
  1795. return hci_dev_test_flag(hdev, HCI_PRIVACY);
  1796. }
  1797. static void hci_start_interleave_scan(struct hci_dev *hdev)
  1798. {
  1799. hdev->interleave_scan_state = INTERLEAVE_SCAN_NO_FILTER;
  1800. queue_delayed_work(hdev->req_workqueue,
  1801. &hdev->interleave_scan, 0);
  1802. }
  1803. static void cancel_interleave_scan(struct hci_dev *hdev)
  1804. {
  1805. bt_dev_dbg(hdev, "cancelling interleave scan");
  1806. cancel_delayed_work_sync(&hdev->interleave_scan);
  1807. hdev->interleave_scan_state = INTERLEAVE_SCAN_NONE;
  1808. }
  1809. /* Return true if interleave_scan wasn't started until exiting this function,
  1810. * otherwise, return false
  1811. */
  1812. static bool hci_update_interleaved_scan_sync(struct hci_dev *hdev)
  1813. {
  1814. /* Do interleaved scan only if all of the following are true:
  1815. * - There is at least one ADV monitor
  1816. * - At least one pending LE connection or one device to be scanned for
  1817. * - Monitor offloading is not supported
  1818. * If so, we should alternate between allowlist scan and one without
  1819. * any filters to save power.
  1820. */
  1821. bool use_interleaving = hci_is_adv_monitoring(hdev) &&
  1822. !(list_empty(&hdev->pend_le_conns) &&
  1823. list_empty(&hdev->pend_le_reports)) &&
  1824. hci_get_adv_monitor_offload_ext(hdev) ==
  1825. HCI_ADV_MONITOR_EXT_NONE;
  1826. bool is_interleaving = is_interleave_scanning(hdev);
  1827. if (use_interleaving && !is_interleaving) {
  1828. hci_start_interleave_scan(hdev);
  1829. bt_dev_dbg(hdev, "starting interleave scan");
  1830. return true;
  1831. }
  1832. if (!use_interleaving && is_interleaving)
  1833. cancel_interleave_scan(hdev);
  1834. return false;
  1835. }
  1836. /* Removes connection to resolve list if needed.*/
  1837. static int hci_le_del_resolve_list_sync(struct hci_dev *hdev,
  1838. bdaddr_t *bdaddr, u8 bdaddr_type)
  1839. {
  1840. struct hci_cp_le_del_from_resolv_list cp;
  1841. struct bdaddr_list_with_irk *entry;
  1842. if (!ll_privacy_capable(hdev))
  1843. return 0;
  1844. /* Check if the IRK has been programmed */
  1845. entry = hci_bdaddr_list_lookup_with_irk(&hdev->le_resolv_list, bdaddr,
  1846. bdaddr_type);
  1847. if (!entry)
  1848. return 0;
  1849. cp.bdaddr_type = bdaddr_type;
  1850. bacpy(&cp.bdaddr, bdaddr);
  1851. return __hci_cmd_sync_status(hdev, HCI_OP_LE_DEL_FROM_RESOLV_LIST,
  1852. sizeof(cp), &cp, HCI_CMD_TIMEOUT);
  1853. }
  1854. static int hci_le_del_accept_list_sync(struct hci_dev *hdev,
  1855. bdaddr_t *bdaddr, u8 bdaddr_type)
  1856. {
  1857. struct hci_cp_le_del_from_accept_list cp;
  1858. int err;
  1859. /* Check if device is on accept list before removing it */
  1860. if (!hci_bdaddr_list_lookup(&hdev->le_accept_list, bdaddr, bdaddr_type))
  1861. return 0;
  1862. cp.bdaddr_type = bdaddr_type;
  1863. bacpy(&cp.bdaddr, bdaddr);
  1864. /* Ignore errors when removing from resolving list as that is likely
  1865. * that the device was never added.
  1866. */
  1867. hci_le_del_resolve_list_sync(hdev, &cp.bdaddr, cp.bdaddr_type);
  1868. err = __hci_cmd_sync_status(hdev, HCI_OP_LE_DEL_FROM_ACCEPT_LIST,
  1869. sizeof(cp), &cp, HCI_CMD_TIMEOUT);
  1870. if (err) {
  1871. bt_dev_err(hdev, "Unable to remove from allow list: %d", err);
  1872. return err;
  1873. }
  1874. bt_dev_dbg(hdev, "Remove %pMR (0x%x) from allow list", &cp.bdaddr,
  1875. cp.bdaddr_type);
  1876. return 0;
  1877. }
  1878. struct conn_params {
  1879. bdaddr_t addr;
  1880. u8 addr_type;
  1881. hci_conn_flags_t flags;
  1882. u8 privacy_mode;
  1883. };
  1884. /* Adds connection to resolve list if needed.
  1885. * Setting params to NULL programs local hdev->irk
  1886. */
  1887. static int hci_le_add_resolve_list_sync(struct hci_dev *hdev,
  1888. struct conn_params *params)
  1889. {
  1890. struct hci_cp_le_add_to_resolv_list cp;
  1891. struct smp_irk *irk;
  1892. struct bdaddr_list_with_irk *entry;
  1893. struct hci_conn_params *p;
  1894. if (!ll_privacy_capable(hdev))
  1895. return 0;
  1896. /* Attempt to program local identity address, type and irk if params is
  1897. * NULL.
  1898. */
  1899. if (!params) {
  1900. if (!hci_dev_test_flag(hdev, HCI_PRIVACY))
  1901. return 0;
  1902. hci_copy_identity_address(hdev, &cp.bdaddr, &cp.bdaddr_type);
  1903. memcpy(cp.peer_irk, hdev->irk, 16);
  1904. goto done;
  1905. } else if (!(params->flags & HCI_CONN_FLAG_ADDRESS_RESOLUTION))
  1906. return 0;
  1907. irk = hci_find_irk_by_addr(hdev, &params->addr, params->addr_type);
  1908. if (!irk)
  1909. return 0;
  1910. /* Check if the IK has _not_ been programmed yet. */
  1911. entry = hci_bdaddr_list_lookup_with_irk(&hdev->le_resolv_list,
  1912. &params->addr,
  1913. params->addr_type);
  1914. if (entry)
  1915. return 0;
  1916. cp.bdaddr_type = params->addr_type;
  1917. bacpy(&cp.bdaddr, &params->addr);
  1918. memcpy(cp.peer_irk, irk->val, 16);
  1919. /* Default privacy mode is always Network */
  1920. params->privacy_mode = HCI_NETWORK_PRIVACY;
  1921. rcu_read_lock();
  1922. p = hci_pend_le_action_lookup(&hdev->pend_le_conns,
  1923. &params->addr, params->addr_type);
  1924. if (!p)
  1925. p = hci_pend_le_action_lookup(&hdev->pend_le_reports,
  1926. &params->addr, params->addr_type);
  1927. if (p)
  1928. WRITE_ONCE(p->privacy_mode, HCI_NETWORK_PRIVACY);
  1929. rcu_read_unlock();
  1930. done:
  1931. if (hci_dev_test_flag(hdev, HCI_PRIVACY))
  1932. memcpy(cp.local_irk, hdev->irk, 16);
  1933. else
  1934. memset(cp.local_irk, 0, 16);
  1935. return __hci_cmd_sync_status(hdev, HCI_OP_LE_ADD_TO_RESOLV_LIST,
  1936. sizeof(cp), &cp, HCI_CMD_TIMEOUT);
  1937. }
  1938. /* Set Device Privacy Mode. */
  1939. static int hci_le_set_privacy_mode_sync(struct hci_dev *hdev,
  1940. struct conn_params *params)
  1941. {
  1942. struct hci_cp_le_set_privacy_mode cp;
  1943. struct smp_irk *irk;
  1944. if (!ll_privacy_capable(hdev) ||
  1945. !(params->flags & HCI_CONN_FLAG_ADDRESS_RESOLUTION))
  1946. return 0;
  1947. /* If device privacy mode has already been set there is nothing to do */
  1948. if (params->privacy_mode == HCI_DEVICE_PRIVACY)
  1949. return 0;
  1950. /* Check if HCI_CONN_FLAG_DEVICE_PRIVACY has been set as it also
  1951. * indicates that LL Privacy has been enabled and
  1952. * HCI_OP_LE_SET_PRIVACY_MODE is supported.
  1953. */
  1954. if (!(params->flags & HCI_CONN_FLAG_DEVICE_PRIVACY))
  1955. return 0;
  1956. irk = hci_find_irk_by_addr(hdev, &params->addr, params->addr_type);
  1957. if (!irk)
  1958. return 0;
  1959. memset(&cp, 0, sizeof(cp));
  1960. cp.bdaddr_type = irk->addr_type;
  1961. bacpy(&cp.bdaddr, &irk->bdaddr);
  1962. cp.mode = HCI_DEVICE_PRIVACY;
  1963. /* Note: params->privacy_mode is not updated since it is a copy */
  1964. return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PRIVACY_MODE,
  1965. sizeof(cp), &cp, HCI_CMD_TIMEOUT);
  1966. }
  1967. /* Adds connection to allow list if needed, if the device uses RPA (has IRK)
  1968. * this attempts to program the device in the resolving list as well and
  1969. * properly set the privacy mode.
  1970. */
  1971. static int hci_le_add_accept_list_sync(struct hci_dev *hdev,
  1972. struct conn_params *params,
  1973. u8 *num_entries)
  1974. {
  1975. struct hci_cp_le_add_to_accept_list cp;
  1976. int err;
  1977. /* During suspend, only wakeable devices can be in acceptlist */
  1978. if (hdev->suspended &&
  1979. !(params->flags & HCI_CONN_FLAG_REMOTE_WAKEUP)) {
  1980. hci_le_del_accept_list_sync(hdev, &params->addr,
  1981. params->addr_type);
  1982. return 0;
  1983. }
  1984. /* Select filter policy to accept all advertising */
  1985. if (*num_entries >= hdev->le_accept_list_size)
  1986. return -ENOSPC;
  1987. /* Attempt to program the device in the resolving list first to avoid
  1988. * having to rollback in case it fails since the resolving list is
  1989. * dynamic it can probably be smaller than the accept list.
  1990. */
  1991. err = hci_le_add_resolve_list_sync(hdev, params);
  1992. if (err) {
  1993. bt_dev_err(hdev, "Unable to add to resolve list: %d", err);
  1994. return err;
  1995. }
  1996. /* Set Privacy Mode */
  1997. err = hci_le_set_privacy_mode_sync(hdev, params);
  1998. if (err) {
  1999. bt_dev_err(hdev, "Unable to set privacy mode: %d", err);
  2000. return err;
  2001. }
  2002. /* Check if already in accept list */
  2003. if (hci_bdaddr_list_lookup(&hdev->le_accept_list, &params->addr,
  2004. params->addr_type))
  2005. return 0;
  2006. *num_entries += 1;
  2007. cp.bdaddr_type = params->addr_type;
  2008. bacpy(&cp.bdaddr, &params->addr);
  2009. err = __hci_cmd_sync_status(hdev, HCI_OP_LE_ADD_TO_ACCEPT_LIST,
  2010. sizeof(cp), &cp, HCI_CMD_TIMEOUT);
  2011. if (err) {
  2012. bt_dev_err(hdev, "Unable to add to allow list: %d", err);
  2013. /* Rollback the device from the resolving list */
  2014. hci_le_del_resolve_list_sync(hdev, &cp.bdaddr, cp.bdaddr_type);
  2015. return err;
  2016. }
  2017. bt_dev_dbg(hdev, "Add %pMR (0x%x) to allow list", &cp.bdaddr,
  2018. cp.bdaddr_type);
  2019. return 0;
  2020. }
  2021. /* This function disables/pause all advertising instances */
  2022. static int hci_pause_advertising_sync(struct hci_dev *hdev)
  2023. {
  2024. int err;
  2025. int old_state;
  2026. /* If controller is not advertising we are done. */
  2027. if (!hci_dev_test_flag(hdev, HCI_LE_ADV))
  2028. return 0;
  2029. /* If already been paused there is nothing to do. */
  2030. if (hdev->advertising_paused)
  2031. return 0;
  2032. bt_dev_dbg(hdev, "Pausing directed advertising");
  2033. /* Stop directed advertising */
  2034. old_state = hci_dev_test_flag(hdev, HCI_ADVERTISING);
  2035. if (old_state) {
  2036. /* When discoverable timeout triggers, then just make sure
  2037. * the limited discoverable flag is cleared. Even in the case
  2038. * of a timeout triggered from general discoverable, it is
  2039. * safe to unconditionally clear the flag.
  2040. */
  2041. hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
  2042. hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
  2043. hdev->discov_timeout = 0;
  2044. }
  2045. bt_dev_dbg(hdev, "Pausing advertising instances");
  2046. /* Call to disable any advertisements active on the controller.
  2047. * This will succeed even if no advertisements are configured.
  2048. */
  2049. err = hci_disable_advertising_sync(hdev);
  2050. if (err)
  2051. return err;
  2052. /* If we are using software rotation, pause the loop */
  2053. if (!ext_adv_capable(hdev))
  2054. cancel_adv_timeout(hdev);
  2055. hdev->advertising_paused = true;
  2056. hdev->advertising_old_state = old_state;
  2057. return 0;
  2058. }
  2059. /* This function enables all user advertising instances */
  2060. static int hci_resume_advertising_sync(struct hci_dev *hdev)
  2061. {
  2062. struct adv_info *adv, *tmp;
  2063. int err;
  2064. /* If advertising has not been paused there is nothing to do. */
  2065. if (!hdev->advertising_paused)
  2066. return 0;
  2067. /* Resume directed advertising */
  2068. hdev->advertising_paused = false;
  2069. if (hdev->advertising_old_state) {
  2070. hci_dev_set_flag(hdev, HCI_ADVERTISING);
  2071. hdev->advertising_old_state = 0;
  2072. }
  2073. bt_dev_dbg(hdev, "Resuming advertising instances");
  2074. if (ext_adv_capable(hdev)) {
  2075. /* Call for each tracked instance to be re-enabled */
  2076. list_for_each_entry_safe(adv, tmp, &hdev->adv_instances, list) {
  2077. err = hci_enable_ext_advertising_sync(hdev,
  2078. adv->instance);
  2079. if (!err)
  2080. continue;
  2081. /* If the instance cannot be resumed remove it */
  2082. hci_remove_ext_adv_instance_sync(hdev, adv->instance,
  2083. NULL);
  2084. }
  2085. /* If current advertising instance is set to instance 0x00
  2086. * then we need to re-enable it.
  2087. */
  2088. if (hci_dev_test_and_clear_flag(hdev, HCI_LE_ADV_0))
  2089. err = hci_enable_ext_advertising_sync(hdev, 0x00);
  2090. } else {
  2091. /* Schedule for most recent instance to be restarted and begin
  2092. * the software rotation loop
  2093. */
  2094. err = hci_schedule_adv_instance_sync(hdev,
  2095. hdev->cur_adv_instance,
  2096. true);
  2097. }
  2098. hdev->advertising_paused = false;
  2099. return err;
  2100. }
  2101. static int hci_pause_addr_resolution(struct hci_dev *hdev)
  2102. {
  2103. int err;
  2104. if (!ll_privacy_capable(hdev))
  2105. return 0;
  2106. if (!hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION))
  2107. return 0;
  2108. /* Cannot disable addr resolution if scanning is enabled or
  2109. * when initiating an LE connection.
  2110. */
  2111. if (hci_dev_test_flag(hdev, HCI_LE_SCAN) ||
  2112. hci_lookup_le_connect(hdev)) {
  2113. bt_dev_err(hdev, "Command not allowed when scan/LE connect");
  2114. return -EPERM;
  2115. }
  2116. /* Cannot disable addr resolution if advertising is enabled. */
  2117. err = hci_pause_advertising_sync(hdev);
  2118. if (err) {
  2119. bt_dev_err(hdev, "Pause advertising failed: %d", err);
  2120. return err;
  2121. }
  2122. err = hci_le_set_addr_resolution_enable_sync(hdev, 0x00);
  2123. if (err)
  2124. bt_dev_err(hdev, "Unable to disable Address Resolution: %d",
  2125. err);
  2126. /* Return if address resolution is disabled and RPA is not used. */
  2127. if (!err && scan_use_rpa(hdev))
  2128. return 0;
  2129. hci_resume_advertising_sync(hdev);
  2130. return err;
  2131. }
  2132. struct sk_buff *hci_read_local_oob_data_sync(struct hci_dev *hdev,
  2133. bool extended, struct sock *sk)
  2134. {
  2135. u16 opcode = extended ? HCI_OP_READ_LOCAL_OOB_EXT_DATA :
  2136. HCI_OP_READ_LOCAL_OOB_DATA;
  2137. return __hci_cmd_sync_sk(hdev, opcode, 0, NULL, 0, HCI_CMD_TIMEOUT, sk);
  2138. }
  2139. static struct conn_params *conn_params_copy(struct list_head *list, size_t *n)
  2140. {
  2141. struct hci_conn_params *params;
  2142. struct conn_params *p;
  2143. size_t i;
  2144. rcu_read_lock();
  2145. i = 0;
  2146. list_for_each_entry_rcu(params, list, action)
  2147. ++i;
  2148. *n = i;
  2149. rcu_read_unlock();
  2150. p = kvzalloc_objs(struct conn_params, *n);
  2151. if (!p)
  2152. return NULL;
  2153. rcu_read_lock();
  2154. i = 0;
  2155. list_for_each_entry_rcu(params, list, action) {
  2156. /* Racing adds are handled in next scan update */
  2157. if (i >= *n)
  2158. break;
  2159. /* No hdev->lock, but: addr, addr_type are immutable.
  2160. * privacy_mode is only written by us or in
  2161. * hci_cc_le_set_privacy_mode that we wait for.
  2162. * We should be idempotent so MGMT updating flags
  2163. * while we are processing is OK.
  2164. */
  2165. bacpy(&p[i].addr, &params->addr);
  2166. p[i].addr_type = params->addr_type;
  2167. p[i].flags = READ_ONCE(params->flags);
  2168. p[i].privacy_mode = READ_ONCE(params->privacy_mode);
  2169. ++i;
  2170. }
  2171. rcu_read_unlock();
  2172. *n = i;
  2173. return p;
  2174. }
  2175. /* Clear LE Accept List */
  2176. static int hci_le_clear_accept_list_sync(struct hci_dev *hdev)
  2177. {
  2178. if (!(hdev->commands[26] & 0x80))
  2179. return 0;
  2180. return __hci_cmd_sync_status(hdev, HCI_OP_LE_CLEAR_ACCEPT_LIST, 0, NULL,
  2181. HCI_CMD_TIMEOUT);
  2182. }
  2183. /* Device must not be scanning when updating the accept list.
  2184. *
  2185. * Update is done using the following sequence:
  2186. *
  2187. * ll_privacy_capable((Disable Advertising) -> Disable Resolving List) ->
  2188. * Remove Devices From Accept List ->
  2189. * (has IRK && ll_privacy_capable(Remove Devices From Resolving List))->
  2190. * Add Devices to Accept List ->
  2191. * (has IRK && ll_privacy_capable(Remove Devices From Resolving List)) ->
  2192. * ll_privacy_capable(Enable Resolving List -> (Enable Advertising)) ->
  2193. * Enable Scanning
  2194. *
  2195. * In case of failure advertising shall be restored to its original state and
  2196. * return would disable accept list since either accept or resolving list could
  2197. * not be programmed.
  2198. *
  2199. */
  2200. static u8 hci_update_accept_list_sync(struct hci_dev *hdev)
  2201. {
  2202. struct conn_params *params;
  2203. struct bdaddr_list *b, *t;
  2204. u8 num_entries = 0;
  2205. bool pend_conn, pend_report;
  2206. u8 filter_policy;
  2207. size_t i, n;
  2208. int err;
  2209. /* Pause advertising if resolving list can be used as controllers
  2210. * cannot accept resolving list modifications while advertising.
  2211. */
  2212. if (ll_privacy_capable(hdev)) {
  2213. err = hci_pause_advertising_sync(hdev);
  2214. if (err) {
  2215. bt_dev_err(hdev, "pause advertising failed: %d", err);
  2216. return 0x00;
  2217. }
  2218. }
  2219. /* Disable address resolution while reprogramming accept list since
  2220. * devices that do have an IRK will be programmed in the resolving list
  2221. * when LL Privacy is enabled.
  2222. */
  2223. err = hci_le_set_addr_resolution_enable_sync(hdev, 0x00);
  2224. if (err) {
  2225. bt_dev_err(hdev, "Unable to disable LL privacy: %d", err);
  2226. goto done;
  2227. }
  2228. /* Force address filtering if PA Sync is in progress */
  2229. if (hci_dev_test_flag(hdev, HCI_PA_SYNC)) {
  2230. struct hci_conn *conn;
  2231. conn = hci_conn_hash_lookup_create_pa_sync(hdev);
  2232. if (conn) {
  2233. struct conn_params pa;
  2234. memset(&pa, 0, sizeof(pa));
  2235. bacpy(&pa.addr, &conn->dst);
  2236. pa.addr_type = conn->dst_type;
  2237. /* Clear first since there could be addresses left
  2238. * behind.
  2239. */
  2240. hci_le_clear_accept_list_sync(hdev);
  2241. num_entries = 1;
  2242. err = hci_le_add_accept_list_sync(hdev, &pa,
  2243. &num_entries);
  2244. goto done;
  2245. }
  2246. }
  2247. /* Go through the current accept list programmed into the
  2248. * controller one by one and check if that address is connected or is
  2249. * still in the list of pending connections or list of devices to
  2250. * report. If not present in either list, then remove it from
  2251. * the controller.
  2252. */
  2253. list_for_each_entry_safe(b, t, &hdev->le_accept_list, list) {
  2254. if (hci_conn_hash_lookup_le(hdev, &b->bdaddr, b->bdaddr_type))
  2255. continue;
  2256. /* Pointers not dereferenced, no locks needed */
  2257. pend_conn = hci_pend_le_action_lookup(&hdev->pend_le_conns,
  2258. &b->bdaddr,
  2259. b->bdaddr_type);
  2260. pend_report = hci_pend_le_action_lookup(&hdev->pend_le_reports,
  2261. &b->bdaddr,
  2262. b->bdaddr_type);
  2263. /* If the device is not likely to connect or report,
  2264. * remove it from the acceptlist.
  2265. */
  2266. if (!pend_conn && !pend_report) {
  2267. hci_le_del_accept_list_sync(hdev, &b->bdaddr,
  2268. b->bdaddr_type);
  2269. continue;
  2270. }
  2271. num_entries++;
  2272. }
  2273. /* Since all no longer valid accept list entries have been
  2274. * removed, walk through the list of pending connections
  2275. * and ensure that any new device gets programmed into
  2276. * the controller.
  2277. *
  2278. * If the list of the devices is larger than the list of
  2279. * available accept list entries in the controller, then
  2280. * just abort and return filer policy value to not use the
  2281. * accept list.
  2282. *
  2283. * The list and params may be mutated while we wait for events,
  2284. * so make a copy and iterate it.
  2285. */
  2286. params = conn_params_copy(&hdev->pend_le_conns, &n);
  2287. if (!params) {
  2288. err = -ENOMEM;
  2289. goto done;
  2290. }
  2291. for (i = 0; i < n; ++i) {
  2292. err = hci_le_add_accept_list_sync(hdev, &params[i],
  2293. &num_entries);
  2294. if (err) {
  2295. kvfree(params);
  2296. goto done;
  2297. }
  2298. }
  2299. kvfree(params);
  2300. /* After adding all new pending connections, walk through
  2301. * the list of pending reports and also add these to the
  2302. * accept list if there is still space. Abort if space runs out.
  2303. */
  2304. params = conn_params_copy(&hdev->pend_le_reports, &n);
  2305. if (!params) {
  2306. err = -ENOMEM;
  2307. goto done;
  2308. }
  2309. for (i = 0; i < n; ++i) {
  2310. err = hci_le_add_accept_list_sync(hdev, &params[i],
  2311. &num_entries);
  2312. if (err) {
  2313. kvfree(params);
  2314. goto done;
  2315. }
  2316. }
  2317. kvfree(params);
  2318. /* Use the allowlist unless the following conditions are all true:
  2319. * - We are not currently suspending
  2320. * - There are 1 or more ADV monitors registered and it's not offloaded
  2321. * - Interleaved scanning is not currently using the allowlist
  2322. */
  2323. if (!idr_is_empty(&hdev->adv_monitors_idr) && !hdev->suspended &&
  2324. hci_get_adv_monitor_offload_ext(hdev) == HCI_ADV_MONITOR_EXT_NONE &&
  2325. hdev->interleave_scan_state != INTERLEAVE_SCAN_ALLOWLIST)
  2326. err = -EINVAL;
  2327. done:
  2328. filter_policy = err ? 0x00 : 0x01;
  2329. /* Enable address resolution when LL Privacy is enabled. */
  2330. err = hci_le_set_addr_resolution_enable_sync(hdev, 0x01);
  2331. if (err)
  2332. bt_dev_err(hdev, "Unable to enable LL privacy: %d", err);
  2333. /* Resume advertising if it was paused */
  2334. if (ll_privacy_capable(hdev))
  2335. hci_resume_advertising_sync(hdev);
  2336. /* Select filter policy to use accept list */
  2337. return filter_policy;
  2338. }
  2339. static void hci_le_scan_phy_params(struct hci_cp_le_scan_phy_params *cp,
  2340. u8 type, u16 interval, u16 window)
  2341. {
  2342. cp->type = type;
  2343. cp->interval = cpu_to_le16(interval);
  2344. cp->window = cpu_to_le16(window);
  2345. }
  2346. static int hci_le_set_ext_scan_param_sync(struct hci_dev *hdev, u8 type,
  2347. u16 interval, u16 window,
  2348. u8 own_addr_type, u8 filter_policy)
  2349. {
  2350. struct hci_cp_le_set_ext_scan_params *cp;
  2351. struct hci_cp_le_scan_phy_params *phy;
  2352. u8 data[sizeof(*cp) + sizeof(*phy) * 2];
  2353. u8 num_phy = 0x00;
  2354. cp = (void *)data;
  2355. phy = (void *)cp->data;
  2356. memset(data, 0, sizeof(data));
  2357. cp->own_addr_type = own_addr_type;
  2358. cp->filter_policy = filter_policy;
  2359. /* Check if PA Sync is in progress then select the PHY based on the
  2360. * hci_conn.iso_qos.
  2361. */
  2362. if (hci_dev_test_flag(hdev, HCI_PA_SYNC)) {
  2363. struct hci_cp_le_add_to_accept_list *sent;
  2364. sent = hci_sent_cmd_data(hdev, HCI_OP_LE_ADD_TO_ACCEPT_LIST);
  2365. if (sent) {
  2366. struct hci_conn *conn;
  2367. conn = hci_conn_hash_lookup_ba(hdev, PA_LINK,
  2368. &sent->bdaddr);
  2369. if (conn) {
  2370. struct bt_iso_qos *qos = &conn->iso_qos;
  2371. if (qos->bcast.in.phys & BT_ISO_PHY_1M ||
  2372. qos->bcast.in.phys & BT_ISO_PHY_2M) {
  2373. cp->scanning_phys |= LE_SCAN_PHY_1M;
  2374. hci_le_scan_phy_params(phy, type,
  2375. interval,
  2376. window);
  2377. num_phy++;
  2378. phy++;
  2379. }
  2380. if (qos->bcast.in.phys & BT_ISO_PHY_CODED) {
  2381. cp->scanning_phys |= LE_SCAN_PHY_CODED;
  2382. hci_le_scan_phy_params(phy, type,
  2383. interval * 3,
  2384. window * 3);
  2385. num_phy++;
  2386. phy++;
  2387. }
  2388. if (num_phy)
  2389. goto done;
  2390. }
  2391. }
  2392. }
  2393. if (scan_1m(hdev) || scan_2m(hdev)) {
  2394. cp->scanning_phys |= LE_SCAN_PHY_1M;
  2395. hci_le_scan_phy_params(phy, type, interval, window);
  2396. num_phy++;
  2397. phy++;
  2398. }
  2399. if (scan_coded(hdev)) {
  2400. cp->scanning_phys |= LE_SCAN_PHY_CODED;
  2401. hci_le_scan_phy_params(phy, type, interval * 3, window * 3);
  2402. num_phy++;
  2403. phy++;
  2404. }
  2405. done:
  2406. if (!num_phy)
  2407. return -EINVAL;
  2408. return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_PARAMS,
  2409. sizeof(*cp) + sizeof(*phy) * num_phy,
  2410. data, HCI_CMD_TIMEOUT);
  2411. }
  2412. static int hci_le_set_scan_param_sync(struct hci_dev *hdev, u8 type,
  2413. u16 interval, u16 window,
  2414. u8 own_addr_type, u8 filter_policy)
  2415. {
  2416. struct hci_cp_le_set_scan_param cp;
  2417. if (use_ext_scan(hdev))
  2418. return hci_le_set_ext_scan_param_sync(hdev, type, interval,
  2419. window, own_addr_type,
  2420. filter_policy);
  2421. memset(&cp, 0, sizeof(cp));
  2422. cp.type = type;
  2423. cp.interval = cpu_to_le16(interval);
  2424. cp.window = cpu_to_le16(window);
  2425. cp.own_address_type = own_addr_type;
  2426. cp.filter_policy = filter_policy;
  2427. return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_PARAM,
  2428. sizeof(cp), &cp, HCI_CMD_TIMEOUT);
  2429. }
  2430. static int hci_start_scan_sync(struct hci_dev *hdev, u8 type, u16 interval,
  2431. u16 window, u8 own_addr_type, u8 filter_policy,
  2432. u8 filter_dup)
  2433. {
  2434. int err;
  2435. if (hdev->scanning_paused) {
  2436. bt_dev_dbg(hdev, "Scanning is paused for suspend");
  2437. return 0;
  2438. }
  2439. err = hci_le_set_scan_param_sync(hdev, type, interval, window,
  2440. own_addr_type, filter_policy);
  2441. if (err)
  2442. return err;
  2443. return hci_le_set_scan_enable_sync(hdev, LE_SCAN_ENABLE, filter_dup);
  2444. }
  2445. static int hci_passive_scan_sync(struct hci_dev *hdev)
  2446. {
  2447. u8 own_addr_type;
  2448. u8 filter_policy;
  2449. u16 window, interval;
  2450. u8 filter_dups = LE_SCAN_FILTER_DUP_ENABLE;
  2451. int err;
  2452. if (hdev->scanning_paused) {
  2453. bt_dev_dbg(hdev, "Scanning is paused for suspend");
  2454. return 0;
  2455. }
  2456. err = hci_scan_disable_sync(hdev);
  2457. if (err) {
  2458. bt_dev_err(hdev, "disable scanning failed: %d", err);
  2459. return err;
  2460. }
  2461. /* Set require_privacy to false since no SCAN_REQ are send
  2462. * during passive scanning. Not using an non-resolvable address
  2463. * here is important so that peer devices using direct
  2464. * advertising with our address will be correctly reported
  2465. * by the controller.
  2466. */
  2467. if (hci_update_random_address_sync(hdev, false, scan_use_rpa(hdev),
  2468. &own_addr_type))
  2469. return 0;
  2470. if (hdev->enable_advmon_interleave_scan &&
  2471. hci_update_interleaved_scan_sync(hdev))
  2472. return 0;
  2473. bt_dev_dbg(hdev, "interleave state %d", hdev->interleave_scan_state);
  2474. /* Adding or removing entries from the accept list must
  2475. * happen before enabling scanning. The controller does
  2476. * not allow accept list modification while scanning.
  2477. */
  2478. filter_policy = hci_update_accept_list_sync(hdev);
  2479. /* If suspended and filter_policy set to 0x00 (no acceptlist) then
  2480. * passive scanning cannot be started since that would require the host
  2481. * to be woken up to process the reports.
  2482. */
  2483. if (hdev->suspended && !filter_policy) {
  2484. /* Check if accept list is empty then there is no need to scan
  2485. * while suspended.
  2486. */
  2487. if (list_empty(&hdev->le_accept_list))
  2488. return 0;
  2489. /* If there are devices is the accept_list that means some
  2490. * devices could not be programmed which in non-suspended case
  2491. * means filter_policy needs to be set to 0x00 so the host needs
  2492. * to filter, but since this is treating suspended case we
  2493. * can ignore device needing host to filter to allow devices in
  2494. * the acceptlist to be able to wakeup the system.
  2495. */
  2496. filter_policy = 0x01;
  2497. }
  2498. /* When the controller is using random resolvable addresses and
  2499. * with that having LE privacy enabled, then controllers with
  2500. * Extended Scanner Filter Policies support can now enable support
  2501. * for handling directed advertising.
  2502. *
  2503. * So instead of using filter polices 0x00 (no acceptlist)
  2504. * and 0x01 (acceptlist enabled) use the new filter policies
  2505. * 0x02 (no acceptlist) and 0x03 (acceptlist enabled).
  2506. */
  2507. if (hci_dev_test_flag(hdev, HCI_PRIVACY) &&
  2508. (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY))
  2509. filter_policy |= 0x02;
  2510. if (hdev->suspended) {
  2511. window = hdev->le_scan_window_suspend;
  2512. interval = hdev->le_scan_int_suspend;
  2513. } else if (hci_is_le_conn_scanning(hdev)) {
  2514. window = hdev->le_scan_window_connect;
  2515. interval = hdev->le_scan_int_connect;
  2516. } else if (hci_is_adv_monitoring(hdev)) {
  2517. window = hdev->le_scan_window_adv_monitor;
  2518. interval = hdev->le_scan_int_adv_monitor;
  2519. /* Disable duplicates filter when scanning for advertisement
  2520. * monitor for the following reasons.
  2521. *
  2522. * For HW pattern filtering (ex. MSFT), Realtek and Qualcomm
  2523. * controllers ignore RSSI_Sampling_Period when the duplicates
  2524. * filter is enabled.
  2525. *
  2526. * For SW pattern filtering, when we're not doing interleaved
  2527. * scanning, it is necessary to disable duplicates filter,
  2528. * otherwise hosts can only receive one advertisement and it's
  2529. * impossible to know if a peer is still in range.
  2530. */
  2531. filter_dups = LE_SCAN_FILTER_DUP_DISABLE;
  2532. } else {
  2533. window = hdev->le_scan_window;
  2534. interval = hdev->le_scan_interval;
  2535. }
  2536. /* Disable all filtering for Mesh */
  2537. if (hci_dev_test_flag(hdev, HCI_MESH)) {
  2538. filter_policy = 0;
  2539. filter_dups = LE_SCAN_FILTER_DUP_DISABLE;
  2540. }
  2541. bt_dev_dbg(hdev, "LE passive scan with acceptlist = %d", filter_policy);
  2542. return hci_start_scan_sync(hdev, LE_SCAN_PASSIVE, interval, window,
  2543. own_addr_type, filter_policy, filter_dups);
  2544. }
  2545. /* This function controls the passive scanning based on hdev->pend_le_conns
  2546. * list. If there are pending LE connection we start the background scanning,
  2547. * otherwise we stop it in the following sequence:
  2548. *
  2549. * If there are devices to scan:
  2550. *
  2551. * Disable Scanning -> Update Accept List ->
  2552. * ll_privacy_capable((Disable Advertising) -> Disable Resolving List ->
  2553. * Update Resolving List -> Enable Resolving List -> (Enable Advertising)) ->
  2554. * Enable Scanning
  2555. *
  2556. * Otherwise:
  2557. *
  2558. * Disable Scanning
  2559. */
  2560. int hci_update_passive_scan_sync(struct hci_dev *hdev)
  2561. {
  2562. int err;
  2563. if (!test_bit(HCI_UP, &hdev->flags) ||
  2564. test_bit(HCI_INIT, &hdev->flags) ||
  2565. hci_dev_test_flag(hdev, HCI_SETUP) ||
  2566. hci_dev_test_flag(hdev, HCI_CONFIG) ||
  2567. hci_dev_test_flag(hdev, HCI_AUTO_OFF) ||
  2568. hci_dev_test_flag(hdev, HCI_UNREGISTER))
  2569. return 0;
  2570. /* No point in doing scanning if LE support hasn't been enabled */
  2571. if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
  2572. return 0;
  2573. /* If discovery is active don't interfere with it */
  2574. if (hdev->discovery.state != DISCOVERY_STOPPED)
  2575. return 0;
  2576. /* Reset RSSI and UUID filters when starting background scanning
  2577. * since these filters are meant for service discovery only.
  2578. *
  2579. * The Start Discovery and Start Service Discovery operations
  2580. * ensure to set proper values for RSSI threshold and UUID
  2581. * filter list. So it is safe to just reset them here.
  2582. */
  2583. hci_discovery_filter_clear(hdev);
  2584. bt_dev_dbg(hdev, "ADV monitoring is %s",
  2585. hci_is_adv_monitoring(hdev) ? "on" : "off");
  2586. if (!hci_dev_test_flag(hdev, HCI_MESH) &&
  2587. list_empty(&hdev->pend_le_conns) &&
  2588. list_empty(&hdev->pend_le_reports) &&
  2589. !hci_is_adv_monitoring(hdev) &&
  2590. !hci_dev_test_flag(hdev, HCI_PA_SYNC)) {
  2591. /* If there is no pending LE connections or devices
  2592. * to be scanned for or no ADV monitors, we should stop the
  2593. * background scanning.
  2594. */
  2595. bt_dev_dbg(hdev, "stopping background scanning");
  2596. err = hci_scan_disable_sync(hdev);
  2597. if (err)
  2598. bt_dev_err(hdev, "stop background scanning failed: %d",
  2599. err);
  2600. } else {
  2601. /* If there is at least one pending LE connection, we should
  2602. * keep the background scan running.
  2603. */
  2604. /* If controller is connecting, we should not start scanning
  2605. * since some controllers are not able to scan and connect at
  2606. * the same time.
  2607. */
  2608. if (hci_lookup_le_connect(hdev))
  2609. return 0;
  2610. bt_dev_dbg(hdev, "start background scanning");
  2611. err = hci_passive_scan_sync(hdev);
  2612. if (err)
  2613. bt_dev_err(hdev, "start background scanning failed: %d",
  2614. err);
  2615. }
  2616. return err;
  2617. }
  2618. static int update_scan_sync(struct hci_dev *hdev, void *data)
  2619. {
  2620. return hci_update_scan_sync(hdev);
  2621. }
  2622. int hci_update_scan(struct hci_dev *hdev)
  2623. {
  2624. return hci_cmd_sync_queue(hdev, update_scan_sync, NULL, NULL);
  2625. }
  2626. static int update_passive_scan_sync(struct hci_dev *hdev, void *data)
  2627. {
  2628. return hci_update_passive_scan_sync(hdev);
  2629. }
  2630. int hci_update_passive_scan(struct hci_dev *hdev)
  2631. {
  2632. int err;
  2633. /* Only queue if it would have any effect */
  2634. if (!test_bit(HCI_UP, &hdev->flags) ||
  2635. test_bit(HCI_INIT, &hdev->flags) ||
  2636. hci_dev_test_flag(hdev, HCI_SETUP) ||
  2637. hci_dev_test_flag(hdev, HCI_CONFIG) ||
  2638. hci_dev_test_flag(hdev, HCI_AUTO_OFF) ||
  2639. hci_dev_test_flag(hdev, HCI_UNREGISTER))
  2640. return 0;
  2641. err = hci_cmd_sync_queue_once(hdev, update_passive_scan_sync, NULL,
  2642. NULL);
  2643. return (err == -EEXIST) ? 0 : err;
  2644. }
  2645. int hci_write_sc_support_sync(struct hci_dev *hdev, u8 val)
  2646. {
  2647. int err;
  2648. if (!bredr_sc_enabled(hdev) || lmp_host_sc_capable(hdev))
  2649. return 0;
  2650. err = __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SC_SUPPORT,
  2651. sizeof(val), &val, HCI_CMD_TIMEOUT);
  2652. if (!err) {
  2653. if (val) {
  2654. hdev->features[1][0] |= LMP_HOST_SC;
  2655. hci_dev_set_flag(hdev, HCI_SC_ENABLED);
  2656. } else {
  2657. hdev->features[1][0] &= ~LMP_HOST_SC;
  2658. hci_dev_clear_flag(hdev, HCI_SC_ENABLED);
  2659. }
  2660. }
  2661. return err;
  2662. }
  2663. int hci_write_ssp_mode_sync(struct hci_dev *hdev, u8 mode)
  2664. {
  2665. int err;
  2666. if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED) ||
  2667. lmp_host_ssp_capable(hdev))
  2668. return 0;
  2669. if (!mode && hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) {
  2670. __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE,
  2671. sizeof(mode), &mode, HCI_CMD_TIMEOUT);
  2672. }
  2673. err = __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_MODE,
  2674. sizeof(mode), &mode, HCI_CMD_TIMEOUT);
  2675. if (err)
  2676. return err;
  2677. return hci_write_sc_support_sync(hdev, 0x01);
  2678. }
  2679. int hci_write_le_host_supported_sync(struct hci_dev *hdev, u8 le, u8 simul)
  2680. {
  2681. struct hci_cp_write_le_host_supported cp;
  2682. if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) ||
  2683. !lmp_bredr_capable(hdev))
  2684. return 0;
  2685. /* Check first if we already have the right host state
  2686. * (host features set)
  2687. */
  2688. if (le == lmp_host_le_capable(hdev) &&
  2689. simul == lmp_host_le_br_capable(hdev))
  2690. return 0;
  2691. memset(&cp, 0, sizeof(cp));
  2692. cp.le = le;
  2693. cp.simul = simul;
  2694. return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED,
  2695. sizeof(cp), &cp, HCI_CMD_TIMEOUT);
  2696. }
  2697. static int hci_powered_update_adv_sync(struct hci_dev *hdev)
  2698. {
  2699. struct adv_info *adv, *tmp;
  2700. int err;
  2701. if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
  2702. return 0;
  2703. /* If RPA Resolution has not been enable yet it means the
  2704. * resolving list is empty and we should attempt to program the
  2705. * local IRK in order to support using own_addr_type
  2706. * ADDR_LE_DEV_RANDOM_RESOLVED (0x03).
  2707. */
  2708. if (!hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION)) {
  2709. hci_le_add_resolve_list_sync(hdev, NULL);
  2710. hci_le_set_addr_resolution_enable_sync(hdev, 0x01);
  2711. }
  2712. /* Make sure the controller has a good default for
  2713. * advertising data. This also applies to the case
  2714. * where BR/EDR was toggled during the AUTO_OFF phase.
  2715. */
  2716. if (hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
  2717. list_empty(&hdev->adv_instances)) {
  2718. if (ext_adv_capable(hdev)) {
  2719. err = hci_setup_ext_adv_instance_sync(hdev, 0x00);
  2720. if (!err)
  2721. hci_update_scan_rsp_data_sync(hdev, 0x00);
  2722. } else {
  2723. err = hci_update_adv_data_sync(hdev, 0x00);
  2724. if (!err)
  2725. hci_update_scan_rsp_data_sync(hdev, 0x00);
  2726. }
  2727. if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
  2728. hci_enable_advertising_sync(hdev);
  2729. }
  2730. /* Call for each tracked instance to be scheduled */
  2731. list_for_each_entry_safe(adv, tmp, &hdev->adv_instances, list)
  2732. hci_schedule_adv_instance_sync(hdev, adv->instance, true);
  2733. return 0;
  2734. }
  2735. static int hci_write_auth_enable_sync(struct hci_dev *hdev)
  2736. {
  2737. u8 link_sec;
  2738. link_sec = hci_dev_test_flag(hdev, HCI_LINK_SECURITY);
  2739. if (link_sec == test_bit(HCI_AUTH, &hdev->flags))
  2740. return 0;
  2741. return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_AUTH_ENABLE,
  2742. sizeof(link_sec), &link_sec,
  2743. HCI_CMD_TIMEOUT);
  2744. }
  2745. int hci_write_fast_connectable_sync(struct hci_dev *hdev, bool enable)
  2746. {
  2747. struct hci_cp_write_page_scan_activity cp;
  2748. u8 type;
  2749. int err = 0;
  2750. if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
  2751. return 0;
  2752. if (hdev->hci_ver < BLUETOOTH_VER_1_2)
  2753. return 0;
  2754. memset(&cp, 0, sizeof(cp));
  2755. if (enable) {
  2756. type = PAGE_SCAN_TYPE_INTERLACED;
  2757. /* 160 msec page scan interval */
  2758. cp.interval = cpu_to_le16(0x0100);
  2759. } else {
  2760. type = hdev->def_page_scan_type;
  2761. cp.interval = cpu_to_le16(hdev->def_page_scan_int);
  2762. }
  2763. cp.window = cpu_to_le16(hdev->def_page_scan_window);
  2764. if (__cpu_to_le16(hdev->page_scan_interval) != cp.interval ||
  2765. __cpu_to_le16(hdev->page_scan_window) != cp.window) {
  2766. err = __hci_cmd_sync_status(hdev,
  2767. HCI_OP_WRITE_PAGE_SCAN_ACTIVITY,
  2768. sizeof(cp), &cp, HCI_CMD_TIMEOUT);
  2769. if (err)
  2770. return err;
  2771. }
  2772. if (hdev->page_scan_type != type)
  2773. err = __hci_cmd_sync_status(hdev,
  2774. HCI_OP_WRITE_PAGE_SCAN_TYPE,
  2775. sizeof(type), &type,
  2776. HCI_CMD_TIMEOUT);
  2777. return err;
  2778. }
  2779. static bool disconnected_accept_list_entries(struct hci_dev *hdev)
  2780. {
  2781. struct bdaddr_list *b;
  2782. list_for_each_entry(b, &hdev->accept_list, list) {
  2783. struct hci_conn *conn;
  2784. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &b->bdaddr);
  2785. if (!conn)
  2786. return true;
  2787. if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG)
  2788. return true;
  2789. }
  2790. return false;
  2791. }
  2792. static int hci_write_scan_enable_sync(struct hci_dev *hdev, u8 val)
  2793. {
  2794. return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SCAN_ENABLE,
  2795. sizeof(val), &val,
  2796. HCI_CMD_TIMEOUT);
  2797. }
  2798. int hci_update_scan_sync(struct hci_dev *hdev)
  2799. {
  2800. u8 scan;
  2801. if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
  2802. return 0;
  2803. if (!hdev_is_powered(hdev))
  2804. return 0;
  2805. if (mgmt_powering_down(hdev))
  2806. return 0;
  2807. if (hdev->scanning_paused)
  2808. return 0;
  2809. if (hci_dev_test_flag(hdev, HCI_CONNECTABLE) ||
  2810. disconnected_accept_list_entries(hdev))
  2811. scan = SCAN_PAGE;
  2812. else
  2813. scan = SCAN_DISABLED;
  2814. if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
  2815. scan |= SCAN_INQUIRY;
  2816. if (test_bit(HCI_PSCAN, &hdev->flags) == !!(scan & SCAN_PAGE) &&
  2817. test_bit(HCI_ISCAN, &hdev->flags) == !!(scan & SCAN_INQUIRY))
  2818. return 0;
  2819. return hci_write_scan_enable_sync(hdev, scan);
  2820. }
  2821. int hci_update_name_sync(struct hci_dev *hdev, const u8 *name)
  2822. {
  2823. struct hci_cp_write_local_name cp;
  2824. memset(&cp, 0, sizeof(cp));
  2825. memcpy(cp.name, name, sizeof(cp.name));
  2826. return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LOCAL_NAME,
  2827. sizeof(cp), &cp,
  2828. HCI_CMD_TIMEOUT);
  2829. }
  2830. /* This function perform powered update HCI command sequence after the HCI init
  2831. * sequence which end up resetting all states, the sequence is as follows:
  2832. *
  2833. * HCI_SSP_ENABLED(Enable SSP)
  2834. * HCI_LE_ENABLED(Enable LE)
  2835. * HCI_LE_ENABLED(ll_privacy_capable(Add local IRK to Resolving List) ->
  2836. * Update adv data)
  2837. * Enable Authentication
  2838. * lmp_bredr_capable(Set Fast Connectable -> Set Scan Type -> Set Class ->
  2839. * Set Name -> Set EIR)
  2840. * HCI_FORCE_STATIC_ADDR | BDADDR_ANY && !HCI_BREDR_ENABLED (Set Static Address)
  2841. */
  2842. int hci_powered_update_sync(struct hci_dev *hdev)
  2843. {
  2844. int err;
  2845. /* Register the available SMP channels (BR/EDR and LE) only when
  2846. * successfully powering on the controller. This late
  2847. * registration is required so that LE SMP can clearly decide if
  2848. * the public address or static address is used.
  2849. */
  2850. smp_register(hdev);
  2851. err = hci_write_ssp_mode_sync(hdev, 0x01);
  2852. if (err)
  2853. return err;
  2854. err = hci_write_le_host_supported_sync(hdev, 0x01, 0x00);
  2855. if (err)
  2856. return err;
  2857. err = hci_powered_update_adv_sync(hdev);
  2858. if (err)
  2859. return err;
  2860. err = hci_write_auth_enable_sync(hdev);
  2861. if (err)
  2862. return err;
  2863. if (lmp_bredr_capable(hdev)) {
  2864. if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE))
  2865. hci_write_fast_connectable_sync(hdev, true);
  2866. else
  2867. hci_write_fast_connectable_sync(hdev, false);
  2868. hci_update_scan_sync(hdev);
  2869. hci_update_class_sync(hdev);
  2870. hci_update_name_sync(hdev, hdev->dev_name);
  2871. hci_update_eir_sync(hdev);
  2872. }
  2873. /* If forcing static address is in use or there is no public
  2874. * address use the static address as random address (but skip
  2875. * the HCI command if the current random address is already the
  2876. * static one.
  2877. *
  2878. * In case BR/EDR has been disabled on a dual-mode controller
  2879. * and a static address has been configured, then use that
  2880. * address instead of the public BR/EDR address.
  2881. */
  2882. if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
  2883. (!bacmp(&hdev->bdaddr, BDADDR_ANY) &&
  2884. !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))) {
  2885. if (bacmp(&hdev->static_addr, BDADDR_ANY))
  2886. return hci_set_random_addr_sync(hdev,
  2887. &hdev->static_addr);
  2888. }
  2889. return 0;
  2890. }
  2891. /**
  2892. * hci_dev_get_bd_addr_from_property - Get the Bluetooth Device Address
  2893. * (BD_ADDR) for a HCI device from
  2894. * a firmware node property.
  2895. * @hdev: The HCI device
  2896. *
  2897. * Search the firmware node for 'local-bd-address'.
  2898. *
  2899. * All-zero BD addresses are rejected, because those could be properties
  2900. * that exist in the firmware tables, but were not updated by the firmware. For
  2901. * example, the DTS could define 'local-bd-address', with zero BD addresses.
  2902. */
  2903. static void hci_dev_get_bd_addr_from_property(struct hci_dev *hdev)
  2904. {
  2905. struct fwnode_handle *fwnode = dev_fwnode(hdev->dev.parent);
  2906. bdaddr_t ba;
  2907. int ret;
  2908. ret = fwnode_property_read_u8_array(fwnode, "local-bd-address",
  2909. (u8 *)&ba, sizeof(ba));
  2910. if (ret < 0 || !bacmp(&ba, BDADDR_ANY))
  2911. return;
  2912. if (hci_test_quirk(hdev, HCI_QUIRK_BDADDR_PROPERTY_BROKEN))
  2913. baswap(&hdev->public_addr, &ba);
  2914. else
  2915. bacpy(&hdev->public_addr, &ba);
  2916. }
  2917. struct hci_init_stage {
  2918. int (*func)(struct hci_dev *hdev);
  2919. };
  2920. /* Run init stage NULL terminated function table */
  2921. static int hci_init_stage_sync(struct hci_dev *hdev,
  2922. const struct hci_init_stage *stage)
  2923. {
  2924. size_t i;
  2925. for (i = 0; stage[i].func; i++) {
  2926. int err;
  2927. err = stage[i].func(hdev);
  2928. if (err)
  2929. return err;
  2930. }
  2931. return 0;
  2932. }
  2933. /* Read Local Version */
  2934. static int hci_read_local_version_sync(struct hci_dev *hdev)
  2935. {
  2936. return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_VERSION,
  2937. 0, NULL, HCI_CMD_TIMEOUT);
  2938. }
  2939. /* Read BD Address */
  2940. static int hci_read_bd_addr_sync(struct hci_dev *hdev)
  2941. {
  2942. return __hci_cmd_sync_status(hdev, HCI_OP_READ_BD_ADDR,
  2943. 0, NULL, HCI_CMD_TIMEOUT);
  2944. }
  2945. #define HCI_INIT(_func) \
  2946. { \
  2947. .func = _func, \
  2948. }
  2949. static const struct hci_init_stage hci_init0[] = {
  2950. /* HCI_OP_READ_LOCAL_VERSION */
  2951. HCI_INIT(hci_read_local_version_sync),
  2952. /* HCI_OP_READ_BD_ADDR */
  2953. HCI_INIT(hci_read_bd_addr_sync),
  2954. {}
  2955. };
  2956. int hci_reset_sync(struct hci_dev *hdev)
  2957. {
  2958. int err;
  2959. set_bit(HCI_RESET, &hdev->flags);
  2960. err = __hci_cmd_sync_status(hdev, HCI_OP_RESET, 0, NULL,
  2961. HCI_CMD_TIMEOUT);
  2962. if (err)
  2963. return err;
  2964. return 0;
  2965. }
  2966. static int hci_init0_sync(struct hci_dev *hdev)
  2967. {
  2968. int err;
  2969. bt_dev_dbg(hdev, "");
  2970. /* Reset */
  2971. if (!hci_test_quirk(hdev, HCI_QUIRK_RESET_ON_CLOSE)) {
  2972. err = hci_reset_sync(hdev);
  2973. if (err)
  2974. return err;
  2975. }
  2976. return hci_init_stage_sync(hdev, hci_init0);
  2977. }
  2978. static int hci_unconf_init_sync(struct hci_dev *hdev)
  2979. {
  2980. int err;
  2981. if (hci_test_quirk(hdev, HCI_QUIRK_RAW_DEVICE))
  2982. return 0;
  2983. err = hci_init0_sync(hdev);
  2984. if (err < 0)
  2985. return err;
  2986. if (hci_dev_test_flag(hdev, HCI_SETUP))
  2987. hci_debugfs_create_basic(hdev);
  2988. return 0;
  2989. }
  2990. /* Read Local Supported Features. */
  2991. static int hci_read_local_features_sync(struct hci_dev *hdev)
  2992. {
  2993. return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_FEATURES,
  2994. 0, NULL, HCI_CMD_TIMEOUT);
  2995. }
  2996. /* BR Controller init stage 1 command sequence */
  2997. static const struct hci_init_stage br_init1[] = {
  2998. /* HCI_OP_READ_LOCAL_FEATURES */
  2999. HCI_INIT(hci_read_local_features_sync),
  3000. /* HCI_OP_READ_LOCAL_VERSION */
  3001. HCI_INIT(hci_read_local_version_sync),
  3002. /* HCI_OP_READ_BD_ADDR */
  3003. HCI_INIT(hci_read_bd_addr_sync),
  3004. {}
  3005. };
  3006. /* Read Local Commands */
  3007. static int hci_read_local_cmds_sync(struct hci_dev *hdev)
  3008. {
  3009. /* All Bluetooth 1.2 and later controllers should support the
  3010. * HCI command for reading the local supported commands.
  3011. *
  3012. * Unfortunately some controllers indicate Bluetooth 1.2 support,
  3013. * but do not have support for this command. If that is the case,
  3014. * the driver can quirk the behavior and skip reading the local
  3015. * supported commands.
  3016. */
  3017. if (hdev->hci_ver > BLUETOOTH_VER_1_1 &&
  3018. !hci_test_quirk(hdev, HCI_QUIRK_BROKEN_LOCAL_COMMANDS))
  3019. return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_COMMANDS,
  3020. 0, NULL, HCI_CMD_TIMEOUT);
  3021. return 0;
  3022. }
  3023. static int hci_init1_sync(struct hci_dev *hdev)
  3024. {
  3025. int err;
  3026. bt_dev_dbg(hdev, "");
  3027. /* Reset */
  3028. if (!hci_test_quirk(hdev, HCI_QUIRK_RESET_ON_CLOSE)) {
  3029. err = hci_reset_sync(hdev);
  3030. if (err)
  3031. return err;
  3032. }
  3033. return hci_init_stage_sync(hdev, br_init1);
  3034. }
  3035. /* Read Buffer Size (ACL mtu, max pkt, etc.) */
  3036. static int hci_read_buffer_size_sync(struct hci_dev *hdev)
  3037. {
  3038. return __hci_cmd_sync_status(hdev, HCI_OP_READ_BUFFER_SIZE,
  3039. 0, NULL, HCI_CMD_TIMEOUT);
  3040. }
  3041. /* Read Class of Device */
  3042. static int hci_read_dev_class_sync(struct hci_dev *hdev)
  3043. {
  3044. return __hci_cmd_sync_status(hdev, HCI_OP_READ_CLASS_OF_DEV,
  3045. 0, NULL, HCI_CMD_TIMEOUT);
  3046. }
  3047. /* Read Local Name */
  3048. static int hci_read_local_name_sync(struct hci_dev *hdev)
  3049. {
  3050. return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_NAME,
  3051. 0, NULL, HCI_CMD_TIMEOUT);
  3052. }
  3053. /* Read Voice Setting */
  3054. static int hci_read_voice_setting_sync(struct hci_dev *hdev)
  3055. {
  3056. if (!read_voice_setting_capable(hdev))
  3057. return 0;
  3058. return __hci_cmd_sync_status(hdev, HCI_OP_READ_VOICE_SETTING,
  3059. 0, NULL, HCI_CMD_TIMEOUT);
  3060. }
  3061. /* Read Number of Supported IAC */
  3062. static int hci_read_num_supported_iac_sync(struct hci_dev *hdev)
  3063. {
  3064. return __hci_cmd_sync_status(hdev, HCI_OP_READ_NUM_SUPPORTED_IAC,
  3065. 0, NULL, HCI_CMD_TIMEOUT);
  3066. }
  3067. /* Read Current IAC LAP */
  3068. static int hci_read_current_iac_lap_sync(struct hci_dev *hdev)
  3069. {
  3070. return __hci_cmd_sync_status(hdev, HCI_OP_READ_CURRENT_IAC_LAP,
  3071. 0, NULL, HCI_CMD_TIMEOUT);
  3072. }
  3073. static int hci_set_event_filter_sync(struct hci_dev *hdev, u8 flt_type,
  3074. u8 cond_type, bdaddr_t *bdaddr,
  3075. u8 auto_accept)
  3076. {
  3077. struct hci_cp_set_event_filter cp;
  3078. if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
  3079. return 0;
  3080. if (hci_test_quirk(hdev, HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL))
  3081. return 0;
  3082. memset(&cp, 0, sizeof(cp));
  3083. cp.flt_type = flt_type;
  3084. if (flt_type != HCI_FLT_CLEAR_ALL) {
  3085. cp.cond_type = cond_type;
  3086. bacpy(&cp.addr_conn_flt.bdaddr, bdaddr);
  3087. cp.addr_conn_flt.auto_accept = auto_accept;
  3088. }
  3089. return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_FLT,
  3090. flt_type == HCI_FLT_CLEAR_ALL ?
  3091. sizeof(cp.flt_type) : sizeof(cp), &cp,
  3092. HCI_CMD_TIMEOUT);
  3093. }
  3094. static int hci_clear_event_filter_sync(struct hci_dev *hdev)
  3095. {
  3096. if (!hci_dev_test_flag(hdev, HCI_EVENT_FILTER_CONFIGURED))
  3097. return 0;
  3098. /* In theory the state machine should not reach here unless
  3099. * a hci_set_event_filter_sync() call succeeds, but we do
  3100. * the check both for parity and as a future reminder.
  3101. */
  3102. if (hci_test_quirk(hdev, HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL))
  3103. return 0;
  3104. return hci_set_event_filter_sync(hdev, HCI_FLT_CLEAR_ALL, 0x00,
  3105. BDADDR_ANY, 0x00);
  3106. }
  3107. /* Connection accept timeout ~20 secs */
  3108. static int hci_write_ca_timeout_sync(struct hci_dev *hdev)
  3109. {
  3110. __le16 param = cpu_to_le16(0x7d00);
  3111. return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CA_TIMEOUT,
  3112. sizeof(param), &param, HCI_CMD_TIMEOUT);
  3113. }
  3114. /* Enable SCO flow control if supported */
  3115. static int hci_write_sync_flowctl_sync(struct hci_dev *hdev)
  3116. {
  3117. struct hci_cp_write_sync_flowctl cp;
  3118. int err;
  3119. /* Check if the controller supports SCO and HCI_OP_WRITE_SYNC_FLOWCTL */
  3120. if (!lmp_sco_capable(hdev) || !(hdev->commands[10] & BIT(4)) ||
  3121. !hci_test_quirk(hdev, HCI_QUIRK_SYNC_FLOWCTL_SUPPORTED))
  3122. return 0;
  3123. memset(&cp, 0, sizeof(cp));
  3124. cp.enable = 0x01;
  3125. err = __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SYNC_FLOWCTL,
  3126. sizeof(cp), &cp, HCI_CMD_TIMEOUT);
  3127. if (!err)
  3128. hci_dev_set_flag(hdev, HCI_SCO_FLOWCTL);
  3129. return err;
  3130. }
  3131. /* BR Controller init stage 2 command sequence */
  3132. static const struct hci_init_stage br_init2[] = {
  3133. /* HCI_OP_READ_BUFFER_SIZE */
  3134. HCI_INIT(hci_read_buffer_size_sync),
  3135. /* HCI_OP_READ_CLASS_OF_DEV */
  3136. HCI_INIT(hci_read_dev_class_sync),
  3137. /* HCI_OP_READ_LOCAL_NAME */
  3138. HCI_INIT(hci_read_local_name_sync),
  3139. /* HCI_OP_READ_VOICE_SETTING */
  3140. HCI_INIT(hci_read_voice_setting_sync),
  3141. /* HCI_OP_READ_NUM_SUPPORTED_IAC */
  3142. HCI_INIT(hci_read_num_supported_iac_sync),
  3143. /* HCI_OP_READ_CURRENT_IAC_LAP */
  3144. HCI_INIT(hci_read_current_iac_lap_sync),
  3145. /* HCI_OP_SET_EVENT_FLT */
  3146. HCI_INIT(hci_clear_event_filter_sync),
  3147. /* HCI_OP_WRITE_CA_TIMEOUT */
  3148. HCI_INIT(hci_write_ca_timeout_sync),
  3149. /* HCI_OP_WRITE_SYNC_FLOWCTL */
  3150. HCI_INIT(hci_write_sync_flowctl_sync),
  3151. {}
  3152. };
  3153. static int hci_write_ssp_mode_1_sync(struct hci_dev *hdev)
  3154. {
  3155. u8 mode = 0x01;
  3156. if (!lmp_ssp_capable(hdev) || !hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
  3157. return 0;
  3158. /* When SSP is available, then the host features page
  3159. * should also be available as well. However some
  3160. * controllers list the max_page as 0 as long as SSP
  3161. * has not been enabled. To achieve proper debugging
  3162. * output, force the minimum max_page to 1 at least.
  3163. */
  3164. hdev->max_page = 0x01;
  3165. return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_MODE,
  3166. sizeof(mode), &mode, HCI_CMD_TIMEOUT);
  3167. }
  3168. static int hci_write_eir_sync(struct hci_dev *hdev)
  3169. {
  3170. struct hci_cp_write_eir cp;
  3171. if (!lmp_ssp_capable(hdev) || hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
  3172. return 0;
  3173. memset(hdev->eir, 0, sizeof(hdev->eir));
  3174. memset(&cp, 0, sizeof(cp));
  3175. return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp,
  3176. HCI_CMD_TIMEOUT);
  3177. }
  3178. static int hci_write_inquiry_mode_sync(struct hci_dev *hdev)
  3179. {
  3180. u8 mode;
  3181. if (!lmp_inq_rssi_capable(hdev) &&
  3182. !hci_test_quirk(hdev, HCI_QUIRK_FIXUP_INQUIRY_MODE))
  3183. return 0;
  3184. /* If Extended Inquiry Result events are supported, then
  3185. * they are clearly preferred over Inquiry Result with RSSI
  3186. * events.
  3187. */
  3188. mode = lmp_ext_inq_capable(hdev) ? 0x02 : 0x01;
  3189. return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_INQUIRY_MODE,
  3190. sizeof(mode), &mode, HCI_CMD_TIMEOUT);
  3191. }
  3192. static int hci_read_inq_rsp_tx_power_sync(struct hci_dev *hdev)
  3193. {
  3194. if (!lmp_inq_tx_pwr_capable(hdev))
  3195. return 0;
  3196. return __hci_cmd_sync_status(hdev, HCI_OP_READ_INQ_RSP_TX_POWER,
  3197. 0, NULL, HCI_CMD_TIMEOUT);
  3198. }
  3199. static int hci_read_local_ext_features_sync(struct hci_dev *hdev, u8 page)
  3200. {
  3201. struct hci_cp_read_local_ext_features cp;
  3202. if (!lmp_ext_feat_capable(hdev))
  3203. return 0;
  3204. memset(&cp, 0, sizeof(cp));
  3205. cp.page = page;
  3206. return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_EXT_FEATURES,
  3207. sizeof(cp), &cp, HCI_CMD_TIMEOUT);
  3208. }
  3209. static int hci_read_local_ext_features_1_sync(struct hci_dev *hdev)
  3210. {
  3211. return hci_read_local_ext_features_sync(hdev, 0x01);
  3212. }
  3213. /* HCI Controller init stage 2 command sequence */
  3214. static const struct hci_init_stage hci_init2[] = {
  3215. /* HCI_OP_READ_LOCAL_COMMANDS */
  3216. HCI_INIT(hci_read_local_cmds_sync),
  3217. /* HCI_OP_WRITE_SSP_MODE */
  3218. HCI_INIT(hci_write_ssp_mode_1_sync),
  3219. /* HCI_OP_WRITE_EIR */
  3220. HCI_INIT(hci_write_eir_sync),
  3221. /* HCI_OP_WRITE_INQUIRY_MODE */
  3222. HCI_INIT(hci_write_inquiry_mode_sync),
  3223. /* HCI_OP_READ_INQ_RSP_TX_POWER */
  3224. HCI_INIT(hci_read_inq_rsp_tx_power_sync),
  3225. /* HCI_OP_READ_LOCAL_EXT_FEATURES */
  3226. HCI_INIT(hci_read_local_ext_features_1_sync),
  3227. /* HCI_OP_WRITE_AUTH_ENABLE */
  3228. HCI_INIT(hci_write_auth_enable_sync),
  3229. {}
  3230. };
  3231. /* Read LE Buffer Size */
  3232. static int hci_le_read_buffer_size_sync(struct hci_dev *hdev)
  3233. {
  3234. /* Use Read LE Buffer Size V2 if supported */
  3235. if (iso_capable(hdev) && hdev->commands[41] & 0x20)
  3236. return __hci_cmd_sync_status(hdev,
  3237. HCI_OP_LE_READ_BUFFER_SIZE_V2,
  3238. 0, NULL, HCI_CMD_TIMEOUT);
  3239. return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_BUFFER_SIZE,
  3240. 0, NULL, HCI_CMD_TIMEOUT);
  3241. }
  3242. /* Read LE Local Supported Features */
  3243. static int hci_le_read_local_features_sync(struct hci_dev *hdev)
  3244. {
  3245. int err;
  3246. err = __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_LOCAL_FEATURES,
  3247. 0, NULL, HCI_CMD_TIMEOUT);
  3248. if (err)
  3249. return err;
  3250. if (ll_ext_feature_capable(hdev) && hdev->commands[47] & BIT(2))
  3251. return __hci_cmd_sync_status(hdev,
  3252. HCI_OP_LE_READ_ALL_LOCAL_FEATURES,
  3253. 0, NULL, HCI_CMD_TIMEOUT);
  3254. return err;
  3255. }
  3256. /* Read LE Supported States */
  3257. static int hci_le_read_supported_states_sync(struct hci_dev *hdev)
  3258. {
  3259. return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_SUPPORTED_STATES,
  3260. 0, NULL, HCI_CMD_TIMEOUT);
  3261. }
  3262. /* LE Controller init stage 2 command sequence */
  3263. static const struct hci_init_stage le_init2[] = {
  3264. /* HCI_OP_LE_READ_LOCAL_FEATURES */
  3265. HCI_INIT(hci_le_read_local_features_sync),
  3266. /* HCI_OP_LE_READ_BUFFER_SIZE */
  3267. HCI_INIT(hci_le_read_buffer_size_sync),
  3268. /* HCI_OP_LE_READ_SUPPORTED_STATES */
  3269. HCI_INIT(hci_le_read_supported_states_sync),
  3270. {}
  3271. };
  3272. static int hci_init2_sync(struct hci_dev *hdev)
  3273. {
  3274. int err;
  3275. bt_dev_dbg(hdev, "");
  3276. err = hci_init_stage_sync(hdev, hci_init2);
  3277. if (err)
  3278. return err;
  3279. if (lmp_bredr_capable(hdev)) {
  3280. err = hci_init_stage_sync(hdev, br_init2);
  3281. if (err)
  3282. return err;
  3283. } else {
  3284. hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED);
  3285. }
  3286. if (lmp_le_capable(hdev)) {
  3287. err = hci_init_stage_sync(hdev, le_init2);
  3288. if (err)
  3289. return err;
  3290. /* LE-only controllers have LE implicitly enabled */
  3291. if (!lmp_bredr_capable(hdev))
  3292. hci_dev_set_flag(hdev, HCI_LE_ENABLED);
  3293. }
  3294. return 0;
  3295. }
  3296. static int hci_set_event_mask_sync(struct hci_dev *hdev)
  3297. {
  3298. /* The second byte is 0xff instead of 0x9f (two reserved bits
  3299. * disabled) since a Broadcom 1.2 dongle doesn't respond to the
  3300. * command otherwise.
  3301. */
  3302. u8 events[8] = { 0xff, 0xff, 0xfb, 0xff, 0x00, 0x00, 0x00, 0x00 };
  3303. /* CSR 1.1 dongles does not accept any bitfield so don't try to set
  3304. * any event mask for pre 1.2 devices.
  3305. */
  3306. if (hdev->hci_ver < BLUETOOTH_VER_1_2)
  3307. return 0;
  3308. if (lmp_bredr_capable(hdev)) {
  3309. events[4] |= 0x01; /* Flow Specification Complete */
  3310. /* Don't set Disconnect Complete and mode change when
  3311. * suspended as that would wakeup the host when disconnecting
  3312. * due to suspend.
  3313. */
  3314. if (hdev->suspended) {
  3315. events[0] &= 0xef;
  3316. events[2] &= 0xf7;
  3317. }
  3318. } else {
  3319. /* Use a different default for LE-only devices */
  3320. memset(events, 0, sizeof(events));
  3321. events[1] |= 0x20; /* Command Complete */
  3322. events[1] |= 0x40; /* Command Status */
  3323. events[1] |= 0x80; /* Hardware Error */
  3324. /* If the controller supports the Disconnect command, enable
  3325. * the corresponding event. In addition enable packet flow
  3326. * control related events.
  3327. */
  3328. if (hdev->commands[0] & 0x20) {
  3329. /* Don't set Disconnect Complete when suspended as that
  3330. * would wakeup the host when disconnecting due to
  3331. * suspend.
  3332. */
  3333. if (!hdev->suspended)
  3334. events[0] |= 0x10; /* Disconnection Complete */
  3335. events[2] |= 0x04; /* Number of Completed Packets */
  3336. events[3] |= 0x02; /* Data Buffer Overflow */
  3337. }
  3338. /* If the controller supports the Read Remote Version
  3339. * Information command, enable the corresponding event.
  3340. */
  3341. if (hdev->commands[2] & 0x80)
  3342. events[1] |= 0x08; /* Read Remote Version Information
  3343. * Complete
  3344. */
  3345. if (hdev->le_features[0] & HCI_LE_ENCRYPTION) {
  3346. events[0] |= 0x80; /* Encryption Change */
  3347. events[5] |= 0x80; /* Encryption Key Refresh Complete */
  3348. }
  3349. }
  3350. if (lmp_inq_rssi_capable(hdev) ||
  3351. hci_test_quirk(hdev, HCI_QUIRK_FIXUP_INQUIRY_MODE))
  3352. events[4] |= 0x02; /* Inquiry Result with RSSI */
  3353. if (lmp_ext_feat_capable(hdev))
  3354. events[4] |= 0x04; /* Read Remote Extended Features Complete */
  3355. if (lmp_esco_capable(hdev)) {
  3356. events[5] |= 0x08; /* Synchronous Connection Complete */
  3357. events[5] |= 0x10; /* Synchronous Connection Changed */
  3358. }
  3359. if (lmp_sniffsubr_capable(hdev))
  3360. events[5] |= 0x20; /* Sniff Subrating */
  3361. if (lmp_pause_enc_capable(hdev))
  3362. events[5] |= 0x80; /* Encryption Key Refresh Complete */
  3363. if (lmp_ext_inq_capable(hdev))
  3364. events[5] |= 0x40; /* Extended Inquiry Result */
  3365. if (lmp_no_flush_capable(hdev))
  3366. events[7] |= 0x01; /* Enhanced Flush Complete */
  3367. if (lmp_lsto_capable(hdev))
  3368. events[6] |= 0x80; /* Link Supervision Timeout Changed */
  3369. if (lmp_ssp_capable(hdev)) {
  3370. events[6] |= 0x01; /* IO Capability Request */
  3371. events[6] |= 0x02; /* IO Capability Response */
  3372. events[6] |= 0x04; /* User Confirmation Request */
  3373. events[6] |= 0x08; /* User Passkey Request */
  3374. events[6] |= 0x10; /* Remote OOB Data Request */
  3375. events[6] |= 0x20; /* Simple Pairing Complete */
  3376. events[7] |= 0x04; /* User Passkey Notification */
  3377. events[7] |= 0x08; /* Keypress Notification */
  3378. events[7] |= 0x10; /* Remote Host Supported
  3379. * Features Notification
  3380. */
  3381. }
  3382. if (lmp_le_capable(hdev))
  3383. events[7] |= 0x20; /* LE Meta-Event */
  3384. return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_MASK,
  3385. sizeof(events), events, HCI_CMD_TIMEOUT);
  3386. }
  3387. static int hci_read_stored_link_key_sync(struct hci_dev *hdev)
  3388. {
  3389. struct hci_cp_read_stored_link_key cp;
  3390. if (!(hdev->commands[6] & 0x20) ||
  3391. hci_test_quirk(hdev, HCI_QUIRK_BROKEN_STORED_LINK_KEY))
  3392. return 0;
  3393. memset(&cp, 0, sizeof(cp));
  3394. bacpy(&cp.bdaddr, BDADDR_ANY);
  3395. cp.read_all = 0x01;
  3396. return __hci_cmd_sync_status(hdev, HCI_OP_READ_STORED_LINK_KEY,
  3397. sizeof(cp), &cp, HCI_CMD_TIMEOUT);
  3398. }
  3399. static int hci_setup_link_policy_sync(struct hci_dev *hdev)
  3400. {
  3401. struct hci_cp_write_def_link_policy cp;
  3402. u16 link_policy = 0;
  3403. if (!(hdev->commands[5] & 0x10))
  3404. return 0;
  3405. memset(&cp, 0, sizeof(cp));
  3406. if (lmp_rswitch_capable(hdev))
  3407. link_policy |= HCI_LP_RSWITCH;
  3408. if (lmp_hold_capable(hdev))
  3409. link_policy |= HCI_LP_HOLD;
  3410. if (lmp_sniff_capable(hdev))
  3411. link_policy |= HCI_LP_SNIFF;
  3412. if (lmp_park_capable(hdev))
  3413. link_policy |= HCI_LP_PARK;
  3414. cp.policy = cpu_to_le16(link_policy);
  3415. return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_DEF_LINK_POLICY,
  3416. sizeof(cp), &cp, HCI_CMD_TIMEOUT);
  3417. }
  3418. static int hci_read_page_scan_activity_sync(struct hci_dev *hdev)
  3419. {
  3420. if (!(hdev->commands[8] & 0x01))
  3421. return 0;
  3422. return __hci_cmd_sync_status(hdev, HCI_OP_READ_PAGE_SCAN_ACTIVITY,
  3423. 0, NULL, HCI_CMD_TIMEOUT);
  3424. }
  3425. static int hci_read_def_err_data_reporting_sync(struct hci_dev *hdev)
  3426. {
  3427. if (!(hdev->commands[18] & 0x04) ||
  3428. !(hdev->features[0][6] & LMP_ERR_DATA_REPORTING) ||
  3429. hci_test_quirk(hdev, HCI_QUIRK_BROKEN_ERR_DATA_REPORTING))
  3430. return 0;
  3431. return __hci_cmd_sync_status(hdev, HCI_OP_READ_DEF_ERR_DATA_REPORTING,
  3432. 0, NULL, HCI_CMD_TIMEOUT);
  3433. }
  3434. static int hci_read_page_scan_type_sync(struct hci_dev *hdev)
  3435. {
  3436. /* Some older Broadcom based Bluetooth 1.2 controllers do not
  3437. * support the Read Page Scan Type command. Check support for
  3438. * this command in the bit mask of supported commands.
  3439. */
  3440. if (!(hdev->commands[13] & 0x01) ||
  3441. hci_test_quirk(hdev, HCI_QUIRK_BROKEN_READ_PAGE_SCAN_TYPE))
  3442. return 0;
  3443. return __hci_cmd_sync_status(hdev, HCI_OP_READ_PAGE_SCAN_TYPE,
  3444. 0, NULL, HCI_CMD_TIMEOUT);
  3445. }
  3446. /* Read features beyond page 1 if available */
  3447. static int hci_read_local_ext_features_all_sync(struct hci_dev *hdev)
  3448. {
  3449. u8 page;
  3450. int err;
  3451. if (!lmp_ext_feat_capable(hdev))
  3452. return 0;
  3453. for (page = 2; page < HCI_MAX_PAGES && page <= hdev->max_page;
  3454. page++) {
  3455. err = hci_read_local_ext_features_sync(hdev, page);
  3456. if (err)
  3457. return err;
  3458. }
  3459. return 0;
  3460. }
  3461. /* HCI Controller init stage 3 command sequence */
  3462. static const struct hci_init_stage hci_init3[] = {
  3463. /* HCI_OP_SET_EVENT_MASK */
  3464. HCI_INIT(hci_set_event_mask_sync),
  3465. /* HCI_OP_READ_STORED_LINK_KEY */
  3466. HCI_INIT(hci_read_stored_link_key_sync),
  3467. /* HCI_OP_WRITE_DEF_LINK_POLICY */
  3468. HCI_INIT(hci_setup_link_policy_sync),
  3469. /* HCI_OP_READ_PAGE_SCAN_ACTIVITY */
  3470. HCI_INIT(hci_read_page_scan_activity_sync),
  3471. /* HCI_OP_READ_DEF_ERR_DATA_REPORTING */
  3472. HCI_INIT(hci_read_def_err_data_reporting_sync),
  3473. /* HCI_OP_READ_PAGE_SCAN_TYPE */
  3474. HCI_INIT(hci_read_page_scan_type_sync),
  3475. /* HCI_OP_READ_LOCAL_EXT_FEATURES */
  3476. HCI_INIT(hci_read_local_ext_features_all_sync),
  3477. {}
  3478. };
  3479. static int hci_le_set_event_mask_sync(struct hci_dev *hdev)
  3480. {
  3481. u8 events[8];
  3482. if (!lmp_le_capable(hdev))
  3483. return 0;
  3484. memset(events, 0, sizeof(events));
  3485. if (hdev->le_features[0] & HCI_LE_ENCRYPTION)
  3486. events[0] |= 0x10; /* LE Long Term Key Request */
  3487. /* If controller supports the Connection Parameters Request
  3488. * Link Layer Procedure, enable the corresponding event.
  3489. */
  3490. if (hdev->le_features[0] & HCI_LE_CONN_PARAM_REQ_PROC)
  3491. /* LE Remote Connection Parameter Request */
  3492. events[0] |= 0x20;
  3493. /* If the controller supports the Data Length Extension
  3494. * feature, enable the corresponding event.
  3495. */
  3496. if (hdev->le_features[0] & HCI_LE_DATA_LEN_EXT)
  3497. events[0] |= 0x40; /* LE Data Length Change */
  3498. /* If the controller supports LL Privacy feature or LE Extended Adv,
  3499. * enable the corresponding event.
  3500. */
  3501. if (use_enhanced_conn_complete(hdev))
  3502. events[1] |= 0x02; /* LE Enhanced Connection Complete */
  3503. /* Mark Device Privacy if Privacy Mode is supported */
  3504. if (privacy_mode_capable(hdev))
  3505. hdev->conn_flags |= HCI_CONN_FLAG_DEVICE_PRIVACY;
  3506. /* Mark Address Resolution if LL Privacy is supported */
  3507. if (ll_privacy_capable(hdev))
  3508. hdev->conn_flags |= HCI_CONN_FLAG_ADDRESS_RESOLUTION;
  3509. /* Mark PAST if supported */
  3510. if (past_capable(hdev))
  3511. hdev->conn_flags |= HCI_CONN_FLAG_PAST;
  3512. /* If the controller supports Extended Scanner Filter
  3513. * Policies, enable the corresponding event.
  3514. */
  3515. if (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY)
  3516. events[1] |= 0x04; /* LE Direct Advertising Report */
  3517. /* If the controller supports Channel Selection Algorithm #2
  3518. * feature, enable the corresponding event.
  3519. */
  3520. if (hdev->le_features[1] & HCI_LE_CHAN_SEL_ALG2)
  3521. events[2] |= 0x08; /* LE Channel Selection Algorithm */
  3522. /* If the controller supports the LE Set Scan Enable command,
  3523. * enable the corresponding advertising report event.
  3524. */
  3525. if (hdev->commands[26] & 0x08)
  3526. events[0] |= 0x02; /* LE Advertising Report */
  3527. /* If the controller supports the LE Create Connection
  3528. * command, enable the corresponding event.
  3529. */
  3530. if (hdev->commands[26] & 0x10)
  3531. events[0] |= 0x01; /* LE Connection Complete */
  3532. /* If the controller supports the LE Connection Update
  3533. * command, enable the corresponding event.
  3534. */
  3535. if (hdev->commands[27] & 0x04)
  3536. events[0] |= 0x04; /* LE Connection Update Complete */
  3537. /* If the controller supports the LE Read Remote Used Features
  3538. * command, enable the corresponding event.
  3539. */
  3540. if (hdev->commands[27] & 0x20)
  3541. /* LE Read Remote Used Features Complete */
  3542. events[0] |= 0x08;
  3543. /* If the controller supports the LE Read Local P-256
  3544. * Public Key command, enable the corresponding event.
  3545. */
  3546. if (hdev->commands[34] & 0x02)
  3547. /* LE Read Local P-256 Public Key Complete */
  3548. events[0] |= 0x80;
  3549. /* If the controller supports the LE Generate DHKey
  3550. * command, enable the corresponding event.
  3551. */
  3552. if (hdev->commands[34] & 0x04)
  3553. events[1] |= 0x01; /* LE Generate DHKey Complete */
  3554. /* If the controller supports the LE Set Default PHY or
  3555. * LE Set PHY commands, enable the corresponding event.
  3556. */
  3557. if (hdev->commands[35] & (0x20 | 0x40))
  3558. events[1] |= 0x08; /* LE PHY Update Complete */
  3559. /* If the controller supports LE Set Extended Scan Parameters
  3560. * and LE Set Extended Scan Enable commands, enable the
  3561. * corresponding event.
  3562. */
  3563. if (use_ext_scan(hdev))
  3564. events[1] |= 0x10; /* LE Extended Advertising Report */
  3565. /* If the controller supports the LE Extended Advertising
  3566. * command, enable the corresponding event.
  3567. */
  3568. if (ext_adv_capable(hdev))
  3569. events[2] |= 0x02; /* LE Advertising Set Terminated */
  3570. if (past_receiver_capable(hdev))
  3571. events[2] |= 0x80; /* LE PAST Received */
  3572. if (cis_capable(hdev)) {
  3573. events[3] |= 0x01; /* LE CIS Established */
  3574. if (cis_peripheral_capable(hdev))
  3575. events[3] |= 0x02; /* LE CIS Request */
  3576. }
  3577. if (bis_capable(hdev)) {
  3578. events[1] |= 0x20; /* LE PA Report */
  3579. events[1] |= 0x40; /* LE PA Sync Established */
  3580. events[1] |= 0x80; /* LE PA Sync Lost */
  3581. events[3] |= 0x04; /* LE Create BIG Complete */
  3582. events[3] |= 0x08; /* LE Terminate BIG Complete */
  3583. events[3] |= 0x10; /* LE BIG Sync Established */
  3584. events[3] |= 0x20; /* LE BIG Sync Loss */
  3585. events[4] |= 0x02; /* LE BIG Info Advertising Report */
  3586. }
  3587. if (le_cs_capable(hdev)) {
  3588. /* Channel Sounding events */
  3589. events[5] |= 0x08; /* LE CS Read Remote Supported Cap Complete event */
  3590. events[5] |= 0x10; /* LE CS Read Remote FAE Table Complete event */
  3591. events[5] |= 0x20; /* LE CS Security Enable Complete event */
  3592. events[5] |= 0x40; /* LE CS Config Complete event */
  3593. events[5] |= 0x80; /* LE CS Procedure Enable Complete event */
  3594. events[6] |= 0x01; /* LE CS Subevent Result event */
  3595. events[6] |= 0x02; /* LE CS Subevent Result Continue event */
  3596. events[6] |= 0x04; /* LE CS Test End Complete event */
  3597. }
  3598. return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EVENT_MASK,
  3599. sizeof(events), events, HCI_CMD_TIMEOUT);
  3600. }
  3601. /* Read LE Advertising Channel TX Power */
  3602. static int hci_le_read_adv_tx_power_sync(struct hci_dev *hdev)
  3603. {
  3604. if ((hdev->commands[25] & 0x40) && !ext_adv_capable(hdev)) {
  3605. /* HCI TS spec forbids mixing of legacy and extended
  3606. * advertising commands wherein READ_ADV_TX_POWER is
  3607. * also included. So do not call it if extended adv
  3608. * is supported otherwise controller will return
  3609. * COMMAND_DISALLOWED for extended commands.
  3610. */
  3611. return __hci_cmd_sync_status(hdev,
  3612. HCI_OP_LE_READ_ADV_TX_POWER,
  3613. 0, NULL, HCI_CMD_TIMEOUT);
  3614. }
  3615. return 0;
  3616. }
  3617. /* Read LE Min/Max Tx Power*/
  3618. static int hci_le_read_tx_power_sync(struct hci_dev *hdev)
  3619. {
  3620. if (!(hdev->commands[38] & 0x80) ||
  3621. hci_test_quirk(hdev, HCI_QUIRK_BROKEN_READ_TRANSMIT_POWER))
  3622. return 0;
  3623. return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_TRANSMIT_POWER,
  3624. 0, NULL, HCI_CMD_TIMEOUT);
  3625. }
  3626. /* Read LE Accept List Size */
  3627. static int hci_le_read_accept_list_size_sync(struct hci_dev *hdev)
  3628. {
  3629. if (!(hdev->commands[26] & 0x40))
  3630. return 0;
  3631. return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_ACCEPT_LIST_SIZE,
  3632. 0, NULL, HCI_CMD_TIMEOUT);
  3633. }
  3634. /* Read LE Resolving List Size */
  3635. static int hci_le_read_resolv_list_size_sync(struct hci_dev *hdev)
  3636. {
  3637. if (!(hdev->commands[34] & 0x40))
  3638. return 0;
  3639. return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_RESOLV_LIST_SIZE,
  3640. 0, NULL, HCI_CMD_TIMEOUT);
  3641. }
  3642. /* Clear LE Resolving List */
  3643. static int hci_le_clear_resolv_list_sync(struct hci_dev *hdev)
  3644. {
  3645. if (!(hdev->commands[34] & 0x20))
  3646. return 0;
  3647. return __hci_cmd_sync_status(hdev, HCI_OP_LE_CLEAR_RESOLV_LIST, 0, NULL,
  3648. HCI_CMD_TIMEOUT);
  3649. }
  3650. /* Set RPA timeout */
  3651. static int hci_le_set_rpa_timeout_sync(struct hci_dev *hdev)
  3652. {
  3653. __le16 timeout = cpu_to_le16(hdev->rpa_timeout);
  3654. if (!(hdev->commands[35] & 0x04) ||
  3655. hci_test_quirk(hdev, HCI_QUIRK_BROKEN_SET_RPA_TIMEOUT))
  3656. return 0;
  3657. return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_RPA_TIMEOUT,
  3658. sizeof(timeout), &timeout,
  3659. HCI_CMD_TIMEOUT);
  3660. }
  3661. /* Read LE Maximum Data Length */
  3662. static int hci_le_read_max_data_len_sync(struct hci_dev *hdev)
  3663. {
  3664. if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
  3665. return 0;
  3666. return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_MAX_DATA_LEN, 0, NULL,
  3667. HCI_CMD_TIMEOUT);
  3668. }
  3669. /* Read LE Suggested Default Data Length */
  3670. static int hci_le_read_def_data_len_sync(struct hci_dev *hdev)
  3671. {
  3672. if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
  3673. return 0;
  3674. return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_DEF_DATA_LEN, 0, NULL,
  3675. HCI_CMD_TIMEOUT);
  3676. }
  3677. /* Read LE Number of Supported Advertising Sets */
  3678. static int hci_le_read_num_support_adv_sets_sync(struct hci_dev *hdev)
  3679. {
  3680. if (!ext_adv_capable(hdev))
  3681. return 0;
  3682. return __hci_cmd_sync_status(hdev,
  3683. HCI_OP_LE_READ_NUM_SUPPORTED_ADV_SETS,
  3684. 0, NULL, HCI_CMD_TIMEOUT);
  3685. }
  3686. /* Write LE Host Supported */
  3687. static int hci_set_le_support_sync(struct hci_dev *hdev)
  3688. {
  3689. struct hci_cp_write_le_host_supported cp;
  3690. /* LE-only devices do not support explicit enablement */
  3691. if (!lmp_bredr_capable(hdev))
  3692. return 0;
  3693. memset(&cp, 0, sizeof(cp));
  3694. if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
  3695. cp.le = 0x01;
  3696. cp.simul = 0x00;
  3697. }
  3698. if (cp.le == lmp_host_le_capable(hdev))
  3699. return 0;
  3700. return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED,
  3701. sizeof(cp), &cp, HCI_CMD_TIMEOUT);
  3702. }
  3703. /* LE Set Host Feature */
  3704. static int hci_le_set_host_feature_sync(struct hci_dev *hdev, u8 bit, u8 value)
  3705. {
  3706. struct hci_cp_le_set_host_feature cp;
  3707. memset(&cp, 0, sizeof(cp));
  3708. /* Connected Isochronous Channels (Host Support) */
  3709. cp.bit_number = bit;
  3710. cp.bit_value = value;
  3711. return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_HOST_FEATURE,
  3712. sizeof(cp), &cp, HCI_CMD_TIMEOUT);
  3713. }
  3714. /* Set Host Features, each feature needs to be sent separately since
  3715. * HCI_OP_LE_SET_HOST_FEATURE doesn't support setting all of them at once.
  3716. */
  3717. static int hci_le_set_host_features_sync(struct hci_dev *hdev)
  3718. {
  3719. int err;
  3720. if (cis_capable(hdev)) {
  3721. /* Connected Isochronous Channels (Host Support) */
  3722. err = hci_le_set_host_feature_sync(hdev, 32,
  3723. (iso_enabled(hdev) ? 0x01 :
  3724. 0x00));
  3725. if (err)
  3726. return err;
  3727. }
  3728. if (le_cs_capable(hdev))
  3729. /* Channel Sounding (Host Support) */
  3730. err = hci_le_set_host_feature_sync(hdev, 47, 0x01);
  3731. return err;
  3732. }
  3733. /* LE Controller init stage 3 command sequence */
  3734. static const struct hci_init_stage le_init3[] = {
  3735. /* HCI_OP_LE_SET_EVENT_MASK */
  3736. HCI_INIT(hci_le_set_event_mask_sync),
  3737. /* HCI_OP_LE_READ_ADV_TX_POWER */
  3738. HCI_INIT(hci_le_read_adv_tx_power_sync),
  3739. /* HCI_OP_LE_READ_TRANSMIT_POWER */
  3740. HCI_INIT(hci_le_read_tx_power_sync),
  3741. /* HCI_OP_LE_READ_ACCEPT_LIST_SIZE */
  3742. HCI_INIT(hci_le_read_accept_list_size_sync),
  3743. /* HCI_OP_LE_CLEAR_ACCEPT_LIST */
  3744. HCI_INIT(hci_le_clear_accept_list_sync),
  3745. /* HCI_OP_LE_READ_RESOLV_LIST_SIZE */
  3746. HCI_INIT(hci_le_read_resolv_list_size_sync),
  3747. /* HCI_OP_LE_CLEAR_RESOLV_LIST */
  3748. HCI_INIT(hci_le_clear_resolv_list_sync),
  3749. /* HCI_OP_LE_SET_RPA_TIMEOUT */
  3750. HCI_INIT(hci_le_set_rpa_timeout_sync),
  3751. /* HCI_OP_LE_READ_MAX_DATA_LEN */
  3752. HCI_INIT(hci_le_read_max_data_len_sync),
  3753. /* HCI_OP_LE_READ_DEF_DATA_LEN */
  3754. HCI_INIT(hci_le_read_def_data_len_sync),
  3755. /* HCI_OP_LE_READ_NUM_SUPPORTED_ADV_SETS */
  3756. HCI_INIT(hci_le_read_num_support_adv_sets_sync),
  3757. /* HCI_OP_WRITE_LE_HOST_SUPPORTED */
  3758. HCI_INIT(hci_set_le_support_sync),
  3759. /* HCI_OP_LE_SET_HOST_FEATURE */
  3760. HCI_INIT(hci_le_set_host_features_sync),
  3761. {}
  3762. };
  3763. static int hci_init3_sync(struct hci_dev *hdev)
  3764. {
  3765. int err;
  3766. bt_dev_dbg(hdev, "");
  3767. err = hci_init_stage_sync(hdev, hci_init3);
  3768. if (err)
  3769. return err;
  3770. if (lmp_le_capable(hdev))
  3771. return hci_init_stage_sync(hdev, le_init3);
  3772. return 0;
  3773. }
  3774. static int hci_delete_stored_link_key_sync(struct hci_dev *hdev)
  3775. {
  3776. struct hci_cp_delete_stored_link_key cp;
  3777. /* Some Broadcom based Bluetooth controllers do not support the
  3778. * Delete Stored Link Key command. They are clearly indicating its
  3779. * absence in the bit mask of supported commands.
  3780. *
  3781. * Check the supported commands and only if the command is marked
  3782. * as supported send it. If not supported assume that the controller
  3783. * does not have actual support for stored link keys which makes this
  3784. * command redundant anyway.
  3785. *
  3786. * Some controllers indicate that they support handling deleting
  3787. * stored link keys, but they don't. The quirk lets a driver
  3788. * just disable this command.
  3789. */
  3790. if (!(hdev->commands[6] & 0x80) ||
  3791. hci_test_quirk(hdev, HCI_QUIRK_BROKEN_STORED_LINK_KEY))
  3792. return 0;
  3793. memset(&cp, 0, sizeof(cp));
  3794. bacpy(&cp.bdaddr, BDADDR_ANY);
  3795. cp.delete_all = 0x01;
  3796. return __hci_cmd_sync_status(hdev, HCI_OP_DELETE_STORED_LINK_KEY,
  3797. sizeof(cp), &cp, HCI_CMD_TIMEOUT);
  3798. }
  3799. static int hci_set_event_mask_page_2_sync(struct hci_dev *hdev)
  3800. {
  3801. u8 events[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  3802. bool changed = false;
  3803. /* Set event mask page 2 if the HCI command for it is supported */
  3804. if (!(hdev->commands[22] & 0x04))
  3805. return 0;
  3806. /* If Connectionless Peripheral Broadcast central role is supported
  3807. * enable all necessary events for it.
  3808. */
  3809. if (lmp_cpb_central_capable(hdev)) {
  3810. events[1] |= 0x40; /* Triggered Clock Capture */
  3811. events[1] |= 0x80; /* Synchronization Train Complete */
  3812. events[2] |= 0x08; /* Truncated Page Complete */
  3813. events[2] |= 0x20; /* CPB Channel Map Change */
  3814. changed = true;
  3815. }
  3816. /* If Connectionless Peripheral Broadcast peripheral role is supported
  3817. * enable all necessary events for it.
  3818. */
  3819. if (lmp_cpb_peripheral_capable(hdev)) {
  3820. events[2] |= 0x01; /* Synchronization Train Received */
  3821. events[2] |= 0x02; /* CPB Receive */
  3822. events[2] |= 0x04; /* CPB Timeout */
  3823. events[2] |= 0x10; /* Peripheral Page Response Timeout */
  3824. changed = true;
  3825. }
  3826. /* Enable Authenticated Payload Timeout Expired event if supported */
  3827. if (lmp_ping_capable(hdev) || hdev->le_features[0] & HCI_LE_PING) {
  3828. events[2] |= 0x80;
  3829. changed = true;
  3830. }
  3831. /* Some Broadcom based controllers indicate support for Set Event
  3832. * Mask Page 2 command, but then actually do not support it. Since
  3833. * the default value is all bits set to zero, the command is only
  3834. * required if the event mask has to be changed. In case no change
  3835. * to the event mask is needed, skip this command.
  3836. */
  3837. if (!changed)
  3838. return 0;
  3839. return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_MASK_PAGE_2,
  3840. sizeof(events), events, HCI_CMD_TIMEOUT);
  3841. }
  3842. /* Read local codec list if the HCI command is supported */
  3843. static int hci_read_local_codecs_sync(struct hci_dev *hdev)
  3844. {
  3845. if (hdev->commands[45] & 0x04)
  3846. hci_read_supported_codecs_v2(hdev);
  3847. else if (hdev->commands[29] & 0x20)
  3848. hci_read_supported_codecs(hdev);
  3849. return 0;
  3850. }
  3851. /* Read local pairing options if the HCI command is supported */
  3852. static int hci_read_local_pairing_opts_sync(struct hci_dev *hdev)
  3853. {
  3854. if (!(hdev->commands[41] & 0x08))
  3855. return 0;
  3856. return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_PAIRING_OPTS,
  3857. 0, NULL, HCI_CMD_TIMEOUT);
  3858. }
  3859. /* Get MWS transport configuration if the HCI command is supported */
  3860. static int hci_get_mws_transport_config_sync(struct hci_dev *hdev)
  3861. {
  3862. if (!mws_transport_config_capable(hdev))
  3863. return 0;
  3864. return __hci_cmd_sync_status(hdev, HCI_OP_GET_MWS_TRANSPORT_CONFIG,
  3865. 0, NULL, HCI_CMD_TIMEOUT);
  3866. }
  3867. /* Check for Synchronization Train support */
  3868. static int hci_read_sync_train_params_sync(struct hci_dev *hdev)
  3869. {
  3870. if (!lmp_sync_train_capable(hdev))
  3871. return 0;
  3872. return __hci_cmd_sync_status(hdev, HCI_OP_READ_SYNC_TRAIN_PARAMS,
  3873. 0, NULL, HCI_CMD_TIMEOUT);
  3874. }
  3875. /* Enable Secure Connections if supported and configured */
  3876. static int hci_write_sc_support_1_sync(struct hci_dev *hdev)
  3877. {
  3878. u8 support = 0x01;
  3879. if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED) ||
  3880. !bredr_sc_enabled(hdev))
  3881. return 0;
  3882. return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SC_SUPPORT,
  3883. sizeof(support), &support,
  3884. HCI_CMD_TIMEOUT);
  3885. }
  3886. /* Set erroneous data reporting if supported to the wideband speech
  3887. * setting value
  3888. */
  3889. static int hci_set_err_data_report_sync(struct hci_dev *hdev)
  3890. {
  3891. struct hci_cp_write_def_err_data_reporting cp;
  3892. bool enabled = hci_dev_test_flag(hdev, HCI_WIDEBAND_SPEECH_ENABLED);
  3893. if (!(hdev->commands[18] & 0x08) ||
  3894. !(hdev->features[0][6] & LMP_ERR_DATA_REPORTING) ||
  3895. hci_test_quirk(hdev, HCI_QUIRK_BROKEN_ERR_DATA_REPORTING))
  3896. return 0;
  3897. if (enabled == hdev->err_data_reporting)
  3898. return 0;
  3899. memset(&cp, 0, sizeof(cp));
  3900. cp.err_data_reporting = enabled ? ERR_DATA_REPORTING_ENABLED :
  3901. ERR_DATA_REPORTING_DISABLED;
  3902. return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_DEF_ERR_DATA_REPORTING,
  3903. sizeof(cp), &cp, HCI_CMD_TIMEOUT);
  3904. }
  3905. static const struct hci_init_stage hci_init4[] = {
  3906. /* HCI_OP_DELETE_STORED_LINK_KEY */
  3907. HCI_INIT(hci_delete_stored_link_key_sync),
  3908. /* HCI_OP_SET_EVENT_MASK_PAGE_2 */
  3909. HCI_INIT(hci_set_event_mask_page_2_sync),
  3910. /* HCI_OP_READ_LOCAL_CODECS */
  3911. HCI_INIT(hci_read_local_codecs_sync),
  3912. /* HCI_OP_READ_LOCAL_PAIRING_OPTS */
  3913. HCI_INIT(hci_read_local_pairing_opts_sync),
  3914. /* HCI_OP_GET_MWS_TRANSPORT_CONFIG */
  3915. HCI_INIT(hci_get_mws_transport_config_sync),
  3916. /* HCI_OP_READ_SYNC_TRAIN_PARAMS */
  3917. HCI_INIT(hci_read_sync_train_params_sync),
  3918. /* HCI_OP_WRITE_SC_SUPPORT */
  3919. HCI_INIT(hci_write_sc_support_1_sync),
  3920. /* HCI_OP_WRITE_DEF_ERR_DATA_REPORTING */
  3921. HCI_INIT(hci_set_err_data_report_sync),
  3922. {}
  3923. };
  3924. /* Set Suggested Default Data Length to maximum if supported */
  3925. static int hci_le_set_write_def_data_len_sync(struct hci_dev *hdev)
  3926. {
  3927. struct hci_cp_le_write_def_data_len cp;
  3928. if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
  3929. return 0;
  3930. memset(&cp, 0, sizeof(cp));
  3931. cp.tx_len = cpu_to_le16(hdev->le_max_tx_len);
  3932. cp.tx_time = cpu_to_le16(hdev->le_max_tx_time);
  3933. return __hci_cmd_sync_status(hdev, HCI_OP_LE_WRITE_DEF_DATA_LEN,
  3934. sizeof(cp), &cp, HCI_CMD_TIMEOUT);
  3935. }
  3936. /* Set Default PHY parameters if command is supported, enables all supported
  3937. * PHYs according to the LE Features bits.
  3938. */
  3939. static int hci_le_set_default_phy_sync(struct hci_dev *hdev)
  3940. {
  3941. struct hci_cp_le_set_default_phy cp;
  3942. if (!(hdev->commands[35] & 0x20)) {
  3943. /* If the command is not supported it means only 1M PHY is
  3944. * supported.
  3945. */
  3946. hdev->le_tx_def_phys = HCI_LE_SET_PHY_1M;
  3947. hdev->le_rx_def_phys = HCI_LE_SET_PHY_1M;
  3948. return 0;
  3949. }
  3950. memset(&cp, 0, sizeof(cp));
  3951. cp.all_phys = 0x00;
  3952. cp.tx_phys = HCI_LE_SET_PHY_1M;
  3953. cp.rx_phys = HCI_LE_SET_PHY_1M;
  3954. /* Enables 2M PHY if supported */
  3955. if (le_2m_capable(hdev)) {
  3956. cp.tx_phys |= HCI_LE_SET_PHY_2M;
  3957. cp.rx_phys |= HCI_LE_SET_PHY_2M;
  3958. }
  3959. /* Enables Coded PHY if supported */
  3960. if (le_coded_capable(hdev)) {
  3961. cp.tx_phys |= HCI_LE_SET_PHY_CODED;
  3962. cp.rx_phys |= HCI_LE_SET_PHY_CODED;
  3963. }
  3964. return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_DEFAULT_PHY,
  3965. sizeof(cp), &cp, HCI_CMD_TIMEOUT);
  3966. }
  3967. static const struct hci_init_stage le_init4[] = {
  3968. /* HCI_OP_LE_WRITE_DEF_DATA_LEN */
  3969. HCI_INIT(hci_le_set_write_def_data_len_sync),
  3970. /* HCI_OP_LE_SET_DEFAULT_PHY */
  3971. HCI_INIT(hci_le_set_default_phy_sync),
  3972. {}
  3973. };
  3974. static int hci_init4_sync(struct hci_dev *hdev)
  3975. {
  3976. int err;
  3977. bt_dev_dbg(hdev, "");
  3978. err = hci_init_stage_sync(hdev, hci_init4);
  3979. if (err)
  3980. return err;
  3981. if (lmp_le_capable(hdev))
  3982. return hci_init_stage_sync(hdev, le_init4);
  3983. return 0;
  3984. }
  3985. static int hci_init_sync(struct hci_dev *hdev)
  3986. {
  3987. int err;
  3988. err = hci_init1_sync(hdev);
  3989. if (err < 0)
  3990. return err;
  3991. if (hci_dev_test_flag(hdev, HCI_SETUP))
  3992. hci_debugfs_create_basic(hdev);
  3993. err = hci_init2_sync(hdev);
  3994. if (err < 0)
  3995. return err;
  3996. err = hci_init3_sync(hdev);
  3997. if (err < 0)
  3998. return err;
  3999. err = hci_init4_sync(hdev);
  4000. if (err < 0)
  4001. return err;
  4002. /* This function is only called when the controller is actually in
  4003. * configured state. When the controller is marked as unconfigured,
  4004. * this initialization procedure is not run.
  4005. *
  4006. * It means that it is possible that a controller runs through its
  4007. * setup phase and then discovers missing settings. If that is the
  4008. * case, then this function will not be called. It then will only
  4009. * be called during the config phase.
  4010. *
  4011. * So only when in setup phase or config phase, create the debugfs
  4012. * entries and register the SMP channels.
  4013. */
  4014. if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
  4015. !hci_dev_test_flag(hdev, HCI_CONFIG))
  4016. return 0;
  4017. if (hci_dev_test_and_set_flag(hdev, HCI_DEBUGFS_CREATED))
  4018. return 0;
  4019. hci_debugfs_create_common(hdev);
  4020. if (lmp_bredr_capable(hdev))
  4021. hci_debugfs_create_bredr(hdev);
  4022. if (lmp_le_capable(hdev))
  4023. hci_debugfs_create_le(hdev);
  4024. return 0;
  4025. }
  4026. #define HCI_QUIRK_BROKEN(_quirk, _desc) { HCI_QUIRK_BROKEN_##_quirk, _desc }
  4027. static const struct {
  4028. unsigned long quirk;
  4029. const char *desc;
  4030. } hci_broken_table[] = {
  4031. HCI_QUIRK_BROKEN(LOCAL_COMMANDS,
  4032. "HCI Read Local Supported Commands not supported"),
  4033. HCI_QUIRK_BROKEN(STORED_LINK_KEY,
  4034. "HCI Delete Stored Link Key command is advertised, "
  4035. "but not supported."),
  4036. HCI_QUIRK_BROKEN(ERR_DATA_REPORTING,
  4037. "HCI Read Default Erroneous Data Reporting command is "
  4038. "advertised, but not supported."),
  4039. HCI_QUIRK_BROKEN(READ_TRANSMIT_POWER,
  4040. "HCI Read Transmit Power Level command is advertised, "
  4041. "but not supported."),
  4042. HCI_QUIRK_BROKEN(FILTER_CLEAR_ALL,
  4043. "HCI Set Event Filter command not supported."),
  4044. HCI_QUIRK_BROKEN(ENHANCED_SETUP_SYNC_CONN,
  4045. "HCI Enhanced Setup Synchronous Connection command is "
  4046. "advertised, but not supported."),
  4047. HCI_QUIRK_BROKEN(SET_RPA_TIMEOUT,
  4048. "HCI LE Set Random Private Address Timeout command is "
  4049. "advertised, but not supported."),
  4050. HCI_QUIRK_BROKEN(EXT_CREATE_CONN,
  4051. "HCI LE Extended Create Connection command is "
  4052. "advertised, but not supported."),
  4053. HCI_QUIRK_BROKEN(WRITE_AUTH_PAYLOAD_TIMEOUT,
  4054. "HCI WRITE AUTH PAYLOAD TIMEOUT command leads "
  4055. "to unexpected SMP errors when pairing "
  4056. "and will not be used."),
  4057. HCI_QUIRK_BROKEN(LE_CODED,
  4058. "HCI LE Coded PHY feature bit is set, "
  4059. "but its usage is not supported.")
  4060. };
  4061. /* This function handles hdev setup stage:
  4062. *
  4063. * Calls hdev->setup
  4064. * Setup address if HCI_QUIRK_USE_BDADDR_PROPERTY is set.
  4065. */
  4066. static int hci_dev_setup_sync(struct hci_dev *hdev)
  4067. {
  4068. int ret = 0;
  4069. bool invalid_bdaddr;
  4070. size_t i;
  4071. if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
  4072. !hci_test_quirk(hdev, HCI_QUIRK_NON_PERSISTENT_SETUP))
  4073. return 0;
  4074. bt_dev_dbg(hdev, "");
  4075. hci_sock_dev_event(hdev, HCI_DEV_SETUP);
  4076. if (hdev->setup)
  4077. ret = hdev->setup(hdev);
  4078. for (i = 0; i < ARRAY_SIZE(hci_broken_table); i++) {
  4079. if (hci_test_quirk(hdev, hci_broken_table[i].quirk))
  4080. bt_dev_warn(hdev, "%s", hci_broken_table[i].desc);
  4081. }
  4082. /* The transport driver can set the quirk to mark the
  4083. * BD_ADDR invalid before creating the HCI device or in
  4084. * its setup callback.
  4085. */
  4086. invalid_bdaddr = hci_test_quirk(hdev, HCI_QUIRK_INVALID_BDADDR) ||
  4087. hci_test_quirk(hdev, HCI_QUIRK_USE_BDADDR_PROPERTY);
  4088. if (!ret) {
  4089. if (hci_test_quirk(hdev, HCI_QUIRK_USE_BDADDR_PROPERTY) &&
  4090. !bacmp(&hdev->public_addr, BDADDR_ANY))
  4091. hci_dev_get_bd_addr_from_property(hdev);
  4092. if (invalid_bdaddr && bacmp(&hdev->public_addr, BDADDR_ANY) &&
  4093. hdev->set_bdaddr) {
  4094. ret = hdev->set_bdaddr(hdev, &hdev->public_addr);
  4095. if (!ret)
  4096. invalid_bdaddr = false;
  4097. }
  4098. }
  4099. /* The transport driver can set these quirks before
  4100. * creating the HCI device or in its setup callback.
  4101. *
  4102. * For the invalid BD_ADDR quirk it is possible that
  4103. * it becomes a valid address if the bootloader does
  4104. * provide it (see above).
  4105. *
  4106. * In case any of them is set, the controller has to
  4107. * start up as unconfigured.
  4108. */
  4109. if (hci_test_quirk(hdev, HCI_QUIRK_EXTERNAL_CONFIG) ||
  4110. invalid_bdaddr)
  4111. hci_dev_set_flag(hdev, HCI_UNCONFIGURED);
  4112. /* For an unconfigured controller it is required to
  4113. * read at least the version information provided by
  4114. * the Read Local Version Information command.
  4115. *
  4116. * If the set_bdaddr driver callback is provided, then
  4117. * also the original Bluetooth public device address
  4118. * will be read using the Read BD Address command.
  4119. */
  4120. if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
  4121. return hci_unconf_init_sync(hdev);
  4122. return ret;
  4123. }
  4124. /* This function handles hdev init stage:
  4125. *
  4126. * Calls hci_dev_setup_sync to perform setup stage
  4127. * Calls hci_init_sync to perform HCI command init sequence
  4128. */
  4129. static int hci_dev_init_sync(struct hci_dev *hdev)
  4130. {
  4131. int ret;
  4132. bt_dev_dbg(hdev, "");
  4133. atomic_set(&hdev->cmd_cnt, 1);
  4134. set_bit(HCI_INIT, &hdev->flags);
  4135. ret = hci_dev_setup_sync(hdev);
  4136. if (hci_dev_test_flag(hdev, HCI_CONFIG)) {
  4137. /* If public address change is configured, ensure that
  4138. * the address gets programmed. If the driver does not
  4139. * support changing the public address, fail the power
  4140. * on procedure.
  4141. */
  4142. if (bacmp(&hdev->public_addr, BDADDR_ANY) &&
  4143. hdev->set_bdaddr)
  4144. ret = hdev->set_bdaddr(hdev, &hdev->public_addr);
  4145. else
  4146. ret = -EADDRNOTAVAIL;
  4147. }
  4148. if (!ret) {
  4149. if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
  4150. !hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
  4151. ret = hci_init_sync(hdev);
  4152. if (!ret && hdev->post_init)
  4153. ret = hdev->post_init(hdev);
  4154. }
  4155. }
  4156. /* If the HCI Reset command is clearing all diagnostic settings,
  4157. * then they need to be reprogrammed after the init procedure
  4158. * completed.
  4159. */
  4160. if (hci_test_quirk(hdev, HCI_QUIRK_NON_PERSISTENT_DIAG) &&
  4161. !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
  4162. hci_dev_test_flag(hdev, HCI_VENDOR_DIAG) && hdev->set_diag)
  4163. ret = hdev->set_diag(hdev, true);
  4164. if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
  4165. msft_do_open(hdev);
  4166. aosp_do_open(hdev);
  4167. }
  4168. clear_bit(HCI_INIT, &hdev->flags);
  4169. return ret;
  4170. }
  4171. int hci_dev_open_sync(struct hci_dev *hdev)
  4172. {
  4173. int ret;
  4174. bt_dev_dbg(hdev, "");
  4175. if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) {
  4176. ret = -ENODEV;
  4177. goto done;
  4178. }
  4179. if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
  4180. !hci_dev_test_flag(hdev, HCI_CONFIG)) {
  4181. /* Check for rfkill but allow the HCI setup stage to
  4182. * proceed (which in itself doesn't cause any RF activity).
  4183. */
  4184. if (hci_dev_test_flag(hdev, HCI_RFKILLED)) {
  4185. ret = -ERFKILL;
  4186. goto done;
  4187. }
  4188. /* Check for valid public address or a configured static
  4189. * random address, but let the HCI setup proceed to
  4190. * be able to determine if there is a public address
  4191. * or not.
  4192. *
  4193. * In case of user channel usage, it is not important
  4194. * if a public address or static random address is
  4195. * available.
  4196. */
  4197. if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
  4198. !bacmp(&hdev->bdaddr, BDADDR_ANY) &&
  4199. !bacmp(&hdev->static_addr, BDADDR_ANY)) {
  4200. ret = -EADDRNOTAVAIL;
  4201. goto done;
  4202. }
  4203. }
  4204. if (test_bit(HCI_UP, &hdev->flags)) {
  4205. ret = -EALREADY;
  4206. goto done;
  4207. }
  4208. if (hdev->open(hdev)) {
  4209. ret = -EIO;
  4210. goto done;
  4211. }
  4212. hci_devcd_reset(hdev);
  4213. set_bit(HCI_RUNNING, &hdev->flags);
  4214. hci_sock_dev_event(hdev, HCI_DEV_OPEN);
  4215. ret = hci_dev_init_sync(hdev);
  4216. if (!ret) {
  4217. hci_dev_hold(hdev);
  4218. hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
  4219. hci_adv_instances_set_rpa_expired(hdev, true);
  4220. set_bit(HCI_UP, &hdev->flags);
  4221. hci_sock_dev_event(hdev, HCI_DEV_UP);
  4222. hci_leds_update_powered(hdev, true);
  4223. if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
  4224. !hci_dev_test_flag(hdev, HCI_CONFIG) &&
  4225. !hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
  4226. !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
  4227. hci_dev_test_flag(hdev, HCI_MGMT)) {
  4228. ret = hci_powered_update_sync(hdev);
  4229. mgmt_power_on(hdev, ret);
  4230. }
  4231. } else {
  4232. /* Init failed, cleanup */
  4233. flush_work(&hdev->tx_work);
  4234. /* Since hci_rx_work() is possible to awake new cmd_work
  4235. * it should be flushed first to avoid unexpected call of
  4236. * hci_cmd_work()
  4237. */
  4238. flush_work(&hdev->rx_work);
  4239. flush_work(&hdev->cmd_work);
  4240. skb_queue_purge(&hdev->cmd_q);
  4241. skb_queue_purge(&hdev->rx_q);
  4242. if (hdev->flush)
  4243. hdev->flush(hdev);
  4244. if (hdev->sent_cmd) {
  4245. cancel_delayed_work_sync(&hdev->cmd_timer);
  4246. kfree_skb(hdev->sent_cmd);
  4247. hdev->sent_cmd = NULL;
  4248. }
  4249. if (hdev->req_skb) {
  4250. kfree_skb(hdev->req_skb);
  4251. hdev->req_skb = NULL;
  4252. }
  4253. clear_bit(HCI_RUNNING, &hdev->flags);
  4254. hci_sock_dev_event(hdev, HCI_DEV_CLOSE);
  4255. hdev->close(hdev);
  4256. hdev->flags &= BIT(HCI_RAW);
  4257. }
  4258. done:
  4259. return ret;
  4260. }
  4261. /* This function requires the caller holds hdev->lock */
  4262. static void hci_pend_le_actions_clear(struct hci_dev *hdev)
  4263. {
  4264. struct hci_conn_params *p;
  4265. list_for_each_entry(p, &hdev->le_conn_params, list) {
  4266. hci_pend_le_list_del_init(p);
  4267. if (p->conn) {
  4268. hci_conn_drop(p->conn);
  4269. hci_conn_put(p->conn);
  4270. p->conn = NULL;
  4271. }
  4272. }
  4273. BT_DBG("All LE pending actions cleared");
  4274. }
  4275. static int hci_dev_shutdown(struct hci_dev *hdev)
  4276. {
  4277. int err = 0;
  4278. /* Similar to how we first do setup and then set the exclusive access
  4279. * bit for userspace, we must first unset userchannel and then clean up.
  4280. * Otherwise, the kernel can't properly use the hci channel to clean up
  4281. * the controller (some shutdown routines require sending additional
  4282. * commands to the controller for example).
  4283. */
  4284. bool was_userchannel =
  4285. hci_dev_test_and_clear_flag(hdev, HCI_USER_CHANNEL);
  4286. if (!hci_dev_test_flag(hdev, HCI_UNREGISTER) &&
  4287. test_bit(HCI_UP, &hdev->flags)) {
  4288. /* Execute vendor specific shutdown routine */
  4289. if (hdev->shutdown)
  4290. err = hdev->shutdown(hdev);
  4291. }
  4292. if (was_userchannel)
  4293. hci_dev_set_flag(hdev, HCI_USER_CHANNEL);
  4294. return err;
  4295. }
  4296. int hci_dev_close_sync(struct hci_dev *hdev)
  4297. {
  4298. bool auto_off;
  4299. int err = 0;
  4300. bt_dev_dbg(hdev, "");
  4301. if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) {
  4302. disable_delayed_work(&hdev->power_off);
  4303. disable_delayed_work(&hdev->ncmd_timer);
  4304. disable_delayed_work(&hdev->le_scan_disable);
  4305. } else {
  4306. cancel_delayed_work(&hdev->power_off);
  4307. cancel_delayed_work(&hdev->ncmd_timer);
  4308. cancel_delayed_work(&hdev->le_scan_disable);
  4309. }
  4310. hci_cmd_sync_cancel_sync(hdev, ENODEV);
  4311. cancel_interleave_scan(hdev);
  4312. if (hdev->adv_instance_timeout) {
  4313. cancel_delayed_work_sync(&hdev->adv_instance_expire);
  4314. hdev->adv_instance_timeout = 0;
  4315. }
  4316. err = hci_dev_shutdown(hdev);
  4317. if (!test_and_clear_bit(HCI_UP, &hdev->flags)) {
  4318. cancel_delayed_work_sync(&hdev->cmd_timer);
  4319. return err;
  4320. }
  4321. hci_leds_update_powered(hdev, false);
  4322. /* Flush RX and TX works */
  4323. flush_work(&hdev->tx_work);
  4324. flush_work(&hdev->rx_work);
  4325. if (hdev->discov_timeout > 0) {
  4326. hdev->discov_timeout = 0;
  4327. hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
  4328. hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
  4329. }
  4330. if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE))
  4331. cancel_delayed_work(&hdev->service_cache);
  4332. if (hci_dev_test_flag(hdev, HCI_MGMT)) {
  4333. struct adv_info *adv_instance;
  4334. cancel_delayed_work_sync(&hdev->rpa_expired);
  4335. list_for_each_entry(adv_instance, &hdev->adv_instances, list)
  4336. cancel_delayed_work_sync(&adv_instance->rpa_expired_cb);
  4337. }
  4338. /* Avoid potential lockdep warnings from the *_flush() calls by
  4339. * ensuring the workqueue is empty up front.
  4340. */
  4341. drain_workqueue(hdev->workqueue);
  4342. hci_dev_lock(hdev);
  4343. hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
  4344. auto_off = hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF);
  4345. if (!auto_off && !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
  4346. hci_dev_test_flag(hdev, HCI_MGMT))
  4347. __mgmt_power_off(hdev);
  4348. hci_inquiry_cache_flush(hdev);
  4349. hci_pend_le_actions_clear(hdev);
  4350. hci_conn_hash_flush(hdev);
  4351. /* Prevent data races on hdev->smp_data or hdev->smp_bredr_data */
  4352. smp_unregister(hdev);
  4353. hci_dev_unlock(hdev);
  4354. hci_sock_dev_event(hdev, HCI_DEV_DOWN);
  4355. if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
  4356. aosp_do_close(hdev);
  4357. msft_do_close(hdev);
  4358. }
  4359. if (hdev->flush)
  4360. hdev->flush(hdev);
  4361. /* Reset device */
  4362. skb_queue_purge(&hdev->cmd_q);
  4363. atomic_set(&hdev->cmd_cnt, 1);
  4364. if (hci_test_quirk(hdev, HCI_QUIRK_RESET_ON_CLOSE) &&
  4365. !auto_off && !hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
  4366. set_bit(HCI_INIT, &hdev->flags);
  4367. hci_reset_sync(hdev);
  4368. clear_bit(HCI_INIT, &hdev->flags);
  4369. }
  4370. /* flush cmd work */
  4371. flush_work(&hdev->cmd_work);
  4372. /* Drop queues */
  4373. skb_queue_purge(&hdev->rx_q);
  4374. skb_queue_purge(&hdev->cmd_q);
  4375. skb_queue_purge(&hdev->raw_q);
  4376. /* Drop last sent command */
  4377. if (hdev->sent_cmd) {
  4378. cancel_delayed_work_sync(&hdev->cmd_timer);
  4379. kfree_skb(hdev->sent_cmd);
  4380. hdev->sent_cmd = NULL;
  4381. }
  4382. /* Drop last request */
  4383. if (hdev->req_skb) {
  4384. kfree_skb(hdev->req_skb);
  4385. hdev->req_skb = NULL;
  4386. }
  4387. clear_bit(HCI_RUNNING, &hdev->flags);
  4388. hci_sock_dev_event(hdev, HCI_DEV_CLOSE);
  4389. /* After this point our queues are empty and no tasks are scheduled. */
  4390. hdev->close(hdev);
  4391. /* Clear flags */
  4392. hdev->flags &= BIT(HCI_RAW);
  4393. hci_dev_clear_volatile_flags(hdev);
  4394. memset(hdev->eir, 0, sizeof(hdev->eir));
  4395. memset(hdev->dev_class, 0, sizeof(hdev->dev_class));
  4396. bacpy(&hdev->random_addr, BDADDR_ANY);
  4397. hci_codec_list_clear(&hdev->local_codecs);
  4398. hci_dev_put(hdev);
  4399. return err;
  4400. }
  4401. /* This function perform power on HCI command sequence as follows:
  4402. *
  4403. * If controller is already up (HCI_UP) performs hci_powered_update_sync
  4404. * sequence otherwise run hci_dev_open_sync which will follow with
  4405. * hci_powered_update_sync after the init sequence is completed.
  4406. */
  4407. static int hci_power_on_sync(struct hci_dev *hdev)
  4408. {
  4409. int err;
  4410. if (test_bit(HCI_UP, &hdev->flags) &&
  4411. hci_dev_test_flag(hdev, HCI_MGMT) &&
  4412. hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF)) {
  4413. cancel_delayed_work(&hdev->power_off);
  4414. return hci_powered_update_sync(hdev);
  4415. }
  4416. err = hci_dev_open_sync(hdev);
  4417. if (err < 0)
  4418. return err;
  4419. /* During the HCI setup phase, a few error conditions are
  4420. * ignored and they need to be checked now. If they are still
  4421. * valid, it is important to return the device back off.
  4422. */
  4423. if (hci_dev_test_flag(hdev, HCI_RFKILLED) ||
  4424. hci_dev_test_flag(hdev, HCI_UNCONFIGURED) ||
  4425. (!bacmp(&hdev->bdaddr, BDADDR_ANY) &&
  4426. !bacmp(&hdev->static_addr, BDADDR_ANY))) {
  4427. hci_dev_clear_flag(hdev, HCI_AUTO_OFF);
  4428. hci_dev_close_sync(hdev);
  4429. } else if (hci_dev_test_flag(hdev, HCI_AUTO_OFF)) {
  4430. queue_delayed_work(hdev->req_workqueue, &hdev->power_off,
  4431. HCI_AUTO_OFF_TIMEOUT);
  4432. }
  4433. if (hci_dev_test_and_clear_flag(hdev, HCI_SETUP)) {
  4434. /* For unconfigured devices, set the HCI_RAW flag
  4435. * so that userspace can easily identify them.
  4436. */
  4437. if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
  4438. set_bit(HCI_RAW, &hdev->flags);
  4439. /* For fully configured devices, this will send
  4440. * the Index Added event. For unconfigured devices,
  4441. * it will send Unconfigued Index Added event.
  4442. *
  4443. * Devices with HCI_QUIRK_RAW_DEVICE are ignored
  4444. * and no event will be send.
  4445. */
  4446. mgmt_index_added(hdev);
  4447. } else if (hci_dev_test_and_clear_flag(hdev, HCI_CONFIG)) {
  4448. /* When the controller is now configured, then it
  4449. * is important to clear the HCI_RAW flag.
  4450. */
  4451. if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
  4452. clear_bit(HCI_RAW, &hdev->flags);
  4453. /* Powering on the controller with HCI_CONFIG set only
  4454. * happens with the transition from unconfigured to
  4455. * configured. This will send the Index Added event.
  4456. */
  4457. mgmt_index_added(hdev);
  4458. }
  4459. return 0;
  4460. }
  4461. static int hci_remote_name_cancel_sync(struct hci_dev *hdev, bdaddr_t *addr)
  4462. {
  4463. struct hci_cp_remote_name_req_cancel cp;
  4464. memset(&cp, 0, sizeof(cp));
  4465. bacpy(&cp.bdaddr, addr);
  4466. return __hci_cmd_sync_status(hdev, HCI_OP_REMOTE_NAME_REQ_CANCEL,
  4467. sizeof(cp), &cp, HCI_CMD_TIMEOUT);
  4468. }
  4469. int hci_stop_discovery_sync(struct hci_dev *hdev)
  4470. {
  4471. struct discovery_state *d = &hdev->discovery;
  4472. struct inquiry_entry *e;
  4473. int err;
  4474. bt_dev_dbg(hdev, "state %u", hdev->discovery.state);
  4475. if (d->state == DISCOVERY_FINDING || d->state == DISCOVERY_STOPPING) {
  4476. if (test_bit(HCI_INQUIRY, &hdev->flags)) {
  4477. err = __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY_CANCEL,
  4478. 0, NULL, HCI_CMD_TIMEOUT);
  4479. if (err)
  4480. return err;
  4481. }
  4482. if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
  4483. cancel_delayed_work(&hdev->le_scan_disable);
  4484. err = hci_scan_disable_sync(hdev);
  4485. if (err)
  4486. return err;
  4487. }
  4488. } else {
  4489. err = hci_scan_disable_sync(hdev);
  4490. if (err)
  4491. return err;
  4492. }
  4493. /* Resume advertising if it was paused */
  4494. if (ll_privacy_capable(hdev))
  4495. hci_resume_advertising_sync(hdev);
  4496. /* No further actions needed for LE-only discovery */
  4497. if (d->type == DISCOV_TYPE_LE)
  4498. return 0;
  4499. if (d->state == DISCOVERY_RESOLVING || d->state == DISCOVERY_STOPPING) {
  4500. e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY,
  4501. NAME_PENDING);
  4502. if (!e)
  4503. return 0;
  4504. /* Ignore cancel errors since it should interfere with stopping
  4505. * of the discovery.
  4506. */
  4507. hci_remote_name_cancel_sync(hdev, &e->data.bdaddr);
  4508. }
  4509. return 0;
  4510. }
  4511. static int hci_disconnect_sync(struct hci_dev *hdev, struct hci_conn *conn,
  4512. u8 reason)
  4513. {
  4514. struct hci_cp_disconnect cp;
  4515. if (conn->type == BIS_LINK || conn->type == PA_LINK) {
  4516. /* This is a BIS connection, hci_conn_del will
  4517. * do the necessary cleanup.
  4518. */
  4519. hci_dev_lock(hdev);
  4520. hci_conn_failed(conn, reason);
  4521. hci_dev_unlock(hdev);
  4522. return 0;
  4523. }
  4524. memset(&cp, 0, sizeof(cp));
  4525. cp.handle = cpu_to_le16(conn->handle);
  4526. cp.reason = reason;
  4527. /* Wait for HCI_EV_DISCONN_COMPLETE, not HCI_EV_CMD_STATUS, when the
  4528. * reason is anything but HCI_ERROR_REMOTE_POWER_OFF. This reason is
  4529. * used when suspending or powering off, where we don't want to wait
  4530. * for the peer's response.
  4531. */
  4532. if (reason != HCI_ERROR_REMOTE_POWER_OFF)
  4533. return __hci_cmd_sync_status_sk(hdev, HCI_OP_DISCONNECT,
  4534. sizeof(cp), &cp,
  4535. HCI_EV_DISCONN_COMPLETE,
  4536. HCI_CMD_TIMEOUT, NULL);
  4537. return __hci_cmd_sync_status(hdev, HCI_OP_DISCONNECT, sizeof(cp), &cp,
  4538. HCI_CMD_TIMEOUT);
  4539. }
  4540. static int hci_le_connect_cancel_sync(struct hci_dev *hdev,
  4541. struct hci_conn *conn, u8 reason)
  4542. {
  4543. /* Return reason if scanning since the connection shall probably be
  4544. * cleanup directly.
  4545. */
  4546. if (test_bit(HCI_CONN_SCANNING, &conn->flags))
  4547. return reason;
  4548. if (conn->role == HCI_ROLE_SLAVE ||
  4549. test_and_set_bit(HCI_CONN_CANCEL, &conn->flags))
  4550. return 0;
  4551. return __hci_cmd_sync_status(hdev, HCI_OP_LE_CREATE_CONN_CANCEL,
  4552. 0, NULL, HCI_CMD_TIMEOUT);
  4553. }
  4554. static int hci_connect_cancel_sync(struct hci_dev *hdev, struct hci_conn *conn,
  4555. u8 reason)
  4556. {
  4557. if (conn->type == LE_LINK)
  4558. return hci_le_connect_cancel_sync(hdev, conn, reason);
  4559. if (conn->type == CIS_LINK) {
  4560. /* BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E
  4561. * page 1857:
  4562. *
  4563. * If this command is issued for a CIS on the Central and the
  4564. * CIS is successfully terminated before being established,
  4565. * then an HCI_LE_CIS_Established event shall also be sent for
  4566. * this CIS with the Status Operation Cancelled by Host (0x44).
  4567. */
  4568. if (test_bit(HCI_CONN_CREATE_CIS, &conn->flags))
  4569. return hci_disconnect_sync(hdev, conn, reason);
  4570. /* CIS with no Create CIS sent have nothing to cancel */
  4571. return HCI_ERROR_LOCAL_HOST_TERM;
  4572. }
  4573. if (conn->type == BIS_LINK || conn->type == PA_LINK) {
  4574. /* There is no way to cancel a BIS without terminating the BIG
  4575. * which is done later on connection cleanup.
  4576. */
  4577. return 0;
  4578. }
  4579. if (hdev->hci_ver < BLUETOOTH_VER_1_2)
  4580. return 0;
  4581. /* Wait for HCI_EV_CONN_COMPLETE, not HCI_EV_CMD_STATUS, when the
  4582. * reason is anything but HCI_ERROR_REMOTE_POWER_OFF. This reason is
  4583. * used when suspending or powering off, where we don't want to wait
  4584. * for the peer's response.
  4585. */
  4586. if (reason != HCI_ERROR_REMOTE_POWER_OFF)
  4587. return __hci_cmd_sync_status_sk(hdev, HCI_OP_CREATE_CONN_CANCEL,
  4588. 6, &conn->dst,
  4589. HCI_EV_CONN_COMPLETE,
  4590. HCI_CMD_TIMEOUT, NULL);
  4591. return __hci_cmd_sync_status(hdev, HCI_OP_CREATE_CONN_CANCEL,
  4592. 6, &conn->dst, HCI_CMD_TIMEOUT);
  4593. }
  4594. static int hci_reject_sco_sync(struct hci_dev *hdev, struct hci_conn *conn,
  4595. u8 reason)
  4596. {
  4597. struct hci_cp_reject_sync_conn_req cp;
  4598. memset(&cp, 0, sizeof(cp));
  4599. bacpy(&cp.bdaddr, &conn->dst);
  4600. cp.reason = reason;
  4601. /* SCO rejection has its own limited set of
  4602. * allowed error values (0x0D-0x0F).
  4603. */
  4604. if (reason < 0x0d || reason > 0x0f)
  4605. cp.reason = HCI_ERROR_REJ_LIMITED_RESOURCES;
  4606. return __hci_cmd_sync_status(hdev, HCI_OP_REJECT_SYNC_CONN_REQ,
  4607. sizeof(cp), &cp, HCI_CMD_TIMEOUT);
  4608. }
  4609. static int hci_le_reject_cis_sync(struct hci_dev *hdev, struct hci_conn *conn,
  4610. u8 reason)
  4611. {
  4612. struct hci_cp_le_reject_cis cp;
  4613. memset(&cp, 0, sizeof(cp));
  4614. cp.handle = cpu_to_le16(conn->handle);
  4615. cp.reason = reason;
  4616. return __hci_cmd_sync_status(hdev, HCI_OP_LE_REJECT_CIS,
  4617. sizeof(cp), &cp, HCI_CMD_TIMEOUT);
  4618. }
  4619. static int hci_reject_conn_sync(struct hci_dev *hdev, struct hci_conn *conn,
  4620. u8 reason)
  4621. {
  4622. struct hci_cp_reject_conn_req cp;
  4623. if (conn->type == CIS_LINK)
  4624. return hci_le_reject_cis_sync(hdev, conn, reason);
  4625. if (conn->type == BIS_LINK || conn->type == PA_LINK)
  4626. return -EINVAL;
  4627. if (conn->type == SCO_LINK || conn->type == ESCO_LINK)
  4628. return hci_reject_sco_sync(hdev, conn, reason);
  4629. memset(&cp, 0, sizeof(cp));
  4630. bacpy(&cp.bdaddr, &conn->dst);
  4631. cp.reason = reason;
  4632. return __hci_cmd_sync_status(hdev, HCI_OP_REJECT_CONN_REQ,
  4633. sizeof(cp), &cp, HCI_CMD_TIMEOUT);
  4634. }
  4635. int hci_abort_conn_sync(struct hci_dev *hdev, struct hci_conn *conn, u8 reason)
  4636. {
  4637. int err = 0;
  4638. u16 handle = conn->handle;
  4639. bool disconnect = false;
  4640. struct hci_conn *c;
  4641. switch (conn->state) {
  4642. case BT_CONNECTED:
  4643. case BT_CONFIG:
  4644. err = hci_disconnect_sync(hdev, conn, reason);
  4645. break;
  4646. case BT_CONNECT:
  4647. err = hci_connect_cancel_sync(hdev, conn, reason);
  4648. break;
  4649. case BT_CONNECT2:
  4650. err = hci_reject_conn_sync(hdev, conn, reason);
  4651. break;
  4652. case BT_OPEN:
  4653. case BT_BOUND:
  4654. break;
  4655. default:
  4656. disconnect = true;
  4657. break;
  4658. }
  4659. hci_dev_lock(hdev);
  4660. /* Check if the connection has been cleaned up concurrently */
  4661. c = hci_conn_hash_lookup_handle(hdev, handle);
  4662. if (!c || c != conn) {
  4663. err = 0;
  4664. goto unlock;
  4665. }
  4666. /* Cleanup hci_conn object if it cannot be cancelled as it
  4667. * likely means the controller and host stack are out of sync
  4668. * or in case of LE it was still scanning so it can be cleanup
  4669. * safely.
  4670. */
  4671. if (disconnect) {
  4672. conn->state = BT_CLOSED;
  4673. hci_disconn_cfm(conn, reason);
  4674. hci_conn_del(conn);
  4675. } else {
  4676. hci_conn_failed(conn, reason);
  4677. }
  4678. unlock:
  4679. hci_dev_unlock(hdev);
  4680. return err;
  4681. }
  4682. static int hci_disconnect_all_sync(struct hci_dev *hdev, u8 reason)
  4683. {
  4684. struct list_head *head = &hdev->conn_hash.list;
  4685. struct hci_conn *conn;
  4686. rcu_read_lock();
  4687. while ((conn = list_first_or_null_rcu(head, struct hci_conn, list))) {
  4688. /* Make sure the connection is not freed while unlocking */
  4689. conn = hci_conn_get(conn);
  4690. rcu_read_unlock();
  4691. /* Disregard possible errors since hci_conn_del shall have been
  4692. * called even in case of errors had occurred since it would
  4693. * then cause hci_conn_failed to be called which calls
  4694. * hci_conn_del internally.
  4695. */
  4696. hci_abort_conn_sync(hdev, conn, reason);
  4697. hci_conn_put(conn);
  4698. rcu_read_lock();
  4699. }
  4700. rcu_read_unlock();
  4701. return 0;
  4702. }
  4703. /* This function perform power off HCI command sequence as follows:
  4704. *
  4705. * Clear Advertising
  4706. * Stop Discovery
  4707. * Disconnect all connections
  4708. * hci_dev_close_sync
  4709. */
  4710. static int hci_power_off_sync(struct hci_dev *hdev)
  4711. {
  4712. int err;
  4713. /* If controller is already down there is nothing to do */
  4714. if (!test_bit(HCI_UP, &hdev->flags))
  4715. return 0;
  4716. hci_dev_set_flag(hdev, HCI_POWERING_DOWN);
  4717. if (test_bit(HCI_ISCAN, &hdev->flags) ||
  4718. test_bit(HCI_PSCAN, &hdev->flags)) {
  4719. err = hci_write_scan_enable_sync(hdev, 0x00);
  4720. if (err)
  4721. goto out;
  4722. }
  4723. err = hci_clear_adv_sync(hdev, NULL, false);
  4724. if (err)
  4725. goto out;
  4726. err = hci_stop_discovery_sync(hdev);
  4727. if (err)
  4728. goto out;
  4729. /* Terminated due to Power Off */
  4730. err = hci_disconnect_all_sync(hdev, HCI_ERROR_REMOTE_POWER_OFF);
  4731. if (err)
  4732. goto out;
  4733. err = hci_dev_close_sync(hdev);
  4734. out:
  4735. hci_dev_clear_flag(hdev, HCI_POWERING_DOWN);
  4736. return err;
  4737. }
  4738. int hci_set_powered_sync(struct hci_dev *hdev, u8 val)
  4739. {
  4740. if (val)
  4741. return hci_power_on_sync(hdev);
  4742. return hci_power_off_sync(hdev);
  4743. }
  4744. static int hci_write_iac_sync(struct hci_dev *hdev)
  4745. {
  4746. struct hci_cp_write_current_iac_lap cp;
  4747. if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
  4748. return 0;
  4749. memset(&cp, 0, sizeof(cp));
  4750. if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) {
  4751. /* Limited discoverable mode */
  4752. cp.num_iac = min_t(u8, hdev->num_iac, 2);
  4753. cp.iac_lap[0] = 0x00; /* LIAC */
  4754. cp.iac_lap[1] = 0x8b;
  4755. cp.iac_lap[2] = 0x9e;
  4756. cp.iac_lap[3] = 0x33; /* GIAC */
  4757. cp.iac_lap[4] = 0x8b;
  4758. cp.iac_lap[5] = 0x9e;
  4759. } else {
  4760. /* General discoverable mode */
  4761. cp.num_iac = 1;
  4762. cp.iac_lap[0] = 0x33; /* GIAC */
  4763. cp.iac_lap[1] = 0x8b;
  4764. cp.iac_lap[2] = 0x9e;
  4765. }
  4766. return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CURRENT_IAC_LAP,
  4767. (cp.num_iac * 3) + 1, &cp,
  4768. HCI_CMD_TIMEOUT);
  4769. }
  4770. int hci_update_discoverable_sync(struct hci_dev *hdev)
  4771. {
  4772. int err = 0;
  4773. if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
  4774. err = hci_write_iac_sync(hdev);
  4775. if (err)
  4776. return err;
  4777. err = hci_update_scan_sync(hdev);
  4778. if (err)
  4779. return err;
  4780. err = hci_update_class_sync(hdev);
  4781. if (err)
  4782. return err;
  4783. }
  4784. /* Advertising instances don't use the global discoverable setting, so
  4785. * only update AD if advertising was enabled using Set Advertising.
  4786. */
  4787. if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
  4788. err = hci_update_adv_data_sync(hdev, 0x00);
  4789. if (err)
  4790. return err;
  4791. /* Discoverable mode affects the local advertising
  4792. * address in limited privacy mode.
  4793. */
  4794. if (hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY)) {
  4795. if (ext_adv_capable(hdev))
  4796. err = hci_start_ext_adv_sync(hdev, 0x00);
  4797. else
  4798. err = hci_enable_advertising_sync(hdev);
  4799. }
  4800. }
  4801. return err;
  4802. }
  4803. static int update_discoverable_sync(struct hci_dev *hdev, void *data)
  4804. {
  4805. return hci_update_discoverable_sync(hdev);
  4806. }
  4807. int hci_update_discoverable(struct hci_dev *hdev)
  4808. {
  4809. /* Only queue if it would have any effect */
  4810. if (hdev_is_powered(hdev) &&
  4811. hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
  4812. hci_dev_test_flag(hdev, HCI_DISCOVERABLE) &&
  4813. hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY))
  4814. return hci_cmd_sync_queue(hdev, update_discoverable_sync, NULL,
  4815. NULL);
  4816. return 0;
  4817. }
  4818. int hci_update_connectable_sync(struct hci_dev *hdev)
  4819. {
  4820. int err;
  4821. err = hci_update_scan_sync(hdev);
  4822. if (err)
  4823. return err;
  4824. /* If BR/EDR is not enabled and we disable advertising as a
  4825. * by-product of disabling connectable, we need to update the
  4826. * advertising flags.
  4827. */
  4828. if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
  4829. err = hci_update_adv_data_sync(hdev, hdev->cur_adv_instance);
  4830. /* Update the advertising parameters if necessary */
  4831. if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
  4832. !list_empty(&hdev->adv_instances)) {
  4833. if (ext_adv_capable(hdev))
  4834. err = hci_start_ext_adv_sync(hdev,
  4835. hdev->cur_adv_instance);
  4836. else
  4837. err = hci_enable_advertising_sync(hdev);
  4838. if (err)
  4839. return err;
  4840. }
  4841. return hci_update_passive_scan_sync(hdev);
  4842. }
  4843. int hci_inquiry_sync(struct hci_dev *hdev, u8 length, u8 num_rsp)
  4844. {
  4845. const u8 giac[3] = { 0x33, 0x8b, 0x9e };
  4846. const u8 liac[3] = { 0x00, 0x8b, 0x9e };
  4847. struct hci_cp_inquiry cp;
  4848. bt_dev_dbg(hdev, "");
  4849. if (test_bit(HCI_INQUIRY, &hdev->flags))
  4850. return 0;
  4851. hci_dev_lock(hdev);
  4852. hci_inquiry_cache_flush(hdev);
  4853. hci_dev_unlock(hdev);
  4854. memset(&cp, 0, sizeof(cp));
  4855. if (hdev->discovery.limited)
  4856. memcpy(&cp.lap, liac, sizeof(cp.lap));
  4857. else
  4858. memcpy(&cp.lap, giac, sizeof(cp.lap));
  4859. cp.length = length;
  4860. cp.num_rsp = num_rsp;
  4861. return __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY,
  4862. sizeof(cp), &cp, HCI_CMD_TIMEOUT);
  4863. }
  4864. static int hci_active_scan_sync(struct hci_dev *hdev, uint16_t interval)
  4865. {
  4866. u8 own_addr_type;
  4867. /* Accept list is not used for discovery */
  4868. u8 filter_policy = 0x00;
  4869. /* Default is to enable duplicates filter */
  4870. u8 filter_dup = LE_SCAN_FILTER_DUP_ENABLE;
  4871. int err;
  4872. bt_dev_dbg(hdev, "");
  4873. /* If controller is scanning, it means the passive scanning is
  4874. * running. Thus, we should temporarily stop it in order to set the
  4875. * discovery scanning parameters.
  4876. */
  4877. err = hci_scan_disable_sync(hdev);
  4878. if (err) {
  4879. bt_dev_err(hdev, "Unable to disable scanning: %d", err);
  4880. return err;
  4881. }
  4882. cancel_interleave_scan(hdev);
  4883. /* Pause address resolution for active scan and stop advertising if
  4884. * privacy is enabled.
  4885. */
  4886. err = hci_pause_addr_resolution(hdev);
  4887. if (err)
  4888. goto failed;
  4889. /* All active scans will be done with either a resolvable private
  4890. * address (when privacy feature has been enabled) or non-resolvable
  4891. * private address.
  4892. */
  4893. err = hci_update_random_address_sync(hdev, true, scan_use_rpa(hdev),
  4894. &own_addr_type);
  4895. if (err < 0)
  4896. own_addr_type = ADDR_LE_DEV_PUBLIC;
  4897. if (hci_is_adv_monitoring(hdev) ||
  4898. (hci_test_quirk(hdev, HCI_QUIRK_STRICT_DUPLICATE_FILTER) &&
  4899. hdev->discovery.result_filtering)) {
  4900. /* Duplicate filter should be disabled when some advertisement
  4901. * monitor is activated, otherwise AdvMon can only receive one
  4902. * advertisement for one peer(*) during active scanning, and
  4903. * might report loss to these peers.
  4904. *
  4905. * If controller does strict duplicate filtering and the
  4906. * discovery requires result filtering disables controller based
  4907. * filtering since that can cause reports that would match the
  4908. * host filter to not be reported.
  4909. */
  4910. filter_dup = LE_SCAN_FILTER_DUP_DISABLE;
  4911. }
  4912. err = hci_start_scan_sync(hdev, LE_SCAN_ACTIVE, interval,
  4913. hdev->le_scan_window_discovery,
  4914. own_addr_type, filter_policy, filter_dup);
  4915. if (!err)
  4916. return err;
  4917. failed:
  4918. /* Resume advertising if it was paused */
  4919. if (ll_privacy_capable(hdev))
  4920. hci_resume_advertising_sync(hdev);
  4921. /* Resume passive scanning */
  4922. hci_update_passive_scan_sync(hdev);
  4923. return err;
  4924. }
  4925. static int hci_start_interleaved_discovery_sync(struct hci_dev *hdev)
  4926. {
  4927. int err;
  4928. bt_dev_dbg(hdev, "");
  4929. err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery * 2);
  4930. if (err)
  4931. return err;
  4932. return hci_inquiry_sync(hdev, DISCOV_BREDR_INQUIRY_LEN, 0);
  4933. }
  4934. int hci_start_discovery_sync(struct hci_dev *hdev)
  4935. {
  4936. unsigned long timeout;
  4937. int err;
  4938. bt_dev_dbg(hdev, "type %u", hdev->discovery.type);
  4939. switch (hdev->discovery.type) {
  4940. case DISCOV_TYPE_BREDR:
  4941. return hci_inquiry_sync(hdev, DISCOV_BREDR_INQUIRY_LEN, 0);
  4942. case DISCOV_TYPE_INTERLEAVED:
  4943. /* When running simultaneous discovery, the LE scanning time
  4944. * should occupy the whole discovery time sine BR/EDR inquiry
  4945. * and LE scanning are scheduled by the controller.
  4946. *
  4947. * For interleaving discovery in comparison, BR/EDR inquiry
  4948. * and LE scanning are done sequentially with separate
  4949. * timeouts.
  4950. */
  4951. if (hci_test_quirk(hdev, HCI_QUIRK_SIMULTANEOUS_DISCOVERY)) {
  4952. timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
  4953. /* During simultaneous discovery, we double LE scan
  4954. * interval. We must leave some time for the controller
  4955. * to do BR/EDR inquiry.
  4956. */
  4957. err = hci_start_interleaved_discovery_sync(hdev);
  4958. break;
  4959. }
  4960. timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout);
  4961. err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery);
  4962. break;
  4963. case DISCOV_TYPE_LE:
  4964. timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
  4965. err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery);
  4966. break;
  4967. default:
  4968. return -EINVAL;
  4969. }
  4970. if (err)
  4971. return err;
  4972. bt_dev_dbg(hdev, "timeout %u ms", jiffies_to_msecs(timeout));
  4973. queue_delayed_work(hdev->req_workqueue, &hdev->le_scan_disable,
  4974. timeout);
  4975. return 0;
  4976. }
  4977. static void hci_suspend_monitor_sync(struct hci_dev *hdev)
  4978. {
  4979. switch (hci_get_adv_monitor_offload_ext(hdev)) {
  4980. case HCI_ADV_MONITOR_EXT_MSFT:
  4981. msft_suspend_sync(hdev);
  4982. break;
  4983. default:
  4984. return;
  4985. }
  4986. }
  4987. /* This function disables discovery and mark it as paused */
  4988. static int hci_pause_discovery_sync(struct hci_dev *hdev)
  4989. {
  4990. int old_state = hdev->discovery.state;
  4991. int err;
  4992. /* If discovery already stopped/stopping/paused there nothing to do */
  4993. if (old_state == DISCOVERY_STOPPED || old_state == DISCOVERY_STOPPING ||
  4994. hdev->discovery_paused)
  4995. return 0;
  4996. hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
  4997. err = hci_stop_discovery_sync(hdev);
  4998. if (err)
  4999. return err;
  5000. hdev->discovery_paused = true;
  5001. hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
  5002. return 0;
  5003. }
  5004. static int hci_update_event_filter_sync(struct hci_dev *hdev)
  5005. {
  5006. struct bdaddr_list_with_flags *b;
  5007. u8 scan = SCAN_DISABLED;
  5008. bool scanning = test_bit(HCI_PSCAN, &hdev->flags);
  5009. int err;
  5010. if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
  5011. return 0;
  5012. /* Some fake CSR controllers lock up after setting this type of
  5013. * filter, so avoid sending the request altogether.
  5014. */
  5015. if (hci_test_quirk(hdev, HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL))
  5016. return 0;
  5017. /* Always clear event filter when starting */
  5018. hci_clear_event_filter_sync(hdev);
  5019. list_for_each_entry(b, &hdev->accept_list, list) {
  5020. if (!(b->flags & HCI_CONN_FLAG_REMOTE_WAKEUP))
  5021. continue;
  5022. bt_dev_dbg(hdev, "Adding event filters for %pMR", &b->bdaddr);
  5023. err = hci_set_event_filter_sync(hdev, HCI_FLT_CONN_SETUP,
  5024. HCI_CONN_SETUP_ALLOW_BDADDR,
  5025. &b->bdaddr,
  5026. HCI_CONN_SETUP_AUTO_ON);
  5027. if (err)
  5028. bt_dev_err(hdev, "Failed to set event filter for %pMR",
  5029. &b->bdaddr);
  5030. else
  5031. scan = SCAN_PAGE;
  5032. }
  5033. if (scan && !scanning)
  5034. hci_write_scan_enable_sync(hdev, scan);
  5035. else if (!scan && scanning)
  5036. hci_write_scan_enable_sync(hdev, scan);
  5037. return 0;
  5038. }
  5039. /* This function disables scan (BR and LE) and mark it as paused */
  5040. static int hci_pause_scan_sync(struct hci_dev *hdev)
  5041. {
  5042. if (hdev->scanning_paused)
  5043. return 0;
  5044. /* Disable page scan if enabled */
  5045. if (test_bit(HCI_PSCAN, &hdev->flags))
  5046. hci_write_scan_enable_sync(hdev, SCAN_DISABLED);
  5047. hci_scan_disable_sync(hdev);
  5048. hdev->scanning_paused = true;
  5049. return 0;
  5050. }
  5051. /* This function performs the HCI suspend procedures in the follow order:
  5052. *
  5053. * Pause discovery (active scanning/inquiry)
  5054. * Pause Directed Advertising/Advertising
  5055. * Pause Scanning (passive scanning in case discovery was not active)
  5056. * Disconnect all connections
  5057. * Set suspend_status to BT_SUSPEND_DISCONNECT if hdev cannot wakeup
  5058. * otherwise:
  5059. * Update event mask (only set events that are allowed to wake up the host)
  5060. * Update event filter (with devices marked with HCI_CONN_FLAG_REMOTE_WAKEUP)
  5061. * Update passive scanning (lower duty cycle)
  5062. * Set suspend_status to BT_SUSPEND_CONFIGURE_WAKE
  5063. */
  5064. int hci_suspend_sync(struct hci_dev *hdev)
  5065. {
  5066. int err;
  5067. /* If marked as suspended there nothing to do */
  5068. if (hdev->suspended)
  5069. return 0;
  5070. /* Mark device as suspended */
  5071. hdev->suspended = true;
  5072. /* Pause discovery if not already stopped */
  5073. hci_pause_discovery_sync(hdev);
  5074. /* Pause other advertisements */
  5075. hci_pause_advertising_sync(hdev);
  5076. /* Suspend monitor filters */
  5077. hci_suspend_monitor_sync(hdev);
  5078. /* Prevent disconnects from causing scanning to be re-enabled */
  5079. hci_pause_scan_sync(hdev);
  5080. if (hci_conn_count(hdev)) {
  5081. /* Soft disconnect everything (power off) */
  5082. err = hci_disconnect_all_sync(hdev, HCI_ERROR_REMOTE_POWER_OFF);
  5083. if (err) {
  5084. /* Set state to BT_RUNNING so resume doesn't notify */
  5085. hdev->suspend_state = BT_RUNNING;
  5086. hci_resume_sync(hdev);
  5087. return err;
  5088. }
  5089. /* Update event mask so only the allowed event can wakeup the
  5090. * host.
  5091. */
  5092. hci_set_event_mask_sync(hdev);
  5093. }
  5094. /* Only configure accept list if disconnect succeeded and wake
  5095. * isn't being prevented.
  5096. */
  5097. if (!hdev->wakeup || !hdev->wakeup(hdev)) {
  5098. hdev->suspend_state = BT_SUSPEND_DISCONNECT;
  5099. return 0;
  5100. }
  5101. /* Unpause to take care of updating scanning params */
  5102. hdev->scanning_paused = false;
  5103. /* Enable event filter for paired devices */
  5104. hci_update_event_filter_sync(hdev);
  5105. /* Update LE passive scan if enabled */
  5106. hci_update_passive_scan_sync(hdev);
  5107. /* Pause scan changes again. */
  5108. hdev->scanning_paused = true;
  5109. hdev->suspend_state = BT_SUSPEND_CONFIGURE_WAKE;
  5110. return 0;
  5111. }
  5112. /* This function resumes discovery */
  5113. static int hci_resume_discovery_sync(struct hci_dev *hdev)
  5114. {
  5115. int err;
  5116. /* If discovery not paused there nothing to do */
  5117. if (!hdev->discovery_paused)
  5118. return 0;
  5119. hdev->discovery_paused = false;
  5120. hci_discovery_set_state(hdev, DISCOVERY_STARTING);
  5121. err = hci_start_discovery_sync(hdev);
  5122. hci_discovery_set_state(hdev, err ? DISCOVERY_STOPPED :
  5123. DISCOVERY_FINDING);
  5124. return err;
  5125. }
  5126. static void hci_resume_monitor_sync(struct hci_dev *hdev)
  5127. {
  5128. switch (hci_get_adv_monitor_offload_ext(hdev)) {
  5129. case HCI_ADV_MONITOR_EXT_MSFT:
  5130. msft_resume_sync(hdev);
  5131. break;
  5132. default:
  5133. return;
  5134. }
  5135. }
  5136. /* This function resume scan and reset paused flag */
  5137. static int hci_resume_scan_sync(struct hci_dev *hdev)
  5138. {
  5139. if (!hdev->scanning_paused)
  5140. return 0;
  5141. hdev->scanning_paused = false;
  5142. hci_update_scan_sync(hdev);
  5143. /* Reset passive scanning to normal */
  5144. hci_update_passive_scan_sync(hdev);
  5145. return 0;
  5146. }
  5147. /* This function performs the HCI suspend procedures in the follow order:
  5148. *
  5149. * Restore event mask
  5150. * Clear event filter
  5151. * Update passive scanning (normal duty cycle)
  5152. * Resume Directed Advertising/Advertising
  5153. * Resume discovery (active scanning/inquiry)
  5154. */
  5155. int hci_resume_sync(struct hci_dev *hdev)
  5156. {
  5157. /* If not marked as suspended there nothing to do */
  5158. if (!hdev->suspended)
  5159. return 0;
  5160. hdev->suspended = false;
  5161. /* Restore event mask */
  5162. hci_set_event_mask_sync(hdev);
  5163. /* Clear any event filters and restore scan state */
  5164. hci_clear_event_filter_sync(hdev);
  5165. /* Resume scanning */
  5166. hci_resume_scan_sync(hdev);
  5167. /* Resume monitor filters */
  5168. hci_resume_monitor_sync(hdev);
  5169. /* Resume other advertisements */
  5170. hci_resume_advertising_sync(hdev);
  5171. /* Resume discovery */
  5172. hci_resume_discovery_sync(hdev);
  5173. return 0;
  5174. }
  5175. static bool conn_use_rpa(struct hci_conn *conn)
  5176. {
  5177. struct hci_dev *hdev = conn->hdev;
  5178. return hci_dev_test_flag(hdev, HCI_PRIVACY);
  5179. }
  5180. static int hci_le_ext_directed_advertising_sync(struct hci_dev *hdev,
  5181. struct hci_conn *conn)
  5182. {
  5183. struct hci_cp_le_set_ext_adv_params cp;
  5184. struct hci_rp_le_set_ext_adv_params rp;
  5185. int err;
  5186. bdaddr_t random_addr;
  5187. u8 own_addr_type;
  5188. err = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
  5189. &own_addr_type);
  5190. if (err)
  5191. return err;
  5192. /* Set require_privacy to false so that the remote device has a
  5193. * chance of identifying us.
  5194. */
  5195. err = hci_get_random_address(hdev, false, conn_use_rpa(conn), NULL,
  5196. &own_addr_type, &random_addr);
  5197. if (err)
  5198. return err;
  5199. memset(&cp, 0, sizeof(cp));
  5200. cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_DIRECT_IND);
  5201. cp.channel_map = hdev->le_adv_channel_map;
  5202. cp.tx_power = HCI_TX_POWER_INVALID;
  5203. cp.primary_phy = HCI_ADV_PHY_1M;
  5204. cp.secondary_phy = HCI_ADV_PHY_1M;
  5205. cp.handle = 0x00; /* Use instance 0 for directed adv */
  5206. cp.own_addr_type = own_addr_type;
  5207. cp.peer_addr_type = conn->dst_type;
  5208. bacpy(&cp.peer_addr, &conn->dst);
  5209. /* As per Core Spec 5.2 Vol 2, PART E, Sec 7.8.53, for
  5210. * advertising_event_property LE_LEGACY_ADV_DIRECT_IND
  5211. * does not supports advertising data when the advertising set already
  5212. * contains some, the controller shall return erroc code 'Invalid
  5213. * HCI Command Parameters(0x12).
  5214. * So it is required to remove adv set for handle 0x00. since we use
  5215. * instance 0 for directed adv.
  5216. */
  5217. err = hci_remove_ext_adv_instance_sync(hdev, cp.handle, NULL);
  5218. if (err)
  5219. return err;
  5220. err = hci_set_ext_adv_params_sync(hdev, NULL, &cp, &rp);
  5221. if (err)
  5222. return err;
  5223. /* Update adv data as tx power is known now */
  5224. err = hci_set_ext_adv_data_sync(hdev, cp.handle);
  5225. if (err)
  5226. return err;
  5227. /* Check if random address need to be updated */
  5228. if (own_addr_type == ADDR_LE_DEV_RANDOM &&
  5229. bacmp(&random_addr, BDADDR_ANY) &&
  5230. bacmp(&random_addr, &hdev->random_addr)) {
  5231. err = hci_set_adv_set_random_addr_sync(hdev, 0x00,
  5232. &random_addr);
  5233. if (err)
  5234. return err;
  5235. }
  5236. return hci_enable_ext_advertising_sync(hdev, 0x00);
  5237. }
  5238. static int hci_le_directed_advertising_sync(struct hci_dev *hdev,
  5239. struct hci_conn *conn)
  5240. {
  5241. struct hci_cp_le_set_adv_param cp;
  5242. u8 status;
  5243. u8 own_addr_type;
  5244. u8 enable;
  5245. if (ext_adv_capable(hdev))
  5246. return hci_le_ext_directed_advertising_sync(hdev, conn);
  5247. /* Clear the HCI_LE_ADV bit temporarily so that the
  5248. * hci_update_random_address knows that it's safe to go ahead
  5249. * and write a new random address. The flag will be set back on
  5250. * as soon as the SET_ADV_ENABLE HCI command completes.
  5251. */
  5252. hci_dev_clear_flag(hdev, HCI_LE_ADV);
  5253. /* Set require_privacy to false so that the remote device has a
  5254. * chance of identifying us.
  5255. */
  5256. status = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
  5257. &own_addr_type);
  5258. if (status)
  5259. return status;
  5260. memset(&cp, 0, sizeof(cp));
  5261. /* Some controllers might reject command if intervals are not
  5262. * within range for undirected advertising.
  5263. * BCM20702A0 is known to be affected by this.
  5264. */
  5265. cp.min_interval = cpu_to_le16(0x0020);
  5266. cp.max_interval = cpu_to_le16(0x0020);
  5267. cp.type = LE_ADV_DIRECT_IND;
  5268. cp.own_address_type = own_addr_type;
  5269. cp.direct_addr_type = conn->dst_type;
  5270. bacpy(&cp.direct_addr, &conn->dst);
  5271. cp.channel_map = hdev->le_adv_channel_map;
  5272. status = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_PARAM,
  5273. sizeof(cp), &cp, HCI_CMD_TIMEOUT);
  5274. if (status)
  5275. return status;
  5276. enable = 0x01;
  5277. return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
  5278. sizeof(enable), &enable, HCI_CMD_TIMEOUT);
  5279. }
  5280. static void set_ext_conn_params(struct hci_conn *conn,
  5281. struct hci_cp_le_ext_conn_param *p)
  5282. {
  5283. struct hci_dev *hdev = conn->hdev;
  5284. memset(p, 0, sizeof(*p));
  5285. p->scan_interval = cpu_to_le16(hdev->le_scan_int_connect);
  5286. p->scan_window = cpu_to_le16(hdev->le_scan_window_connect);
  5287. p->conn_interval_min = cpu_to_le16(conn->le_conn_min_interval);
  5288. p->conn_interval_max = cpu_to_le16(conn->le_conn_max_interval);
  5289. p->conn_latency = cpu_to_le16(conn->le_conn_latency);
  5290. p->supervision_timeout = cpu_to_le16(conn->le_supv_timeout);
  5291. p->min_ce_len = cpu_to_le16(0x0000);
  5292. p->max_ce_len = cpu_to_le16(0x0000);
  5293. }
  5294. static int hci_le_ext_create_conn_sync(struct hci_dev *hdev,
  5295. struct hci_conn *conn, u8 own_addr_type)
  5296. {
  5297. struct hci_cp_le_ext_create_conn *cp;
  5298. struct hci_cp_le_ext_conn_param *p;
  5299. u8 data[sizeof(*cp) + sizeof(*p) * 3];
  5300. u32 plen;
  5301. cp = (void *)data;
  5302. p = (void *)cp->data;
  5303. memset(cp, 0, sizeof(*cp));
  5304. bacpy(&cp->peer_addr, &conn->dst);
  5305. cp->peer_addr_type = conn->dst_type;
  5306. cp->own_addr_type = own_addr_type;
  5307. plen = sizeof(*cp);
  5308. if (scan_1m(hdev) && (conn->le_adv_phy == HCI_ADV_PHY_1M ||
  5309. conn->le_adv_sec_phy == HCI_ADV_PHY_1M)) {
  5310. cp->phys |= LE_SCAN_PHY_1M;
  5311. set_ext_conn_params(conn, p);
  5312. p++;
  5313. plen += sizeof(*p);
  5314. }
  5315. if (scan_2m(hdev) && (conn->le_adv_phy == HCI_ADV_PHY_2M ||
  5316. conn->le_adv_sec_phy == HCI_ADV_PHY_2M)) {
  5317. cp->phys |= LE_SCAN_PHY_2M;
  5318. set_ext_conn_params(conn, p);
  5319. p++;
  5320. plen += sizeof(*p);
  5321. }
  5322. if (scan_coded(hdev) && (conn->le_adv_phy == HCI_ADV_PHY_CODED ||
  5323. conn->le_adv_sec_phy == HCI_ADV_PHY_CODED)) {
  5324. cp->phys |= LE_SCAN_PHY_CODED;
  5325. set_ext_conn_params(conn, p);
  5326. plen += sizeof(*p);
  5327. }
  5328. return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_EXT_CREATE_CONN,
  5329. plen, data,
  5330. HCI_EV_LE_ENHANCED_CONN_COMPLETE,
  5331. conn->conn_timeout, NULL);
  5332. }
  5333. static int hci_le_create_conn_sync(struct hci_dev *hdev, void *data)
  5334. {
  5335. struct hci_cp_le_create_conn cp;
  5336. struct hci_conn_params *params;
  5337. u8 own_addr_type;
  5338. int err;
  5339. struct hci_conn *conn = data;
  5340. if (!hci_conn_valid(hdev, conn))
  5341. return -ECANCELED;
  5342. bt_dev_dbg(hdev, "conn %p", conn);
  5343. clear_bit(HCI_CONN_SCANNING, &conn->flags);
  5344. conn->state = BT_CONNECT;
  5345. /* If requested to connect as peripheral use directed advertising */
  5346. if (conn->role == HCI_ROLE_SLAVE) {
  5347. /* If we're active scanning and simultaneous roles is not
  5348. * enabled simply reject the attempt.
  5349. */
  5350. if (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
  5351. hdev->le_scan_type == LE_SCAN_ACTIVE &&
  5352. !hci_dev_test_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES)) {
  5353. hci_conn_del(conn);
  5354. return -EBUSY;
  5355. }
  5356. /* Pause advertising while doing directed advertising. */
  5357. hci_pause_advertising_sync(hdev);
  5358. err = hci_le_directed_advertising_sync(hdev, conn);
  5359. goto done;
  5360. }
  5361. /* Disable advertising if simultaneous roles is not in use. */
  5362. if (!hci_dev_test_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES))
  5363. hci_pause_advertising_sync(hdev);
  5364. params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
  5365. if (params) {
  5366. conn->le_conn_min_interval = params->conn_min_interval;
  5367. conn->le_conn_max_interval = params->conn_max_interval;
  5368. conn->le_conn_latency = params->conn_latency;
  5369. conn->le_supv_timeout = params->supervision_timeout;
  5370. } else {
  5371. conn->le_conn_min_interval = hdev->le_conn_min_interval;
  5372. conn->le_conn_max_interval = hdev->le_conn_max_interval;
  5373. conn->le_conn_latency = hdev->le_conn_latency;
  5374. conn->le_supv_timeout = hdev->le_supv_timeout;
  5375. }
  5376. /* If controller is scanning, we stop it since some controllers are
  5377. * not able to scan and connect at the same time. Also set the
  5378. * HCI_LE_SCAN_INTERRUPTED flag so that the command complete
  5379. * handler for scan disabling knows to set the correct discovery
  5380. * state.
  5381. */
  5382. if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
  5383. hci_dev_set_flag(hdev, HCI_LE_SCAN_INTERRUPTED);
  5384. hci_scan_disable_sync(hdev);
  5385. }
  5386. /* Update random address, but set require_privacy to false so
  5387. * that we never connect with an non-resolvable address.
  5388. */
  5389. err = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
  5390. &own_addr_type);
  5391. if (err)
  5392. goto done;
  5393. /* Send command LE Extended Create Connection if supported */
  5394. if (use_ext_conn(hdev)) {
  5395. err = hci_le_ext_create_conn_sync(hdev, conn, own_addr_type);
  5396. goto done;
  5397. }
  5398. memset(&cp, 0, sizeof(cp));
  5399. cp.scan_interval = cpu_to_le16(hdev->le_scan_int_connect);
  5400. cp.scan_window = cpu_to_le16(hdev->le_scan_window_connect);
  5401. bacpy(&cp.peer_addr, &conn->dst);
  5402. cp.peer_addr_type = conn->dst_type;
  5403. cp.own_address_type = own_addr_type;
  5404. cp.conn_interval_min = cpu_to_le16(conn->le_conn_min_interval);
  5405. cp.conn_interval_max = cpu_to_le16(conn->le_conn_max_interval);
  5406. cp.conn_latency = cpu_to_le16(conn->le_conn_latency);
  5407. cp.supervision_timeout = cpu_to_le16(conn->le_supv_timeout);
  5408. cp.min_ce_len = cpu_to_le16(0x0000);
  5409. cp.max_ce_len = cpu_to_le16(0x0000);
  5410. /* BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E page 2261:
  5411. *
  5412. * If this event is unmasked and the HCI_LE_Connection_Complete event
  5413. * is unmasked, only the HCI_LE_Enhanced_Connection_Complete event is
  5414. * sent when a new connection has been created.
  5415. */
  5416. err = __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CREATE_CONN,
  5417. sizeof(cp), &cp,
  5418. use_enhanced_conn_complete(hdev) ?
  5419. HCI_EV_LE_ENHANCED_CONN_COMPLETE :
  5420. HCI_EV_LE_CONN_COMPLETE,
  5421. conn->conn_timeout, NULL);
  5422. done:
  5423. if (err == -ETIMEDOUT)
  5424. hci_le_connect_cancel_sync(hdev, conn, 0x00);
  5425. /* Re-enable advertising after the connection attempt is finished. */
  5426. hci_resume_advertising_sync(hdev);
  5427. return err;
  5428. }
  5429. int hci_le_create_cis_sync(struct hci_dev *hdev)
  5430. {
  5431. DEFINE_FLEX(struct hci_cp_le_create_cis, cmd, cis, num_cis, 0x1f);
  5432. size_t aux_num_cis = 0;
  5433. struct hci_conn *conn;
  5434. u8 cig = BT_ISO_QOS_CIG_UNSET;
  5435. /* The spec allows only one pending LE Create CIS command at a time. If
  5436. * the command is pending now, don't do anything. We check for pending
  5437. * connections after each CIS Established event.
  5438. *
  5439. * BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E
  5440. * page 2566:
  5441. *
  5442. * If the Host issues this command before all the
  5443. * HCI_LE_CIS_Established events from the previous use of the
  5444. * command have been generated, the Controller shall return the
  5445. * error code Command Disallowed (0x0C).
  5446. *
  5447. * BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E
  5448. * page 2567:
  5449. *
  5450. * When the Controller receives the HCI_LE_Create_CIS command, the
  5451. * Controller sends the HCI_Command_Status event to the Host. An
  5452. * HCI_LE_CIS_Established event will be generated for each CIS when it
  5453. * is established or if it is disconnected or considered lost before
  5454. * being established; until all the events are generated, the command
  5455. * remains pending.
  5456. */
  5457. hci_dev_lock(hdev);
  5458. rcu_read_lock();
  5459. /* Wait until previous Create CIS has completed */
  5460. list_for_each_entry_rcu(conn, &hdev->conn_hash.list, list) {
  5461. if (test_bit(HCI_CONN_CREATE_CIS, &conn->flags))
  5462. goto done;
  5463. }
  5464. /* Find CIG with all CIS ready */
  5465. list_for_each_entry_rcu(conn, &hdev->conn_hash.list, list) {
  5466. struct hci_conn *link;
  5467. if (hci_conn_check_create_cis(conn))
  5468. continue;
  5469. cig = conn->iso_qos.ucast.cig;
  5470. list_for_each_entry_rcu(link, &hdev->conn_hash.list, list) {
  5471. if (hci_conn_check_create_cis(link) > 0 &&
  5472. link->iso_qos.ucast.cig == cig &&
  5473. link->state != BT_CONNECTED) {
  5474. cig = BT_ISO_QOS_CIG_UNSET;
  5475. break;
  5476. }
  5477. }
  5478. if (cig != BT_ISO_QOS_CIG_UNSET)
  5479. break;
  5480. }
  5481. if (cig == BT_ISO_QOS_CIG_UNSET)
  5482. goto done;
  5483. list_for_each_entry_rcu(conn, &hdev->conn_hash.list, list) {
  5484. struct hci_cis *cis = &cmd->cis[aux_num_cis];
  5485. if (hci_conn_check_create_cis(conn) ||
  5486. conn->iso_qos.ucast.cig != cig)
  5487. continue;
  5488. set_bit(HCI_CONN_CREATE_CIS, &conn->flags);
  5489. cis->acl_handle = cpu_to_le16(conn->parent->handle);
  5490. cis->cis_handle = cpu_to_le16(conn->handle);
  5491. aux_num_cis++;
  5492. if (aux_num_cis >= cmd->num_cis)
  5493. break;
  5494. }
  5495. cmd->num_cis = aux_num_cis;
  5496. done:
  5497. rcu_read_unlock();
  5498. hci_dev_unlock(hdev);
  5499. if (!aux_num_cis)
  5500. return 0;
  5501. /* Wait for HCI_LE_CIS_Established */
  5502. return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CREATE_CIS,
  5503. struct_size(cmd, cis, cmd->num_cis),
  5504. cmd, HCI_EVT_LE_CIS_ESTABLISHED,
  5505. conn->conn_timeout, NULL);
  5506. }
  5507. int hci_le_remove_cig_sync(struct hci_dev *hdev, u8 handle)
  5508. {
  5509. struct hci_cp_le_remove_cig cp;
  5510. memset(&cp, 0, sizeof(cp));
  5511. cp.cig_id = handle;
  5512. return __hci_cmd_sync_status(hdev, HCI_OP_LE_REMOVE_CIG, sizeof(cp),
  5513. &cp, HCI_CMD_TIMEOUT);
  5514. }
  5515. int hci_le_big_terminate_sync(struct hci_dev *hdev, u8 handle)
  5516. {
  5517. struct hci_cp_le_big_term_sync cp;
  5518. memset(&cp, 0, sizeof(cp));
  5519. cp.handle = handle;
  5520. return __hci_cmd_sync_status(hdev, HCI_OP_LE_BIG_TERM_SYNC,
  5521. sizeof(cp), &cp, HCI_CMD_TIMEOUT);
  5522. }
  5523. int hci_le_pa_terminate_sync(struct hci_dev *hdev, u16 handle)
  5524. {
  5525. struct hci_cp_le_pa_term_sync cp;
  5526. memset(&cp, 0, sizeof(cp));
  5527. cp.handle = cpu_to_le16(handle);
  5528. return __hci_cmd_sync_status(hdev, HCI_OP_LE_PA_TERM_SYNC,
  5529. sizeof(cp), &cp, HCI_CMD_TIMEOUT);
  5530. }
  5531. int hci_get_random_address(struct hci_dev *hdev, bool require_privacy,
  5532. bool use_rpa, struct adv_info *adv_instance,
  5533. u8 *own_addr_type, bdaddr_t *rand_addr)
  5534. {
  5535. int err;
  5536. bacpy(rand_addr, BDADDR_ANY);
  5537. /* If privacy is enabled use a resolvable private address. If
  5538. * current RPA has expired then generate a new one.
  5539. */
  5540. if (use_rpa) {
  5541. /* If Controller supports LL Privacy use own address type is
  5542. * 0x03
  5543. */
  5544. if (ll_privacy_capable(hdev))
  5545. *own_addr_type = ADDR_LE_DEV_RANDOM_RESOLVED;
  5546. else
  5547. *own_addr_type = ADDR_LE_DEV_RANDOM;
  5548. if (adv_instance) {
  5549. if (adv_rpa_valid(adv_instance))
  5550. return 0;
  5551. } else {
  5552. if (rpa_valid(hdev))
  5553. return 0;
  5554. }
  5555. err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa);
  5556. if (err < 0) {
  5557. bt_dev_err(hdev, "failed to generate new RPA");
  5558. return err;
  5559. }
  5560. bacpy(rand_addr, &hdev->rpa);
  5561. return 0;
  5562. }
  5563. /* In case of required privacy without resolvable private address,
  5564. * use an non-resolvable private address. This is useful for
  5565. * non-connectable advertising.
  5566. */
  5567. if (require_privacy) {
  5568. bdaddr_t nrpa;
  5569. while (true) {
  5570. /* The non-resolvable private address is generated
  5571. * from random six bytes with the two most significant
  5572. * bits cleared.
  5573. */
  5574. get_random_bytes(&nrpa, 6);
  5575. nrpa.b[5] &= 0x3f;
  5576. /* The non-resolvable private address shall not be
  5577. * equal to the public address.
  5578. */
  5579. if (bacmp(&hdev->bdaddr, &nrpa))
  5580. break;
  5581. }
  5582. *own_addr_type = ADDR_LE_DEV_RANDOM;
  5583. bacpy(rand_addr, &nrpa);
  5584. return 0;
  5585. }
  5586. /* No privacy, use the current address */
  5587. hci_copy_identity_address(hdev, rand_addr, own_addr_type);
  5588. return 0;
  5589. }
  5590. static int _update_adv_data_sync(struct hci_dev *hdev, void *data)
  5591. {
  5592. u8 instance = PTR_UINT(data);
  5593. return hci_update_adv_data_sync(hdev, instance);
  5594. }
  5595. int hci_update_adv_data(struct hci_dev *hdev, u8 instance)
  5596. {
  5597. return hci_cmd_sync_queue(hdev, _update_adv_data_sync,
  5598. UINT_PTR(instance), NULL);
  5599. }
  5600. static int hci_acl_create_conn_sync(struct hci_dev *hdev, void *data)
  5601. {
  5602. struct hci_conn *conn = data;
  5603. struct inquiry_entry *ie;
  5604. struct hci_cp_create_conn cp;
  5605. int err;
  5606. if (!hci_conn_valid(hdev, conn))
  5607. return -ECANCELED;
  5608. /* Many controllers disallow HCI Create Connection while it is doing
  5609. * HCI Inquiry. So we cancel the Inquiry first before issuing HCI Create
  5610. * Connection. This may cause the MGMT discovering state to become false
  5611. * without user space's request but it is okay since the MGMT Discovery
  5612. * APIs do not promise that discovery should be done forever. Instead,
  5613. * the user space monitors the status of MGMT discovering and it may
  5614. * request for discovery again when this flag becomes false.
  5615. */
  5616. if (test_bit(HCI_INQUIRY, &hdev->flags)) {
  5617. err = __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY_CANCEL, 0,
  5618. NULL, HCI_CMD_TIMEOUT);
  5619. if (err)
  5620. bt_dev_warn(hdev, "Failed to cancel inquiry %d", err);
  5621. }
  5622. conn->state = BT_CONNECT;
  5623. conn->out = true;
  5624. conn->role = HCI_ROLE_MASTER;
  5625. conn->attempt++;
  5626. memset(&cp, 0, sizeof(cp));
  5627. bacpy(&cp.bdaddr, &conn->dst);
  5628. cp.pscan_rep_mode = 0x02;
  5629. ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
  5630. if (ie) {
  5631. if (inquiry_entry_age(ie) <= INQUIRY_ENTRY_AGE_MAX) {
  5632. cp.pscan_rep_mode = ie->data.pscan_rep_mode;
  5633. cp.pscan_mode = ie->data.pscan_mode;
  5634. cp.clock_offset = ie->data.clock_offset |
  5635. cpu_to_le16(0x8000);
  5636. }
  5637. memcpy(conn->dev_class, ie->data.dev_class, 3);
  5638. }
  5639. cp.pkt_type = cpu_to_le16(conn->pkt_type);
  5640. if (lmp_rswitch_capable(hdev) && !(hdev->link_mode & HCI_LM_MASTER))
  5641. cp.role_switch = 0x01;
  5642. else
  5643. cp.role_switch = 0x00;
  5644. return __hci_cmd_sync_status_sk(hdev, HCI_OP_CREATE_CONN,
  5645. sizeof(cp), &cp,
  5646. HCI_EV_CONN_COMPLETE,
  5647. conn->conn_timeout, NULL);
  5648. }
  5649. int hci_connect_acl_sync(struct hci_dev *hdev, struct hci_conn *conn)
  5650. {
  5651. int err;
  5652. err = hci_cmd_sync_queue_once(hdev, hci_acl_create_conn_sync, conn,
  5653. NULL);
  5654. return (err == -EEXIST) ? 0 : err;
  5655. }
  5656. static void create_le_conn_complete(struct hci_dev *hdev, void *data, int err)
  5657. {
  5658. struct hci_conn *conn = data;
  5659. bt_dev_dbg(hdev, "err %d", err);
  5660. if (err == -ECANCELED)
  5661. return;
  5662. hci_dev_lock(hdev);
  5663. if (!hci_conn_valid(hdev, conn))
  5664. goto done;
  5665. if (!err) {
  5666. hci_connect_le_scan_cleanup(conn, 0x00);
  5667. goto done;
  5668. }
  5669. /* Check if connection is still pending */
  5670. if (conn != hci_lookup_le_connect(hdev))
  5671. goto done;
  5672. /* Flush to make sure we send create conn cancel command if needed */
  5673. flush_delayed_work(&conn->le_conn_timeout);
  5674. hci_conn_failed(conn, bt_status(err));
  5675. done:
  5676. hci_dev_unlock(hdev);
  5677. }
  5678. int hci_connect_le_sync(struct hci_dev *hdev, struct hci_conn *conn)
  5679. {
  5680. int err;
  5681. err = hci_cmd_sync_queue_once(hdev, hci_le_create_conn_sync, conn,
  5682. create_le_conn_complete);
  5683. return (err == -EEXIST) ? 0 : err;
  5684. }
  5685. int hci_cancel_connect_sync(struct hci_dev *hdev, struct hci_conn *conn)
  5686. {
  5687. if (conn->state != BT_OPEN)
  5688. return -EINVAL;
  5689. switch (conn->type) {
  5690. case ACL_LINK:
  5691. return !hci_cmd_sync_dequeue_once(hdev,
  5692. hci_acl_create_conn_sync,
  5693. conn, NULL);
  5694. case LE_LINK:
  5695. return !hci_cmd_sync_dequeue_once(hdev, hci_le_create_conn_sync,
  5696. conn, create_le_conn_complete);
  5697. }
  5698. return -ENOENT;
  5699. }
  5700. int hci_le_conn_update_sync(struct hci_dev *hdev, struct hci_conn *conn,
  5701. struct hci_conn_params *params)
  5702. {
  5703. struct hci_cp_le_conn_update cp;
  5704. memset(&cp, 0, sizeof(cp));
  5705. cp.handle = cpu_to_le16(conn->handle);
  5706. cp.conn_interval_min = cpu_to_le16(params->conn_min_interval);
  5707. cp.conn_interval_max = cpu_to_le16(params->conn_max_interval);
  5708. cp.conn_latency = cpu_to_le16(params->conn_latency);
  5709. cp.supervision_timeout = cpu_to_le16(params->supervision_timeout);
  5710. cp.min_ce_len = cpu_to_le16(0x0000);
  5711. cp.max_ce_len = cpu_to_le16(0x0000);
  5712. return __hci_cmd_sync_status(hdev, HCI_OP_LE_CONN_UPDATE,
  5713. sizeof(cp), &cp, HCI_CMD_TIMEOUT);
  5714. }
  5715. static void create_pa_complete(struct hci_dev *hdev, void *data, int err)
  5716. {
  5717. struct hci_conn *conn = data;
  5718. struct hci_conn *pa_sync;
  5719. bt_dev_dbg(hdev, "err %d", err);
  5720. if (err == -ECANCELED)
  5721. return;
  5722. hci_dev_lock(hdev);
  5723. if (hci_conn_valid(hdev, conn))
  5724. clear_bit(HCI_CONN_CREATE_PA_SYNC, &conn->flags);
  5725. if (!err)
  5726. goto unlock;
  5727. /* Add connection to indicate PA sync error */
  5728. pa_sync = hci_conn_add_unset(hdev, PA_LINK, BDADDR_ANY, 0,
  5729. HCI_ROLE_SLAVE);
  5730. if (IS_ERR(pa_sync))
  5731. goto unlock;
  5732. set_bit(HCI_CONN_PA_SYNC_FAILED, &pa_sync->flags);
  5733. /* Notify iso layer */
  5734. hci_connect_cfm(pa_sync, bt_status(err));
  5735. unlock:
  5736. hci_dev_unlock(hdev);
  5737. }
  5738. static int hci_le_past_params_sync(struct hci_dev *hdev, struct hci_conn *conn,
  5739. struct hci_conn *acl, struct bt_iso_qos *qos)
  5740. {
  5741. struct hci_cp_le_past_params cp;
  5742. int err;
  5743. memset(&cp, 0, sizeof(cp));
  5744. cp.handle = cpu_to_le16(acl->handle);
  5745. /* An HCI_LE_Periodic_Advertising_Sync_Transfer_Received event is sent
  5746. * to the Host. HCI_LE_Periodic_Advertising_Report events will be
  5747. * enabled with duplicate filtering enabled.
  5748. */
  5749. cp.mode = 0x03;
  5750. cp.skip = cpu_to_le16(qos->bcast.skip);
  5751. cp.sync_timeout = cpu_to_le16(qos->bcast.sync_timeout);
  5752. cp.cte_type = qos->bcast.sync_cte_type;
  5753. /* HCI_LE_PAST_PARAMS command returns a command complete event so it
  5754. * cannot wait for HCI_EV_LE_PAST_RECEIVED.
  5755. */
  5756. err = __hci_cmd_sync_status(hdev, HCI_OP_LE_PAST_PARAMS,
  5757. sizeof(cp), &cp, HCI_CMD_TIMEOUT);
  5758. if (err)
  5759. return err;
  5760. /* Wait for HCI_EV_LE_PAST_RECEIVED event */
  5761. return __hci_cmd_sync_status_sk(hdev, HCI_OP_NOP, 0, NULL,
  5762. HCI_EV_LE_PAST_RECEIVED,
  5763. conn->conn_timeout, NULL);
  5764. }
  5765. static int hci_le_pa_create_sync(struct hci_dev *hdev, void *data)
  5766. {
  5767. struct hci_cp_le_pa_create_sync cp;
  5768. struct hci_conn *conn = data, *le;
  5769. struct bt_iso_qos *qos = &conn->iso_qos;
  5770. int err;
  5771. if (!hci_conn_valid(hdev, conn))
  5772. return -ECANCELED;
  5773. if (conn->sync_handle != HCI_SYNC_HANDLE_INVALID)
  5774. return -EINVAL;
  5775. if (hci_dev_test_and_set_flag(hdev, HCI_PA_SYNC))
  5776. return -EBUSY;
  5777. /* Stop scanning if SID has not been set and active scanning is enabled
  5778. * so we use passive scanning which will be scanning using the allow
  5779. * list programmed to contain only the connection address.
  5780. */
  5781. if (conn->sid == HCI_SID_INVALID &&
  5782. hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
  5783. hci_scan_disable_sync(hdev);
  5784. hci_dev_set_flag(hdev, HCI_LE_SCAN_INTERRUPTED);
  5785. hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
  5786. }
  5787. /* Mark HCI_CONN_CREATE_PA_SYNC so hci_update_passive_scan_sync can
  5788. * program the address in the allow list so PA advertisements can be
  5789. * received.
  5790. */
  5791. set_bit(HCI_CONN_CREATE_PA_SYNC, &conn->flags);
  5792. hci_update_passive_scan_sync(hdev);
  5793. /* Check if PAST is possible:
  5794. *
  5795. * 1. Check if an ACL connection with the destination address exists
  5796. * 2. Check if that HCI_CONN_FLAG_PAST has been set which indicates that
  5797. * user really intended to use PAST.
  5798. */
  5799. le = hci_conn_hash_lookup_le(hdev, &conn->dst, conn->dst_type);
  5800. if (le) {
  5801. struct hci_conn_params *params;
  5802. params = hci_conn_params_lookup(hdev, &le->dst, le->dst_type);
  5803. if (params && params->flags & HCI_CONN_FLAG_PAST) {
  5804. err = hci_le_past_params_sync(hdev, conn, le, qos);
  5805. if (!err)
  5806. goto done;
  5807. }
  5808. }
  5809. /* SID has not been set listen for HCI_EV_LE_EXT_ADV_REPORT to update
  5810. * it.
  5811. */
  5812. if (conn->sid == HCI_SID_INVALID) {
  5813. err = __hci_cmd_sync_status_sk(hdev, HCI_OP_NOP, 0, NULL,
  5814. HCI_EV_LE_EXT_ADV_REPORT,
  5815. conn->conn_timeout, NULL);
  5816. if (err == -ETIMEDOUT)
  5817. goto done;
  5818. }
  5819. memset(&cp, 0, sizeof(cp));
  5820. cp.options = qos->bcast.options;
  5821. cp.sid = conn->sid;
  5822. cp.addr_type = conn->dst_type;
  5823. bacpy(&cp.addr, &conn->dst);
  5824. cp.skip = cpu_to_le16(qos->bcast.skip);
  5825. cp.sync_timeout = cpu_to_le16(qos->bcast.sync_timeout);
  5826. cp.sync_cte_type = qos->bcast.sync_cte_type;
  5827. /* The spec allows only one pending LE Periodic Advertising Create
  5828. * Sync command at a time so we forcefully wait for PA Sync Established
  5829. * event since cmd_work can only schedule one command at a time.
  5830. *
  5831. * BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E
  5832. * page 2493:
  5833. *
  5834. * If the Host issues this command when another HCI_LE_Periodic_
  5835. * Advertising_Create_Sync command is pending, the Controller shall
  5836. * return the error code Command Disallowed (0x0C).
  5837. */
  5838. err = __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_PA_CREATE_SYNC,
  5839. sizeof(cp), &cp,
  5840. HCI_EV_LE_PA_SYNC_ESTABLISHED,
  5841. conn->conn_timeout, NULL);
  5842. if (err == -ETIMEDOUT)
  5843. __hci_cmd_sync_status(hdev, HCI_OP_LE_PA_CREATE_SYNC_CANCEL,
  5844. 0, NULL, HCI_CMD_TIMEOUT);
  5845. done:
  5846. hci_dev_clear_flag(hdev, HCI_PA_SYNC);
  5847. /* Update passive scan since HCI_PA_SYNC flag has been cleared */
  5848. hci_update_passive_scan_sync(hdev);
  5849. return err;
  5850. }
  5851. int hci_connect_pa_sync(struct hci_dev *hdev, struct hci_conn *conn)
  5852. {
  5853. int err;
  5854. err = hci_cmd_sync_queue_once(hdev, hci_le_pa_create_sync, conn,
  5855. create_pa_complete);
  5856. return (err == -EEXIST) ? 0 : err;
  5857. }
  5858. static void create_big_complete(struct hci_dev *hdev, void *data, int err)
  5859. {
  5860. struct hci_conn *conn = data;
  5861. bt_dev_dbg(hdev, "err %d", err);
  5862. if (err == -ECANCELED)
  5863. return;
  5864. if (hci_conn_valid(hdev, conn))
  5865. clear_bit(HCI_CONN_CREATE_BIG_SYNC, &conn->flags);
  5866. }
  5867. static int hci_le_big_create_sync(struct hci_dev *hdev, void *data)
  5868. {
  5869. DEFINE_FLEX(struct hci_cp_le_big_create_sync, cp, bis, num_bis,
  5870. HCI_MAX_ISO_BIS);
  5871. struct hci_conn *conn = data;
  5872. struct bt_iso_qos *qos = &conn->iso_qos;
  5873. int err;
  5874. if (!hci_conn_valid(hdev, conn))
  5875. return -ECANCELED;
  5876. set_bit(HCI_CONN_CREATE_BIG_SYNC, &conn->flags);
  5877. memset(cp, 0, sizeof(*cp));
  5878. cp->handle = qos->bcast.big;
  5879. cp->sync_handle = cpu_to_le16(conn->sync_handle);
  5880. cp->encryption = qos->bcast.encryption;
  5881. memcpy(cp->bcode, qos->bcast.bcode, sizeof(cp->bcode));
  5882. cp->mse = qos->bcast.mse;
  5883. cp->timeout = cpu_to_le16(qos->bcast.timeout);
  5884. cp->num_bis = conn->num_bis;
  5885. memcpy(cp->bis, conn->bis, conn->num_bis);
  5886. /* The spec allows only one pending LE BIG Create Sync command at
  5887. * a time, so we forcefully wait for BIG Sync Established event since
  5888. * cmd_work can only schedule one command at a time.
  5889. *
  5890. * BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E
  5891. * page 2586:
  5892. *
  5893. * If the Host sends this command when the Controller is in the
  5894. * process of synchronizing to any BIG, i.e. the HCI_LE_BIG_Sync_
  5895. * Established event has not been generated, the Controller shall
  5896. * return the error code Command Disallowed (0x0C).
  5897. */
  5898. err = __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_BIG_CREATE_SYNC,
  5899. struct_size(cp, bis, cp->num_bis), cp,
  5900. HCI_EVT_LE_BIG_SYNC_ESTABLISHED,
  5901. conn->conn_timeout, NULL);
  5902. if (err == -ETIMEDOUT)
  5903. hci_le_big_terminate_sync(hdev, cp->handle);
  5904. return err;
  5905. }
  5906. int hci_connect_big_sync(struct hci_dev *hdev, struct hci_conn *conn)
  5907. {
  5908. int err;
  5909. err = hci_cmd_sync_queue_once(hdev, hci_le_big_create_sync, conn,
  5910. create_big_complete);
  5911. return (err == -EEXIST) ? 0 : err;
  5912. }
  5913. struct past_data {
  5914. struct hci_conn *conn;
  5915. struct hci_conn *le;
  5916. };
  5917. static void past_complete(struct hci_dev *hdev, void *data, int err)
  5918. {
  5919. struct past_data *past = data;
  5920. bt_dev_dbg(hdev, "err %d", err);
  5921. kfree(past);
  5922. }
  5923. static int hci_le_past_set_info_sync(struct hci_dev *hdev, void *data)
  5924. {
  5925. struct past_data *past = data;
  5926. struct hci_cp_le_past_set_info cp;
  5927. hci_dev_lock(hdev);
  5928. if (!hci_conn_valid(hdev, past->conn) ||
  5929. !hci_conn_valid(hdev, past->le)) {
  5930. hci_dev_unlock(hdev);
  5931. return -ECANCELED;
  5932. }
  5933. memset(&cp, 0, sizeof(cp));
  5934. cp.handle = cpu_to_le16(past->le->handle);
  5935. cp.adv_handle = past->conn->iso_qos.bcast.bis;
  5936. hci_dev_unlock(hdev);
  5937. return __hci_cmd_sync_status(hdev, HCI_OP_LE_PAST_SET_INFO,
  5938. sizeof(cp), &cp, HCI_CMD_TIMEOUT);
  5939. }
  5940. static int hci_le_past_sync(struct hci_dev *hdev, void *data)
  5941. {
  5942. struct past_data *past = data;
  5943. struct hci_cp_le_past cp;
  5944. hci_dev_lock(hdev);
  5945. if (!hci_conn_valid(hdev, past->conn) ||
  5946. !hci_conn_valid(hdev, past->le)) {
  5947. hci_dev_unlock(hdev);
  5948. return -ECANCELED;
  5949. }
  5950. memset(&cp, 0, sizeof(cp));
  5951. cp.handle = cpu_to_le16(past->le->handle);
  5952. cp.sync_handle = cpu_to_le16(past->conn->sync_handle);
  5953. hci_dev_unlock(hdev);
  5954. return __hci_cmd_sync_status(hdev, HCI_OP_LE_PAST,
  5955. sizeof(cp), &cp, HCI_CMD_TIMEOUT);
  5956. }
  5957. int hci_past_sync(struct hci_conn *conn, struct hci_conn *le)
  5958. {
  5959. struct past_data *data;
  5960. int err;
  5961. if (conn->type != BIS_LINK && conn->type != PA_LINK)
  5962. return -EINVAL;
  5963. if (!past_sender_capable(conn->hdev))
  5964. return -EOPNOTSUPP;
  5965. data = kmalloc_obj(*data);
  5966. if (!data)
  5967. return -ENOMEM;
  5968. data->conn = conn;
  5969. data->le = le;
  5970. if (conn->role == HCI_ROLE_MASTER)
  5971. err = hci_cmd_sync_queue_once(conn->hdev,
  5972. hci_le_past_set_info_sync, data,
  5973. past_complete);
  5974. else
  5975. err = hci_cmd_sync_queue_once(conn->hdev, hci_le_past_sync,
  5976. data, past_complete);
  5977. if (err)
  5978. kfree(data);
  5979. return (err == -EEXIST) ? 0 : err;
  5980. }
  5981. static void le_read_features_complete(struct hci_dev *hdev, void *data, int err)
  5982. {
  5983. struct hci_conn *conn = data;
  5984. bt_dev_dbg(hdev, "err %d", err);
  5985. hci_conn_drop(conn);
  5986. hci_conn_put(conn);
  5987. }
  5988. static int hci_le_read_all_remote_features_sync(struct hci_dev *hdev,
  5989. void *data)
  5990. {
  5991. struct hci_conn *conn = data;
  5992. struct hci_cp_le_read_all_remote_features cp;
  5993. memset(&cp, 0, sizeof(cp));
  5994. cp.handle = cpu_to_le16(conn->handle);
  5995. cp.pages = 10; /* Attempt to read all pages */
  5996. /* Wait for HCI_EVT_LE_ALL_REMOTE_FEATURES_COMPLETE event otherwise
  5997. * hci_conn_drop may run prematurely causing a disconnection.
  5998. */
  5999. return __hci_cmd_sync_status_sk(hdev,
  6000. HCI_OP_LE_READ_ALL_REMOTE_FEATURES,
  6001. sizeof(cp), &cp,
  6002. HCI_EVT_LE_ALL_REMOTE_FEATURES_COMPLETE,
  6003. HCI_CMD_TIMEOUT, NULL);
  6004. return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_ALL_REMOTE_FEATURES,
  6005. sizeof(cp), &cp, HCI_CMD_TIMEOUT);
  6006. }
  6007. static int hci_le_read_remote_features_sync(struct hci_dev *hdev, void *data)
  6008. {
  6009. struct hci_conn *conn = data;
  6010. struct hci_cp_le_read_remote_features cp;
  6011. if (!hci_conn_valid(hdev, conn))
  6012. return -ECANCELED;
  6013. /* Check if LL Extended Feature Set is supported and
  6014. * HCI_OP_LE_READ_ALL_REMOTE_FEATURES is supported then use that to read
  6015. * all features.
  6016. */
  6017. if (ll_ext_feature_capable(hdev) && hdev->commands[47] & BIT(3))
  6018. return hci_le_read_all_remote_features_sync(hdev, data);
  6019. memset(&cp, 0, sizeof(cp));
  6020. cp.handle = cpu_to_le16(conn->handle);
  6021. /* Wait for HCI_EV_LE_REMOTE_FEAT_COMPLETE event otherwise
  6022. * hci_conn_drop may run prematurely causing a disconnection.
  6023. */
  6024. return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_READ_REMOTE_FEATURES,
  6025. sizeof(cp), &cp,
  6026. HCI_EV_LE_REMOTE_FEAT_COMPLETE,
  6027. HCI_CMD_TIMEOUT, NULL);
  6028. }
  6029. int hci_le_read_remote_features(struct hci_conn *conn)
  6030. {
  6031. struct hci_dev *hdev = conn->hdev;
  6032. int err;
  6033. /* The remote features procedure is defined for central
  6034. * role only. So only in case of an initiated connection
  6035. * request the remote features.
  6036. *
  6037. * If the local controller supports peripheral-initiated features
  6038. * exchange, then requesting the remote features in peripheral
  6039. * role is possible. Otherwise just transition into the
  6040. * connected state without requesting the remote features.
  6041. */
  6042. if (conn->out || (hdev->le_features[0] & HCI_LE_PERIPHERAL_FEATURES)) {
  6043. err = hci_cmd_sync_queue_once(hdev,
  6044. hci_le_read_remote_features_sync,
  6045. hci_conn_hold(hci_conn_get(conn)),
  6046. le_read_features_complete);
  6047. if (err) {
  6048. hci_conn_drop(conn);
  6049. hci_conn_put(conn);
  6050. }
  6051. } else {
  6052. err = -EOPNOTSUPP;
  6053. }
  6054. return (err == -EEXIST) ? 0 : err;
  6055. }
  6056. static void pkt_type_changed(struct hci_dev *hdev, void *data, int err)
  6057. {
  6058. struct hci_cp_change_conn_ptype *cp = data;
  6059. bt_dev_dbg(hdev, "err %d", err);
  6060. kfree(cp);
  6061. }
  6062. static int hci_change_conn_ptype_sync(struct hci_dev *hdev, void *data)
  6063. {
  6064. struct hci_cp_change_conn_ptype *cp = data;
  6065. return __hci_cmd_sync_status_sk(hdev, HCI_OP_CHANGE_CONN_PTYPE,
  6066. sizeof(*cp), cp,
  6067. HCI_EV_PKT_TYPE_CHANGE,
  6068. HCI_CMD_TIMEOUT, NULL);
  6069. }
  6070. int hci_acl_change_pkt_type(struct hci_conn *conn, u16 pkt_type)
  6071. {
  6072. struct hci_dev *hdev = conn->hdev;
  6073. struct hci_cp_change_conn_ptype *cp;
  6074. int err;
  6075. cp = kmalloc_obj(*cp);
  6076. if (!cp)
  6077. return -ENOMEM;
  6078. cp->handle = cpu_to_le16(conn->handle);
  6079. cp->pkt_type = cpu_to_le16(pkt_type);
  6080. err = hci_cmd_sync_queue_once(hdev, hci_change_conn_ptype_sync, cp,
  6081. pkt_type_changed);
  6082. if (err)
  6083. kfree(cp);
  6084. return (err == -EEXIST) ? 0 : err;
  6085. }
  6086. static void le_phy_update_complete(struct hci_dev *hdev, void *data, int err)
  6087. {
  6088. struct hci_cp_le_set_phy *cp = data;
  6089. bt_dev_dbg(hdev, "err %d", err);
  6090. kfree(cp);
  6091. }
  6092. static int hci_le_set_phy_sync(struct hci_dev *hdev, void *data)
  6093. {
  6094. struct hci_cp_le_set_phy *cp = data;
  6095. return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_SET_PHY,
  6096. sizeof(*cp), cp,
  6097. HCI_EV_LE_PHY_UPDATE_COMPLETE,
  6098. HCI_CMD_TIMEOUT, NULL);
  6099. }
  6100. int hci_le_set_phy(struct hci_conn *conn, u8 tx_phys, u8 rx_phys)
  6101. {
  6102. struct hci_dev *hdev = conn->hdev;
  6103. struct hci_cp_le_set_phy *cp;
  6104. int err;
  6105. cp = kmalloc_obj(*cp);
  6106. if (!cp)
  6107. return -ENOMEM;
  6108. memset(cp, 0, sizeof(*cp));
  6109. cp->handle = cpu_to_le16(conn->handle);
  6110. cp->tx_phys = tx_phys;
  6111. cp->rx_phys = rx_phys;
  6112. err = hci_cmd_sync_queue_once(hdev, hci_le_set_phy_sync, cp,
  6113. le_phy_update_complete);
  6114. if (err)
  6115. kfree(cp);
  6116. return (err == -EEXIST) ? 0 : err;
  6117. }