hci_sync.c 163 KB

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