hci_sync.c 187 KB

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