hub.c 148 KB

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