hub.c 147 KB

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