hub.c 171 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * USB hub driver.
  4. *
  5. * (C) Copyright 1999 Linus Torvalds
  6. * (C) Copyright 1999 Johannes Erdfelt
  7. * (C) Copyright 1999 Gregory P. Smith
  8. * (C) Copyright 2001 Brad Hards (bhards@bigpond.net.au)
  9. *
  10. * Released under the GPLv2 only.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/errno.h>
  14. #include <linux/module.h>
  15. #include <linux/moduleparam.h>
  16. #include <linux/completion.h>
  17. #include <linux/sched/mm.h>
  18. #include <linux/list.h>
  19. #include <linux/slab.h>
  20. #include <linux/ioctl.h>
  21. #include <linux/usb.h>
  22. #include <linux/usbdevice_fs.h>
  23. #include <linux/usb/hcd.h>
  24. #include <linux/usb/otg.h>
  25. #include <linux/usb/quirks.h>
  26. #include <linux/workqueue.h>
  27. #include <linux/mutex.h>
  28. #include <linux/random.h>
  29. #include <linux/pm_qos.h>
  30. #include <linux/kobject.h>
  31. #include <linux/uaccess.h>
  32. #include <asm/byteorder.h>
  33. #include "hub.h"
  34. #include "otg_whitelist.h"
  35. #define USB_VENDOR_GENESYS_LOGIC 0x05e3
  36. #define HUB_QUIRK_CHECK_PORT_AUTOSUSPEND 0x01
  37. #define USB_TP_TRANSMISSION_DELAY 40 /* ns */
  38. #define USB_TP_TRANSMISSION_DELAY_MAX 65535 /* ns */
  39. /* Protect struct usb_device->state and ->children members
  40. * Note: Both are also protected by ->dev.sem, except that ->state can
  41. * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */
  42. static DEFINE_SPINLOCK(device_state_lock);
  43. /* workqueue to process hub events */
  44. static struct workqueue_struct *hub_wq;
  45. static void hub_event(struct work_struct *work);
  46. /* synchronize hub-port add/remove and peering operations */
  47. DEFINE_MUTEX(usb_port_peer_mutex);
  48. /* cycle leds on hubs that aren't blinking for attention */
  49. static bool blinkenlights;
  50. module_param(blinkenlights, bool, S_IRUGO);
  51. MODULE_PARM_DESC(blinkenlights, "true to cycle leds on hubs");
  52. /*
  53. * Device SATA8000 FW1.0 from DATAST0R Technology Corp requires about
  54. * 10 seconds to send reply for the initial 64-byte descriptor request.
  55. */
  56. /* define initial 64-byte descriptor request timeout in milliseconds */
  57. static int initial_descriptor_timeout = USB_CTRL_GET_TIMEOUT;
  58. module_param(initial_descriptor_timeout, int, S_IRUGO|S_IWUSR);
  59. MODULE_PARM_DESC(initial_descriptor_timeout,
  60. "initial 64-byte descriptor request timeout in milliseconds "
  61. "(default 5000 - 5.0 seconds)");
  62. /*
  63. * As of 2.6.10 we introduce a new USB device initialization scheme which
  64. * closely resembles the way Windows works. Hopefully it will be compatible
  65. * with a wider range of devices than the old scheme. However some previously
  66. * working devices may start giving rise to "device not accepting address"
  67. * errors; if that happens the user can try the old scheme by adjusting the
  68. * following module parameters.
  69. *
  70. * For maximum flexibility there are two boolean parameters to control the
  71. * hub driver's behavior. On the first initialization attempt, if the
  72. * "old_scheme_first" parameter is set then the old scheme will be used,
  73. * otherwise the new scheme is used. If that fails and "use_both_schemes"
  74. * is set, then the driver will make another attempt, using the other scheme.
  75. */
  76. static bool old_scheme_first;
  77. module_param(old_scheme_first, bool, S_IRUGO | S_IWUSR);
  78. MODULE_PARM_DESC(old_scheme_first,
  79. "start with the old device initialization scheme");
  80. static bool use_both_schemes = 1;
  81. module_param(use_both_schemes, bool, S_IRUGO | S_IWUSR);
  82. MODULE_PARM_DESC(use_both_schemes,
  83. "try the other device initialization scheme if the "
  84. "first one fails");
  85. /* Mutual exclusion for EHCI CF initialization. This interferes with
  86. * port reset on some companion controllers.
  87. */
  88. DECLARE_RWSEM(ehci_cf_port_reset_rwsem);
  89. EXPORT_SYMBOL_GPL(ehci_cf_port_reset_rwsem);
  90. #define HUB_DEBOUNCE_TIMEOUT 2000
  91. #define HUB_DEBOUNCE_STEP 25
  92. #define HUB_DEBOUNCE_STABLE 100
  93. static void hub_release(struct kref *kref);
  94. static int usb_reset_and_verify_device(struct usb_device *udev);
  95. static int hub_port_disable(struct usb_hub *hub, int port1, int set_state);
  96. static inline char *portspeed(struct usb_hub *hub, int portstatus)
  97. {
  98. if (hub_is_superspeedplus(hub->hdev))
  99. return "10.0 Gb/s";
  100. if (hub_is_superspeed(hub->hdev))
  101. return "5.0 Gb/s";
  102. if (portstatus & USB_PORT_STAT_HIGH_SPEED)
  103. return "480 Mb/s";
  104. else if (portstatus & USB_PORT_STAT_LOW_SPEED)
  105. return "1.5 Mb/s";
  106. else
  107. return "12 Mb/s";
  108. }
  109. /* Note that hdev or one of its children must be locked! */
  110. struct usb_hub *usb_hub_to_struct_hub(struct usb_device *hdev)
  111. {
  112. if (!hdev || !hdev->actconfig || !hdev->maxchild)
  113. return NULL;
  114. return usb_get_intfdata(hdev->actconfig->interface[0]);
  115. }
  116. int usb_device_supports_lpm(struct usb_device *udev)
  117. {
  118. /* Some devices have trouble with LPM */
  119. if (udev->quirks & USB_QUIRK_NO_LPM)
  120. return 0;
  121. /* USB 2.1 (and greater) devices indicate LPM support through
  122. * their USB 2.0 Extended Capabilities BOS descriptor.
  123. */
  124. if (udev->speed == USB_SPEED_HIGH || udev->speed == USB_SPEED_FULL) {
  125. if (udev->bos->ext_cap &&
  126. (USB_LPM_SUPPORT &
  127. le32_to_cpu(udev->bos->ext_cap->bmAttributes)))
  128. return 1;
  129. return 0;
  130. }
  131. /*
  132. * According to the USB 3.0 spec, all USB 3.0 devices must support LPM.
  133. * However, there are some that don't, and they set the U1/U2 exit
  134. * latencies to zero.
  135. */
  136. if (!udev->bos->ss_cap) {
  137. dev_info(&udev->dev, "No LPM exit latency info found, disabling LPM.\n");
  138. return 0;
  139. }
  140. if (udev->bos->ss_cap->bU1devExitLat == 0 &&
  141. udev->bos->ss_cap->bU2DevExitLat == 0) {
  142. if (udev->parent)
  143. dev_info(&udev->dev, "LPM exit latency is zeroed, disabling LPM.\n");
  144. else
  145. dev_info(&udev->dev, "We don't know the algorithms for LPM for this host, disabling LPM.\n");
  146. return 0;
  147. }
  148. if (!udev->parent || udev->parent->lpm_capable)
  149. return 1;
  150. return 0;
  151. }
  152. /*
  153. * Set the Maximum Exit Latency (MEL) for the host to initiate a transition from
  154. * either U1 or U2.
  155. */
  156. static void usb_set_lpm_mel(struct usb_device *udev,
  157. struct usb3_lpm_parameters *udev_lpm_params,
  158. unsigned int udev_exit_latency,
  159. struct usb_hub *hub,
  160. struct usb3_lpm_parameters *hub_lpm_params,
  161. unsigned int hub_exit_latency)
  162. {
  163. unsigned int total_mel;
  164. unsigned int device_mel;
  165. unsigned int hub_mel;
  166. /*
  167. * Calculate the time it takes to transition all links from the roothub
  168. * to the parent hub into U0. The parent hub must then decode the
  169. * packet (hub header decode latency) to figure out which port it was
  170. * bound for.
  171. *
  172. * The Hub Header decode latency is expressed in 0.1us intervals (0x1
  173. * means 0.1us). Multiply that by 100 to get nanoseconds.
  174. */
  175. total_mel = hub_lpm_params->mel +
  176. (hub->descriptor->u.ss.bHubHdrDecLat * 100);
  177. /*
  178. * How long will it take to transition the downstream hub's port into
  179. * U0? The greater of either the hub exit latency or the device exit
  180. * latency.
  181. *
  182. * The BOS U1/U2 exit latencies are expressed in 1us intervals.
  183. * Multiply that by 1000 to get nanoseconds.
  184. */
  185. device_mel = udev_exit_latency * 1000;
  186. hub_mel = hub_exit_latency * 1000;
  187. if (device_mel > hub_mel)
  188. total_mel += device_mel;
  189. else
  190. total_mel += hub_mel;
  191. udev_lpm_params->mel = total_mel;
  192. }
  193. /*
  194. * Set the maximum Device to Host Exit Latency (PEL) for the device to initiate
  195. * a transition from either U1 or U2.
  196. */
  197. static void usb_set_lpm_pel(struct usb_device *udev,
  198. struct usb3_lpm_parameters *udev_lpm_params,
  199. unsigned int udev_exit_latency,
  200. struct usb_hub *hub,
  201. struct usb3_lpm_parameters *hub_lpm_params,
  202. unsigned int hub_exit_latency,
  203. unsigned int port_to_port_exit_latency)
  204. {
  205. unsigned int first_link_pel;
  206. unsigned int hub_pel;
  207. /*
  208. * First, the device sends an LFPS to transition the link between the
  209. * device and the parent hub into U0. The exit latency is the bigger of
  210. * the device exit latency or the hub exit latency.
  211. */
  212. if (udev_exit_latency > hub_exit_latency)
  213. first_link_pel = udev_exit_latency * 1000;
  214. else
  215. first_link_pel = hub_exit_latency * 1000;
  216. /*
  217. * When the hub starts to receive the LFPS, there is a slight delay for
  218. * it to figure out that one of the ports is sending an LFPS. Then it
  219. * will forward the LFPS to its upstream link. The exit latency is the
  220. * delay, plus the PEL that we calculated for this hub.
  221. */
  222. hub_pel = port_to_port_exit_latency * 1000 + hub_lpm_params->pel;
  223. /*
  224. * According to figure C-7 in the USB 3.0 spec, the PEL for this device
  225. * is the greater of the two exit latencies.
  226. */
  227. if (first_link_pel > hub_pel)
  228. udev_lpm_params->pel = first_link_pel;
  229. else
  230. udev_lpm_params->pel = hub_pel;
  231. }
  232. /*
  233. * Set the System Exit Latency (SEL) to indicate the total worst-case time from
  234. * when a device initiates a transition to U0, until when it will receive the
  235. * first packet from the host controller.
  236. *
  237. * Section C.1.5.1 describes the four components to this:
  238. * - t1: device PEL
  239. * - t2: time for the ERDY to make it from the device to the host.
  240. * - t3: a host-specific delay to process the ERDY.
  241. * - t4: time for the packet to make it from the host to the device.
  242. *
  243. * t3 is specific to both the xHCI host and the platform the host is integrated
  244. * into. The Intel HW folks have said it's negligible, FIXME if a different
  245. * vendor says otherwise.
  246. */
  247. static void usb_set_lpm_sel(struct usb_device *udev,
  248. struct usb3_lpm_parameters *udev_lpm_params)
  249. {
  250. struct usb_device *parent;
  251. unsigned int num_hubs;
  252. unsigned int total_sel;
  253. /* t1 = device PEL */
  254. total_sel = udev_lpm_params->pel;
  255. /* How many external hubs are in between the device & the root port. */
  256. for (parent = udev->parent, num_hubs = 0; parent->parent;
  257. parent = parent->parent)
  258. num_hubs++;
  259. /* t2 = 2.1us + 250ns * (num_hubs - 1) */
  260. if (num_hubs > 0)
  261. total_sel += 2100 + 250 * (num_hubs - 1);
  262. /* t4 = 250ns * num_hubs */
  263. total_sel += 250 * num_hubs;
  264. udev_lpm_params->sel = total_sel;
  265. }
  266. static void usb_set_lpm_parameters(struct usb_device *udev)
  267. {
  268. struct usb_hub *hub;
  269. unsigned int port_to_port_delay;
  270. unsigned int udev_u1_del;
  271. unsigned int udev_u2_del;
  272. unsigned int hub_u1_del;
  273. unsigned int hub_u2_del;
  274. if (!udev->lpm_capable || udev->speed < USB_SPEED_SUPER)
  275. return;
  276. hub = usb_hub_to_struct_hub(udev->parent);
  277. /* It doesn't take time to transition the roothub into U0, since it
  278. * doesn't have an upstream link.
  279. */
  280. if (!hub)
  281. return;
  282. udev_u1_del = udev->bos->ss_cap->bU1devExitLat;
  283. udev_u2_del = le16_to_cpu(udev->bos->ss_cap->bU2DevExitLat);
  284. hub_u1_del = udev->parent->bos->ss_cap->bU1devExitLat;
  285. hub_u2_del = le16_to_cpu(udev->parent->bos->ss_cap->bU2DevExitLat);
  286. usb_set_lpm_mel(udev, &udev->u1_params, udev_u1_del,
  287. hub, &udev->parent->u1_params, hub_u1_del);
  288. usb_set_lpm_mel(udev, &udev->u2_params, udev_u2_del,
  289. hub, &udev->parent->u2_params, hub_u2_del);
  290. /*
  291. * Appendix C, section C.2.2.2, says that there is a slight delay from
  292. * when the parent hub notices the downstream port is trying to
  293. * transition to U0 to when the hub initiates a U0 transition on its
  294. * upstream port. The section says the delays are tPort2PortU1EL and
  295. * tPort2PortU2EL, but it doesn't define what they are.
  296. *
  297. * The hub chapter, sections 10.4.2.4 and 10.4.2.5 seem to be talking
  298. * about the same delays. Use the maximum delay calculations from those
  299. * sections. For U1, it's tHubPort2PortExitLat, which is 1us max. For
  300. * U2, it's tHubPort2PortExitLat + U2DevExitLat - U1DevExitLat. I
  301. * assume the device exit latencies they are talking about are the hub
  302. * exit latencies.
  303. *
  304. * What do we do if the U2 exit latency is less than the U1 exit
  305. * latency? It's possible, although not likely...
  306. */
  307. port_to_port_delay = 1;
  308. usb_set_lpm_pel(udev, &udev->u1_params, udev_u1_del,
  309. hub, &udev->parent->u1_params, hub_u1_del,
  310. port_to_port_delay);
  311. if (hub_u2_del > hub_u1_del)
  312. port_to_port_delay = 1 + hub_u2_del - hub_u1_del;
  313. else
  314. port_to_port_delay = 1 + hub_u1_del;
  315. usb_set_lpm_pel(udev, &udev->u2_params, udev_u2_del,
  316. hub, &udev->parent->u2_params, hub_u2_del,
  317. port_to_port_delay);
  318. /* Now that we've got PEL, calculate SEL. */
  319. usb_set_lpm_sel(udev, &udev->u1_params);
  320. usb_set_lpm_sel(udev, &udev->u2_params);
  321. }
  322. /* USB 2.0 spec Section 11.24.4.5 */
  323. static int get_hub_descriptor(struct usb_device *hdev,
  324. struct usb_hub_descriptor *desc)
  325. {
  326. int i, ret, size;
  327. unsigned dtype;
  328. if (hub_is_superspeed(hdev)) {
  329. dtype = USB_DT_SS_HUB;
  330. size = USB_DT_SS_HUB_SIZE;
  331. } else {
  332. dtype = USB_DT_HUB;
  333. size = sizeof(struct usb_hub_descriptor);
  334. }
  335. for (i = 0; i < 3; i++) {
  336. ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
  337. USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
  338. dtype << 8, 0, desc, size,
  339. USB_CTRL_GET_TIMEOUT);
  340. if (hub_is_superspeed(hdev)) {
  341. if (ret == size)
  342. return ret;
  343. } else if (ret >= USB_DT_HUB_NONVAR_SIZE + 2) {
  344. /* Make sure we have the DeviceRemovable field. */
  345. size = USB_DT_HUB_NONVAR_SIZE + desc->bNbrPorts / 8 + 1;
  346. if (ret < size)
  347. return -EMSGSIZE;
  348. return ret;
  349. }
  350. }
  351. return -EINVAL;
  352. }
  353. /*
  354. * USB 2.0 spec Section 11.24.2.1
  355. */
  356. static int clear_hub_feature(struct usb_device *hdev, int feature)
  357. {
  358. return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  359. USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, 1000);
  360. }
  361. /*
  362. * USB 2.0 spec Section 11.24.2.2
  363. */
  364. int usb_clear_port_feature(struct usb_device *hdev, int port1, int feature)
  365. {
  366. return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  367. USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1,
  368. NULL, 0, 1000);
  369. }
  370. /*
  371. * USB 2.0 spec Section 11.24.2.13
  372. */
  373. static int set_port_feature(struct usb_device *hdev, int port1, int feature)
  374. {
  375. return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  376. USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1,
  377. NULL, 0, 1000);
  378. }
  379. static char *to_led_name(int selector)
  380. {
  381. switch (selector) {
  382. case HUB_LED_AMBER:
  383. return "amber";
  384. case HUB_LED_GREEN:
  385. return "green";
  386. case HUB_LED_OFF:
  387. return "off";
  388. case HUB_LED_AUTO:
  389. return "auto";
  390. default:
  391. return "??";
  392. }
  393. }
  394. /*
  395. * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7
  396. * for info about using port indicators
  397. */
  398. static void set_port_led(struct usb_hub *hub, int port1, int selector)
  399. {
  400. struct usb_port *port_dev = hub->ports[port1 - 1];
  401. int status;
  402. status = set_port_feature(hub->hdev, (selector << 8) | port1,
  403. USB_PORT_FEAT_INDICATOR);
  404. dev_dbg(&port_dev->dev, "indicator %s status %d\n",
  405. to_led_name(selector), status);
  406. }
  407. #define LED_CYCLE_PERIOD ((2*HZ)/3)
  408. static void led_work(struct work_struct *work)
  409. {
  410. struct usb_hub *hub =
  411. container_of(work, struct usb_hub, leds.work);
  412. struct usb_device *hdev = hub->hdev;
  413. unsigned i;
  414. unsigned changed = 0;
  415. int cursor = -1;
  416. if (hdev->state != USB_STATE_CONFIGURED || hub->quiescing)
  417. return;
  418. for (i = 0; i < hdev->maxchild; i++) {
  419. unsigned selector, mode;
  420. /* 30%-50% duty cycle */
  421. switch (hub->indicator[i]) {
  422. /* cycle marker */
  423. case INDICATOR_CYCLE:
  424. cursor = i;
  425. selector = HUB_LED_AUTO;
  426. mode = INDICATOR_AUTO;
  427. break;
  428. /* blinking green = sw attention */
  429. case INDICATOR_GREEN_BLINK:
  430. selector = HUB_LED_GREEN;
  431. mode = INDICATOR_GREEN_BLINK_OFF;
  432. break;
  433. case INDICATOR_GREEN_BLINK_OFF:
  434. selector = HUB_LED_OFF;
  435. mode = INDICATOR_GREEN_BLINK;
  436. break;
  437. /* blinking amber = hw attention */
  438. case INDICATOR_AMBER_BLINK:
  439. selector = HUB_LED_AMBER;
  440. mode = INDICATOR_AMBER_BLINK_OFF;
  441. break;
  442. case INDICATOR_AMBER_BLINK_OFF:
  443. selector = HUB_LED_OFF;
  444. mode = INDICATOR_AMBER_BLINK;
  445. break;
  446. /* blink green/amber = reserved */
  447. case INDICATOR_ALT_BLINK:
  448. selector = HUB_LED_GREEN;
  449. mode = INDICATOR_ALT_BLINK_OFF;
  450. break;
  451. case INDICATOR_ALT_BLINK_OFF:
  452. selector = HUB_LED_AMBER;
  453. mode = INDICATOR_ALT_BLINK;
  454. break;
  455. default:
  456. continue;
  457. }
  458. if (selector != HUB_LED_AUTO)
  459. changed = 1;
  460. set_port_led(hub, i + 1, selector);
  461. hub->indicator[i] = mode;
  462. }
  463. if (!changed && blinkenlights) {
  464. cursor++;
  465. cursor %= hdev->maxchild;
  466. set_port_led(hub, cursor + 1, HUB_LED_GREEN);
  467. hub->indicator[cursor] = INDICATOR_CYCLE;
  468. changed++;
  469. }
  470. if (changed)
  471. queue_delayed_work(system_power_efficient_wq,
  472. &hub->leds, LED_CYCLE_PERIOD);
  473. }
  474. /* use a short timeout for hub/port status fetches */
  475. #define USB_STS_TIMEOUT 1000
  476. #define USB_STS_RETRIES 5
  477. /*
  478. * USB 2.0 spec Section 11.24.2.6
  479. */
  480. static int get_hub_status(struct usb_device *hdev,
  481. struct usb_hub_status *data)
  482. {
  483. int i, status = -ETIMEDOUT;
  484. for (i = 0; i < USB_STS_RETRIES &&
  485. (status == -ETIMEDOUT || status == -EPIPE); i++) {
  486. status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
  487. USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
  488. data, sizeof(*data), USB_STS_TIMEOUT);
  489. }
  490. return status;
  491. }
  492. /*
  493. * USB 2.0 spec Section 11.24.2.7
  494. * USB 3.1 takes into use the wValue and wLength fields, spec Section 10.16.2.6
  495. */
  496. static int get_port_status(struct usb_device *hdev, int port1,
  497. void *data, u16 value, u16 length)
  498. {
  499. int i, status = -ETIMEDOUT;
  500. for (i = 0; i < USB_STS_RETRIES &&
  501. (status == -ETIMEDOUT || status == -EPIPE); i++) {
  502. status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
  503. USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, value,
  504. port1, data, length, USB_STS_TIMEOUT);
  505. }
  506. return status;
  507. }
  508. static int hub_ext_port_status(struct usb_hub *hub, int port1, int type,
  509. u16 *status, u16 *change, u32 *ext_status)
  510. {
  511. int ret;
  512. int len = 4;
  513. if (type != HUB_PORT_STATUS)
  514. len = 8;
  515. mutex_lock(&hub->status_mutex);
  516. ret = get_port_status(hub->hdev, port1, &hub->status->port, type, len);
  517. if (ret < len) {
  518. if (ret != -ENODEV)
  519. dev_err(hub->intfdev,
  520. "%s failed (err = %d)\n", __func__, ret);
  521. if (ret >= 0)
  522. ret = -EIO;
  523. } else {
  524. *status = le16_to_cpu(hub->status->port.wPortStatus);
  525. *change = le16_to_cpu(hub->status->port.wPortChange);
  526. if (type != HUB_PORT_STATUS && ext_status)
  527. *ext_status = le32_to_cpu(
  528. hub->status->port.dwExtPortStatus);
  529. ret = 0;
  530. }
  531. mutex_unlock(&hub->status_mutex);
  532. return ret;
  533. }
  534. static int hub_port_status(struct usb_hub *hub, int port1,
  535. u16 *status, u16 *change)
  536. {
  537. return hub_ext_port_status(hub, port1, HUB_PORT_STATUS,
  538. status, change, NULL);
  539. }
  540. static void kick_hub_wq(struct usb_hub *hub)
  541. {
  542. struct usb_interface *intf;
  543. if (hub->disconnected || work_pending(&hub->events))
  544. return;
  545. /*
  546. * Suppress autosuspend until the event is proceed.
  547. *
  548. * Be careful and make sure that the symmetric operation is
  549. * always called. We are here only when there is no pending
  550. * work for this hub. Therefore put the interface either when
  551. * the new work is called or when it is canceled.
  552. */
  553. intf = to_usb_interface(hub->intfdev);
  554. usb_autopm_get_interface_no_resume(intf);
  555. kref_get(&hub->kref);
  556. if (queue_work(hub_wq, &hub->events))
  557. return;
  558. /* the work has already been scheduled */
  559. usb_autopm_put_interface_async(intf);
  560. kref_put(&hub->kref, hub_release);
  561. }
  562. void usb_kick_hub_wq(struct usb_device *hdev)
  563. {
  564. struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
  565. if (hub)
  566. kick_hub_wq(hub);
  567. }
  568. /*
  569. * Let the USB core know that a USB 3.0 device has sent a Function Wake Device
  570. * Notification, which indicates it had initiated remote wakeup.
  571. *
  572. * USB 3.0 hubs do not report the port link state change from U3 to U0 when the
  573. * device initiates resume, so the USB core will not receive notice of the
  574. * resume through the normal hub interrupt URB.
  575. */
  576. void usb_wakeup_notification(struct usb_device *hdev,
  577. unsigned int portnum)
  578. {
  579. struct usb_hub *hub;
  580. struct usb_port *port_dev;
  581. if (!hdev)
  582. return;
  583. hub = usb_hub_to_struct_hub(hdev);
  584. if (hub) {
  585. port_dev = hub->ports[portnum - 1];
  586. if (port_dev && port_dev->child)
  587. pm_wakeup_event(&port_dev->child->dev, 0);
  588. set_bit(portnum, hub->wakeup_bits);
  589. kick_hub_wq(hub);
  590. }
  591. }
  592. EXPORT_SYMBOL_GPL(usb_wakeup_notification);
  593. /* completion function, fires on port status changes and various faults */
  594. static void hub_irq(struct urb *urb)
  595. {
  596. struct usb_hub *hub = urb->context;
  597. int status = urb->status;
  598. unsigned i;
  599. unsigned long bits;
  600. switch (status) {
  601. case -ENOENT: /* synchronous unlink */
  602. case -ECONNRESET: /* async unlink */
  603. case -ESHUTDOWN: /* hardware going away */
  604. return;
  605. default: /* presumably an error */
  606. /* Cause a hub reset after 10 consecutive errors */
  607. dev_dbg(hub->intfdev, "transfer --> %d\n", status);
  608. if ((++hub->nerrors < 10) || hub->error)
  609. goto resubmit;
  610. hub->error = status;
  611. /* FALL THROUGH */
  612. /* let hub_wq handle things */
  613. case 0: /* we got data: port status changed */
  614. bits = 0;
  615. for (i = 0; i < urb->actual_length; ++i)
  616. bits |= ((unsigned long) ((*hub->buffer)[i]))
  617. << (i*8);
  618. hub->event_bits[0] = bits;
  619. break;
  620. }
  621. hub->nerrors = 0;
  622. /* Something happened, let hub_wq figure it out */
  623. kick_hub_wq(hub);
  624. resubmit:
  625. if (hub->quiescing)
  626. return;
  627. status = usb_submit_urb(hub->urb, GFP_ATOMIC);
  628. if (status != 0 && status != -ENODEV && status != -EPERM)
  629. dev_err(hub->intfdev, "resubmit --> %d\n", status);
  630. }
  631. /* USB 2.0 spec Section 11.24.2.3 */
  632. static inline int
  633. hub_clear_tt_buffer(struct usb_device *hdev, u16 devinfo, u16 tt)
  634. {
  635. /* Need to clear both directions for control ep */
  636. if (((devinfo >> 11) & USB_ENDPOINT_XFERTYPE_MASK) ==
  637. USB_ENDPOINT_XFER_CONTROL) {
  638. int status = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  639. HUB_CLEAR_TT_BUFFER, USB_RT_PORT,
  640. devinfo ^ 0x8000, tt, NULL, 0, 1000);
  641. if (status)
  642. return status;
  643. }
  644. return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  645. HUB_CLEAR_TT_BUFFER, USB_RT_PORT, devinfo,
  646. tt, NULL, 0, 1000);
  647. }
  648. /*
  649. * enumeration blocks hub_wq for a long time. we use keventd instead, since
  650. * long blocking there is the exception, not the rule. accordingly, HCDs
  651. * talking to TTs must queue control transfers (not just bulk and iso), so
  652. * both can talk to the same hub concurrently.
  653. */
  654. static void hub_tt_work(struct work_struct *work)
  655. {
  656. struct usb_hub *hub =
  657. container_of(work, struct usb_hub, tt.clear_work);
  658. unsigned long flags;
  659. spin_lock_irqsave(&hub->tt.lock, flags);
  660. while (!list_empty(&hub->tt.clear_list)) {
  661. struct list_head *next;
  662. struct usb_tt_clear *clear;
  663. struct usb_device *hdev = hub->hdev;
  664. const struct hc_driver *drv;
  665. int status;
  666. next = hub->tt.clear_list.next;
  667. clear = list_entry(next, struct usb_tt_clear, clear_list);
  668. list_del(&clear->clear_list);
  669. /* drop lock so HCD can concurrently report other TT errors */
  670. spin_unlock_irqrestore(&hub->tt.lock, flags);
  671. status = hub_clear_tt_buffer(hdev, clear->devinfo, clear->tt);
  672. if (status && status != -ENODEV)
  673. dev_err(&hdev->dev,
  674. "clear tt %d (%04x) error %d\n",
  675. clear->tt, clear->devinfo, status);
  676. /* Tell the HCD, even if the operation failed */
  677. drv = clear->hcd->driver;
  678. if (drv->clear_tt_buffer_complete)
  679. (drv->clear_tt_buffer_complete)(clear->hcd, clear->ep);
  680. kfree(clear);
  681. spin_lock_irqsave(&hub->tt.lock, flags);
  682. }
  683. spin_unlock_irqrestore(&hub->tt.lock, flags);
  684. }
  685. /**
  686. * usb_hub_set_port_power - control hub port's power state
  687. * @hdev: USB device belonging to the usb hub
  688. * @hub: target hub
  689. * @port1: port index
  690. * @set: expected status
  691. *
  692. * call this function to control port's power via setting or
  693. * clearing the port's PORT_POWER feature.
  694. *
  695. * Return: 0 if successful. A negative error code otherwise.
  696. */
  697. int usb_hub_set_port_power(struct usb_device *hdev, struct usb_hub *hub,
  698. int port1, bool set)
  699. {
  700. int ret;
  701. if (set)
  702. ret = set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
  703. else
  704. ret = usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
  705. if (ret)
  706. return ret;
  707. if (set)
  708. set_bit(port1, hub->power_bits);
  709. else
  710. clear_bit(port1, hub->power_bits);
  711. return 0;
  712. }
  713. /**
  714. * usb_hub_clear_tt_buffer - clear control/bulk TT state in high speed hub
  715. * @urb: an URB associated with the failed or incomplete split transaction
  716. *
  717. * High speed HCDs use this to tell the hub driver that some split control or
  718. * bulk transaction failed in a way that requires clearing internal state of
  719. * a transaction translator. This is normally detected (and reported) from
  720. * interrupt context.
  721. *
  722. * It may not be possible for that hub to handle additional full (or low)
  723. * speed transactions until that state is fully cleared out.
  724. *
  725. * Return: 0 if successful. A negative error code otherwise.
  726. */
  727. int usb_hub_clear_tt_buffer(struct urb *urb)
  728. {
  729. struct usb_device *udev = urb->dev;
  730. int pipe = urb->pipe;
  731. struct usb_tt *tt = udev->tt;
  732. unsigned long flags;
  733. struct usb_tt_clear *clear;
  734. /* we've got to cope with an arbitrary number of pending TT clears,
  735. * since each TT has "at least two" buffers that can need it (and
  736. * there can be many TTs per hub). even if they're uncommon.
  737. */
  738. clear = kmalloc(sizeof *clear, GFP_ATOMIC);
  739. if (clear == NULL) {
  740. dev_err(&udev->dev, "can't save CLEAR_TT_BUFFER state\n");
  741. /* FIXME recover somehow ... RESET_TT? */
  742. return -ENOMEM;
  743. }
  744. /* info that CLEAR_TT_BUFFER needs */
  745. clear->tt = tt->multi ? udev->ttport : 1;
  746. clear->devinfo = usb_pipeendpoint (pipe);
  747. clear->devinfo |= udev->devnum << 4;
  748. clear->devinfo |= usb_pipecontrol(pipe)
  749. ? (USB_ENDPOINT_XFER_CONTROL << 11)
  750. : (USB_ENDPOINT_XFER_BULK << 11);
  751. if (usb_pipein(pipe))
  752. clear->devinfo |= 1 << 15;
  753. /* info for completion callback */
  754. clear->hcd = bus_to_hcd(udev->bus);
  755. clear->ep = urb->ep;
  756. /* tell keventd to clear state for this TT */
  757. spin_lock_irqsave(&tt->lock, flags);
  758. list_add_tail(&clear->clear_list, &tt->clear_list);
  759. schedule_work(&tt->clear_work);
  760. spin_unlock_irqrestore(&tt->lock, flags);
  761. return 0;
  762. }
  763. EXPORT_SYMBOL_GPL(usb_hub_clear_tt_buffer);
  764. static void hub_power_on(struct usb_hub *hub, bool do_delay)
  765. {
  766. int port1;
  767. /* Enable power on each port. Some hubs have reserved values
  768. * of LPSM (> 2) in their descriptors, even though they are
  769. * USB 2.0 hubs. Some hubs do not implement port-power switching
  770. * but only emulate it. In all cases, the ports won't work
  771. * unless we send these messages to the hub.
  772. */
  773. if (hub_is_port_power_switchable(hub))
  774. dev_dbg(hub->intfdev, "enabling power on all ports\n");
  775. else
  776. dev_dbg(hub->intfdev, "trying to enable port power on "
  777. "non-switchable hub\n");
  778. for (port1 = 1; port1 <= hub->hdev->maxchild; port1++)
  779. if (test_bit(port1, hub->power_bits))
  780. set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER);
  781. else
  782. usb_clear_port_feature(hub->hdev, port1,
  783. USB_PORT_FEAT_POWER);
  784. if (do_delay)
  785. msleep(hub_power_on_good_delay(hub));
  786. }
  787. static int hub_hub_status(struct usb_hub *hub,
  788. u16 *status, u16 *change)
  789. {
  790. int ret;
  791. mutex_lock(&hub->status_mutex);
  792. ret = get_hub_status(hub->hdev, &hub->status->hub);
  793. if (ret < 0) {
  794. if (ret != -ENODEV)
  795. dev_err(hub->intfdev,
  796. "%s failed (err = %d)\n", __func__, ret);
  797. } else {
  798. *status = le16_to_cpu(hub->status->hub.wHubStatus);
  799. *change = le16_to_cpu(hub->status->hub.wHubChange);
  800. ret = 0;
  801. }
  802. mutex_unlock(&hub->status_mutex);
  803. return ret;
  804. }
  805. static int hub_set_port_link_state(struct usb_hub *hub, int port1,
  806. unsigned int link_status)
  807. {
  808. return set_port_feature(hub->hdev,
  809. port1 | (link_status << 3),
  810. USB_PORT_FEAT_LINK_STATE);
  811. }
  812. /*
  813. * Disable a port and mark a logical connect-change event, so that some
  814. * time later hub_wq will disconnect() any existing usb_device on the port
  815. * and will re-enumerate if there actually is a device attached.
  816. */
  817. static void hub_port_logical_disconnect(struct usb_hub *hub, int port1)
  818. {
  819. dev_dbg(&hub->ports[port1 - 1]->dev, "logical disconnect\n");
  820. hub_port_disable(hub, port1, 1);
  821. /* FIXME let caller ask to power down the port:
  822. * - some devices won't enumerate without a VBUS power cycle
  823. * - SRP saves power that way
  824. * - ... new call, TBD ...
  825. * That's easy if this hub can switch power per-port, and
  826. * hub_wq reactivates the port later (timer, SRP, etc).
  827. * Powerdown must be optional, because of reset/DFU.
  828. */
  829. set_bit(port1, hub->change_bits);
  830. kick_hub_wq(hub);
  831. }
  832. /**
  833. * usb_remove_device - disable a device's port on its parent hub
  834. * @udev: device to be disabled and removed
  835. * Context: @udev locked, must be able to sleep.
  836. *
  837. * After @udev's port has been disabled, hub_wq is notified and it will
  838. * see that the device has been disconnected. When the device is
  839. * physically unplugged and something is plugged in, the events will
  840. * be received and processed normally.
  841. *
  842. * Return: 0 if successful. A negative error code otherwise.
  843. */
  844. int usb_remove_device(struct usb_device *udev)
  845. {
  846. struct usb_hub *hub;
  847. struct usb_interface *intf;
  848. if (!udev->parent) /* Can't remove a root hub */
  849. return -EINVAL;
  850. hub = usb_hub_to_struct_hub(udev->parent);
  851. intf = to_usb_interface(hub->intfdev);
  852. usb_autopm_get_interface(intf);
  853. set_bit(udev->portnum, hub->removed_bits);
  854. hub_port_logical_disconnect(hub, udev->portnum);
  855. usb_autopm_put_interface(intf);
  856. return 0;
  857. }
  858. enum hub_activation_type {
  859. HUB_INIT, HUB_INIT2, HUB_INIT3, /* INITs must come first */
  860. HUB_POST_RESET, HUB_RESUME, HUB_RESET_RESUME,
  861. };
  862. static void hub_init_func2(struct work_struct *ws);
  863. static void hub_init_func3(struct work_struct *ws);
  864. static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
  865. {
  866. struct usb_device *hdev = hub->hdev;
  867. struct usb_hcd *hcd;
  868. int ret;
  869. int port1;
  870. int status;
  871. bool need_debounce_delay = false;
  872. unsigned delay;
  873. /* Continue a partial initialization */
  874. if (type == HUB_INIT2 || type == HUB_INIT3) {
  875. device_lock(&hdev->dev);
  876. /* Was the hub disconnected while we were waiting? */
  877. if (hub->disconnected)
  878. goto disconnected;
  879. if (type == HUB_INIT2)
  880. goto init2;
  881. goto init3;
  882. }
  883. kref_get(&hub->kref);
  884. /* The superspeed hub except for root hub has to use Hub Depth
  885. * value as an offset into the route string to locate the bits
  886. * it uses to determine the downstream port number. So hub driver
  887. * should send a set hub depth request to superspeed hub after
  888. * the superspeed hub is set configuration in initialization or
  889. * reset procedure.
  890. *
  891. * After a resume, port power should still be on.
  892. * For any other type of activation, turn it on.
  893. */
  894. if (type != HUB_RESUME) {
  895. if (hdev->parent && hub_is_superspeed(hdev)) {
  896. ret = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  897. HUB_SET_DEPTH, USB_RT_HUB,
  898. hdev->level - 1, 0, NULL, 0,
  899. USB_CTRL_SET_TIMEOUT);
  900. if (ret < 0)
  901. dev_err(hub->intfdev,
  902. "set hub depth failed\n");
  903. }
  904. /* Speed up system boot by using a delayed_work for the
  905. * hub's initial power-up delays. This is pretty awkward
  906. * and the implementation looks like a home-brewed sort of
  907. * setjmp/longjmp, but it saves at least 100 ms for each
  908. * root hub (assuming usbcore is compiled into the kernel
  909. * rather than as a module). It adds up.
  910. *
  911. * This can't be done for HUB_RESUME or HUB_RESET_RESUME
  912. * because for those activation types the ports have to be
  913. * operational when we return. In theory this could be done
  914. * for HUB_POST_RESET, but it's easier not to.
  915. */
  916. if (type == HUB_INIT) {
  917. delay = hub_power_on_good_delay(hub);
  918. hub_power_on(hub, false);
  919. INIT_DELAYED_WORK(&hub->init_work, hub_init_func2);
  920. queue_delayed_work(system_power_efficient_wq,
  921. &hub->init_work,
  922. msecs_to_jiffies(delay));
  923. /* Suppress autosuspend until init is done */
  924. usb_autopm_get_interface_no_resume(
  925. to_usb_interface(hub->intfdev));
  926. return; /* Continues at init2: below */
  927. } else if (type == HUB_RESET_RESUME) {
  928. /* The internal host controller state for the hub device
  929. * may be gone after a host power loss on system resume.
  930. * Update the device's info so the HW knows it's a hub.
  931. */
  932. hcd = bus_to_hcd(hdev->bus);
  933. if (hcd->driver->update_hub_device) {
  934. ret = hcd->driver->update_hub_device(hcd, hdev,
  935. &hub->tt, GFP_NOIO);
  936. if (ret < 0) {
  937. dev_err(hub->intfdev,
  938. "Host not accepting hub info update\n");
  939. dev_err(hub->intfdev,
  940. "LS/FS devices and hubs may not work under this hub\n");
  941. }
  942. }
  943. hub_power_on(hub, true);
  944. } else {
  945. hub_power_on(hub, true);
  946. }
  947. }
  948. init2:
  949. /*
  950. * Check each port and set hub->change_bits to let hub_wq know
  951. * which ports need attention.
  952. */
  953. for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
  954. struct usb_port *port_dev = hub->ports[port1 - 1];
  955. struct usb_device *udev = port_dev->child;
  956. u16 portstatus, portchange;
  957. portstatus = portchange = 0;
  958. status = hub_port_status(hub, port1, &portstatus, &portchange);
  959. if (status)
  960. goto abort;
  961. if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
  962. dev_dbg(&port_dev->dev, "status %04x change %04x\n",
  963. portstatus, portchange);
  964. /*
  965. * After anything other than HUB_RESUME (i.e., initialization
  966. * or any sort of reset), every port should be disabled.
  967. * Unconnected ports should likewise be disabled (paranoia),
  968. * and so should ports for which we have no usb_device.
  969. */
  970. if ((portstatus & USB_PORT_STAT_ENABLE) && (
  971. type != HUB_RESUME ||
  972. !(portstatus & USB_PORT_STAT_CONNECTION) ||
  973. !udev ||
  974. udev->state == USB_STATE_NOTATTACHED)) {
  975. /*
  976. * USB3 protocol ports will automatically transition
  977. * to Enabled state when detect an USB3.0 device attach.
  978. * Do not disable USB3 protocol ports, just pretend
  979. * power was lost
  980. */
  981. portstatus &= ~USB_PORT_STAT_ENABLE;
  982. if (!hub_is_superspeed(hdev))
  983. usb_clear_port_feature(hdev, port1,
  984. USB_PORT_FEAT_ENABLE);
  985. }
  986. /* Clear status-change flags; we'll debounce later */
  987. if (portchange & USB_PORT_STAT_C_CONNECTION) {
  988. need_debounce_delay = true;
  989. usb_clear_port_feature(hub->hdev, port1,
  990. USB_PORT_FEAT_C_CONNECTION);
  991. }
  992. if (portchange & USB_PORT_STAT_C_ENABLE) {
  993. need_debounce_delay = true;
  994. usb_clear_port_feature(hub->hdev, port1,
  995. USB_PORT_FEAT_C_ENABLE);
  996. }
  997. if (portchange & USB_PORT_STAT_C_RESET) {
  998. need_debounce_delay = true;
  999. usb_clear_port_feature(hub->hdev, port1,
  1000. USB_PORT_FEAT_C_RESET);
  1001. }
  1002. if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
  1003. hub_is_superspeed(hub->hdev)) {
  1004. need_debounce_delay = true;
  1005. usb_clear_port_feature(hub->hdev, port1,
  1006. USB_PORT_FEAT_C_BH_PORT_RESET);
  1007. }
  1008. /* We can forget about a "removed" device when there's a
  1009. * physical disconnect or the connect status changes.
  1010. */
  1011. if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
  1012. (portchange & USB_PORT_STAT_C_CONNECTION))
  1013. clear_bit(port1, hub->removed_bits);
  1014. if (!udev || udev->state == USB_STATE_NOTATTACHED) {
  1015. /* Tell hub_wq to disconnect the device or
  1016. * check for a new connection or over current condition.
  1017. * Based on USB2.0 Spec Section 11.12.5,
  1018. * C_PORT_OVER_CURRENT could be set while
  1019. * PORT_OVER_CURRENT is not. So check for any of them.
  1020. */
  1021. if (udev || (portstatus & USB_PORT_STAT_CONNECTION) ||
  1022. (portstatus & USB_PORT_STAT_OVERCURRENT) ||
  1023. (portchange & USB_PORT_STAT_C_OVERCURRENT))
  1024. set_bit(port1, hub->change_bits);
  1025. } else if (portstatus & USB_PORT_STAT_ENABLE) {
  1026. bool port_resumed = (portstatus &
  1027. USB_PORT_STAT_LINK_STATE) ==
  1028. USB_SS_PORT_LS_U0;
  1029. /* The power session apparently survived the resume.
  1030. * If there was an overcurrent or suspend change
  1031. * (i.e., remote wakeup request), have hub_wq
  1032. * take care of it. Look at the port link state
  1033. * for USB 3.0 hubs, since they don't have a suspend
  1034. * change bit, and they don't set the port link change
  1035. * bit on device-initiated resume.
  1036. */
  1037. if (portchange || (hub_is_superspeed(hub->hdev) &&
  1038. port_resumed))
  1039. set_bit(port1, hub->change_bits);
  1040. } else if (udev->persist_enabled) {
  1041. #ifdef CONFIG_PM
  1042. udev->reset_resume = 1;
  1043. #endif
  1044. /* Don't set the change_bits when the device
  1045. * was powered off.
  1046. */
  1047. if (test_bit(port1, hub->power_bits))
  1048. set_bit(port1, hub->change_bits);
  1049. } else {
  1050. /* The power session is gone; tell hub_wq */
  1051. usb_set_device_state(udev, USB_STATE_NOTATTACHED);
  1052. set_bit(port1, hub->change_bits);
  1053. }
  1054. }
  1055. /* If no port-status-change flags were set, we don't need any
  1056. * debouncing. If flags were set we can try to debounce the
  1057. * ports all at once right now, instead of letting hub_wq do them
  1058. * one at a time later on.
  1059. *
  1060. * If any port-status changes do occur during this delay, hub_wq
  1061. * will see them later and handle them normally.
  1062. */
  1063. if (need_debounce_delay) {
  1064. delay = HUB_DEBOUNCE_STABLE;
  1065. /* Don't do a long sleep inside a workqueue routine */
  1066. if (type == HUB_INIT2) {
  1067. INIT_DELAYED_WORK(&hub->init_work, hub_init_func3);
  1068. queue_delayed_work(system_power_efficient_wq,
  1069. &hub->init_work,
  1070. msecs_to_jiffies(delay));
  1071. device_unlock(&hdev->dev);
  1072. return; /* Continues at init3: below */
  1073. } else {
  1074. msleep(delay);
  1075. }
  1076. }
  1077. init3:
  1078. hub->quiescing = 0;
  1079. status = usb_submit_urb(hub->urb, GFP_NOIO);
  1080. if (status < 0)
  1081. dev_err(hub->intfdev, "activate --> %d\n", status);
  1082. if (hub->has_indicators && blinkenlights)
  1083. queue_delayed_work(system_power_efficient_wq,
  1084. &hub->leds, LED_CYCLE_PERIOD);
  1085. /* Scan all ports that need attention */
  1086. kick_hub_wq(hub);
  1087. abort:
  1088. if (type == HUB_INIT2 || type == HUB_INIT3) {
  1089. /* Allow autosuspend if it was suppressed */
  1090. disconnected:
  1091. usb_autopm_put_interface_async(to_usb_interface(hub->intfdev));
  1092. device_unlock(&hdev->dev);
  1093. }
  1094. kref_put(&hub->kref, hub_release);
  1095. }
  1096. /* Implement the continuations for the delays above */
  1097. static void hub_init_func2(struct work_struct *ws)
  1098. {
  1099. struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
  1100. hub_activate(hub, HUB_INIT2);
  1101. }
  1102. static void hub_init_func3(struct work_struct *ws)
  1103. {
  1104. struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
  1105. hub_activate(hub, HUB_INIT3);
  1106. }
  1107. enum hub_quiescing_type {
  1108. HUB_DISCONNECT, HUB_PRE_RESET, HUB_SUSPEND
  1109. };
  1110. static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type)
  1111. {
  1112. struct usb_device *hdev = hub->hdev;
  1113. int i;
  1114. /* hub_wq and related activity won't re-trigger */
  1115. hub->quiescing = 1;
  1116. if (type != HUB_SUSPEND) {
  1117. /* Disconnect all the children */
  1118. for (i = 0; i < hdev->maxchild; ++i) {
  1119. if (hub->ports[i]->child)
  1120. usb_disconnect(&hub->ports[i]->child);
  1121. }
  1122. }
  1123. /* Stop hub_wq and related activity */
  1124. usb_kill_urb(hub->urb);
  1125. if (hub->has_indicators)
  1126. cancel_delayed_work_sync(&hub->leds);
  1127. if (hub->tt.hub)
  1128. flush_work(&hub->tt.clear_work);
  1129. }
  1130. static void hub_pm_barrier_for_all_ports(struct usb_hub *hub)
  1131. {
  1132. int i;
  1133. for (i = 0; i < hub->hdev->maxchild; ++i)
  1134. pm_runtime_barrier(&hub->ports[i]->dev);
  1135. }
  1136. /* caller has locked the hub device */
  1137. static int hub_pre_reset(struct usb_interface *intf)
  1138. {
  1139. struct usb_hub *hub = usb_get_intfdata(intf);
  1140. hub_quiesce(hub, HUB_PRE_RESET);
  1141. hub->in_reset = 1;
  1142. hub_pm_barrier_for_all_ports(hub);
  1143. return 0;
  1144. }
  1145. /* caller has locked the hub device */
  1146. static int hub_post_reset(struct usb_interface *intf)
  1147. {
  1148. struct usb_hub *hub = usb_get_intfdata(intf);
  1149. hub->in_reset = 0;
  1150. hub_pm_barrier_for_all_ports(hub);
  1151. hub_activate(hub, HUB_POST_RESET);
  1152. return 0;
  1153. }
  1154. static int hub_configure(struct usb_hub *hub,
  1155. struct usb_endpoint_descriptor *endpoint)
  1156. {
  1157. struct usb_hcd *hcd;
  1158. struct usb_device *hdev = hub->hdev;
  1159. struct device *hub_dev = hub->intfdev;
  1160. u16 hubstatus, hubchange;
  1161. u16 wHubCharacteristics;
  1162. unsigned int pipe;
  1163. int maxp, ret, i;
  1164. char *message = "out of memory";
  1165. unsigned unit_load;
  1166. unsigned full_load;
  1167. unsigned maxchild;
  1168. hub->buffer = kmalloc(sizeof(*hub->buffer), GFP_KERNEL);
  1169. if (!hub->buffer) {
  1170. ret = -ENOMEM;
  1171. goto fail;
  1172. }
  1173. hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL);
  1174. if (!hub->status) {
  1175. ret = -ENOMEM;
  1176. goto fail;
  1177. }
  1178. mutex_init(&hub->status_mutex);
  1179. hub->descriptor = kzalloc(sizeof(*hub->descriptor), GFP_KERNEL);
  1180. if (!hub->descriptor) {
  1181. ret = -ENOMEM;
  1182. goto fail;
  1183. }
  1184. /* Request the entire hub descriptor.
  1185. * hub->descriptor can handle USB_MAXCHILDREN ports,
  1186. * but a (non-SS) hub can/will return fewer bytes here.
  1187. */
  1188. ret = get_hub_descriptor(hdev, hub->descriptor);
  1189. if (ret < 0) {
  1190. message = "can't read hub descriptor";
  1191. goto fail;
  1192. }
  1193. maxchild = USB_MAXCHILDREN;
  1194. if (hub_is_superspeed(hdev))
  1195. maxchild = min_t(unsigned, maxchild, USB_SS_MAXPORTS);
  1196. if (hub->descriptor->bNbrPorts > maxchild) {
  1197. message = "hub has too many ports!";
  1198. ret = -ENODEV;
  1199. goto fail;
  1200. } else if (hub->descriptor->bNbrPorts == 0) {
  1201. message = "hub doesn't have any ports!";
  1202. ret = -ENODEV;
  1203. goto fail;
  1204. }
  1205. /*
  1206. * Accumulate wHubDelay + 40ns for every hub in the tree of devices.
  1207. * The resulting value will be used for SetIsochDelay() request.
  1208. */
  1209. if (hub_is_superspeed(hdev) || hub_is_superspeedplus(hdev)) {
  1210. u32 delay = __le16_to_cpu(hub->descriptor->u.ss.wHubDelay);
  1211. if (hdev->parent)
  1212. delay += hdev->parent->hub_delay;
  1213. delay += USB_TP_TRANSMISSION_DELAY;
  1214. hdev->hub_delay = min_t(u32, delay, USB_TP_TRANSMISSION_DELAY_MAX);
  1215. }
  1216. maxchild = hub->descriptor->bNbrPorts;
  1217. dev_info(hub_dev, "%d port%s detected\n", maxchild,
  1218. (maxchild == 1) ? "" : "s");
  1219. hub->ports = kcalloc(maxchild, sizeof(struct usb_port *), GFP_KERNEL);
  1220. if (!hub->ports) {
  1221. ret = -ENOMEM;
  1222. goto fail;
  1223. }
  1224. wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
  1225. if (hub_is_superspeed(hdev)) {
  1226. unit_load = 150;
  1227. full_load = 900;
  1228. } else {
  1229. unit_load = 100;
  1230. full_load = 500;
  1231. }
  1232. /* FIXME for USB 3.0, skip for now */
  1233. if ((wHubCharacteristics & HUB_CHAR_COMPOUND) &&
  1234. !(hub_is_superspeed(hdev))) {
  1235. char portstr[USB_MAXCHILDREN + 1];
  1236. for (i = 0; i < maxchild; i++)
  1237. portstr[i] = hub->descriptor->u.hs.DeviceRemovable
  1238. [((i + 1) / 8)] & (1 << ((i + 1) % 8))
  1239. ? 'F' : 'R';
  1240. portstr[maxchild] = 0;
  1241. dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr);
  1242. } else
  1243. dev_dbg(hub_dev, "standalone hub\n");
  1244. switch (wHubCharacteristics & HUB_CHAR_LPSM) {
  1245. case HUB_CHAR_COMMON_LPSM:
  1246. dev_dbg(hub_dev, "ganged power switching\n");
  1247. break;
  1248. case HUB_CHAR_INDV_PORT_LPSM:
  1249. dev_dbg(hub_dev, "individual port power switching\n");
  1250. break;
  1251. case HUB_CHAR_NO_LPSM:
  1252. case HUB_CHAR_LPSM:
  1253. dev_dbg(hub_dev, "no power switching (usb 1.0)\n");
  1254. break;
  1255. }
  1256. switch (wHubCharacteristics & HUB_CHAR_OCPM) {
  1257. case HUB_CHAR_COMMON_OCPM:
  1258. dev_dbg(hub_dev, "global over-current protection\n");
  1259. break;
  1260. case HUB_CHAR_INDV_PORT_OCPM:
  1261. dev_dbg(hub_dev, "individual port over-current protection\n");
  1262. break;
  1263. case HUB_CHAR_NO_OCPM:
  1264. case HUB_CHAR_OCPM:
  1265. dev_dbg(hub_dev, "no over-current protection\n");
  1266. break;
  1267. }
  1268. spin_lock_init(&hub->tt.lock);
  1269. INIT_LIST_HEAD(&hub->tt.clear_list);
  1270. INIT_WORK(&hub->tt.clear_work, hub_tt_work);
  1271. switch (hdev->descriptor.bDeviceProtocol) {
  1272. case USB_HUB_PR_FS:
  1273. break;
  1274. case USB_HUB_PR_HS_SINGLE_TT:
  1275. dev_dbg(hub_dev, "Single TT\n");
  1276. hub->tt.hub = hdev;
  1277. break;
  1278. case USB_HUB_PR_HS_MULTI_TT:
  1279. ret = usb_set_interface(hdev, 0, 1);
  1280. if (ret == 0) {
  1281. dev_dbg(hub_dev, "TT per port\n");
  1282. hub->tt.multi = 1;
  1283. } else
  1284. dev_err(hub_dev, "Using single TT (err %d)\n",
  1285. ret);
  1286. hub->tt.hub = hdev;
  1287. break;
  1288. case USB_HUB_PR_SS:
  1289. /* USB 3.0 hubs don't have a TT */
  1290. break;
  1291. default:
  1292. dev_dbg(hub_dev, "Unrecognized hub protocol %d\n",
  1293. hdev->descriptor.bDeviceProtocol);
  1294. break;
  1295. }
  1296. /* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */
  1297. switch (wHubCharacteristics & HUB_CHAR_TTTT) {
  1298. case HUB_TTTT_8_BITS:
  1299. if (hdev->descriptor.bDeviceProtocol != 0) {
  1300. hub->tt.think_time = 666;
  1301. dev_dbg(hub_dev, "TT requires at most %d "
  1302. "FS bit times (%d ns)\n",
  1303. 8, hub->tt.think_time);
  1304. }
  1305. break;
  1306. case HUB_TTTT_16_BITS:
  1307. hub->tt.think_time = 666 * 2;
  1308. dev_dbg(hub_dev, "TT requires at most %d "
  1309. "FS bit times (%d ns)\n",
  1310. 16, hub->tt.think_time);
  1311. break;
  1312. case HUB_TTTT_24_BITS:
  1313. hub->tt.think_time = 666 * 3;
  1314. dev_dbg(hub_dev, "TT requires at most %d "
  1315. "FS bit times (%d ns)\n",
  1316. 24, hub->tt.think_time);
  1317. break;
  1318. case HUB_TTTT_32_BITS:
  1319. hub->tt.think_time = 666 * 4;
  1320. dev_dbg(hub_dev, "TT requires at most %d "
  1321. "FS bit times (%d ns)\n",
  1322. 32, hub->tt.think_time);
  1323. break;
  1324. }
  1325. /* probe() zeroes hub->indicator[] */
  1326. if (wHubCharacteristics & HUB_CHAR_PORTIND) {
  1327. hub->has_indicators = 1;
  1328. dev_dbg(hub_dev, "Port indicators are supported\n");
  1329. }
  1330. dev_dbg(hub_dev, "power on to power good time: %dms\n",
  1331. hub->descriptor->bPwrOn2PwrGood * 2);
  1332. /* power budgeting mostly matters with bus-powered hubs,
  1333. * and battery-powered root hubs (may provide just 8 mA).
  1334. */
  1335. ret = usb_get_std_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus);
  1336. if (ret) {
  1337. message = "can't get hub status";
  1338. goto fail;
  1339. }
  1340. hcd = bus_to_hcd(hdev->bus);
  1341. if (hdev == hdev->bus->root_hub) {
  1342. if (hcd->power_budget > 0)
  1343. hdev->bus_mA = hcd->power_budget;
  1344. else
  1345. hdev->bus_mA = full_load * maxchild;
  1346. if (hdev->bus_mA >= full_load)
  1347. hub->mA_per_port = full_load;
  1348. else {
  1349. hub->mA_per_port = hdev->bus_mA;
  1350. hub->limited_power = 1;
  1351. }
  1352. } else if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
  1353. int remaining = hdev->bus_mA -
  1354. hub->descriptor->bHubContrCurrent;
  1355. dev_dbg(hub_dev, "hub controller current requirement: %dmA\n",
  1356. hub->descriptor->bHubContrCurrent);
  1357. hub->limited_power = 1;
  1358. if (remaining < maxchild * unit_load)
  1359. dev_warn(hub_dev,
  1360. "insufficient power available "
  1361. "to use all downstream ports\n");
  1362. hub->mA_per_port = unit_load; /* 7.2.1 */
  1363. } else { /* Self-powered external hub */
  1364. /* FIXME: What about battery-powered external hubs that
  1365. * provide less current per port? */
  1366. hub->mA_per_port = full_load;
  1367. }
  1368. if (hub->mA_per_port < full_load)
  1369. dev_dbg(hub_dev, "%umA bus power budget for each child\n",
  1370. hub->mA_per_port);
  1371. ret = hub_hub_status(hub, &hubstatus, &hubchange);
  1372. if (ret < 0) {
  1373. message = "can't get hub status";
  1374. goto fail;
  1375. }
  1376. /* local power status reports aren't always correct */
  1377. if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER)
  1378. dev_dbg(hub_dev, "local power source is %s\n",
  1379. (hubstatus & HUB_STATUS_LOCAL_POWER)
  1380. ? "lost (inactive)" : "good");
  1381. if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0)
  1382. dev_dbg(hub_dev, "%sover-current condition exists\n",
  1383. (hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no ");
  1384. /* set up the interrupt endpoint
  1385. * We use the EP's maxpacket size instead of (PORTS+1+7)/8
  1386. * bytes as USB2.0[11.12.3] says because some hubs are known
  1387. * to send more data (and thus cause overflow). For root hubs,
  1388. * maxpktsize is defined in hcd.c's fake endpoint descriptors
  1389. * to be big enough for at least USB_MAXCHILDREN ports. */
  1390. pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress);
  1391. maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe));
  1392. if (maxp > sizeof(*hub->buffer))
  1393. maxp = sizeof(*hub->buffer);
  1394. hub->urb = usb_alloc_urb(0, GFP_KERNEL);
  1395. if (!hub->urb) {
  1396. ret = -ENOMEM;
  1397. goto fail;
  1398. }
  1399. usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq,
  1400. hub, endpoint->bInterval);
  1401. /* maybe cycle the hub leds */
  1402. if (hub->has_indicators && blinkenlights)
  1403. hub->indicator[0] = INDICATOR_CYCLE;
  1404. mutex_lock(&usb_port_peer_mutex);
  1405. for (i = 0; i < maxchild; i++) {
  1406. ret = usb_hub_create_port_device(hub, i + 1);
  1407. if (ret < 0) {
  1408. dev_err(hub->intfdev,
  1409. "couldn't create port%d device.\n", i + 1);
  1410. break;
  1411. }
  1412. }
  1413. hdev->maxchild = i;
  1414. for (i = 0; i < hdev->maxchild; i++) {
  1415. struct usb_port *port_dev = hub->ports[i];
  1416. pm_runtime_put(&port_dev->dev);
  1417. }
  1418. mutex_unlock(&usb_port_peer_mutex);
  1419. if (ret < 0)
  1420. goto fail;
  1421. /* Update the HCD's internal representation of this hub before hub_wq
  1422. * starts getting port status changes for devices under the hub.
  1423. */
  1424. if (hcd->driver->update_hub_device) {
  1425. ret = hcd->driver->update_hub_device(hcd, hdev,
  1426. &hub->tt, GFP_KERNEL);
  1427. if (ret < 0) {
  1428. message = "can't update HCD hub info";
  1429. goto fail;
  1430. }
  1431. }
  1432. usb_hub_adjust_deviceremovable(hdev, hub->descriptor);
  1433. hub_activate(hub, HUB_INIT);
  1434. return 0;
  1435. fail:
  1436. dev_err(hub_dev, "config failed, %s (err %d)\n",
  1437. message, ret);
  1438. /* hub_disconnect() frees urb and descriptor */
  1439. return ret;
  1440. }
  1441. static void hub_release(struct kref *kref)
  1442. {
  1443. struct usb_hub *hub = container_of(kref, struct usb_hub, kref);
  1444. usb_put_dev(hub->hdev);
  1445. usb_put_intf(to_usb_interface(hub->intfdev));
  1446. kfree(hub);
  1447. }
  1448. static unsigned highspeed_hubs;
  1449. static void hub_disconnect(struct usb_interface *intf)
  1450. {
  1451. struct usb_hub *hub = usb_get_intfdata(intf);
  1452. struct usb_device *hdev = interface_to_usbdev(intf);
  1453. int port1;
  1454. /*
  1455. * Stop adding new hub events. We do not want to block here and thus
  1456. * will not try to remove any pending work item.
  1457. */
  1458. hub->disconnected = 1;
  1459. /* Disconnect all children and quiesce the hub */
  1460. hub->error = 0;
  1461. hub_quiesce(hub, HUB_DISCONNECT);
  1462. mutex_lock(&usb_port_peer_mutex);
  1463. /* Avoid races with recursively_mark_NOTATTACHED() */
  1464. spin_lock_irq(&device_state_lock);
  1465. port1 = hdev->maxchild;
  1466. hdev->maxchild = 0;
  1467. usb_set_intfdata(intf, NULL);
  1468. spin_unlock_irq(&device_state_lock);
  1469. for (; port1 > 0; --port1)
  1470. usb_hub_remove_port_device(hub, port1);
  1471. mutex_unlock(&usb_port_peer_mutex);
  1472. if (hub->hdev->speed == USB_SPEED_HIGH)
  1473. highspeed_hubs--;
  1474. usb_free_urb(hub->urb);
  1475. kfree(hub->ports);
  1476. kfree(hub->descriptor);
  1477. kfree(hub->status);
  1478. kfree(hub->buffer);
  1479. pm_suspend_ignore_children(&intf->dev, false);
  1480. kref_put(&hub->kref, hub_release);
  1481. }
  1482. static bool hub_descriptor_is_sane(struct usb_host_interface *desc)
  1483. {
  1484. /* Some hubs have a subclass of 1, which AFAICT according to the */
  1485. /* specs is not defined, but it works */
  1486. if (desc->desc.bInterfaceSubClass != 0 &&
  1487. desc->desc.bInterfaceSubClass != 1)
  1488. return false;
  1489. /* Multiple endpoints? What kind of mutant ninja-hub is this? */
  1490. if (desc->desc.bNumEndpoints != 1)
  1491. return false;
  1492. /* If the first endpoint is not interrupt IN, we'd better punt! */
  1493. if (!usb_endpoint_is_int_in(&desc->endpoint[0].desc))
  1494. return false;
  1495. return true;
  1496. }
  1497. static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
  1498. {
  1499. struct usb_host_interface *desc;
  1500. struct usb_device *hdev;
  1501. struct usb_hub *hub;
  1502. desc = intf->cur_altsetting;
  1503. hdev = interface_to_usbdev(intf);
  1504. /*
  1505. * Set default autosuspend delay as 0 to speedup bus suspend,
  1506. * based on the below considerations:
  1507. *
  1508. * - Unlike other drivers, the hub driver does not rely on the
  1509. * autosuspend delay to provide enough time to handle a wakeup
  1510. * event, and the submitted status URB is just to check future
  1511. * change on hub downstream ports, so it is safe to do it.
  1512. *
  1513. * - The patch might cause one or more auto supend/resume for
  1514. * below very rare devices when they are plugged into hub
  1515. * first time:
  1516. *
  1517. * devices having trouble initializing, and disconnect
  1518. * themselves from the bus and then reconnect a second
  1519. * or so later
  1520. *
  1521. * devices just for downloading firmware, and disconnects
  1522. * themselves after completing it
  1523. *
  1524. * For these quite rare devices, their drivers may change the
  1525. * autosuspend delay of their parent hub in the probe() to one
  1526. * appropriate value to avoid the subtle problem if someone
  1527. * does care it.
  1528. *
  1529. * - The patch may cause one or more auto suspend/resume on
  1530. * hub during running 'lsusb', but it is probably too
  1531. * infrequent to worry about.
  1532. *
  1533. * - Change autosuspend delay of hub can avoid unnecessary auto
  1534. * suspend timer for hub, also may decrease power consumption
  1535. * of USB bus.
  1536. *
  1537. * - If user has indicated to prevent autosuspend by passing
  1538. * usbcore.autosuspend = -1 then keep autosuspend disabled.
  1539. */
  1540. #ifdef CONFIG_PM
  1541. if (hdev->dev.power.autosuspend_delay >= 0)
  1542. pm_runtime_set_autosuspend_delay(&hdev->dev, 0);
  1543. #endif
  1544. /*
  1545. * Hubs have proper suspend/resume support, except for root hubs
  1546. * where the controller driver doesn't have bus_suspend and
  1547. * bus_resume methods.
  1548. */
  1549. if (hdev->parent) { /* normal device */
  1550. usb_enable_autosuspend(hdev);
  1551. } else { /* root hub */
  1552. const struct hc_driver *drv = bus_to_hcd(hdev->bus)->driver;
  1553. if (drv->bus_suspend && drv->bus_resume)
  1554. usb_enable_autosuspend(hdev);
  1555. }
  1556. if (hdev->level == MAX_TOPO_LEVEL) {
  1557. dev_err(&intf->dev,
  1558. "Unsupported bus topology: hub nested too deep\n");
  1559. return -E2BIG;
  1560. }
  1561. #ifdef CONFIG_USB_OTG_BLACKLIST_HUB
  1562. if (hdev->parent) {
  1563. dev_warn(&intf->dev, "ignoring external hub\n");
  1564. return -ENODEV;
  1565. }
  1566. #endif
  1567. if (!hub_descriptor_is_sane(desc)) {
  1568. dev_err(&intf->dev, "bad descriptor, ignoring hub\n");
  1569. return -EIO;
  1570. }
  1571. /* We found a hub */
  1572. dev_info(&intf->dev, "USB hub found\n");
  1573. hub = kzalloc(sizeof(*hub), GFP_KERNEL);
  1574. if (!hub)
  1575. return -ENOMEM;
  1576. kref_init(&hub->kref);
  1577. hub->intfdev = &intf->dev;
  1578. hub->hdev = hdev;
  1579. INIT_DELAYED_WORK(&hub->leds, led_work);
  1580. INIT_DELAYED_WORK(&hub->init_work, NULL);
  1581. INIT_WORK(&hub->events, hub_event);
  1582. usb_get_intf(intf);
  1583. usb_get_dev(hdev);
  1584. usb_set_intfdata(intf, hub);
  1585. intf->needs_remote_wakeup = 1;
  1586. pm_suspend_ignore_children(&intf->dev, true);
  1587. if (hdev->speed == USB_SPEED_HIGH)
  1588. highspeed_hubs++;
  1589. if (id->driver_info & HUB_QUIRK_CHECK_PORT_AUTOSUSPEND)
  1590. hub->quirk_check_port_auto_suspend = 1;
  1591. if (hub_configure(hub, &desc->endpoint[0].desc) >= 0)
  1592. return 0;
  1593. hub_disconnect(intf);
  1594. return -ENODEV;
  1595. }
  1596. static int
  1597. hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data)
  1598. {
  1599. struct usb_device *hdev = interface_to_usbdev(intf);
  1600. struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
  1601. /* assert ifno == 0 (part of hub spec) */
  1602. switch (code) {
  1603. case USBDEVFS_HUB_PORTINFO: {
  1604. struct usbdevfs_hub_portinfo *info = user_data;
  1605. int i;
  1606. spin_lock_irq(&device_state_lock);
  1607. if (hdev->devnum <= 0)
  1608. info->nports = 0;
  1609. else {
  1610. info->nports = hdev->maxchild;
  1611. for (i = 0; i < info->nports; i++) {
  1612. if (hub->ports[i]->child == NULL)
  1613. info->port[i] = 0;
  1614. else
  1615. info->port[i] =
  1616. hub->ports[i]->child->devnum;
  1617. }
  1618. }
  1619. spin_unlock_irq(&device_state_lock);
  1620. return info->nports + 1;
  1621. }
  1622. default:
  1623. return -ENOSYS;
  1624. }
  1625. }
  1626. /*
  1627. * Allow user programs to claim ports on a hub. When a device is attached
  1628. * to one of these "claimed" ports, the program will "own" the device.
  1629. */
  1630. static int find_port_owner(struct usb_device *hdev, unsigned port1,
  1631. struct usb_dev_state ***ppowner)
  1632. {
  1633. struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
  1634. if (hdev->state == USB_STATE_NOTATTACHED)
  1635. return -ENODEV;
  1636. if (port1 == 0 || port1 > hdev->maxchild)
  1637. return -EINVAL;
  1638. /* Devices not managed by the hub driver
  1639. * will always have maxchild equal to 0.
  1640. */
  1641. *ppowner = &(hub->ports[port1 - 1]->port_owner);
  1642. return 0;
  1643. }
  1644. /* In the following three functions, the caller must hold hdev's lock */
  1645. int usb_hub_claim_port(struct usb_device *hdev, unsigned port1,
  1646. struct usb_dev_state *owner)
  1647. {
  1648. int rc;
  1649. struct usb_dev_state **powner;
  1650. rc = find_port_owner(hdev, port1, &powner);
  1651. if (rc)
  1652. return rc;
  1653. if (*powner)
  1654. return -EBUSY;
  1655. *powner = owner;
  1656. return rc;
  1657. }
  1658. EXPORT_SYMBOL_GPL(usb_hub_claim_port);
  1659. int usb_hub_release_port(struct usb_device *hdev, unsigned port1,
  1660. struct usb_dev_state *owner)
  1661. {
  1662. int rc;
  1663. struct usb_dev_state **powner;
  1664. rc = find_port_owner(hdev, port1, &powner);
  1665. if (rc)
  1666. return rc;
  1667. if (*powner != owner)
  1668. return -ENOENT;
  1669. *powner = NULL;
  1670. return rc;
  1671. }
  1672. EXPORT_SYMBOL_GPL(usb_hub_release_port);
  1673. void usb_hub_release_all_ports(struct usb_device *hdev, struct usb_dev_state *owner)
  1674. {
  1675. struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
  1676. int n;
  1677. for (n = 0; n < hdev->maxchild; n++) {
  1678. if (hub->ports[n]->port_owner == owner)
  1679. hub->ports[n]->port_owner = NULL;
  1680. }
  1681. }
  1682. /* The caller must hold udev's lock */
  1683. bool usb_device_is_owned(struct usb_device *udev)
  1684. {
  1685. struct usb_hub *hub;
  1686. if (udev->state == USB_STATE_NOTATTACHED || !udev->parent)
  1687. return false;
  1688. hub = usb_hub_to_struct_hub(udev->parent);
  1689. return !!hub->ports[udev->portnum - 1]->port_owner;
  1690. }
  1691. static void recursively_mark_NOTATTACHED(struct usb_device *udev)
  1692. {
  1693. struct usb_hub *hub = usb_hub_to_struct_hub(udev);
  1694. int i;
  1695. for (i = 0; i < udev->maxchild; ++i) {
  1696. if (hub->ports[i]->child)
  1697. recursively_mark_NOTATTACHED(hub->ports[i]->child);
  1698. }
  1699. if (udev->state == USB_STATE_SUSPENDED)
  1700. udev->active_duration -= jiffies;
  1701. udev->state = USB_STATE_NOTATTACHED;
  1702. }
  1703. /**
  1704. * usb_set_device_state - change a device's current state (usbcore, hcds)
  1705. * @udev: pointer to device whose state should be changed
  1706. * @new_state: new state value to be stored
  1707. *
  1708. * udev->state is _not_ fully protected by the device lock. Although
  1709. * most transitions are made only while holding the lock, the state can
  1710. * can change to USB_STATE_NOTATTACHED at almost any time. This
  1711. * is so that devices can be marked as disconnected as soon as possible,
  1712. * without having to wait for any semaphores to be released. As a result,
  1713. * all changes to any device's state must be protected by the
  1714. * device_state_lock spinlock.
  1715. *
  1716. * Once a device has been added to the device tree, all changes to its state
  1717. * should be made using this routine. The state should _not_ be set directly.
  1718. *
  1719. * If udev->state is already USB_STATE_NOTATTACHED then no change is made.
  1720. * Otherwise udev->state is set to new_state, and if new_state is
  1721. * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set
  1722. * to USB_STATE_NOTATTACHED.
  1723. */
  1724. void usb_set_device_state(struct usb_device *udev,
  1725. enum usb_device_state new_state)
  1726. {
  1727. unsigned long flags;
  1728. int wakeup = -1;
  1729. spin_lock_irqsave(&device_state_lock, flags);
  1730. if (udev->state == USB_STATE_NOTATTACHED)
  1731. ; /* do nothing */
  1732. else if (new_state != USB_STATE_NOTATTACHED) {
  1733. /* root hub wakeup capabilities are managed out-of-band
  1734. * and may involve silicon errata ... ignore them here.
  1735. */
  1736. if (udev->parent) {
  1737. if (udev->state == USB_STATE_SUSPENDED
  1738. || new_state == USB_STATE_SUSPENDED)
  1739. ; /* No change to wakeup settings */
  1740. else if (new_state == USB_STATE_CONFIGURED)
  1741. wakeup = (udev->quirks &
  1742. USB_QUIRK_IGNORE_REMOTE_WAKEUP) ? 0 :
  1743. udev->actconfig->desc.bmAttributes &
  1744. USB_CONFIG_ATT_WAKEUP;
  1745. else
  1746. wakeup = 0;
  1747. }
  1748. if (udev->state == USB_STATE_SUSPENDED &&
  1749. new_state != USB_STATE_SUSPENDED)
  1750. udev->active_duration -= jiffies;
  1751. else if (new_state == USB_STATE_SUSPENDED &&
  1752. udev->state != USB_STATE_SUSPENDED)
  1753. udev->active_duration += jiffies;
  1754. udev->state = new_state;
  1755. } else
  1756. recursively_mark_NOTATTACHED(udev);
  1757. spin_unlock_irqrestore(&device_state_lock, flags);
  1758. if (wakeup >= 0)
  1759. device_set_wakeup_capable(&udev->dev, wakeup);
  1760. }
  1761. EXPORT_SYMBOL_GPL(usb_set_device_state);
  1762. /*
  1763. * Choose a device number.
  1764. *
  1765. * Device numbers are used as filenames in usbfs. On USB-1.1 and
  1766. * USB-2.0 buses they are also used as device addresses, however on
  1767. * USB-3.0 buses the address is assigned by the controller hardware
  1768. * and it usually is not the same as the device number.
  1769. *
  1770. * WUSB devices are simple: they have no hubs behind, so the mapping
  1771. * device <-> virtual port number becomes 1:1. Why? to simplify the
  1772. * life of the device connection logic in
  1773. * drivers/usb/wusbcore/devconnect.c. When we do the initial secret
  1774. * handshake we need to assign a temporary address in the unauthorized
  1775. * space. For simplicity we use the first virtual port number found to
  1776. * be free [drivers/usb/wusbcore/devconnect.c:wusbhc_devconnect_ack()]
  1777. * and that becomes it's address [X < 128] or its unauthorized address
  1778. * [X | 0x80].
  1779. *
  1780. * We add 1 as an offset to the one-based USB-stack port number
  1781. * (zero-based wusb virtual port index) for two reasons: (a) dev addr
  1782. * 0 is reserved by USB for default address; (b) Linux's USB stack
  1783. * uses always #1 for the root hub of the controller. So USB stack's
  1784. * port #1, which is wusb virtual-port #0 has address #2.
  1785. *
  1786. * Devices connected under xHCI are not as simple. The host controller
  1787. * supports virtualization, so the hardware assigns device addresses and
  1788. * the HCD must setup data structures before issuing a set address
  1789. * command to the hardware.
  1790. */
  1791. static void choose_devnum(struct usb_device *udev)
  1792. {
  1793. int devnum;
  1794. struct usb_bus *bus = udev->bus;
  1795. /* be safe when more hub events are proceed in parallel */
  1796. mutex_lock(&bus->devnum_next_mutex);
  1797. if (udev->wusb) {
  1798. devnum = udev->portnum + 1;
  1799. BUG_ON(test_bit(devnum, bus->devmap.devicemap));
  1800. } else {
  1801. /* Try to allocate the next devnum beginning at
  1802. * bus->devnum_next. */
  1803. devnum = find_next_zero_bit(bus->devmap.devicemap, 128,
  1804. bus->devnum_next);
  1805. if (devnum >= 128)
  1806. devnum = find_next_zero_bit(bus->devmap.devicemap,
  1807. 128, 1);
  1808. bus->devnum_next = (devnum >= 127 ? 1 : devnum + 1);
  1809. }
  1810. if (devnum < 128) {
  1811. set_bit(devnum, bus->devmap.devicemap);
  1812. udev->devnum = devnum;
  1813. }
  1814. mutex_unlock(&bus->devnum_next_mutex);
  1815. }
  1816. static void release_devnum(struct usb_device *udev)
  1817. {
  1818. if (udev->devnum > 0) {
  1819. clear_bit(udev->devnum, udev->bus->devmap.devicemap);
  1820. udev->devnum = -1;
  1821. }
  1822. }
  1823. static void update_devnum(struct usb_device *udev, int devnum)
  1824. {
  1825. /* The address for a WUSB device is managed by wusbcore. */
  1826. if (!udev->wusb)
  1827. udev->devnum = devnum;
  1828. }
  1829. static void hub_free_dev(struct usb_device *udev)
  1830. {
  1831. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  1832. /* Root hubs aren't real devices, so don't free HCD resources */
  1833. if (hcd->driver->free_dev && udev->parent)
  1834. hcd->driver->free_dev(hcd, udev);
  1835. }
  1836. static void hub_disconnect_children(struct usb_device *udev)
  1837. {
  1838. struct usb_hub *hub = usb_hub_to_struct_hub(udev);
  1839. int i;
  1840. /* Free up all the children before we remove this device */
  1841. for (i = 0; i < udev->maxchild; i++) {
  1842. if (hub->ports[i]->child)
  1843. usb_disconnect(&hub->ports[i]->child);
  1844. }
  1845. }
  1846. /**
  1847. * usb_disconnect - disconnect a device (usbcore-internal)
  1848. * @pdev: pointer to device being disconnected
  1849. * Context: !in_interrupt ()
  1850. *
  1851. * Something got disconnected. Get rid of it and all of its children.
  1852. *
  1853. * If *pdev is a normal device then the parent hub must already be locked.
  1854. * If *pdev is a root hub then the caller must hold the usb_bus_idr_lock,
  1855. * which protects the set of root hubs as well as the list of buses.
  1856. *
  1857. * Only hub drivers (including virtual root hub drivers for host
  1858. * controllers) should ever call this.
  1859. *
  1860. * This call is synchronous, and may not be used in an interrupt context.
  1861. */
  1862. void usb_disconnect(struct usb_device **pdev)
  1863. {
  1864. struct usb_port *port_dev = NULL;
  1865. struct usb_device *udev = *pdev;
  1866. struct usb_hub *hub = NULL;
  1867. int port1 = 1;
  1868. /* mark the device as inactive, so any further urb submissions for
  1869. * this device (and any of its children) will fail immediately.
  1870. * this quiesces everything except pending urbs.
  1871. */
  1872. usb_set_device_state(udev, USB_STATE_NOTATTACHED);
  1873. dev_info(&udev->dev, "USB disconnect, device number %d\n",
  1874. udev->devnum);
  1875. /*
  1876. * Ensure that the pm runtime code knows that the USB device
  1877. * is in the process of being disconnected.
  1878. */
  1879. pm_runtime_barrier(&udev->dev);
  1880. usb_lock_device(udev);
  1881. hub_disconnect_children(udev);
  1882. /* deallocate hcd/hardware state ... nuking all pending urbs and
  1883. * cleaning up all state associated with the current configuration
  1884. * so that the hardware is now fully quiesced.
  1885. */
  1886. dev_dbg(&udev->dev, "unregistering device\n");
  1887. usb_disable_device(udev, 0);
  1888. usb_hcd_synchronize_unlinks(udev);
  1889. if (udev->parent) {
  1890. port1 = udev->portnum;
  1891. hub = usb_hub_to_struct_hub(udev->parent);
  1892. port_dev = hub->ports[port1 - 1];
  1893. sysfs_remove_link(&udev->dev.kobj, "port");
  1894. sysfs_remove_link(&port_dev->dev.kobj, "device");
  1895. /*
  1896. * As usb_port_runtime_resume() de-references udev, make
  1897. * sure no resumes occur during removal
  1898. */
  1899. if (!test_and_set_bit(port1, hub->child_usage_bits))
  1900. pm_runtime_get_sync(&port_dev->dev);
  1901. }
  1902. usb_remove_ep_devs(&udev->ep0);
  1903. usb_unlock_device(udev);
  1904. /* Unregister the device. The device driver is responsible
  1905. * for de-configuring the device and invoking the remove-device
  1906. * notifier chain (used by usbfs and possibly others).
  1907. */
  1908. device_del(&udev->dev);
  1909. /* Free the device number and delete the parent's children[]
  1910. * (or root_hub) pointer.
  1911. */
  1912. release_devnum(udev);
  1913. /* Avoid races with recursively_mark_NOTATTACHED() */
  1914. spin_lock_irq(&device_state_lock);
  1915. *pdev = NULL;
  1916. spin_unlock_irq(&device_state_lock);
  1917. if (port_dev && test_and_clear_bit(port1, hub->child_usage_bits))
  1918. pm_runtime_put(&port_dev->dev);
  1919. hub_free_dev(udev);
  1920. put_device(&udev->dev);
  1921. }
  1922. #ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES
  1923. static void show_string(struct usb_device *udev, char *id, char *string)
  1924. {
  1925. if (!string)
  1926. return;
  1927. dev_info(&udev->dev, "%s: %s\n", id, string);
  1928. }
  1929. static void announce_device(struct usb_device *udev)
  1930. {
  1931. u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
  1932. dev_info(&udev->dev,
  1933. "New USB device found, idVendor=%04x, idProduct=%04x, bcdDevice=%2x.%02x\n",
  1934. le16_to_cpu(udev->descriptor.idVendor),
  1935. le16_to_cpu(udev->descriptor.idProduct),
  1936. bcdDevice >> 8, bcdDevice & 0xff);
  1937. dev_info(&udev->dev,
  1938. "New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
  1939. udev->descriptor.iManufacturer,
  1940. udev->descriptor.iProduct,
  1941. udev->descriptor.iSerialNumber);
  1942. show_string(udev, "Product", udev->product);
  1943. show_string(udev, "Manufacturer", udev->manufacturer);
  1944. show_string(udev, "SerialNumber", udev->serial);
  1945. }
  1946. #else
  1947. static inline void announce_device(struct usb_device *udev) { }
  1948. #endif
  1949. /**
  1950. * usb_enumerate_device_otg - FIXME (usbcore-internal)
  1951. * @udev: newly addressed device (in ADDRESS state)
  1952. *
  1953. * Finish enumeration for On-The-Go devices
  1954. *
  1955. * Return: 0 if successful. A negative error code otherwise.
  1956. */
  1957. static int usb_enumerate_device_otg(struct usb_device *udev)
  1958. {
  1959. int err = 0;
  1960. #ifdef CONFIG_USB_OTG
  1961. /*
  1962. * OTG-aware devices on OTG-capable root hubs may be able to use SRP,
  1963. * to wake us after we've powered off VBUS; and HNP, switching roles
  1964. * "host" to "peripheral". The OTG descriptor helps figure this out.
  1965. */
  1966. if (!udev->bus->is_b_host
  1967. && udev->config
  1968. && udev->parent == udev->bus->root_hub) {
  1969. struct usb_otg_descriptor *desc = NULL;
  1970. struct usb_bus *bus = udev->bus;
  1971. unsigned port1 = udev->portnum;
  1972. /* descriptor may appear anywhere in config */
  1973. err = __usb_get_extra_descriptor(udev->rawdescriptors[0],
  1974. le16_to_cpu(udev->config[0].desc.wTotalLength),
  1975. USB_DT_OTG, (void **) &desc, sizeof(*desc));
  1976. if (err || !(desc->bmAttributes & USB_OTG_HNP))
  1977. return 0;
  1978. dev_info(&udev->dev, "Dual-Role OTG device on %sHNP port\n",
  1979. (port1 == bus->otg_port) ? "" : "non-");
  1980. /* enable HNP before suspend, it's simpler */
  1981. if (port1 == bus->otg_port) {
  1982. bus->b_hnp_enable = 1;
  1983. err = usb_control_msg(udev,
  1984. usb_sndctrlpipe(udev, 0),
  1985. USB_REQ_SET_FEATURE, 0,
  1986. USB_DEVICE_B_HNP_ENABLE,
  1987. 0, NULL, 0,
  1988. USB_CTRL_SET_TIMEOUT);
  1989. if (err < 0) {
  1990. /*
  1991. * OTG MESSAGE: report errors here,
  1992. * customize to match your product.
  1993. */
  1994. dev_err(&udev->dev, "can't set HNP mode: %d\n",
  1995. err);
  1996. bus->b_hnp_enable = 0;
  1997. }
  1998. } else if (desc->bLength == sizeof
  1999. (struct usb_otg_descriptor)) {
  2000. /* Set a_alt_hnp_support for legacy otg device */
  2001. err = usb_control_msg(udev,
  2002. usb_sndctrlpipe(udev, 0),
  2003. USB_REQ_SET_FEATURE, 0,
  2004. USB_DEVICE_A_ALT_HNP_SUPPORT,
  2005. 0, NULL, 0,
  2006. USB_CTRL_SET_TIMEOUT);
  2007. if (err < 0)
  2008. dev_err(&udev->dev,
  2009. "set a_alt_hnp_support failed: %d\n",
  2010. err);
  2011. }
  2012. }
  2013. #endif
  2014. return err;
  2015. }
  2016. /**
  2017. * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal)
  2018. * @udev: newly addressed device (in ADDRESS state)
  2019. *
  2020. * This is only called by usb_new_device() and usb_authorize_device()
  2021. * and FIXME -- all comments that apply to them apply here wrt to
  2022. * environment.
  2023. *
  2024. * If the device is WUSB and not authorized, we don't attempt to read
  2025. * the string descriptors, as they will be errored out by the device
  2026. * until it has been authorized.
  2027. *
  2028. * Return: 0 if successful. A negative error code otherwise.
  2029. */
  2030. static int usb_enumerate_device(struct usb_device *udev)
  2031. {
  2032. int err;
  2033. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  2034. if (udev->config == NULL) {
  2035. err = usb_get_configuration(udev);
  2036. if (err < 0) {
  2037. if (err != -ENODEV)
  2038. dev_err(&udev->dev, "can't read configurations, error %d\n",
  2039. err);
  2040. return err;
  2041. }
  2042. }
  2043. /* read the standard strings and cache them if present */
  2044. udev->product = usb_cache_string(udev, udev->descriptor.iProduct);
  2045. udev->manufacturer = usb_cache_string(udev,
  2046. udev->descriptor.iManufacturer);
  2047. udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber);
  2048. err = usb_enumerate_device_otg(udev);
  2049. if (err < 0)
  2050. return err;
  2051. if (IS_ENABLED(CONFIG_USB_OTG_WHITELIST) && hcd->tpl_support &&
  2052. !is_targeted(udev)) {
  2053. /* Maybe it can talk to us, though we can't talk to it.
  2054. * (Includes HNP test device.)
  2055. */
  2056. if (IS_ENABLED(CONFIG_USB_OTG) && (udev->bus->b_hnp_enable
  2057. || udev->bus->is_b_host)) {
  2058. err = usb_port_suspend(udev, PMSG_AUTO_SUSPEND);
  2059. if (err < 0)
  2060. dev_dbg(&udev->dev, "HNP fail, %d\n", err);
  2061. }
  2062. return -ENOTSUPP;
  2063. }
  2064. usb_detect_interface_quirks(udev);
  2065. return 0;
  2066. }
  2067. static void set_usb_port_removable(struct usb_device *udev)
  2068. {
  2069. struct usb_device *hdev = udev->parent;
  2070. struct usb_hub *hub;
  2071. u8 port = udev->portnum;
  2072. u16 wHubCharacteristics;
  2073. bool removable = true;
  2074. if (!hdev)
  2075. return;
  2076. hub = usb_hub_to_struct_hub(udev->parent);
  2077. /*
  2078. * If the platform firmware has provided information about a port,
  2079. * use that to determine whether it's removable.
  2080. */
  2081. switch (hub->ports[udev->portnum - 1]->connect_type) {
  2082. case USB_PORT_CONNECT_TYPE_HOT_PLUG:
  2083. udev->removable = USB_DEVICE_REMOVABLE;
  2084. return;
  2085. case USB_PORT_CONNECT_TYPE_HARD_WIRED:
  2086. case USB_PORT_NOT_USED:
  2087. udev->removable = USB_DEVICE_FIXED;
  2088. return;
  2089. default:
  2090. break;
  2091. }
  2092. /*
  2093. * Otherwise, check whether the hub knows whether a port is removable
  2094. * or not
  2095. */
  2096. wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
  2097. if (!(wHubCharacteristics & HUB_CHAR_COMPOUND))
  2098. return;
  2099. if (hub_is_superspeed(hdev)) {
  2100. if (le16_to_cpu(hub->descriptor->u.ss.DeviceRemovable)
  2101. & (1 << port))
  2102. removable = false;
  2103. } else {
  2104. if (hub->descriptor->u.hs.DeviceRemovable[port / 8] & (1 << (port % 8)))
  2105. removable = false;
  2106. }
  2107. if (removable)
  2108. udev->removable = USB_DEVICE_REMOVABLE;
  2109. else
  2110. udev->removable = USB_DEVICE_FIXED;
  2111. }
  2112. /**
  2113. * usb_new_device - perform initial device setup (usbcore-internal)
  2114. * @udev: newly addressed device (in ADDRESS state)
  2115. *
  2116. * This is called with devices which have been detected but not fully
  2117. * enumerated. The device descriptor is available, but not descriptors
  2118. * for any device configuration. The caller must have locked either
  2119. * the parent hub (if udev is a normal device) or else the
  2120. * usb_bus_idr_lock (if udev is a root hub). The parent's pointer to
  2121. * udev has already been installed, but udev is not yet visible through
  2122. * sysfs or other filesystem code.
  2123. *
  2124. * This call is synchronous, and may not be used in an interrupt context.
  2125. *
  2126. * Only the hub driver or root-hub registrar should ever call this.
  2127. *
  2128. * Return: Whether the device is configured properly or not. Zero if the
  2129. * interface was registered with the driver core; else a negative errno
  2130. * value.
  2131. *
  2132. */
  2133. int usb_new_device(struct usb_device *udev)
  2134. {
  2135. int err;
  2136. if (udev->parent) {
  2137. /* Initialize non-root-hub device wakeup to disabled;
  2138. * device (un)configuration controls wakeup capable
  2139. * sysfs power/wakeup controls wakeup enabled/disabled
  2140. */
  2141. device_init_wakeup(&udev->dev, 0);
  2142. }
  2143. /* Tell the runtime-PM framework the device is active */
  2144. pm_runtime_set_active(&udev->dev);
  2145. pm_runtime_get_noresume(&udev->dev);
  2146. pm_runtime_use_autosuspend(&udev->dev);
  2147. pm_runtime_enable(&udev->dev);
  2148. /* By default, forbid autosuspend for all devices. It will be
  2149. * allowed for hubs during binding.
  2150. */
  2151. usb_disable_autosuspend(udev);
  2152. err = usb_enumerate_device(udev); /* Read descriptors */
  2153. if (err < 0)
  2154. goto fail;
  2155. dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n",
  2156. udev->devnum, udev->bus->busnum,
  2157. (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
  2158. /* export the usbdev device-node for libusb */
  2159. udev->dev.devt = MKDEV(USB_DEVICE_MAJOR,
  2160. (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
  2161. /* Tell the world! */
  2162. announce_device(udev);
  2163. if (udev->serial)
  2164. add_device_randomness(udev->serial, strlen(udev->serial));
  2165. if (udev->product)
  2166. add_device_randomness(udev->product, strlen(udev->product));
  2167. if (udev->manufacturer)
  2168. add_device_randomness(udev->manufacturer,
  2169. strlen(udev->manufacturer));
  2170. device_enable_async_suspend(&udev->dev);
  2171. /* check whether the hub or firmware marks this port as non-removable */
  2172. if (udev->parent)
  2173. set_usb_port_removable(udev);
  2174. /* Register the device. The device driver is responsible
  2175. * for configuring the device and invoking the add-device
  2176. * notifier chain (used by usbfs and possibly others).
  2177. */
  2178. err = device_add(&udev->dev);
  2179. if (err) {
  2180. dev_err(&udev->dev, "can't device_add, error %d\n", err);
  2181. goto fail;
  2182. }
  2183. /* Create link files between child device and usb port device. */
  2184. if (udev->parent) {
  2185. struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
  2186. int port1 = udev->portnum;
  2187. struct usb_port *port_dev = hub->ports[port1 - 1];
  2188. err = sysfs_create_link(&udev->dev.kobj,
  2189. &port_dev->dev.kobj, "port");
  2190. if (err)
  2191. goto fail;
  2192. err = sysfs_create_link(&port_dev->dev.kobj,
  2193. &udev->dev.kobj, "device");
  2194. if (err) {
  2195. sysfs_remove_link(&udev->dev.kobj, "port");
  2196. goto fail;
  2197. }
  2198. if (!test_and_set_bit(port1, hub->child_usage_bits))
  2199. pm_runtime_get_sync(&port_dev->dev);
  2200. }
  2201. (void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev);
  2202. usb_mark_last_busy(udev);
  2203. pm_runtime_put_sync_autosuspend(&udev->dev);
  2204. return err;
  2205. fail:
  2206. usb_set_device_state(udev, USB_STATE_NOTATTACHED);
  2207. pm_runtime_disable(&udev->dev);
  2208. pm_runtime_set_suspended(&udev->dev);
  2209. return err;
  2210. }
  2211. /**
  2212. * usb_deauthorize_device - deauthorize a device (usbcore-internal)
  2213. * @usb_dev: USB device
  2214. *
  2215. * Move the USB device to a very basic state where interfaces are disabled
  2216. * and the device is in fact unconfigured and unusable.
  2217. *
  2218. * We share a lock (that we have) with device_del(), so we need to
  2219. * defer its call.
  2220. *
  2221. * Return: 0.
  2222. */
  2223. int usb_deauthorize_device(struct usb_device *usb_dev)
  2224. {
  2225. usb_lock_device(usb_dev);
  2226. if (usb_dev->authorized == 0)
  2227. goto out_unauthorized;
  2228. usb_dev->authorized = 0;
  2229. usb_set_configuration(usb_dev, -1);
  2230. out_unauthorized:
  2231. usb_unlock_device(usb_dev);
  2232. return 0;
  2233. }
  2234. int usb_authorize_device(struct usb_device *usb_dev)
  2235. {
  2236. int result = 0, c;
  2237. usb_lock_device(usb_dev);
  2238. if (usb_dev->authorized == 1)
  2239. goto out_authorized;
  2240. result = usb_autoresume_device(usb_dev);
  2241. if (result < 0) {
  2242. dev_err(&usb_dev->dev,
  2243. "can't autoresume for authorization: %d\n", result);
  2244. goto error_autoresume;
  2245. }
  2246. if (usb_dev->wusb) {
  2247. result = usb_get_device_descriptor(usb_dev, sizeof(usb_dev->descriptor));
  2248. if (result < 0) {
  2249. dev_err(&usb_dev->dev, "can't re-read device descriptor for "
  2250. "authorization: %d\n", result);
  2251. goto error_device_descriptor;
  2252. }
  2253. }
  2254. usb_dev->authorized = 1;
  2255. /* Choose and set the configuration. This registers the interfaces
  2256. * with the driver core and lets interface drivers bind to them.
  2257. */
  2258. c = usb_choose_configuration(usb_dev);
  2259. if (c >= 0) {
  2260. result = usb_set_configuration(usb_dev, c);
  2261. if (result) {
  2262. dev_err(&usb_dev->dev,
  2263. "can't set config #%d, error %d\n", c, result);
  2264. /* This need not be fatal. The user can try to
  2265. * set other configurations. */
  2266. }
  2267. }
  2268. dev_info(&usb_dev->dev, "authorized to connect\n");
  2269. error_device_descriptor:
  2270. usb_autosuspend_device(usb_dev);
  2271. error_autoresume:
  2272. out_authorized:
  2273. usb_unlock_device(usb_dev); /* complements locktree */
  2274. return result;
  2275. }
  2276. /*
  2277. * Return 1 if port speed is SuperSpeedPlus, 0 otherwise
  2278. * check it from the link protocol field of the current speed ID attribute.
  2279. * current speed ID is got from ext port status request. Sublink speed attribute
  2280. * table is returned with the hub BOS SSP device capability descriptor
  2281. */
  2282. static int port_speed_is_ssp(struct usb_device *hdev, int speed_id)
  2283. {
  2284. int ssa_count;
  2285. u32 ss_attr;
  2286. int i;
  2287. struct usb_ssp_cap_descriptor *ssp_cap = hdev->bos->ssp_cap;
  2288. if (!ssp_cap)
  2289. return 0;
  2290. ssa_count = le32_to_cpu(ssp_cap->bmAttributes) &
  2291. USB_SSP_SUBLINK_SPEED_ATTRIBS;
  2292. for (i = 0; i <= ssa_count; i++) {
  2293. ss_attr = le32_to_cpu(ssp_cap->bmSublinkSpeedAttr[i]);
  2294. if (speed_id == (ss_attr & USB_SSP_SUBLINK_SPEED_SSID))
  2295. return !!(ss_attr & USB_SSP_SUBLINK_SPEED_LP);
  2296. }
  2297. return 0;
  2298. }
  2299. /* Returns 1 if @hub is a WUSB root hub, 0 otherwise */
  2300. static unsigned hub_is_wusb(struct usb_hub *hub)
  2301. {
  2302. struct usb_hcd *hcd;
  2303. if (hub->hdev->parent != NULL) /* not a root hub? */
  2304. return 0;
  2305. hcd = bus_to_hcd(hub->hdev->bus);
  2306. return hcd->wireless;
  2307. }
  2308. #define PORT_RESET_TRIES 5
  2309. #define SET_ADDRESS_TRIES 2
  2310. #define GET_DESCRIPTOR_TRIES 2
  2311. #define SET_CONFIG_TRIES (2 * (use_both_schemes + 1))
  2312. #define USE_NEW_SCHEME(i, scheme) ((i) / 2 == (int)scheme)
  2313. #define HUB_ROOT_RESET_TIME 60 /* times are in msec */
  2314. #define HUB_SHORT_RESET_TIME 10
  2315. #define HUB_BH_RESET_TIME 50
  2316. #define HUB_LONG_RESET_TIME 200
  2317. #define HUB_RESET_TIMEOUT 800
  2318. /*
  2319. * "New scheme" enumeration causes an extra state transition to be
  2320. * exposed to an xhci host and causes USB3 devices to receive control
  2321. * commands in the default state. This has been seen to cause
  2322. * enumeration failures, so disable this enumeration scheme for USB3
  2323. * devices.
  2324. */
  2325. static bool use_new_scheme(struct usb_device *udev, int retry,
  2326. struct usb_port *port_dev)
  2327. {
  2328. int old_scheme_first_port =
  2329. port_dev->quirks & USB_PORT_QUIRK_OLD_SCHEME;
  2330. int quick_enumeration = (udev->speed == USB_SPEED_HIGH);
  2331. if (udev->speed >= USB_SPEED_SUPER)
  2332. return false;
  2333. return USE_NEW_SCHEME(retry, old_scheme_first_port || old_scheme_first
  2334. || quick_enumeration);
  2335. }
  2336. /* Is a USB 3.0 port in the Inactive or Compliance Mode state?
  2337. * Port worm reset is required to recover
  2338. */
  2339. static bool hub_port_warm_reset_required(struct usb_hub *hub, int port1,
  2340. u16 portstatus)
  2341. {
  2342. u16 link_state;
  2343. if (!hub_is_superspeed(hub->hdev))
  2344. return false;
  2345. if (test_bit(port1, hub->warm_reset_bits))
  2346. return true;
  2347. link_state = portstatus & USB_PORT_STAT_LINK_STATE;
  2348. return link_state == USB_SS_PORT_LS_SS_INACTIVE
  2349. || link_state == USB_SS_PORT_LS_COMP_MOD;
  2350. }
  2351. static int hub_port_wait_reset(struct usb_hub *hub, int port1,
  2352. struct usb_device *udev, unsigned int delay, bool warm)
  2353. {
  2354. int delay_time, ret;
  2355. u16 portstatus;
  2356. u16 portchange;
  2357. u32 ext_portstatus = 0;
  2358. for (delay_time = 0;
  2359. delay_time < HUB_RESET_TIMEOUT;
  2360. delay_time += delay) {
  2361. /* wait to give the device a chance to reset */
  2362. msleep(delay);
  2363. /* read and decode port status */
  2364. if (hub_is_superspeedplus(hub->hdev))
  2365. ret = hub_ext_port_status(hub, port1,
  2366. HUB_EXT_PORT_STATUS,
  2367. &portstatus, &portchange,
  2368. &ext_portstatus);
  2369. else
  2370. ret = hub_port_status(hub, port1, &portstatus,
  2371. &portchange);
  2372. if (ret < 0)
  2373. return ret;
  2374. /*
  2375. * The port state is unknown until the reset completes.
  2376. *
  2377. * On top of that, some chips may require additional time
  2378. * to re-establish a connection after the reset is complete,
  2379. * so also wait for the connection to be re-established.
  2380. */
  2381. if (!(portstatus & USB_PORT_STAT_RESET) &&
  2382. (portstatus & USB_PORT_STAT_CONNECTION))
  2383. break;
  2384. /* switch to the long delay after two short delay failures */
  2385. if (delay_time >= 2 * HUB_SHORT_RESET_TIME)
  2386. delay = HUB_LONG_RESET_TIME;
  2387. dev_dbg(&hub->ports[port1 - 1]->dev,
  2388. "not %sreset yet, waiting %dms\n",
  2389. warm ? "warm " : "", delay);
  2390. }
  2391. if ((portstatus & USB_PORT_STAT_RESET))
  2392. return -EBUSY;
  2393. if (hub_port_warm_reset_required(hub, port1, portstatus))
  2394. return -ENOTCONN;
  2395. /* Device went away? */
  2396. if (!(portstatus & USB_PORT_STAT_CONNECTION))
  2397. return -ENOTCONN;
  2398. /* Retry if connect change is set but status is still connected.
  2399. * A USB 3.0 connection may bounce if multiple warm resets were issued,
  2400. * but the device may have successfully re-connected. Ignore it.
  2401. */
  2402. if (!hub_is_superspeed(hub->hdev) &&
  2403. (portchange & USB_PORT_STAT_C_CONNECTION)) {
  2404. usb_clear_port_feature(hub->hdev, port1,
  2405. USB_PORT_FEAT_C_CONNECTION);
  2406. return -EAGAIN;
  2407. }
  2408. if (!(portstatus & USB_PORT_STAT_ENABLE))
  2409. return -EBUSY;
  2410. if (!udev)
  2411. return 0;
  2412. if (hub_is_superspeedplus(hub->hdev)) {
  2413. /* extended portstatus Rx and Tx lane count are zero based */
  2414. udev->rx_lanes = USB_EXT_PORT_RX_LANES(ext_portstatus) + 1;
  2415. udev->tx_lanes = USB_EXT_PORT_TX_LANES(ext_portstatus) + 1;
  2416. } else {
  2417. udev->rx_lanes = 1;
  2418. udev->tx_lanes = 1;
  2419. }
  2420. if (hub_is_wusb(hub))
  2421. udev->speed = USB_SPEED_WIRELESS;
  2422. else if (hub_is_superspeedplus(hub->hdev) &&
  2423. port_speed_is_ssp(hub->hdev, ext_portstatus &
  2424. USB_EXT_PORT_STAT_RX_SPEED_ID))
  2425. udev->speed = USB_SPEED_SUPER_PLUS;
  2426. else if (hub_is_superspeed(hub->hdev))
  2427. udev->speed = USB_SPEED_SUPER;
  2428. else if (portstatus & USB_PORT_STAT_HIGH_SPEED)
  2429. udev->speed = USB_SPEED_HIGH;
  2430. else if (portstatus & USB_PORT_STAT_LOW_SPEED)
  2431. udev->speed = USB_SPEED_LOW;
  2432. else
  2433. udev->speed = USB_SPEED_FULL;
  2434. return 0;
  2435. }
  2436. /* Handle port reset and port warm(BH) reset (for USB3 protocol ports) */
  2437. static int hub_port_reset(struct usb_hub *hub, int port1,
  2438. struct usb_device *udev, unsigned int delay, bool warm)
  2439. {
  2440. int i, status;
  2441. u16 portchange, portstatus;
  2442. struct usb_port *port_dev = hub->ports[port1 - 1];
  2443. int reset_recovery_time;
  2444. if (!hub_is_superspeed(hub->hdev)) {
  2445. if (warm) {
  2446. dev_err(hub->intfdev, "only USB3 hub support "
  2447. "warm reset\n");
  2448. return -EINVAL;
  2449. }
  2450. /* Block EHCI CF initialization during the port reset.
  2451. * Some companion controllers don't like it when they mix.
  2452. */
  2453. down_read(&ehci_cf_port_reset_rwsem);
  2454. } else if (!warm) {
  2455. /*
  2456. * If the caller hasn't explicitly requested a warm reset,
  2457. * double check and see if one is needed.
  2458. */
  2459. if (hub_port_status(hub, port1, &portstatus, &portchange) == 0)
  2460. if (hub_port_warm_reset_required(hub, port1,
  2461. portstatus))
  2462. warm = true;
  2463. }
  2464. clear_bit(port1, hub->warm_reset_bits);
  2465. /* Reset the port */
  2466. for (i = 0; i < PORT_RESET_TRIES; i++) {
  2467. status = set_port_feature(hub->hdev, port1, (warm ?
  2468. USB_PORT_FEAT_BH_PORT_RESET :
  2469. USB_PORT_FEAT_RESET));
  2470. if (status == -ENODEV) {
  2471. ; /* The hub is gone */
  2472. } else if (status) {
  2473. dev_err(&port_dev->dev,
  2474. "cannot %sreset (err = %d)\n",
  2475. warm ? "warm " : "", status);
  2476. } else {
  2477. status = hub_port_wait_reset(hub, port1, udev, delay,
  2478. warm);
  2479. if (status && status != -ENOTCONN && status != -ENODEV)
  2480. dev_dbg(hub->intfdev,
  2481. "port_wait_reset: err = %d\n",
  2482. status);
  2483. }
  2484. /* Check for disconnect or reset */
  2485. if (status == 0 || status == -ENOTCONN || status == -ENODEV) {
  2486. usb_clear_port_feature(hub->hdev, port1,
  2487. USB_PORT_FEAT_C_RESET);
  2488. if (!hub_is_superspeed(hub->hdev))
  2489. goto done;
  2490. usb_clear_port_feature(hub->hdev, port1,
  2491. USB_PORT_FEAT_C_BH_PORT_RESET);
  2492. usb_clear_port_feature(hub->hdev, port1,
  2493. USB_PORT_FEAT_C_PORT_LINK_STATE);
  2494. if (udev)
  2495. usb_clear_port_feature(hub->hdev, port1,
  2496. USB_PORT_FEAT_C_CONNECTION);
  2497. /*
  2498. * If a USB 3.0 device migrates from reset to an error
  2499. * state, re-issue the warm reset.
  2500. */
  2501. if (hub_port_status(hub, port1,
  2502. &portstatus, &portchange) < 0)
  2503. goto done;
  2504. if (!hub_port_warm_reset_required(hub, port1,
  2505. portstatus))
  2506. goto done;
  2507. /*
  2508. * If the port is in SS.Inactive or Compliance Mode, the
  2509. * hot or warm reset failed. Try another warm reset.
  2510. */
  2511. if (!warm) {
  2512. dev_dbg(&port_dev->dev,
  2513. "hot reset failed, warm reset\n");
  2514. warm = true;
  2515. }
  2516. }
  2517. dev_dbg(&port_dev->dev,
  2518. "not enabled, trying %sreset again...\n",
  2519. warm ? "warm " : "");
  2520. delay = HUB_LONG_RESET_TIME;
  2521. }
  2522. dev_err(&port_dev->dev, "Cannot enable. Maybe the USB cable is bad?\n");
  2523. done:
  2524. if (status == 0) {
  2525. if (port_dev->quirks & USB_PORT_QUIRK_FAST_ENUM)
  2526. usleep_range(10000, 12000);
  2527. else {
  2528. /* TRSTRCY = 10 ms; plus some extra */
  2529. reset_recovery_time = 10 + 40;
  2530. /* Hub needs extra delay after resetting its port. */
  2531. if (hub->hdev->quirks & USB_QUIRK_HUB_SLOW_RESET)
  2532. reset_recovery_time += 100;
  2533. msleep(reset_recovery_time);
  2534. }
  2535. if (udev) {
  2536. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  2537. update_devnum(udev, 0);
  2538. /* The xHC may think the device is already reset,
  2539. * so ignore the status.
  2540. */
  2541. if (hcd->driver->reset_device)
  2542. hcd->driver->reset_device(hcd, udev);
  2543. usb_set_device_state(udev, USB_STATE_DEFAULT);
  2544. }
  2545. } else {
  2546. if (udev)
  2547. usb_set_device_state(udev, USB_STATE_NOTATTACHED);
  2548. }
  2549. if (!hub_is_superspeed(hub->hdev))
  2550. up_read(&ehci_cf_port_reset_rwsem);
  2551. return status;
  2552. }
  2553. /* Check if a port is power on */
  2554. static int port_is_power_on(struct usb_hub *hub, unsigned portstatus)
  2555. {
  2556. int ret = 0;
  2557. if (hub_is_superspeed(hub->hdev)) {
  2558. if (portstatus & USB_SS_PORT_STAT_POWER)
  2559. ret = 1;
  2560. } else {
  2561. if (portstatus & USB_PORT_STAT_POWER)
  2562. ret = 1;
  2563. }
  2564. return ret;
  2565. }
  2566. static void usb_lock_port(struct usb_port *port_dev)
  2567. __acquires(&port_dev->status_lock)
  2568. {
  2569. mutex_lock(&port_dev->status_lock);
  2570. __acquire(&port_dev->status_lock);
  2571. }
  2572. static void usb_unlock_port(struct usb_port *port_dev)
  2573. __releases(&port_dev->status_lock)
  2574. {
  2575. mutex_unlock(&port_dev->status_lock);
  2576. __release(&port_dev->status_lock);
  2577. }
  2578. #ifdef CONFIG_PM
  2579. /* Check if a port is suspended(USB2.0 port) or in U3 state(USB3.0 port) */
  2580. static int port_is_suspended(struct usb_hub *hub, unsigned portstatus)
  2581. {
  2582. int ret = 0;
  2583. if (hub_is_superspeed(hub->hdev)) {
  2584. if ((portstatus & USB_PORT_STAT_LINK_STATE)
  2585. == USB_SS_PORT_LS_U3)
  2586. ret = 1;
  2587. } else {
  2588. if (portstatus & USB_PORT_STAT_SUSPEND)
  2589. ret = 1;
  2590. }
  2591. return ret;
  2592. }
  2593. /* Determine whether the device on a port is ready for a normal resume,
  2594. * is ready for a reset-resume, or should be disconnected.
  2595. */
  2596. static int check_port_resume_type(struct usb_device *udev,
  2597. struct usb_hub *hub, int port1,
  2598. int status, u16 portchange, u16 portstatus)
  2599. {
  2600. struct usb_port *port_dev = hub->ports[port1 - 1];
  2601. int retries = 3;
  2602. retry:
  2603. /* Is a warm reset needed to recover the connection? */
  2604. if (status == 0 && udev->reset_resume
  2605. && hub_port_warm_reset_required(hub, port1, portstatus)) {
  2606. /* pass */;
  2607. }
  2608. /* Is the device still present? */
  2609. else if (status || port_is_suspended(hub, portstatus) ||
  2610. !port_is_power_on(hub, portstatus)) {
  2611. if (status >= 0)
  2612. status = -ENODEV;
  2613. } else if (!(portstatus & USB_PORT_STAT_CONNECTION)) {
  2614. if (retries--) {
  2615. usleep_range(200, 300);
  2616. status = hub_port_status(hub, port1, &portstatus,
  2617. &portchange);
  2618. goto retry;
  2619. }
  2620. status = -ENODEV;
  2621. }
  2622. /* Can't do a normal resume if the port isn't enabled,
  2623. * so try a reset-resume instead.
  2624. */
  2625. else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) {
  2626. if (udev->persist_enabled)
  2627. udev->reset_resume = 1;
  2628. else
  2629. status = -ENODEV;
  2630. }
  2631. if (status) {
  2632. dev_dbg(&port_dev->dev, "status %04x.%04x after resume, %d\n",
  2633. portchange, portstatus, status);
  2634. } else if (udev->reset_resume) {
  2635. /* Late port handoff can set status-change bits */
  2636. if (portchange & USB_PORT_STAT_C_CONNECTION)
  2637. usb_clear_port_feature(hub->hdev, port1,
  2638. USB_PORT_FEAT_C_CONNECTION);
  2639. if (portchange & USB_PORT_STAT_C_ENABLE)
  2640. usb_clear_port_feature(hub->hdev, port1,
  2641. USB_PORT_FEAT_C_ENABLE);
  2642. }
  2643. return status;
  2644. }
  2645. int usb_disable_ltm(struct usb_device *udev)
  2646. {
  2647. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  2648. /* Check if the roothub and device supports LTM. */
  2649. if (!usb_device_supports_ltm(hcd->self.root_hub) ||
  2650. !usb_device_supports_ltm(udev))
  2651. return 0;
  2652. /* Clear Feature LTM Enable can only be sent if the device is
  2653. * configured.
  2654. */
  2655. if (!udev->actconfig)
  2656. return 0;
  2657. return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  2658. USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
  2659. USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
  2660. USB_CTRL_SET_TIMEOUT);
  2661. }
  2662. EXPORT_SYMBOL_GPL(usb_disable_ltm);
  2663. void usb_enable_ltm(struct usb_device *udev)
  2664. {
  2665. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  2666. /* Check if the roothub and device supports LTM. */
  2667. if (!usb_device_supports_ltm(hcd->self.root_hub) ||
  2668. !usb_device_supports_ltm(udev))
  2669. return;
  2670. /* Set Feature LTM Enable can only be sent if the device is
  2671. * configured.
  2672. */
  2673. if (!udev->actconfig)
  2674. return;
  2675. usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  2676. USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
  2677. USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
  2678. USB_CTRL_SET_TIMEOUT);
  2679. }
  2680. EXPORT_SYMBOL_GPL(usb_enable_ltm);
  2681. /*
  2682. * usb_enable_remote_wakeup - enable remote wakeup for a device
  2683. * @udev: target device
  2684. *
  2685. * For USB-2 devices: Set the device's remote wakeup feature.
  2686. *
  2687. * For USB-3 devices: Assume there's only one function on the device and
  2688. * enable remote wake for the first interface. FIXME if the interface
  2689. * association descriptor shows there's more than one function.
  2690. */
  2691. static int usb_enable_remote_wakeup(struct usb_device *udev)
  2692. {
  2693. if (udev->speed < USB_SPEED_SUPER)
  2694. return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  2695. USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
  2696. USB_DEVICE_REMOTE_WAKEUP, 0, NULL, 0,
  2697. USB_CTRL_SET_TIMEOUT);
  2698. else
  2699. return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  2700. USB_REQ_SET_FEATURE, USB_RECIP_INTERFACE,
  2701. USB_INTRF_FUNC_SUSPEND,
  2702. USB_INTRF_FUNC_SUSPEND_RW |
  2703. USB_INTRF_FUNC_SUSPEND_LP,
  2704. NULL, 0, USB_CTRL_SET_TIMEOUT);
  2705. }
  2706. /*
  2707. * usb_disable_remote_wakeup - disable remote wakeup for a device
  2708. * @udev: target device
  2709. *
  2710. * For USB-2 devices: Clear the device's remote wakeup feature.
  2711. *
  2712. * For USB-3 devices: Assume there's only one function on the device and
  2713. * disable remote wake for the first interface. FIXME if the interface
  2714. * association descriptor shows there's more than one function.
  2715. */
  2716. static int usb_disable_remote_wakeup(struct usb_device *udev)
  2717. {
  2718. if (udev->speed < USB_SPEED_SUPER)
  2719. return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  2720. USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
  2721. USB_DEVICE_REMOTE_WAKEUP, 0, NULL, 0,
  2722. USB_CTRL_SET_TIMEOUT);
  2723. else
  2724. return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  2725. USB_REQ_SET_FEATURE, USB_RECIP_INTERFACE,
  2726. USB_INTRF_FUNC_SUSPEND, 0, NULL, 0,
  2727. USB_CTRL_SET_TIMEOUT);
  2728. }
  2729. /* Count of wakeup-enabled devices at or below udev */
  2730. static unsigned wakeup_enabled_descendants(struct usb_device *udev)
  2731. {
  2732. struct usb_hub *hub = usb_hub_to_struct_hub(udev);
  2733. return udev->do_remote_wakeup +
  2734. (hub ? hub->wakeup_enabled_descendants : 0);
  2735. }
  2736. /*
  2737. * usb_port_suspend - suspend a usb device's upstream port
  2738. * @udev: device that's no longer in active use, not a root hub
  2739. * Context: must be able to sleep; device not locked; pm locks held
  2740. *
  2741. * Suspends a USB device that isn't in active use, conserving power.
  2742. * Devices may wake out of a suspend, if anything important happens,
  2743. * using the remote wakeup mechanism. They may also be taken out of
  2744. * suspend by the host, using usb_port_resume(). It's also routine
  2745. * to disconnect devices while they are suspended.
  2746. *
  2747. * This only affects the USB hardware for a device; its interfaces
  2748. * (and, for hubs, child devices) must already have been suspended.
  2749. *
  2750. * Selective port suspend reduces power; most suspended devices draw
  2751. * less than 500 uA. It's also used in OTG, along with remote wakeup.
  2752. * All devices below the suspended port are also suspended.
  2753. *
  2754. * Devices leave suspend state when the host wakes them up. Some devices
  2755. * also support "remote wakeup", where the device can activate the USB
  2756. * tree above them to deliver data, such as a keypress or packet. In
  2757. * some cases, this wakes the USB host.
  2758. *
  2759. * Suspending OTG devices may trigger HNP, if that's been enabled
  2760. * between a pair of dual-role devices. That will change roles, such
  2761. * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral.
  2762. *
  2763. * Devices on USB hub ports have only one "suspend" state, corresponding
  2764. * to ACPI D2, "may cause the device to lose some context".
  2765. * State transitions include:
  2766. *
  2767. * - suspend, resume ... when the VBUS power link stays live
  2768. * - suspend, disconnect ... VBUS lost
  2769. *
  2770. * Once VBUS drop breaks the circuit, the port it's using has to go through
  2771. * normal re-enumeration procedures, starting with enabling VBUS power.
  2772. * Other than re-initializing the hub (plug/unplug, except for root hubs),
  2773. * Linux (2.6) currently has NO mechanisms to initiate that: no hub_wq
  2774. * timer, no SRP, no requests through sysfs.
  2775. *
  2776. * If Runtime PM isn't enabled or used, non-SuperSpeed devices may not get
  2777. * suspended until their bus goes into global suspend (i.e., the root
  2778. * hub is suspended). Nevertheless, we change @udev->state to
  2779. * USB_STATE_SUSPENDED as this is the device's "logical" state. The actual
  2780. * upstream port setting is stored in @udev->port_is_suspended.
  2781. *
  2782. * Returns 0 on success, else negative errno.
  2783. */
  2784. int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
  2785. {
  2786. struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
  2787. struct usb_port *port_dev = hub->ports[udev->portnum - 1];
  2788. int port1 = udev->portnum;
  2789. int status;
  2790. bool really_suspend = true;
  2791. usb_lock_port(port_dev);
  2792. /* enable remote wakeup when appropriate; this lets the device
  2793. * wake up the upstream hub (including maybe the root hub).
  2794. *
  2795. * NOTE: OTG devices may issue remote wakeup (or SRP) even when
  2796. * we don't explicitly enable it here.
  2797. */
  2798. if (udev->do_remote_wakeup) {
  2799. status = usb_enable_remote_wakeup(udev);
  2800. if (status) {
  2801. dev_dbg(&udev->dev, "won't remote wakeup, status %d\n",
  2802. status);
  2803. /* bail if autosuspend is requested */
  2804. if (PMSG_IS_AUTO(msg))
  2805. goto err_wakeup;
  2806. }
  2807. }
  2808. /* disable USB2 hardware LPM */
  2809. if (udev->usb2_hw_lpm_enabled == 1)
  2810. usb_set_usb2_hardware_lpm(udev, 0);
  2811. if (usb_disable_ltm(udev)) {
  2812. dev_err(&udev->dev, "Failed to disable LTM before suspend\n");
  2813. status = -ENOMEM;
  2814. if (PMSG_IS_AUTO(msg))
  2815. goto err_ltm;
  2816. }
  2817. /* see 7.1.7.6 */
  2818. if (hub_is_superspeed(hub->hdev))
  2819. status = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_U3);
  2820. /*
  2821. * For system suspend, we do not need to enable the suspend feature
  2822. * on individual USB-2 ports. The devices will automatically go
  2823. * into suspend a few ms after the root hub stops sending packets.
  2824. * The USB 2.0 spec calls this "global suspend".
  2825. *
  2826. * However, many USB hubs have a bug: They don't relay wakeup requests
  2827. * from a downstream port if the port's suspend feature isn't on.
  2828. * Therefore we will turn on the suspend feature if udev or any of its
  2829. * descendants is enabled for remote wakeup.
  2830. */
  2831. else if (PMSG_IS_AUTO(msg) || wakeup_enabled_descendants(udev) > 0)
  2832. status = set_port_feature(hub->hdev, port1,
  2833. USB_PORT_FEAT_SUSPEND);
  2834. else {
  2835. really_suspend = false;
  2836. status = 0;
  2837. }
  2838. if (status) {
  2839. dev_dbg(&port_dev->dev, "can't suspend, status %d\n", status);
  2840. /* Try to enable USB3 LTM again */
  2841. usb_enable_ltm(udev);
  2842. err_ltm:
  2843. /* Try to enable USB2 hardware LPM again */
  2844. if (udev->usb2_hw_lpm_capable == 1)
  2845. usb_set_usb2_hardware_lpm(udev, 1);
  2846. if (udev->do_remote_wakeup)
  2847. (void) usb_disable_remote_wakeup(udev);
  2848. err_wakeup:
  2849. /* System sleep transitions should never fail */
  2850. if (!PMSG_IS_AUTO(msg))
  2851. status = 0;
  2852. } else {
  2853. dev_dbg(&udev->dev, "usb %ssuspend, wakeup %d\n",
  2854. (PMSG_IS_AUTO(msg) ? "auto-" : ""),
  2855. udev->do_remote_wakeup);
  2856. if (really_suspend) {
  2857. udev->port_is_suspended = 1;
  2858. /* device has up to 10 msec to fully suspend */
  2859. msleep(10);
  2860. }
  2861. usb_set_device_state(udev, USB_STATE_SUSPENDED);
  2862. }
  2863. if (status == 0 && !udev->do_remote_wakeup && udev->persist_enabled
  2864. && test_and_clear_bit(port1, hub->child_usage_bits))
  2865. pm_runtime_put_sync(&port_dev->dev);
  2866. usb_mark_last_busy(hub->hdev);
  2867. usb_unlock_port(port_dev);
  2868. return status;
  2869. }
  2870. /*
  2871. * If the USB "suspend" state is in use (rather than "global suspend"),
  2872. * many devices will be individually taken out of suspend state using
  2873. * special "resume" signaling. This routine kicks in shortly after
  2874. * hardware resume signaling is finished, either because of selective
  2875. * resume (by host) or remote wakeup (by device) ... now see what changed
  2876. * in the tree that's rooted at this device.
  2877. *
  2878. * If @udev->reset_resume is set then the device is reset before the
  2879. * status check is done.
  2880. */
  2881. static int finish_port_resume(struct usb_device *udev)
  2882. {
  2883. int status = 0;
  2884. u16 devstatus = 0;
  2885. /* caller owns the udev device lock */
  2886. dev_dbg(&udev->dev, "%s\n",
  2887. udev->reset_resume ? "finish reset-resume" : "finish resume");
  2888. /* usb ch9 identifies four variants of SUSPENDED, based on what
  2889. * state the device resumes to. Linux currently won't see the
  2890. * first two on the host side; they'd be inside hub_port_init()
  2891. * during many timeouts, but hub_wq can't suspend until later.
  2892. */
  2893. usb_set_device_state(udev, udev->actconfig
  2894. ? USB_STATE_CONFIGURED
  2895. : USB_STATE_ADDRESS);
  2896. /* 10.5.4.5 says not to reset a suspended port if the attached
  2897. * device is enabled for remote wakeup. Hence the reset
  2898. * operation is carried out here, after the port has been
  2899. * resumed.
  2900. */
  2901. if (udev->reset_resume) {
  2902. /*
  2903. * If the device morphs or switches modes when it is reset,
  2904. * we don't want to perform a reset-resume. We'll fail the
  2905. * resume, which will cause a logical disconnect, and then
  2906. * the device will be rediscovered.
  2907. */
  2908. retry_reset_resume:
  2909. if (udev->quirks & USB_QUIRK_RESET)
  2910. status = -ENODEV;
  2911. else
  2912. status = usb_reset_and_verify_device(udev);
  2913. }
  2914. /* 10.5.4.5 says be sure devices in the tree are still there.
  2915. * For now let's assume the device didn't go crazy on resume,
  2916. * and device drivers will know about any resume quirks.
  2917. */
  2918. if (status == 0) {
  2919. devstatus = 0;
  2920. status = usb_get_std_status(udev, USB_RECIP_DEVICE, 0, &devstatus);
  2921. /* If a normal resume failed, try doing a reset-resume */
  2922. if (status && !udev->reset_resume && udev->persist_enabled) {
  2923. dev_dbg(&udev->dev, "retry with reset-resume\n");
  2924. udev->reset_resume = 1;
  2925. goto retry_reset_resume;
  2926. }
  2927. }
  2928. if (status) {
  2929. dev_dbg(&udev->dev, "gone after usb resume? status %d\n",
  2930. status);
  2931. /*
  2932. * There are a few quirky devices which violate the standard
  2933. * by claiming to have remote wakeup enabled after a reset,
  2934. * which crash if the feature is cleared, hence check for
  2935. * udev->reset_resume
  2936. */
  2937. } else if (udev->actconfig && !udev->reset_resume) {
  2938. if (udev->speed < USB_SPEED_SUPER) {
  2939. if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP))
  2940. status = usb_disable_remote_wakeup(udev);
  2941. } else {
  2942. status = usb_get_std_status(udev, USB_RECIP_INTERFACE, 0,
  2943. &devstatus);
  2944. if (!status && devstatus & (USB_INTRF_STAT_FUNC_RW_CAP
  2945. | USB_INTRF_STAT_FUNC_RW))
  2946. status = usb_disable_remote_wakeup(udev);
  2947. }
  2948. if (status)
  2949. dev_dbg(&udev->dev,
  2950. "disable remote wakeup, status %d\n",
  2951. status);
  2952. status = 0;
  2953. }
  2954. return status;
  2955. }
  2956. /*
  2957. * There are some SS USB devices which take longer time for link training.
  2958. * XHCI specs 4.19.4 says that when Link training is successful, port
  2959. * sets CCS bit to 1. So if SW reads port status before successful link
  2960. * training, then it will not find device to be present.
  2961. * USB Analyzer log with such buggy devices show that in some cases
  2962. * device switch on the RX termination after long delay of host enabling
  2963. * the VBUS. In few other cases it has been seen that device fails to
  2964. * negotiate link training in first attempt. It has been
  2965. * reported till now that few devices take as long as 2000 ms to train
  2966. * the link after host enabling its VBUS and termination. Following
  2967. * routine implements a 2000 ms timeout for link training. If in a case
  2968. * link trains before timeout, loop will exit earlier.
  2969. *
  2970. * There are also some 2.0 hard drive based devices and 3.0 thumb
  2971. * drives that, when plugged into a 2.0 only port, take a long
  2972. * time to set CCS after VBUS enable.
  2973. *
  2974. * FIXME: If a device was connected before suspend, but was removed
  2975. * while system was asleep, then the loop in the following routine will
  2976. * only exit at timeout.
  2977. *
  2978. * This routine should only be called when persist is enabled.
  2979. */
  2980. static int wait_for_connected(struct usb_device *udev,
  2981. struct usb_hub *hub, int *port1,
  2982. u16 *portchange, u16 *portstatus)
  2983. {
  2984. int status = 0, delay_ms = 0;
  2985. while (delay_ms < 2000) {
  2986. if (status || *portstatus & USB_PORT_STAT_CONNECTION)
  2987. break;
  2988. if (!port_is_power_on(hub, *portstatus)) {
  2989. status = -ENODEV;
  2990. break;
  2991. }
  2992. msleep(20);
  2993. delay_ms += 20;
  2994. status = hub_port_status(hub, *port1, portstatus, portchange);
  2995. }
  2996. dev_dbg(&udev->dev, "Waited %dms for CONNECT\n", delay_ms);
  2997. return status;
  2998. }
  2999. /*
  3000. * usb_port_resume - re-activate a suspended usb device's upstream port
  3001. * @udev: device to re-activate, not a root hub
  3002. * Context: must be able to sleep; device not locked; pm locks held
  3003. *
  3004. * This will re-activate the suspended device, increasing power usage
  3005. * while letting drivers communicate again with its endpoints.
  3006. * USB resume explicitly guarantees that the power session between
  3007. * the host and the device is the same as it was when the device
  3008. * suspended.
  3009. *
  3010. * If @udev->reset_resume is set then this routine won't check that the
  3011. * port is still enabled. Furthermore, finish_port_resume() above will
  3012. * reset @udev. The end result is that a broken power session can be
  3013. * recovered and @udev will appear to persist across a loss of VBUS power.
  3014. *
  3015. * For example, if a host controller doesn't maintain VBUS suspend current
  3016. * during a system sleep or is reset when the system wakes up, all the USB
  3017. * power sessions below it will be broken. This is especially troublesome
  3018. * for mass-storage devices containing mounted filesystems, since the
  3019. * device will appear to have disconnected and all the memory mappings
  3020. * to it will be lost. Using the USB_PERSIST facility, the device can be
  3021. * made to appear as if it had not disconnected.
  3022. *
  3023. * This facility can be dangerous. Although usb_reset_and_verify_device() makes
  3024. * every effort to insure that the same device is present after the
  3025. * reset as before, it cannot provide a 100% guarantee. Furthermore it's
  3026. * quite possible for a device to remain unaltered but its media to be
  3027. * changed. If the user replaces a flash memory card while the system is
  3028. * asleep, he will have only himself to blame when the filesystem on the
  3029. * new card is corrupted and the system crashes.
  3030. *
  3031. * Returns 0 on success, else negative errno.
  3032. */
  3033. int usb_port_resume(struct usb_device *udev, pm_message_t msg)
  3034. {
  3035. struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
  3036. struct usb_port *port_dev = hub->ports[udev->portnum - 1];
  3037. int port1 = udev->portnum;
  3038. int status;
  3039. u16 portchange, portstatus;
  3040. if (!test_and_set_bit(port1, hub->child_usage_bits)) {
  3041. status = pm_runtime_get_sync(&port_dev->dev);
  3042. if (status < 0) {
  3043. dev_dbg(&udev->dev, "can't resume usb port, status %d\n",
  3044. status);
  3045. return status;
  3046. }
  3047. }
  3048. usb_lock_port(port_dev);
  3049. /* Skip the initial Clear-Suspend step for a remote wakeup */
  3050. status = hub_port_status(hub, port1, &portstatus, &portchange);
  3051. if (status == 0 && !port_is_suspended(hub, portstatus)) {
  3052. if (portchange & USB_PORT_STAT_C_SUSPEND)
  3053. pm_wakeup_event(&udev->dev, 0);
  3054. goto SuspendCleared;
  3055. }
  3056. /* see 7.1.7.7; affects power usage, but not budgeting */
  3057. if (hub_is_superspeed(hub->hdev))
  3058. status = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_U0);
  3059. else
  3060. status = usb_clear_port_feature(hub->hdev,
  3061. port1, USB_PORT_FEAT_SUSPEND);
  3062. if (status) {
  3063. dev_dbg(&port_dev->dev, "can't resume, status %d\n", status);
  3064. } else {
  3065. /* drive resume for USB_RESUME_TIMEOUT msec */
  3066. dev_dbg(&udev->dev, "usb %sresume\n",
  3067. (PMSG_IS_AUTO(msg) ? "auto-" : ""));
  3068. msleep(USB_RESUME_TIMEOUT);
  3069. /* Virtual root hubs can trigger on GET_PORT_STATUS to
  3070. * stop resume signaling. Then finish the resume
  3071. * sequence.
  3072. */
  3073. status = hub_port_status(hub, port1, &portstatus, &portchange);
  3074. /* TRSMRCY = 10 msec */
  3075. msleep(10);
  3076. }
  3077. SuspendCleared:
  3078. if (status == 0) {
  3079. udev->port_is_suspended = 0;
  3080. if (hub_is_superspeed(hub->hdev)) {
  3081. if (portchange & USB_PORT_STAT_C_LINK_STATE)
  3082. usb_clear_port_feature(hub->hdev, port1,
  3083. USB_PORT_FEAT_C_PORT_LINK_STATE);
  3084. } else {
  3085. if (portchange & USB_PORT_STAT_C_SUSPEND)
  3086. usb_clear_port_feature(hub->hdev, port1,
  3087. USB_PORT_FEAT_C_SUSPEND);
  3088. }
  3089. }
  3090. if (udev->persist_enabled)
  3091. status = wait_for_connected(udev, hub, &port1, &portchange,
  3092. &portstatus);
  3093. status = check_port_resume_type(udev,
  3094. hub, port1, status, portchange, portstatus);
  3095. if (status == 0)
  3096. status = finish_port_resume(udev);
  3097. if (status < 0) {
  3098. dev_dbg(&udev->dev, "can't resume, status %d\n", status);
  3099. hub_port_logical_disconnect(hub, port1);
  3100. } else {
  3101. /* Try to enable USB2 hardware LPM */
  3102. if (udev->usb2_hw_lpm_capable == 1)
  3103. usb_set_usb2_hardware_lpm(udev, 1);
  3104. /* Try to enable USB3 LTM */
  3105. usb_enable_ltm(udev);
  3106. }
  3107. usb_unlock_port(port_dev);
  3108. return status;
  3109. }
  3110. int usb_remote_wakeup(struct usb_device *udev)
  3111. {
  3112. int status = 0;
  3113. usb_lock_device(udev);
  3114. if (udev->state == USB_STATE_SUSPENDED) {
  3115. dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
  3116. status = usb_autoresume_device(udev);
  3117. if (status == 0) {
  3118. /* Let the drivers do their thing, then... */
  3119. usb_autosuspend_device(udev);
  3120. }
  3121. }
  3122. usb_unlock_device(udev);
  3123. return status;
  3124. }
  3125. /* Returns 1 if there was a remote wakeup and a connect status change. */
  3126. static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
  3127. u16 portstatus, u16 portchange)
  3128. __must_hold(&port_dev->status_lock)
  3129. {
  3130. struct usb_port *port_dev = hub->ports[port - 1];
  3131. struct usb_device *hdev;
  3132. struct usb_device *udev;
  3133. int connect_change = 0;
  3134. int ret;
  3135. hdev = hub->hdev;
  3136. udev = port_dev->child;
  3137. if (!hub_is_superspeed(hdev)) {
  3138. if (!(portchange & USB_PORT_STAT_C_SUSPEND))
  3139. return 0;
  3140. usb_clear_port_feature(hdev, port, USB_PORT_FEAT_C_SUSPEND);
  3141. } else {
  3142. if (!udev || udev->state != USB_STATE_SUSPENDED ||
  3143. (portstatus & USB_PORT_STAT_LINK_STATE) !=
  3144. USB_SS_PORT_LS_U0)
  3145. return 0;
  3146. }
  3147. if (udev) {
  3148. /* TRSMRCY = 10 msec */
  3149. msleep(10);
  3150. usb_unlock_port(port_dev);
  3151. ret = usb_remote_wakeup(udev);
  3152. usb_lock_port(port_dev);
  3153. if (ret < 0)
  3154. connect_change = 1;
  3155. } else {
  3156. ret = -ENODEV;
  3157. hub_port_disable(hub, port, 1);
  3158. }
  3159. dev_dbg(&port_dev->dev, "resume, status %d\n", ret);
  3160. return connect_change;
  3161. }
  3162. static int check_ports_changed(struct usb_hub *hub)
  3163. {
  3164. int port1;
  3165. for (port1 = 1; port1 <= hub->hdev->maxchild; ++port1) {
  3166. u16 portstatus, portchange;
  3167. int status;
  3168. status = hub_port_status(hub, port1, &portstatus, &portchange);
  3169. if (!status && portchange)
  3170. return 1;
  3171. }
  3172. return 0;
  3173. }
  3174. static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
  3175. {
  3176. struct usb_hub *hub = usb_get_intfdata(intf);
  3177. struct usb_device *hdev = hub->hdev;
  3178. unsigned port1;
  3179. int status;
  3180. /*
  3181. * Warn if children aren't already suspended.
  3182. * Also, add up the number of wakeup-enabled descendants.
  3183. */
  3184. hub->wakeup_enabled_descendants = 0;
  3185. for (port1 = 1; port1 <= hdev->maxchild; port1++) {
  3186. struct usb_port *port_dev = hub->ports[port1 - 1];
  3187. struct usb_device *udev = port_dev->child;
  3188. if (udev && udev->can_submit) {
  3189. dev_warn(&port_dev->dev, "device %s not suspended yet\n",
  3190. dev_name(&udev->dev));
  3191. if (PMSG_IS_AUTO(msg))
  3192. return -EBUSY;
  3193. }
  3194. if (udev)
  3195. hub->wakeup_enabled_descendants +=
  3196. wakeup_enabled_descendants(udev);
  3197. }
  3198. if (hdev->do_remote_wakeup && hub->quirk_check_port_auto_suspend) {
  3199. /* check if there are changes pending on hub ports */
  3200. if (check_ports_changed(hub)) {
  3201. if (PMSG_IS_AUTO(msg))
  3202. return -EBUSY;
  3203. pm_wakeup_event(&hdev->dev, 2000);
  3204. }
  3205. }
  3206. if (hub_is_superspeed(hdev) && hdev->do_remote_wakeup) {
  3207. /* Enable hub to send remote wakeup for all ports. */
  3208. for (port1 = 1; port1 <= hdev->maxchild; port1++) {
  3209. status = set_port_feature(hdev,
  3210. port1 |
  3211. USB_PORT_FEAT_REMOTE_WAKE_CONNECT |
  3212. USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT |
  3213. USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT,
  3214. USB_PORT_FEAT_REMOTE_WAKE_MASK);
  3215. }
  3216. }
  3217. dev_dbg(&intf->dev, "%s\n", __func__);
  3218. /* stop hub_wq and related activity */
  3219. hub_quiesce(hub, HUB_SUSPEND);
  3220. return 0;
  3221. }
  3222. /* Report wakeup requests from the ports of a resuming root hub */
  3223. static void report_wakeup_requests(struct usb_hub *hub)
  3224. {
  3225. struct usb_device *hdev = hub->hdev;
  3226. struct usb_device *udev;
  3227. struct usb_hcd *hcd;
  3228. unsigned long resuming_ports;
  3229. int i;
  3230. if (hdev->parent)
  3231. return; /* Not a root hub */
  3232. hcd = bus_to_hcd(hdev->bus);
  3233. if (hcd->driver->get_resuming_ports) {
  3234. /*
  3235. * The get_resuming_ports() method returns a bitmap (origin 0)
  3236. * of ports which have started wakeup signaling but have not
  3237. * yet finished resuming. During system resume we will
  3238. * resume all the enabled ports, regardless of any wakeup
  3239. * signals, which means the wakeup requests would be lost.
  3240. * To prevent this, report them to the PM core here.
  3241. */
  3242. resuming_ports = hcd->driver->get_resuming_ports(hcd);
  3243. for (i = 0; i < hdev->maxchild; ++i) {
  3244. if (test_bit(i, &resuming_ports)) {
  3245. udev = hub->ports[i]->child;
  3246. if (udev)
  3247. pm_wakeup_event(&udev->dev, 0);
  3248. }
  3249. }
  3250. }
  3251. }
  3252. static int hub_resume(struct usb_interface *intf)
  3253. {
  3254. struct usb_hub *hub = usb_get_intfdata(intf);
  3255. dev_dbg(&intf->dev, "%s\n", __func__);
  3256. hub_activate(hub, HUB_RESUME);
  3257. /*
  3258. * This should be called only for system resume, not runtime resume.
  3259. * We can't tell the difference here, so some wakeup requests will be
  3260. * reported at the wrong time or more than once. This shouldn't
  3261. * matter much, so long as they do get reported.
  3262. */
  3263. report_wakeup_requests(hub);
  3264. return 0;
  3265. }
  3266. static int hub_reset_resume(struct usb_interface *intf)
  3267. {
  3268. struct usb_hub *hub = usb_get_intfdata(intf);
  3269. dev_dbg(&intf->dev, "%s\n", __func__);
  3270. hub_activate(hub, HUB_RESET_RESUME);
  3271. return 0;
  3272. }
  3273. /**
  3274. * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power
  3275. * @rhdev: struct usb_device for the root hub
  3276. *
  3277. * The USB host controller driver calls this function when its root hub
  3278. * is resumed and Vbus power has been interrupted or the controller
  3279. * has been reset. The routine marks @rhdev as having lost power.
  3280. * When the hub driver is resumed it will take notice and carry out
  3281. * power-session recovery for all the "USB-PERSIST"-enabled child devices;
  3282. * the others will be disconnected.
  3283. */
  3284. void usb_root_hub_lost_power(struct usb_device *rhdev)
  3285. {
  3286. dev_notice(&rhdev->dev, "root hub lost power or was reset\n");
  3287. rhdev->reset_resume = 1;
  3288. }
  3289. EXPORT_SYMBOL_GPL(usb_root_hub_lost_power);
  3290. static const char * const usb3_lpm_names[] = {
  3291. "U0",
  3292. "U1",
  3293. "U2",
  3294. "U3",
  3295. };
  3296. /*
  3297. * Send a Set SEL control transfer to the device, prior to enabling
  3298. * device-initiated U1 or U2. This lets the device know the exit latencies from
  3299. * the time the device initiates a U1 or U2 exit, to the time it will receive a
  3300. * packet from the host.
  3301. *
  3302. * This function will fail if the SEL or PEL values for udev are greater than
  3303. * the maximum allowed values for the link state to be enabled.
  3304. */
  3305. static int usb_req_set_sel(struct usb_device *udev, enum usb3_link_state state)
  3306. {
  3307. struct usb_set_sel_req *sel_values;
  3308. unsigned long long u1_sel;
  3309. unsigned long long u1_pel;
  3310. unsigned long long u2_sel;
  3311. unsigned long long u2_pel;
  3312. int ret;
  3313. if (udev->state != USB_STATE_CONFIGURED)
  3314. return 0;
  3315. /* Convert SEL and PEL stored in ns to us */
  3316. u1_sel = DIV_ROUND_UP(udev->u1_params.sel, 1000);
  3317. u1_pel = DIV_ROUND_UP(udev->u1_params.pel, 1000);
  3318. u2_sel = DIV_ROUND_UP(udev->u2_params.sel, 1000);
  3319. u2_pel = DIV_ROUND_UP(udev->u2_params.pel, 1000);
  3320. /*
  3321. * Make sure that the calculated SEL and PEL values for the link
  3322. * state we're enabling aren't bigger than the max SEL/PEL
  3323. * value that will fit in the SET SEL control transfer.
  3324. * Otherwise the device would get an incorrect idea of the exit
  3325. * latency for the link state, and could start a device-initiated
  3326. * U1/U2 when the exit latencies are too high.
  3327. */
  3328. if ((state == USB3_LPM_U1 &&
  3329. (u1_sel > USB3_LPM_MAX_U1_SEL_PEL ||
  3330. u1_pel > USB3_LPM_MAX_U1_SEL_PEL)) ||
  3331. (state == USB3_LPM_U2 &&
  3332. (u2_sel > USB3_LPM_MAX_U2_SEL_PEL ||
  3333. u2_pel > USB3_LPM_MAX_U2_SEL_PEL))) {
  3334. dev_dbg(&udev->dev, "Device-initiated %s disabled due to long SEL %llu us or PEL %llu us\n",
  3335. usb3_lpm_names[state], u1_sel, u1_pel);
  3336. return -EINVAL;
  3337. }
  3338. /*
  3339. * If we're enabling device-initiated LPM for one link state,
  3340. * but the other link state has a too high SEL or PEL value,
  3341. * just set those values to the max in the Set SEL request.
  3342. */
  3343. if (u1_sel > USB3_LPM_MAX_U1_SEL_PEL)
  3344. u1_sel = USB3_LPM_MAX_U1_SEL_PEL;
  3345. if (u1_pel > USB3_LPM_MAX_U1_SEL_PEL)
  3346. u1_pel = USB3_LPM_MAX_U1_SEL_PEL;
  3347. if (u2_sel > USB3_LPM_MAX_U2_SEL_PEL)
  3348. u2_sel = USB3_LPM_MAX_U2_SEL_PEL;
  3349. if (u2_pel > USB3_LPM_MAX_U2_SEL_PEL)
  3350. u2_pel = USB3_LPM_MAX_U2_SEL_PEL;
  3351. /*
  3352. * usb_enable_lpm() can be called as part of a failed device reset,
  3353. * which may be initiated by an error path of a mass storage driver.
  3354. * Therefore, use GFP_NOIO.
  3355. */
  3356. sel_values = kmalloc(sizeof *(sel_values), GFP_NOIO);
  3357. if (!sel_values)
  3358. return -ENOMEM;
  3359. sel_values->u1_sel = u1_sel;
  3360. sel_values->u1_pel = u1_pel;
  3361. sel_values->u2_sel = cpu_to_le16(u2_sel);
  3362. sel_values->u2_pel = cpu_to_le16(u2_pel);
  3363. ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  3364. USB_REQ_SET_SEL,
  3365. USB_RECIP_DEVICE,
  3366. 0, 0,
  3367. sel_values, sizeof *(sel_values),
  3368. USB_CTRL_SET_TIMEOUT);
  3369. kfree(sel_values);
  3370. return ret;
  3371. }
  3372. /*
  3373. * Enable or disable device-initiated U1 or U2 transitions.
  3374. */
  3375. static int usb_set_device_initiated_lpm(struct usb_device *udev,
  3376. enum usb3_link_state state, bool enable)
  3377. {
  3378. int ret;
  3379. int feature;
  3380. switch (state) {
  3381. case USB3_LPM_U1:
  3382. feature = USB_DEVICE_U1_ENABLE;
  3383. break;
  3384. case USB3_LPM_U2:
  3385. feature = USB_DEVICE_U2_ENABLE;
  3386. break;
  3387. default:
  3388. dev_warn(&udev->dev, "%s: Can't %s non-U1 or U2 state.\n",
  3389. __func__, enable ? "enable" : "disable");
  3390. return -EINVAL;
  3391. }
  3392. if (udev->state != USB_STATE_CONFIGURED) {
  3393. dev_dbg(&udev->dev, "%s: Can't %s %s state "
  3394. "for unconfigured device.\n",
  3395. __func__, enable ? "enable" : "disable",
  3396. usb3_lpm_names[state]);
  3397. return 0;
  3398. }
  3399. if (enable) {
  3400. /*
  3401. * Now send the control transfer to enable device-initiated LPM
  3402. * for either U1 or U2.
  3403. */
  3404. ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  3405. USB_REQ_SET_FEATURE,
  3406. USB_RECIP_DEVICE,
  3407. feature,
  3408. 0, NULL, 0,
  3409. USB_CTRL_SET_TIMEOUT);
  3410. } else {
  3411. ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  3412. USB_REQ_CLEAR_FEATURE,
  3413. USB_RECIP_DEVICE,
  3414. feature,
  3415. 0, NULL, 0,
  3416. USB_CTRL_SET_TIMEOUT);
  3417. }
  3418. if (ret < 0) {
  3419. dev_warn(&udev->dev, "%s of device-initiated %s failed.\n",
  3420. enable ? "Enable" : "Disable",
  3421. usb3_lpm_names[state]);
  3422. return -EBUSY;
  3423. }
  3424. return 0;
  3425. }
  3426. static int usb_set_lpm_timeout(struct usb_device *udev,
  3427. enum usb3_link_state state, int timeout)
  3428. {
  3429. int ret;
  3430. int feature;
  3431. switch (state) {
  3432. case USB3_LPM_U1:
  3433. feature = USB_PORT_FEAT_U1_TIMEOUT;
  3434. break;
  3435. case USB3_LPM_U2:
  3436. feature = USB_PORT_FEAT_U2_TIMEOUT;
  3437. break;
  3438. default:
  3439. dev_warn(&udev->dev, "%s: Can't set timeout for non-U1 or U2 state.\n",
  3440. __func__);
  3441. return -EINVAL;
  3442. }
  3443. if (state == USB3_LPM_U1 && timeout > USB3_LPM_U1_MAX_TIMEOUT &&
  3444. timeout != USB3_LPM_DEVICE_INITIATED) {
  3445. dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x, "
  3446. "which is a reserved value.\n",
  3447. usb3_lpm_names[state], timeout);
  3448. return -EINVAL;
  3449. }
  3450. ret = set_port_feature(udev->parent,
  3451. USB_PORT_LPM_TIMEOUT(timeout) | udev->portnum,
  3452. feature);
  3453. if (ret < 0) {
  3454. dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x,"
  3455. "error code %i\n", usb3_lpm_names[state],
  3456. timeout, ret);
  3457. return -EBUSY;
  3458. }
  3459. if (state == USB3_LPM_U1)
  3460. udev->u1_params.timeout = timeout;
  3461. else
  3462. udev->u2_params.timeout = timeout;
  3463. return 0;
  3464. }
  3465. /*
  3466. * Enable the hub-initiated U1/U2 idle timeouts, and enable device-initiated
  3467. * U1/U2 entry.
  3468. *
  3469. * We will attempt to enable U1 or U2, but there are no guarantees that the
  3470. * control transfers to set the hub timeout or enable device-initiated U1/U2
  3471. * will be successful.
  3472. *
  3473. * If we cannot set the parent hub U1/U2 timeout, we attempt to let the xHCI
  3474. * driver know about it. If that call fails, it should be harmless, and just
  3475. * take up more slightly more bus bandwidth for unnecessary U1/U2 exit latency.
  3476. */
  3477. static void usb_enable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
  3478. enum usb3_link_state state)
  3479. {
  3480. int timeout, ret;
  3481. __u8 u1_mel = udev->bos->ss_cap->bU1devExitLat;
  3482. __le16 u2_mel = udev->bos->ss_cap->bU2DevExitLat;
  3483. /* If the device says it doesn't have *any* exit latency to come out of
  3484. * U1 or U2, it's probably lying. Assume it doesn't implement that link
  3485. * state.
  3486. */
  3487. if ((state == USB3_LPM_U1 && u1_mel == 0) ||
  3488. (state == USB3_LPM_U2 && u2_mel == 0))
  3489. return;
  3490. /*
  3491. * First, let the device know about the exit latencies
  3492. * associated with the link state we're about to enable.
  3493. */
  3494. ret = usb_req_set_sel(udev, state);
  3495. if (ret < 0) {
  3496. dev_warn(&udev->dev, "Set SEL for device-initiated %s failed.\n",
  3497. usb3_lpm_names[state]);
  3498. return;
  3499. }
  3500. /* We allow the host controller to set the U1/U2 timeout internally
  3501. * first, so that it can change its schedule to account for the
  3502. * additional latency to send data to a device in a lower power
  3503. * link state.
  3504. */
  3505. timeout = hcd->driver->enable_usb3_lpm_timeout(hcd, udev, state);
  3506. /* xHCI host controller doesn't want to enable this LPM state. */
  3507. if (timeout == 0)
  3508. return;
  3509. if (timeout < 0) {
  3510. dev_warn(&udev->dev, "Could not enable %s link state, "
  3511. "xHCI error %i.\n", usb3_lpm_names[state],
  3512. timeout);
  3513. return;
  3514. }
  3515. if (usb_set_lpm_timeout(udev, state, timeout)) {
  3516. /* If we can't set the parent hub U1/U2 timeout,
  3517. * device-initiated LPM won't be allowed either, so let the xHCI
  3518. * host know that this link state won't be enabled.
  3519. */
  3520. hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state);
  3521. } else {
  3522. /* Only a configured device will accept the Set Feature
  3523. * U1/U2_ENABLE
  3524. */
  3525. if (udev->actconfig)
  3526. usb_set_device_initiated_lpm(udev, state, true);
  3527. /* As soon as usb_set_lpm_timeout(timeout) returns 0, the
  3528. * hub-initiated LPM is enabled. Thus, LPM is enabled no
  3529. * matter the result of usb_set_device_initiated_lpm().
  3530. * The only difference is whether device is able to initiate
  3531. * LPM.
  3532. */
  3533. if (state == USB3_LPM_U1)
  3534. udev->usb3_lpm_u1_enabled = 1;
  3535. else if (state == USB3_LPM_U2)
  3536. udev->usb3_lpm_u2_enabled = 1;
  3537. }
  3538. }
  3539. /*
  3540. * Disable the hub-initiated U1/U2 idle timeouts, and disable device-initiated
  3541. * U1/U2 entry.
  3542. *
  3543. * If this function returns -EBUSY, the parent hub will still allow U1/U2 entry.
  3544. * If zero is returned, the parent will not allow the link to go into U1/U2.
  3545. *
  3546. * If zero is returned, device-initiated U1/U2 entry may still be enabled, but
  3547. * it won't have an effect on the bus link state because the parent hub will
  3548. * still disallow device-initiated U1/U2 entry.
  3549. *
  3550. * If zero is returned, the xHCI host controller may still think U1/U2 entry is
  3551. * possible. The result will be slightly more bus bandwidth will be taken up
  3552. * (to account for U1/U2 exit latency), but it should be harmless.
  3553. */
  3554. static int usb_disable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
  3555. enum usb3_link_state state)
  3556. {
  3557. switch (state) {
  3558. case USB3_LPM_U1:
  3559. case USB3_LPM_U2:
  3560. break;
  3561. default:
  3562. dev_warn(&udev->dev, "%s: Can't disable non-U1 or U2 state.\n",
  3563. __func__);
  3564. return -EINVAL;
  3565. }
  3566. if (usb_set_lpm_timeout(udev, state, 0))
  3567. return -EBUSY;
  3568. usb_set_device_initiated_lpm(udev, state, false);
  3569. if (hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state))
  3570. dev_warn(&udev->dev, "Could not disable xHCI %s timeout, "
  3571. "bus schedule bandwidth may be impacted.\n",
  3572. usb3_lpm_names[state]);
  3573. /* As soon as usb_set_lpm_timeout(0) return 0, hub initiated LPM
  3574. * is disabled. Hub will disallows link to enter U1/U2 as well,
  3575. * even device is initiating LPM. Hence LPM is disabled if hub LPM
  3576. * timeout set to 0, no matter device-initiated LPM is disabled or
  3577. * not.
  3578. */
  3579. if (state == USB3_LPM_U1)
  3580. udev->usb3_lpm_u1_enabled = 0;
  3581. else if (state == USB3_LPM_U2)
  3582. udev->usb3_lpm_u2_enabled = 0;
  3583. return 0;
  3584. }
  3585. /*
  3586. * Disable hub-initiated and device-initiated U1 and U2 entry.
  3587. * Caller must own the bandwidth_mutex.
  3588. *
  3589. * This will call usb_enable_lpm() on failure, which will decrement
  3590. * lpm_disable_count, and will re-enable LPM if lpm_disable_count reaches zero.
  3591. */
  3592. int usb_disable_lpm(struct usb_device *udev)
  3593. {
  3594. struct usb_hcd *hcd;
  3595. if (!udev || !udev->parent ||
  3596. udev->speed < USB_SPEED_SUPER ||
  3597. !udev->lpm_capable ||
  3598. udev->state < USB_STATE_DEFAULT)
  3599. return 0;
  3600. hcd = bus_to_hcd(udev->bus);
  3601. if (!hcd || !hcd->driver->disable_usb3_lpm_timeout)
  3602. return 0;
  3603. udev->lpm_disable_count++;
  3604. if ((udev->u1_params.timeout == 0 && udev->u2_params.timeout == 0))
  3605. return 0;
  3606. /* If LPM is enabled, attempt to disable it. */
  3607. if (usb_disable_link_state(hcd, udev, USB3_LPM_U1))
  3608. goto enable_lpm;
  3609. if (usb_disable_link_state(hcd, udev, USB3_LPM_U2))
  3610. goto enable_lpm;
  3611. return 0;
  3612. enable_lpm:
  3613. usb_enable_lpm(udev);
  3614. return -EBUSY;
  3615. }
  3616. EXPORT_SYMBOL_GPL(usb_disable_lpm);
  3617. /* Grab the bandwidth_mutex before calling usb_disable_lpm() */
  3618. int usb_unlocked_disable_lpm(struct usb_device *udev)
  3619. {
  3620. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  3621. int ret;
  3622. if (!hcd)
  3623. return -EINVAL;
  3624. mutex_lock(hcd->bandwidth_mutex);
  3625. ret = usb_disable_lpm(udev);
  3626. mutex_unlock(hcd->bandwidth_mutex);
  3627. return ret;
  3628. }
  3629. EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
  3630. /*
  3631. * Attempt to enable device-initiated and hub-initiated U1 and U2 entry. The
  3632. * xHCI host policy may prevent U1 or U2 from being enabled.
  3633. *
  3634. * Other callers may have disabled link PM, so U1 and U2 entry will be disabled
  3635. * until the lpm_disable_count drops to zero. Caller must own the
  3636. * bandwidth_mutex.
  3637. */
  3638. void usb_enable_lpm(struct usb_device *udev)
  3639. {
  3640. struct usb_hcd *hcd;
  3641. struct usb_hub *hub;
  3642. struct usb_port *port_dev;
  3643. if (!udev || !udev->parent ||
  3644. udev->speed < USB_SPEED_SUPER ||
  3645. !udev->lpm_capable ||
  3646. udev->state < USB_STATE_DEFAULT)
  3647. return;
  3648. udev->lpm_disable_count--;
  3649. hcd = bus_to_hcd(udev->bus);
  3650. /* Double check that we can both enable and disable LPM.
  3651. * Device must be configured to accept set feature U1/U2 timeout.
  3652. */
  3653. if (!hcd || !hcd->driver->enable_usb3_lpm_timeout ||
  3654. !hcd->driver->disable_usb3_lpm_timeout)
  3655. return;
  3656. if (udev->lpm_disable_count > 0)
  3657. return;
  3658. hub = usb_hub_to_struct_hub(udev->parent);
  3659. if (!hub)
  3660. return;
  3661. port_dev = hub->ports[udev->portnum - 1];
  3662. if (port_dev->usb3_lpm_u1_permit)
  3663. usb_enable_link_state(hcd, udev, USB3_LPM_U1);
  3664. if (port_dev->usb3_lpm_u2_permit)
  3665. usb_enable_link_state(hcd, udev, USB3_LPM_U2);
  3666. }
  3667. EXPORT_SYMBOL_GPL(usb_enable_lpm);
  3668. /* Grab the bandwidth_mutex before calling usb_enable_lpm() */
  3669. void usb_unlocked_enable_lpm(struct usb_device *udev)
  3670. {
  3671. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  3672. if (!hcd)
  3673. return;
  3674. mutex_lock(hcd->bandwidth_mutex);
  3675. usb_enable_lpm(udev);
  3676. mutex_unlock(hcd->bandwidth_mutex);
  3677. }
  3678. EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
  3679. /* usb3 devices use U3 for disabled, make sure remote wakeup is disabled */
  3680. static void hub_usb3_port_prepare_disable(struct usb_hub *hub,
  3681. struct usb_port *port_dev)
  3682. {
  3683. struct usb_device *udev = port_dev->child;
  3684. int ret;
  3685. if (udev && udev->port_is_suspended && udev->do_remote_wakeup) {
  3686. ret = hub_set_port_link_state(hub, port_dev->portnum,
  3687. USB_SS_PORT_LS_U0);
  3688. if (!ret) {
  3689. msleep(USB_RESUME_TIMEOUT);
  3690. ret = usb_disable_remote_wakeup(udev);
  3691. }
  3692. if (ret)
  3693. dev_warn(&udev->dev,
  3694. "Port disable: can't disable remote wake\n");
  3695. udev->do_remote_wakeup = 0;
  3696. }
  3697. }
  3698. #else /* CONFIG_PM */
  3699. #define hub_suspend NULL
  3700. #define hub_resume NULL
  3701. #define hub_reset_resume NULL
  3702. static inline void hub_usb3_port_prepare_disable(struct usb_hub *hub,
  3703. struct usb_port *port_dev) { }
  3704. int usb_disable_lpm(struct usb_device *udev)
  3705. {
  3706. return 0;
  3707. }
  3708. EXPORT_SYMBOL_GPL(usb_disable_lpm);
  3709. void usb_enable_lpm(struct usb_device *udev) { }
  3710. EXPORT_SYMBOL_GPL(usb_enable_lpm);
  3711. int usb_unlocked_disable_lpm(struct usb_device *udev)
  3712. {
  3713. return 0;
  3714. }
  3715. EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
  3716. void usb_unlocked_enable_lpm(struct usb_device *udev) { }
  3717. EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
  3718. int usb_disable_ltm(struct usb_device *udev)
  3719. {
  3720. return 0;
  3721. }
  3722. EXPORT_SYMBOL_GPL(usb_disable_ltm);
  3723. void usb_enable_ltm(struct usb_device *udev) { }
  3724. EXPORT_SYMBOL_GPL(usb_enable_ltm);
  3725. static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
  3726. u16 portstatus, u16 portchange)
  3727. {
  3728. return 0;
  3729. }
  3730. #endif /* CONFIG_PM */
  3731. /*
  3732. * USB-3 does not have a similar link state as USB-2 that will avoid negotiating
  3733. * a connection with a plugged-in cable but will signal the host when the cable
  3734. * is unplugged. Disable remote wake and set link state to U3 for USB-3 devices
  3735. */
  3736. static int hub_port_disable(struct usb_hub *hub, int port1, int set_state)
  3737. {
  3738. struct usb_port *port_dev = hub->ports[port1 - 1];
  3739. struct usb_device *hdev = hub->hdev;
  3740. int ret = 0;
  3741. if (!hub->error) {
  3742. if (hub_is_superspeed(hub->hdev)) {
  3743. hub_usb3_port_prepare_disable(hub, port_dev);
  3744. ret = hub_set_port_link_state(hub, port_dev->portnum,
  3745. USB_SS_PORT_LS_U3);
  3746. } else {
  3747. ret = usb_clear_port_feature(hdev, port1,
  3748. USB_PORT_FEAT_ENABLE);
  3749. }
  3750. }
  3751. if (port_dev->child && set_state)
  3752. usb_set_device_state(port_dev->child, USB_STATE_NOTATTACHED);
  3753. if (ret && ret != -ENODEV)
  3754. dev_err(&port_dev->dev, "cannot disable (err = %d)\n", ret);
  3755. return ret;
  3756. }
  3757. /*
  3758. * usb_port_disable - disable a usb device's upstream port
  3759. * @udev: device to disable
  3760. * Context: @udev locked, must be able to sleep.
  3761. *
  3762. * Disables a USB device that isn't in active use.
  3763. */
  3764. int usb_port_disable(struct usb_device *udev)
  3765. {
  3766. struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
  3767. return hub_port_disable(hub, udev->portnum, 0);
  3768. }
  3769. /* USB 2.0 spec, 7.1.7.3 / fig 7-29:
  3770. *
  3771. * Between connect detection and reset signaling there must be a delay
  3772. * of 100ms at least for debounce and power-settling. The corresponding
  3773. * timer shall restart whenever the downstream port detects a disconnect.
  3774. *
  3775. * Apparently there are some bluetooth and irda-dongles and a number of
  3776. * low-speed devices for which this debounce period may last over a second.
  3777. * Not covered by the spec - but easy to deal with.
  3778. *
  3779. * This implementation uses a 1500ms total debounce timeout; if the
  3780. * connection isn't stable by then it returns -ETIMEDOUT. It checks
  3781. * every 25ms for transient disconnects. When the port status has been
  3782. * unchanged for 100ms it returns the port status.
  3783. */
  3784. int hub_port_debounce(struct usb_hub *hub, int port1, bool must_be_connected)
  3785. {
  3786. int ret;
  3787. u16 portchange, portstatus;
  3788. unsigned connection = 0xffff;
  3789. int total_time, stable_time = 0;
  3790. struct usb_port *port_dev = hub->ports[port1 - 1];
  3791. for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
  3792. ret = hub_port_status(hub, port1, &portstatus, &portchange);
  3793. if (ret < 0)
  3794. return ret;
  3795. if (!(portchange & USB_PORT_STAT_C_CONNECTION) &&
  3796. (portstatus & USB_PORT_STAT_CONNECTION) == connection) {
  3797. if (!must_be_connected ||
  3798. (connection == USB_PORT_STAT_CONNECTION))
  3799. stable_time += HUB_DEBOUNCE_STEP;
  3800. if (stable_time >= HUB_DEBOUNCE_STABLE)
  3801. break;
  3802. } else {
  3803. stable_time = 0;
  3804. connection = portstatus & USB_PORT_STAT_CONNECTION;
  3805. }
  3806. if (portchange & USB_PORT_STAT_C_CONNECTION) {
  3807. usb_clear_port_feature(hub->hdev, port1,
  3808. USB_PORT_FEAT_C_CONNECTION);
  3809. }
  3810. if (total_time >= HUB_DEBOUNCE_TIMEOUT)
  3811. break;
  3812. msleep(HUB_DEBOUNCE_STEP);
  3813. }
  3814. dev_dbg(&port_dev->dev, "debounce total %dms stable %dms status 0x%x\n",
  3815. total_time, stable_time, portstatus);
  3816. if (stable_time < HUB_DEBOUNCE_STABLE)
  3817. return -ETIMEDOUT;
  3818. return portstatus;
  3819. }
  3820. void usb_ep0_reinit(struct usb_device *udev)
  3821. {
  3822. usb_disable_endpoint(udev, 0 + USB_DIR_IN, true);
  3823. usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true);
  3824. usb_enable_endpoint(udev, &udev->ep0, true);
  3825. }
  3826. EXPORT_SYMBOL_GPL(usb_ep0_reinit);
  3827. #define usb_sndaddr0pipe() (PIPE_CONTROL << 30)
  3828. #define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN)
  3829. static int hub_set_address(struct usb_device *udev, int devnum)
  3830. {
  3831. int retval;
  3832. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  3833. /*
  3834. * The host controller will choose the device address,
  3835. * instead of the core having chosen it earlier
  3836. */
  3837. if (!hcd->driver->address_device && devnum <= 1)
  3838. return -EINVAL;
  3839. if (udev->state == USB_STATE_ADDRESS)
  3840. return 0;
  3841. if (udev->state != USB_STATE_DEFAULT)
  3842. return -EINVAL;
  3843. if (hcd->driver->address_device)
  3844. retval = hcd->driver->address_device(hcd, udev);
  3845. else
  3846. retval = usb_control_msg(udev, usb_sndaddr0pipe(),
  3847. USB_REQ_SET_ADDRESS, 0, devnum, 0,
  3848. NULL, 0, USB_CTRL_SET_TIMEOUT);
  3849. if (retval == 0) {
  3850. update_devnum(udev, devnum);
  3851. /* Device now using proper address. */
  3852. usb_set_device_state(udev, USB_STATE_ADDRESS);
  3853. usb_ep0_reinit(udev);
  3854. }
  3855. return retval;
  3856. }
  3857. /*
  3858. * There are reports of USB 3.0 devices that say they support USB 2.0 Link PM
  3859. * when they're plugged into a USB 2.0 port, but they don't work when LPM is
  3860. * enabled.
  3861. *
  3862. * Only enable USB 2.0 Link PM if the port is internal (hardwired), or the
  3863. * device says it supports the new USB 2.0 Link PM errata by setting the BESL
  3864. * support bit in the BOS descriptor.
  3865. */
  3866. static void hub_set_initial_usb2_lpm_policy(struct usb_device *udev)
  3867. {
  3868. struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
  3869. int connect_type = USB_PORT_CONNECT_TYPE_UNKNOWN;
  3870. if (!udev->usb2_hw_lpm_capable || !udev->bos)
  3871. return;
  3872. if (hub)
  3873. connect_type = hub->ports[udev->portnum - 1]->connect_type;
  3874. if ((udev->bos->ext_cap->bmAttributes & cpu_to_le32(USB_BESL_SUPPORT)) ||
  3875. connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) {
  3876. udev->usb2_hw_lpm_allowed = 1;
  3877. usb_set_usb2_hardware_lpm(udev, 1);
  3878. }
  3879. }
  3880. static int hub_enable_device(struct usb_device *udev)
  3881. {
  3882. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  3883. if (!hcd->driver->enable_device)
  3884. return 0;
  3885. if (udev->state == USB_STATE_ADDRESS)
  3886. return 0;
  3887. if (udev->state != USB_STATE_DEFAULT)
  3888. return -EINVAL;
  3889. return hcd->driver->enable_device(hcd, udev);
  3890. }
  3891. /* Reset device, (re)assign address, get device descriptor.
  3892. * Device connection must be stable, no more debouncing needed.
  3893. * Returns device in USB_STATE_ADDRESS, except on error.
  3894. *
  3895. * If this is called for an already-existing device (as part of
  3896. * usb_reset_and_verify_device), the caller must own the device lock and
  3897. * the port lock. For a newly detected device that is not accessible
  3898. * through any global pointers, it's not necessary to lock the device,
  3899. * but it is still necessary to lock the port.
  3900. */
  3901. static int
  3902. hub_port_init(struct usb_hub *hub, struct usb_device *udev, int port1,
  3903. int retry_counter)
  3904. {
  3905. struct usb_device *hdev = hub->hdev;
  3906. struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
  3907. struct usb_port *port_dev = hub->ports[port1 - 1];
  3908. int retries, operations, retval, i;
  3909. unsigned delay = HUB_SHORT_RESET_TIME;
  3910. enum usb_device_speed oldspeed = udev->speed;
  3911. const char *speed;
  3912. int devnum = udev->devnum;
  3913. const char *driver_name;
  3914. /* root hub ports have a slightly longer reset period
  3915. * (from USB 2.0 spec, section 7.1.7.5)
  3916. */
  3917. if (!hdev->parent) {
  3918. delay = HUB_ROOT_RESET_TIME;
  3919. if (port1 == hdev->bus->otg_port)
  3920. hdev->bus->b_hnp_enable = 0;
  3921. }
  3922. /* Some low speed devices have problems with the quick delay, so */
  3923. /* be a bit pessimistic with those devices. RHbug #23670 */
  3924. if (oldspeed == USB_SPEED_LOW)
  3925. delay = HUB_LONG_RESET_TIME;
  3926. mutex_lock(hcd->address0_mutex);
  3927. /* Reset the device; full speed may morph to high speed */
  3928. /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */
  3929. retval = hub_port_reset(hub, port1, udev, delay, false);
  3930. if (retval < 0) /* error or disconnect */
  3931. goto fail;
  3932. /* success, speed is known */
  3933. retval = -ENODEV;
  3934. /* Don't allow speed changes at reset, except usb 3.0 to faster */
  3935. if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed &&
  3936. !(oldspeed == USB_SPEED_SUPER && udev->speed > oldspeed)) {
  3937. dev_dbg(&udev->dev, "device reset changed speed!\n");
  3938. goto fail;
  3939. }
  3940. oldspeed = udev->speed;
  3941. /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
  3942. * it's fixed size except for full speed devices.
  3943. * For Wireless USB devices, ep0 max packet is always 512 (tho
  3944. * reported as 0xff in the device descriptor). WUSB1.0[4.8.1].
  3945. */
  3946. switch (udev->speed) {
  3947. case USB_SPEED_SUPER_PLUS:
  3948. case USB_SPEED_SUPER:
  3949. case USB_SPEED_WIRELESS: /* fixed at 512 */
  3950. udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
  3951. break;
  3952. case USB_SPEED_HIGH: /* fixed at 64 */
  3953. udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
  3954. break;
  3955. case USB_SPEED_FULL: /* 8, 16, 32, or 64 */
  3956. /* to determine the ep0 maxpacket size, try to read
  3957. * the device descriptor to get bMaxPacketSize0 and
  3958. * then correct our initial guess.
  3959. */
  3960. udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
  3961. break;
  3962. case USB_SPEED_LOW: /* fixed at 8 */
  3963. udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8);
  3964. break;
  3965. default:
  3966. goto fail;
  3967. }
  3968. if (udev->speed == USB_SPEED_WIRELESS)
  3969. speed = "variable speed Wireless";
  3970. else
  3971. speed = usb_speed_string(udev->speed);
  3972. /*
  3973. * The controller driver may be NULL if the controller device
  3974. * is the middle device between platform device and roothub.
  3975. * This middle device may not need a device driver due to
  3976. * all hardware control can be at platform device driver, this
  3977. * platform device is usually a dual-role USB controller device.
  3978. */
  3979. if (udev->bus->controller->driver)
  3980. driver_name = udev->bus->controller->driver->name;
  3981. else
  3982. driver_name = udev->bus->sysdev->driver->name;
  3983. if (udev->speed < USB_SPEED_SUPER)
  3984. dev_info(&udev->dev,
  3985. "%s %s USB device number %d using %s\n",
  3986. (udev->config) ? "reset" : "new", speed,
  3987. devnum, driver_name);
  3988. /* Set up TT records, if needed */
  3989. if (hdev->tt) {
  3990. udev->tt = hdev->tt;
  3991. udev->ttport = hdev->ttport;
  3992. } else if (udev->speed != USB_SPEED_HIGH
  3993. && hdev->speed == USB_SPEED_HIGH) {
  3994. if (!hub->tt.hub) {
  3995. dev_err(&udev->dev, "parent hub has no TT\n");
  3996. retval = -EINVAL;
  3997. goto fail;
  3998. }
  3999. udev->tt = &hub->tt;
  4000. udev->ttport = port1;
  4001. }
  4002. /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way?
  4003. * Because device hardware and firmware is sometimes buggy in
  4004. * this area, and this is how Linux has done it for ages.
  4005. * Change it cautiously.
  4006. *
  4007. * NOTE: If use_new_scheme() is true we will start by issuing
  4008. * a 64-byte GET_DESCRIPTOR request. This is what Windows does,
  4009. * so it may help with some non-standards-compliant devices.
  4010. * Otherwise we start with SET_ADDRESS and then try to read the
  4011. * first 8 bytes of the device descriptor to get the ep0 maxpacket
  4012. * value.
  4013. */
  4014. for (retries = 0; retries < GET_DESCRIPTOR_TRIES; (++retries, msleep(100))) {
  4015. bool did_new_scheme = false;
  4016. if (use_new_scheme(udev, retry_counter, port_dev)) {
  4017. struct usb_device_descriptor *buf;
  4018. int r = 0;
  4019. did_new_scheme = true;
  4020. retval = hub_enable_device(udev);
  4021. if (retval < 0) {
  4022. dev_err(&udev->dev,
  4023. "hub failed to enable device, error %d\n",
  4024. retval);
  4025. goto fail;
  4026. }
  4027. #define GET_DESCRIPTOR_BUFSIZE 64
  4028. buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO);
  4029. if (!buf) {
  4030. retval = -ENOMEM;
  4031. continue;
  4032. }
  4033. /* Retry on all errors; some devices are flakey.
  4034. * 255 is for WUSB devices, we actually need to use
  4035. * 512 (WUSB1.0[4.8.1]).
  4036. */
  4037. for (operations = 0; operations < 3; ++operations) {
  4038. buf->bMaxPacketSize0 = 0;
  4039. r = usb_control_msg(udev, usb_rcvaddr0pipe(),
  4040. USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
  4041. USB_DT_DEVICE << 8, 0,
  4042. buf, GET_DESCRIPTOR_BUFSIZE,
  4043. initial_descriptor_timeout);
  4044. switch (buf->bMaxPacketSize0) {
  4045. case 8: case 16: case 32: case 64: case 255:
  4046. if (buf->bDescriptorType ==
  4047. USB_DT_DEVICE) {
  4048. r = 0;
  4049. break;
  4050. }
  4051. /* FALL THROUGH */
  4052. default:
  4053. if (r == 0)
  4054. r = -EPROTO;
  4055. break;
  4056. }
  4057. /*
  4058. * Some devices time out if they are powered on
  4059. * when already connected. They need a second
  4060. * reset. But only on the first attempt,
  4061. * lest we get into a time out/reset loop
  4062. */
  4063. if (r == 0 || (r == -ETIMEDOUT &&
  4064. retries == 0 &&
  4065. udev->speed > USB_SPEED_FULL))
  4066. break;
  4067. }
  4068. udev->descriptor.bMaxPacketSize0 =
  4069. buf->bMaxPacketSize0;
  4070. kfree(buf);
  4071. retval = hub_port_reset(hub, port1, udev, delay, false);
  4072. if (retval < 0) /* error or disconnect */
  4073. goto fail;
  4074. if (oldspeed != udev->speed) {
  4075. dev_dbg(&udev->dev,
  4076. "device reset changed speed!\n");
  4077. retval = -ENODEV;
  4078. goto fail;
  4079. }
  4080. if (r) {
  4081. if (r != -ENODEV)
  4082. dev_err(&udev->dev, "device descriptor read/64, error %d\n",
  4083. r);
  4084. retval = -EMSGSIZE;
  4085. continue;
  4086. }
  4087. #undef GET_DESCRIPTOR_BUFSIZE
  4088. }
  4089. /*
  4090. * If device is WUSB, we already assigned an
  4091. * unauthorized address in the Connect Ack sequence;
  4092. * authorization will assign the final address.
  4093. */
  4094. if (udev->wusb == 0) {
  4095. for (operations = 0; operations < SET_ADDRESS_TRIES; ++operations) {
  4096. retval = hub_set_address(udev, devnum);
  4097. if (retval >= 0)
  4098. break;
  4099. msleep(200);
  4100. }
  4101. if (retval < 0) {
  4102. if (retval != -ENODEV)
  4103. dev_err(&udev->dev, "device not accepting address %d, error %d\n",
  4104. devnum, retval);
  4105. goto fail;
  4106. }
  4107. if (udev->speed >= USB_SPEED_SUPER) {
  4108. devnum = udev->devnum;
  4109. dev_info(&udev->dev,
  4110. "%s SuperSpeed%s%s USB device number %d using %s\n",
  4111. (udev->config) ? "reset" : "new",
  4112. (udev->speed == USB_SPEED_SUPER_PLUS) ?
  4113. "Plus Gen 2" : " Gen 1",
  4114. (udev->rx_lanes == 2 && udev->tx_lanes == 2) ?
  4115. "x2" : "",
  4116. devnum, driver_name);
  4117. }
  4118. /* cope with hardware quirkiness:
  4119. * - let SET_ADDRESS settle, some device hardware wants it
  4120. * - read ep0 maxpacket even for high and low speed,
  4121. */
  4122. msleep(10);
  4123. /* use_new_scheme() checks the speed which may have
  4124. * changed since the initial look so we cache the result
  4125. * in did_new_scheme
  4126. */
  4127. if (did_new_scheme)
  4128. break;
  4129. }
  4130. retval = usb_get_device_descriptor(udev, 8);
  4131. if (retval < 8) {
  4132. if (retval != -ENODEV)
  4133. dev_err(&udev->dev,
  4134. "device descriptor read/8, error %d\n",
  4135. retval);
  4136. if (retval >= 0)
  4137. retval = -EMSGSIZE;
  4138. } else {
  4139. u32 delay;
  4140. retval = 0;
  4141. delay = udev->parent->hub_delay;
  4142. udev->hub_delay = min_t(u32, delay,
  4143. USB_TP_TRANSMISSION_DELAY_MAX);
  4144. retval = usb_set_isoch_delay(udev);
  4145. if (retval) {
  4146. dev_dbg(&udev->dev,
  4147. "Failed set isoch delay, error %d\n",
  4148. retval);
  4149. retval = 0;
  4150. }
  4151. break;
  4152. }
  4153. }
  4154. if (retval)
  4155. goto fail;
  4156. /*
  4157. * Some superspeed devices have finished the link training process
  4158. * and attached to a superspeed hub port, but the device descriptor
  4159. * got from those devices show they aren't superspeed devices. Warm
  4160. * reset the port attached by the devices can fix them.
  4161. */
  4162. if ((udev->speed >= USB_SPEED_SUPER) &&
  4163. (le16_to_cpu(udev->descriptor.bcdUSB) < 0x0300)) {
  4164. dev_err(&udev->dev, "got a wrong device descriptor, "
  4165. "warm reset device\n");
  4166. hub_port_reset(hub, port1, udev,
  4167. HUB_BH_RESET_TIME, true);
  4168. retval = -EINVAL;
  4169. goto fail;
  4170. }
  4171. if (udev->descriptor.bMaxPacketSize0 == 0xff ||
  4172. udev->speed >= USB_SPEED_SUPER)
  4173. i = 512;
  4174. else
  4175. i = udev->descriptor.bMaxPacketSize0;
  4176. if (usb_endpoint_maxp(&udev->ep0.desc) != i) {
  4177. if (udev->speed == USB_SPEED_LOW ||
  4178. !(i == 8 || i == 16 || i == 32 || i == 64)) {
  4179. dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", i);
  4180. retval = -EMSGSIZE;
  4181. goto fail;
  4182. }
  4183. if (udev->speed == USB_SPEED_FULL)
  4184. dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i);
  4185. else
  4186. dev_warn(&udev->dev, "Using ep0 maxpacket: %d\n", i);
  4187. udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i);
  4188. usb_ep0_reinit(udev);
  4189. }
  4190. retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE);
  4191. if (retval < (signed)sizeof(udev->descriptor)) {
  4192. if (retval != -ENODEV)
  4193. dev_err(&udev->dev, "device descriptor read/all, error %d\n",
  4194. retval);
  4195. if (retval >= 0)
  4196. retval = -ENOMSG;
  4197. goto fail;
  4198. }
  4199. usb_detect_quirks(udev);
  4200. if (udev->wusb == 0 && le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0201) {
  4201. retval = usb_get_bos_descriptor(udev);
  4202. if (!retval) {
  4203. udev->lpm_capable = usb_device_supports_lpm(udev);
  4204. usb_set_lpm_parameters(udev);
  4205. }
  4206. }
  4207. retval = 0;
  4208. /* notify HCD that we have a device connected and addressed */
  4209. if (hcd->driver->update_device)
  4210. hcd->driver->update_device(hcd, udev);
  4211. hub_set_initial_usb2_lpm_policy(udev);
  4212. fail:
  4213. if (retval) {
  4214. hub_port_disable(hub, port1, 0);
  4215. update_devnum(udev, devnum); /* for disconnect processing */
  4216. }
  4217. mutex_unlock(hcd->address0_mutex);
  4218. return retval;
  4219. }
  4220. static void
  4221. check_highspeed(struct usb_hub *hub, struct usb_device *udev, int port1)
  4222. {
  4223. struct usb_qualifier_descriptor *qual;
  4224. int status;
  4225. if (udev->quirks & USB_QUIRK_DEVICE_QUALIFIER)
  4226. return;
  4227. qual = kmalloc(sizeof *qual, GFP_KERNEL);
  4228. if (qual == NULL)
  4229. return;
  4230. status = usb_get_descriptor(udev, USB_DT_DEVICE_QUALIFIER, 0,
  4231. qual, sizeof *qual);
  4232. if (status == sizeof *qual) {
  4233. dev_info(&udev->dev, "not running at top speed; "
  4234. "connect to a high speed hub\n");
  4235. /* hub LEDs are probably harder to miss than syslog */
  4236. if (hub->has_indicators) {
  4237. hub->indicator[port1-1] = INDICATOR_GREEN_BLINK;
  4238. queue_delayed_work(system_power_efficient_wq,
  4239. &hub->leds, 0);
  4240. }
  4241. }
  4242. kfree(qual);
  4243. }
  4244. static unsigned
  4245. hub_power_remaining(struct usb_hub *hub)
  4246. {
  4247. struct usb_device *hdev = hub->hdev;
  4248. int remaining;
  4249. int port1;
  4250. if (!hub->limited_power)
  4251. return 0;
  4252. remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent;
  4253. for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
  4254. struct usb_port *port_dev = hub->ports[port1 - 1];
  4255. struct usb_device *udev = port_dev->child;
  4256. unsigned unit_load;
  4257. int delta;
  4258. if (!udev)
  4259. continue;
  4260. if (hub_is_superspeed(udev))
  4261. unit_load = 150;
  4262. else
  4263. unit_load = 100;
  4264. /*
  4265. * Unconfigured devices may not use more than one unit load,
  4266. * or 8mA for OTG ports
  4267. */
  4268. if (udev->actconfig)
  4269. delta = usb_get_max_power(udev, udev->actconfig);
  4270. else if (port1 != udev->bus->otg_port || hdev->parent)
  4271. delta = unit_load;
  4272. else
  4273. delta = 8;
  4274. if (delta > hub->mA_per_port)
  4275. dev_warn(&port_dev->dev, "%dmA is over %umA budget!\n",
  4276. delta, hub->mA_per_port);
  4277. remaining -= delta;
  4278. }
  4279. if (remaining < 0) {
  4280. dev_warn(hub->intfdev, "%dmA over power budget!\n",
  4281. -remaining);
  4282. remaining = 0;
  4283. }
  4284. return remaining;
  4285. }
  4286. static void hub_port_connect(struct usb_hub *hub, int port1, u16 portstatus,
  4287. u16 portchange)
  4288. {
  4289. int status = -ENODEV;
  4290. int i;
  4291. unsigned unit_load;
  4292. struct usb_device *hdev = hub->hdev;
  4293. struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
  4294. struct usb_port *port_dev = hub->ports[port1 - 1];
  4295. struct usb_device *udev = port_dev->child;
  4296. static int unreliable_port = -1;
  4297. /* Disconnect any existing devices under this port */
  4298. if (udev) {
  4299. if (hcd->usb_phy && !hdev->parent)
  4300. usb_phy_notify_disconnect(hcd->usb_phy, udev->speed);
  4301. usb_disconnect(&port_dev->child);
  4302. }
  4303. /* We can forget about a "removed" device when there's a physical
  4304. * disconnect or the connect status changes.
  4305. */
  4306. if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
  4307. (portchange & USB_PORT_STAT_C_CONNECTION))
  4308. clear_bit(port1, hub->removed_bits);
  4309. if (portchange & (USB_PORT_STAT_C_CONNECTION |
  4310. USB_PORT_STAT_C_ENABLE)) {
  4311. status = hub_port_debounce_be_stable(hub, port1);
  4312. if (status < 0) {
  4313. if (status != -ENODEV &&
  4314. port1 != unreliable_port &&
  4315. printk_ratelimit())
  4316. dev_err(&port_dev->dev, "connect-debounce failed\n");
  4317. portstatus &= ~USB_PORT_STAT_CONNECTION;
  4318. unreliable_port = port1;
  4319. } else {
  4320. portstatus = status;
  4321. }
  4322. }
  4323. /* Return now if debouncing failed or nothing is connected or
  4324. * the device was "removed".
  4325. */
  4326. if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
  4327. test_bit(port1, hub->removed_bits)) {
  4328. /*
  4329. * maybe switch power back on (e.g. root hub was reset)
  4330. * but only if the port isn't owned by someone else.
  4331. */
  4332. if (hub_is_port_power_switchable(hub)
  4333. && !port_is_power_on(hub, portstatus)
  4334. && !port_dev->port_owner)
  4335. set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
  4336. if (portstatus & USB_PORT_STAT_ENABLE)
  4337. goto done;
  4338. return;
  4339. }
  4340. if (hub_is_superspeed(hub->hdev))
  4341. unit_load = 150;
  4342. else
  4343. unit_load = 100;
  4344. status = 0;
  4345. for (i = 0; i < SET_CONFIG_TRIES; i++) {
  4346. /* reallocate for each attempt, since references
  4347. * to the previous one can escape in various ways
  4348. */
  4349. udev = usb_alloc_dev(hdev, hdev->bus, port1);
  4350. if (!udev) {
  4351. dev_err(&port_dev->dev,
  4352. "couldn't allocate usb_device\n");
  4353. goto done;
  4354. }
  4355. usb_set_device_state(udev, USB_STATE_POWERED);
  4356. udev->bus_mA = hub->mA_per_port;
  4357. udev->level = hdev->level + 1;
  4358. udev->wusb = hub_is_wusb(hub);
  4359. /* Devices connected to SuperSpeed hubs are USB 3.0 or later */
  4360. if (hub_is_superspeed(hub->hdev))
  4361. udev->speed = USB_SPEED_SUPER;
  4362. else
  4363. udev->speed = USB_SPEED_UNKNOWN;
  4364. choose_devnum(udev);
  4365. if (udev->devnum <= 0) {
  4366. status = -ENOTCONN; /* Don't retry */
  4367. goto loop;
  4368. }
  4369. /* reset (non-USB 3.0 devices) and get descriptor */
  4370. usb_lock_port(port_dev);
  4371. status = hub_port_init(hub, udev, port1, i);
  4372. usb_unlock_port(port_dev);
  4373. if (status < 0)
  4374. goto loop;
  4375. if (udev->quirks & USB_QUIRK_DELAY_INIT)
  4376. msleep(2000);
  4377. /* consecutive bus-powered hubs aren't reliable; they can
  4378. * violate the voltage drop budget. if the new child has
  4379. * a "powered" LED, users should notice we didn't enable it
  4380. * (without reading syslog), even without per-port LEDs
  4381. * on the parent.
  4382. */
  4383. if (udev->descriptor.bDeviceClass == USB_CLASS_HUB
  4384. && udev->bus_mA <= unit_load) {
  4385. u16 devstat;
  4386. status = usb_get_std_status(udev, USB_RECIP_DEVICE, 0,
  4387. &devstat);
  4388. if (status) {
  4389. dev_dbg(&udev->dev, "get status %d ?\n", status);
  4390. goto loop_disable;
  4391. }
  4392. if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
  4393. dev_err(&udev->dev,
  4394. "can't connect bus-powered hub "
  4395. "to this port\n");
  4396. if (hub->has_indicators) {
  4397. hub->indicator[port1-1] =
  4398. INDICATOR_AMBER_BLINK;
  4399. queue_delayed_work(
  4400. system_power_efficient_wq,
  4401. &hub->leds, 0);
  4402. }
  4403. status = -ENOTCONN; /* Don't retry */
  4404. goto loop_disable;
  4405. }
  4406. }
  4407. /* check for devices running slower than they could */
  4408. if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200
  4409. && udev->speed == USB_SPEED_FULL
  4410. && highspeed_hubs != 0)
  4411. check_highspeed(hub, udev, port1);
  4412. /* Store the parent's children[] pointer. At this point
  4413. * udev becomes globally accessible, although presumably
  4414. * no one will look at it until hdev is unlocked.
  4415. */
  4416. status = 0;
  4417. mutex_lock(&usb_port_peer_mutex);
  4418. /* We mustn't add new devices if the parent hub has
  4419. * been disconnected; we would race with the
  4420. * recursively_mark_NOTATTACHED() routine.
  4421. */
  4422. spin_lock_irq(&device_state_lock);
  4423. if (hdev->state == USB_STATE_NOTATTACHED)
  4424. status = -ENOTCONN;
  4425. else
  4426. port_dev->child = udev;
  4427. spin_unlock_irq(&device_state_lock);
  4428. mutex_unlock(&usb_port_peer_mutex);
  4429. /* Run it through the hoops (find a driver, etc) */
  4430. if (!status) {
  4431. status = usb_new_device(udev);
  4432. if (status) {
  4433. mutex_lock(&usb_port_peer_mutex);
  4434. spin_lock_irq(&device_state_lock);
  4435. port_dev->child = NULL;
  4436. spin_unlock_irq(&device_state_lock);
  4437. mutex_unlock(&usb_port_peer_mutex);
  4438. } else {
  4439. if (hcd->usb_phy && !hdev->parent)
  4440. usb_phy_notify_connect(hcd->usb_phy,
  4441. udev->speed);
  4442. }
  4443. }
  4444. if (status)
  4445. goto loop_disable;
  4446. status = hub_power_remaining(hub);
  4447. if (status)
  4448. dev_dbg(hub->intfdev, "%dmA power budget left\n", status);
  4449. return;
  4450. loop_disable:
  4451. hub_port_disable(hub, port1, 1);
  4452. loop:
  4453. usb_ep0_reinit(udev);
  4454. release_devnum(udev);
  4455. hub_free_dev(udev);
  4456. usb_put_dev(udev);
  4457. if ((status == -ENOTCONN) || (status == -ENOTSUPP))
  4458. break;
  4459. /* When halfway through our retry count, power-cycle the port */
  4460. if (i == (SET_CONFIG_TRIES / 2) - 1) {
  4461. dev_info(&port_dev->dev, "attempt power cycle\n");
  4462. usb_hub_set_port_power(hdev, hub, port1, false);
  4463. msleep(2 * hub_power_on_good_delay(hub));
  4464. usb_hub_set_port_power(hdev, hub, port1, true);
  4465. msleep(hub_power_on_good_delay(hub));
  4466. }
  4467. }
  4468. if (hub->hdev->parent ||
  4469. !hcd->driver->port_handed_over ||
  4470. !(hcd->driver->port_handed_over)(hcd, port1)) {
  4471. if (status != -ENOTCONN && status != -ENODEV)
  4472. dev_err(&port_dev->dev,
  4473. "unable to enumerate USB device\n");
  4474. }
  4475. done:
  4476. hub_port_disable(hub, port1, 1);
  4477. if (hcd->driver->relinquish_port && !hub->hdev->parent) {
  4478. if (status != -ENOTCONN && status != -ENODEV)
  4479. hcd->driver->relinquish_port(hcd, port1);
  4480. }
  4481. }
  4482. /* Handle physical or logical connection change events.
  4483. * This routine is called when:
  4484. * a port connection-change occurs;
  4485. * a port enable-change occurs (often caused by EMI);
  4486. * usb_reset_and_verify_device() encounters changed descriptors (as from
  4487. * a firmware download)
  4488. * caller already locked the hub
  4489. */
  4490. static void hub_port_connect_change(struct usb_hub *hub, int port1,
  4491. u16 portstatus, u16 portchange)
  4492. __must_hold(&port_dev->status_lock)
  4493. {
  4494. struct usb_port *port_dev = hub->ports[port1 - 1];
  4495. struct usb_device *udev = port_dev->child;
  4496. int status = -ENODEV;
  4497. dev_dbg(&port_dev->dev, "status %04x, change %04x, %s\n", portstatus,
  4498. portchange, portspeed(hub, portstatus));
  4499. if (hub->has_indicators) {
  4500. set_port_led(hub, port1, HUB_LED_AUTO);
  4501. hub->indicator[port1-1] = INDICATOR_AUTO;
  4502. }
  4503. #ifdef CONFIG_USB_OTG
  4504. /* during HNP, don't repeat the debounce */
  4505. if (hub->hdev->bus->is_b_host)
  4506. portchange &= ~(USB_PORT_STAT_C_CONNECTION |
  4507. USB_PORT_STAT_C_ENABLE);
  4508. #endif
  4509. /* Try to resuscitate an existing device */
  4510. if ((portstatus & USB_PORT_STAT_CONNECTION) && udev &&
  4511. udev->state != USB_STATE_NOTATTACHED) {
  4512. if (portstatus & USB_PORT_STAT_ENABLE) {
  4513. status = 0; /* Nothing to do */
  4514. #ifdef CONFIG_PM
  4515. } else if (udev->state == USB_STATE_SUSPENDED &&
  4516. udev->persist_enabled) {
  4517. /* For a suspended device, treat this as a
  4518. * remote wakeup event.
  4519. */
  4520. usb_unlock_port(port_dev);
  4521. status = usb_remote_wakeup(udev);
  4522. usb_lock_port(port_dev);
  4523. #endif
  4524. } else {
  4525. /* Don't resuscitate */;
  4526. }
  4527. }
  4528. clear_bit(port1, hub->change_bits);
  4529. /* successfully revalidated the connection */
  4530. if (status == 0)
  4531. return;
  4532. usb_unlock_port(port_dev);
  4533. hub_port_connect(hub, port1, portstatus, portchange);
  4534. usb_lock_port(port_dev);
  4535. }
  4536. /* Handle notifying userspace about hub over-current events */
  4537. static void port_over_current_notify(struct usb_port *port_dev)
  4538. {
  4539. char *envp[3];
  4540. struct device *hub_dev;
  4541. char *port_dev_path;
  4542. sysfs_notify(&port_dev->dev.kobj, NULL, "over_current_count");
  4543. hub_dev = port_dev->dev.parent;
  4544. if (!hub_dev)
  4545. return;
  4546. port_dev_path = kobject_get_path(&port_dev->dev.kobj, GFP_KERNEL);
  4547. if (!port_dev_path)
  4548. return;
  4549. envp[0] = kasprintf(GFP_KERNEL, "OVER_CURRENT_PORT=%s", port_dev_path);
  4550. if (!envp[0])
  4551. goto exit_path;
  4552. envp[1] = kasprintf(GFP_KERNEL, "OVER_CURRENT_COUNT=%u",
  4553. port_dev->over_current_count);
  4554. if (!envp[1])
  4555. goto exit;
  4556. envp[2] = NULL;
  4557. kobject_uevent_env(&hub_dev->kobj, KOBJ_CHANGE, envp);
  4558. kfree(envp[1]);
  4559. exit:
  4560. kfree(envp[0]);
  4561. exit_path:
  4562. kfree(port_dev_path);
  4563. }
  4564. static void port_event(struct usb_hub *hub, int port1)
  4565. __must_hold(&port_dev->status_lock)
  4566. {
  4567. int connect_change;
  4568. struct usb_port *port_dev = hub->ports[port1 - 1];
  4569. struct usb_device *udev = port_dev->child;
  4570. struct usb_device *hdev = hub->hdev;
  4571. u16 portstatus, portchange;
  4572. connect_change = test_bit(port1, hub->change_bits);
  4573. clear_bit(port1, hub->event_bits);
  4574. clear_bit(port1, hub->wakeup_bits);
  4575. if (hub_port_status(hub, port1, &portstatus, &portchange) < 0)
  4576. return;
  4577. if (portchange & USB_PORT_STAT_C_CONNECTION) {
  4578. usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
  4579. connect_change = 1;
  4580. }
  4581. if (portchange & USB_PORT_STAT_C_ENABLE) {
  4582. if (!connect_change)
  4583. dev_dbg(&port_dev->dev, "enable change, status %08x\n",
  4584. portstatus);
  4585. usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
  4586. /*
  4587. * EM interference sometimes causes badly shielded USB devices
  4588. * to be shutdown by the hub, this hack enables them again.
  4589. * Works at least with mouse driver.
  4590. */
  4591. if (!(portstatus & USB_PORT_STAT_ENABLE)
  4592. && !connect_change && udev) {
  4593. dev_err(&port_dev->dev, "disabled by hub (EMI?), re-enabling...\n");
  4594. connect_change = 1;
  4595. }
  4596. }
  4597. if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
  4598. u16 status = 0, unused;
  4599. port_dev->over_current_count++;
  4600. port_over_current_notify(port_dev);
  4601. dev_dbg(&port_dev->dev, "over-current change #%u\n",
  4602. port_dev->over_current_count);
  4603. usb_clear_port_feature(hdev, port1,
  4604. USB_PORT_FEAT_C_OVER_CURRENT);
  4605. msleep(100); /* Cool down */
  4606. hub_power_on(hub, true);
  4607. hub_port_status(hub, port1, &status, &unused);
  4608. if (status & USB_PORT_STAT_OVERCURRENT)
  4609. dev_err(&port_dev->dev, "over-current condition\n");
  4610. }
  4611. if (portchange & USB_PORT_STAT_C_RESET) {
  4612. dev_dbg(&port_dev->dev, "reset change\n");
  4613. usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_RESET);
  4614. }
  4615. if ((portchange & USB_PORT_STAT_C_BH_RESET)
  4616. && hub_is_superspeed(hdev)) {
  4617. dev_dbg(&port_dev->dev, "warm reset change\n");
  4618. usb_clear_port_feature(hdev, port1,
  4619. USB_PORT_FEAT_C_BH_PORT_RESET);
  4620. }
  4621. if (portchange & USB_PORT_STAT_C_LINK_STATE) {
  4622. dev_dbg(&port_dev->dev, "link state change\n");
  4623. usb_clear_port_feature(hdev, port1,
  4624. USB_PORT_FEAT_C_PORT_LINK_STATE);
  4625. }
  4626. if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) {
  4627. dev_warn(&port_dev->dev, "config error\n");
  4628. usb_clear_port_feature(hdev, port1,
  4629. USB_PORT_FEAT_C_PORT_CONFIG_ERROR);
  4630. }
  4631. /* skip port actions that require the port to be powered on */
  4632. if (!pm_runtime_active(&port_dev->dev))
  4633. return;
  4634. if (hub_handle_remote_wakeup(hub, port1, portstatus, portchange))
  4635. connect_change = 1;
  4636. /*
  4637. * Warm reset a USB3 protocol port if it's in
  4638. * SS.Inactive state.
  4639. */
  4640. if (hub_port_warm_reset_required(hub, port1, portstatus)) {
  4641. dev_dbg(&port_dev->dev, "do warm reset\n");
  4642. if (!udev || !(portstatus & USB_PORT_STAT_CONNECTION)
  4643. || udev->state == USB_STATE_NOTATTACHED) {
  4644. if (hub_port_reset(hub, port1, NULL,
  4645. HUB_BH_RESET_TIME, true) < 0)
  4646. hub_port_disable(hub, port1, 1);
  4647. } else {
  4648. usb_unlock_port(port_dev);
  4649. usb_lock_device(udev);
  4650. usb_reset_device(udev);
  4651. usb_unlock_device(udev);
  4652. usb_lock_port(port_dev);
  4653. connect_change = 0;
  4654. }
  4655. }
  4656. if (connect_change)
  4657. hub_port_connect_change(hub, port1, portstatus, portchange);
  4658. }
  4659. static void hub_event(struct work_struct *work)
  4660. {
  4661. struct usb_device *hdev;
  4662. struct usb_interface *intf;
  4663. struct usb_hub *hub;
  4664. struct device *hub_dev;
  4665. u16 hubstatus;
  4666. u16 hubchange;
  4667. int i, ret;
  4668. hub = container_of(work, struct usb_hub, events);
  4669. hdev = hub->hdev;
  4670. hub_dev = hub->intfdev;
  4671. intf = to_usb_interface(hub_dev);
  4672. dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n",
  4673. hdev->state, hdev->maxchild,
  4674. /* NOTE: expects max 15 ports... */
  4675. (u16) hub->change_bits[0],
  4676. (u16) hub->event_bits[0]);
  4677. /* Lock the device, then check to see if we were
  4678. * disconnected while waiting for the lock to succeed. */
  4679. usb_lock_device(hdev);
  4680. if (unlikely(hub->disconnected))
  4681. goto out_hdev_lock;
  4682. /* If the hub has died, clean up after it */
  4683. if (hdev->state == USB_STATE_NOTATTACHED) {
  4684. hub->error = -ENODEV;
  4685. hub_quiesce(hub, HUB_DISCONNECT);
  4686. goto out_hdev_lock;
  4687. }
  4688. /* Autoresume */
  4689. ret = usb_autopm_get_interface(intf);
  4690. if (ret) {
  4691. dev_dbg(hub_dev, "Can't autoresume: %d\n", ret);
  4692. goto out_hdev_lock;
  4693. }
  4694. /* If this is an inactive hub, do nothing */
  4695. if (hub->quiescing)
  4696. goto out_autopm;
  4697. if (hub->error) {
  4698. dev_dbg(hub_dev, "resetting for error %d\n", hub->error);
  4699. ret = usb_reset_device(hdev);
  4700. if (ret) {
  4701. dev_dbg(hub_dev, "error resetting hub: %d\n", ret);
  4702. goto out_autopm;
  4703. }
  4704. hub->nerrors = 0;
  4705. hub->error = 0;
  4706. }
  4707. /* deal with port status changes */
  4708. for (i = 1; i <= hdev->maxchild; i++) {
  4709. struct usb_port *port_dev = hub->ports[i - 1];
  4710. if (test_bit(i, hub->event_bits)
  4711. || test_bit(i, hub->change_bits)
  4712. || test_bit(i, hub->wakeup_bits)) {
  4713. /*
  4714. * The get_noresume and barrier ensure that if
  4715. * the port was in the process of resuming, we
  4716. * flush that work and keep the port active for
  4717. * the duration of the port_event(). However,
  4718. * if the port is runtime pm suspended
  4719. * (powered-off), we leave it in that state, run
  4720. * an abbreviated port_event(), and move on.
  4721. */
  4722. pm_runtime_get_noresume(&port_dev->dev);
  4723. pm_runtime_barrier(&port_dev->dev);
  4724. usb_lock_port(port_dev);
  4725. port_event(hub, i);
  4726. usb_unlock_port(port_dev);
  4727. pm_runtime_put_sync(&port_dev->dev);
  4728. }
  4729. }
  4730. /* deal with hub status changes */
  4731. if (test_and_clear_bit(0, hub->event_bits) == 0)
  4732. ; /* do nothing */
  4733. else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0)
  4734. dev_err(hub_dev, "get_hub_status failed\n");
  4735. else {
  4736. if (hubchange & HUB_CHANGE_LOCAL_POWER) {
  4737. dev_dbg(hub_dev, "power change\n");
  4738. clear_hub_feature(hdev, C_HUB_LOCAL_POWER);
  4739. if (hubstatus & HUB_STATUS_LOCAL_POWER)
  4740. /* FIXME: Is this always true? */
  4741. hub->limited_power = 1;
  4742. else
  4743. hub->limited_power = 0;
  4744. }
  4745. if (hubchange & HUB_CHANGE_OVERCURRENT) {
  4746. u16 status = 0;
  4747. u16 unused;
  4748. dev_dbg(hub_dev, "over-current change\n");
  4749. clear_hub_feature(hdev, C_HUB_OVER_CURRENT);
  4750. msleep(500); /* Cool down */
  4751. hub_power_on(hub, true);
  4752. hub_hub_status(hub, &status, &unused);
  4753. if (status & HUB_STATUS_OVERCURRENT)
  4754. dev_err(hub_dev, "over-current condition\n");
  4755. }
  4756. }
  4757. out_autopm:
  4758. /* Balance the usb_autopm_get_interface() above */
  4759. usb_autopm_put_interface_no_suspend(intf);
  4760. out_hdev_lock:
  4761. usb_unlock_device(hdev);
  4762. /* Balance the stuff in kick_hub_wq() and allow autosuspend */
  4763. usb_autopm_put_interface(intf);
  4764. kref_put(&hub->kref, hub_release);
  4765. }
  4766. static const struct usb_device_id hub_id_table[] = {
  4767. { .match_flags = USB_DEVICE_ID_MATCH_VENDOR
  4768. | USB_DEVICE_ID_MATCH_INT_CLASS,
  4769. .idVendor = USB_VENDOR_GENESYS_LOGIC,
  4770. .bInterfaceClass = USB_CLASS_HUB,
  4771. .driver_info = HUB_QUIRK_CHECK_PORT_AUTOSUSPEND},
  4772. { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
  4773. .bDeviceClass = USB_CLASS_HUB},
  4774. { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
  4775. .bInterfaceClass = USB_CLASS_HUB},
  4776. { } /* Terminating entry */
  4777. };
  4778. MODULE_DEVICE_TABLE(usb, hub_id_table);
  4779. static struct usb_driver hub_driver = {
  4780. .name = "hub",
  4781. .probe = hub_probe,
  4782. .disconnect = hub_disconnect,
  4783. .suspend = hub_suspend,
  4784. .resume = hub_resume,
  4785. .reset_resume = hub_reset_resume,
  4786. .pre_reset = hub_pre_reset,
  4787. .post_reset = hub_post_reset,
  4788. .unlocked_ioctl = hub_ioctl,
  4789. .id_table = hub_id_table,
  4790. .supports_autosuspend = 1,
  4791. };
  4792. int usb_hub_init(void)
  4793. {
  4794. if (usb_register(&hub_driver) < 0) {
  4795. printk(KERN_ERR "%s: can't register hub driver\n",
  4796. usbcore_name);
  4797. return -1;
  4798. }
  4799. /*
  4800. * The workqueue needs to be freezable to avoid interfering with
  4801. * USB-PERSIST port handover. Otherwise it might see that a full-speed
  4802. * device was gone before the EHCI controller had handed its port
  4803. * over to the companion full-speed controller.
  4804. */
  4805. hub_wq = alloc_workqueue("usb_hub_wq", WQ_FREEZABLE, 0);
  4806. if (hub_wq)
  4807. return 0;
  4808. /* Fall through if kernel_thread failed */
  4809. usb_deregister(&hub_driver);
  4810. pr_err("%s: can't allocate workqueue for usb hub\n", usbcore_name);
  4811. return -1;
  4812. }
  4813. void usb_hub_cleanup(void)
  4814. {
  4815. destroy_workqueue(hub_wq);
  4816. /*
  4817. * Hub resources are freed for us by usb_deregister. It calls
  4818. * usb_driver_purge on every device which in turn calls that
  4819. * devices disconnect function if it is using this driver.
  4820. * The hub_disconnect function takes care of releasing the
  4821. * individual hub resources. -greg
  4822. */
  4823. usb_deregister(&hub_driver);
  4824. } /* usb_hub_cleanup() */
  4825. static int descriptors_changed(struct usb_device *udev,
  4826. struct usb_device_descriptor *old_device_descriptor,
  4827. struct usb_host_bos *old_bos)
  4828. {
  4829. int changed = 0;
  4830. unsigned index;
  4831. unsigned serial_len = 0;
  4832. unsigned len;
  4833. unsigned old_length;
  4834. int length;
  4835. char *buf;
  4836. if (memcmp(&udev->descriptor, old_device_descriptor,
  4837. sizeof(*old_device_descriptor)) != 0)
  4838. return 1;
  4839. if ((old_bos && !udev->bos) || (!old_bos && udev->bos))
  4840. return 1;
  4841. if (udev->bos) {
  4842. len = le16_to_cpu(udev->bos->desc->wTotalLength);
  4843. if (len != le16_to_cpu(old_bos->desc->wTotalLength))
  4844. return 1;
  4845. if (memcmp(udev->bos->desc, old_bos->desc, len))
  4846. return 1;
  4847. }
  4848. /* Since the idVendor, idProduct, and bcdDevice values in the
  4849. * device descriptor haven't changed, we will assume the
  4850. * Manufacturer and Product strings haven't changed either.
  4851. * But the SerialNumber string could be different (e.g., a
  4852. * different flash card of the same brand).
  4853. */
  4854. if (udev->serial)
  4855. serial_len = strlen(udev->serial) + 1;
  4856. len = serial_len;
  4857. for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
  4858. old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
  4859. len = max(len, old_length);
  4860. }
  4861. buf = kmalloc(len, GFP_NOIO);
  4862. if (!buf)
  4863. /* assume the worst */
  4864. return 1;
  4865. for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
  4866. old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
  4867. length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf,
  4868. old_length);
  4869. if (length != old_length) {
  4870. dev_dbg(&udev->dev, "config index %d, error %d\n",
  4871. index, length);
  4872. changed = 1;
  4873. break;
  4874. }
  4875. if (memcmp(buf, udev->rawdescriptors[index], old_length)
  4876. != 0) {
  4877. dev_dbg(&udev->dev, "config index %d changed (#%d)\n",
  4878. index,
  4879. ((struct usb_config_descriptor *) buf)->
  4880. bConfigurationValue);
  4881. changed = 1;
  4882. break;
  4883. }
  4884. }
  4885. if (!changed && serial_len) {
  4886. length = usb_string(udev, udev->descriptor.iSerialNumber,
  4887. buf, serial_len);
  4888. if (length + 1 != serial_len) {
  4889. dev_dbg(&udev->dev, "serial string error %d\n",
  4890. length);
  4891. changed = 1;
  4892. } else if (memcmp(buf, udev->serial, length) != 0) {
  4893. dev_dbg(&udev->dev, "serial string changed\n");
  4894. changed = 1;
  4895. }
  4896. }
  4897. kfree(buf);
  4898. return changed;
  4899. }
  4900. /**
  4901. * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device
  4902. * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
  4903. *
  4904. * WARNING - don't use this routine to reset a composite device
  4905. * (one with multiple interfaces owned by separate drivers)!
  4906. * Use usb_reset_device() instead.
  4907. *
  4908. * Do a port reset, reassign the device's address, and establish its
  4909. * former operating configuration. If the reset fails, or the device's
  4910. * descriptors change from their values before the reset, or the original
  4911. * configuration and altsettings cannot be restored, a flag will be set
  4912. * telling hub_wq to pretend the device has been disconnected and then
  4913. * re-connected. All drivers will be unbound, and the device will be
  4914. * re-enumerated and probed all over again.
  4915. *
  4916. * Return: 0 if the reset succeeded, -ENODEV if the device has been
  4917. * flagged for logical disconnection, or some other negative error code
  4918. * if the reset wasn't even attempted.
  4919. *
  4920. * Note:
  4921. * The caller must own the device lock and the port lock, the latter is
  4922. * taken by usb_reset_device(). For example, it's safe to use
  4923. * usb_reset_device() from a driver probe() routine after downloading
  4924. * new firmware. For calls that might not occur during probe(), drivers
  4925. * should lock the device using usb_lock_device_for_reset().
  4926. *
  4927. * Locking exception: This routine may also be called from within an
  4928. * autoresume handler. Such usage won't conflict with other tasks
  4929. * holding the device lock because these tasks should always call
  4930. * usb_autopm_resume_device(), thereby preventing any unwanted
  4931. * autoresume. The autoresume handler is expected to have already
  4932. * acquired the port lock before calling this routine.
  4933. */
  4934. static int usb_reset_and_verify_device(struct usb_device *udev)
  4935. {
  4936. struct usb_device *parent_hdev = udev->parent;
  4937. struct usb_hub *parent_hub;
  4938. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  4939. struct usb_device_descriptor descriptor = udev->descriptor;
  4940. struct usb_host_bos *bos;
  4941. int i, j, ret = 0;
  4942. int port1 = udev->portnum;
  4943. if (udev->state == USB_STATE_NOTATTACHED ||
  4944. udev->state == USB_STATE_SUSPENDED) {
  4945. dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
  4946. udev->state);
  4947. return -EINVAL;
  4948. }
  4949. if (!parent_hdev)
  4950. return -EISDIR;
  4951. parent_hub = usb_hub_to_struct_hub(parent_hdev);
  4952. /* Disable USB2 hardware LPM.
  4953. * It will be re-enabled by the enumeration process.
  4954. */
  4955. if (udev->usb2_hw_lpm_enabled == 1)
  4956. usb_set_usb2_hardware_lpm(udev, 0);
  4957. /* Disable LPM while we reset the device and reinstall the alt settings.
  4958. * Device-initiated LPM, and system exit latency settings are cleared
  4959. * when the device is reset, so we have to set them up again.
  4960. */
  4961. ret = usb_unlocked_disable_lpm(udev);
  4962. if (ret) {
  4963. dev_err(&udev->dev, "%s Failed to disable LPM\n", __func__);
  4964. goto re_enumerate_no_bos;
  4965. }
  4966. bos = udev->bos;
  4967. udev->bos = NULL;
  4968. for (i = 0; i < SET_CONFIG_TRIES; ++i) {
  4969. /* ep0 maxpacket size may change; let the HCD know about it.
  4970. * Other endpoints will be handled by re-enumeration. */
  4971. usb_ep0_reinit(udev);
  4972. ret = hub_port_init(parent_hub, udev, port1, i);
  4973. if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV)
  4974. break;
  4975. }
  4976. if (ret < 0)
  4977. goto re_enumerate;
  4978. /* Device might have changed firmware (DFU or similar) */
  4979. if (descriptors_changed(udev, &descriptor, bos)) {
  4980. dev_info(&udev->dev, "device firmware changed\n");
  4981. udev->descriptor = descriptor; /* for disconnect() calls */
  4982. goto re_enumerate;
  4983. }
  4984. /* Restore the device's previous configuration */
  4985. if (!udev->actconfig)
  4986. goto done;
  4987. mutex_lock(hcd->bandwidth_mutex);
  4988. ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL);
  4989. if (ret < 0) {
  4990. dev_warn(&udev->dev,
  4991. "Busted HC? Not enough HCD resources for "
  4992. "old configuration.\n");
  4993. mutex_unlock(hcd->bandwidth_mutex);
  4994. goto re_enumerate;
  4995. }
  4996. ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  4997. USB_REQ_SET_CONFIGURATION, 0,
  4998. udev->actconfig->desc.bConfigurationValue, 0,
  4999. NULL, 0, USB_CTRL_SET_TIMEOUT);
  5000. if (ret < 0) {
  5001. dev_err(&udev->dev,
  5002. "can't restore configuration #%d (error=%d)\n",
  5003. udev->actconfig->desc.bConfigurationValue, ret);
  5004. mutex_unlock(hcd->bandwidth_mutex);
  5005. goto re_enumerate;
  5006. }
  5007. mutex_unlock(hcd->bandwidth_mutex);
  5008. usb_set_device_state(udev, USB_STATE_CONFIGURED);
  5009. /* Put interfaces back into the same altsettings as before.
  5010. * Don't bother to send the Set-Interface request for interfaces
  5011. * that were already in altsetting 0; besides being unnecessary,
  5012. * many devices can't handle it. Instead just reset the host-side
  5013. * endpoint state.
  5014. */
  5015. for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
  5016. struct usb_host_config *config = udev->actconfig;
  5017. struct usb_interface *intf = config->interface[i];
  5018. struct usb_interface_descriptor *desc;
  5019. desc = &intf->cur_altsetting->desc;
  5020. if (desc->bAlternateSetting == 0) {
  5021. usb_disable_interface(udev, intf, true);
  5022. usb_enable_interface(udev, intf, true);
  5023. ret = 0;
  5024. } else {
  5025. /* Let the bandwidth allocation function know that this
  5026. * device has been reset, and it will have to use
  5027. * alternate setting 0 as the current alternate setting.
  5028. */
  5029. intf->resetting_device = 1;
  5030. ret = usb_set_interface(udev, desc->bInterfaceNumber,
  5031. desc->bAlternateSetting);
  5032. intf->resetting_device = 0;
  5033. }
  5034. if (ret < 0) {
  5035. dev_err(&udev->dev, "failed to restore interface %d "
  5036. "altsetting %d (error=%d)\n",
  5037. desc->bInterfaceNumber,
  5038. desc->bAlternateSetting,
  5039. ret);
  5040. goto re_enumerate;
  5041. }
  5042. /* Resetting also frees any allocated streams */
  5043. for (j = 0; j < intf->cur_altsetting->desc.bNumEndpoints; j++)
  5044. intf->cur_altsetting->endpoint[j].streams = 0;
  5045. }
  5046. done:
  5047. /* Now that the alt settings are re-installed, enable LTM and LPM. */
  5048. usb_set_usb2_hardware_lpm(udev, 1);
  5049. usb_unlocked_enable_lpm(udev);
  5050. usb_enable_ltm(udev);
  5051. usb_release_bos_descriptor(udev);
  5052. udev->bos = bos;
  5053. return 0;
  5054. re_enumerate:
  5055. usb_release_bos_descriptor(udev);
  5056. udev->bos = bos;
  5057. re_enumerate_no_bos:
  5058. /* LPM state doesn't matter when we're about to destroy the device. */
  5059. hub_port_logical_disconnect(parent_hub, port1);
  5060. return -ENODEV;
  5061. }
  5062. /**
  5063. * usb_reset_device - warn interface drivers and perform a USB port reset
  5064. * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
  5065. *
  5066. * Warns all drivers bound to registered interfaces (using their pre_reset
  5067. * method), performs the port reset, and then lets the drivers know that
  5068. * the reset is over (using their post_reset method).
  5069. *
  5070. * Return: The same as for usb_reset_and_verify_device().
  5071. *
  5072. * Note:
  5073. * The caller must own the device lock. For example, it's safe to use
  5074. * this from a driver probe() routine after downloading new firmware.
  5075. * For calls that might not occur during probe(), drivers should lock
  5076. * the device using usb_lock_device_for_reset().
  5077. *
  5078. * If an interface is currently being probed or disconnected, we assume
  5079. * its driver knows how to handle resets. For all other interfaces,
  5080. * if the driver doesn't have pre_reset and post_reset methods then
  5081. * we attempt to unbind it and rebind afterward.
  5082. */
  5083. int usb_reset_device(struct usb_device *udev)
  5084. {
  5085. int ret;
  5086. int i;
  5087. unsigned int noio_flag;
  5088. struct usb_port *port_dev;
  5089. struct usb_host_config *config = udev->actconfig;
  5090. struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
  5091. if (udev->state == USB_STATE_NOTATTACHED ||
  5092. udev->state == USB_STATE_SUSPENDED) {
  5093. dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
  5094. udev->state);
  5095. return -EINVAL;
  5096. }
  5097. if (!udev->parent) {
  5098. /* this requires hcd-specific logic; see ohci_restart() */
  5099. dev_dbg(&udev->dev, "%s for root hub!\n", __func__);
  5100. return -EISDIR;
  5101. }
  5102. port_dev = hub->ports[udev->portnum - 1];
  5103. /*
  5104. * Don't allocate memory with GFP_KERNEL in current
  5105. * context to avoid possible deadlock if usb mass
  5106. * storage interface or usbnet interface(iSCSI case)
  5107. * is included in current configuration. The easist
  5108. * approach is to do it for every device reset,
  5109. * because the device 'memalloc_noio' flag may have
  5110. * not been set before reseting the usb device.
  5111. */
  5112. noio_flag = memalloc_noio_save();
  5113. /* Prevent autosuspend during the reset */
  5114. usb_autoresume_device(udev);
  5115. if (config) {
  5116. for (i = 0; i < config->desc.bNumInterfaces; ++i) {
  5117. struct usb_interface *cintf = config->interface[i];
  5118. struct usb_driver *drv;
  5119. int unbind = 0;
  5120. if (cintf->dev.driver) {
  5121. drv = to_usb_driver(cintf->dev.driver);
  5122. if (drv->pre_reset && drv->post_reset)
  5123. unbind = (drv->pre_reset)(cintf);
  5124. else if (cintf->condition ==
  5125. USB_INTERFACE_BOUND)
  5126. unbind = 1;
  5127. if (unbind)
  5128. usb_forced_unbind_intf(cintf);
  5129. }
  5130. }
  5131. }
  5132. usb_lock_port(port_dev);
  5133. ret = usb_reset_and_verify_device(udev);
  5134. usb_unlock_port(port_dev);
  5135. if (config) {
  5136. for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) {
  5137. struct usb_interface *cintf = config->interface[i];
  5138. struct usb_driver *drv;
  5139. int rebind = cintf->needs_binding;
  5140. if (!rebind && cintf->dev.driver) {
  5141. drv = to_usb_driver(cintf->dev.driver);
  5142. if (drv->post_reset)
  5143. rebind = (drv->post_reset)(cintf);
  5144. else if (cintf->condition ==
  5145. USB_INTERFACE_BOUND)
  5146. rebind = 1;
  5147. if (rebind)
  5148. cintf->needs_binding = 1;
  5149. }
  5150. }
  5151. usb_unbind_and_rebind_marked_interfaces(udev);
  5152. }
  5153. usb_autosuspend_device(udev);
  5154. memalloc_noio_restore(noio_flag);
  5155. return ret;
  5156. }
  5157. EXPORT_SYMBOL_GPL(usb_reset_device);
  5158. /**
  5159. * usb_queue_reset_device - Reset a USB device from an atomic context
  5160. * @iface: USB interface belonging to the device to reset
  5161. *
  5162. * This function can be used to reset a USB device from an atomic
  5163. * context, where usb_reset_device() won't work (as it blocks).
  5164. *
  5165. * Doing a reset via this method is functionally equivalent to calling
  5166. * usb_reset_device(), except for the fact that it is delayed to a
  5167. * workqueue. This means that any drivers bound to other interfaces
  5168. * might be unbound, as well as users from usbfs in user space.
  5169. *
  5170. * Corner cases:
  5171. *
  5172. * - Scheduling two resets at the same time from two different drivers
  5173. * attached to two different interfaces of the same device is
  5174. * possible; depending on how the driver attached to each interface
  5175. * handles ->pre_reset(), the second reset might happen or not.
  5176. *
  5177. * - If the reset is delayed so long that the interface is unbound from
  5178. * its driver, the reset will be skipped.
  5179. *
  5180. * - This function can be called during .probe(). It can also be called
  5181. * during .disconnect(), but doing so is pointless because the reset
  5182. * will not occur. If you really want to reset the device during
  5183. * .disconnect(), call usb_reset_device() directly -- but watch out
  5184. * for nested unbinding issues!
  5185. */
  5186. void usb_queue_reset_device(struct usb_interface *iface)
  5187. {
  5188. if (schedule_work(&iface->reset_ws))
  5189. usb_get_intf(iface);
  5190. }
  5191. EXPORT_SYMBOL_GPL(usb_queue_reset_device);
  5192. /**
  5193. * usb_hub_find_child - Get the pointer of child device
  5194. * attached to the port which is specified by @port1.
  5195. * @hdev: USB device belonging to the usb hub
  5196. * @port1: port num to indicate which port the child device
  5197. * is attached to.
  5198. *
  5199. * USB drivers call this function to get hub's child device
  5200. * pointer.
  5201. *
  5202. * Return: %NULL if input param is invalid and
  5203. * child's usb_device pointer if non-NULL.
  5204. */
  5205. struct usb_device *usb_hub_find_child(struct usb_device *hdev,
  5206. int port1)
  5207. {
  5208. struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
  5209. if (port1 < 1 || port1 > hdev->maxchild)
  5210. return NULL;
  5211. return hub->ports[port1 - 1]->child;
  5212. }
  5213. EXPORT_SYMBOL_GPL(usb_hub_find_child);
  5214. void usb_hub_adjust_deviceremovable(struct usb_device *hdev,
  5215. struct usb_hub_descriptor *desc)
  5216. {
  5217. struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
  5218. enum usb_port_connect_type connect_type;
  5219. int i;
  5220. if (!hub)
  5221. return;
  5222. if (!hub_is_superspeed(hdev)) {
  5223. for (i = 1; i <= hdev->maxchild; i++) {
  5224. struct usb_port *port_dev = hub->ports[i - 1];
  5225. connect_type = port_dev->connect_type;
  5226. if (connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) {
  5227. u8 mask = 1 << (i%8);
  5228. if (!(desc->u.hs.DeviceRemovable[i/8] & mask)) {
  5229. dev_dbg(&port_dev->dev, "DeviceRemovable is changed to 1 according to platform information.\n");
  5230. desc->u.hs.DeviceRemovable[i/8] |= mask;
  5231. }
  5232. }
  5233. }
  5234. } else {
  5235. u16 port_removable = le16_to_cpu(desc->u.ss.DeviceRemovable);
  5236. for (i = 1; i <= hdev->maxchild; i++) {
  5237. struct usb_port *port_dev = hub->ports[i - 1];
  5238. connect_type = port_dev->connect_type;
  5239. if (connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) {
  5240. u16 mask = 1 << i;
  5241. if (!(port_removable & mask)) {
  5242. dev_dbg(&port_dev->dev, "DeviceRemovable is changed to 1 according to platform information.\n");
  5243. port_removable |= mask;
  5244. }
  5245. }
  5246. }
  5247. desc->u.ss.DeviceRemovable = cpu_to_le16(port_removable);
  5248. }
  5249. }
  5250. #ifdef CONFIG_ACPI
  5251. /**
  5252. * usb_get_hub_port_acpi_handle - Get the usb port's acpi handle
  5253. * @hdev: USB device belonging to the usb hub
  5254. * @port1: port num of the port
  5255. *
  5256. * Return: Port's acpi handle if successful, %NULL if params are
  5257. * invalid.
  5258. */
  5259. acpi_handle usb_get_hub_port_acpi_handle(struct usb_device *hdev,
  5260. int port1)
  5261. {
  5262. struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
  5263. if (!hub)
  5264. return NULL;
  5265. return ACPI_HANDLE(&hub->ports[port1 - 1]->dev);
  5266. }
  5267. #endif