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