hub.c 158 KB

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