hub.c 166 KB

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