hub.c 149 KB

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