hub.c 155 KB

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