hub.c 145 KB

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