hub.c 143 KB

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