hub.c 154 KB

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