hub.c 139 KB

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