hub.c 153 KB

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