hub.c 147 KB

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