hub.c 165 KB

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