hub.c 163 KB

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