hub.c 165 KB

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