hub.c 160 KB

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