hub.c 173 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051
  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. hub_port_error_notify(udev);
  2559. dev_err(&port_dev->dev, "Cannot enable. Maybe the USB cable is bad?\n");
  2560. done:
  2561. if (status == 0) {
  2562. if (port_dev->quirks & USB_PORT_QUIRK_FAST_ENUM)
  2563. usleep_range(10000, 12000);
  2564. else {
  2565. /* TRSTRCY = 10 ms; plus some extra */
  2566. reset_recovery_time = 10 + 40;
  2567. /* Hub needs extra delay after resetting its port. */
  2568. if (hub->hdev->quirks & USB_QUIRK_HUB_SLOW_RESET)
  2569. reset_recovery_time += 100;
  2570. msleep(reset_recovery_time);
  2571. }
  2572. if (udev) {
  2573. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  2574. update_devnum(udev, 0);
  2575. /* The xHC may think the device is already reset,
  2576. * so ignore the status.
  2577. */
  2578. if (hcd->driver->reset_device)
  2579. hcd->driver->reset_device(hcd, udev);
  2580. usb_set_device_state(udev, USB_STATE_DEFAULT);
  2581. }
  2582. } else {
  2583. if (udev)
  2584. usb_set_device_state(udev, USB_STATE_NOTATTACHED);
  2585. }
  2586. if (!hub_is_superspeed(hub->hdev))
  2587. up_read(&ehci_cf_port_reset_rwsem);
  2588. return status;
  2589. }
  2590. /* Check if a port is power on */
  2591. static int port_is_power_on(struct usb_hub *hub, unsigned portstatus)
  2592. {
  2593. int ret = 0;
  2594. if (hub_is_superspeed(hub->hdev)) {
  2595. if (portstatus & USB_SS_PORT_STAT_POWER)
  2596. ret = 1;
  2597. } else {
  2598. if (portstatus & USB_PORT_STAT_POWER)
  2599. ret = 1;
  2600. }
  2601. return ret;
  2602. }
  2603. static void usb_lock_port(struct usb_port *port_dev)
  2604. __acquires(&port_dev->status_lock)
  2605. {
  2606. mutex_lock(&port_dev->status_lock);
  2607. __acquire(&port_dev->status_lock);
  2608. }
  2609. static void usb_unlock_port(struct usb_port *port_dev)
  2610. __releases(&port_dev->status_lock)
  2611. {
  2612. mutex_unlock(&port_dev->status_lock);
  2613. __release(&port_dev->status_lock);
  2614. }
  2615. #ifdef CONFIG_PM
  2616. /* Check if a port is suspended(USB2.0 port) or in U3 state(USB3.0 port) */
  2617. static int port_is_suspended(struct usb_hub *hub, unsigned portstatus)
  2618. {
  2619. int ret = 0;
  2620. if (hub_is_superspeed(hub->hdev)) {
  2621. if ((portstatus & USB_PORT_STAT_LINK_STATE)
  2622. == USB_SS_PORT_LS_U3)
  2623. ret = 1;
  2624. } else {
  2625. if (portstatus & USB_PORT_STAT_SUSPEND)
  2626. ret = 1;
  2627. }
  2628. return ret;
  2629. }
  2630. /* Determine whether the device on a port is ready for a normal resume,
  2631. * is ready for a reset-resume, or should be disconnected.
  2632. */
  2633. static int check_port_resume_type(struct usb_device *udev,
  2634. struct usb_hub *hub, int port1,
  2635. int status, u16 portchange, u16 portstatus)
  2636. {
  2637. struct usb_port *port_dev = hub->ports[port1 - 1];
  2638. int retries = 3;
  2639. retry:
  2640. /* Is a warm reset needed to recover the connection? */
  2641. if (status == 0 && udev->reset_resume
  2642. && hub_port_warm_reset_required(hub, port1, portstatus)) {
  2643. /* pass */;
  2644. }
  2645. /* Is the device still present? */
  2646. else if (status || port_is_suspended(hub, portstatus) ||
  2647. !port_is_power_on(hub, portstatus)) {
  2648. if (status >= 0)
  2649. status = -ENODEV;
  2650. } else if (!(portstatus & USB_PORT_STAT_CONNECTION)) {
  2651. if (retries--) {
  2652. usleep_range(200, 300);
  2653. status = hub_port_status(hub, port1, &portstatus,
  2654. &portchange);
  2655. goto retry;
  2656. }
  2657. status = -ENODEV;
  2658. }
  2659. /* Can't do a normal resume if the port isn't enabled,
  2660. * so try a reset-resume instead.
  2661. */
  2662. else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) {
  2663. if (udev->persist_enabled)
  2664. udev->reset_resume = 1;
  2665. else
  2666. status = -ENODEV;
  2667. }
  2668. if (status) {
  2669. dev_dbg(&port_dev->dev, "status %04x.%04x after resume, %d\n",
  2670. portchange, portstatus, status);
  2671. } else if (udev->reset_resume) {
  2672. /* Late port handoff can set status-change bits */
  2673. if (portchange & USB_PORT_STAT_C_CONNECTION)
  2674. usb_clear_port_feature(hub->hdev, port1,
  2675. USB_PORT_FEAT_C_CONNECTION);
  2676. if (portchange & USB_PORT_STAT_C_ENABLE)
  2677. usb_clear_port_feature(hub->hdev, port1,
  2678. USB_PORT_FEAT_C_ENABLE);
  2679. /*
  2680. * Whatever made this reset-resume necessary may have
  2681. * turned on the port1 bit in hub->change_bits. But after
  2682. * a successful reset-resume we want the bit to be clear;
  2683. * if it was on it would indicate that something happened
  2684. * following the reset-resume.
  2685. */
  2686. clear_bit(port1, hub->change_bits);
  2687. }
  2688. return status;
  2689. }
  2690. int usb_disable_ltm(struct usb_device *udev)
  2691. {
  2692. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  2693. /* Check if the roothub and device supports LTM. */
  2694. if (!usb_device_supports_ltm(hcd->self.root_hub) ||
  2695. !usb_device_supports_ltm(udev))
  2696. return 0;
  2697. /* Clear Feature LTM Enable can only be sent if the device is
  2698. * configured.
  2699. */
  2700. if (!udev->actconfig)
  2701. return 0;
  2702. return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  2703. USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
  2704. USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
  2705. USB_CTRL_SET_TIMEOUT);
  2706. }
  2707. EXPORT_SYMBOL_GPL(usb_disable_ltm);
  2708. void usb_enable_ltm(struct usb_device *udev)
  2709. {
  2710. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  2711. /* Check if the roothub and device supports LTM. */
  2712. if (!usb_device_supports_ltm(hcd->self.root_hub) ||
  2713. !usb_device_supports_ltm(udev))
  2714. return;
  2715. /* Set Feature LTM Enable can only be sent if the device is
  2716. * configured.
  2717. */
  2718. if (!udev->actconfig)
  2719. return;
  2720. usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  2721. USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
  2722. USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
  2723. USB_CTRL_SET_TIMEOUT);
  2724. }
  2725. EXPORT_SYMBOL_GPL(usb_enable_ltm);
  2726. /*
  2727. * usb_enable_remote_wakeup - enable remote wakeup for a device
  2728. * @udev: target device
  2729. *
  2730. * For USB-2 devices: Set the device's remote wakeup feature.
  2731. *
  2732. * For USB-3 devices: Assume there's only one function on the device and
  2733. * enable remote wake for the first interface. FIXME if the interface
  2734. * association descriptor shows there's more than one function.
  2735. */
  2736. static int usb_enable_remote_wakeup(struct usb_device *udev)
  2737. {
  2738. if (udev->speed < USB_SPEED_SUPER)
  2739. return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  2740. USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
  2741. USB_DEVICE_REMOTE_WAKEUP, 0, NULL, 0,
  2742. USB_CTRL_SET_TIMEOUT);
  2743. else
  2744. return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  2745. USB_REQ_SET_FEATURE, USB_RECIP_INTERFACE,
  2746. USB_INTRF_FUNC_SUSPEND,
  2747. USB_INTRF_FUNC_SUSPEND_RW |
  2748. USB_INTRF_FUNC_SUSPEND_LP,
  2749. NULL, 0, USB_CTRL_SET_TIMEOUT);
  2750. }
  2751. /*
  2752. * usb_disable_remote_wakeup - disable remote wakeup for a device
  2753. * @udev: target device
  2754. *
  2755. * For USB-2 devices: Clear the device's remote wakeup feature.
  2756. *
  2757. * For USB-3 devices: Assume there's only one function on the device and
  2758. * disable remote wake for the first interface. FIXME if the interface
  2759. * association descriptor shows there's more than one function.
  2760. */
  2761. static int usb_disable_remote_wakeup(struct usb_device *udev)
  2762. {
  2763. if (udev->speed < USB_SPEED_SUPER)
  2764. return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  2765. USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
  2766. USB_DEVICE_REMOTE_WAKEUP, 0, NULL, 0,
  2767. USB_CTRL_SET_TIMEOUT);
  2768. else
  2769. return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  2770. USB_REQ_SET_FEATURE, USB_RECIP_INTERFACE,
  2771. USB_INTRF_FUNC_SUSPEND, 0, NULL, 0,
  2772. USB_CTRL_SET_TIMEOUT);
  2773. }
  2774. /* Count of wakeup-enabled devices at or below udev */
  2775. static unsigned wakeup_enabled_descendants(struct usb_device *udev)
  2776. {
  2777. struct usb_hub *hub = usb_hub_to_struct_hub(udev);
  2778. return udev->do_remote_wakeup +
  2779. (hub ? hub->wakeup_enabled_descendants : 0);
  2780. }
  2781. /*
  2782. * usb_port_suspend - suspend a usb device's upstream port
  2783. * @udev: device that's no longer in active use, not a root hub
  2784. * Context: must be able to sleep; device not locked; pm locks held
  2785. *
  2786. * Suspends a USB device that isn't in active use, conserving power.
  2787. * Devices may wake out of a suspend, if anything important happens,
  2788. * using the remote wakeup mechanism. They may also be taken out of
  2789. * suspend by the host, using usb_port_resume(). It's also routine
  2790. * to disconnect devices while they are suspended.
  2791. *
  2792. * This only affects the USB hardware for a device; its interfaces
  2793. * (and, for hubs, child devices) must already have been suspended.
  2794. *
  2795. * Selective port suspend reduces power; most suspended devices draw
  2796. * less than 500 uA. It's also used in OTG, along with remote wakeup.
  2797. * All devices below the suspended port are also suspended.
  2798. *
  2799. * Devices leave suspend state when the host wakes them up. Some devices
  2800. * also support "remote wakeup", where the device can activate the USB
  2801. * tree above them to deliver data, such as a keypress or packet. In
  2802. * some cases, this wakes the USB host.
  2803. *
  2804. * Suspending OTG devices may trigger HNP, if that's been enabled
  2805. * between a pair of dual-role devices. That will change roles, such
  2806. * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral.
  2807. *
  2808. * Devices on USB hub ports have only one "suspend" state, corresponding
  2809. * to ACPI D2, "may cause the device to lose some context".
  2810. * State transitions include:
  2811. *
  2812. * - suspend, resume ... when the VBUS power link stays live
  2813. * - suspend, disconnect ... VBUS lost
  2814. *
  2815. * Once VBUS drop breaks the circuit, the port it's using has to go through
  2816. * normal re-enumeration procedures, starting with enabling VBUS power.
  2817. * Other than re-initializing the hub (plug/unplug, except for root hubs),
  2818. * Linux (2.6) currently has NO mechanisms to initiate that: no hub_wq
  2819. * timer, no SRP, no requests through sysfs.
  2820. *
  2821. * If Runtime PM isn't enabled or used, non-SuperSpeed devices may not get
  2822. * suspended until their bus goes into global suspend (i.e., the root
  2823. * hub is suspended). Nevertheless, we change @udev->state to
  2824. * USB_STATE_SUSPENDED as this is the device's "logical" state. The actual
  2825. * upstream port setting is stored in @udev->port_is_suspended.
  2826. *
  2827. * Returns 0 on success, else negative errno.
  2828. */
  2829. int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
  2830. {
  2831. struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
  2832. struct usb_port *port_dev = hub->ports[udev->portnum - 1];
  2833. int port1 = udev->portnum;
  2834. int status;
  2835. bool really_suspend = true;
  2836. usb_lock_port(port_dev);
  2837. /* enable remote wakeup when appropriate; this lets the device
  2838. * wake up the upstream hub (including maybe the root hub).
  2839. *
  2840. * NOTE: OTG devices may issue remote wakeup (or SRP) even when
  2841. * we don't explicitly enable it here.
  2842. */
  2843. if (udev->do_remote_wakeup) {
  2844. status = usb_enable_remote_wakeup(udev);
  2845. if (status) {
  2846. dev_dbg(&udev->dev, "won't remote wakeup, status %d\n",
  2847. status);
  2848. /* bail if autosuspend is requested */
  2849. if (PMSG_IS_AUTO(msg))
  2850. goto err_wakeup;
  2851. }
  2852. }
  2853. /* disable USB2 hardware LPM */
  2854. usb_disable_usb2_hardware_lpm(udev);
  2855. if (usb_disable_ltm(udev)) {
  2856. dev_err(&udev->dev, "Failed to disable LTM before suspend\n");
  2857. status = -ENOMEM;
  2858. if (PMSG_IS_AUTO(msg))
  2859. goto err_ltm;
  2860. }
  2861. /* see 7.1.7.6 */
  2862. if (hub_is_superspeed(hub->hdev))
  2863. status = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_U3);
  2864. /*
  2865. * For system suspend, we do not need to enable the suspend feature
  2866. * on individual USB-2 ports. The devices will automatically go
  2867. * into suspend a few ms after the root hub stops sending packets.
  2868. * The USB 2.0 spec calls this "global suspend".
  2869. *
  2870. * However, many USB hubs have a bug: They don't relay wakeup requests
  2871. * from a downstream port if the port's suspend feature isn't on.
  2872. * Therefore we will turn on the suspend feature if udev or any of its
  2873. * descendants is enabled for remote wakeup.
  2874. */
  2875. else if (PMSG_IS_AUTO(msg) || wakeup_enabled_descendants(udev) > 0)
  2876. status = set_port_feature(hub->hdev, port1,
  2877. USB_PORT_FEAT_SUSPEND);
  2878. else {
  2879. really_suspend = false;
  2880. status = 0;
  2881. }
  2882. if (status) {
  2883. dev_dbg(&port_dev->dev, "can't suspend, status %d\n", status);
  2884. /* Try to enable USB3 LTM again */
  2885. usb_enable_ltm(udev);
  2886. err_ltm:
  2887. /* Try to enable USB2 hardware LPM again */
  2888. usb_enable_usb2_hardware_lpm(udev);
  2889. if (udev->do_remote_wakeup)
  2890. (void) usb_disable_remote_wakeup(udev);
  2891. err_wakeup:
  2892. /* System sleep transitions should never fail */
  2893. if (!PMSG_IS_AUTO(msg))
  2894. status = 0;
  2895. } else {
  2896. dev_dbg(&udev->dev, "usb %ssuspend, wakeup %d\n",
  2897. (PMSG_IS_AUTO(msg) ? "auto-" : ""),
  2898. udev->do_remote_wakeup);
  2899. if (really_suspend) {
  2900. udev->port_is_suspended = 1;
  2901. /* device has up to 10 msec to fully suspend */
  2902. msleep(10);
  2903. }
  2904. usb_set_device_state(udev, USB_STATE_SUSPENDED);
  2905. }
  2906. if (status == 0 && !udev->do_remote_wakeup && udev->persist_enabled
  2907. && test_and_clear_bit(port1, hub->child_usage_bits))
  2908. pm_runtime_put_sync(&port_dev->dev);
  2909. usb_mark_last_busy(hub->hdev);
  2910. usb_unlock_port(port_dev);
  2911. return status;
  2912. }
  2913. /*
  2914. * If the USB "suspend" state is in use (rather than "global suspend"),
  2915. * many devices will be individually taken out of suspend state using
  2916. * special "resume" signaling. This routine kicks in shortly after
  2917. * hardware resume signaling is finished, either because of selective
  2918. * resume (by host) or remote wakeup (by device) ... now see what changed
  2919. * in the tree that's rooted at this device.
  2920. *
  2921. * If @udev->reset_resume is set then the device is reset before the
  2922. * status check is done.
  2923. */
  2924. static int finish_port_resume(struct usb_device *udev)
  2925. {
  2926. int status = 0;
  2927. u16 devstatus = 0;
  2928. /* caller owns the udev device lock */
  2929. dev_dbg(&udev->dev, "%s\n",
  2930. udev->reset_resume ? "finish reset-resume" : "finish resume");
  2931. /* usb ch9 identifies four variants of SUSPENDED, based on what
  2932. * state the device resumes to. Linux currently won't see the
  2933. * first two on the host side; they'd be inside hub_port_init()
  2934. * during many timeouts, but hub_wq can't suspend until later.
  2935. */
  2936. usb_set_device_state(udev, udev->actconfig
  2937. ? USB_STATE_CONFIGURED
  2938. : USB_STATE_ADDRESS);
  2939. /* 10.5.4.5 says not to reset a suspended port if the attached
  2940. * device is enabled for remote wakeup. Hence the reset
  2941. * operation is carried out here, after the port has been
  2942. * resumed.
  2943. */
  2944. if (udev->reset_resume) {
  2945. /*
  2946. * If the device morphs or switches modes when it is reset,
  2947. * we don't want to perform a reset-resume. We'll fail the
  2948. * resume, which will cause a logical disconnect, and then
  2949. * the device will be rediscovered.
  2950. */
  2951. retry_reset_resume:
  2952. if (udev->quirks & USB_QUIRK_RESET)
  2953. status = -ENODEV;
  2954. else
  2955. status = usb_reset_and_verify_device(udev);
  2956. }
  2957. /* 10.5.4.5 says be sure devices in the tree are still there.
  2958. * For now let's assume the device didn't go crazy on resume,
  2959. * and device drivers will know about any resume quirks.
  2960. */
  2961. if (status == 0) {
  2962. devstatus = 0;
  2963. status = usb_get_std_status(udev, USB_RECIP_DEVICE, 0, &devstatus);
  2964. /* If a normal resume failed, try doing a reset-resume */
  2965. if (status && !udev->reset_resume && udev->persist_enabled) {
  2966. dev_dbg(&udev->dev, "retry with reset-resume\n");
  2967. udev->reset_resume = 1;
  2968. goto retry_reset_resume;
  2969. }
  2970. }
  2971. if (status) {
  2972. dev_dbg(&udev->dev, "gone after usb resume? status %d\n",
  2973. status);
  2974. /*
  2975. * There are a few quirky devices which violate the standard
  2976. * by claiming to have remote wakeup enabled after a reset,
  2977. * which crash if the feature is cleared, hence check for
  2978. * udev->reset_resume
  2979. */
  2980. } else if (udev->actconfig && !udev->reset_resume) {
  2981. if (udev->speed < USB_SPEED_SUPER) {
  2982. if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP))
  2983. status = usb_disable_remote_wakeup(udev);
  2984. } else {
  2985. status = usb_get_std_status(udev, USB_RECIP_INTERFACE, 0,
  2986. &devstatus);
  2987. if (!status && devstatus & (USB_INTRF_STAT_FUNC_RW_CAP
  2988. | USB_INTRF_STAT_FUNC_RW))
  2989. status = usb_disable_remote_wakeup(udev);
  2990. }
  2991. if (status)
  2992. dev_dbg(&udev->dev,
  2993. "disable remote wakeup, status %d\n",
  2994. status);
  2995. status = 0;
  2996. }
  2997. return status;
  2998. }
  2999. /*
  3000. * There are some SS USB devices which take longer time for link training.
  3001. * XHCI specs 4.19.4 says that when Link training is successful, port
  3002. * sets CCS bit to 1. So if SW reads port status before successful link
  3003. * training, then it will not find device to be present.
  3004. * USB Analyzer log with such buggy devices show that in some cases
  3005. * device switch on the RX termination after long delay of host enabling
  3006. * the VBUS. In few other cases it has been seen that device fails to
  3007. * negotiate link training in first attempt. It has been
  3008. * reported till now that few devices take as long as 2000 ms to train
  3009. * the link after host enabling its VBUS and termination. Following
  3010. * routine implements a 2000 ms timeout for link training. If in a case
  3011. * link trains before timeout, loop will exit earlier.
  3012. *
  3013. * There are also some 2.0 hard drive based devices and 3.0 thumb
  3014. * drives that, when plugged into a 2.0 only port, take a long
  3015. * time to set CCS after VBUS enable.
  3016. *
  3017. * FIXME: If a device was connected before suspend, but was removed
  3018. * while system was asleep, then the loop in the following routine will
  3019. * only exit at timeout.
  3020. *
  3021. * This routine should only be called when persist is enabled.
  3022. */
  3023. static int wait_for_connected(struct usb_device *udev,
  3024. struct usb_hub *hub, int *port1,
  3025. u16 *portchange, u16 *portstatus)
  3026. {
  3027. int status = 0, delay_ms = 0;
  3028. while (delay_ms < 2000) {
  3029. if (status || *portstatus & USB_PORT_STAT_CONNECTION)
  3030. break;
  3031. if (!port_is_power_on(hub, *portstatus)) {
  3032. status = -ENODEV;
  3033. break;
  3034. }
  3035. msleep(20);
  3036. delay_ms += 20;
  3037. status = hub_port_status(hub, *port1, portstatus, portchange);
  3038. }
  3039. dev_dbg(&udev->dev, "Waited %dms for CONNECT\n", delay_ms);
  3040. return status;
  3041. }
  3042. /*
  3043. * usb_port_resume - re-activate a suspended usb device's upstream port
  3044. * @udev: device to re-activate, not a root hub
  3045. * Context: must be able to sleep; device not locked; pm locks held
  3046. *
  3047. * This will re-activate the suspended device, increasing power usage
  3048. * while letting drivers communicate again with its endpoints.
  3049. * USB resume explicitly guarantees that the power session between
  3050. * the host and the device is the same as it was when the device
  3051. * suspended.
  3052. *
  3053. * If @udev->reset_resume is set then this routine won't check that the
  3054. * port is still enabled. Furthermore, finish_port_resume() above will
  3055. * reset @udev. The end result is that a broken power session can be
  3056. * recovered and @udev will appear to persist across a loss of VBUS power.
  3057. *
  3058. * For example, if a host controller doesn't maintain VBUS suspend current
  3059. * during a system sleep or is reset when the system wakes up, all the USB
  3060. * power sessions below it will be broken. This is especially troublesome
  3061. * for mass-storage devices containing mounted filesystems, since the
  3062. * device will appear to have disconnected and all the memory mappings
  3063. * to it will be lost. Using the USB_PERSIST facility, the device can be
  3064. * made to appear as if it had not disconnected.
  3065. *
  3066. * This facility can be dangerous. Although usb_reset_and_verify_device() makes
  3067. * every effort to insure that the same device is present after the
  3068. * reset as before, it cannot provide a 100% guarantee. Furthermore it's
  3069. * quite possible for a device to remain unaltered but its media to be
  3070. * changed. If the user replaces a flash memory card while the system is
  3071. * asleep, he will have only himself to blame when the filesystem on the
  3072. * new card is corrupted and the system crashes.
  3073. *
  3074. * Returns 0 on success, else negative errno.
  3075. */
  3076. int usb_port_resume(struct usb_device *udev, pm_message_t msg)
  3077. {
  3078. struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
  3079. struct usb_port *port_dev = hub->ports[udev->portnum - 1];
  3080. int port1 = udev->portnum;
  3081. int status;
  3082. u16 portchange, portstatus;
  3083. if (!test_and_set_bit(port1, hub->child_usage_bits)) {
  3084. status = pm_runtime_get_sync(&port_dev->dev);
  3085. if (status < 0) {
  3086. dev_dbg(&udev->dev, "can't resume usb port, status %d\n",
  3087. status);
  3088. return status;
  3089. }
  3090. }
  3091. usb_lock_port(port_dev);
  3092. /* Skip the initial Clear-Suspend step for a remote wakeup */
  3093. status = hub_port_status(hub, port1, &portstatus, &portchange);
  3094. if (status == 0 && !port_is_suspended(hub, portstatus)) {
  3095. if (portchange & USB_PORT_STAT_C_SUSPEND)
  3096. pm_wakeup_event(&udev->dev, 0);
  3097. goto SuspendCleared;
  3098. }
  3099. /* see 7.1.7.7; affects power usage, but not budgeting */
  3100. if (hub_is_superspeed(hub->hdev))
  3101. status = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_U0);
  3102. else
  3103. status = usb_clear_port_feature(hub->hdev,
  3104. port1, USB_PORT_FEAT_SUSPEND);
  3105. if (status) {
  3106. dev_dbg(&port_dev->dev, "can't resume, status %d\n", status);
  3107. } else {
  3108. /* drive resume for USB_RESUME_TIMEOUT msec */
  3109. dev_dbg(&udev->dev, "usb %sresume\n",
  3110. (PMSG_IS_AUTO(msg) ? "auto-" : ""));
  3111. msleep(USB_RESUME_TIMEOUT);
  3112. /* Virtual root hubs can trigger on GET_PORT_STATUS to
  3113. * stop resume signaling. Then finish the resume
  3114. * sequence.
  3115. */
  3116. status = hub_port_status(hub, port1, &portstatus, &portchange);
  3117. }
  3118. SuspendCleared:
  3119. if (status == 0) {
  3120. udev->port_is_suspended = 0;
  3121. if (hub_is_superspeed(hub->hdev)) {
  3122. if (portchange & USB_PORT_STAT_C_LINK_STATE)
  3123. usb_clear_port_feature(hub->hdev, port1,
  3124. USB_PORT_FEAT_C_PORT_LINK_STATE);
  3125. } else {
  3126. if (portchange & USB_PORT_STAT_C_SUSPEND)
  3127. usb_clear_port_feature(hub->hdev, port1,
  3128. USB_PORT_FEAT_C_SUSPEND);
  3129. }
  3130. /* TRSMRCY = 10 msec */
  3131. msleep(10);
  3132. }
  3133. if (udev->persist_enabled)
  3134. status = wait_for_connected(udev, hub, &port1, &portchange,
  3135. &portstatus);
  3136. status = check_port_resume_type(udev,
  3137. hub, port1, status, portchange, portstatus);
  3138. if (status == 0)
  3139. status = finish_port_resume(udev);
  3140. if (status < 0) {
  3141. dev_dbg(&udev->dev, "can't resume, status %d\n", status);
  3142. hub_port_logical_disconnect(hub, port1);
  3143. } else {
  3144. /* Try to enable USB2 hardware LPM */
  3145. usb_enable_usb2_hardware_lpm(udev);
  3146. /* Try to enable USB3 LTM */
  3147. usb_enable_ltm(udev);
  3148. }
  3149. usb_unlock_port(port_dev);
  3150. return status;
  3151. }
  3152. int usb_remote_wakeup(struct usb_device *udev)
  3153. {
  3154. int status = 0;
  3155. usb_lock_device(udev);
  3156. if (udev->state == USB_STATE_SUSPENDED) {
  3157. dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
  3158. status = usb_autoresume_device(udev);
  3159. if (status == 0) {
  3160. /* Let the drivers do their thing, then... */
  3161. usb_autosuspend_device(udev);
  3162. }
  3163. }
  3164. usb_unlock_device(udev);
  3165. return status;
  3166. }
  3167. /* Returns 1 if there was a remote wakeup and a connect status change. */
  3168. static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
  3169. u16 portstatus, u16 portchange)
  3170. __must_hold(&port_dev->status_lock)
  3171. {
  3172. struct usb_port *port_dev = hub->ports[port - 1];
  3173. struct usb_device *hdev;
  3174. struct usb_device *udev;
  3175. int connect_change = 0;
  3176. u16 link_state;
  3177. int ret;
  3178. hdev = hub->hdev;
  3179. udev = port_dev->child;
  3180. if (!hub_is_superspeed(hdev)) {
  3181. if (!(portchange & USB_PORT_STAT_C_SUSPEND))
  3182. return 0;
  3183. usb_clear_port_feature(hdev, port, USB_PORT_FEAT_C_SUSPEND);
  3184. } else {
  3185. link_state = portstatus & USB_PORT_STAT_LINK_STATE;
  3186. if (!udev || udev->state != USB_STATE_SUSPENDED ||
  3187. (link_state != USB_SS_PORT_LS_U0 &&
  3188. link_state != USB_SS_PORT_LS_U1 &&
  3189. link_state != USB_SS_PORT_LS_U2))
  3190. return 0;
  3191. }
  3192. if (udev) {
  3193. /* TRSMRCY = 10 msec */
  3194. msleep(10);
  3195. usb_unlock_port(port_dev);
  3196. ret = usb_remote_wakeup(udev);
  3197. usb_lock_port(port_dev);
  3198. if (ret < 0)
  3199. connect_change = 1;
  3200. } else {
  3201. ret = -ENODEV;
  3202. hub_port_disable(hub, port, 1);
  3203. }
  3204. dev_dbg(&port_dev->dev, "resume, status %d\n", ret);
  3205. return connect_change;
  3206. }
  3207. static int check_ports_changed(struct usb_hub *hub)
  3208. {
  3209. int port1;
  3210. for (port1 = 1; port1 <= hub->hdev->maxchild; ++port1) {
  3211. u16 portstatus, portchange;
  3212. int status;
  3213. status = hub_port_status(hub, port1, &portstatus, &portchange);
  3214. if (!status && portchange)
  3215. return 1;
  3216. }
  3217. return 0;
  3218. }
  3219. static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
  3220. {
  3221. struct usb_hub *hub = usb_get_intfdata(intf);
  3222. struct usb_device *hdev = hub->hdev;
  3223. unsigned port1;
  3224. int status;
  3225. /*
  3226. * Warn if children aren't already suspended.
  3227. * Also, add up the number of wakeup-enabled descendants.
  3228. */
  3229. hub->wakeup_enabled_descendants = 0;
  3230. for (port1 = 1; port1 <= hdev->maxchild; port1++) {
  3231. struct usb_port *port_dev = hub->ports[port1 - 1];
  3232. struct usb_device *udev = port_dev->child;
  3233. if (udev && udev->can_submit) {
  3234. dev_warn(&port_dev->dev, "device %s not suspended yet\n",
  3235. dev_name(&udev->dev));
  3236. if (PMSG_IS_AUTO(msg))
  3237. return -EBUSY;
  3238. }
  3239. if (udev)
  3240. hub->wakeup_enabled_descendants +=
  3241. wakeup_enabled_descendants(udev);
  3242. }
  3243. if (hdev->do_remote_wakeup && hub->quirk_check_port_auto_suspend) {
  3244. /* check if there are changes pending on hub ports */
  3245. if (check_ports_changed(hub)) {
  3246. if (PMSG_IS_AUTO(msg))
  3247. return -EBUSY;
  3248. pm_wakeup_event(&hdev->dev, 2000);
  3249. }
  3250. }
  3251. if (hub_is_superspeed(hdev) && hdev->do_remote_wakeup) {
  3252. /* Enable hub to send remote wakeup for all ports. */
  3253. for (port1 = 1; port1 <= hdev->maxchild; port1++) {
  3254. status = set_port_feature(hdev,
  3255. port1 |
  3256. USB_PORT_FEAT_REMOTE_WAKE_CONNECT |
  3257. USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT |
  3258. USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT,
  3259. USB_PORT_FEAT_REMOTE_WAKE_MASK);
  3260. }
  3261. }
  3262. dev_dbg(&intf->dev, "%s\n", __func__);
  3263. /* stop hub_wq and related activity */
  3264. hub_quiesce(hub, HUB_SUSPEND);
  3265. return 0;
  3266. }
  3267. /* Report wakeup requests from the ports of a resuming root hub */
  3268. static void report_wakeup_requests(struct usb_hub *hub)
  3269. {
  3270. struct usb_device *hdev = hub->hdev;
  3271. struct usb_device *udev;
  3272. struct usb_hcd *hcd;
  3273. unsigned long resuming_ports;
  3274. int i;
  3275. if (hdev->parent)
  3276. return; /* Not a root hub */
  3277. hcd = bus_to_hcd(hdev->bus);
  3278. if (hcd->driver->get_resuming_ports) {
  3279. /*
  3280. * The get_resuming_ports() method returns a bitmap (origin 0)
  3281. * of ports which have started wakeup signaling but have not
  3282. * yet finished resuming. During system resume we will
  3283. * resume all the enabled ports, regardless of any wakeup
  3284. * signals, which means the wakeup requests would be lost.
  3285. * To prevent this, report them to the PM core here.
  3286. */
  3287. resuming_ports = hcd->driver->get_resuming_ports(hcd);
  3288. for (i = 0; i < hdev->maxchild; ++i) {
  3289. if (test_bit(i, &resuming_ports)) {
  3290. udev = hub->ports[i]->child;
  3291. if (udev)
  3292. pm_wakeup_event(&udev->dev, 0);
  3293. }
  3294. }
  3295. }
  3296. }
  3297. static int hub_resume(struct usb_interface *intf)
  3298. {
  3299. struct usb_hub *hub = usb_get_intfdata(intf);
  3300. dev_dbg(&intf->dev, "%s\n", __func__);
  3301. hub_activate(hub, HUB_RESUME);
  3302. /*
  3303. * This should be called only for system resume, not runtime resume.
  3304. * We can't tell the difference here, so some wakeup requests will be
  3305. * reported at the wrong time or more than once. This shouldn't
  3306. * matter much, so long as they do get reported.
  3307. */
  3308. report_wakeup_requests(hub);
  3309. return 0;
  3310. }
  3311. static int hub_reset_resume(struct usb_interface *intf)
  3312. {
  3313. struct usb_hub *hub = usb_get_intfdata(intf);
  3314. dev_dbg(&intf->dev, "%s\n", __func__);
  3315. hub_activate(hub, HUB_RESET_RESUME);
  3316. return 0;
  3317. }
  3318. /**
  3319. * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power
  3320. * @rhdev: struct usb_device for the root hub
  3321. *
  3322. * The USB host controller driver calls this function when its root hub
  3323. * is resumed and Vbus power has been interrupted or the controller
  3324. * has been reset. The routine marks @rhdev as having lost power.
  3325. * When the hub driver is resumed it will take notice and carry out
  3326. * power-session recovery for all the "USB-PERSIST"-enabled child devices;
  3327. * the others will be disconnected.
  3328. */
  3329. void usb_root_hub_lost_power(struct usb_device *rhdev)
  3330. {
  3331. dev_notice(&rhdev->dev, "root hub lost power or was reset\n");
  3332. rhdev->reset_resume = 1;
  3333. }
  3334. EXPORT_SYMBOL_GPL(usb_root_hub_lost_power);
  3335. static const char * const usb3_lpm_names[] = {
  3336. "U0",
  3337. "U1",
  3338. "U2",
  3339. "U3",
  3340. };
  3341. /*
  3342. * Send a Set SEL control transfer to the device, prior to enabling
  3343. * device-initiated U1 or U2. This lets the device know the exit latencies from
  3344. * the time the device initiates a U1 or U2 exit, to the time it will receive a
  3345. * packet from the host.
  3346. *
  3347. * This function will fail if the SEL or PEL values for udev are greater than
  3348. * the maximum allowed values for the link state to be enabled.
  3349. */
  3350. static int usb_req_set_sel(struct usb_device *udev, enum usb3_link_state state)
  3351. {
  3352. struct usb_set_sel_req *sel_values;
  3353. unsigned long long u1_sel;
  3354. unsigned long long u1_pel;
  3355. unsigned long long u2_sel;
  3356. unsigned long long u2_pel;
  3357. int ret;
  3358. if (udev->state != USB_STATE_CONFIGURED)
  3359. return 0;
  3360. /* Convert SEL and PEL stored in ns to us */
  3361. u1_sel = DIV_ROUND_UP(udev->u1_params.sel, 1000);
  3362. u1_pel = DIV_ROUND_UP(udev->u1_params.pel, 1000);
  3363. u2_sel = DIV_ROUND_UP(udev->u2_params.sel, 1000);
  3364. u2_pel = DIV_ROUND_UP(udev->u2_params.pel, 1000);
  3365. /*
  3366. * Make sure that the calculated SEL and PEL values for the link
  3367. * state we're enabling aren't bigger than the max SEL/PEL
  3368. * value that will fit in the SET SEL control transfer.
  3369. * Otherwise the device would get an incorrect idea of the exit
  3370. * latency for the link state, and could start a device-initiated
  3371. * U1/U2 when the exit latencies are too high.
  3372. */
  3373. if ((state == USB3_LPM_U1 &&
  3374. (u1_sel > USB3_LPM_MAX_U1_SEL_PEL ||
  3375. u1_pel > USB3_LPM_MAX_U1_SEL_PEL)) ||
  3376. (state == USB3_LPM_U2 &&
  3377. (u2_sel > USB3_LPM_MAX_U2_SEL_PEL ||
  3378. u2_pel > USB3_LPM_MAX_U2_SEL_PEL))) {
  3379. dev_dbg(&udev->dev, "Device-initiated %s disabled due to long SEL %llu us or PEL %llu us\n",
  3380. usb3_lpm_names[state], u1_sel, u1_pel);
  3381. return -EINVAL;
  3382. }
  3383. /*
  3384. * If we're enabling device-initiated LPM for one link state,
  3385. * but the other link state has a too high SEL or PEL value,
  3386. * just set those values to the max in the Set SEL request.
  3387. */
  3388. if (u1_sel > USB3_LPM_MAX_U1_SEL_PEL)
  3389. u1_sel = USB3_LPM_MAX_U1_SEL_PEL;
  3390. if (u1_pel > USB3_LPM_MAX_U1_SEL_PEL)
  3391. u1_pel = USB3_LPM_MAX_U1_SEL_PEL;
  3392. if (u2_sel > USB3_LPM_MAX_U2_SEL_PEL)
  3393. u2_sel = USB3_LPM_MAX_U2_SEL_PEL;
  3394. if (u2_pel > USB3_LPM_MAX_U2_SEL_PEL)
  3395. u2_pel = USB3_LPM_MAX_U2_SEL_PEL;
  3396. /*
  3397. * usb_enable_lpm() can be called as part of a failed device reset,
  3398. * which may be initiated by an error path of a mass storage driver.
  3399. * Therefore, use GFP_NOIO.
  3400. */
  3401. sel_values = kmalloc(sizeof *(sel_values), GFP_NOIO);
  3402. if (!sel_values)
  3403. return -ENOMEM;
  3404. sel_values->u1_sel = u1_sel;
  3405. sel_values->u1_pel = u1_pel;
  3406. sel_values->u2_sel = cpu_to_le16(u2_sel);
  3407. sel_values->u2_pel = cpu_to_le16(u2_pel);
  3408. ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  3409. USB_REQ_SET_SEL,
  3410. USB_RECIP_DEVICE,
  3411. 0, 0,
  3412. sel_values, sizeof *(sel_values),
  3413. USB_CTRL_SET_TIMEOUT);
  3414. kfree(sel_values);
  3415. return ret;
  3416. }
  3417. /*
  3418. * Enable or disable device-initiated U1 or U2 transitions.
  3419. */
  3420. static int usb_set_device_initiated_lpm(struct usb_device *udev,
  3421. enum usb3_link_state state, bool enable)
  3422. {
  3423. int ret;
  3424. int feature;
  3425. switch (state) {
  3426. case USB3_LPM_U1:
  3427. feature = USB_DEVICE_U1_ENABLE;
  3428. break;
  3429. case USB3_LPM_U2:
  3430. feature = USB_DEVICE_U2_ENABLE;
  3431. break;
  3432. default:
  3433. dev_warn(&udev->dev, "%s: Can't %s non-U1 or U2 state.\n",
  3434. __func__, enable ? "enable" : "disable");
  3435. return -EINVAL;
  3436. }
  3437. if (udev->state != USB_STATE_CONFIGURED) {
  3438. dev_dbg(&udev->dev, "%s: Can't %s %s state "
  3439. "for unconfigured device.\n",
  3440. __func__, enable ? "enable" : "disable",
  3441. usb3_lpm_names[state]);
  3442. return 0;
  3443. }
  3444. if (enable) {
  3445. /*
  3446. * Now send the control transfer to enable device-initiated LPM
  3447. * for either U1 or U2.
  3448. */
  3449. ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  3450. USB_REQ_SET_FEATURE,
  3451. USB_RECIP_DEVICE,
  3452. feature,
  3453. 0, NULL, 0,
  3454. USB_CTRL_SET_TIMEOUT);
  3455. } else {
  3456. ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  3457. USB_REQ_CLEAR_FEATURE,
  3458. USB_RECIP_DEVICE,
  3459. feature,
  3460. 0, NULL, 0,
  3461. USB_CTRL_SET_TIMEOUT);
  3462. }
  3463. if (ret < 0) {
  3464. dev_warn(&udev->dev, "%s of device-initiated %s failed.\n",
  3465. enable ? "Enable" : "Disable",
  3466. usb3_lpm_names[state]);
  3467. return -EBUSY;
  3468. }
  3469. return 0;
  3470. }
  3471. static int usb_set_lpm_timeout(struct usb_device *udev,
  3472. enum usb3_link_state state, int timeout)
  3473. {
  3474. int ret;
  3475. int feature;
  3476. switch (state) {
  3477. case USB3_LPM_U1:
  3478. feature = USB_PORT_FEAT_U1_TIMEOUT;
  3479. break;
  3480. case USB3_LPM_U2:
  3481. feature = USB_PORT_FEAT_U2_TIMEOUT;
  3482. break;
  3483. default:
  3484. dev_warn(&udev->dev, "%s: Can't set timeout for non-U1 or U2 state.\n",
  3485. __func__);
  3486. return -EINVAL;
  3487. }
  3488. if (state == USB3_LPM_U1 && timeout > USB3_LPM_U1_MAX_TIMEOUT &&
  3489. timeout != USB3_LPM_DEVICE_INITIATED) {
  3490. dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x, "
  3491. "which is a reserved value.\n",
  3492. usb3_lpm_names[state], timeout);
  3493. return -EINVAL;
  3494. }
  3495. ret = set_port_feature(udev->parent,
  3496. USB_PORT_LPM_TIMEOUT(timeout) | udev->portnum,
  3497. feature);
  3498. if (ret < 0) {
  3499. dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x,"
  3500. "error code %i\n", usb3_lpm_names[state],
  3501. timeout, ret);
  3502. return -EBUSY;
  3503. }
  3504. if (state == USB3_LPM_U1)
  3505. udev->u1_params.timeout = timeout;
  3506. else
  3507. udev->u2_params.timeout = timeout;
  3508. return 0;
  3509. }
  3510. /*
  3511. * Enable the hub-initiated U1/U2 idle timeouts, and enable device-initiated
  3512. * U1/U2 entry.
  3513. *
  3514. * We will attempt to enable U1 or U2, but there are no guarantees that the
  3515. * control transfers to set the hub timeout or enable device-initiated U1/U2
  3516. * will be successful.
  3517. *
  3518. * If the control transfer to enable device-initiated U1/U2 entry fails, then
  3519. * hub-initiated U1/U2 will be disabled.
  3520. *
  3521. * If we cannot set the parent hub U1/U2 timeout, we attempt to let the xHCI
  3522. * driver know about it. If that call fails, it should be harmless, and just
  3523. * take up more slightly more bus bandwidth for unnecessary U1/U2 exit latency.
  3524. */
  3525. static void usb_enable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
  3526. enum usb3_link_state state)
  3527. {
  3528. int timeout, ret;
  3529. __u8 u1_mel = udev->bos->ss_cap->bU1devExitLat;
  3530. __le16 u2_mel = udev->bos->ss_cap->bU2DevExitLat;
  3531. /* If the device says it doesn't have *any* exit latency to come out of
  3532. * U1 or U2, it's probably lying. Assume it doesn't implement that link
  3533. * state.
  3534. */
  3535. if ((state == USB3_LPM_U1 && u1_mel == 0) ||
  3536. (state == USB3_LPM_U2 && u2_mel == 0))
  3537. return;
  3538. /*
  3539. * First, let the device know about the exit latencies
  3540. * associated with the link state we're about to enable.
  3541. */
  3542. ret = usb_req_set_sel(udev, state);
  3543. if (ret < 0) {
  3544. dev_warn(&udev->dev, "Set SEL for device-initiated %s failed.\n",
  3545. usb3_lpm_names[state]);
  3546. return;
  3547. }
  3548. /* We allow the host controller to set the U1/U2 timeout internally
  3549. * first, so that it can change its schedule to account for the
  3550. * additional latency to send data to a device in a lower power
  3551. * link state.
  3552. */
  3553. timeout = hcd->driver->enable_usb3_lpm_timeout(hcd, udev, state);
  3554. /* xHCI host controller doesn't want to enable this LPM state. */
  3555. if (timeout == 0)
  3556. return;
  3557. if (timeout < 0) {
  3558. dev_warn(&udev->dev, "Could not enable %s link state, "
  3559. "xHCI error %i.\n", usb3_lpm_names[state],
  3560. timeout);
  3561. return;
  3562. }
  3563. if (usb_set_lpm_timeout(udev, state, timeout)) {
  3564. /* If we can't set the parent hub U1/U2 timeout,
  3565. * device-initiated LPM won't be allowed either, so let the xHCI
  3566. * host know that this link state won't be enabled.
  3567. */
  3568. hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state);
  3569. return;
  3570. }
  3571. /* Only a configured device will accept the Set Feature
  3572. * U1/U2_ENABLE
  3573. */
  3574. if (udev->actconfig &&
  3575. usb_set_device_initiated_lpm(udev, state, true) == 0) {
  3576. if (state == USB3_LPM_U1)
  3577. udev->usb3_lpm_u1_enabled = 1;
  3578. else if (state == USB3_LPM_U2)
  3579. udev->usb3_lpm_u2_enabled = 1;
  3580. } else {
  3581. /* Don't request U1/U2 entry if the device
  3582. * cannot transition to U1/U2.
  3583. */
  3584. usb_set_lpm_timeout(udev, state, 0);
  3585. hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state);
  3586. }
  3587. }
  3588. /*
  3589. * Disable the hub-initiated U1/U2 idle timeouts, and disable device-initiated
  3590. * U1/U2 entry.
  3591. *
  3592. * If this function returns -EBUSY, the parent hub will still allow U1/U2 entry.
  3593. * If zero is returned, the parent will not allow the link to go into U1/U2.
  3594. *
  3595. * If zero is returned, device-initiated U1/U2 entry may still be enabled, but
  3596. * it won't have an effect on the bus link state because the parent hub will
  3597. * still disallow device-initiated U1/U2 entry.
  3598. *
  3599. * If zero is returned, the xHCI host controller may still think U1/U2 entry is
  3600. * possible. The result will be slightly more bus bandwidth will be taken up
  3601. * (to account for U1/U2 exit latency), but it should be harmless.
  3602. */
  3603. static int usb_disable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
  3604. enum usb3_link_state state)
  3605. {
  3606. switch (state) {
  3607. case USB3_LPM_U1:
  3608. case USB3_LPM_U2:
  3609. break;
  3610. default:
  3611. dev_warn(&udev->dev, "%s: Can't disable non-U1 or U2 state.\n",
  3612. __func__);
  3613. return -EINVAL;
  3614. }
  3615. if (usb_set_lpm_timeout(udev, state, 0))
  3616. return -EBUSY;
  3617. usb_set_device_initiated_lpm(udev, state, false);
  3618. if (hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state))
  3619. dev_warn(&udev->dev, "Could not disable xHCI %s timeout, "
  3620. "bus schedule bandwidth may be impacted.\n",
  3621. usb3_lpm_names[state]);
  3622. /* As soon as usb_set_lpm_timeout(0) return 0, hub initiated LPM
  3623. * is disabled. Hub will disallows link to enter U1/U2 as well,
  3624. * even device is initiating LPM. Hence LPM is disabled if hub LPM
  3625. * timeout set to 0, no matter device-initiated LPM is disabled or
  3626. * not.
  3627. */
  3628. if (state == USB3_LPM_U1)
  3629. udev->usb3_lpm_u1_enabled = 0;
  3630. else if (state == USB3_LPM_U2)
  3631. udev->usb3_lpm_u2_enabled = 0;
  3632. return 0;
  3633. }
  3634. /*
  3635. * Disable hub-initiated and device-initiated U1 and U2 entry.
  3636. * Caller must own the bandwidth_mutex.
  3637. *
  3638. * This will call usb_enable_lpm() on failure, which will decrement
  3639. * lpm_disable_count, and will re-enable LPM if lpm_disable_count reaches zero.
  3640. */
  3641. int usb_disable_lpm(struct usb_device *udev)
  3642. {
  3643. struct usb_hcd *hcd;
  3644. if (!udev || !udev->parent ||
  3645. udev->speed < USB_SPEED_SUPER ||
  3646. !udev->lpm_capable ||
  3647. udev->state < USB_STATE_DEFAULT)
  3648. return 0;
  3649. hcd = bus_to_hcd(udev->bus);
  3650. if (!hcd || !hcd->driver->disable_usb3_lpm_timeout)
  3651. return 0;
  3652. udev->lpm_disable_count++;
  3653. if ((udev->u1_params.timeout == 0 && udev->u2_params.timeout == 0))
  3654. return 0;
  3655. /* If LPM is enabled, attempt to disable it. */
  3656. if (usb_disable_link_state(hcd, udev, USB3_LPM_U1))
  3657. goto enable_lpm;
  3658. if (usb_disable_link_state(hcd, udev, USB3_LPM_U2))
  3659. goto enable_lpm;
  3660. return 0;
  3661. enable_lpm:
  3662. usb_enable_lpm(udev);
  3663. return -EBUSY;
  3664. }
  3665. EXPORT_SYMBOL_GPL(usb_disable_lpm);
  3666. /* Grab the bandwidth_mutex before calling usb_disable_lpm() */
  3667. int usb_unlocked_disable_lpm(struct usb_device *udev)
  3668. {
  3669. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  3670. int ret;
  3671. if (!hcd)
  3672. return -EINVAL;
  3673. mutex_lock(hcd->bandwidth_mutex);
  3674. ret = usb_disable_lpm(udev);
  3675. mutex_unlock(hcd->bandwidth_mutex);
  3676. return ret;
  3677. }
  3678. EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
  3679. /*
  3680. * Attempt to enable device-initiated and hub-initiated U1 and U2 entry. The
  3681. * xHCI host policy may prevent U1 or U2 from being enabled.
  3682. *
  3683. * Other callers may have disabled link PM, so U1 and U2 entry will be disabled
  3684. * until the lpm_disable_count drops to zero. Caller must own the
  3685. * bandwidth_mutex.
  3686. */
  3687. void usb_enable_lpm(struct usb_device *udev)
  3688. {
  3689. struct usb_hcd *hcd;
  3690. struct usb_hub *hub;
  3691. struct usb_port *port_dev;
  3692. if (!udev || !udev->parent ||
  3693. udev->speed < USB_SPEED_SUPER ||
  3694. !udev->lpm_capable ||
  3695. udev->state < USB_STATE_DEFAULT)
  3696. return;
  3697. udev->lpm_disable_count--;
  3698. hcd = bus_to_hcd(udev->bus);
  3699. /* Double check that we can both enable and disable LPM.
  3700. * Device must be configured to accept set feature U1/U2 timeout.
  3701. */
  3702. if (!hcd || !hcd->driver->enable_usb3_lpm_timeout ||
  3703. !hcd->driver->disable_usb3_lpm_timeout)
  3704. return;
  3705. if (udev->lpm_disable_count > 0)
  3706. return;
  3707. hub = usb_hub_to_struct_hub(udev->parent);
  3708. if (!hub)
  3709. return;
  3710. port_dev = hub->ports[udev->portnum - 1];
  3711. if (port_dev->usb3_lpm_u1_permit)
  3712. usb_enable_link_state(hcd, udev, USB3_LPM_U1);
  3713. if (port_dev->usb3_lpm_u2_permit)
  3714. usb_enable_link_state(hcd, udev, USB3_LPM_U2);
  3715. }
  3716. EXPORT_SYMBOL_GPL(usb_enable_lpm);
  3717. /* Grab the bandwidth_mutex before calling usb_enable_lpm() */
  3718. void usb_unlocked_enable_lpm(struct usb_device *udev)
  3719. {
  3720. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  3721. if (!hcd)
  3722. return;
  3723. mutex_lock(hcd->bandwidth_mutex);
  3724. usb_enable_lpm(udev);
  3725. mutex_unlock(hcd->bandwidth_mutex);
  3726. }
  3727. EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
  3728. /* usb3 devices use U3 for disabled, make sure remote wakeup is disabled */
  3729. static void hub_usb3_port_prepare_disable(struct usb_hub *hub,
  3730. struct usb_port *port_dev)
  3731. {
  3732. struct usb_device *udev = port_dev->child;
  3733. int ret;
  3734. if (udev && udev->port_is_suspended && udev->do_remote_wakeup) {
  3735. ret = hub_set_port_link_state(hub, port_dev->portnum,
  3736. USB_SS_PORT_LS_U0);
  3737. if (!ret) {
  3738. msleep(USB_RESUME_TIMEOUT);
  3739. ret = usb_disable_remote_wakeup(udev);
  3740. }
  3741. if (ret)
  3742. dev_warn(&udev->dev,
  3743. "Port disable: can't disable remote wake\n");
  3744. udev->do_remote_wakeup = 0;
  3745. }
  3746. }
  3747. #else /* CONFIG_PM */
  3748. #define hub_suspend NULL
  3749. #define hub_resume NULL
  3750. #define hub_reset_resume NULL
  3751. static inline void hub_usb3_port_prepare_disable(struct usb_hub *hub,
  3752. struct usb_port *port_dev) { }
  3753. int usb_disable_lpm(struct usb_device *udev)
  3754. {
  3755. return 0;
  3756. }
  3757. EXPORT_SYMBOL_GPL(usb_disable_lpm);
  3758. void usb_enable_lpm(struct usb_device *udev) { }
  3759. EXPORT_SYMBOL_GPL(usb_enable_lpm);
  3760. int usb_unlocked_disable_lpm(struct usb_device *udev)
  3761. {
  3762. return 0;
  3763. }
  3764. EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
  3765. void usb_unlocked_enable_lpm(struct usb_device *udev) { }
  3766. EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
  3767. int usb_disable_ltm(struct usb_device *udev)
  3768. {
  3769. return 0;
  3770. }
  3771. EXPORT_SYMBOL_GPL(usb_disable_ltm);
  3772. void usb_enable_ltm(struct usb_device *udev) { }
  3773. EXPORT_SYMBOL_GPL(usb_enable_ltm);
  3774. static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
  3775. u16 portstatus, u16 portchange)
  3776. {
  3777. return 0;
  3778. }
  3779. #endif /* CONFIG_PM */
  3780. /*
  3781. * USB-3 does not have a similar link state as USB-2 that will avoid negotiating
  3782. * a connection with a plugged-in cable but will signal the host when the cable
  3783. * is unplugged. Disable remote wake and set link state to U3 for USB-3 devices
  3784. */
  3785. static int hub_port_disable(struct usb_hub *hub, int port1, int set_state)
  3786. {
  3787. struct usb_port *port_dev = hub->ports[port1 - 1];
  3788. struct usb_device *hdev = hub->hdev;
  3789. int ret = 0;
  3790. if (!hub->error) {
  3791. if (hub_is_superspeed(hub->hdev)) {
  3792. hub_usb3_port_prepare_disable(hub, port_dev);
  3793. ret = hub_set_port_link_state(hub, port_dev->portnum,
  3794. USB_SS_PORT_LS_U3);
  3795. } else {
  3796. ret = usb_clear_port_feature(hdev, port1,
  3797. USB_PORT_FEAT_ENABLE);
  3798. }
  3799. }
  3800. if (port_dev->child && set_state)
  3801. usb_set_device_state(port_dev->child, USB_STATE_NOTATTACHED);
  3802. if (ret && ret != -ENODEV)
  3803. dev_err(&port_dev->dev, "cannot disable (err = %d)\n", ret);
  3804. return ret;
  3805. }
  3806. /*
  3807. * usb_port_disable - disable a usb device's upstream port
  3808. * @udev: device to disable
  3809. * Context: @udev locked, must be able to sleep.
  3810. *
  3811. * Disables a USB device that isn't in active use.
  3812. */
  3813. int usb_port_disable(struct usb_device *udev)
  3814. {
  3815. struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
  3816. return hub_port_disable(hub, udev->portnum, 0);
  3817. }
  3818. /* USB 2.0 spec, 7.1.7.3 / fig 7-29:
  3819. *
  3820. * Between connect detection and reset signaling there must be a delay
  3821. * of 100ms at least for debounce and power-settling. The corresponding
  3822. * timer shall restart whenever the downstream port detects a disconnect.
  3823. *
  3824. * Apparently there are some bluetooth and irda-dongles and a number of
  3825. * low-speed devices for which this debounce period may last over a second.
  3826. * Not covered by the spec - but easy to deal with.
  3827. *
  3828. * This implementation uses a 1500ms total debounce timeout; if the
  3829. * connection isn't stable by then it returns -ETIMEDOUT. It checks
  3830. * every 25ms for transient disconnects. When the port status has been
  3831. * unchanged for 100ms it returns the port status.
  3832. */
  3833. int hub_port_debounce(struct usb_hub *hub, int port1, bool must_be_connected)
  3834. {
  3835. int ret;
  3836. u16 portchange, portstatus;
  3837. unsigned connection = 0xffff;
  3838. int total_time, stable_time = 0;
  3839. struct usb_port *port_dev = hub->ports[port1 - 1];
  3840. for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
  3841. ret = hub_port_status(hub, port1, &portstatus, &portchange);
  3842. if (ret < 0)
  3843. return ret;
  3844. if (!(portchange & USB_PORT_STAT_C_CONNECTION) &&
  3845. (portstatus & USB_PORT_STAT_CONNECTION) == connection) {
  3846. if (!must_be_connected ||
  3847. (connection == USB_PORT_STAT_CONNECTION))
  3848. stable_time += HUB_DEBOUNCE_STEP;
  3849. if (stable_time >= HUB_DEBOUNCE_STABLE)
  3850. break;
  3851. } else {
  3852. stable_time = 0;
  3853. connection = portstatus & USB_PORT_STAT_CONNECTION;
  3854. }
  3855. if (portchange & USB_PORT_STAT_C_CONNECTION) {
  3856. usb_clear_port_feature(hub->hdev, port1,
  3857. USB_PORT_FEAT_C_CONNECTION);
  3858. }
  3859. if (total_time >= HUB_DEBOUNCE_TIMEOUT)
  3860. break;
  3861. msleep(HUB_DEBOUNCE_STEP);
  3862. }
  3863. dev_dbg(&port_dev->dev, "debounce total %dms stable %dms status 0x%x\n",
  3864. total_time, stable_time, portstatus);
  3865. if (stable_time < HUB_DEBOUNCE_STABLE)
  3866. return -ETIMEDOUT;
  3867. return portstatus;
  3868. }
  3869. void usb_ep0_reinit(struct usb_device *udev)
  3870. {
  3871. usb_disable_endpoint(udev, 0 + USB_DIR_IN, true);
  3872. usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true);
  3873. usb_enable_endpoint(udev, &udev->ep0, true);
  3874. }
  3875. EXPORT_SYMBOL_GPL(usb_ep0_reinit);
  3876. #define usb_sndaddr0pipe() (PIPE_CONTROL << 30)
  3877. #define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN)
  3878. static int hub_set_address(struct usb_device *udev, int devnum)
  3879. {
  3880. int retval;
  3881. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  3882. /*
  3883. * The host controller will choose the device address,
  3884. * instead of the core having chosen it earlier
  3885. */
  3886. if (!hcd->driver->address_device && devnum <= 1)
  3887. return -EINVAL;
  3888. if (udev->state == USB_STATE_ADDRESS)
  3889. return 0;
  3890. if (udev->state != USB_STATE_DEFAULT)
  3891. return -EINVAL;
  3892. if (hcd->driver->address_device)
  3893. retval = hcd->driver->address_device(hcd, udev);
  3894. else
  3895. retval = usb_control_msg(udev, usb_sndaddr0pipe(),
  3896. USB_REQ_SET_ADDRESS, 0, devnum, 0,
  3897. NULL, 0, USB_CTRL_SET_TIMEOUT);
  3898. if (retval == 0) {
  3899. update_devnum(udev, devnum);
  3900. /* Device now using proper address. */
  3901. usb_set_device_state(udev, USB_STATE_ADDRESS);
  3902. usb_ep0_reinit(udev);
  3903. }
  3904. return retval;
  3905. }
  3906. /*
  3907. * There are reports of USB 3.0 devices that say they support USB 2.0 Link PM
  3908. * when they're plugged into a USB 2.0 port, but they don't work when LPM is
  3909. * enabled.
  3910. *
  3911. * Only enable USB 2.0 Link PM if the port is internal (hardwired), or the
  3912. * device says it supports the new USB 2.0 Link PM errata by setting the BESL
  3913. * support bit in the BOS descriptor.
  3914. */
  3915. static void hub_set_initial_usb2_lpm_policy(struct usb_device *udev)
  3916. {
  3917. struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
  3918. int connect_type = USB_PORT_CONNECT_TYPE_UNKNOWN;
  3919. if (!udev->usb2_hw_lpm_capable || !udev->bos)
  3920. return;
  3921. if (hub)
  3922. connect_type = hub->ports[udev->portnum - 1]->connect_type;
  3923. if ((udev->bos->ext_cap->bmAttributes & cpu_to_le32(USB_BESL_SUPPORT)) ||
  3924. connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) {
  3925. udev->usb2_hw_lpm_allowed = 1;
  3926. usb_enable_usb2_hardware_lpm(udev);
  3927. }
  3928. }
  3929. static int hub_enable_device(struct usb_device *udev)
  3930. {
  3931. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  3932. if (!hcd->driver->enable_device)
  3933. return 0;
  3934. if (udev->state == USB_STATE_ADDRESS)
  3935. return 0;
  3936. if (udev->state != USB_STATE_DEFAULT)
  3937. return -EINVAL;
  3938. return hcd->driver->enable_device(hcd, udev);
  3939. }
  3940. /* Reset device, (re)assign address, get device descriptor.
  3941. * Device connection must be stable, no more debouncing needed.
  3942. * Returns device in USB_STATE_ADDRESS, except on error.
  3943. *
  3944. * If this is called for an already-existing device (as part of
  3945. * usb_reset_and_verify_device), the caller must own the device lock and
  3946. * the port lock. For a newly detected device that is not accessible
  3947. * through any global pointers, it's not necessary to lock the device,
  3948. * but it is still necessary to lock the port.
  3949. */
  3950. static int
  3951. hub_port_init(struct usb_hub *hub, struct usb_device *udev, int port1,
  3952. int retry_counter)
  3953. {
  3954. struct usb_device *hdev = hub->hdev;
  3955. struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
  3956. struct usb_port *port_dev = hub->ports[port1 - 1];
  3957. int retries, operations, retval, i;
  3958. unsigned delay = HUB_SHORT_RESET_TIME;
  3959. enum usb_device_speed oldspeed = udev->speed;
  3960. const char *speed;
  3961. int devnum = udev->devnum;
  3962. const char *driver_name;
  3963. /* root hub ports have a slightly longer reset period
  3964. * (from USB 2.0 spec, section 7.1.7.5)
  3965. */
  3966. if (!hdev->parent) {
  3967. delay = HUB_ROOT_RESET_TIME;
  3968. if (port1 == hdev->bus->otg_port)
  3969. hdev->bus->b_hnp_enable = 0;
  3970. }
  3971. /* Some low speed devices have problems with the quick delay, so */
  3972. /* be a bit pessimistic with those devices. RHbug #23670 */
  3973. if (oldspeed == USB_SPEED_LOW)
  3974. delay = HUB_LONG_RESET_TIME;
  3975. mutex_lock(hcd->address0_mutex);
  3976. /* Reset the device; full speed may morph to high speed */
  3977. /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */
  3978. retval = hub_port_reset(hub, port1, udev, delay, false);
  3979. if (retval < 0) /* error or disconnect */
  3980. goto fail;
  3981. /* success, speed is known */
  3982. retval = -ENODEV;
  3983. /* Don't allow speed changes at reset, except usb 3.0 to faster */
  3984. if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed &&
  3985. !(oldspeed == USB_SPEED_SUPER && udev->speed > oldspeed)) {
  3986. dev_dbg(&udev->dev, "device reset changed speed!\n");
  3987. goto fail;
  3988. }
  3989. oldspeed = udev->speed;
  3990. /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
  3991. * it's fixed size except for full speed devices.
  3992. * For Wireless USB devices, ep0 max packet is always 512 (tho
  3993. * reported as 0xff in the device descriptor). WUSB1.0[4.8.1].
  3994. */
  3995. switch (udev->speed) {
  3996. case USB_SPEED_SUPER_PLUS:
  3997. case USB_SPEED_SUPER:
  3998. case USB_SPEED_WIRELESS: /* fixed at 512 */
  3999. udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
  4000. break;
  4001. case USB_SPEED_HIGH: /* fixed at 64 */
  4002. udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
  4003. break;
  4004. case USB_SPEED_FULL: /* 8, 16, 32, or 64 */
  4005. /* to determine the ep0 maxpacket size, try to read
  4006. * the device descriptor to get bMaxPacketSize0 and
  4007. * then correct our initial guess.
  4008. */
  4009. udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
  4010. break;
  4011. case USB_SPEED_LOW: /* fixed at 8 */
  4012. udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8);
  4013. break;
  4014. default:
  4015. goto fail;
  4016. }
  4017. if (udev->speed == USB_SPEED_WIRELESS)
  4018. speed = "variable speed Wireless";
  4019. else
  4020. speed = usb_speed_string(udev->speed);
  4021. /*
  4022. * The controller driver may be NULL if the controller device
  4023. * is the middle device between platform device and roothub.
  4024. * This middle device may not need a device driver due to
  4025. * all hardware control can be at platform device driver, this
  4026. * platform device is usually a dual-role USB controller device.
  4027. */
  4028. if (udev->bus->controller->driver)
  4029. driver_name = udev->bus->controller->driver->name;
  4030. else
  4031. driver_name = udev->bus->sysdev->driver->name;
  4032. if (udev->speed < USB_SPEED_SUPER)
  4033. dev_info(&udev->dev,
  4034. "%s %s USB device number %d using %s\n",
  4035. (udev->config) ? "reset" : "new", speed,
  4036. devnum, driver_name);
  4037. /* Set up TT records, if needed */
  4038. if (hdev->tt) {
  4039. udev->tt = hdev->tt;
  4040. udev->ttport = hdev->ttport;
  4041. } else if (udev->speed != USB_SPEED_HIGH
  4042. && hdev->speed == USB_SPEED_HIGH) {
  4043. if (!hub->tt.hub) {
  4044. dev_err(&udev->dev, "parent hub has no TT\n");
  4045. retval = -EINVAL;
  4046. goto fail;
  4047. }
  4048. udev->tt = &hub->tt;
  4049. udev->ttport = port1;
  4050. }
  4051. /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way?
  4052. * Because device hardware and firmware is sometimes buggy in
  4053. * this area, and this is how Linux has done it for ages.
  4054. * Change it cautiously.
  4055. *
  4056. * NOTE: If use_new_scheme() is true we will start by issuing
  4057. * a 64-byte GET_DESCRIPTOR request. This is what Windows does,
  4058. * so it may help with some non-standards-compliant devices.
  4059. * Otherwise we start with SET_ADDRESS and then try to read the
  4060. * first 8 bytes of the device descriptor to get the ep0 maxpacket
  4061. * value.
  4062. */
  4063. for (retries = 0; retries < GET_DESCRIPTOR_TRIES; (++retries, msleep(100))) {
  4064. bool did_new_scheme = false;
  4065. if (use_new_scheme(udev, retry_counter, port_dev)) {
  4066. struct usb_device_descriptor *buf;
  4067. int r = 0;
  4068. did_new_scheme = true;
  4069. retval = hub_enable_device(udev);
  4070. if (retval < 0) {
  4071. dev_err(&udev->dev,
  4072. "hub failed to enable device, error %d\n",
  4073. retval);
  4074. goto fail;
  4075. }
  4076. #define GET_DESCRIPTOR_BUFSIZE 64
  4077. buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO);
  4078. if (!buf) {
  4079. retval = -ENOMEM;
  4080. continue;
  4081. }
  4082. /* Retry on all errors; some devices are flakey.
  4083. * 255 is for WUSB devices, we actually need to use
  4084. * 512 (WUSB1.0[4.8.1]).
  4085. */
  4086. for (operations = 0; operations < 3; ++operations) {
  4087. buf->bMaxPacketSize0 = 0;
  4088. r = usb_control_msg(udev, usb_rcvaddr0pipe(),
  4089. USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
  4090. USB_DT_DEVICE << 8, 0,
  4091. buf, GET_DESCRIPTOR_BUFSIZE,
  4092. initial_descriptor_timeout);
  4093. switch (buf->bMaxPacketSize0) {
  4094. case 8: case 16: case 32: case 64: case 255:
  4095. if (buf->bDescriptorType ==
  4096. USB_DT_DEVICE) {
  4097. r = 0;
  4098. break;
  4099. }
  4100. /* FALL THROUGH */
  4101. default:
  4102. if (r == 0)
  4103. r = -EPROTO;
  4104. break;
  4105. }
  4106. /*
  4107. * Some devices time out if they are powered on
  4108. * when already connected. They need a second
  4109. * reset. But only on the first attempt,
  4110. * lest we get into a time out/reset loop
  4111. */
  4112. if (r == 0 || (r == -ETIMEDOUT &&
  4113. retries == 0 &&
  4114. udev->speed > USB_SPEED_FULL))
  4115. break;
  4116. }
  4117. udev->descriptor.bMaxPacketSize0 =
  4118. buf->bMaxPacketSize0;
  4119. kfree(buf);
  4120. #ifndef CONFIG_USB_CONFIGFS_F_APPLE_PTP_SIM
  4121. retval = hub_port_reset(hub, port1, udev, delay, false);
  4122. dev_info(&udev->dev, "%s:%d speed=%d\n", __func__, __LINE__,udev->speed);
  4123. if (retval < 0) /* error or disconnect */
  4124. goto fail;
  4125. if (oldspeed != udev->speed) {
  4126. dev_dbg(&udev->dev,
  4127. "device reset changed speed!\n");
  4128. retval = -ENODEV;
  4129. goto fail;
  4130. }
  4131. #else
  4132. int i;
  4133. for(i = 0; i < 5; i++) {
  4134. retval = hub_port_reset(hub, port1, udev, delay, false);
  4135. dev_info(&udev->dev, "%s:%d speed=%d \n", __func__, __LINE__, udev->speed);
  4136. if (retval < 0) /* error or disconnect */
  4137. goto fail;
  4138. if (oldspeed == udev->speed){
  4139. dev_info(&udev->dev, "%s:%d speed=%d\n", __func__, __LINE__, udev->speed);
  4140. break;
  4141. }
  4142. }
  4143. if (oldspeed != udev->speed) {
  4144. dev_dbg(&udev->dev, "device reset changed speed!\n");
  4145. dev_info(&udev->dev, "%s:%d speed=%d\n", __func__, __LINE__,udev->speed);
  4146. retval = -ENODEV;
  4147. goto fail;
  4148. }
  4149. #endif
  4150. if (r) {
  4151. if (r != -ENODEV)
  4152. dev_err(&udev->dev, "device descriptor read/64, error %d\n",
  4153. r);
  4154. retval = -EMSGSIZE;
  4155. continue;
  4156. }
  4157. #undef GET_DESCRIPTOR_BUFSIZE
  4158. }
  4159. /*
  4160. * If device is WUSB, we already assigned an
  4161. * unauthorized address in the Connect Ack sequence;
  4162. * authorization will assign the final address.
  4163. */
  4164. if (udev->wusb == 0) {
  4165. for (operations = 0; operations < SET_ADDRESS_TRIES; ++operations) {
  4166. retval = hub_set_address(udev, devnum);
  4167. if (retval >= 0)
  4168. break;
  4169. msleep(200);
  4170. }
  4171. if (retval < 0) {
  4172. if (retval != -ENODEV)
  4173. dev_err(&udev->dev, "device not accepting address %d, error %d\n",
  4174. devnum, retval);
  4175. goto fail;
  4176. }
  4177. if (udev->speed >= USB_SPEED_SUPER) {
  4178. devnum = udev->devnum;
  4179. dev_info(&udev->dev,
  4180. "%s SuperSpeed%s%s USB device number %d using %s\n",
  4181. (udev->config) ? "reset" : "new",
  4182. (udev->speed == USB_SPEED_SUPER_PLUS) ?
  4183. "Plus Gen 2" : " Gen 1",
  4184. (udev->rx_lanes == 2 && udev->tx_lanes == 2) ?
  4185. "x2" : "",
  4186. devnum, driver_name);
  4187. }
  4188. /* cope with hardware quirkiness:
  4189. * - let SET_ADDRESS settle, some device hardware wants it
  4190. * - read ep0 maxpacket even for high and low speed,
  4191. */
  4192. msleep(10);
  4193. /* use_new_scheme() checks the speed which may have
  4194. * changed since the initial look so we cache the result
  4195. * in did_new_scheme
  4196. */
  4197. if (did_new_scheme)
  4198. break;
  4199. }
  4200. retval = usb_get_device_descriptor(udev, 8);
  4201. if (retval < 8) {
  4202. if (retval != -ENODEV)
  4203. dev_err(&udev->dev,
  4204. "device descriptor read/8, error %d\n",
  4205. retval);
  4206. if (retval >= 0)
  4207. retval = -EMSGSIZE;
  4208. } else {
  4209. u32 delay;
  4210. retval = 0;
  4211. delay = udev->parent->hub_delay;
  4212. udev->hub_delay = min_t(u32, delay,
  4213. USB_TP_TRANSMISSION_DELAY_MAX);
  4214. retval = usb_set_isoch_delay(udev);
  4215. if (retval) {
  4216. dev_dbg(&udev->dev,
  4217. "Failed set isoch delay, error %d\n",
  4218. retval);
  4219. retval = 0;
  4220. }
  4221. break;
  4222. }
  4223. }
  4224. if (retval)
  4225. goto fail;
  4226. /*
  4227. * Some superspeed devices have finished the link training process
  4228. * and attached to a superspeed hub port, but the device descriptor
  4229. * got from those devices show they aren't superspeed devices. Warm
  4230. * reset the port attached by the devices can fix them.
  4231. */
  4232. if ((udev->speed >= USB_SPEED_SUPER) &&
  4233. (le16_to_cpu(udev->descriptor.bcdUSB) < 0x0300)) {
  4234. dev_err(&udev->dev, "got a wrong device descriptor, "
  4235. "warm reset device\n");
  4236. hub_port_reset(hub, port1, udev,
  4237. HUB_BH_RESET_TIME, true);
  4238. retval = -EINVAL;
  4239. goto fail;
  4240. }
  4241. if (udev->descriptor.bMaxPacketSize0 == 0xff ||
  4242. udev->speed >= USB_SPEED_SUPER)
  4243. i = 512;
  4244. else
  4245. i = udev->descriptor.bMaxPacketSize0;
  4246. if (usb_endpoint_maxp(&udev->ep0.desc) != i) {
  4247. if (udev->speed == USB_SPEED_LOW ||
  4248. !(i == 8 || i == 16 || i == 32 || i == 64)) {
  4249. dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", i);
  4250. retval = -EMSGSIZE;
  4251. goto fail;
  4252. }
  4253. if (udev->speed == USB_SPEED_FULL)
  4254. dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i);
  4255. else
  4256. dev_warn(&udev->dev, "Using ep0 maxpacket: %d\n", i);
  4257. udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i);
  4258. usb_ep0_reinit(udev);
  4259. }
  4260. retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE);
  4261. if (retval < (signed)sizeof(udev->descriptor)) {
  4262. if (retval != -ENODEV)
  4263. dev_err(&udev->dev, "device descriptor read/all, error %d\n",
  4264. retval);
  4265. if (retval >= 0)
  4266. retval = -ENOMSG;
  4267. goto fail;
  4268. }
  4269. usb_detect_quirks(udev);
  4270. if (udev->wusb == 0 && le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0201) {
  4271. retval = usb_get_bos_descriptor(udev);
  4272. if (!retval) {
  4273. #ifndef CONFIG_USB_CONFIGFS_F_APPLE_PTP_SIM
  4274. udev->lpm_capable = usb_device_supports_lpm(udev);
  4275. usb_set_lpm_parameters(udev);
  4276. #endif
  4277. }
  4278. }
  4279. retval = 0;
  4280. /* notify HCD that we have a device connected and addressed */
  4281. if (hcd->driver->update_device)
  4282. hcd->driver->update_device(hcd, udev);
  4283. hub_set_initial_usb2_lpm_policy(udev);
  4284. fail:
  4285. if (retval) {
  4286. hub_port_disable(hub, port1, 0);
  4287. update_devnum(udev, devnum); /* for disconnect processing */
  4288. }
  4289. mutex_unlock(hcd->address0_mutex);
  4290. return retval;
  4291. }
  4292. static void
  4293. check_highspeed(struct usb_hub *hub, struct usb_device *udev, int port1)
  4294. {
  4295. struct usb_qualifier_descriptor *qual;
  4296. int status;
  4297. if (udev->quirks & USB_QUIRK_DEVICE_QUALIFIER)
  4298. return;
  4299. qual = kmalloc(sizeof *qual, GFP_KERNEL);
  4300. if (qual == NULL)
  4301. return;
  4302. status = usb_get_descriptor(udev, USB_DT_DEVICE_QUALIFIER, 0,
  4303. qual, sizeof *qual);
  4304. if (status == sizeof *qual) {
  4305. dev_info(&udev->dev, "not running at top speed; "
  4306. "connect to a high speed hub\n");
  4307. /* hub LEDs are probably harder to miss than syslog */
  4308. if (hub->has_indicators) {
  4309. hub->indicator[port1-1] = INDICATOR_GREEN_BLINK;
  4310. queue_delayed_work(system_power_efficient_wq,
  4311. &hub->leds, 0);
  4312. }
  4313. }
  4314. kfree(qual);
  4315. }
  4316. static unsigned
  4317. hub_power_remaining(struct usb_hub *hub)
  4318. {
  4319. struct usb_device *hdev = hub->hdev;
  4320. int remaining;
  4321. int port1;
  4322. if (!hub->limited_power)
  4323. return 0;
  4324. remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent;
  4325. for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
  4326. struct usb_port *port_dev = hub->ports[port1 - 1];
  4327. struct usb_device *udev = port_dev->child;
  4328. unsigned unit_load;
  4329. int delta;
  4330. if (!udev)
  4331. continue;
  4332. if (hub_is_superspeed(udev))
  4333. unit_load = 150;
  4334. else
  4335. unit_load = 100;
  4336. /*
  4337. * Unconfigured devices may not use more than one unit load,
  4338. * or 8mA for OTG ports
  4339. */
  4340. if (udev->actconfig)
  4341. delta = usb_get_max_power(udev, udev->actconfig);
  4342. else if (port1 != udev->bus->otg_port || hdev->parent)
  4343. delta = unit_load;
  4344. else
  4345. delta = 8;
  4346. if (delta > hub->mA_per_port)
  4347. dev_warn(&port_dev->dev, "%dmA is over %umA budget!\n",
  4348. delta, hub->mA_per_port);
  4349. remaining -= delta;
  4350. }
  4351. if (remaining < 0) {
  4352. dev_warn(hub->intfdev, "%dmA over power budget!\n",
  4353. -remaining);
  4354. remaining = 0;
  4355. }
  4356. return remaining;
  4357. }
  4358. static void hub_port_connect(struct usb_hub *hub, int port1, u16 portstatus,
  4359. u16 portchange)
  4360. {
  4361. int status = -ENODEV;
  4362. int i;
  4363. unsigned unit_load;
  4364. struct usb_device *hdev = hub->hdev;
  4365. struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
  4366. struct usb_port *port_dev = hub->ports[port1 - 1];
  4367. struct usb_device *udev = port_dev->child;
  4368. static int unreliable_port = -1;
  4369. /* Disconnect any existing devices under this port */
  4370. if (udev) {
  4371. if (hcd->usb_phy && !hdev->parent)
  4372. usb_phy_notify_disconnect(hcd->usb_phy, udev->speed);
  4373. usb_disconnect(&port_dev->child);
  4374. }
  4375. /* We can forget about a "removed" device when there's a physical
  4376. * disconnect or the connect status changes.
  4377. */
  4378. if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
  4379. (portchange & USB_PORT_STAT_C_CONNECTION))
  4380. clear_bit(port1, hub->removed_bits);
  4381. #ifndef CONFIG_USB_CONFIGFS_F_APPLE_PTP_SIM
  4382. if (portchange & (USB_PORT_STAT_C_CONNECTION |
  4383. USB_PORT_STAT_C_ENABLE)) {
  4384. status = hub_port_debounce_be_stable(hub, port1);
  4385. if (status < 0) {
  4386. if (status != -ENODEV &&
  4387. port1 != unreliable_port &&
  4388. printk_ratelimit())
  4389. dev_err(&port_dev->dev, "connect-debounce failed\n");
  4390. portstatus &= ~USB_PORT_STAT_CONNECTION;
  4391. unreliable_port = port1;
  4392. } else {
  4393. portstatus = status;
  4394. }
  4395. }
  4396. #endif
  4397. /* Return now if debouncing failed or nothing is connected or
  4398. * the device was "removed".
  4399. */
  4400. if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
  4401. test_bit(port1, hub->removed_bits)) {
  4402. /*
  4403. * maybe switch power back on (e.g. root hub was reset)
  4404. * but only if the port isn't owned by someone else.
  4405. */
  4406. if (hub_is_port_power_switchable(hub)
  4407. && !port_is_power_on(hub, portstatus)
  4408. && !port_dev->port_owner)
  4409. set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
  4410. if (portstatus & USB_PORT_STAT_ENABLE)
  4411. goto done;
  4412. return;
  4413. }
  4414. if (hub_is_superspeed(hub->hdev))
  4415. unit_load = 150;
  4416. else
  4417. unit_load = 100;
  4418. status = 0;
  4419. for (i = 0; i < SET_CONFIG_TRIES; i++) {
  4420. /* reallocate for each attempt, since references
  4421. * to the previous one can escape in various ways
  4422. */
  4423. udev = usb_alloc_dev(hdev, hdev->bus, port1);
  4424. if (!udev) {
  4425. dev_err(&port_dev->dev,
  4426. "couldn't allocate usb_device\n");
  4427. goto done;
  4428. }
  4429. usb_set_device_state(udev, USB_STATE_POWERED);
  4430. udev->bus_mA = hub->mA_per_port;
  4431. udev->level = hdev->level + 1;
  4432. udev->wusb = hub_is_wusb(hub);
  4433. /* Devices connected to SuperSpeed hubs are USB 3.0 or later */
  4434. if (hub_is_superspeed(hub->hdev))
  4435. udev->speed = USB_SPEED_SUPER;
  4436. else
  4437. udev->speed = USB_SPEED_UNKNOWN;
  4438. choose_devnum(udev);
  4439. if (udev->devnum <= 0) {
  4440. status = -ENOTCONN; /* Don't retry */
  4441. goto loop;
  4442. }
  4443. /* reset (non-USB 3.0 devices) and get descriptor */
  4444. usb_lock_port(port_dev);
  4445. status = hub_port_init(hub, udev, port1, i);
  4446. usb_unlock_port(port_dev);
  4447. if (status < 0)
  4448. goto loop;
  4449. if (udev->quirks & USB_QUIRK_DELAY_INIT)
  4450. msleep(2000);
  4451. /* consecutive bus-powered hubs aren't reliable; they can
  4452. * violate the voltage drop budget. if the new child has
  4453. * a "powered" LED, users should notice we didn't enable it
  4454. * (without reading syslog), even without per-port LEDs
  4455. * on the parent.
  4456. */
  4457. if (udev->descriptor.bDeviceClass == USB_CLASS_HUB
  4458. && udev->bus_mA <= unit_load) {
  4459. u16 devstat;
  4460. status = usb_get_std_status(udev, USB_RECIP_DEVICE, 0,
  4461. &devstat);
  4462. if (status) {
  4463. dev_dbg(&udev->dev, "get status %d ?\n", status);
  4464. goto loop_disable;
  4465. }
  4466. if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
  4467. dev_err(&udev->dev,
  4468. "can't connect bus-powered hub "
  4469. "to this port\n");
  4470. if (hub->has_indicators) {
  4471. hub->indicator[port1-1] =
  4472. INDICATOR_AMBER_BLINK;
  4473. queue_delayed_work(
  4474. system_power_efficient_wq,
  4475. &hub->leds, 0);
  4476. }
  4477. status = -ENOTCONN; /* Don't retry */
  4478. goto loop_disable;
  4479. }
  4480. }
  4481. /* check for devices running slower than they could */
  4482. if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200
  4483. && udev->speed == USB_SPEED_FULL
  4484. && highspeed_hubs != 0)
  4485. check_highspeed(hub, udev, port1);
  4486. /* Store the parent's children[] pointer. At this point
  4487. * udev becomes globally accessible, although presumably
  4488. * no one will look at it until hdev is unlocked.
  4489. */
  4490. status = 0;
  4491. mutex_lock(&usb_port_peer_mutex);
  4492. /* We mustn't add new devices if the parent hub has
  4493. * been disconnected; we would race with the
  4494. * recursively_mark_NOTATTACHED() routine.
  4495. */
  4496. spin_lock_irq(&device_state_lock);
  4497. if (hdev->state == USB_STATE_NOTATTACHED)
  4498. status = -ENOTCONN;
  4499. else
  4500. port_dev->child = udev;
  4501. spin_unlock_irq(&device_state_lock);
  4502. mutex_unlock(&usb_port_peer_mutex);
  4503. /* Run it through the hoops (find a driver, etc) */
  4504. if (!status) {
  4505. status = usb_new_device(udev);
  4506. if (status) {
  4507. mutex_lock(&usb_port_peer_mutex);
  4508. spin_lock_irq(&device_state_lock);
  4509. port_dev->child = NULL;
  4510. spin_unlock_irq(&device_state_lock);
  4511. mutex_unlock(&usb_port_peer_mutex);
  4512. } else {
  4513. if (hcd->usb_phy && !hdev->parent)
  4514. usb_phy_notify_connect(hcd->usb_phy,
  4515. udev->speed);
  4516. }
  4517. }
  4518. if (status)
  4519. goto loop_disable;
  4520. status = hub_power_remaining(hub);
  4521. if (status)
  4522. dev_dbg(hub->intfdev, "%dmA power budget left\n", status);
  4523. return;
  4524. loop_disable:
  4525. hub_port_disable(hub, port1, 1);
  4526. loop:
  4527. usb_ep0_reinit(udev);
  4528. release_devnum(udev);
  4529. hub_free_dev(udev);
  4530. usb_put_dev(udev);
  4531. if ((status == -ENOTCONN) || (status == -ENOTSUPP))
  4532. break;
  4533. /* When halfway through our retry count, power-cycle the port */
  4534. if (i == (SET_CONFIG_TRIES / 2) - 1) {
  4535. dev_info(&port_dev->dev, "attempt power cycle\n");
  4536. usb_hub_set_port_power(hdev, hub, port1, false);
  4537. msleep(2 * hub_power_on_good_delay(hub));
  4538. usb_hub_set_port_power(hdev, hub, port1, true);
  4539. msleep(hub_power_on_good_delay(hub));
  4540. }
  4541. }
  4542. if (hub->hdev->parent ||
  4543. !hcd->driver->port_handed_over ||
  4544. !(hcd->driver->port_handed_over)(hcd, port1)) {
  4545. if (status != -ENOTCONN && status != -ENODEV)
  4546. dev_err(&port_dev->dev,
  4547. "unable to enumerate USB device\n");
  4548. }
  4549. done:
  4550. hub_port_disable(hub, port1, 1);
  4551. if (hcd->driver->relinquish_port && !hub->hdev->parent) {
  4552. if (status != -ENOTCONN && status != -ENODEV)
  4553. hcd->driver->relinquish_port(hcd, port1);
  4554. }
  4555. }
  4556. /* Handle physical or logical connection change events.
  4557. * This routine is called when:
  4558. * a port connection-change occurs;
  4559. * a port enable-change occurs (often caused by EMI);
  4560. * usb_reset_and_verify_device() encounters changed descriptors (as from
  4561. * a firmware download)
  4562. * caller already locked the hub
  4563. */
  4564. static void hub_port_connect_change(struct usb_hub *hub, int port1,
  4565. u16 portstatus, u16 portchange)
  4566. __must_hold(&port_dev->status_lock)
  4567. {
  4568. struct usb_port *port_dev = hub->ports[port1 - 1];
  4569. struct usb_device *udev = port_dev->child;
  4570. int status = -ENODEV;
  4571. dev_dbg(&port_dev->dev, "status %04x, change %04x, %s\n", portstatus,
  4572. portchange, portspeed(hub, portstatus));
  4573. if (hub->has_indicators) {
  4574. set_port_led(hub, port1, HUB_LED_AUTO);
  4575. hub->indicator[port1-1] = INDICATOR_AUTO;
  4576. }
  4577. #ifdef CONFIG_USB_OTG
  4578. /* during HNP, don't repeat the debounce */
  4579. if (hub->hdev->bus->is_b_host)
  4580. portchange &= ~(USB_PORT_STAT_C_CONNECTION |
  4581. USB_PORT_STAT_C_ENABLE);
  4582. #endif
  4583. /* Try to resuscitate an existing device */
  4584. if ((portstatus & USB_PORT_STAT_CONNECTION) && udev &&
  4585. udev->state != USB_STATE_NOTATTACHED) {
  4586. if (portstatus & USB_PORT_STAT_ENABLE) {
  4587. status = 0; /* Nothing to do */
  4588. #ifdef CONFIG_PM
  4589. } else if (udev->state == USB_STATE_SUSPENDED &&
  4590. udev->persist_enabled) {
  4591. /* For a suspended device, treat this as a
  4592. * remote wakeup event.
  4593. */
  4594. usb_unlock_port(port_dev);
  4595. status = usb_remote_wakeup(udev);
  4596. usb_lock_port(port_dev);
  4597. #endif
  4598. } else {
  4599. /* Don't resuscitate */;
  4600. }
  4601. }
  4602. clear_bit(port1, hub->change_bits);
  4603. /* successfully revalidated the connection */
  4604. if (status == 0)
  4605. return;
  4606. usb_unlock_port(port_dev);
  4607. hub_port_connect(hub, port1, portstatus, portchange);
  4608. usb_lock_port(port_dev);
  4609. }
  4610. static void port_event(struct usb_hub *hub, int port1)
  4611. __must_hold(&port_dev->status_lock)
  4612. {
  4613. int connect_change;
  4614. struct usb_port *port_dev = hub->ports[port1 - 1];
  4615. struct usb_device *udev = port_dev->child;
  4616. struct usb_device *hdev = hub->hdev;
  4617. u16 portstatus, portchange;
  4618. connect_change = test_bit(port1, hub->change_bits);
  4619. clear_bit(port1, hub->event_bits);
  4620. clear_bit(port1, hub->wakeup_bits);
  4621. if (hub_port_status(hub, port1, &portstatus, &portchange) < 0)
  4622. return;
  4623. if (portchange & USB_PORT_STAT_C_CONNECTION) {
  4624. usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
  4625. connect_change = 1;
  4626. }
  4627. if (portchange & USB_PORT_STAT_C_ENABLE) {
  4628. if (!connect_change)
  4629. dev_dbg(&port_dev->dev, "enable change, status %08x\n",
  4630. portstatus);
  4631. usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
  4632. /*
  4633. * EM interference sometimes causes badly shielded USB devices
  4634. * to be shutdown by the hub, this hack enables them again.
  4635. * Works at least with mouse driver.
  4636. */
  4637. if (!(portstatus & USB_PORT_STAT_ENABLE)
  4638. && !connect_change && udev) {
  4639. dev_err(&port_dev->dev, "disabled by hub (EMI?), re-enabling...\n");
  4640. connect_change = 1;
  4641. }
  4642. }
  4643. if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
  4644. u16 status = 0, unused;
  4645. port_dev->over_current_count++;
  4646. dev_dbg(&port_dev->dev, "over-current change #%u\n",
  4647. port_dev->over_current_count);
  4648. usb_clear_port_feature(hdev, port1,
  4649. USB_PORT_FEAT_C_OVER_CURRENT);
  4650. msleep(100); /* Cool down */
  4651. hub_power_on(hub, true);
  4652. hub_port_status(hub, port1, &status, &unused);
  4653. if (status & USB_PORT_STAT_OVERCURRENT)
  4654. dev_err(&port_dev->dev, "over-current condition\n");
  4655. }
  4656. if (portchange & USB_PORT_STAT_C_RESET) {
  4657. dev_dbg(&port_dev->dev, "reset change\n");
  4658. usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_RESET);
  4659. }
  4660. if ((portchange & USB_PORT_STAT_C_BH_RESET)
  4661. && hub_is_superspeed(hdev)) {
  4662. dev_dbg(&port_dev->dev, "warm reset change\n");
  4663. usb_clear_port_feature(hdev, port1,
  4664. USB_PORT_FEAT_C_BH_PORT_RESET);
  4665. }
  4666. if (portchange & USB_PORT_STAT_C_LINK_STATE) {
  4667. dev_dbg(&port_dev->dev, "link state change\n");
  4668. usb_clear_port_feature(hdev, port1,
  4669. USB_PORT_FEAT_C_PORT_LINK_STATE);
  4670. }
  4671. if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) {
  4672. dev_warn(&port_dev->dev, "config error\n");
  4673. usb_clear_port_feature(hdev, port1,
  4674. USB_PORT_FEAT_C_PORT_CONFIG_ERROR);
  4675. }
  4676. /* skip port actions that require the port to be powered on */
  4677. if (!pm_runtime_active(&port_dev->dev))
  4678. return;
  4679. if (hub_handle_remote_wakeup(hub, port1, portstatus, portchange))
  4680. connect_change = 1;
  4681. /*
  4682. * Warm reset a USB3 protocol port if it's in
  4683. * SS.Inactive state.
  4684. */
  4685. if (hub_port_warm_reset_required(hub, port1, portstatus)) {
  4686. dev_dbg(&port_dev->dev, "do warm reset\n");
  4687. if (!udev || !(portstatus & USB_PORT_STAT_CONNECTION)
  4688. || udev->state == USB_STATE_NOTATTACHED) {
  4689. if (hub_port_reset(hub, port1, NULL,
  4690. HUB_BH_RESET_TIME, true) < 0)
  4691. hub_port_disable(hub, port1, 1);
  4692. } else {
  4693. usb_unlock_port(port_dev);
  4694. usb_lock_device(udev);
  4695. usb_reset_device(udev);
  4696. usb_unlock_device(udev);
  4697. usb_lock_port(port_dev);
  4698. connect_change = 0;
  4699. }
  4700. }
  4701. if (connect_change)
  4702. hub_port_connect_change(hub, port1, portstatus, portchange);
  4703. }
  4704. static void hub_event(struct work_struct *work)
  4705. {
  4706. struct usb_device *hdev;
  4707. struct usb_interface *intf;
  4708. struct usb_hub *hub;
  4709. struct device *hub_dev;
  4710. u16 hubstatus;
  4711. u16 hubchange;
  4712. int i, ret;
  4713. hub = container_of(work, struct usb_hub, events);
  4714. hdev = hub->hdev;
  4715. hub_dev = hub->intfdev;
  4716. intf = to_usb_interface(hub_dev);
  4717. dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n",
  4718. hdev->state, hdev->maxchild,
  4719. /* NOTE: expects max 15 ports... */
  4720. (u16) hub->change_bits[0],
  4721. (u16) hub->event_bits[0]);
  4722. /* Lock the device, then check to see if we were
  4723. * disconnected while waiting for the lock to succeed. */
  4724. usb_lock_device(hdev);
  4725. if (unlikely(hub->disconnected))
  4726. goto out_hdev_lock;
  4727. /* If the hub has died, clean up after it */
  4728. if (hdev->state == USB_STATE_NOTATTACHED) {
  4729. hub->error = -ENODEV;
  4730. hub_quiesce(hub, HUB_DISCONNECT);
  4731. goto out_hdev_lock;
  4732. }
  4733. /* Autoresume */
  4734. ret = usb_autopm_get_interface(intf);
  4735. if (ret) {
  4736. dev_dbg(hub_dev, "Can't autoresume: %d\n", ret);
  4737. goto out_hdev_lock;
  4738. }
  4739. /* If this is an inactive hub, do nothing */
  4740. if (hub->quiescing)
  4741. goto out_autopm;
  4742. if (hub->error) {
  4743. dev_dbg(hub_dev, "resetting for error %d\n", hub->error);
  4744. ret = usb_reset_device(hdev);
  4745. if (ret) {
  4746. dev_dbg(hub_dev, "error resetting hub: %d\n", ret);
  4747. goto out_autopm;
  4748. }
  4749. hub->nerrors = 0;
  4750. hub->error = 0;
  4751. }
  4752. /* deal with port status changes */
  4753. for (i = 1; i <= hdev->maxchild; i++) {
  4754. struct usb_port *port_dev = hub->ports[i - 1];
  4755. if (test_bit(i, hub->event_bits)
  4756. || test_bit(i, hub->change_bits)
  4757. || test_bit(i, hub->wakeup_bits)) {
  4758. /*
  4759. * The get_noresume and barrier ensure that if
  4760. * the port was in the process of resuming, we
  4761. * flush that work and keep the port active for
  4762. * the duration of the port_event(). However,
  4763. * if the port is runtime pm suspended
  4764. * (powered-off), we leave it in that state, run
  4765. * an abbreviated port_event(), and move on.
  4766. */
  4767. pm_runtime_get_noresume(&port_dev->dev);
  4768. pm_runtime_barrier(&port_dev->dev);
  4769. usb_lock_port(port_dev);
  4770. port_event(hub, i);
  4771. usb_unlock_port(port_dev);
  4772. pm_runtime_put_sync(&port_dev->dev);
  4773. }
  4774. }
  4775. /* deal with hub status changes */
  4776. if (test_and_clear_bit(0, hub->event_bits) == 0)
  4777. ; /* do nothing */
  4778. else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0)
  4779. dev_err(hub_dev, "get_hub_status failed\n");
  4780. else {
  4781. if (hubchange & HUB_CHANGE_LOCAL_POWER) {
  4782. dev_dbg(hub_dev, "power change\n");
  4783. clear_hub_feature(hdev, C_HUB_LOCAL_POWER);
  4784. if (hubstatus & HUB_STATUS_LOCAL_POWER)
  4785. /* FIXME: Is this always true? */
  4786. hub->limited_power = 1;
  4787. else
  4788. hub->limited_power = 0;
  4789. }
  4790. if (hubchange & HUB_CHANGE_OVERCURRENT) {
  4791. u16 status = 0;
  4792. u16 unused;
  4793. dev_dbg(hub_dev, "over-current change\n");
  4794. clear_hub_feature(hdev, C_HUB_OVER_CURRENT);
  4795. msleep(500); /* Cool down */
  4796. hub_power_on(hub, true);
  4797. hub_hub_status(hub, &status, &unused);
  4798. if (status & HUB_STATUS_OVERCURRENT)
  4799. dev_err(hub_dev, "over-current condition\n");
  4800. }
  4801. }
  4802. out_autopm:
  4803. /* Balance the usb_autopm_get_interface() above */
  4804. usb_autopm_put_interface_no_suspend(intf);
  4805. out_hdev_lock:
  4806. usb_unlock_device(hdev);
  4807. /* Balance the stuff in kick_hub_wq() and allow autosuspend */
  4808. usb_autopm_put_interface(intf);
  4809. kref_put(&hub->kref, hub_release);
  4810. }
  4811. static const struct usb_device_id hub_id_table[] = {
  4812. { .match_flags = USB_DEVICE_ID_MATCH_VENDOR
  4813. | USB_DEVICE_ID_MATCH_PRODUCT
  4814. | USB_DEVICE_ID_MATCH_INT_CLASS,
  4815. .idVendor = USB_VENDOR_SMSC,
  4816. .idProduct = USB_PRODUCT_USB5534B,
  4817. .bInterfaceClass = USB_CLASS_HUB,
  4818. .driver_info = HUB_QUIRK_DISABLE_AUTOSUSPEND},
  4819. { .match_flags = USB_DEVICE_ID_MATCH_VENDOR
  4820. | USB_DEVICE_ID_MATCH_INT_CLASS,
  4821. .idVendor = USB_VENDOR_GENESYS_LOGIC,
  4822. .bInterfaceClass = USB_CLASS_HUB,
  4823. .driver_info = HUB_QUIRK_CHECK_PORT_AUTOSUSPEND},
  4824. { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
  4825. .bDeviceClass = USB_CLASS_HUB},
  4826. { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
  4827. .bInterfaceClass = USB_CLASS_HUB},
  4828. { } /* Terminating entry */
  4829. };
  4830. MODULE_DEVICE_TABLE(usb, hub_id_table);
  4831. static struct usb_driver hub_driver = {
  4832. .name = "hub",
  4833. .probe = hub_probe,
  4834. .disconnect = hub_disconnect,
  4835. .suspend = hub_suspend,
  4836. .resume = hub_resume,
  4837. .reset_resume = hub_reset_resume,
  4838. .pre_reset = hub_pre_reset,
  4839. .post_reset = hub_post_reset,
  4840. .unlocked_ioctl = hub_ioctl,
  4841. .id_table = hub_id_table,
  4842. .supports_autosuspend = 1,
  4843. };
  4844. int usb_hub_init(void)
  4845. {
  4846. if (usb_register(&hub_driver) < 0) {
  4847. printk(KERN_ERR "%s: can't register hub driver\n",
  4848. usbcore_name);
  4849. return -1;
  4850. }
  4851. /*
  4852. * The workqueue needs to be freezable to avoid interfering with
  4853. * USB-PERSIST port handover. Otherwise it might see that a full-speed
  4854. * device was gone before the EHCI controller had handed its port
  4855. * over to the companion full-speed controller.
  4856. */
  4857. hub_wq = alloc_workqueue("usb_hub_wq", WQ_FREEZABLE, 0);
  4858. if (hub_wq)
  4859. return 0;
  4860. /* Fall through if kernel_thread failed */
  4861. usb_deregister(&hub_driver);
  4862. pr_err("%s: can't allocate workqueue for usb hub\n", usbcore_name);
  4863. return -1;
  4864. }
  4865. void usb_hub_cleanup(void)
  4866. {
  4867. destroy_workqueue(hub_wq);
  4868. /*
  4869. * Hub resources are freed for us by usb_deregister. It calls
  4870. * usb_driver_purge on every device which in turn calls that
  4871. * devices disconnect function if it is using this driver.
  4872. * The hub_disconnect function takes care of releasing the
  4873. * individual hub resources. -greg
  4874. */
  4875. usb_deregister(&hub_driver);
  4876. } /* usb_hub_cleanup() */
  4877. static int descriptors_changed(struct usb_device *udev,
  4878. struct usb_device_descriptor *old_device_descriptor,
  4879. struct usb_host_bos *old_bos)
  4880. {
  4881. int changed = 0;
  4882. unsigned index;
  4883. unsigned serial_len = 0;
  4884. unsigned len;
  4885. unsigned old_length;
  4886. int length;
  4887. char *buf;
  4888. if (memcmp(&udev->descriptor, old_device_descriptor,
  4889. sizeof(*old_device_descriptor)) != 0)
  4890. return 1;
  4891. if ((old_bos && !udev->bos) || (!old_bos && udev->bos))
  4892. return 1;
  4893. if (udev->bos) {
  4894. len = le16_to_cpu(udev->bos->desc->wTotalLength);
  4895. if (len != le16_to_cpu(old_bos->desc->wTotalLength))
  4896. return 1;
  4897. if (memcmp(udev->bos->desc, old_bos->desc, len))
  4898. return 1;
  4899. }
  4900. /* Since the idVendor, idProduct, and bcdDevice values in the
  4901. * device descriptor haven't changed, we will assume the
  4902. * Manufacturer and Product strings haven't changed either.
  4903. * But the SerialNumber string could be different (e.g., a
  4904. * different flash card of the same brand).
  4905. */
  4906. if (udev->serial)
  4907. serial_len = strlen(udev->serial) + 1;
  4908. len = serial_len;
  4909. for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
  4910. old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
  4911. len = max(len, old_length);
  4912. }
  4913. buf = kmalloc(len, GFP_NOIO);
  4914. if (!buf)
  4915. /* assume the worst */
  4916. return 1;
  4917. for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
  4918. old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
  4919. length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf,
  4920. old_length);
  4921. if (length != old_length) {
  4922. dev_dbg(&udev->dev, "config index %d, error %d\n",
  4923. index, length);
  4924. changed = 1;
  4925. break;
  4926. }
  4927. if (memcmp(buf, udev->rawdescriptors[index], old_length)
  4928. != 0) {
  4929. dev_dbg(&udev->dev, "config index %d changed (#%d)\n",
  4930. index,
  4931. ((struct usb_config_descriptor *) buf)->
  4932. bConfigurationValue);
  4933. changed = 1;
  4934. break;
  4935. }
  4936. }
  4937. if (!changed && serial_len) {
  4938. length = usb_string(udev, udev->descriptor.iSerialNumber,
  4939. buf, serial_len);
  4940. if (length + 1 != serial_len) {
  4941. dev_dbg(&udev->dev, "serial string error %d\n",
  4942. length);
  4943. changed = 1;
  4944. } else if (memcmp(buf, udev->serial, length) != 0) {
  4945. dev_dbg(&udev->dev, "serial string changed\n");
  4946. changed = 1;
  4947. }
  4948. }
  4949. kfree(buf);
  4950. return changed;
  4951. }
  4952. /**
  4953. * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device
  4954. * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
  4955. *
  4956. * WARNING - don't use this routine to reset a composite device
  4957. * (one with multiple interfaces owned by separate drivers)!
  4958. * Use usb_reset_device() instead.
  4959. *
  4960. * Do a port reset, reassign the device's address, and establish its
  4961. * former operating configuration. If the reset fails, or the device's
  4962. * descriptors change from their values before the reset, or the original
  4963. * configuration and altsettings cannot be restored, a flag will be set
  4964. * telling hub_wq to pretend the device has been disconnected and then
  4965. * re-connected. All drivers will be unbound, and the device will be
  4966. * re-enumerated and probed all over again.
  4967. *
  4968. * Return: 0 if the reset succeeded, -ENODEV if the device has been
  4969. * flagged for logical disconnection, or some other negative error code
  4970. * if the reset wasn't even attempted.
  4971. *
  4972. * Note:
  4973. * The caller must own the device lock and the port lock, the latter is
  4974. * taken by usb_reset_device(). For example, it's safe to use
  4975. * usb_reset_device() from a driver probe() routine after downloading
  4976. * new firmware. For calls that might not occur during probe(), drivers
  4977. * should lock the device using usb_lock_device_for_reset().
  4978. *
  4979. * Locking exception: This routine may also be called from within an
  4980. * autoresume handler. Such usage won't conflict with other tasks
  4981. * holding the device lock because these tasks should always call
  4982. * usb_autopm_resume_device(), thereby preventing any unwanted
  4983. * autoresume. The autoresume handler is expected to have already
  4984. * acquired the port lock before calling this routine.
  4985. */
  4986. static int usb_reset_and_verify_device(struct usb_device *udev)
  4987. {
  4988. struct usb_device *parent_hdev = udev->parent;
  4989. struct usb_hub *parent_hub;
  4990. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  4991. struct usb_device_descriptor descriptor = udev->descriptor;
  4992. struct usb_host_bos *bos;
  4993. int i, j, ret = 0;
  4994. int port1 = udev->portnum;
  4995. if (udev->state == USB_STATE_NOTATTACHED ||
  4996. udev->state == USB_STATE_SUSPENDED) {
  4997. dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
  4998. udev->state);
  4999. return -EINVAL;
  5000. }
  5001. if (!parent_hdev)
  5002. return -EISDIR;
  5003. parent_hub = usb_hub_to_struct_hub(parent_hdev);
  5004. /* Disable USB2 hardware LPM.
  5005. * It will be re-enabled by the enumeration process.
  5006. */
  5007. usb_disable_usb2_hardware_lpm(udev);
  5008. /* Disable LPM while we reset the device and reinstall the alt settings.
  5009. * Device-initiated LPM, and system exit latency settings are cleared
  5010. * when the device is reset, so we have to set them up again.
  5011. */
  5012. ret = usb_unlocked_disable_lpm(udev);
  5013. if (ret) {
  5014. dev_err(&udev->dev, "%s Failed to disable LPM\n", __func__);
  5015. goto re_enumerate_no_bos;
  5016. }
  5017. bos = udev->bos;
  5018. udev->bos = NULL;
  5019. for (i = 0; i < SET_CONFIG_TRIES; ++i) {
  5020. /* ep0 maxpacket size may change; let the HCD know about it.
  5021. * Other endpoints will be handled by re-enumeration. */
  5022. usb_ep0_reinit(udev);
  5023. ret = hub_port_init(parent_hub, udev, port1, i);
  5024. if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV)
  5025. break;
  5026. }
  5027. if (ret < 0)
  5028. goto re_enumerate;
  5029. /* Device might have changed firmware (DFU or similar) */
  5030. if (descriptors_changed(udev, &descriptor, bos)) {
  5031. dev_info(&udev->dev, "device firmware changed\n");
  5032. udev->descriptor = descriptor; /* for disconnect() calls */
  5033. goto re_enumerate;
  5034. }
  5035. /* Restore the device's previous configuration */
  5036. if (!udev->actconfig)
  5037. goto done;
  5038. mutex_lock(hcd->bandwidth_mutex);
  5039. ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL);
  5040. if (ret < 0) {
  5041. dev_warn(&udev->dev,
  5042. "Busted HC? Not enough HCD resources for "
  5043. "old configuration.\n");
  5044. mutex_unlock(hcd->bandwidth_mutex);
  5045. goto re_enumerate;
  5046. }
  5047. ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  5048. USB_REQ_SET_CONFIGURATION, 0,
  5049. udev->actconfig->desc.bConfigurationValue, 0,
  5050. NULL, 0, USB_CTRL_SET_TIMEOUT);
  5051. if (ret < 0) {
  5052. dev_err(&udev->dev,
  5053. "can't restore configuration #%d (error=%d)\n",
  5054. udev->actconfig->desc.bConfigurationValue, ret);
  5055. mutex_unlock(hcd->bandwidth_mutex);
  5056. goto re_enumerate;
  5057. }
  5058. mutex_unlock(hcd->bandwidth_mutex);
  5059. usb_set_device_state(udev, USB_STATE_CONFIGURED);
  5060. /* Put interfaces back into the same altsettings as before.
  5061. * Don't bother to send the Set-Interface request for interfaces
  5062. * that were already in altsetting 0; besides being unnecessary,
  5063. * many devices can't handle it. Instead just reset the host-side
  5064. * endpoint state.
  5065. */
  5066. for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
  5067. struct usb_host_config *config = udev->actconfig;
  5068. struct usb_interface *intf = config->interface[i];
  5069. struct usb_interface_descriptor *desc;
  5070. desc = &intf->cur_altsetting->desc;
  5071. if (desc->bAlternateSetting == 0) {
  5072. usb_disable_interface(udev, intf, true);
  5073. usb_enable_interface(udev, intf, true);
  5074. ret = 0;
  5075. } else {
  5076. /* Let the bandwidth allocation function know that this
  5077. * device has been reset, and it will have to use
  5078. * alternate setting 0 as the current alternate setting.
  5079. */
  5080. intf->resetting_device = 1;
  5081. ret = usb_set_interface(udev, desc->bInterfaceNumber,
  5082. desc->bAlternateSetting);
  5083. intf->resetting_device = 0;
  5084. }
  5085. if (ret < 0) {
  5086. dev_err(&udev->dev, "failed to restore interface %d "
  5087. "altsetting %d (error=%d)\n",
  5088. desc->bInterfaceNumber,
  5089. desc->bAlternateSetting,
  5090. ret);
  5091. goto re_enumerate;
  5092. }
  5093. /* Resetting also frees any allocated streams */
  5094. for (j = 0; j < intf->cur_altsetting->desc.bNumEndpoints; j++)
  5095. intf->cur_altsetting->endpoint[j].streams = 0;
  5096. }
  5097. done:
  5098. /* Now that the alt settings are re-installed, enable LTM and LPM. */
  5099. usb_enable_usb2_hardware_lpm(udev);
  5100. usb_unlocked_enable_lpm(udev);
  5101. usb_enable_ltm(udev);
  5102. usb_release_bos_descriptor(udev);
  5103. udev->bos = bos;
  5104. return 0;
  5105. re_enumerate:
  5106. usb_release_bos_descriptor(udev);
  5107. udev->bos = bos;
  5108. re_enumerate_no_bos:
  5109. /* LPM state doesn't matter when we're about to destroy the device. */
  5110. hub_port_logical_disconnect(parent_hub, port1);
  5111. return -ENODEV;
  5112. }
  5113. /**
  5114. * usb_reset_device - warn interface drivers and perform a USB port reset
  5115. * @udev: device to reset (not in NOTATTACHED state)
  5116. *
  5117. * Warns all drivers bound to registered interfaces (using their pre_reset
  5118. * method), performs the port reset, and then lets the drivers know that
  5119. * the reset is over (using their post_reset method).
  5120. *
  5121. * Return: The same as for usb_reset_and_verify_device().
  5122. *
  5123. * Note:
  5124. * The caller must own the device lock. For example, it's safe to use
  5125. * this from a driver probe() routine after downloading new firmware.
  5126. * For calls that might not occur during probe(), drivers should lock
  5127. * the device using usb_lock_device_for_reset().
  5128. *
  5129. * If an interface is currently being probed or disconnected, we assume
  5130. * its driver knows how to handle resets. For all other interfaces,
  5131. * if the driver doesn't have pre_reset and post_reset methods then
  5132. * we attempt to unbind it and rebind afterward.
  5133. */
  5134. int usb_reset_device(struct usb_device *udev)
  5135. {
  5136. int ret;
  5137. int i;
  5138. unsigned int noio_flag;
  5139. struct usb_port *port_dev;
  5140. struct usb_host_config *config = udev->actconfig;
  5141. struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
  5142. if (udev->state == USB_STATE_NOTATTACHED) {
  5143. dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
  5144. udev->state);
  5145. return -EINVAL;
  5146. }
  5147. if (!udev->parent) {
  5148. /* this requires hcd-specific logic; see ohci_restart() */
  5149. dev_dbg(&udev->dev, "%s for root hub!\n", __func__);
  5150. return -EISDIR;
  5151. }
  5152. port_dev = hub->ports[udev->portnum - 1];
  5153. /*
  5154. * Don't allocate memory with GFP_KERNEL in current
  5155. * context to avoid possible deadlock if usb mass
  5156. * storage interface or usbnet interface(iSCSI case)
  5157. * is included in current configuration. The easist
  5158. * approach is to do it for every device reset,
  5159. * because the device 'memalloc_noio' flag may have
  5160. * not been set before reseting the usb device.
  5161. */
  5162. noio_flag = memalloc_noio_save();
  5163. /* Prevent autosuspend during the reset */
  5164. usb_autoresume_device(udev);
  5165. if (config) {
  5166. for (i = 0; i < config->desc.bNumInterfaces; ++i) {
  5167. struct usb_interface *cintf = config->interface[i];
  5168. struct usb_driver *drv;
  5169. int unbind = 0;
  5170. if (cintf->dev.driver) {
  5171. drv = to_usb_driver(cintf->dev.driver);
  5172. if (drv->pre_reset && drv->post_reset)
  5173. unbind = (drv->pre_reset)(cintf);
  5174. else if (cintf->condition ==
  5175. USB_INTERFACE_BOUND)
  5176. unbind = 1;
  5177. if (unbind)
  5178. usb_forced_unbind_intf(cintf);
  5179. }
  5180. }
  5181. }
  5182. usb_lock_port(port_dev);
  5183. ret = usb_reset_and_verify_device(udev);
  5184. usb_unlock_port(port_dev);
  5185. if (config) {
  5186. for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) {
  5187. struct usb_interface *cintf = config->interface[i];
  5188. struct usb_driver *drv;
  5189. int rebind = cintf->needs_binding;
  5190. if (!rebind && cintf->dev.driver) {
  5191. drv = to_usb_driver(cintf->dev.driver);
  5192. if (drv->post_reset)
  5193. rebind = (drv->post_reset)(cintf);
  5194. else if (cintf->condition ==
  5195. USB_INTERFACE_BOUND)
  5196. rebind = 1;
  5197. if (rebind)
  5198. cintf->needs_binding = 1;
  5199. }
  5200. }
  5201. /* If the reset failed, hub_wq will unbind drivers later */
  5202. if (ret == 0)
  5203. usb_unbind_and_rebind_marked_interfaces(udev);
  5204. }
  5205. usb_autosuspend_device(udev);
  5206. memalloc_noio_restore(noio_flag);
  5207. return ret;
  5208. }
  5209. EXPORT_SYMBOL_GPL(usb_reset_device);
  5210. /**
  5211. * usb_queue_reset_device - Reset a USB device from an atomic context
  5212. * @iface: USB interface belonging to the device to reset
  5213. *
  5214. * This function can be used to reset a USB device from an atomic
  5215. * context, where usb_reset_device() won't work (as it blocks).
  5216. *
  5217. * Doing a reset via this method is functionally equivalent to calling
  5218. * usb_reset_device(), except for the fact that it is delayed to a
  5219. * workqueue. This means that any drivers bound to other interfaces
  5220. * might be unbound, as well as users from usbfs in user space.
  5221. *
  5222. * Corner cases:
  5223. *
  5224. * - Scheduling two resets at the same time from two different drivers
  5225. * attached to two different interfaces of the same device is
  5226. * possible; depending on how the driver attached to each interface
  5227. * handles ->pre_reset(), the second reset might happen or not.
  5228. *
  5229. * - If the reset is delayed so long that the interface is unbound from
  5230. * its driver, the reset will be skipped.
  5231. *
  5232. * - This function can be called during .probe(). It can also be called
  5233. * during .disconnect(), but doing so is pointless because the reset
  5234. * will not occur. If you really want to reset the device during
  5235. * .disconnect(), call usb_reset_device() directly -- but watch out
  5236. * for nested unbinding issues!
  5237. */
  5238. void usb_queue_reset_device(struct usb_interface *iface)
  5239. {
  5240. if (schedule_work(&iface->reset_ws))
  5241. usb_get_intf(iface);
  5242. }
  5243. EXPORT_SYMBOL_GPL(usb_queue_reset_device);
  5244. /**
  5245. * usb_hub_find_child - Get the pointer of child device
  5246. * attached to the port which is specified by @port1.
  5247. * @hdev: USB device belonging to the usb hub
  5248. * @port1: port num to indicate which port the child device
  5249. * is attached to.
  5250. *
  5251. * USB drivers call this function to get hub's child device
  5252. * pointer.
  5253. *
  5254. * Return: %NULL if input param is invalid and
  5255. * child's usb_device pointer if non-NULL.
  5256. */
  5257. struct usb_device *usb_hub_find_child(struct usb_device *hdev,
  5258. int port1)
  5259. {
  5260. struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
  5261. if (port1 < 1 || port1 > hdev->maxchild)
  5262. return NULL;
  5263. return hub->ports[port1 - 1]->child;
  5264. }
  5265. EXPORT_SYMBOL_GPL(usb_hub_find_child);
  5266. void usb_hub_adjust_deviceremovable(struct usb_device *hdev,
  5267. struct usb_hub_descriptor *desc)
  5268. {
  5269. struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
  5270. enum usb_port_connect_type connect_type;
  5271. int i;
  5272. if (!hub)
  5273. return;
  5274. if (!hub_is_superspeed(hdev)) {
  5275. for (i = 1; i <= hdev->maxchild; i++) {
  5276. struct usb_port *port_dev = hub->ports[i - 1];
  5277. connect_type = port_dev->connect_type;
  5278. if (connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) {
  5279. u8 mask = 1 << (i%8);
  5280. if (!(desc->u.hs.DeviceRemovable[i/8] & mask)) {
  5281. dev_dbg(&port_dev->dev, "DeviceRemovable is changed to 1 according to platform information.\n");
  5282. desc->u.hs.DeviceRemovable[i/8] |= mask;
  5283. }
  5284. }
  5285. }
  5286. } else {
  5287. u16 port_removable = le16_to_cpu(desc->u.ss.DeviceRemovable);
  5288. for (i = 1; i <= hdev->maxchild; i++) {
  5289. struct usb_port *port_dev = hub->ports[i - 1];
  5290. connect_type = port_dev->connect_type;
  5291. if (connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) {
  5292. u16 mask = 1 << i;
  5293. if (!(port_removable & mask)) {
  5294. dev_dbg(&port_dev->dev, "DeviceRemovable is changed to 1 according to platform information.\n");
  5295. port_removable |= mask;
  5296. }
  5297. }
  5298. }
  5299. desc->u.ss.DeviceRemovable = cpu_to_le16(port_removable);
  5300. }
  5301. }
  5302. #ifdef CONFIG_ACPI
  5303. /**
  5304. * usb_get_hub_port_acpi_handle - Get the usb port's acpi handle
  5305. * @hdev: USB device belonging to the usb hub
  5306. * @port1: port num of the port
  5307. *
  5308. * Return: Port's acpi handle if successful, %NULL if params are
  5309. * invalid.
  5310. */
  5311. acpi_handle usb_get_hub_port_acpi_handle(struct usb_device *hdev,
  5312. int port1)
  5313. {
  5314. struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
  5315. if (!hub)
  5316. return NULL;
  5317. return ACPI_HANDLE(&hub->ports[port1 - 1]->dev);
  5318. }
  5319. #endif