hub.c 153 KB

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