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