hub.c 161 KB

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