hub.c 156 KB

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