hub.c 167 KB

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