hub.c 170 KB

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