hub.c 143 KB

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