hub.c 173 KB

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