hub.c 153 KB

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