hub.c 145 KB

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