hub.c 156 KB

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