hub.c 170 KB

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