hub.c 145 KB

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