hub.c 154 KB

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