hub.c 166 KB

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