hub.c 187 KB

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