hub.c 136 KB

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