hub.c 149 KB

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