hub.c 165 KB

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