hub.c 149 KB

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