hub.c 167 KB

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