hub.c 166 KB

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