hub.c 139 KB

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