hub.c 163 KB

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