hub.c 136 KB

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