hub.c 154 KB

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