hub.c 155 KB

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