hub.c 170 KB

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