hub.c 147 KB

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