hub.c 161 KB

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