hub.c 166 KB

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