hub.c 156 KB

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