hub.c 165 KB

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