950-0106-Add-dwc_otg-driver.patch 1.7 MB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285292862928729288292892929029291292922929329294292952929629297292982929929300293012930229303293042930529306293072930829309293102931129312293132931429315293162931729318293192932029321293222932329324293252932629327293282932929330293312933229333293342933529336293372933829339293402934129342293432934429345293462934729348293492935029351293522935329354293552935629357293582935929360293612936229363293642936529366293672936829369293702937129372293732937429375293762937729378293792938029381293822938329384293852938629387293882938929390293912939229393293942939529396293972939829399294002940129402294032940429405294062940729408294092941029411294122941329414294152941629417294182941929420294212942229423294242942529426294272942829429294302943129432294332943429435294362943729438294392944029441294422944329444294452944629447294482944929450294512945229453294542945529456294572945829459294602946129462294632946429465294662946729468294692947029471294722947329474294752947629477294782947929480294812948229483294842948529486294872948829489294902949129492294932949429495294962949729498294992950029501295022950329504295052950629507295082950929510295112951229513295142951529516295172951829519295202952129522295232952429525295262952729528295292953029531295322953329534295352953629537295382953929540295412954229543295442954529546295472954829549295502955129552295532955429555295562955729558295592956029561295622956329564295652956629567295682956929570295712957229573295742957529576295772957829579295802958129582295832958429585295862958729588295892959029591295922959329594295952959629597295982959929600296012960229603296042960529606296072960829609296102961129612296132961429615296162961729618296192962029621296222962329624296252962629627296282962929630296312963229633296342963529636296372963829639296402964129642296432964429645296462964729648296492965029651296522965329654296552965629657296582965929660296612966229663296642966529666296672966829669296702967129672296732967429675296762967729678296792968029681296822968329684296852968629687296882968929690296912969229693296942969529696296972969829699297002970129702297032970429705297062970729708297092971029711297122971329714297152971629717297182971929720297212972229723297242972529726297272972829729297302973129732297332973429735297362973729738297392974029741297422974329744297452974629747297482974929750297512975229753297542975529756297572975829759297602976129762297632976429765297662976729768297692977029771297722977329774297752977629777297782977929780297812978229783297842978529786297872978829789297902979129792297932979429795297962979729798297992980029801298022980329804298052980629807298082980929810298112981229813298142981529816298172981829819298202982129822298232982429825298262982729828298292983029831298322983329834298352983629837298382983929840298412984229843298442984529846298472984829849298502985129852298532985429855298562985729858298592986029861298622986329864298652986629867298682986929870298712987229873298742987529876298772987829879298802988129882298832988429885298862988729888298892989029891298922989329894298952989629897298982989929900299012990229903299042990529906299072990829909299102991129912299132991429915299162991729918299192992029921299222992329924299252992629927299282992929930299312993229933299342993529936299372993829939299402994129942299432994429945299462994729948299492995029951299522995329954299552995629957299582995929960299612996229963299642996529966299672996829969299702997129972299732997429975299762997729978299792998029981299822998329984299852998629987299882998929990299912999229993299942999529996299972999829999300003000130002300033000430005300063000730008300093001030011300123001330014300153001630017300183001930020300213002230023300243002530026300273002830029300303003130032300333003430035300363003730038300393004030041300423004330044300453004630047300483004930050300513005230053300543005530056300573005830059300603006130062300633006430065300663006730068300693007030071300723007330074300753007630077300783007930080300813008230083300843008530086300873008830089300903009130092300933009430095300963009730098300993010030101301023010330104301053010630107301083010930110301113011230113301143011530116301173011830119301203012130122301233012430125301263012730128301293013030131301323013330134301353013630137301383013930140301413014230143301443014530146301473014830149301503015130152301533015430155301563015730158301593016030161301623016330164301653016630167301683016930170301713017230173301743017530176301773017830179301803018130182301833018430185301863018730188301893019030191301923019330194301953019630197301983019930200302013020230203302043020530206302073020830209302103021130212302133021430215302163021730218302193022030221302223022330224302253022630227302283022930230302313023230233302343023530236302373023830239302403024130242302433024430245302463024730248302493025030251302523025330254302553025630257302583025930260302613026230263302643026530266302673026830269302703027130272302733027430275302763027730278302793028030281302823028330284302853028630287302883028930290302913029230293302943029530296302973029830299303003030130302303033030430305303063030730308303093031030311303123031330314303153031630317303183031930320303213032230323303243032530326303273032830329303303033130332303333033430335303363033730338303393034030341303423034330344303453034630347303483034930350303513035230353303543035530356303573035830359303603036130362303633036430365303663036730368303693037030371303723037330374303753037630377303783037930380303813038230383303843038530386303873038830389303903039130392303933039430395303963039730398303993040030401304023040330404304053040630407304083040930410304113041230413304143041530416304173041830419304203042130422304233042430425304263042730428304293043030431304323043330434304353043630437304383043930440304413044230443304443044530446304473044830449304503045130452304533045430455304563045730458304593046030461304623046330464304653046630467304683046930470304713047230473304743047530476304773047830479304803048130482304833048430485304863048730488304893049030491304923049330494304953049630497304983049930500305013050230503305043050530506305073050830509305103051130512305133051430515305163051730518305193052030521305223052330524305253052630527305283052930530305313053230533305343053530536305373053830539305403054130542305433054430545305463054730548305493055030551305523055330554305553055630557305583055930560305613056230563305643056530566305673056830569305703057130572305733057430575305763057730578305793058030581305823058330584305853058630587305883058930590305913059230593305943059530596305973059830599306003060130602306033060430605306063060730608306093061030611306123061330614306153061630617306183061930620306213062230623306243062530626306273062830629306303063130632306333063430635306363063730638306393064030641306423064330644306453064630647306483064930650306513065230653306543065530656306573065830659306603066130662306633066430665306663066730668306693067030671306723067330674306753067630677306783067930680306813068230683306843068530686306873068830689306903069130692306933069430695306963069730698306993070030701307023070330704307053070630707307083070930710307113071230713307143071530716307173071830719307203072130722307233072430725307263072730728307293073030731307323073330734307353073630737307383073930740307413074230743307443074530746307473074830749307503075130752307533075430755307563075730758307593076030761307623076330764307653076630767307683076930770307713077230773307743077530776307773077830779307803078130782307833078430785307863078730788307893079030791307923079330794307953079630797307983079930800308013080230803308043080530806308073080830809308103081130812308133081430815308163081730818308193082030821308223082330824308253082630827308283082930830308313083230833308343083530836308373083830839308403084130842308433084430845308463084730848308493085030851308523085330854308553085630857308583085930860308613086230863308643086530866308673086830869308703087130872308733087430875308763087730878308793088030881308823088330884308853088630887308883088930890308913089230893308943089530896308973089830899309003090130902309033090430905309063090730908309093091030911309123091330914309153091630917309183091930920309213092230923309243092530926309273092830929309303093130932309333093430935309363093730938309393094030941309423094330944309453094630947309483094930950309513095230953309543095530956309573095830959309603096130962309633096430965309663096730968309693097030971309723097330974309753097630977309783097930980309813098230983309843098530986309873098830989309903099130992309933099430995309963099730998309993100031001310023100331004310053100631007310083100931010310113101231013310143101531016310173101831019310203102131022310233102431025310263102731028310293103031031310323103331034310353103631037310383103931040310413104231043310443104531046310473104831049310503105131052310533105431055310563105731058310593106031061310623106331064310653106631067310683106931070310713107231073310743107531076310773107831079310803108131082310833108431085310863108731088310893109031091310923109331094310953109631097310983109931100311013110231103311043110531106311073110831109311103111131112311133111431115311163111731118311193112031121311223112331124311253112631127311283112931130311313113231133311343113531136311373113831139311403114131142311433114431145311463114731148311493115031151311523115331154311553115631157311583115931160311613116231163311643116531166311673116831169311703117131172311733117431175311763117731178311793118031181311823118331184311853118631187311883118931190311913119231193311943119531196311973119831199312003120131202312033120431205312063120731208312093121031211312123121331214312153121631217312183121931220312213122231223312243122531226312273122831229312303123131232312333123431235312363123731238312393124031241312423124331244312453124631247312483124931250312513125231253312543125531256312573125831259312603126131262312633126431265312663126731268312693127031271312723127331274312753127631277312783127931280312813128231283312843128531286312873128831289312903129131292312933129431295312963129731298312993130031301313023130331304313053130631307313083130931310313113131231313313143131531316313173131831319313203132131322313233132431325313263132731328313293133031331313323133331334313353133631337313383133931340313413134231343313443134531346313473134831349313503135131352313533135431355313563135731358313593136031361313623136331364313653136631367313683136931370313713137231373313743137531376313773137831379313803138131382313833138431385313863138731388313893139031391313923139331394313953139631397313983139931400314013140231403314043140531406314073140831409314103141131412314133141431415314163141731418314193142031421314223142331424314253142631427314283142931430314313143231433314343143531436314373143831439314403144131442314433144431445314463144731448314493145031451314523145331454314553145631457314583145931460314613146231463314643146531466314673146831469314703147131472314733147431475314763147731478314793148031481314823148331484314853148631487314883148931490314913149231493314943149531496314973149831499315003150131502315033150431505315063150731508315093151031511315123151331514315153151631517315183151931520315213152231523315243152531526315273152831529315303153131532315333153431535315363153731538315393154031541315423154331544315453154631547315483154931550315513155231553315543155531556315573155831559315603156131562315633156431565315663156731568315693157031571315723157331574315753157631577315783157931580315813158231583315843158531586315873158831589315903159131592315933159431595315963159731598315993160031601316023160331604316053160631607316083160931610316113161231613316143161531616316173161831619316203162131622316233162431625316263162731628316293163031631316323163331634316353163631637316383163931640316413164231643316443164531646316473164831649316503165131652316533165431655316563165731658316593166031661316623166331664316653166631667316683166931670316713167231673316743167531676316773167831679316803168131682316833168431685316863168731688316893169031691316923169331694316953169631697316983169931700317013170231703317043170531706317073170831709317103171131712317133171431715317163171731718317193172031721317223172331724317253172631727317283172931730317313173231733317343173531736317373173831739317403174131742317433174431745317463174731748317493175031751317523175331754317553175631757317583175931760317613176231763317643176531766317673176831769317703177131772317733177431775317763177731778317793178031781317823178331784317853178631787317883178931790317913179231793317943179531796317973179831799318003180131802318033180431805318063180731808318093181031811318123181331814318153181631817318183181931820318213182231823318243182531826318273182831829318303183131832318333183431835318363183731838318393184031841318423184331844318453184631847318483184931850318513185231853318543185531856318573185831859318603186131862318633186431865318663186731868318693187031871318723187331874318753187631877318783187931880318813188231883318843188531886318873188831889318903189131892318933189431895318963189731898318993190031901319023190331904319053190631907319083190931910319113191231913319143191531916319173191831919319203192131922319233192431925319263192731928319293193031931319323193331934319353193631937319383193931940319413194231943319443194531946319473194831949319503195131952319533195431955319563195731958319593196031961319623196331964319653196631967319683196931970319713197231973319743197531976319773197831979319803198131982319833198431985319863198731988319893199031991319923199331994319953199631997319983199932000320013200232003320043200532006320073200832009320103201132012320133201432015320163201732018320193202032021320223202332024320253202632027320283202932030320313203232033320343203532036320373203832039320403204132042320433204432045320463204732048320493205032051320523205332054320553205632057320583205932060320613206232063320643206532066320673206832069320703207132072320733207432075320763207732078320793208032081320823208332084320853208632087320883208932090320913209232093320943209532096320973209832099321003210132102321033210432105321063210732108321093211032111321123211332114321153211632117321183211932120321213212232123321243212532126321273212832129321303213132132321333213432135321363213732138321393214032141321423214332144321453214632147321483214932150321513215232153321543215532156321573215832159321603216132162321633216432165321663216732168321693217032171321723217332174321753217632177321783217932180321813218232183321843218532186321873218832189321903219132192321933219432195321963219732198321993220032201322023220332204322053220632207322083220932210322113221232213322143221532216322173221832219322203222132222322233222432225322263222732228322293223032231322323223332234322353223632237322383223932240322413224232243322443224532246322473224832249322503225132252322533225432255322563225732258322593226032261322623226332264322653226632267322683226932270322713227232273322743227532276322773227832279322803228132282322833228432285322863228732288322893229032291322923229332294322953229632297322983229932300323013230232303323043230532306323073230832309323103231132312323133231432315323163231732318323193232032321323223232332324323253232632327323283232932330323313233232333323343233532336323373233832339323403234132342323433234432345323463234732348323493235032351323523235332354323553235632357323583235932360323613236232363323643236532366323673236832369323703237132372323733237432375323763237732378323793238032381323823238332384323853238632387323883238932390323913239232393323943239532396323973239832399324003240132402324033240432405324063240732408324093241032411324123241332414324153241632417324183241932420324213242232423324243242532426324273242832429324303243132432324333243432435324363243732438324393244032441324423244332444324453244632447324483244932450324513245232453324543245532456324573245832459324603246132462324633246432465324663246732468324693247032471324723247332474324753247632477324783247932480324813248232483324843248532486324873248832489324903249132492324933249432495324963249732498324993250032501325023250332504325053250632507325083250932510325113251232513325143251532516325173251832519325203252132522325233252432525325263252732528325293253032531325323253332534325353253632537325383253932540325413254232543325443254532546325473254832549325503255132552325533255432555325563255732558325593256032561325623256332564325653256632567325683256932570325713257232573325743257532576325773257832579325803258132582325833258432585325863258732588325893259032591325923259332594325953259632597325983259932600326013260232603326043260532606326073260832609326103261132612326133261432615326163261732618326193262032621326223262332624326253262632627326283262932630326313263232633326343263532636326373263832639326403264132642326433264432645326463264732648326493265032651326523265332654326553265632657326583265932660326613266232663326643266532666326673266832669326703267132672326733267432675326763267732678326793268032681326823268332684326853268632687326883268932690326913269232693326943269532696326973269832699327003270132702327033270432705327063270732708327093271032711327123271332714327153271632717327183271932720327213272232723327243272532726327273272832729327303273132732327333273432735327363273732738327393274032741327423274332744327453274632747327483274932750327513275232753327543275532756327573275832759327603276132762327633276432765327663276732768327693277032771327723277332774327753277632777327783277932780327813278232783327843278532786327873278832789327903279132792327933279432795327963279732798327993280032801328023280332804328053280632807328083280932810328113281232813328143281532816328173281832819328203282132822328233282432825328263282732828328293283032831328323283332834328353283632837328383283932840328413284232843328443284532846328473284832849328503285132852328533285432855328563285732858328593286032861328623286332864328653286632867328683286932870328713287232873328743287532876328773287832879328803288132882328833288432885328863288732888328893289032891328923289332894328953289632897328983289932900329013290232903329043290532906329073290832909329103291132912329133291432915329163291732918329193292032921329223292332924329253292632927329283292932930329313293232933329343293532936329373293832939329403294132942329433294432945329463294732948329493295032951329523295332954329553295632957329583295932960329613296232963329643296532966329673296832969329703297132972329733297432975329763297732978329793298032981329823298332984329853298632987329883298932990329913299232993329943299532996329973299832999330003300133002330033300433005330063300733008330093301033011330123301333014330153301633017330183301933020330213302233023330243302533026330273302833029330303303133032330333303433035330363303733038330393304033041330423304333044330453304633047330483304933050330513305233053330543305533056330573305833059330603306133062330633306433065330663306733068330693307033071330723307333074330753307633077330783307933080330813308233083330843308533086330873308833089330903309133092330933309433095330963309733098330993310033101331023310333104331053310633107331083310933110331113311233113331143311533116331173311833119331203312133122331233312433125331263312733128331293313033131331323313333134331353313633137331383313933140331413314233143331443314533146331473314833149331503315133152331533315433155331563315733158331593316033161331623316333164331653316633167331683316933170331713317233173331743317533176331773317833179331803318133182331833318433185331863318733188331893319033191331923319333194331953319633197331983319933200332013320233203332043320533206332073320833209332103321133212332133321433215332163321733218332193322033221332223322333224332253322633227332283322933230332313323233233332343323533236332373323833239332403324133242332433324433245332463324733248332493325033251332523325333254332553325633257332583325933260332613326233263332643326533266332673326833269332703327133272332733327433275332763327733278332793328033281332823328333284332853328633287332883328933290332913329233293332943329533296332973329833299333003330133302333033330433305333063330733308333093331033311333123331333314333153331633317333183331933320333213332233323333243332533326333273332833329333303333133332333333333433335333363333733338333393334033341333423334333344333453334633347333483334933350333513335233353333543335533356333573335833359333603336133362333633336433365333663336733368333693337033371333723337333374333753337633377333783337933380333813338233383333843338533386333873338833389333903339133392333933339433395333963339733398333993340033401334023340333404334053340633407334083340933410334113341233413334143341533416334173341833419334203342133422334233342433425334263342733428334293343033431334323343333434334353343633437334383343933440334413344233443334443344533446334473344833449334503345133452334533345433455334563345733458334593346033461334623346333464334653346633467334683346933470334713347233473334743347533476334773347833479334803348133482334833348433485334863348733488334893349033491334923349333494334953349633497334983349933500335013350233503335043350533506335073350833509335103351133512335133351433515335163351733518335193352033521335223352333524335253352633527335283352933530335313353233533335343353533536335373353833539335403354133542335433354433545335463354733548335493355033551335523355333554335553355633557335583355933560335613356233563335643356533566335673356833569335703357133572335733357433575335763357733578335793358033581335823358333584335853358633587335883358933590335913359233593335943359533596335973359833599336003360133602336033360433605336063360733608336093361033611336123361333614336153361633617336183361933620336213362233623336243362533626336273362833629336303363133632336333363433635336363363733638336393364033641336423364333644336453364633647336483364933650336513365233653336543365533656336573365833659336603366133662336633366433665336663366733668336693367033671336723367333674336753367633677336783367933680336813368233683336843368533686336873368833689336903369133692336933369433695336963369733698336993370033701337023370333704337053370633707337083370933710337113371233713337143371533716337173371833719337203372133722337233372433725337263372733728337293373033731337323373333734337353373633737337383373933740337413374233743337443374533746337473374833749337503375133752337533375433755337563375733758337593376033761337623376333764337653376633767337683376933770337713377233773337743377533776337773377833779337803378133782337833378433785337863378733788337893379033791337923379333794337953379633797337983379933800338013380233803338043380533806338073380833809338103381133812338133381433815338163381733818338193382033821338223382333824338253382633827338283382933830338313383233833338343383533836338373383833839338403384133842338433384433845338463384733848338493385033851338523385333854338553385633857338583385933860338613386233863338643386533866338673386833869338703387133872338733387433875338763387733878338793388033881338823388333884338853388633887338883388933890338913389233893338943389533896338973389833899339003390133902339033390433905339063390733908339093391033911339123391333914339153391633917339183391933920339213392233923339243392533926339273392833929339303393133932339333393433935339363393733938339393394033941339423394333944339453394633947339483394933950339513395233953339543395533956339573395833959339603396133962339633396433965339663396733968339693397033971339723397333974339753397633977339783397933980339813398233983339843398533986339873398833989339903399133992339933399433995339963399733998339993400034001340023400334004340053400634007340083400934010340113401234013340143401534016340173401834019340203402134022340233402434025340263402734028340293403034031340323403334034340353403634037340383403934040340413404234043340443404534046340473404834049340503405134052340533405434055340563405734058340593406034061340623406334064340653406634067340683406934070340713407234073340743407534076340773407834079340803408134082340833408434085340863408734088340893409034091340923409334094340953409634097340983409934100341013410234103341043410534106341073410834109341103411134112341133411434115341163411734118341193412034121341223412334124341253412634127341283412934130341313413234133341343413534136341373413834139341403414134142341433414434145341463414734148341493415034151341523415334154341553415634157341583415934160341613416234163341643416534166341673416834169341703417134172341733417434175341763417734178341793418034181341823418334184341853418634187341883418934190341913419234193341943419534196341973419834199342003420134202342033420434205342063420734208342093421034211342123421334214342153421634217342183421934220342213422234223342243422534226342273422834229342303423134232342333423434235342363423734238342393424034241342423424334244342453424634247342483424934250342513425234253342543425534256342573425834259342603426134262342633426434265342663426734268342693427034271342723427334274342753427634277342783427934280342813428234283342843428534286342873428834289342903429134292342933429434295342963429734298342993430034301343023430334304343053430634307343083430934310343113431234313343143431534316343173431834319343203432134322343233432434325343263432734328343293433034331343323433334334343353433634337343383433934340343413434234343343443434534346343473434834349343503435134352343533435434355343563435734358343593436034361343623436334364343653436634367343683436934370343713437234373343743437534376343773437834379343803438134382343833438434385343863438734388343893439034391343923439334394343953439634397343983439934400344013440234403344043440534406344073440834409344103441134412344133441434415344163441734418344193442034421344223442334424344253442634427344283442934430344313443234433344343443534436344373443834439344403444134442344433444434445344463444734448344493445034451344523445334454344553445634457344583445934460344613446234463344643446534466344673446834469344703447134472344733447434475344763447734478344793448034481344823448334484344853448634487344883448934490344913449234493344943449534496344973449834499345003450134502345033450434505345063450734508345093451034511345123451334514345153451634517345183451934520345213452234523345243452534526345273452834529345303453134532345333453434535345363453734538345393454034541345423454334544345453454634547345483454934550345513455234553345543455534556345573455834559345603456134562345633456434565345663456734568345693457034571345723457334574345753457634577345783457934580345813458234583345843458534586345873458834589345903459134592345933459434595345963459734598345993460034601346023460334604346053460634607346083460934610346113461234613346143461534616346173461834619346203462134622346233462434625346263462734628346293463034631346323463334634346353463634637346383463934640346413464234643346443464534646346473464834649346503465134652346533465434655346563465734658346593466034661346623466334664346653466634667346683466934670346713467234673346743467534676346773467834679346803468134682346833468434685346863468734688346893469034691346923469334694346953469634697346983469934700347013470234703347043470534706347073470834709347103471134712347133471434715347163471734718347193472034721347223472334724347253472634727347283472934730347313473234733347343473534736347373473834739347403474134742347433474434745347463474734748347493475034751347523475334754347553475634757347583475934760347613476234763347643476534766347673476834769347703477134772347733477434775347763477734778347793478034781347823478334784347853478634787347883478934790347913479234793347943479534796347973479834799348003480134802348033480434805348063480734808348093481034811348123481334814348153481634817348183481934820348213482234823348243482534826348273482834829348303483134832348333483434835348363483734838348393484034841348423484334844348453484634847348483484934850348513485234853348543485534856348573485834859348603486134862348633486434865348663486734868348693487034871348723487334874348753487634877348783487934880348813488234883348843488534886348873488834889348903489134892348933489434895348963489734898348993490034901349023490334904349053490634907349083490934910349113491234913349143491534916349173491834919349203492134922349233492434925349263492734928349293493034931349323493334934349353493634937349383493934940349413494234943349443494534946349473494834949349503495134952349533495434955349563495734958349593496034961349623496334964349653496634967349683496934970349713497234973349743497534976349773497834979349803498134982349833498434985349863498734988349893499034991349923499334994349953499634997349983499935000350013500235003350043500535006350073500835009350103501135012350133501435015350163501735018350193502035021350223502335024350253502635027350283502935030350313503235033350343503535036350373503835039350403504135042350433504435045350463504735048350493505035051350523505335054350553505635057350583505935060350613506235063350643506535066350673506835069350703507135072350733507435075350763507735078350793508035081350823508335084350853508635087350883508935090350913509235093350943509535096350973509835099351003510135102351033510435105351063510735108351093511035111351123511335114351153511635117351183511935120351213512235123351243512535126351273512835129351303513135132351333513435135351363513735138351393514035141351423514335144351453514635147351483514935150351513515235153351543515535156351573515835159351603516135162351633516435165351663516735168351693517035171351723517335174351753517635177351783517935180351813518235183351843518535186351873518835189351903519135192351933519435195351963519735198351993520035201352023520335204352053520635207352083520935210352113521235213352143521535216352173521835219352203522135222352233522435225352263522735228352293523035231352323523335234352353523635237352383523935240352413524235243352443524535246352473524835249352503525135252352533525435255352563525735258352593526035261352623526335264352653526635267352683526935270352713527235273352743527535276352773527835279352803528135282352833528435285352863528735288352893529035291352923529335294352953529635297352983529935300353013530235303353043530535306353073530835309353103531135312353133531435315353163531735318353193532035321353223532335324353253532635327353283532935330353313533235333353343533535336353373533835339353403534135342353433534435345353463534735348353493535035351353523535335354353553535635357353583535935360353613536235363353643536535366353673536835369353703537135372353733537435375353763537735378353793538035381353823538335384353853538635387353883538935390353913539235393353943539535396353973539835399354003540135402354033540435405354063540735408354093541035411354123541335414354153541635417354183541935420354213542235423354243542535426354273542835429354303543135432354333543435435354363543735438354393544035441354423544335444354453544635447354483544935450354513545235453354543545535456354573545835459354603546135462354633546435465354663546735468354693547035471354723547335474354753547635477354783547935480354813548235483354843548535486354873548835489354903549135492354933549435495354963549735498354993550035501355023550335504355053550635507355083550935510355113551235513355143551535516355173551835519355203552135522355233552435525355263552735528355293553035531355323553335534355353553635537355383553935540355413554235543355443554535546355473554835549355503555135552355533555435555355563555735558355593556035561355623556335564355653556635567355683556935570355713557235573355743557535576355773557835579355803558135582355833558435585355863558735588355893559035591355923559335594355953559635597355983559935600356013560235603356043560535606356073560835609356103561135612356133561435615356163561735618356193562035621356223562335624356253562635627356283562935630356313563235633356343563535636356373563835639356403564135642356433564435645356463564735648356493565035651356523565335654356553565635657356583565935660356613566235663356643566535666356673566835669356703567135672356733567435675356763567735678356793568035681356823568335684356853568635687356883568935690356913569235693356943569535696356973569835699357003570135702357033570435705357063570735708357093571035711357123571335714357153571635717357183571935720357213572235723357243572535726357273572835729357303573135732357333573435735357363573735738357393574035741357423574335744357453574635747357483574935750357513575235753357543575535756357573575835759357603576135762357633576435765357663576735768357693577035771357723577335774357753577635777357783577935780357813578235783357843578535786357873578835789357903579135792357933579435795357963579735798357993580035801358023580335804358053580635807358083580935810358113581235813358143581535816358173581835819358203582135822358233582435825358263582735828358293583035831358323583335834358353583635837358383583935840358413584235843358443584535846358473584835849358503585135852358533585435855358563585735858358593586035861358623586335864358653586635867358683586935870358713587235873358743587535876358773587835879358803588135882358833588435885358863588735888358893589035891358923589335894358953589635897358983589935900359013590235903359043590535906359073590835909359103591135912359133591435915359163591735918359193592035921359223592335924359253592635927359283592935930359313593235933359343593535936359373593835939359403594135942359433594435945359463594735948359493595035951359523595335954359553595635957359583595935960359613596235963359643596535966359673596835969359703597135972359733597435975359763597735978359793598035981359823598335984359853598635987359883598935990359913599235993359943599535996359973599835999360003600136002360033600436005360063600736008360093601036011360123601336014360153601636017360183601936020360213602236023360243602536026360273602836029360303603136032360333603436035360363603736038360393604036041360423604336044360453604636047360483604936050360513605236053360543605536056360573605836059360603606136062360633606436065360663606736068360693607036071360723607336074360753607636077360783607936080360813608236083360843608536086360873608836089360903609136092360933609436095360963609736098360993610036101361023610336104361053610636107361083610936110361113611236113361143611536116361173611836119361203612136122361233612436125361263612736128361293613036131361323613336134361353613636137361383613936140361413614236143361443614536146361473614836149361503615136152361533615436155361563615736158361593616036161361623616336164361653616636167361683616936170361713617236173361743617536176361773617836179361803618136182361833618436185361863618736188361893619036191361923619336194361953619636197361983619936200362013620236203362043620536206362073620836209362103621136212362133621436215362163621736218362193622036221362223622336224362253622636227362283622936230362313623236233362343623536236362373623836239362403624136242362433624436245362463624736248362493625036251362523625336254362553625636257362583625936260362613626236263362643626536266362673626836269362703627136272362733627436275362763627736278362793628036281362823628336284362853628636287362883628936290362913629236293362943629536296362973629836299363003630136302363033630436305363063630736308363093631036311363123631336314363153631636317363183631936320363213632236323363243632536326363273632836329363303633136332363333633436335363363633736338363393634036341363423634336344363453634636347363483634936350363513635236353363543635536356363573635836359363603636136362363633636436365363663636736368363693637036371363723637336374363753637636377363783637936380363813638236383363843638536386363873638836389363903639136392363933639436395363963639736398363993640036401364023640336404364053640636407364083640936410364113641236413364143641536416364173641836419364203642136422364233642436425364263642736428364293643036431364323643336434364353643636437364383643936440364413644236443364443644536446364473644836449364503645136452364533645436455364563645736458364593646036461364623646336464364653646636467364683646936470364713647236473364743647536476364773647836479364803648136482364833648436485364863648736488364893649036491364923649336494364953649636497364983649936500365013650236503365043650536506365073650836509365103651136512365133651436515365163651736518365193652036521365223652336524365253652636527365283652936530365313653236533365343653536536365373653836539365403654136542365433654436545365463654736548365493655036551365523655336554365553655636557365583655936560365613656236563365643656536566365673656836569365703657136572365733657436575365763657736578365793658036581365823658336584365853658636587365883658936590365913659236593365943659536596365973659836599366003660136602366033660436605366063660736608366093661036611366123661336614366153661636617366183661936620366213662236623366243662536626366273662836629366303663136632366333663436635366363663736638366393664036641366423664336644366453664636647366483664936650366513665236653366543665536656366573665836659366603666136662366633666436665366663666736668366693667036671366723667336674366753667636677366783667936680366813668236683366843668536686366873668836689366903669136692366933669436695366963669736698366993670036701367023670336704367053670636707367083670936710367113671236713367143671536716367173671836719367203672136722367233672436725367263672736728367293673036731367323673336734367353673636737367383673936740367413674236743367443674536746367473674836749367503675136752367533675436755367563675736758367593676036761367623676336764367653676636767367683676936770367713677236773367743677536776367773677836779367803678136782367833678436785367863678736788367893679036791367923679336794367953679636797367983679936800368013680236803368043680536806368073680836809368103681136812368133681436815368163681736818368193682036821368223682336824368253682636827368283682936830368313683236833368343683536836368373683836839368403684136842368433684436845368463684736848368493685036851368523685336854368553685636857368583685936860368613686236863368643686536866368673686836869368703687136872368733687436875368763687736878368793688036881368823688336884368853688636887368883688936890368913689236893368943689536896368973689836899369003690136902369033690436905369063690736908369093691036911369123691336914369153691636917369183691936920369213692236923369243692536926369273692836929369303693136932369333693436935369363693736938369393694036941369423694336944369453694636947369483694936950369513695236953369543695536956369573695836959369603696136962369633696436965369663696736968369693697036971369723697336974369753697636977369783697936980369813698236983369843698536986369873698836989369903699136992369933699436995369963699736998369993700037001370023700337004370053700637007370083700937010370113701237013370143701537016370173701837019370203702137022370233702437025370263702737028370293703037031370323703337034370353703637037370383703937040370413704237043370443704537046370473704837049370503705137052370533705437055370563705737058370593706037061370623706337064370653706637067370683706937070370713707237073370743707537076370773707837079370803708137082370833708437085370863708737088370893709037091370923709337094370953709637097370983709937100371013710237103371043710537106371073710837109371103711137112371133711437115371163711737118371193712037121371223712337124371253712637127371283712937130371313713237133371343713537136371373713837139371403714137142371433714437145371463714737148371493715037151371523715337154371553715637157371583715937160371613716237163371643716537166371673716837169371703717137172371733717437175371763717737178371793718037181371823718337184371853718637187371883718937190371913719237193371943719537196371973719837199372003720137202372033720437205372063720737208372093721037211372123721337214372153721637217372183721937220372213722237223372243722537226372273722837229372303723137232372333723437235372363723737238372393724037241372423724337244372453724637247372483724937250372513725237253372543725537256372573725837259372603726137262372633726437265372663726737268372693727037271372723727337274372753727637277372783727937280372813728237283372843728537286372873728837289372903729137292372933729437295372963729737298372993730037301373023730337304373053730637307373083730937310373113731237313373143731537316373173731837319373203732137322373233732437325373263732737328373293733037331373323733337334373353733637337373383733937340373413734237343373443734537346373473734837349373503735137352373533735437355373563735737358373593736037361373623736337364373653736637367373683736937370373713737237373373743737537376373773737837379373803738137382373833738437385373863738737388373893739037391373923739337394373953739637397373983739937400374013740237403374043740537406374073740837409374103741137412374133741437415374163741737418374193742037421374223742337424374253742637427374283742937430374313743237433374343743537436374373743837439374403744137442374433744437445374463744737448374493745037451374523745337454374553745637457374583745937460374613746237463374643746537466374673746837469374703747137472374733747437475374763747737478374793748037481374823748337484374853748637487374883748937490374913749237493374943749537496374973749837499375003750137502375033750437505375063750737508375093751037511375123751337514375153751637517375183751937520375213752237523375243752537526375273752837529375303753137532375333753437535375363753737538375393754037541375423754337544375453754637547375483754937550375513755237553375543755537556375573755837559375603756137562375633756437565375663756737568375693757037571375723757337574375753757637577375783757937580375813758237583375843758537586375873758837589375903759137592375933759437595375963759737598375993760037601376023760337604376053760637607376083760937610376113761237613376143761537616376173761837619376203762137622376233762437625376263762737628376293763037631376323763337634376353763637637376383763937640376413764237643376443764537646376473764837649376503765137652376533765437655376563765737658376593766037661376623766337664376653766637667376683766937670376713767237673376743767537676376773767837679376803768137682376833768437685376863768737688376893769037691376923769337694376953769637697376983769937700377013770237703377043770537706377073770837709377103771137712377133771437715377163771737718377193772037721377223772337724377253772637727377283772937730377313773237733377343773537736377373773837739377403774137742377433774437745377463774737748377493775037751377523775337754377553775637757377583775937760377613776237763377643776537766377673776837769377703777137772377733777437775377763777737778377793778037781377823778337784377853778637787377883778937790377913779237793377943779537796377973779837799378003780137802378033780437805378063780737808378093781037811378123781337814378153781637817378183781937820378213782237823378243782537826378273782837829378303783137832378333783437835378363783737838378393784037841378423784337844378453784637847378483784937850378513785237853378543785537856378573785837859378603786137862378633786437865378663786737868378693787037871378723787337874378753787637877378783787937880378813788237883378843788537886378873788837889378903789137892378933789437895378963789737898378993790037901379023790337904379053790637907379083790937910379113791237913379143791537916379173791837919379203792137922379233792437925379263792737928379293793037931379323793337934379353793637937379383793937940379413794237943379443794537946379473794837949379503795137952379533795437955379563795737958379593796037961379623796337964379653796637967379683796937970379713797237973379743797537976379773797837979379803798137982379833798437985379863798737988379893799037991379923799337994379953799637997379983799938000380013800238003380043800538006380073800838009380103801138012380133801438015380163801738018380193802038021380223802338024380253802638027380283802938030380313803238033380343803538036380373803838039380403804138042380433804438045380463804738048380493805038051380523805338054380553805638057380583805938060380613806238063380643806538066380673806838069380703807138072380733807438075380763807738078380793808038081380823808338084380853808638087380883808938090380913809238093380943809538096380973809838099381003810138102381033810438105381063810738108381093811038111381123811338114381153811638117381183811938120381213812238123381243812538126381273812838129381303813138132381333813438135381363813738138381393814038141381423814338144381453814638147381483814938150381513815238153381543815538156381573815838159381603816138162381633816438165381663816738168381693817038171381723817338174381753817638177381783817938180381813818238183381843818538186381873818838189381903819138192381933819438195381963819738198381993820038201382023820338204382053820638207382083820938210382113821238213382143821538216382173821838219382203822138222382233822438225382263822738228382293823038231382323823338234382353823638237382383823938240382413824238243382443824538246382473824838249382503825138252382533825438255382563825738258382593826038261382623826338264382653826638267382683826938270382713827238273382743827538276382773827838279382803828138282382833828438285382863828738288382893829038291382923829338294382953829638297382983829938300383013830238303383043830538306383073830838309383103831138312383133831438315383163831738318383193832038321383223832338324383253832638327383283832938330383313833238333383343833538336383373833838339383403834138342383433834438345383463834738348383493835038351383523835338354383553835638357383583835938360383613836238363383643836538366383673836838369383703837138372383733837438375383763837738378383793838038381383823838338384383853838638387383883838938390383913839238393383943839538396383973839838399384003840138402384033840438405384063840738408384093841038411384123841338414384153841638417384183841938420384213842238423384243842538426384273842838429384303843138432384333843438435384363843738438384393844038441384423844338444384453844638447384483844938450384513845238453384543845538456384573845838459384603846138462384633846438465384663846738468384693847038471384723847338474384753847638477384783847938480384813848238483384843848538486384873848838489384903849138492384933849438495384963849738498384993850038501385023850338504385053850638507385083850938510385113851238513385143851538516385173851838519385203852138522385233852438525385263852738528385293853038531385323853338534385353853638537385383853938540385413854238543385443854538546385473854838549385503855138552385533855438555385563855738558385593856038561385623856338564385653856638567385683856938570385713857238573385743857538576385773857838579385803858138582385833858438585385863858738588385893859038591385923859338594385953859638597385983859938600386013860238603386043860538606386073860838609386103861138612386133861438615386163861738618386193862038621386223862338624386253862638627386283862938630386313863238633386343863538636386373863838639386403864138642386433864438645386463864738648386493865038651386523865338654386553865638657386583865938660386613866238663386643866538666386673866838669386703867138672386733867438675386763867738678386793868038681386823868338684386853868638687386883868938690386913869238693386943869538696386973869838699387003870138702387033870438705387063870738708387093871038711387123871338714387153871638717387183871938720387213872238723387243872538726387273872838729387303873138732387333873438735387363873738738387393874038741387423874338744387453874638747387483874938750387513875238753387543875538756387573875838759387603876138762387633876438765387663876738768387693877038771387723877338774387753877638777387783877938780387813878238783387843878538786387873878838789387903879138792387933879438795387963879738798387993880038801388023880338804388053880638807388083880938810388113881238813388143881538816388173881838819388203882138822388233882438825388263882738828388293883038831388323883338834388353883638837388383883938840388413884238843388443884538846388473884838849388503885138852388533885438855388563885738858388593886038861388623886338864388653886638867388683886938870388713887238873388743887538876388773887838879388803888138882388833888438885388863888738888388893889038891388923889338894388953889638897388983889938900389013890238903389043890538906389073890838909389103891138912389133891438915389163891738918389193892038921389223892338924389253892638927389283892938930389313893238933389343893538936389373893838939389403894138942389433894438945389463894738948389493895038951389523895338954389553895638957389583895938960389613896238963389643896538966389673896838969389703897138972389733897438975389763897738978389793898038981389823898338984389853898638987389883898938990389913899238993389943899538996389973899838999390003900139002390033900439005390063900739008390093901039011390123901339014390153901639017390183901939020390213902239023390243902539026390273902839029390303903139032390333903439035390363903739038390393904039041390423904339044390453904639047390483904939050390513905239053390543905539056390573905839059390603906139062390633906439065390663906739068390693907039071390723907339074390753907639077390783907939080390813908239083390843908539086390873908839089390903909139092390933909439095390963909739098390993910039101391023910339104391053910639107391083910939110391113911239113391143911539116391173911839119391203912139122391233912439125391263912739128391293913039131391323913339134391353913639137391383913939140391413914239143391443914539146391473914839149391503915139152391533915439155391563915739158391593916039161391623916339164391653916639167391683916939170391713917239173391743917539176391773917839179391803918139182391833918439185391863918739188391893919039191391923919339194391953919639197391983919939200392013920239203392043920539206392073920839209392103921139212392133921439215392163921739218392193922039221392223922339224392253922639227392283922939230392313923239233392343923539236392373923839239392403924139242392433924439245392463924739248392493925039251392523925339254392553925639257392583925939260392613926239263392643926539266392673926839269392703927139272392733927439275392763927739278392793928039281392823928339284392853928639287392883928939290392913929239293392943929539296392973929839299393003930139302393033930439305393063930739308393093931039311393123931339314393153931639317393183931939320393213932239323393243932539326393273932839329393303933139332393333933439335393363933739338393393934039341393423934339344393453934639347393483934939350393513935239353393543935539356393573935839359393603936139362393633936439365393663936739368393693937039371393723937339374393753937639377393783937939380393813938239383393843938539386393873938839389393903939139392393933939439395393963939739398393993940039401394023940339404394053940639407394083940939410394113941239413394143941539416394173941839419394203942139422394233942439425394263942739428394293943039431394323943339434394353943639437394383943939440394413944239443394443944539446394473944839449394503945139452394533945439455394563945739458394593946039461394623946339464394653946639467394683946939470394713947239473394743947539476394773947839479394803948139482394833948439485394863948739488394893949039491394923949339494394953949639497394983949939500395013950239503395043950539506395073950839509395103951139512395133951439515395163951739518395193952039521395223952339524395253952639527395283952939530395313953239533395343953539536395373953839539395403954139542395433954439545395463954739548395493955039551395523955339554395553955639557395583955939560395613956239563395643956539566395673956839569395703957139572395733957439575395763957739578395793958039581395823958339584395853958639587395883958939590395913959239593395943959539596395973959839599396003960139602396033960439605396063960739608396093961039611396123961339614396153961639617396183961939620396213962239623396243962539626396273962839629396303963139632396333963439635396363963739638396393964039641396423964339644396453964639647396483964939650396513965239653396543965539656396573965839659396603966139662396633966439665396663966739668396693967039671396723967339674396753967639677396783967939680396813968239683396843968539686396873968839689396903969139692396933969439695396963969739698396993970039701397023970339704397053970639707397083970939710397113971239713397143971539716397173971839719397203972139722397233972439725397263972739728397293973039731397323973339734397353973639737397383973939740397413974239743397443974539746397473974839749397503975139752397533975439755397563975739758397593976039761397623976339764397653976639767397683976939770397713977239773397743977539776397773977839779397803978139782397833978439785397863978739788397893979039791397923979339794397953979639797397983979939800398013980239803398043980539806398073980839809398103981139812398133981439815398163981739818398193982039821398223982339824398253982639827398283982939830398313983239833398343983539836398373983839839398403984139842398433984439845398463984739848398493985039851398523985339854398553985639857398583985939860398613986239863398643986539866398673986839869398703987139872398733987439875398763987739878398793988039881398823988339884398853988639887398883988939890398913989239893398943989539896398973989839899399003990139902399033990439905399063990739908399093991039911399123991339914399153991639917399183991939920399213992239923399243992539926399273992839929399303993139932399333993439935399363993739938399393994039941399423994339944399453994639947399483994939950399513995239953399543995539956399573995839959399603996139962399633996439965399663996739968399693997039971399723997339974399753997639977399783997939980399813998239983399843998539986399873998839989399903999139992399933999439995399963999739998399994000040001400024000340004400054000640007400084000940010400114001240013400144001540016400174001840019400204002140022400234002440025400264002740028400294003040031400324003340034400354003640037400384003940040400414004240043400444004540046400474004840049400504005140052400534005440055400564005740058400594006040061400624006340064400654006640067400684006940070400714007240073400744007540076400774007840079400804008140082400834008440085400864008740088400894009040091400924009340094400954009640097400984009940100401014010240103401044010540106401074010840109401104011140112401134011440115401164011740118401194012040121401224012340124401254012640127401284012940130401314013240133401344013540136401374013840139401404014140142401434014440145401464014740148401494015040151401524015340154401554015640157401584015940160401614016240163401644016540166401674016840169401704017140172401734017440175401764017740178401794018040181401824018340184401854018640187401884018940190401914019240193401944019540196401974019840199402004020140202402034020440205402064020740208402094021040211402124021340214402154021640217402184021940220402214022240223402244022540226402274022840229402304023140232402334023440235402364023740238402394024040241402424024340244402454024640247402484024940250402514025240253402544025540256402574025840259402604026140262402634026440265402664026740268402694027040271402724027340274402754027640277402784027940280402814028240283402844028540286402874028840289402904029140292402934029440295402964029740298402994030040301403024030340304403054030640307403084030940310403114031240313403144031540316403174031840319403204032140322403234032440325403264032740328403294033040331403324033340334403354033640337403384033940340403414034240343403444034540346403474034840349403504035140352403534035440355403564035740358403594036040361403624036340364403654036640367403684036940370403714037240373403744037540376403774037840379403804038140382403834038440385403864038740388403894039040391403924039340394403954039640397403984039940400404014040240403404044040540406404074040840409404104041140412404134041440415404164041740418404194042040421404224042340424404254042640427404284042940430404314043240433404344043540436404374043840439404404044140442404434044440445404464044740448404494045040451404524045340454404554045640457404584045940460404614046240463404644046540466404674046840469404704047140472404734047440475404764047740478404794048040481404824048340484404854048640487404884048940490404914049240493404944049540496404974049840499405004050140502405034050440505405064050740508405094051040511405124051340514405154051640517405184051940520405214052240523405244052540526405274052840529405304053140532405334053440535405364053740538405394054040541405424054340544405454054640547405484054940550405514055240553405544055540556405574055840559405604056140562405634056440565405664056740568405694057040571405724057340574405754057640577405784057940580405814058240583405844058540586405874058840589405904059140592405934059440595405964059740598405994060040601406024060340604406054060640607406084060940610406114061240613406144061540616406174061840619406204062140622406234062440625406264062740628406294063040631406324063340634406354063640637406384063940640406414064240643406444064540646406474064840649406504065140652406534065440655406564065740658406594066040661406624066340664406654066640667406684066940670406714067240673406744067540676406774067840679406804068140682406834068440685406864068740688406894069040691406924069340694406954069640697406984069940700407014070240703407044070540706407074070840709407104071140712407134071440715407164071740718407194072040721407224072340724407254072640727407284072940730407314073240733407344073540736407374073840739407404074140742407434074440745407464074740748407494075040751407524075340754407554075640757407584075940760407614076240763407644076540766407674076840769407704077140772407734077440775407764077740778407794078040781407824078340784407854078640787407884078940790407914079240793407944079540796407974079840799408004080140802408034080440805408064080740808408094081040811408124081340814408154081640817408184081940820408214082240823408244082540826408274082840829408304083140832408334083440835408364083740838408394084040841408424084340844408454084640847408484084940850408514085240853408544085540856408574085840859408604086140862408634086440865408664086740868408694087040871408724087340874408754087640877408784087940880408814088240883408844088540886408874088840889408904089140892408934089440895408964089740898408994090040901409024090340904409054090640907409084090940910409114091240913409144091540916409174091840919409204092140922409234092440925409264092740928409294093040931409324093340934409354093640937409384093940940409414094240943409444094540946409474094840949409504095140952409534095440955409564095740958409594096040961409624096340964409654096640967409684096940970409714097240973409744097540976409774097840979409804098140982409834098440985409864098740988409894099040991409924099340994409954099640997409984099941000410014100241003410044100541006410074100841009410104101141012410134101441015410164101741018410194102041021410224102341024410254102641027410284102941030410314103241033410344103541036410374103841039410404104141042410434104441045410464104741048410494105041051410524105341054410554105641057410584105941060410614106241063410644106541066410674106841069410704107141072410734107441075410764107741078410794108041081410824108341084410854108641087410884108941090410914109241093410944109541096410974109841099411004110141102411034110441105411064110741108411094111041111411124111341114411154111641117411184111941120411214112241123411244112541126411274112841129411304113141132411334113441135411364113741138411394114041141411424114341144411454114641147411484114941150411514115241153411544115541156411574115841159411604116141162411634116441165411664116741168411694117041171411724117341174411754117641177411784117941180411814118241183411844118541186411874118841189411904119141192411934119441195411964119741198411994120041201412024120341204412054120641207412084120941210412114121241213412144121541216412174121841219412204122141222412234122441225412264122741228412294123041231412324123341234412354123641237412384123941240412414124241243412444124541246412474124841249412504125141252412534125441255412564125741258412594126041261412624126341264412654126641267412684126941270412714127241273412744127541276412774127841279412804128141282412834128441285412864128741288412894129041291412924129341294412954129641297412984129941300413014130241303413044130541306413074130841309413104131141312413134131441315413164131741318413194132041321413224132341324413254132641327413284132941330413314133241333413344133541336413374133841339413404134141342413434134441345413464134741348413494135041351413524135341354413554135641357413584135941360413614136241363413644136541366413674136841369413704137141372413734137441375413764137741378413794138041381413824138341384413854138641387413884138941390413914139241393413944139541396413974139841399414004140141402414034140441405414064140741408414094141041411414124141341414414154141641417414184141941420414214142241423414244142541426414274142841429414304143141432414334143441435414364143741438414394144041441414424144341444414454144641447414484144941450414514145241453414544145541456414574145841459414604146141462414634146441465414664146741468414694147041471414724147341474414754147641477414784147941480414814148241483414844148541486414874148841489414904149141492414934149441495414964149741498414994150041501415024150341504415054150641507415084150941510415114151241513415144151541516415174151841519415204152141522415234152441525415264152741528415294153041531415324153341534415354153641537415384153941540415414154241543415444154541546415474154841549415504155141552415534155441555415564155741558415594156041561415624156341564415654156641567415684156941570415714157241573415744157541576415774157841579415804158141582415834158441585415864158741588415894159041591415924159341594415954159641597415984159941600416014160241603416044160541606416074160841609416104161141612416134161441615416164161741618416194162041621416224162341624416254162641627416284162941630416314163241633416344163541636416374163841639416404164141642416434164441645416464164741648416494165041651416524165341654416554165641657416584165941660416614166241663416644166541666416674166841669416704167141672416734167441675416764167741678416794168041681416824168341684416854168641687416884168941690416914169241693416944169541696416974169841699417004170141702417034170441705417064170741708417094171041711417124171341714417154171641717417184171941720417214172241723417244172541726417274172841729417304173141732417334173441735417364173741738417394174041741417424174341744417454174641747417484174941750417514175241753417544175541756417574175841759417604176141762417634176441765417664176741768417694177041771417724177341774417754177641777417784177941780417814178241783417844178541786417874178841789417904179141792417934179441795417964179741798417994180041801418024180341804418054180641807418084180941810418114181241813418144181541816418174181841819418204182141822418234182441825418264182741828418294183041831418324183341834418354183641837418384183941840418414184241843418444184541846418474184841849418504185141852418534185441855418564185741858418594186041861418624186341864418654186641867418684186941870418714187241873418744187541876418774187841879418804188141882418834188441885418864188741888418894189041891418924189341894418954189641897418984189941900419014190241903419044190541906419074190841909419104191141912419134191441915419164191741918419194192041921419224192341924419254192641927419284192941930419314193241933419344193541936419374193841939419404194141942419434194441945419464194741948419494195041951419524195341954419554195641957419584195941960419614196241963419644196541966419674196841969419704197141972419734197441975419764197741978419794198041981419824198341984419854198641987419884198941990419914199241993419944199541996419974199841999420004200142002420034200442005420064200742008420094201042011420124201342014420154201642017420184201942020420214202242023420244202542026420274202842029420304203142032420334203442035420364203742038420394204042041420424204342044420454204642047420484204942050420514205242053420544205542056420574205842059420604206142062420634206442065420664206742068420694207042071420724207342074420754207642077420784207942080420814208242083420844208542086420874208842089420904209142092420934209442095420964209742098420994210042101421024210342104421054210642107421084210942110421114211242113421144211542116421174211842119421204212142122421234212442125421264212742128421294213042131421324213342134421354213642137421384213942140421414214242143421444214542146421474214842149421504215142152421534215442155421564215742158421594216042161421624216342164421654216642167421684216942170421714217242173421744217542176421774217842179421804218142182421834218442185421864218742188421894219042191421924219342194421954219642197421984219942200422014220242203422044220542206422074220842209422104221142212422134221442215422164221742218422194222042221422224222342224422254222642227422284222942230422314223242233422344223542236422374223842239422404224142242422434224442245422464224742248422494225042251422524225342254422554225642257422584225942260422614226242263422644226542266422674226842269422704227142272422734227442275422764227742278422794228042281422824228342284422854228642287422884228942290422914229242293422944229542296422974229842299423004230142302423034230442305423064230742308423094231042311423124231342314423154231642317423184231942320423214232242323423244232542326423274232842329423304233142332423334233442335423364233742338423394234042341423424234342344423454234642347423484234942350423514235242353423544235542356423574235842359423604236142362423634236442365423664236742368423694237042371423724237342374423754237642377423784237942380423814238242383423844238542386423874238842389423904239142392423934239442395423964239742398423994240042401424024240342404424054240642407424084240942410424114241242413424144241542416424174241842419424204242142422424234242442425424264242742428424294243042431424324243342434424354243642437424384243942440424414244242443424444244542446424474244842449424504245142452424534245442455424564245742458424594246042461424624246342464424654246642467424684246942470424714247242473424744247542476424774247842479424804248142482424834248442485424864248742488424894249042491424924249342494424954249642497424984249942500425014250242503425044250542506425074250842509425104251142512425134251442515425164251742518425194252042521425224252342524425254252642527425284252942530425314253242533425344253542536425374253842539425404254142542425434254442545425464254742548425494255042551425524255342554425554255642557425584255942560425614256242563425644256542566425674256842569425704257142572425734257442575425764257742578425794258042581425824258342584425854258642587425884258942590425914259242593425944259542596425974259842599426004260142602426034260442605426064260742608426094261042611426124261342614426154261642617426184261942620426214262242623426244262542626426274262842629426304263142632426334263442635426364263742638426394264042641426424264342644426454264642647426484264942650426514265242653426544265542656426574265842659426604266142662426634266442665426664266742668426694267042671426724267342674426754267642677426784267942680426814268242683426844268542686426874268842689426904269142692426934269442695426964269742698426994270042701427024270342704427054270642707427084270942710427114271242713427144271542716427174271842719427204272142722427234272442725427264272742728427294273042731427324273342734427354273642737427384273942740427414274242743427444274542746427474274842749427504275142752427534275442755427564275742758427594276042761427624276342764427654276642767427684276942770427714277242773427744277542776427774277842779427804278142782427834278442785427864278742788427894279042791427924279342794427954279642797427984279942800428014280242803428044280542806428074280842809428104281142812428134281442815428164281742818428194282042821428224282342824428254282642827428284282942830428314283242833428344283542836428374283842839428404284142842428434284442845428464284742848428494285042851428524285342854428554285642857428584285942860428614286242863428644286542866428674286842869428704287142872428734287442875428764287742878428794288042881428824288342884428854288642887428884288942890428914289242893428944289542896428974289842899429004290142902429034290442905429064290742908429094291042911429124291342914429154291642917429184291942920429214292242923429244292542926429274292842929429304293142932429334293442935429364293742938429394294042941429424294342944429454294642947429484294942950429514295242953429544295542956429574295842959429604296142962429634296442965429664296742968429694297042971429724297342974429754297642977429784297942980429814298242983429844298542986429874298842989429904299142992429934299442995429964299742998429994300043001430024300343004430054300643007430084300943010430114301243013430144301543016430174301843019430204302143022430234302443025430264302743028430294303043031430324303343034430354303643037430384303943040430414304243043430444304543046430474304843049430504305143052430534305443055430564305743058430594306043061430624306343064430654306643067430684306943070430714307243073430744307543076430774307843079430804308143082430834308443085430864308743088430894309043091430924309343094430954309643097430984309943100431014310243103431044310543106431074310843109431104311143112431134311443115431164311743118431194312043121431224312343124431254312643127431284312943130431314313243133431344313543136431374313843139431404314143142431434314443145431464314743148431494315043151431524315343154431554315643157431584315943160431614316243163431644316543166431674316843169431704317143172431734317443175431764317743178431794318043181431824318343184431854318643187431884318943190431914319243193431944319543196431974319843199432004320143202432034320443205432064320743208432094321043211432124321343214432154321643217432184321943220432214322243223432244322543226432274322843229432304323143232432334323443235432364323743238432394324043241432424324343244432454324643247432484324943250432514325243253432544325543256432574325843259432604326143262432634326443265432664326743268432694327043271432724327343274432754327643277432784327943280432814328243283432844328543286432874328843289432904329143292432934329443295432964329743298432994330043301433024330343304433054330643307433084330943310433114331243313433144331543316433174331843319433204332143322433234332443325433264332743328433294333043331433324333343334433354333643337433384333943340433414334243343433444334543346433474334843349433504335143352433534335443355433564335743358433594336043361433624336343364433654336643367433684336943370433714337243373433744337543376433774337843379433804338143382433834338443385433864338743388433894339043391433924339343394433954339643397433984339943400434014340243403434044340543406434074340843409434104341143412434134341443415434164341743418434194342043421434224342343424434254342643427434284342943430434314343243433434344343543436434374343843439434404344143442434434344443445434464344743448434494345043451434524345343454434554345643457434584345943460434614346243463434644346543466434674346843469434704347143472434734347443475434764347743478434794348043481434824348343484434854348643487434884348943490434914349243493434944349543496434974349843499435004350143502435034350443505435064350743508435094351043511435124351343514435154351643517435184351943520435214352243523435244352543526435274352843529435304353143532435334353443535435364353743538435394354043541435424354343544435454354643547435484354943550435514355243553435544355543556435574355843559435604356143562435634356443565435664356743568435694357043571435724357343574435754357643577435784357943580435814358243583435844358543586435874358843589435904359143592435934359443595435964359743598435994360043601436024360343604436054360643607436084360943610436114361243613436144361543616436174361843619436204362143622436234362443625436264362743628436294363043631436324363343634436354363643637436384363943640436414364243643436444364543646436474364843649436504365143652436534365443655436564365743658436594366043661436624366343664436654366643667436684366943670436714367243673436744367543676436774367843679436804368143682436834368443685436864368743688436894369043691436924369343694436954369643697436984369943700437014370243703437044370543706437074370843709437104371143712437134371443715437164371743718437194372043721437224372343724437254372643727437284372943730437314373243733437344373543736437374373843739437404374143742437434374443745437464374743748437494375043751437524375343754437554375643757437584375943760437614376243763437644376543766437674376843769437704377143772437734377443775437764377743778437794378043781437824378343784437854378643787437884378943790437914379243793437944379543796437974379843799438004380143802438034380443805438064380743808438094381043811438124381343814438154381643817438184381943820438214382243823438244382543826438274382843829438304383143832438334383443835438364383743838438394384043841438424384343844438454384643847438484384943850438514385243853438544385543856438574385843859438604386143862438634386443865438664386743868438694387043871438724387343874438754387643877438784387943880438814388243883438844388543886438874388843889438904389143892438934389443895438964389743898438994390043901439024390343904439054390643907439084390943910439114391243913439144391543916439174391843919439204392143922439234392443925439264392743928439294393043931439324393343934439354393643937439384393943940439414394243943439444394543946439474394843949439504395143952439534395443955439564395743958439594396043961439624396343964439654396643967439684396943970439714397243973439744397543976439774397843979439804398143982439834398443985439864398743988439894399043991439924399343994439954399643997439984399944000440014400244003440044400544006440074400844009440104401144012440134401444015440164401744018440194402044021440224402344024440254402644027440284402944030440314403244033440344403544036440374403844039440404404144042440434404444045440464404744048440494405044051440524405344054440554405644057440584405944060440614406244063440644406544066440674406844069440704407144072440734407444075440764407744078440794408044081440824408344084440854408644087440884408944090440914409244093440944409544096440974409844099441004410144102441034410444105441064410744108441094411044111441124411344114441154411644117441184411944120441214412244123441244412544126441274412844129441304413144132441334413444135441364413744138441394414044141441424414344144441454414644147441484414944150441514415244153441544415544156441574415844159441604416144162441634416444165441664416744168441694417044171441724417344174441754417644177441784417944180441814418244183441844418544186441874418844189441904419144192441934419444195441964419744198441994420044201442024420344204442054420644207442084420944210442114421244213442144421544216442174421844219442204422144222442234422444225442264422744228442294423044231442324423344234442354423644237442384423944240442414424244243442444424544246442474424844249442504425144252442534425444255442564425744258442594426044261442624426344264442654426644267442684426944270442714427244273442744427544276442774427844279442804428144282442834428444285442864428744288442894429044291442924429344294442954429644297442984429944300443014430244303443044430544306443074430844309443104431144312443134431444315443164431744318443194432044321443224432344324443254432644327443284432944330443314433244333443344433544336443374433844339443404434144342443434434444345443464434744348443494435044351443524435344354443554435644357443584435944360443614436244363443644436544366443674436844369443704437144372443734437444375443764437744378443794438044381443824438344384443854438644387443884438944390443914439244393443944439544396443974439844399444004440144402444034440444405444064440744408444094441044411444124441344414444154441644417444184441944420444214442244423444244442544426444274442844429444304443144432444334443444435444364443744438444394444044441444424444344444444454444644447444484444944450444514445244453444544445544456444574445844459444604446144462444634446444465444664446744468444694447044471444724447344474444754447644477444784447944480444814448244483444844448544486444874448844489444904449144492444934449444495444964449744498444994450044501445024450344504445054450644507445084450944510445114451244513445144451544516445174451844519445204452144522445234452444525445264452744528445294453044531445324453344534445354453644537445384453944540445414454244543445444454544546445474454844549445504455144552445534455444555445564455744558445594456044561445624456344564445654456644567445684456944570445714457244573445744457544576445774457844579445804458144582445834458444585445864458744588445894459044591445924459344594445954459644597445984459944600446014460244603446044460544606446074460844609446104461144612446134461444615446164461744618446194462044621446224462344624446254462644627446284462944630446314463244633446344463544636446374463844639446404464144642446434464444645446464464744648446494465044651446524465344654446554465644657446584465944660446614466244663446644466544666446674466844669446704467144672446734467444675446764467744678446794468044681446824468344684446854468644687446884468944690446914469244693446944469544696446974469844699447004470144702447034470444705447064470744708447094471044711447124471344714447154471644717447184471944720447214472244723447244472544726447274472844729447304473144732447334473444735447364473744738447394474044741447424474344744447454474644747447484474944750447514475244753447544475544756447574475844759447604476144762447634476444765447664476744768447694477044771447724477344774447754477644777447784477944780447814478244783447844478544786447874478844789447904479144792447934479444795447964479744798447994480044801448024480344804448054480644807448084480944810448114481244813448144481544816448174481844819448204482144822448234482444825448264482744828448294483044831448324483344834448354483644837448384483944840448414484244843448444484544846448474484844849448504485144852448534485444855448564485744858448594486044861448624486344864448654486644867448684486944870448714487244873448744487544876448774487844879448804488144882448834488444885448864488744888448894489044891448924489344894448954489644897448984489944900449014490244903449044490544906449074490844909449104491144912449134491444915449164491744918449194492044921449224492344924449254492644927449284492944930449314493244933449344493544936449374493844939449404494144942449434494444945449464494744948449494495044951449524495344954449554495644957449584495944960449614496244963449644496544966449674496844969449704497144972449734497444975449764497744978449794498044981449824498344984449854498644987449884498944990449914499244993449944499544996449974499844999450004500145002450034500445005450064500745008450094501045011450124501345014450154501645017450184501945020450214502245023450244502545026450274502845029450304503145032450334503445035450364503745038450394504045041450424504345044450454504645047450484504945050450514505245053450544505545056450574505845059450604506145062450634506445065450664506745068450694507045071450724507345074450754507645077450784507945080450814508245083450844508545086450874508845089450904509145092450934509445095450964509745098450994510045101451024510345104451054510645107451084510945110451114511245113451144511545116451174511845119451204512145122451234512445125451264512745128451294513045131451324513345134451354513645137451384513945140451414514245143451444514545146451474514845149451504515145152451534515445155451564515745158451594516045161451624516345164451654516645167451684516945170451714517245173451744517545176451774517845179451804518145182451834518445185451864518745188451894519045191451924519345194451954519645197451984519945200452014520245203452044520545206452074520845209452104521145212452134521445215452164521745218452194522045221452224522345224452254522645227452284522945230452314523245233452344523545236452374523845239452404524145242452434524445245452464524745248452494525045251452524525345254452554525645257452584525945260452614526245263452644526545266452674526845269452704527145272452734527445275452764527745278452794528045281452824528345284452854528645287452884528945290452914529245293452944529545296452974529845299453004530145302453034530445305453064530745308453094531045311453124531345314453154531645317453184531945320453214532245323453244532545326453274532845329453304533145332453334533445335453364533745338453394534045341453424534345344453454534645347453484534945350453514535245353453544535545356453574535845359453604536145362453634536445365453664536745368453694537045371453724537345374453754537645377453784537945380453814538245383453844538545386453874538845389453904539145392453934539445395453964539745398453994540045401454024540345404454054540645407454084540945410454114541245413454144541545416454174541845419454204542145422454234542445425454264542745428454294543045431454324543345434454354543645437454384543945440454414544245443454444544545446454474544845449454504545145452454534545445455454564545745458454594546045461454624546345464454654546645467454684546945470454714547245473454744547545476454774547845479454804548145482454834548445485454864548745488454894549045491454924549345494454954549645497454984549945500455014550245503455044550545506455074550845509455104551145512455134551445515455164551745518455194552045521455224552345524455254552645527455284552945530455314553245533455344553545536455374553845539455404554145542455434554445545455464554745548455494555045551455524555345554455554555645557455584555945560455614556245563455644556545566455674556845569455704557145572455734557445575455764557745578455794558045581455824558345584455854558645587455884558945590455914559245593455944559545596455974559845599456004560145602456034560445605456064560745608456094561045611456124561345614456154561645617456184561945620456214562245623456244562545626456274562845629456304563145632456334563445635456364563745638456394564045641456424564345644456454564645647456484564945650456514565245653456544565545656456574565845659456604566145662456634566445665456664566745668456694567045671456724567345674456754567645677456784567945680456814568245683456844568545686456874568845689456904569145692456934569445695456964569745698456994570045701457024570345704457054570645707457084570945710457114571245713457144571545716457174571845719457204572145722457234572445725457264572745728457294573045731457324573345734457354573645737457384573945740457414574245743457444574545746457474574845749457504575145752457534575445755457564575745758457594576045761457624576345764457654576645767457684576945770457714577245773457744577545776457774577845779457804578145782457834578445785457864578745788457894579045791457924579345794457954579645797457984579945800458014580245803458044580545806458074580845809458104581145812458134581445815458164581745818458194582045821458224582345824458254582645827458284582945830458314583245833458344583545836458374583845839458404584145842458434584445845458464584745848458494585045851458524585345854458554585645857458584585945860458614586245863458644586545866458674586845869458704587145872458734587445875458764587745878458794588045881458824588345884458854588645887458884588945890458914589245893458944589545896458974589845899459004590145902459034590445905459064590745908459094591045911459124591345914459154591645917459184591945920459214592245923459244592545926459274592845929459304593145932459334593445935459364593745938459394594045941459424594345944459454594645947459484594945950459514595245953459544595545956459574595845959459604596145962459634596445965459664596745968459694597045971459724597345974459754597645977459784597945980459814598245983459844598545986459874598845989459904599145992459934599445995459964599745998459994600046001460024600346004460054600646007460084600946010460114601246013460144601546016460174601846019460204602146022460234602446025460264602746028460294603046031460324603346034460354603646037460384603946040460414604246043460444604546046460474604846049460504605146052460534605446055460564605746058460594606046061460624606346064460654606646067460684606946070460714607246073460744607546076460774607846079460804608146082460834608446085460864608746088460894609046091460924609346094460954609646097460984609946100461014610246103461044610546106461074610846109461104611146112461134611446115461164611746118461194612046121461224612346124461254612646127461284612946130461314613246133461344613546136461374613846139461404614146142461434614446145461464614746148461494615046151461524615346154461554615646157461584615946160461614616246163461644616546166461674616846169461704617146172461734617446175461764617746178461794618046181461824618346184461854618646187461884618946190461914619246193461944619546196461974619846199462004620146202462034620446205462064620746208462094621046211462124621346214462154621646217462184621946220462214622246223462244622546226462274622846229462304623146232462334623446235462364623746238462394624046241462424624346244462454624646247462484624946250462514625246253462544625546256462574625846259462604626146262462634626446265462664626746268462694627046271462724627346274462754627646277462784627946280462814628246283462844628546286462874628846289462904629146292462934629446295462964629746298462994630046301463024630346304463054630646307463084630946310463114631246313463144631546316463174631846319463204632146322463234632446325463264632746328463294633046331463324633346334463354633646337463384633946340463414634246343463444634546346463474634846349463504635146352463534635446355463564635746358463594636046361463624636346364463654636646367463684636946370463714637246373463744637546376463774637846379463804638146382463834638446385463864638746388463894639046391463924639346394463954639646397463984639946400464014640246403464044640546406464074640846409464104641146412464134641446415464164641746418464194642046421464224642346424464254642646427464284642946430464314643246433464344643546436464374643846439464404644146442464434644446445464464644746448464494645046451464524645346454464554645646457464584645946460464614646246463464644646546466464674646846469464704647146472464734647446475464764647746478464794648046481464824648346484464854648646487464884648946490464914649246493464944649546496464974649846499465004650146502465034650446505465064650746508465094651046511465124651346514465154651646517465184651946520465214652246523465244652546526465274652846529465304653146532465334653446535465364653746538465394654046541465424654346544465454654646547465484654946550465514655246553465544655546556465574655846559465604656146562465634656446565465664656746568465694657046571465724657346574465754657646577465784657946580465814658246583465844658546586465874658846589465904659146592465934659446595465964659746598465994660046601466024660346604466054660646607466084660946610466114661246613466144661546616466174661846619466204662146622466234662446625466264662746628466294663046631466324663346634466354663646637466384663946640466414664246643466444664546646466474664846649466504665146652466534665446655466564665746658466594666046661466624666346664466654666646667466684666946670466714667246673466744667546676466774667846679466804668146682466834668446685466864668746688466894669046691466924669346694466954669646697466984669946700467014670246703467044670546706467074670846709467104671146712467134671446715467164671746718467194672046721467224672346724467254672646727467284672946730467314673246733467344673546736467374673846739467404674146742467434674446745467464674746748467494675046751467524675346754467554675646757467584675946760467614676246763467644676546766467674676846769467704677146772467734677446775467764677746778467794678046781467824678346784467854678646787467884678946790467914679246793467944679546796467974679846799468004680146802468034680446805468064680746808468094681046811468124681346814468154681646817468184681946820468214682246823468244682546826468274682846829468304683146832468334683446835468364683746838468394684046841468424684346844468454684646847468484684946850468514685246853468544685546856468574685846859468604686146862468634686446865468664686746868468694687046871468724687346874468754687646877468784687946880468814688246883468844688546886468874688846889468904689146892468934689446895468964689746898468994690046901469024690346904469054690646907469084690946910469114691246913469144691546916469174691846919469204692146922469234692446925469264692746928469294693046931469324693346934469354693646937469384693946940469414694246943469444694546946469474694846949469504695146952469534695446955469564695746958469594696046961469624696346964469654696646967469684696946970469714697246973469744697546976469774697846979469804698146982469834698446985469864698746988469894699046991469924699346994469954699646997469984699947000470014700247003470044700547006470074700847009470104701147012470134701447015470164701747018470194702047021470224702347024470254702647027470284702947030470314703247033470344703547036470374703847039470404704147042470434704447045470464704747048470494705047051470524705347054470554705647057470584705947060470614706247063470644706547066470674706847069470704707147072470734707447075470764707747078470794708047081470824708347084470854708647087470884708947090470914709247093470944709547096470974709847099471004710147102471034710447105471064710747108471094711047111471124711347114471154711647117471184711947120471214712247123471244712547126471274712847129471304713147132471334713447135471364713747138471394714047141471424714347144471454714647147471484714947150471514715247153471544715547156471574715847159471604716147162471634716447165471664716747168471694717047171471724717347174471754717647177471784717947180471814718247183471844718547186471874718847189471904719147192471934719447195471964719747198471994720047201472024720347204472054720647207472084720947210472114721247213472144721547216472174721847219472204722147222472234722447225472264722747228472294723047231472324723347234472354723647237472384723947240472414724247243472444724547246472474724847249472504725147252472534725447255472564725747258472594726047261472624726347264472654726647267472684726947270472714727247273472744727547276472774727847279472804728147282472834728447285472864728747288472894729047291472924729347294472954729647297472984729947300473014730247303473044730547306473074730847309473104731147312473134731447315473164731747318473194732047321473224732347324473254732647327473284732947330473314733247333473344733547336473374733847339473404734147342473434734447345473464734747348473494735047351473524735347354473554735647357473584735947360473614736247363473644736547366473674736847369473704737147372473734737447375473764737747378473794738047381473824738347384473854738647387473884738947390473914739247393473944739547396473974739847399474004740147402474034740447405474064740747408474094741047411474124741347414474154741647417474184741947420474214742247423474244742547426474274742847429474304743147432474334743447435474364743747438474394744047441474424744347444474454744647447474484744947450474514745247453474544745547456474574745847459474604746147462474634746447465474664746747468474694747047471474724747347474474754747647477474784747947480474814748247483474844748547486474874748847489474904749147492474934749447495474964749747498474994750047501475024750347504475054750647507475084750947510475114751247513475144751547516475174751847519475204752147522475234752447525475264752747528475294753047531475324753347534475354753647537475384753947540475414754247543475444754547546475474754847549475504755147552475534755447555475564755747558475594756047561475624756347564475654756647567475684756947570475714757247573475744757547576475774757847579475804758147582475834758447585475864758747588475894759047591475924759347594475954759647597475984759947600476014760247603476044760547606476074760847609476104761147612476134761447615476164761747618476194762047621476224762347624476254762647627476284762947630476314763247633476344763547636476374763847639476404764147642476434764447645476464764747648476494765047651476524765347654476554765647657476584765947660476614766247663476644766547666476674766847669476704767147672476734767447675476764767747678476794768047681476824768347684476854768647687476884768947690476914769247693476944769547696476974769847699477004770147702477034770447705477064770747708477094771047711477124771347714477154771647717477184771947720477214772247723477244772547726477274772847729477304773147732477334773447735477364773747738477394774047741477424774347744477454774647747477484774947750477514775247753477544775547756477574775847759477604776147762477634776447765477664776747768477694777047771477724777347774477754777647777477784777947780477814778247783477844778547786477874778847789477904779147792477934779447795477964779747798477994780047801478024780347804478054780647807478084780947810478114781247813478144781547816478174781847819478204782147822478234782447825478264782747828478294783047831478324783347834478354783647837478384783947840478414784247843478444784547846478474784847849478504785147852478534785447855478564785747858478594786047861478624786347864478654786647867478684786947870478714787247873478744787547876478774787847879478804788147882478834788447885478864788747888478894789047891478924789347894478954789647897478984789947900479014790247903479044790547906479074790847909479104791147912479134791447915479164791747918479194792047921479224792347924479254792647927479284792947930479314793247933479344793547936479374793847939479404794147942479434794447945479464794747948479494795047951479524795347954479554795647957479584795947960479614796247963479644796547966479674796847969479704797147972479734797447975479764797747978479794798047981479824798347984479854798647987479884798947990479914799247993479944799547996479974799847999480004800148002480034800448005480064800748008480094801048011480124801348014480154801648017480184801948020480214802248023480244802548026480274802848029480304803148032480334803448035480364803748038480394804048041480424804348044480454804648047480484804948050480514805248053480544805548056480574805848059480604806148062480634806448065480664806748068480694807048071480724807348074480754807648077480784807948080480814808248083480844808548086480874808848089480904809148092480934809448095480964809748098480994810048101481024810348104481054810648107481084810948110481114811248113481144811548116481174811848119481204812148122481234812448125481264812748128481294813048131481324813348134481354813648137481384813948140481414814248143481444814548146481474814848149481504815148152481534815448155481564815748158481594816048161481624816348164481654816648167481684816948170481714817248173481744817548176481774817848179481804818148182481834818448185481864818748188481894819048191481924819348194481954819648197481984819948200482014820248203482044820548206482074820848209482104821148212482134821448215482164821748218482194822048221482224822348224482254822648227482284822948230482314823248233482344823548236482374823848239482404824148242482434824448245482464824748248482494825048251482524825348254482554825648257482584825948260482614826248263482644826548266482674826848269482704827148272482734827448275482764827748278482794828048281482824828348284482854828648287482884828948290482914829248293482944829548296482974829848299483004830148302483034830448305483064830748308483094831048311483124831348314483154831648317483184831948320483214832248323483244832548326483274832848329483304833148332483334833448335483364833748338483394834048341483424834348344483454834648347483484834948350483514835248353483544835548356483574835848359483604836148362483634836448365483664836748368483694837048371483724837348374483754837648377483784837948380483814838248383483844838548386483874838848389483904839148392483934839448395483964839748398483994840048401484024840348404484054840648407484084840948410484114841248413484144841548416484174841848419484204842148422484234842448425484264842748428484294843048431484324843348434484354843648437484384843948440484414844248443484444844548446484474844848449484504845148452484534845448455484564845748458484594846048461484624846348464484654846648467484684846948470484714847248473484744847548476484774847848479484804848148482484834848448485484864848748488484894849048491484924849348494484954849648497484984849948500485014850248503485044850548506485074850848509485104851148512485134851448515485164851748518485194852048521485224852348524485254852648527485284852948530485314853248533485344853548536485374853848539485404854148542485434854448545485464854748548485494855048551485524855348554485554855648557485584855948560485614856248563485644856548566485674856848569485704857148572485734857448575485764857748578485794858048581485824858348584485854858648587485884858948590485914859248593485944859548596485974859848599486004860148602486034860448605486064860748608486094861048611486124861348614486154861648617486184861948620486214862248623486244862548626486274862848629486304863148632486334863448635486364863748638486394864048641486424864348644486454864648647486484864948650486514865248653486544865548656486574865848659486604866148662486634866448665486664866748668486694867048671486724867348674486754867648677486784867948680486814868248683486844868548686486874868848689486904869148692486934869448695486964869748698486994870048701487024870348704487054870648707487084870948710487114871248713487144871548716487174871848719487204872148722487234872448725487264872748728487294873048731487324873348734487354873648737487384873948740487414874248743487444874548746487474874848749487504875148752487534875448755487564875748758487594876048761487624876348764487654876648767487684876948770487714877248773487744877548776487774877848779487804878148782487834878448785487864878748788487894879048791487924879348794487954879648797487984879948800488014880248803488044880548806488074880848809488104881148812488134881448815488164881748818488194882048821488224882348824488254882648827488284882948830488314883248833488344883548836488374883848839488404884148842488434884448845488464884748848488494885048851488524885348854488554885648857488584885948860488614886248863488644886548866488674886848869488704887148872488734887448875488764887748878488794888048881488824888348884488854888648887488884888948890488914889248893488944889548896488974889848899489004890148902489034890448905489064890748908489094891048911489124891348914489154891648917489184891948920489214892248923489244892548926489274892848929489304893148932489334893448935489364893748938489394894048941489424894348944489454894648947489484894948950489514895248953489544895548956489574895848959489604896148962489634896448965489664896748968489694897048971489724897348974489754897648977489784897948980489814898248983489844898548986489874898848989489904899148992489934899448995489964899748998489994900049001490024900349004490054900649007490084900949010490114901249013490144901549016490174901849019490204902149022490234902449025490264902749028490294903049031490324903349034490354903649037490384903949040490414904249043490444904549046490474904849049490504905149052490534905449055490564905749058490594906049061490624906349064490654906649067490684906949070490714907249073490744907549076490774907849079490804908149082490834908449085490864908749088490894909049091490924909349094490954909649097490984909949100491014910249103491044910549106491074910849109491104911149112491134911449115491164911749118491194912049121491224912349124491254912649127491284912949130491314913249133491344913549136491374913849139491404914149142491434914449145491464914749148491494915049151491524915349154491554915649157491584915949160491614916249163491644916549166491674916849169491704917149172491734917449175491764917749178491794918049181491824918349184491854918649187491884918949190491914919249193491944919549196491974919849199492004920149202492034920449205492064920749208492094921049211492124921349214492154921649217492184921949220492214922249223492244922549226492274922849229492304923149232492334923449235492364923749238492394924049241492424924349244492454924649247492484924949250492514925249253492544925549256492574925849259492604926149262492634926449265492664926749268492694927049271492724927349274492754927649277492784927949280492814928249283492844928549286492874928849289492904929149292492934929449295492964929749298492994930049301493024930349304493054930649307493084930949310493114931249313493144931549316493174931849319493204932149322493234932449325493264932749328493294933049331493324933349334493354933649337493384933949340493414934249343493444934549346493474934849349493504935149352493534935449355493564935749358493594936049361493624936349364493654936649367493684936949370493714937249373493744937549376493774937849379493804938149382493834938449385493864938749388493894939049391493924939349394493954939649397493984939949400494014940249403494044940549406494074940849409494104941149412494134941449415494164941749418494194942049421494224942349424494254942649427494284942949430494314943249433494344943549436494374943849439494404944149442494434944449445494464944749448494494945049451494524945349454494554945649457494584945949460494614946249463494644946549466494674946849469494704947149472494734947449475494764947749478494794948049481494824948349484494854948649487494884948949490494914949249493494944949549496494974949849499495004950149502495034950449505495064950749508495094951049511495124951349514495154951649517495184951949520495214952249523495244952549526495274952849529495304953149532495334953449535495364953749538495394954049541495424954349544495454954649547495484954949550495514955249553495544955549556495574955849559495604956149562495634956449565495664956749568495694957049571495724957349574495754957649577495784957949580495814958249583495844958549586495874958849589495904959149592495934959449595495964959749598495994960049601496024960349604496054960649607496084960949610496114961249613496144961549616496174961849619496204962149622496234962449625496264962749628496294963049631496324963349634496354963649637496384963949640496414964249643496444964549646496474964849649496504965149652496534965449655496564965749658496594966049661496624966349664496654966649667496684966949670496714967249673496744967549676496774967849679496804968149682496834968449685496864968749688496894969049691496924969349694496954969649697496984969949700497014970249703497044970549706497074970849709497104971149712497134971449715497164971749718497194972049721497224972349724497254972649727497284972949730497314973249733497344973549736497374973849739497404974149742497434974449745497464974749748497494975049751497524975349754497554975649757497584975949760497614976249763497644976549766497674976849769497704977149772497734977449775497764977749778497794978049781497824978349784497854978649787497884978949790497914979249793497944979549796497974979849799498004980149802498034980449805498064980749808498094981049811498124981349814498154981649817498184981949820498214982249823498244982549826498274982849829498304983149832498334983449835498364983749838498394984049841498424984349844498454984649847498484984949850498514985249853498544985549856498574985849859498604986149862498634986449865498664986749868498694987049871498724987349874498754987649877498784987949880498814988249883498844988549886498874988849889498904989149892498934989449895498964989749898498994990049901499024990349904499054990649907499084990949910499114991249913499144991549916499174991849919499204992149922499234992449925499264992749928499294993049931499324993349934499354993649937499384993949940499414994249943499444994549946499474994849949499504995149952499534995449955499564995749958499594996049961499624996349964499654996649967499684996949970499714997249973499744997549976499774997849979499804998149982499834998449985499864998749988499894999049991499924999349994499954999649997499984999950000500015000250003500045000550006500075000850009500105001150012500135001450015500165001750018500195002050021500225002350024500255002650027500285002950030500315003250033500345003550036500375003850039500405004150042500435004450045500465004750048500495005050051500525005350054500555005650057500585005950060500615006250063500645006550066500675006850069500705007150072500735007450075500765007750078500795008050081500825008350084500855008650087500885008950090500915009250093500945009550096500975009850099501005010150102501035010450105501065010750108501095011050111501125011350114501155011650117501185011950120501215012250123501245012550126501275012850129501305013150132501335013450135501365013750138501395014050141501425014350144501455014650147501485014950150501515015250153501545015550156501575015850159501605016150162501635016450165501665016750168501695017050171501725017350174501755017650177501785017950180501815018250183501845018550186501875018850189501905019150192501935019450195501965019750198501995020050201502025020350204502055020650207502085020950210502115021250213502145021550216502175021850219502205022150222502235022450225502265022750228502295023050231502325023350234502355023650237502385023950240502415024250243502445024550246502475024850249502505025150252502535025450255502565025750258502595026050261502625026350264502655026650267502685026950270502715027250273502745027550276502775027850279502805028150282502835028450285502865028750288502895029050291502925029350294502955029650297502985029950300503015030250303503045030550306503075030850309503105031150312503135031450315503165031750318503195032050321503225032350324503255032650327503285032950330503315033250333503345033550336503375033850339503405034150342503435034450345503465034750348503495035050351503525035350354503555035650357503585035950360503615036250363503645036550366503675036850369503705037150372503735037450375503765037750378503795038050381503825038350384503855038650387503885038950390503915039250393503945039550396503975039850399504005040150402504035040450405504065040750408504095041050411504125041350414504155041650417504185041950420504215042250423504245042550426504275042850429504305043150432504335043450435504365043750438504395044050441504425044350444504455044650447504485044950450504515045250453504545045550456504575045850459504605046150462504635046450465504665046750468504695047050471504725047350474504755047650477504785047950480504815048250483504845048550486504875048850489504905049150492504935049450495504965049750498504995050050501505025050350504505055050650507505085050950510505115051250513505145051550516505175051850519505205052150522505235052450525505265052750528505295053050531505325053350534505355053650537505385053950540505415054250543505445054550546505475054850549505505055150552505535055450555505565055750558505595056050561505625056350564505655056650567505685056950570505715057250573505745057550576505775057850579505805058150582505835058450585505865058750588505895059050591505925059350594505955059650597505985059950600506015060250603506045060550606506075060850609506105061150612506135061450615506165061750618506195062050621506225062350624506255062650627506285062950630506315063250633506345063550636506375063850639506405064150642506435064450645506465064750648506495065050651506525065350654506555065650657506585065950660506615066250663506645066550666506675066850669506705067150672506735067450675506765067750678506795068050681506825068350684506855068650687506885068950690506915069250693506945069550696506975069850699507005070150702507035070450705507065070750708507095071050711507125071350714507155071650717507185071950720507215072250723507245072550726507275072850729507305073150732507335073450735507365073750738507395074050741507425074350744507455074650747507485074950750507515075250753507545075550756507575075850759507605076150762507635076450765507665076750768507695077050771507725077350774507755077650777507785077950780507815078250783507845078550786507875078850789507905079150792507935079450795507965079750798507995080050801508025080350804508055080650807508085080950810508115081250813508145081550816508175081850819508205082150822508235082450825508265082750828508295083050831508325083350834508355083650837508385083950840508415084250843508445084550846508475084850849508505085150852508535085450855508565085750858508595086050861508625086350864508655086650867508685086950870508715087250873508745087550876508775087850879508805088150882508835088450885508865088750888508895089050891508925089350894508955089650897508985089950900509015090250903509045090550906509075090850909509105091150912509135091450915509165091750918509195092050921509225092350924509255092650927509285092950930509315093250933509345093550936509375093850939509405094150942509435094450945509465094750948509495095050951509525095350954509555095650957509585095950960509615096250963509645096550966509675096850969509705097150972509735097450975509765097750978509795098050981509825098350984509855098650987509885098950990509915099250993509945099550996509975099850999510005100151002510035100451005510065100751008510095101051011510125101351014510155101651017510185101951020510215102251023510245102551026510275102851029510305103151032510335103451035510365103751038510395104051041510425104351044510455104651047510485104951050510515105251053510545105551056510575105851059510605106151062510635106451065510665106751068510695107051071510725107351074510755107651077510785107951080510815108251083510845108551086510875108851089510905109151092510935109451095510965109751098510995110051101511025110351104511055110651107511085110951110511115111251113511145111551116511175111851119511205112151122511235112451125511265112751128511295113051131511325113351134511355113651137511385113951140511415114251143511445114551146511475114851149511505115151152511535115451155511565115751158511595116051161511625116351164511655116651167511685116951170511715117251173511745117551176511775117851179511805118151182511835118451185511865118751188511895119051191511925119351194511955119651197511985119951200512015120251203512045120551206512075120851209512105121151212512135121451215512165121751218512195122051221512225122351224512255122651227512285122951230512315123251233512345123551236512375123851239512405124151242512435124451245512465124751248512495125051251512525125351254512555125651257512585125951260512615126251263512645126551266512675126851269512705127151272512735127451275512765127751278512795128051281512825128351284512855128651287512885128951290512915129251293512945129551296512975129851299513005130151302513035130451305513065130751308513095131051311513125131351314513155131651317513185131951320513215132251323513245132551326513275132851329513305133151332513335133451335513365133751338513395134051341513425134351344513455134651347513485134951350513515135251353513545135551356513575135851359513605136151362513635136451365513665136751368513695137051371513725137351374513755137651377513785137951380513815138251383513845138551386513875138851389513905139151392513935139451395513965139751398513995140051401514025140351404514055140651407514085140951410514115141251413514145141551416514175141851419514205142151422514235142451425514265142751428514295143051431514325143351434514355143651437514385143951440514415144251443514445144551446514475144851449514505145151452514535145451455514565145751458514595146051461514625146351464514655146651467514685146951470514715147251473514745147551476514775147851479514805148151482514835148451485514865148751488514895149051491514925149351494514955149651497514985149951500515015150251503515045150551506515075150851509515105151151512515135151451515515165151751518515195152051521515225152351524515255152651527515285152951530515315153251533515345153551536515375153851539515405154151542515435154451545515465154751548515495155051551515525155351554515555155651557515585155951560515615156251563515645156551566515675156851569515705157151572515735157451575515765157751578515795158051581515825158351584515855158651587515885158951590515915159251593515945159551596515975159851599516005160151602516035160451605516065160751608516095161051611516125161351614516155161651617516185161951620516215162251623516245162551626516275162851629516305163151632516335163451635516365163751638516395164051641516425164351644516455164651647516485164951650516515165251653516545165551656516575165851659516605166151662516635166451665516665166751668516695167051671516725167351674516755167651677516785167951680516815168251683516845168551686516875168851689516905169151692516935169451695516965169751698516995170051701517025170351704517055170651707517085170951710517115171251713517145171551716517175171851719517205172151722517235172451725517265172751728517295173051731517325173351734517355173651737517385173951740517415174251743517445174551746517475174851749517505175151752517535175451755517565175751758517595176051761517625176351764517655176651767517685176951770517715177251773517745177551776517775177851779517805178151782517835178451785517865178751788517895179051791517925179351794517955179651797517985179951800518015180251803518045180551806518075180851809518105181151812518135181451815518165181751818518195182051821518225182351824518255182651827518285182951830518315183251833518345183551836518375183851839518405184151842518435184451845518465184751848518495185051851518525185351854518555185651857518585185951860518615186251863518645186551866518675186851869518705187151872518735187451875518765187751878518795188051881518825188351884518855188651887518885188951890518915189251893518945189551896518975189851899519005190151902519035190451905519065190751908519095191051911519125191351914519155191651917519185191951920519215192251923519245192551926519275192851929519305193151932519335193451935519365193751938519395194051941519425194351944519455194651947519485194951950519515195251953519545195551956519575195851959519605196151962519635196451965519665196751968519695197051971519725197351974519755197651977519785197951980519815198251983519845198551986519875198851989519905199151992519935199451995519965199751998519995200052001520025200352004520055200652007520085200952010520115201252013520145201552016520175201852019520205202152022520235202452025520265202752028520295203052031520325203352034520355203652037520385203952040520415204252043520445204552046520475204852049520505205152052520535205452055520565205752058520595206052061520625206352064520655206652067520685206952070520715207252073520745207552076520775207852079520805208152082520835208452085520865208752088520895209052091520925209352094520955209652097520985209952100521015210252103521045210552106521075210852109521105211152112521135211452115521165211752118521195212052121521225212352124521255212652127521285212952130521315213252133521345213552136521375213852139521405214152142521435214452145521465214752148521495215052151521525215352154521555215652157521585215952160521615216252163521645216552166521675216852169521705217152172521735217452175521765217752178521795218052181521825218352184521855218652187521885218952190521915219252193521945219552196521975219852199522005220152202522035220452205522065220752208522095221052211522125221352214522155221652217522185221952220522215222252223522245222552226522275222852229522305223152232522335223452235522365223752238522395224052241522425224352244522455224652247522485224952250522515225252253522545225552256522575225852259522605226152262522635226452265522665226752268522695227052271522725227352274522755227652277522785227952280522815228252283522845228552286522875228852289522905229152292522935229452295522965229752298522995230052301523025230352304523055230652307523085230952310523115231252313523145231552316523175231852319523205232152322523235232452325523265232752328523295233052331523325233352334523355233652337523385233952340523415234252343523445234552346523475234852349523505235152352523535235452355523565235752358523595236052361523625236352364523655236652367523685236952370523715237252373523745237552376523775237852379523805238152382523835238452385523865238752388523895239052391523925239352394523955239652397523985239952400524015240252403524045240552406524075240852409524105241152412524135241452415524165241752418524195242052421524225242352424524255242652427524285242952430524315243252433524345243552436524375243852439524405244152442524435244452445524465244752448524495245052451524525245352454524555245652457524585245952460524615246252463524645246552466524675246852469524705247152472524735247452475524765247752478524795248052481524825248352484524855248652487524885248952490524915249252493524945249552496524975249852499525005250152502525035250452505525065250752508525095251052511525125251352514525155251652517525185251952520525215252252523525245252552526525275252852529525305253152532525335253452535525365253752538525395254052541525425254352544525455254652547525485254952550525515255252553525545255552556525575255852559525605256152562525635256452565525665256752568525695257052571525725257352574525755257652577525785257952580525815258252583525845258552586525875258852589525905259152592525935259452595525965259752598525995260052601526025260352604526055260652607526085260952610526115261252613526145261552616526175261852619526205262152622526235262452625526265262752628526295263052631526325263352634526355263652637526385263952640526415264252643526445264552646526475264852649526505265152652526535265452655526565265752658526595266052661526625266352664526655266652667526685266952670526715267252673526745267552676526775267852679526805268152682526835268452685526865268752688526895269052691526925269352694526955269652697526985269952700527015270252703527045270552706527075270852709527105271152712527135271452715527165271752718527195272052721527225272352724527255272652727527285272952730527315273252733527345273552736527375273852739527405274152742527435274452745527465274752748527495275052751527525275352754527555275652757527585275952760527615276252763527645276552766527675276852769527705277152772527735277452775527765277752778527795278052781527825278352784527855278652787527885278952790527915279252793527945279552796527975279852799528005280152802528035280452805528065280752808528095281052811528125281352814528155281652817528185281952820528215282252823528245282552826528275282852829528305283152832528335283452835528365283752838528395284052841528425284352844528455284652847528485284952850528515285252853528545285552856528575285852859528605286152862528635286452865528665286752868528695287052871528725287352874528755287652877528785287952880528815288252883528845288552886528875288852889528905289152892528935289452895528965289752898528995290052901529025290352904529055290652907529085290952910529115291252913529145291552916529175291852919529205292152922529235292452925529265292752928529295293052931529325293352934529355293652937529385293952940529415294252943529445294552946529475294852949529505295152952529535295452955529565295752958529595296052961529625296352964529655296652967529685296952970529715297252973529745297552976529775297852979529805298152982529835298452985529865298752988529895299052991529925299352994529955299652997529985299953000530015300253003530045300553006530075300853009530105301153012530135301453015530165301753018530195302053021530225302353024530255302653027530285302953030530315303253033530345303553036530375303853039530405304153042530435304453045530465304753048530495305053051530525305353054530555305653057530585305953060530615306253063530645306553066530675306853069530705307153072530735307453075530765307753078530795308053081530825308353084530855308653087530885308953090530915309253093530945309553096530975309853099531005310153102531035310453105531065310753108531095311053111531125311353114531155311653117531185311953120531215312253123531245312553126531275312853129531305313153132531335313453135531365313753138531395314053141531425314353144531455314653147531485314953150531515315253153531545315553156531575315853159531605316153162531635316453165531665316753168531695317053171531725317353174531755317653177531785317953180531815318253183531845318553186531875318853189531905319153192531935319453195531965319753198531995320053201532025320353204532055320653207532085320953210532115321253213532145321553216532175321853219532205322153222532235322453225532265322753228532295323053231532325323353234532355323653237532385323953240532415324253243532445324553246532475324853249532505325153252532535325453255532565325753258532595326053261532625326353264532655326653267532685326953270532715327253273532745327553276532775327853279532805328153282532835328453285532865328753288532895329053291532925329353294532955329653297532985329953300533015330253303533045330553306533075330853309533105331153312533135331453315533165331753318533195332053321533225332353324533255332653327533285332953330533315333253333533345333553336533375333853339533405334153342533435334453345533465334753348533495335053351533525335353354533555335653357533585335953360533615336253363533645336553366533675336853369533705337153372533735337453375533765337753378533795338053381533825338353384533855338653387533885338953390533915339253393533945339553396533975339853399534005340153402534035340453405534065340753408534095341053411534125341353414534155341653417534185341953420534215342253423534245342553426534275342853429534305343153432534335343453435534365343753438534395344053441534425344353444534455344653447534485344953450534515345253453534545345553456534575345853459534605346153462534635346453465534665346753468534695347053471534725347353474534755347653477534785347953480534815348253483534845348553486534875348853489534905349153492534935349453495534965349753498534995350053501535025350353504535055350653507535085350953510535115351253513535145351553516535175351853519535205352153522535235352453525535265352753528535295353053531535325353353534535355353653537535385353953540535415354253543535445354553546535475354853549535505355153552535535355453555535565355753558535595356053561535625356353564535655356653567535685356953570535715357253573535745357553576535775357853579535805358153582535835358453585535865358753588535895359053591535925359353594535955359653597535985359953600536015360253603536045360553606536075360853609536105361153612536135361453615536165361753618536195362053621536225362353624536255362653627536285362953630536315363253633536345363553636536375363853639536405364153642536435364453645536465364753648536495365053651536525365353654536555365653657536585365953660536615366253663536645366553666536675366853669536705367153672536735367453675536765367753678536795368053681536825368353684536855368653687536885368953690536915369253693536945369553696536975369853699537005370153702537035370453705537065370753708537095371053711537125371353714537155371653717537185371953720537215372253723537245372553726537275372853729537305373153732537335373453735537365373753738537395374053741537425374353744537455374653747537485374953750537515375253753537545375553756537575375853759537605376153762537635376453765537665376753768537695377053771537725377353774537755377653777537785377953780537815378253783537845378553786537875378853789537905379153792537935379453795537965379753798537995380053801538025380353804538055380653807538085380953810538115381253813538145381553816538175381853819538205382153822538235382453825538265382753828538295383053831538325383353834538355383653837538385383953840538415384253843538445384553846538475384853849538505385153852538535385453855538565385753858538595386053861538625386353864538655386653867538685386953870538715387253873538745387553876538775387853879538805388153882538835388453885538865388753888538895389053891538925389353894538955389653897538985389953900539015390253903539045390553906539075390853909539105391153912539135391453915539165391753918539195392053921539225392353924539255392653927539285392953930539315393253933539345393553936539375393853939539405394153942539435394453945539465394753948539495395053951539525395353954539555395653957539585395953960539615396253963539645396553966539675396853969539705397153972539735397453975539765397753978539795398053981539825398353984539855398653987539885398953990539915399253993539945399553996539975399853999540005400154002540035400454005540065400754008540095401054011540125401354014540155401654017540185401954020540215402254023540245402554026540275402854029540305403154032540335403454035540365403754038540395404054041540425404354044540455404654047540485404954050540515405254053540545405554056540575405854059540605406154062540635406454065540665406754068540695407054071540725407354074540755407654077540785407954080540815408254083540845408554086540875408854089540905409154092540935409454095540965409754098540995410054101541025410354104541055410654107541085410954110541115411254113541145411554116541175411854119541205412154122541235412454125541265412754128541295413054131541325413354134541355413654137541385413954140541415414254143541445414554146541475414854149541505415154152541535415454155541565415754158541595416054161541625416354164541655416654167541685416954170541715417254173541745417554176541775417854179541805418154182541835418454185541865418754188541895419054191541925419354194541955419654197541985419954200542015420254203542045420554206542075420854209542105421154212542135421454215542165421754218542195422054221542225422354224542255422654227542285422954230542315423254233542345423554236542375423854239542405424154242542435424454245542465424754248542495425054251542525425354254542555425654257542585425954260542615426254263542645426554266542675426854269542705427154272542735427454275542765427754278542795428054281542825428354284542855428654287542885428954290542915429254293542945429554296542975429854299543005430154302543035430454305543065430754308543095431054311543125431354314543155431654317543185431954320543215432254323543245432554326543275432854329543305433154332543335433454335543365433754338543395434054341543425434354344543455434654347543485434954350543515435254353543545435554356543575435854359543605436154362543635436454365543665436754368543695437054371543725437354374543755437654377543785437954380543815438254383543845438554386543875438854389543905439154392543935439454395543965439754398543995440054401544025440354404544055440654407544085440954410544115441254413544145441554416544175441854419544205442154422544235442454425544265442754428544295443054431544325443354434544355443654437544385443954440544415444254443544445444554446544475444854449544505445154452544535445454455544565445754458544595446054461544625446354464544655446654467544685446954470544715447254473544745447554476544775447854479544805448154482544835448454485544865448754488544895449054491544925449354494544955449654497544985449954500545015450254503545045450554506545075450854509545105451154512545135451454515545165451754518545195452054521545225452354524545255452654527545285452954530545315453254533545345453554536545375453854539545405454154542545435454454545545465454754548545495455054551545525455354554545555455654557545585455954560545615456254563545645456554566545675456854569545705457154572545735457454575545765457754578545795458054581545825458354584545855458654587545885458954590545915459254593545945459554596545975459854599546005460154602546035460454605546065460754608546095461054611546125461354614546155461654617546185461954620546215462254623546245462554626546275462854629546305463154632546335463454635546365463754638546395464054641546425464354644546455464654647546485464954650546515465254653546545465554656546575465854659546605466154662546635466454665546665466754668546695467054671546725467354674546755467654677546785467954680546815468254683546845468554686546875468854689546905469154692546935469454695546965469754698546995470054701547025470354704547055470654707547085470954710547115471254713547145471554716547175471854719547205472154722547235472454725547265472754728547295473054731547325473354734547355473654737547385473954740547415474254743547445474554746547475474854749547505475154752547535475454755547565475754758547595476054761547625476354764547655476654767547685476954770547715477254773547745477554776547775477854779547805478154782547835478454785547865478754788547895479054791547925479354794547955479654797547985479954800548015480254803548045480554806548075480854809548105481154812548135481454815548165481754818548195482054821548225482354824548255482654827548285482954830548315483254833548345483554836548375483854839548405484154842548435484454845548465484754848548495485054851548525485354854548555485654857548585485954860548615486254863548645486554866548675486854869548705487154872548735487454875548765487754878548795488054881548825488354884548855488654887548885488954890548915489254893548945489554896548975489854899549005490154902549035490454905549065490754908549095491054911549125491354914549155491654917549185491954920549215492254923549245492554926549275492854929549305493154932549335493454935549365493754938549395494054941549425494354944549455494654947549485494954950549515495254953549545495554956549575495854959549605496154962549635496454965549665496754968549695497054971549725497354974549755497654977549785497954980549815498254983549845498554986549875498854989549905499154992549935499454995549965499754998549995500055001550025500355004550055500655007550085500955010550115501255013550145501555016550175501855019550205502155022550235502455025550265502755028550295503055031550325503355034550355503655037550385503955040550415504255043550445504555046550475504855049550505505155052550535505455055550565505755058550595506055061550625506355064550655506655067550685506955070550715507255073550745507555076550775507855079550805508155082550835508455085550865508755088550895509055091550925509355094550955509655097550985509955100551015510255103551045510555106551075510855109551105511155112551135511455115551165511755118551195512055121551225512355124551255512655127551285512955130551315513255133551345513555136551375513855139551405514155142551435514455145551465514755148551495515055151551525515355154551555515655157551585515955160551615516255163551645516555166551675516855169551705517155172551735517455175551765517755178551795518055181551825518355184551855518655187551885518955190551915519255193551945519555196551975519855199552005520155202552035520455205552065520755208552095521055211552125521355214552155521655217552185521955220552215522255223552245522555226552275522855229552305523155232552335523455235552365523755238552395524055241552425524355244552455524655247552485524955250552515525255253552545525555256552575525855259552605526155262552635526455265552665526755268552695527055271552725527355274552755527655277552785527955280552815528255283552845528555286552875528855289552905529155292552935529455295552965529755298552995530055301553025530355304553055530655307553085530955310553115531255313553145531555316553175531855319553205532155322553235532455325553265532755328553295533055331553325533355334553355533655337553385533955340553415534255343553445534555346553475534855349553505535155352553535535455355553565535755358553595536055361553625536355364553655536655367553685536955370553715537255373553745537555376553775537855379553805538155382553835538455385553865538755388553895539055391553925539355394553955539655397553985539955400554015540255403554045540555406554075540855409554105541155412554135541455415554165541755418554195542055421554225542355424554255542655427554285542955430554315543255433554345543555436554375543855439554405544155442554435544455445554465544755448554495545055451554525545355454554555545655457554585545955460554615546255463554645546555466554675546855469554705547155472554735547455475554765547755478554795548055481554825548355484554855548655487554885548955490554915549255493554945549555496554975549855499555005550155502555035550455505555065550755508555095551055511555125551355514555155551655517555185551955520555215552255523555245552555526555275552855529555305553155532555335553455535555365553755538555395554055541555425554355544555455554655547555485554955550555515555255553555545555555556555575555855559555605556155562555635556455565555665556755568555695557055571555725557355574555755557655577555785557955580555815558255583555845558555586555875558855589555905559155592555935559455595555965559755598555995560055601556025560355604556055560655607556085560955610556115561255613556145561555616556175561855619556205562155622556235562455625556265562755628556295563055631556325563355634556355563655637556385563955640556415564255643556445564555646556475564855649556505565155652556535565455655556565565755658556595566055661556625566355664556655566655667556685566955670556715567255673556745567555676556775567855679556805568155682556835568455685556865568755688556895569055691556925569355694556955569655697556985569955700557015570255703557045570555706557075570855709557105571155712557135571455715557165571755718557195572055721557225572355724557255572655727557285572955730557315573255733557345573555736557375573855739557405574155742557435574455745557465574755748557495575055751557525575355754557555575655757557585575955760557615576255763557645576555766557675576855769557705577155772557735577455775557765577755778557795578055781557825578355784557855578655787557885578955790557915579255793557945579555796557975579855799558005580155802558035580455805558065580755808558095581055811558125581355814558155581655817558185581955820558215582255823558245582555826558275582855829558305583155832558335583455835558365583755838558395584055841558425584355844558455584655847558485584955850558515585255853558545585555856558575585855859558605586155862558635586455865558665586755868558695587055871558725587355874558755587655877558785587955880558815588255883558845588555886558875588855889558905589155892558935589455895558965589755898558995590055901559025590355904559055590655907559085590955910559115591255913559145591555916559175591855919559205592155922559235592455925559265592755928559295593055931559325593355934559355593655937559385593955940559415594255943559445594555946559475594855949559505595155952559535595455955559565595755958559595596055961559625596355964559655596655967559685596955970559715597255973559745597555976559775597855979559805598155982559835598455985559865598755988559895599055991559925599355994559955599655997559985599956000560015600256003560045600556006560075600856009560105601156012560135601456015560165601756018560195602056021560225602356024560255602656027560285602956030560315603256033560345603556036560375603856039560405604156042560435604456045560465604756048560495605056051560525605356054560555605656057560585605956060560615606256063560645606556066560675606856069560705607156072560735607456075560765607756078560795608056081560825608356084560855608656087560885608956090560915609256093560945609556096560975609856099561005610156102561035610456105561065610756108561095611056111561125611356114561155611656117561185611956120561215612256123561245612556126561275612856129561305613156132561335613456135561365613756138561395614056141561425614356144561455614656147561485614956150561515615256153561545615556156561575615856159561605616156162561635616456165561665616756168561695617056171561725617356174561755617656177561785617956180561815618256183561845618556186561875618856189561905619156192561935619456195561965619756198561995620056201562025620356204562055620656207562085620956210562115621256213562145621556216562175621856219562205622156222562235622456225562265622756228562295623056231562325623356234562355623656237562385623956240562415624256243562445624556246562475624856249562505625156252562535625456255562565625756258562595626056261562625626356264562655626656267562685626956270562715627256273562745627556276562775627856279562805628156282562835628456285562865628756288562895629056291562925629356294562955629656297562985629956300563015630256303563045630556306563075630856309563105631156312563135631456315563165631756318563195632056321563225632356324563255632656327563285632956330563315633256333563345633556336563375633856339563405634156342563435634456345563465634756348563495635056351563525635356354563555635656357563585635956360563615636256363563645636556366563675636856369563705637156372563735637456375563765637756378563795638056381563825638356384563855638656387563885638956390563915639256393563945639556396563975639856399564005640156402564035640456405564065640756408564095641056411564125641356414564155641656417564185641956420564215642256423564245642556426564275642856429564305643156432564335643456435564365643756438564395644056441564425644356444564455644656447564485644956450564515645256453564545645556456564575645856459564605646156462564635646456465564665646756468564695647056471564725647356474564755647656477564785647956480564815648256483564845648556486564875648856489564905649156492564935649456495564965649756498564995650056501565025650356504565055650656507565085650956510565115651256513565145651556516565175651856519565205652156522565235652456525565265652756528565295653056531565325653356534565355653656537565385653956540565415654256543565445654556546565475654856549565505655156552565535655456555565565655756558565595656056561565625656356564565655656656567565685656956570565715657256573565745657556576565775657856579565805658156582565835658456585565865658756588565895659056591565925659356594565955659656597565985659956600566015660256603566045660556606566075660856609566105661156612566135661456615566165661756618566195662056621566225662356624566255662656627566285662956630566315663256633566345663556636566375663856639566405664156642566435664456645566465664756648566495665056651566525665356654566555665656657566585665956660566615666256663566645666556666566675666856669566705667156672566735667456675566765667756678566795668056681566825668356684566855668656687566885668956690566915669256693566945669556696566975669856699567005670156702567035670456705567065670756708567095671056711567125671356714567155671656717567185671956720567215672256723567245672556726567275672856729567305673156732567335673456735567365673756738567395674056741567425674356744567455674656747567485674956750567515675256753567545675556756567575675856759567605676156762567635676456765567665676756768567695677056771567725677356774567755677656777567785677956780567815678256783567845678556786567875678856789567905679156792567935679456795567965679756798567995680056801568025680356804568055680656807568085680956810568115681256813568145681556816568175681856819568205682156822568235682456825568265682756828568295683056831568325683356834568355683656837568385683956840568415684256843568445684556846568475684856849568505685156852568535685456855568565685756858568595686056861568625686356864568655686656867568685686956870568715687256873568745687556876568775687856879568805688156882568835688456885568865688756888568895689056891568925689356894568955689656897568985689956900569015690256903569045690556906569075690856909569105691156912569135691456915569165691756918569195692056921569225692356924569255692656927569285692956930569315693256933569345693556936569375693856939569405694156942569435694456945569465694756948569495695056951569525695356954569555695656957569585695956960569615696256963569645696556966569675696856969569705697156972569735697456975569765697756978569795698056981569825698356984569855698656987569885698956990569915699256993569945699556996569975699856999570005700157002570035700457005570065700757008570095701057011570125701357014570155701657017570185701957020570215702257023570245702557026570275702857029570305703157032570335703457035570365703757038570395704057041570425704357044570455704657047570485704957050570515705257053570545705557056570575705857059570605706157062570635706457065570665706757068570695707057071570725707357074570755707657077570785707957080570815708257083570845708557086570875708857089570905709157092570935709457095570965709757098570995710057101571025710357104571055710657107571085710957110571115711257113571145711557116571175711857119571205712157122571235712457125571265712757128571295713057131571325713357134571355713657137571385713957140571415714257143571445714557146571475714857149571505715157152571535715457155571565715757158571595716057161571625716357164571655716657167571685716957170571715717257173571745717557176571775717857179571805718157182571835718457185571865718757188571895719057191571925719357194571955719657197571985719957200572015720257203572045720557206572075720857209572105721157212572135721457215572165721757218572195722057221572225722357224572255722657227572285722957230572315723257233572345723557236572375723857239572405724157242572435724457245572465724757248572495725057251572525725357254572555725657257572585725957260572615726257263572645726557266572675726857269572705727157272572735727457275572765727757278572795728057281572825728357284572855728657287572885728957290572915729257293572945729557296572975729857299573005730157302573035730457305573065730757308573095731057311573125731357314573155731657317573185731957320573215732257323573245732557326573275732857329573305733157332573335733457335573365733757338573395734057341573425734357344573455734657347573485734957350573515735257353573545735557356573575735857359573605736157362573635736457365573665736757368573695737057371573725737357374573755737657377573785737957380573815738257383573845738557386573875738857389573905739157392573935739457395573965739757398573995740057401574025740357404574055740657407574085740957410574115741257413574145741557416574175741857419574205742157422574235742457425574265742757428574295743057431574325743357434574355743657437574385743957440574415744257443574445744557446574475744857449574505745157452574535745457455574565745757458574595746057461574625746357464574655746657467574685746957470574715747257473574745747557476574775747857479574805748157482574835748457485574865748757488574895749057491574925749357494574955749657497574985749957500575015750257503575045750557506575075750857509575105751157512575135751457515575165751757518575195752057521575225752357524575255752657527575285752957530575315753257533575345753557536575375753857539575405754157542575435754457545575465754757548575495755057551575525755357554575555755657557575585755957560575615756257563575645756557566575675756857569575705757157572575735757457575575765757757578575795758057581575825758357584575855758657587575885758957590575915759257593575945759557596575975759857599576005760157602576035760457605576065760757608576095761057611576125761357614576155761657617576185761957620576215762257623576245762557626576275762857629576305763157632576335763457635576365763757638576395764057641576425764357644576455764657647576485764957650576515765257653576545765557656576575765857659576605766157662576635766457665576665766757668576695767057671576725767357674576755767657677576785767957680576815768257683576845768557686576875768857689576905769157692576935769457695576965769757698576995770057701577025770357704577055770657707577085770957710577115771257713577145771557716577175771857719577205772157722577235772457725577265772757728577295773057731577325773357734577355773657737577385773957740577415774257743577445774557746577475774857749577505775157752577535775457755577565775757758577595776057761577625776357764577655776657767577685776957770577715777257773577745777557776577775777857779577805778157782577835778457785577865778757788577895779057791577925779357794577955779657797577985779957800578015780257803578045780557806578075780857809578105781157812578135781457815578165781757818578195782057821578225782357824578255782657827578285782957830578315783257833578345783557836578375783857839578405784157842578435784457845578465784757848578495785057851578525785357854578555785657857578585785957860578615786257863578645786557866578675786857869578705787157872578735787457875578765787757878578795788057881578825788357884578855788657887578885788957890578915789257893578945789557896578975789857899579005790157902579035790457905579065790757908579095791057911579125791357914579155791657917579185791957920579215792257923579245792557926579275792857929579305793157932579335793457935579365793757938579395794057941579425794357944579455794657947579485794957950579515795257953579545795557956579575795857959579605796157962579635796457965579665796757968579695797057971579725797357974579755797657977579785797957980579815798257983579845798557986579875798857989579905799157992579935799457995579965799757998579995800058001580025800358004580055800658007580085800958010580115801258013580145801558016580175801858019580205802158022580235802458025580265802758028580295803058031580325803358034580355803658037580385803958040580415804258043580445804558046580475804858049580505805158052580535805458055580565805758058580595806058061580625806358064580655806658067580685806958070580715807258073580745807558076580775807858079580805808158082580835808458085580865808758088580895809058091580925809358094580955809658097580985809958100581015810258103581045810558106581075810858109581105811158112581135811458115581165811758118581195812058121581225812358124581255812658127581285812958130581315813258133581345813558136581375813858139581405814158142581435814458145581465814758148581495815058151581525815358154581555815658157581585815958160581615816258163581645816558166581675816858169581705817158172581735817458175581765817758178581795818058181581825818358184581855818658187581885818958190581915819258193581945819558196581975819858199582005820158202582035820458205582065820758208582095821058211582125821358214582155821658217582185821958220582215822258223582245822558226582275822858229582305823158232582335823458235582365823758238582395824058241582425824358244582455824658247582485824958250582515825258253582545825558256582575825858259582605826158262582635826458265582665826758268582695827058271582725827358274582755827658277582785827958280582815828258283582845828558286582875828858289582905829158292582935829458295582965829758298582995830058301583025830358304583055830658307583085830958310583115831258313583145831558316583175831858319583205832158322583235832458325583265832758328583295833058331583325833358334583355833658337583385833958340583415834258343583445834558346583475834858349583505835158352583535835458355583565835758358583595836058361583625836358364583655836658367583685836958370583715837258373583745837558376583775837858379583805838158382583835838458385583865838758388583895839058391583925839358394583955839658397583985839958400584015840258403584045840558406584075840858409584105841158412584135841458415584165841758418584195842058421584225842358424584255842658427584285842958430584315843258433584345843558436584375843858439584405844158442584435844458445584465844758448584495845058451584525845358454584555845658457584585845958460584615846258463584645846558466584675846858469584705847158472584735847458475584765847758478584795848058481584825848358484584855848658487584885848958490584915849258493584945849558496584975849858499585005850158502585035850458505585065850758508585095851058511585125851358514585155851658517585185851958520585215852258523585245852558526585275852858529585305853158532585335853458535585365853758538585395854058541585425854358544585455854658547585485854958550585515855258553585545855558556585575855858559585605856158562585635856458565585665856758568585695857058571585725857358574585755857658577585785857958580585815858258583585845858558586585875858858589585905859158592585935859458595585965859758598585995860058601586025860358604586055860658607586085860958610586115861258613586145861558616586175861858619586205862158622586235862458625586265862758628586295863058631586325863358634586355863658637586385863958640586415864258643586445864558646586475864858649586505865158652586535865458655586565865758658586595866058661586625866358664586655866658667586685866958670586715867258673586745867558676586775867858679586805868158682586835868458685586865868758688586895869058691586925869358694586955869658697586985869958700587015870258703587045870558706587075870858709587105871158712587135871458715587165871758718587195872058721587225872358724587255872658727587285872958730587315873258733587345873558736587375873858739587405874158742587435874458745587465874758748587495875058751587525875358754587555875658757587585875958760587615876258763587645876558766587675876858769587705877158772587735877458775587765877758778587795878058781587825878358784587855878658787587885878958790587915879258793587945879558796587975879858799588005880158802588035880458805588065880758808588095881058811588125881358814588155881658817588185881958820588215882258823588245882558826588275882858829588305883158832588335883458835588365883758838588395884058841588425884358844588455884658847588485884958850588515885258853588545885558856588575885858859588605886158862588635886458865588665886758868588695887058871588725887358874588755887658877588785887958880588815888258883588845888558886588875888858889588905889158892588935889458895588965889758898588995890058901589025890358904589055890658907589085890958910589115891258913589145891558916589175891858919589205892158922589235892458925589265892758928589295893058931589325893358934589355893658937589385893958940589415894258943589445894558946589475894858949589505895158952589535895458955589565895758958589595896058961589625896358964589655896658967589685896958970589715897258973589745897558976589775897858979589805898158982589835898458985589865898758988589895899058991589925899358994589955899658997589985899959000590015900259003590045900559006590075900859009590105901159012590135901459015590165901759018590195902059021590225902359024590255902659027590285902959030590315903259033590345903559036590375903859039590405904159042590435904459045590465904759048590495905059051590525905359054590555905659057590585905959060590615906259063590645906559066590675906859069590705907159072590735907459075590765907759078590795908059081590825908359084590855908659087590885908959090590915909259093590945909559096590975909859099591005910159102591035910459105591065910759108591095911059111591125911359114591155911659117591185911959120591215912259123591245912559126591275912859129591305913159132591335913459135591365913759138591395914059141591425914359144591455914659147591485914959150591515915259153591545915559156591575915859159591605916159162591635916459165591665916759168591695917059171591725917359174591755917659177591785917959180591815918259183591845918559186591875918859189591905919159192591935919459195591965919759198591995920059201592025920359204592055920659207592085920959210592115921259213592145921559216592175921859219592205922159222592235922459225592265922759228592295923059231592325923359234592355923659237592385923959240592415924259243592445924559246592475924859249592505925159252592535925459255592565925759258592595926059261592625926359264592655926659267592685926959270592715927259273592745927559276592775927859279592805928159282592835928459285592865928759288592895929059291592925929359294592955929659297592985929959300593015930259303593045930559306593075930859309593105931159312593135931459315593165931759318593195932059321593225932359324593255932659327593285932959330593315933259333593345933559336593375933859339593405934159342593435934459345593465934759348593495935059351593525935359354593555935659357593585935959360593615936259363593645936559366593675936859369593705937159372593735937459375593765937759378593795938059381593825938359384593855938659387593885938959390593915939259393593945939559396593975939859399594005940159402594035940459405594065940759408594095941059411594125941359414594155941659417594185941959420594215942259423594245942559426594275942859429594305943159432594335943459435594365943759438594395944059441594425944359444594455944659447594485944959450594515945259453594545945559456594575945859459594605946159462594635946459465594665946759468594695947059471594725947359474594755947659477594785947959480594815948259483594845948559486594875948859489594905949159492594935949459495594965949759498594995950059501595025950359504595055950659507595085950959510595115951259513595145951559516595175951859519595205952159522595235952459525595265952759528595295953059531595325953359534595355953659537595385953959540595415954259543595445954559546595475954859549595505955159552595535955459555595565955759558595595956059561595625956359564595655956659567595685956959570595715957259573595745957559576595775957859579595805958159582595835958459585595865958759588595895959059591595925959359594595955959659597595985959959600596015960259603596045960559606596075960859609596105961159612596135961459615596165961759618596195962059621596225962359624596255962659627596285962959630596315963259633596345963559636596375963859639596405964159642596435964459645596465964759648596495965059651596525965359654596555965659657596585965959660596615966259663596645966559666596675966859669596705967159672596735967459675596765967759678596795968059681596825968359684596855968659687596885968959690596915969259693596945969559696596975969859699597005970159702597035970459705597065970759708597095971059711597125971359714597155971659717597185971959720597215972259723597245972559726597275972859729597305973159732597335973459735597365973759738597395974059741597425974359744597455974659747597485974959750597515975259753597545975559756597575975859759597605976159762597635976459765597665976759768597695977059771597725977359774597755977659777597785977959780597815978259783597845978559786597875978859789597905979159792597935979459795597965979759798597995980059801598025980359804598055980659807598085980959810598115981259813598145981559816598175981859819598205982159822598235982459825598265982759828598295983059831598325983359834598355983659837598385983959840598415984259843598445984559846598475984859849598505985159852598535985459855598565985759858598595986059861598625986359864598655986659867598685986959870598715987259873598745987559876598775987859879598805988159882598835988459885598865988759888598895989059891598925989359894598955989659897598985989959900599015990259903599045990559906599075990859909599105991159912599135991459915599165991759918599195992059921599225992359924599255992659927599285992959930599315993259933599345993559936599375993859939599405994159942599435994459945599465994759948599495995059951599525995359954599555995659957599585995959960599615996259963599645996559966599675996859969599705997159972599735997459975599765997759978599795998059981599825998359984599855998659987599885998959990599915999259993599945999559996599975999859999600006000160002600036000460005600066000760008600096001060011600126001360014600156001660017600186001960020600216002260023600246002560026600276002860029600306003160032600336003460035600366003760038600396004060041600426004360044600456004660047600486004960050600516005260053600546005560056600576005860059600606006160062600636006460065600666006760068600696007060071600726007360074600756007660077600786007960080600816008260083600846008560086600876008860089600906009160092600936009460095600966009760098600996010060101601026010360104601056010660107601086010960110601116011260113601146011560116601176011860119601206012160122601236012460125601266012760128601296013060131601326013360134601356013660137601386013960140601416014260143601446014560146601476014860149601506015160152601536015460155601566015760158601596016060161601626016360164601656016660167601686016960170601716017260173601746017560176601776017860179601806018160182601836018460185601866018760188601896019060191601926019360194601956019660197601986019960200602016020260203602046020560206602076020860209602106021160212602136021460215602166021760218602196022060221602226022360224602256022660227602286022960230602316023260233602346023560236602376023860239602406024160242602436024460245602466024760248602496025060251602526025360254602556025660257602586025960260602616026260263602646026560266602676026860269602706027160272602736027460275602766027760278602796028060281602826028360284602856028660287602886028960290602916029260293602946029560296602976029860299603006030160302603036030460305603066030760308603096031060311603126031360314603156031660317603186031960320603216032260323603246032560326603276032860329603306033160332603336033460335603366033760338603396034060341603426034360344603456034660347603486034960350603516035260353603546035560356603576035860359603606036160362603636036460365603666036760368603696037060371603726037360374603756037660377603786037960380603816038260383603846038560386603876038860389603906039160392603936039460395603966039760398603996040060401604026040360404604056040660407604086040960410604116041260413604146041560416604176041860419604206042160422604236042460425604266042760428604296043060431604326043360434604356043660437604386043960440604416044260443604446044560446604476044860449604506045160452604536045460455604566045760458604596046060461604626046360464604656046660467604686046960470604716047260473604746047560476604776047860479604806048160482604836048460485604866048760488604896049060491604926049360494604956049660497604986049960500605016050260503605046050560506605076050860509605106051160512605136051460515605166051760518605196052060521605226052360524605256052660527605286052960530605316053260533605346053560536605376053860539605406054160542605436054460545605466054760548605496055060551605526055360554605556055660557605586055960560605616056260563605646056560566605676056860569605706057160572605736057460575605766057760578605796058060581605826058360584605856058660587605886058960590605916059260593605946059560596605976059860599606006060160602606036060460605606066060760608606096061060611606126061360614606156061660617606186061960620606216062260623606246062560626606276062860629606306063160632606336063460635606366063760638606396064060641606426064360644606456064660647606486064960650606516065260653606546065560656606576065860659606606066160662606636066460665606666066760668606696067060671606726067360674606756067660677606786067960680606816068260683606846068560686606876068860689606906069160692606936069460695606966069760698606996070060701607026070360704607056070660707607086070960710607116071260713607146071560716607176071860719607206072160722607236072460725607266072760728607296073060731607326073360734607356073660737607386073960740607416074260743607446074560746607476074860749607506075160752607536075460755607566075760758607596076060761607626076360764607656076660767607686076960770607716077260773607746077560776607776077860779607806078160782607836078460785607866078760788607896079060791607926079360794607956079660797607986079960800608016080260803608046080560806608076080860809608106081160812608136081460815608166081760818608196082060821608226082360824608256082660827608286082960830608316083260833608346083560836608376083860839608406084160842608436084460845608466084760848608496085060851608526085360854608556085660857608586085960860608616086260863608646086560866608676086860869608706087160872608736087460875608766087760878608796088060881608826088360884608856088660887608886088960890608916089260893608946089560896608976089860899609006090160902609036090460905609066090760908609096091060911609126091360914609156091660917609186091960920609216092260923609246092560926609276092860929609306093160932609336093460935609366093760938609396094060941609426094360944609456094660947609486094960950609516095260953609546095560956609576095860959609606096160962609636096460965609666096760968609696097060971609726097360974609756097660977609786097960980609816098260983609846098560986609876098860989609906099160992609936099460995609966099760998609996100061001610026100361004610056100661007610086100961010610116101261013610146101561016610176101861019610206102161022
  1. From fe24a2249a07c6c70767d6030b4b467a5dc6717f Mon Sep 17 00:00:00 2001
  2. From: popcornmix <[email protected]>
  3. Date: Wed, 1 May 2013 19:46:17 +0100
  4. Subject: [PATCH] Add dwc_otg driver
  5. MIME-Version: 1.0
  6. Content-Type: text/plain; charset=UTF-8
  7. Content-Transfer-Encoding: 8bit
  8. Signed-off-by: popcornmix <[email protected]>
  9. usb: dwc: fix lockdep false positive
  10. Signed-off-by: Kari Suvanto <[email protected]>
  11. usb: dwc: fix inconsistent lock state
  12. Signed-off-by: Kari Suvanto <[email protected]>
  13. Add FIQ patch to dwc_otg driver. Enable with dwc_otg.fiq_fix_enable=1. Should give about 10% more ARM performance.
  14. Thanks to Gordon and Costas
  15. Avoid dynamic memory allocation for channel lock in USB driver. Thanks ddv2005.
  16. Add NAK holdoff scheme. Enabled by default, disable with dwc_otg.nak_holdoff_enable=0. Thanks gsh
  17. Make sure we wait for the reset to finish
  18. dwc_otg: fix bug in dwc_otg_hcd.c resulting in silent kernel
  19. memory corruption, escalating to OOPS under high USB load.
  20. dwc_otg: Fix unsafe access of QTD during URB enqueue
  21. In dwc_otg_hcd_urb_enqueue during qtd creation, it was possible that the
  22. transaction could complete almost immediately after the qtd was assigned
  23. to a host channel during URB enqueue, which meant the qtd pointer was no
  24. longer valid having been completed and removed. Usually, this resulted in
  25. an OOPS during URB submission. By predetermining whether transactions
  26. need to be queued or not, this unsafe pointer access is avoided.
  27. This bug was only evident on the Pi model A where a device was attached
  28. that had no periodic endpoints (e.g. USB pendrive or some wlan devices).
  29. dwc_otg: Fix incorrect URB allocation error handling
  30. If the memory allocation for a dwc_otg_urb failed, the kernel would OOPS
  31. because for some reason a member of the *unallocated* struct was set to
  32. zero. Error handling changed to fail correctly.
  33. dwc_otg: fix potential use-after-free case in interrupt handler
  34. If a transaction had previously aborted, certain interrupts are
  35. enabled to track error counts and reset where necessary. On IN
  36. endpoints the host generates an ACK interrupt near-simultaneously
  37. with completion of transfer. In the case where this transfer had
  38. previously had an error, this results in a use-after-free on
  39. the QTD memory space with a 1-byte length being overwritten to
  40. 0x00.
  41. dwc_otg: add handling of SPLIT transaction data toggle errors
  42. Previously a data toggle error on packets from a USB1.1 device behind
  43. a TT would result in the Pi locking up as the driver never handled
  44. the associated interrupt. Patch adds basic retry mechanism and
  45. interrupt acknowledgement to cater for either a chance toggle error or
  46. for devices that have a broken initial toggle state (FT8U232/FT232BM).
  47. dwc_otg: implement tasklet for returning URBs to usbcore hcd layer
  48. The dwc_otg driver interrupt handler for transfer completion will spend
  49. a very long time with interrupts disabled when a URB is completed -
  50. this is because usb_hcd_giveback_urb is called from within the handler
  51. which for a USB device driver with complicated processing (e.g. webcam)
  52. will take an exorbitant amount of time to complete. This results in
  53. missed completion interrupts for other USB packets which lead to them
  54. being dropped due to microframe overruns.
  55. This patch splits returning the URB to the usb hcd layer into a
  56. high-priority tasklet. This will have most benefit for isochronous IN
  57. transfers but will also have incidental benefit where multiple periodic
  58. devices are active at once.
  59. dwc_otg: fix NAK holdoff and allow on split transactions only
  60. This corrects a bug where if a single active non-periodic endpoint
  61. had at least one transaction in its qh, on frnum == MAX_FRNUM the qh
  62. would get skipped and never get queued again. This would result in
  63. a silent device until error detection (automatic or otherwise) would
  64. either reset the device or flush and requeue the URBs.
  65. Additionally the NAK holdoff was enabled for all transactions - this
  66. would potentially stall a HS endpoint for 1ms if a previous error state
  67. enabled this interrupt and the next response was a NAK. Fix so that
  68. only split transactions get held off.
  69. dwc_otg: Call usb_hcd_unlink_urb_from_ep with lock held in completion handler
  70. usb_hcd_unlink_urb_from_ep must be called with the HCD lock held. Calling it
  71. asynchronously in the tasklet was not safe (regression in
  72. c4564d4a1a0a9b10d4419e48239f5d99e88d2667).
  73. This change unlinks it from the endpoint prior to queueing it for handling in
  74. the tasklet, and also adds a check to ensure the urb is OK to be unlinked
  75. before doing so.
  76. NULL pointer dereference kernel oopses had been observed in usb_hcd_giveback_urb
  77. when a USB device was unplugged/replugged during data transfer. This effect
  78. was reproduced using automated USB port power control, hundreds of replug
  79. events were performed during active transfers to confirm that the problem was
  80. eliminated.
  81. USB fix using a FIQ to implement split transactions
  82. This commit adds a FIQ implementaion that schedules
  83. the split transactions using a FIQ so we don't get
  84. held off by the interrupt latency of Linux
  85. dwc_otg: fix device attributes and avoid kernel warnings on boot
  86. dcw_otg: avoid logging function that can cause panics
  87. See: https://github.com/raspberrypi/firmware/issues/21
  88. Thanks to cleverca22 for fix
  89. dwc_otg: mask correct interrupts after transaction error recovery
  90. The dwc_otg driver will unmask certain interrupts on a transaction
  91. that previously halted in the error state in order to reset the
  92. QTD error count. The various fine-grained interrupt handlers do not
  93. consider that other interrupts besides themselves were unmasked.
  94. By disabling the two other interrupts only ever enabled in DMA mode
  95. for this purpose, we can avoid unnecessary function calls in the
  96. IRQ handler. This will also prevent an unneccesary FIQ interrupt
  97. from being generated if the FIQ is enabled.
  98. dwc_otg: fiq: prevent FIQ thrash and incorrect state passing to IRQ
  99. In the case of a transaction to a device that had previously aborted
  100. due to an error, several interrupts are enabled to reset the error
  101. count when a device responds. This has the side-effect of making the
  102. FIQ thrash because the hardware will generate multiple instances of
  103. a NAK on an IN bulk/interrupt endpoint and multiple instances of ACK
  104. on an OUT bulk/interrupt endpoint. Make the FIQ mask and clear the
  105. associated interrupts.
  106. Additionally, on non-split transactions make sure that only unmasked
  107. interrupts are cleared. This caused a hard-to-trigger but serious
  108. race condition when you had the combination of an endpoint awaiting
  109. error recovery and a transaction completed on an endpoint - due to
  110. the sequencing and timing of interrupts generated by the dwc_otg core,
  111. it was possible to confuse the IRQ handler.
  112. Fix function tracing
  113. dwc_otg: whitespace cleanup in dwc_otg_urb_enqueue
  114. dwc_otg: prevent OOPSes during device disconnects
  115. The dwc_otg_urb_enqueue function is thread-unsafe. In particular the
  116. access of urb->hcpriv, usb_hcd_link_urb_to_ep, dwc_otg_urb->qtd and
  117. friends does not occur within a critical section and so if a device
  118. was unplugged during activity there was a high chance that the
  119. usbcore hub_thread would try to disable the endpoint with partially-
  120. formed entries in the URB queue. This would result in BUG() or null
  121. pointer dereferences.
  122. Fix so that access of urb->hcpriv, enqueuing to the hardware and
  123. adding to usbcore endpoint URB lists is contained within a single
  124. critical section.
  125. dwc_otg: prevent BUG() in TT allocation if hub address is > 16
  126. A fixed-size array is used to track TT allocation. This was
  127. previously set to 16 which caused a crash because
  128. dwc_otg_hcd_allocate_port would read past the end of the array.
  129. This was hit if a hub was plugged in which enumerated as addr > 16,
  130. due to previous device resets or unplugs.
  131. Also add #ifdef FIQ_DEBUG around hcd->hub_port_alloc[], which grows
  132. to a large size if 128 hub addresses are supported. This field is
  133. for debug only for tracking which frame an allocate happened in.
  134. dwc_otg: make channel halts with unknown state less damaging
  135. If the IRQ received a channel halt interrupt through the FIQ
  136. with no other bits set, the IRQ would not release the host
  137. channel and never complete the URB.
  138. Add catchall handling to treat as a transaction error and retry.
  139. dwc_otg: fiq_split: use TTs with more granularity
  140. This fixes certain issues with split transaction scheduling.
  141. - Isochronous multi-packet OUT transactions now hog the TT until
  142. they are completed - this prevents hubs aborting transactions
  143. if they get a periodic start-split out-of-order
  144. - Don't perform TT allocation on non-periodic endpoints - this
  145. allows simultaneous use of the TT's bulk/control and periodic
  146. transaction buffers
  147. This commit will mainly affect USB audio playback.
  148. dwc_otg: fix potential sleep while atomic during urb enqueue
  149. Fixes a regression introduced with eb1b482a. Kmalloc called from
  150. dwc_otg_hcd_qtd_add / dwc_otg_hcd_qtd_create did not always have
  151. the GPF_ATOMIC flag set. Force this flag when inside the larger
  152. critical section.
  153. dwc_otg: make fiq_split_enable imply fiq_fix_enable
  154. Failing to set up the FIQ correctly would result in
  155. "IRQ 32: nobody cared" errors in dmesg.
  156. dwc_otg: prevent crashes on host port disconnects
  157. Fix several issues resulting in crashes or inconsistent state
  158. if a Model A root port was disconnected.
  159. - Clean up queue heads properly in kill_urbs_in_qh_list by
  160. removing the empty QHs from the schedule lists
  161. - Set the halt status properly to prevent IRQ handlers from
  162. using freed memory
  163. - Add fiq_split related cleanup for saved registers
  164. - Make microframe scheduling reclaim host channels if
  165. active during a disconnect
  166. - Abort URBs with -ESHUTDOWN status response, informing
  167. device drivers so they respond in a more correct fashion
  168. and don't try to resubmit URBs
  169. - Prevent IRQ handlers from attempting to handle channel
  170. interrupts if the associated URB was dequeued (and the
  171. driver state was cleared)
  172. dwc_otg: prevent leaking URBs during enqueue
  173. A dwc_otg_urb would get leaked if the HCD enqueue function
  174. failed for any reason. Free the URB at the appropriate points.
  175. dwc_otg: Enable NAK holdoff for control split transactions
  176. Certain low-speed devices take a very long time to complete a
  177. data or status stage of a control transaction, producing NAK
  178. responses until they complete internal processing - the USB2.0
  179. spec limit is up to 500mS. This causes the same type of interrupt
  180. storm as seen with USB-serial dongles prior to c8edb238.
  181. In certain circumstances, usually while booting, this interrupt
  182. storm could cause SD card timeouts.
  183. dwc_otg: Fix for occasional lockup on boot when doing a USB reset
  184. dwc_otg: Don't issue traffic to LS devices in FS mode
  185. Issuing low-speed packets when the root port is in full-speed mode
  186. causes the root port to stop responding. Explicitly fail when
  187. enqueuing URBs to a LS endpoint on a FS bus.
  188. Fix ARM architecture issue with local_irq_restore()
  189. If local_fiq_enable() is called before a local_irq_restore(flags) where
  190. the flags variable has the F bit set, the FIQ will be erroneously disabled.
  191. Fixup arch_local_irq_restore to avoid trampling the F bit in CPSR.
  192. Also fix some of the hacks previously implemented for previous dwc_otg
  193. incarnations.
  194. dwc_otg: fiq_fsm: Base commit for driver rewrite
  195. This commit removes the previous FIQ fixes entirely and adds fiq_fsm.
  196. This rewrite features much more complete support for split transactions
  197. and takes into account several OTG hardware bugs. High-speed
  198. isochronous transactions are also capable of being performed by fiq_fsm.
  199. All driver options have been removed and replaced with:
  200. - dwc_otg.fiq_enable (bool)
  201. - dwc_otg.fiq_fsm_enable (bool)
  202. - dwc_otg.fiq_fsm_mask (bitmask)
  203. - dwc_otg.nak_holdoff (unsigned int)
  204. Defaults are specified such that fiq_fsm behaves similarly to the
  205. previously implemented FIQ fixes.
  206. fiq_fsm: Push error recovery into the FIQ when fiq_fsm is used
  207. If the transfer associated with a QTD failed due to a bus error, the HCD
  208. would retry the transfer up to 3 times (implementing the USB2.0
  209. three-strikes retry in software).
  210. Due to the masking mechanism used by fiq_fsm, it is only possible to pass
  211. a single interrupt through to the HCD per-transfer.
  212. In this instance host channels would fall off the radar because the error
  213. reset would function, but the subsequent channel halt would be lost.
  214. Push the error count reset into the FIQ handler.
  215. fiq_fsm: Implement timeout mechanism
  216. For full-speed endpoints with a large packet size, interrupt latency
  217. runs the risk of the FIQ starting a transaction too late in a full-speed
  218. frame. If the device is still transmitting data when EOF2 for the
  219. downstream frame occurs, the hub will disable the port. This change is
  220. not reflected in the hub status endpoint and the device becomes
  221. unresponsive.
  222. Prevent high-bandwidth transactions from being started too late in a
  223. frame. The mechanism is not guaranteed: a combination of bit stuffing
  224. and hub latency may still result in a device overrunning.
  225. fiq_fsm: fix bounce buffer utilisation for Isochronous OUT
  226. Multi-packet isochronous OUT transactions were subject to a few bounday
  227. bugs. Fix them.
  228. Audio playback is now much more robust: however, an issue stands with
  229. devices that have adaptive sinks - ALSA plays samples too fast.
  230. dwc_otg: Return full-speed frame numbers in HS mode
  231. The frame counter increments on every *microframe* in high-speed mode.
  232. Most device drivers expect this number to be in full-speed frames - this
  233. caused considerable confusion to e.g. snd_usb_audio which uses the
  234. frame counter to estimate the number of samples played.
  235. fiq_fsm: save PID on completion of interrupt OUT transfers
  236. Also add edge case handling for interrupt transports.
  237. Note that for periodic split IN, data toggles are unimplemented in the
  238. OTG host hardware - it unconditionally accepts any PID.
  239. fiq_fsm: add missing case for fiq_fsm_tt_in_use()
  240. Certain combinations of bitrate and endpoint activity could
  241. result in a periodic transaction erroneously getting started
  242. while the previous Isochronous OUT was still active.
  243. fiq_fsm: clear hcintmsk for aborted transactions
  244. Prevents the FIQ from erroneously handling interrupts
  245. on a timed out channel.
  246. fiq_fsm: enable by default
  247. fiq_fsm: fix dequeues for non-periodic split transactions
  248. If a dequeue happened between the SSPLIT and CSPLIT phases of the
  249. transaction, the HCD would never receive an interrupt.
  250. fiq_fsm: Disable by default
  251. fiq_fsm: Handle HC babble errors
  252. The HCTSIZ transfer size field raises a babble interrupt if
  253. the counter wraps. Handle the resulting interrupt in this case.
  254. dwc_otg: fix interrupt registration for fiq_enable=0
  255. Additionally make the module parameter conditional for wherever
  256. hcd->fiq_state is touched.
  257. fiq_fsm: Enable by default
  258. dwc_otg: Fix various issues with root port and transaction errors
  259. Process the host port interrupts correctly (and don't trample them).
  260. Root port hotplug now functional again.
  261. Fix a few thinkos with the transaction error passthrough for fiq_fsm.
  262. fiq_fsm: Implement hack for Split Interrupt transactions
  263. Hubs aren't too picky about which endpoint we send Control type split
  264. transactions to. By treating Interrupt transfers as Control, it is
  265. possible to use the non-periodic queue in the OTG core as well as the
  266. non-periodic FIFOs in the hub itself. This massively reduces the
  267. microframe exclusivity/contention that periodic split transactions
  268. otherwise have to enforce.
  269. It goes without saying that this is a fairly egregious USB specification
  270. violation, but it works.
  271. Original idea by Hans Petter Selasky @ FreeBSD.org.
  272. dwc_otg: FIQ support on SMP. Set up FIQ stack and handler on Core 0 only.
  273. dwc_otg: introduce fiq_fsm_spin(un|)lock()
  274. SMP safety for the FIQ relies on register read-modify write cycles being
  275. completed in the correct order. Several places in the DWC code modify
  276. registers also touched by the FIQ. Protect these by a bare-bones lock
  277. mechanism.
  278. This also makes it possible to run the FIQ and IRQ handlers on different
  279. cores.
  280. fiq_fsm: fix build on bcm2708 and bcm2709 platforms
  281. dwc_otg: put some barriers back where they should be for UP
  282. bcm2709/dwc_otg: Setup FIQ on core 1 if >1 core active
  283. dwc_otg: fixup read-modify-write in critical paths
  284. Be more careful about read-modify-write on registers that the FIQ
  285. also touches.
  286. Guard fiq_fsm_spin_lock with fiq_enable check
  287. fiq_fsm: Falling out of the state machine isn't fatal
  288. This edge case can be hit if the port is disabled while the FIQ is
  289. in the middle of a transaction. Make the effects less severe.
  290. Also get rid of the useless return value.
  291. squash: dwc_otg: Allow to build without SMP
  292. usb: core: make overcurrent messages more prominent
  293. Hub overcurrent messages are more serious than "debug". Increase loglevel.
  294. usb: dwc_otg: Don't use dma_to_virt()
  295. Commit 6ce0d20 changes dma_to_virt() which breaks this driver.
  296. Open code the old dma_to_virt() implementation to work around this.
  297. Limit the use of __bus_to_virt() to cases where transfer_buffer_length
  298. is set and transfer_buffer is not set. This is done to increase the
  299. chance that this driver will also work on ARCH_BCM2835.
  300. transfer_buffer should not be NULL if the length is set, but the
  301. comment in the code indicates that there are situations where this
  302. might happen. drivers/usb/isp1760/isp1760-hcd.c also has a similar
  303. comment pointing to a possible: 'usb storage / SCSI bug'.
  304. Signed-off-by: Noralf Trønnes <[email protected]>
  305. dwc_otg: Fix crash when fiq_enable=0
  306. dwc_otg: fiq_fsm: Make high-speed isochronous strided transfers work properly
  307. Certain low-bandwidth high-speed USB devices (specialist audio devices,
  308. compressed-frame webcams) have packet intervals > 1 microframe.
  309. Stride these transfers in the FIQ by using the start-of-frame interrupt
  310. to restart the channel at the right time.
  311. dwc_otg: Force host mode to fix incorrect compute module boards
  312. dwc_otg: Add ARCH_BCM2835 support
  313. Signed-off-by: Noralf Trønnes <[email protected]>
  314. dwc_otg: Simplify FIQ irq number code
  315. Dropping ATAGS means we can simplify the FIQ irq number code.
  316. Also add error checking on the returned irq number.
  317. Signed-off-by: Noralf Trønnes <[email protected]>
  318. dwc_otg: Remove duplicate gadget probe/unregister function
  319. dwc_otg: Properly set the HFIR
  320. Douglas Anderson reported:
  321. According to the most up to date version of the dwc2 databook, the FRINT
  322. field of the HFIR register should be programmed to:
  323. * 125 us * (PHY clock freq for HS) - 1
  324. * 1000 us * (PHY clock freq for FS/LS) - 1
  325. This is opposed to older versions of the doc that claimed it should be:
  326. * 125 us * (PHY clock freq for HS)
  327. * 1000 us * (PHY clock freq for FS/LS)
  328. and reported lower timing jitter on a USB analyser
  329. dcw_otg: trim xfer length when buffer larger than allocated size is received
  330. dwc_otg: Don't free qh align buffers in atomic context
  331. dwc_otg: Enable the hack for Split Interrupt transactions by default
  332. dwc_otg.fiq_fsm_mask=0xF has long been a suggestion for users with audio stutters or other USB bandwidth issues.
  333. So far we are aware of many success stories but no failure caused by this setting.
  334. Make it a default to learn more.
  335. See: https://www.raspberrypi.org/forums/viewtopic.php?f=28&t=70437
  336. Signed-off-by: popcornmix <[email protected]>
  337. dwc_otg: Use kzalloc when suitable
  338. dwc_otg: Pass struct device to dma_alloc*()
  339. This makes it possible to get the bus address from Device Tree.
  340. Signed-off-by: Noralf Trønnes <[email protected]>
  341. dwc_otg: fix summarize urb->actual_length for isochronous transfers
  342. Kernel does not copy input data of ISO transfers to userspace
  343. if actual_length is set only in ISO transfers and not summarized
  344. in urb->actual_length. Fixes raspberrypi/linux#903
  345. fiq_fsm: Use correct states when starting isoc OUT transfers
  346. In fiq_fsm_start_next_periodic() if an isochronous OUT transfer
  347. was selected, no regard was given as to whether this was a single-packet
  348. transfer or a multi-packet staged transfer.
  349. For single-packet transfers, this had the effect of repeatedly sending
  350. OUT packets with bogus data and lengths.
  351. Eventually if the channel was repeatedly enabled enough times, this
  352. would lock up the OTG core and no further bus transfers would happen.
  353. Set the FSM state up properly if we select a single-packet transfer.
  354. Fixes https://github.com/raspberrypi/linux/issues/1842
  355. dwc_otg: make nak_holdoff work as intended with empty queues
  356. If URBs reading from non-periodic split endpoints were dequeued and
  357. the last transfer from the endpoint was a NAK handshake, the resulting
  358. qh->nak_frame value was stale which would result in unnecessarily long
  359. polling intervals for the first subsequent transfer with a fresh URB.
  360. Fixup qh->nak_frame in dwc_otg_hcd_urb_dequeue and also guard against
  361. a case where a single URB is submitted to the endpoint, a NAK was
  362. received on the transfer immediately prior to receiving data and the
  363. device subsequently resubmits another URB past the qh->nak_frame interval.
  364. Fixes https://github.com/raspberrypi/linux/issues/1709
  365. dwc_otg: fix split transaction data toggle handling around dequeues
  366. See https://github.com/raspberrypi/linux/issues/1709
  367. Fix several issues regarding endpoint state when URBs are dequeued
  368. - If the HCD is disconnected, flush FIQ-enabled channels properly
  369. - Save the data toggle state for bulk endpoints if the last transfer
  370. from an endpoint where URBs were dequeued returned a data packet
  371. - Reset hc->start_pkt_count properly in assign_and_init_hc()
  372. dwc_otg: fix several potential crash sources
  373. On root port disconnect events, the host driver state is cleared and
  374. in-progress host channels are forcibly stopped. This doesn't play
  375. well with the FIQ running in the background, so:
  376. - Guard the disconnect callback with both the host spinlock and FIQ
  377. spinlock
  378. - Move qtd dereference in dwc_otg_handle_hc_fsm() after the early-out
  379. so we don't dereference a qtd that has gone away
  380. - Turn catch-all BUG()s in dwc_otg_handle_hc_fsm() into warnings.
  381. dwc_otg: delete hcd->channel_lock
  382. The lock serves no purpose as it is only held while the HCD spinlock
  383. is already being held.
  384. dwc_otg: remove unnecessary dma-mode channel halts on disconnect interrupt
  385. Host channels are already halted in kill_urbs_in_qh_list() with the
  386. subsequent interrupt processing behaving as if the URB was dequeued
  387. via HCD callback.
  388. There's no need to clobber the host channel registers a second time
  389. as this exposes races between the driver and host channel resulting
  390. in hcd->free_hc_list becoming corrupted.
  391. dwcotg: Allow to build without FIQ on ARM64
  392. Signed-off-by: popcornmix <[email protected]>
  393. dwc_otg: make periodic scheduling behave properly for FS buses
  394. If the root port is in full-speed mode, transfer times at 12mbit/s
  395. would be calculated but matched against high-speed quotas.
  396. Reinitialise hcd->frame_usecs[i] on each port enable event so that
  397. full-speed bandwidth can be tracked sensibly.
  398. Also, don't bother using the FIQ for transfers when in full-speed
  399. mode - at the slower bus speed, interrupt frequency is reduced by
  400. an order of magnitude.
  401. Related issue: https://github.com/raspberrypi/linux/issues/2020
  402. dwc_otg: fiq_fsm: Make isochronous compatibility checks work properly
  403. Get rid of the spammy printk and local pointer mangling.
  404. Also, there is a nominal benefit for using fiq_fsm for isochronous
  405. transfers in FS mode (~1.1k IRQs per second vs 2.1k IRQs per second)
  406. so remove the root port speed check.
  407. dwc_otg: add module parameter int_ep_interval_min
  408. Add a module parameter (defaulting to ignored) that clamps the polling rate
  409. of high-speed Interrupt endpoints to a minimum microframe interval.
  410. The parameter is modifiable at runtime as it is used when activating new
  411. endpoints (such as on device connect).
  412. dwc_otg: fiq_fsm: Add non-periodic TT exclusivity constraints
  413. Certain hub types do not discriminate between pipe direction (IN or OUT)
  414. when considering non-periodic transfers. Therefore these hubs get confused
  415. if multiple transfers are issued in different directions with the same
  416. device address and endpoint number.
  417. Constrain queuing non-periodic split transactions so they are performed
  418. serially in such cases.
  419. Related: https://github.com/raspberrypi/linux/issues/2024
  420. dwc_otg: Fixup change to DRIVER_ATTR interface
  421. dwc_otg: Fix compilation warnings
  422. Signed-off-by: Phil Elwell <[email protected]>
  423. USB_DWCOTG: Disable building dwc_otg as a module (#2265)
  424. When dwc_otg is built as a module, build will fail with the following
  425. error:
  426. ERROR: "DWC_TASK_HI_SCHEDULE" [drivers/usb/host/dwc_otg/dwc_otg.ko] undefined!
  427. scripts/Makefile.modpost:91: recipe for target '__modpost' failed
  428. make[1]: *** [__modpost] Error 1
  429. Makefile:1199: recipe for target 'modules' failed
  430. make: *** [modules] Error 2
  431. Even if the error is solved by including the missing
  432. DWC_TASK_HI_SCHEDULE function, the kernel will panic when loading
  433. dwc_otg.
  434. As a workaround, simply prevent user from building dwc_otg as a module
  435. as the current kernel does not support it.
  436. See: https://github.com/raspberrypi/linux/issues/2258
  437. Signed-off-by: Malik Olivier Boussejra <[email protected]>
  438. dwc_otg: New timer API
  439. dwc_otg: Fix removed ACCESS_ONCE->READ_ONCE
  440. dwc_otg: don't unconditionally force host mode in dwc_otg_cil_init()
  441. Add the ability to disable force_host_mode for those that want to use
  442. dwc_otg in both device and host modes.
  443. dwc_otg: Fix a regression when dequeueing isochronous transfers
  444. In 282bed95 (dwc_otg: make nak_holdoff work as intended with empty queues)
  445. the dequeue mechanism was changed to leave FIQ-enabled transfers to run
  446. to completion - to avoid leaving hub TT buffers with stale packets lying
  447. around.
  448. This broke FIQ-accelerated isochronous transfers, as this then meant that
  449. dozens of transfers were performed after the dequeue function returned.
  450. Restore the state machine fence for isochronous transfers.
  451. fiq_fsm: rewind DMA pointer for OUT transactions that fail (#2288)
  452. See: https://github.com/raspberrypi/linux/issues/2140
  453. dwc_otg: add smp_mb() to prevent driver state corruption on boot
  454. Occasional crashes have been seen where the FIQ code dereferences
  455. invalid/random pointers immediately after being set up, leading to
  456. panic on boot.
  457. The crash occurs as the FIQ code races against hcd_init_fiq() and
  458. the hcd_init_fiq() code races against the outstanding memory stores
  459. from dwc_otg_hcd_init(). Use explicit barriers after touching
  460. driver state.
  461. usb: dwc_otg: fix memory corruption in dwc_otg driver
  462. [Upstream commit 51b1b6491752ac066ee8d32cc66042fcc955fef6]
  463. The move from the staging tree to the main tree exposed a
  464. longstanding memory corruption bug in the dwc2 driver. The
  465. reordering of the driver initialization caused the dwc2 driver
  466. to corrupt the initialization data of the sdhci driver on the
  467. Raspberry Pi platform, which made the bug show up.
  468. The error is in calling to_usb_device(hsotg->dev), since ->dev
  469. is not a member of struct usb_device. The easiest fix is to
  470. just remove the offending code, since it is not really needed.
  471. Thanks to Stephen Warren for tracking down the cause of this.
  472. Reported-by: Andre Heider <[email protected]>
  473. Tested-by: Stephen Warren <[email protected]>
  474. Signed-off-by: Paul Zimmerman <[email protected]>
  475. Signed-off-by: Greg Kroah-Hartman <[email protected]>
  476. [lukas: port from upstream dwc2 to out-of-tree dwc_otg driver]
  477. Signed-off-by: Lukas Wunner <[email protected]>
  478. usb: dwb_otg: Fix unreachable switch statement warning
  479. This warning appears with GCC 7.3.0 from toolchains.bootlin.com:
  480. ../drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.c: In function ‘fiq_fsm_update_hs_isoc’:
  481. ../drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.c:595:61: warning: statement will never be executed [-Wswitch-unreachable]
  482. st->hctsiz_copy.b.xfersize = nrpackets * st->hcchar_copy.b.mps;
  483. ~~~~~~~~~~~~~~~~~^~~~
  484. Signed-off-by: Nathan Chancellor <[email protected]>
  485. dwc_otg: fiq_fsm: fix incorrect DMA register offset calculation
  486. Rationalise the offset and update all call sites.
  487. Fixes https://github.com/raspberrypi/linux/issues/2408
  488. dwc_otg: fix bug with port_addr assignment for single-TT hubs
  489. See https://github.com/raspberrypi/linux/issues/2734
  490. The "Hub Port" field in the split transaction packet was always set
  491. to 1 for single-TT hubs. The majority of single-TT hub products
  492. apparently ignore this field and broadcast to all downstream enabled
  493. ports, which masked the issue. A subset of hub devices apparently
  494. need the port number to be exact or split transactions will fail.
  495. usb: dwc_otg: Clean up build warnings on 64bit kernels
  496. No functional changes. Almost all are changes to logging lines.
  497. Signed-off-by: Dave Stevenson <[email protected]>
  498. usb: dwc_otg: Use dma allocation for mphi dummy_send buffer
  499. The FIQ driver used a kzalloc'ed buffer for dummy_send,
  500. passing a kernel virtual address to the hardware block.
  501. The buffer is only ever used for a dummy read, so it
  502. should be harmless, but there is the chance that it will
  503. cause exceptions.
  504. Use a dma allocation so that we have a genuine bus address,
  505. and read from that.
  506. Free the allocation when done for good measure.
  507. Signed-off-by: Dave Stevenson <[email protected]>
  508. dwc_otg: only do_split when we actually need to do a split
  509. The previous test would fail if the root port was in fullspeed mode
  510. and there was a hub between the FS device and the root port. While
  511. the transfer worked, the schedule mangling performed for high-speed
  512. split transfers would break leading to an 8ms polling interval.
  513. dwc_otg: fix locking around dequeueing and killing URBs
  514. kill_urbs_in_qh_list() is practically only ever called with the fiq lock
  515. already held, so don't spinlock twice in the case where we need to cancel
  516. an isochronous transfer.
  517. Also fix up a case where the global interrupt register could be read with
  518. the fiq lock not held.
  519. Fixes the deadlock seen in https://github.com/raspberrypi/linux/issues/2907
  520. ARM64/DWC_OTG: Port dwc_otg driver to ARM64
  521. In ARM64, the FIQ mechanism used by this driver is not current
  522. implemented. As a workaround, reqular IRQ is used instead
  523. of FIQ.
  524. In a separate change, the IRQ-CPU mapping is round robined
  525. on ARM64 to increase concurrency and allow multiple interrupts
  526. to be serviced at a time. This reduces the need for FIQ.
  527. Tests Run:
  528. This mechanism is most likely to break when multiple USB devices
  529. are attached at the same time. So the system was tested under
  530. stress.
  531. Devices:
  532. 1. USB Speakers playing back a FLAC audio through VLC
  533. at 96KHz.(Higher then typically, but supported on my speakers).
  534. 2. sftp transferring large files through the buildin ethernet
  535. connection which is connected through USB.
  536. 3. Keyboard and mouse attached and being used.
  537. Although I do occasionally hear some glitches, the music seems to
  538. play quite well.
  539. Signed-off-by: Michael Zoran <[email protected]>
  540. usb: dwc_otg: Clean up interrupt claiming code
  541. The FIQ/IRQ interrupt number identification code is scattered through
  542. the dwc_otg driver. Rationalise it, simplifying the code and solving
  543. an existing issue.
  544. See: https://github.com/raspberrypi/linux/issues/2612
  545. Signed-off-by: Phil Elwell <[email protected]>
  546. dwc_otg: Choose appropriate IRQ handover strategy
  547. 2711 has no MPHI peripheral, but the ARM Control block can fake
  548. interrupts. Use the size of the DTB "mphi" reg block to determine
  549. which is required.
  550. Signed-off-by: Phil Elwell <[email protected]>
  551. usb: host: dwc_otg: fix compiling in separate directory
  552. The dwc_otg Makefile does not respect the O=path argument correctly:
  553. include paths in CFLAGS are given relatively to object path, not source
  554. path. Compiling in a separate directory yields #include errors.
  555. Signed-off-by: Marek Behún <[email protected]>
  556. dwc_otg: use align_buf for small IN control transfers (#3150)
  557. The hardware will do a 4-byte write to memory on any IN packet received
  558. that is between 1 and 3 bytes long. This tramples memory in the uvcvideo
  559. driver, as it uses a sequence of 1- and 2-byte control transfers to
  560. query the min/max/range/step of each individual camera control and
  561. gives us buffers that are offsets into a struct.
  562. Catch small control transfers in the data phase and use the align_buf
  563. to bounce the correct number of bytes into the URB's buffer.
  564. In general, short packets on non-control endpoints should be OK as URBs
  565. should have enough buffer space for a wMaxPacket size transfer.
  566. See: https://github.com/raspberrypi/linux/issues/3148
  567. Signed-off-by: Jonathan Bell <[email protected]>
  568. dwc_otg: Declare DMA capability with HCD_DMA flag
  569. Following [1], USB controllers have to declare DMA capabilities in
  570. order for them to be used by adding the HCD_DMA flag to their hc_driver
  571. struct.
  572. [1] 7b81cb6bddd2 ("usb: add a HCD_DMA flag instead of guestimating DMA capabilities")
  573. Signed-off-by: Phil Elwell <[email protected]>
  574. dwc_otg: checking the urb->transfer_buffer too early (#3332)
  575. After enable the HIGHMEM and VMSPLIT_3G, the dwc_otg driver doesn't
  576. work well on Pi2/3 boards with 1G physical ram. Users experience
  577. the failure when copying a file of 600M size to the USB stick. And
  578. at the same time, the dmesg shows:
  579. usb 1-1.1.2: reset high-speed USB device number 8 using dwc_otg
  580. sd 0:0:0:0: [sda] tag#0 FAILED Result: hostbyte=DID_ERROR driverbyte=DRIVER_OK
  581. blk_update_request: I/O error, dev sda, sector 3024048 op 0x1:(WRITE) flags 0x4000 phys_seg 15 prio class 0
  582. When this happens, the sg_buf sent to the driver is located in the
  583. highmem region, the usb_sg_init() in the core/message.c will leave
  584. transfer_buffer to NULL if the sg_buf is in highmem, but in the
  585. dwc_otg driver, it returns -EINVAL unconditionally if transfer_buffer
  586. is NULL.
  587. The driver can handle the situation of buffer to be NULL, if it is in
  588. DMA mode, it will convert an address from transfer_dma.
  589. But if the conversion fails or it is in the PIO mode, we should check
  590. buffer and return -EINVAL if it is NULL.
  591. BugLink: https://bugs.launchpad.net/bugs/1852510
  592. Signed-off-by: Hui Wang <[email protected]>
  593. dwc_otg: constrain endpoint max packet and transfer size on split IN
  594. The hcd would unconditionally set the transfer length to the endpoint
  595. packet size for non-isoc IN transfers. If the remaining buffer length
  596. was less than the length of returned data, random memory would get
  597. scribbled over, with bad effects if it crossed a page boundary.
  598. Force a babble error if this happens by limiting the max transfer size
  599. to the available buffer space. DMA will stop writing to memory on a
  600. babble condition.
  601. The hardware expects xfersize to be an integer multiple of maxpacket
  602. size, so override hcchar.b.mps as well.
  603. Signed-off-by: Jonathan Bell <[email protected]>
  604. dwc_otg: fiq_fsm: pause when cancelling split transactions
  605. Non-periodic splits will DMA to/from the driver-provided transfer_buffer,
  606. which may be freed immediately after the dequeue call returns. Block until
  607. we know the transfer is complete.
  608. A similar delay is needed when cleaning up disconnects, as the FIQ could
  609. have started a periodic transfer in the previous microframe to the one
  610. that triggered a disconnect.
  611. Signed-off-by: Jonathan Bell <[email protected]>
  612. dwc_otg: fiq_fsm: add a barrier on entry into FIQ handler(s)
  613. On BCM2835, there is no hardware guarantee that multiple outstanding
  614. reads to different peripherals will complete in-order. The FIQ code
  615. uses peripheral reads without barriers for performance, so in the case
  616. where a read to a slow peripheral was issued immediately prior to FIQ
  617. entry, the first peripheral read that the FIQ did could end up with
  618. wrong read data returned.
  619. Add dsb(sy) on entry so that all outstanding reads are retired.
  620. The FIQ only issues reads to the dwc_otg core, so per-read barriers
  621. in the handler itself are not required.
  622. On BCM2836 and BCM2837 the barrier is not strictly required due to
  623. differences in how the peripheral bus is implemented, but having
  624. arch-specific handlers that introduce different latencies is risky.
  625. Signed-off-by: Jonathan Bell <[email protected]>
  626. dwc_otg: whitelist_table is now productlist_table
  627. dwc_otg: initialise sched_frame for periodic QHs that were parked
  628. If a periodic QH has no remaining QTDs, then it is removed from all
  629. periodic schedules. When re-adding, initialise the sched_frame and
  630. start_split_frame from the current value of the frame counter.
  631. See https://bugs.launchpad.net/raspbian/+bug/1819560
  632. and
  633. https://github.com/raspberrypi/linux/issues/3883
  634. Signed-off-by: Jonathan Bell <[email protected]>
  635. dwc_otg: Minimise header and fix build warnings
  636. Delete a large amount of unused declaration from "usb.h", some of which
  637. were causing build warnings, and get the module building cleanly.
  638. Signed-off-by: Phil Elwell <[email protected]>
  639. dwc-otg: fix clang -Wignored-attributes warning
  640. warning: attribute declaration must precede definition
  641. dwc-otg: fix clang -Wsometimes-uninitialized warning
  642. warning: variable 'retval' is used uninitialized whenever 'if' condition is false
  643. dwc-otg: fix clang -Wpointer-bool-conversion warning
  644. warning: address of array 'desc->wMaxPacketSize' will always evaluate to 'true'
  645. The wMaxPacketSize field is actually a two element array which content should
  646. be accessed via the UGETW macro.
  647. dwc_otg: fix an undeclared variable
  648. Replace an undeclared variable used by DWC_DEBUGPL with the real endpoint address. DWC_DEBUGPL does nothing with DEBUG undefined so it did not go wrong before.
  649. Signed-off-by: Zixuan Wang <[email protected]>
  650. dwc_otg: Update NetBSD usb.h header licence
  651. NetBSD have changed their licensing requirements such that the 2-clause
  652. licence is preferred. Update usb.h in the downstream dwc_otg code
  653. accordingly.
  654. See https://www.netbsd.org/about/redistribution.html for more
  655. information.
  656. Signed-off-by: Phil Elwell <[email protected]>
  657. dwc_otg: pay attention to qh->interval when rescheduling periodic queues
  658. A regression introduced in https://github.com/raspberrypi/linux/pull/3887
  659. meant that if the newly scheduled transfer immediately returned data, and
  660. the driver resubmitted a single URB after every transfer, then the effective
  661. polling interval would end up being approx 1ms.
  662. Use the larger of SCHEDULE_SLOP or the configured endpoint interval.
  663. Signed-off-by: Jonathan Bell <[email protected]>
  664. ---
  665. arch/arm/include/asm/irqflags.h | 16 +-
  666. arch/arm/kernel/fiqasm.S | 4 +
  667. drivers/usb/Makefile | 1 +
  668. drivers/usb/core/generic.c | 1 +
  669. drivers/usb/core/hub.c | 2 +-
  670. drivers/usb/core/message.c | 79 +
  671. drivers/usb/core/otg_productlist.h | 114 +-
  672. drivers/usb/gadget/file_storage.c | 3676 +++++++++
  673. drivers/usb/host/Kconfig | 10 +
  674. drivers/usb/host/Makefile | 1 +
  675. drivers/usb/host/dwc_common_port/Makefile | 58 +
  676. .../usb/host/dwc_common_port/Makefile.fbsd | 17 +
  677. .../usb/host/dwc_common_port/Makefile.linux | 49 +
  678. drivers/usb/host/dwc_common_port/changes.txt | 174 +
  679. .../usb/host/dwc_common_port/doc/doxygen.cfg | 270 +
  680. drivers/usb/host/dwc_common_port/dwc_cc.c | 532 ++
  681. drivers/usb/host/dwc_common_port/dwc_cc.h | 224 +
  682. .../host/dwc_common_port/dwc_common_fbsd.c | 1308 +++
  683. .../host/dwc_common_port/dwc_common_linux.c | 1409 ++++
  684. .../host/dwc_common_port/dwc_common_nbsd.c | 1275 +++
  685. drivers/usb/host/dwc_common_port/dwc_crypto.c | 308 +
  686. drivers/usb/host/dwc_common_port/dwc_crypto.h | 111 +
  687. drivers/usb/host/dwc_common_port/dwc_dh.c | 291 +
  688. drivers/usb/host/dwc_common_port/dwc_dh.h | 106 +
  689. drivers/usb/host/dwc_common_port/dwc_list.h | 594 ++
  690. drivers/usb/host/dwc_common_port/dwc_mem.c | 245 +
  691. drivers/usb/host/dwc_common_port/dwc_modpow.c | 636 ++
  692. drivers/usb/host/dwc_common_port/dwc_modpow.h | 34 +
  693. .../usb/host/dwc_common_port/dwc_notifier.c | 319 +
  694. .../usb/host/dwc_common_port/dwc_notifier.h | 122 +
  695. drivers/usb/host/dwc_common_port/dwc_os.h | 1275 +++
  696. drivers/usb/host/dwc_common_port/usb.h | 275 +
  697. drivers/usb/host/dwc_otg/Makefile | 85 +
  698. drivers/usb/host/dwc_otg/doc/doxygen.cfg | 224 +
  699. drivers/usb/host/dwc_otg/dummy_audio.c | 1574 ++++
  700. drivers/usb/host/dwc_otg/dwc_cfi_common.h | 142 +
  701. drivers/usb/host/dwc_otg/dwc_otg_adp.c | 854 ++
  702. drivers/usb/host/dwc_otg/dwc_otg_adp.h | 80 +
  703. drivers/usb/host/dwc_otg/dwc_otg_attr.c | 1212 +++
  704. drivers/usb/host/dwc_otg/dwc_otg_attr.h | 89 +
  705. drivers/usb/host/dwc_otg/dwc_otg_cfi.c | 1876 +++++
  706. drivers/usb/host/dwc_otg/dwc_otg_cfi.h | 320 +
  707. drivers/usb/host/dwc_otg/dwc_otg_cil.c | 7146 +++++++++++++++++
  708. drivers/usb/host/dwc_otg/dwc_otg_cil.h | 1464 ++++
  709. drivers/usb/host/dwc_otg/dwc_otg_cil_intr.c | 1601 ++++
  710. drivers/usb/host/dwc_otg/dwc_otg_core_if.h | 705 ++
  711. drivers/usb/host/dwc_otg/dwc_otg_dbg.h | 117 +
  712. drivers/usb/host/dwc_otg/dwc_otg_driver.c | 1772 ++++
  713. drivers/usb/host/dwc_otg/dwc_otg_driver.h | 86 +
  714. drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.c | 1433 ++++
  715. drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.h | 399 +
  716. drivers/usb/host/dwc_otg/dwc_otg_fiq_stub.S | 80 +
  717. drivers/usb/host/dwc_otg/dwc_otg_hcd.c | 4363 ++++++++++
  718. drivers/usb/host/dwc_otg/dwc_otg_hcd.h | 870 ++
  719. drivers/usb/host/dwc_otg/dwc_otg_hcd_ddma.c | 1135 +++
  720. drivers/usb/host/dwc_otg/dwc_otg_hcd_if.h | 421 +
  721. drivers/usb/host/dwc_otg/dwc_otg_hcd_intr.c | 2757 +++++++
  722. drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c | 1086 +++
  723. drivers/usb/host/dwc_otg/dwc_otg_hcd_queue.c | 974 +++
  724. drivers/usb/host/dwc_otg/dwc_otg_os_dep.h | 200 +
  725. drivers/usb/host/dwc_otg/dwc_otg_pcd.c | 2725 +++++++
  726. drivers/usb/host/dwc_otg/dwc_otg_pcd.h | 273 +
  727. drivers/usb/host/dwc_otg/dwc_otg_pcd_if.h | 361 +
  728. drivers/usb/host/dwc_otg/dwc_otg_pcd_intr.c | 5148 ++++++++++++
  729. drivers/usb/host/dwc_otg/dwc_otg_pcd_linux.c | 1262 +++
  730. drivers/usb/host/dwc_otg/dwc_otg_regs.h | 2550 ++++++
  731. drivers/usb/host/dwc_otg/test/Makefile | 16 +
  732. drivers/usb/host/dwc_otg/test/dwc_otg_test.pm | 337 +
  733. .../usb/host/dwc_otg/test/test_mod_param.pl | 133 +
  734. drivers/usb/host/dwc_otg/test/test_sysfs.pl | 193 +
  735. 70 files changed, 59583 insertions(+), 16 deletions(-)
  736. create mode 100644 drivers/usb/gadget/file_storage.c
  737. create mode 100644 drivers/usb/host/dwc_common_port/Makefile
  738. create mode 100644 drivers/usb/host/dwc_common_port/Makefile.fbsd
  739. create mode 100644 drivers/usb/host/dwc_common_port/Makefile.linux
  740. create mode 100644 drivers/usb/host/dwc_common_port/changes.txt
  741. create mode 100644 drivers/usb/host/dwc_common_port/doc/doxygen.cfg
  742. create mode 100644 drivers/usb/host/dwc_common_port/dwc_cc.c
  743. create mode 100644 drivers/usb/host/dwc_common_port/dwc_cc.h
  744. create mode 100644 drivers/usb/host/dwc_common_port/dwc_common_fbsd.c
  745. create mode 100644 drivers/usb/host/dwc_common_port/dwc_common_linux.c
  746. create mode 100644 drivers/usb/host/dwc_common_port/dwc_common_nbsd.c
  747. create mode 100644 drivers/usb/host/dwc_common_port/dwc_crypto.c
  748. create mode 100644 drivers/usb/host/dwc_common_port/dwc_crypto.h
  749. create mode 100644 drivers/usb/host/dwc_common_port/dwc_dh.c
  750. create mode 100644 drivers/usb/host/dwc_common_port/dwc_dh.h
  751. create mode 100644 drivers/usb/host/dwc_common_port/dwc_list.h
  752. create mode 100644 drivers/usb/host/dwc_common_port/dwc_mem.c
  753. create mode 100644 drivers/usb/host/dwc_common_port/dwc_modpow.c
  754. create mode 100644 drivers/usb/host/dwc_common_port/dwc_modpow.h
  755. create mode 100644 drivers/usb/host/dwc_common_port/dwc_notifier.c
  756. create mode 100644 drivers/usb/host/dwc_common_port/dwc_notifier.h
  757. create mode 100644 drivers/usb/host/dwc_common_port/dwc_os.h
  758. create mode 100644 drivers/usb/host/dwc_common_port/usb.h
  759. create mode 100644 drivers/usb/host/dwc_otg/Makefile
  760. create mode 100644 drivers/usb/host/dwc_otg/doc/doxygen.cfg
  761. create mode 100644 drivers/usb/host/dwc_otg/dummy_audio.c
  762. create mode 100644 drivers/usb/host/dwc_otg/dwc_cfi_common.h
  763. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_adp.c
  764. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_adp.h
  765. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_attr.c
  766. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_attr.h
  767. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_cfi.c
  768. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_cfi.h
  769. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_cil.c
  770. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_cil.h
  771. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_cil_intr.c
  772. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_core_if.h
  773. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_dbg.h
  774. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_driver.c
  775. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_driver.h
  776. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.c
  777. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.h
  778. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_fiq_stub.S
  779. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd.c
  780. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd.h
  781. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd_ddma.c
  782. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd_if.h
  783. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd_intr.c
  784. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c
  785. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd_queue.c
  786. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_os_dep.h
  787. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_pcd.c
  788. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_pcd.h
  789. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_pcd_if.h
  790. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_pcd_intr.c
  791. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_pcd_linux.c
  792. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_regs.h
  793. create mode 100644 drivers/usb/host/dwc_otg/test/Makefile
  794. create mode 100644 drivers/usb/host/dwc_otg/test/dwc_otg_test.pm
  795. create mode 100644 drivers/usb/host/dwc_otg/test/test_mod_param.pl
  796. create mode 100644 drivers/usb/host/dwc_otg/test/test_sysfs.pl
  797. --- a/arch/arm/include/asm/irqflags.h
  798. +++ b/arch/arm/include/asm/irqflags.h
  799. @@ -163,13 +163,23 @@ static inline unsigned long arch_local_s
  800. }
  801. /*
  802. - * restore saved IRQ & FIQ state
  803. + * restore saved IRQ state
  804. */
  805. #define arch_local_irq_restore arch_local_irq_restore
  806. static inline void arch_local_irq_restore(unsigned long flags)
  807. {
  808. - asm volatile(
  809. - " msr " IRQMASK_REG_NAME_W ", %0 @ local_irq_restore"
  810. + unsigned long temp = 0;
  811. + flags &= ~(1 << 6);
  812. + asm volatile (
  813. + " mrs %0, cpsr"
  814. + : "=r" (temp)
  815. + :
  816. + : "memory", "cc");
  817. + /* Preserve FIQ bit */
  818. + temp &= (1 << 6);
  819. + flags = flags | temp;
  820. + asm volatile (
  821. + " msr cpsr_c, %0 @ local_irq_restore"
  822. :
  823. : "r" (flags)
  824. : "memory", "cc");
  825. --- a/arch/arm/kernel/fiqasm.S
  826. +++ b/arch/arm/kernel/fiqasm.S
  827. @@ -47,3 +47,7 @@ ENTRY(__get_fiq_regs)
  828. mov r0, r0 @ avoid hazard prior to ARMv4
  829. ret lr
  830. ENDPROC(__get_fiq_regs)
  831. +
  832. +ENTRY(__FIQ_Branch)
  833. + mov pc, r8
  834. +ENDPROC(__FIQ_Branch)
  835. --- a/drivers/usb/Makefile
  836. +++ b/drivers/usb/Makefile
  837. @@ -9,6 +9,7 @@ obj-$(CONFIG_USB_COMMON) += common/
  838. obj-$(CONFIG_USB) += core/
  839. obj-$(CONFIG_USB_SUPPORT) += phy/
  840. +obj-$(CONFIG_USB_DWCOTG) += host/
  841. obj-$(CONFIG_USB_DWC3) += dwc3/
  842. obj-$(CONFIG_USB_DWC2) += dwc2/
  843. obj-$(CONFIG_USB_ISP1760) += isp1760/
  844. --- a/drivers/usb/core/generic.c
  845. +++ b/drivers/usb/core/generic.c
  846. @@ -190,6 +190,7 @@ int usb_choose_configuration(struct usb_
  847. dev_warn(&udev->dev,
  848. "no configuration chosen from %d choice%s\n",
  849. num_configs, plural(num_configs));
  850. + dev_warn(&udev->dev, "No support over %dmA\n", udev->bus_mA);
  851. }
  852. return i;
  853. }
  854. --- a/drivers/usb/core/hub.c
  855. +++ b/drivers/usb/core/hub.c
  856. @@ -5677,7 +5677,7 @@ static void port_event(struct usb_hub *h
  857. port_dev->over_current_count++;
  858. port_over_current_notify(port_dev);
  859. - dev_dbg(&port_dev->dev, "over-current change #%u\n",
  860. + dev_notice(&port_dev->dev, "over-current change #%u\n",
  861. port_dev->over_current_count);
  862. usb_clear_port_feature(hdev, port1,
  863. USB_PORT_FEAT_C_OVER_CURRENT);
  864. --- a/drivers/usb/core/message.c
  865. +++ b/drivers/usb/core/message.c
  866. @@ -2135,6 +2135,85 @@ free_interfaces:
  867. if (cp->string == NULL &&
  868. !(dev->quirks & USB_QUIRK_CONFIG_INTF_STRINGS))
  869. cp->string = usb_cache_string(dev, cp->desc.iConfiguration);
  870. +/* Uncomment this define to enable the HS Electrical Test support */
  871. +#define DWC_HS_ELECT_TST 1
  872. +#ifdef DWC_HS_ELECT_TST
  873. + /* Here we implement the HS Electrical Test support. The
  874. + * tester uses a vendor ID of 0x1A0A to indicate we should
  875. + * run a special test sequence. The product ID tells us
  876. + * which sequence to run. We invoke the test sequence by
  877. + * sending a non-standard SetFeature command to our root
  878. + * hub port. Our dwc_otg_hcd_hub_control() routine will
  879. + * recognize the command and perform the desired test
  880. + * sequence.
  881. + */
  882. + if (dev->descriptor.idVendor == 0x1A0A) {
  883. + /* HSOTG Electrical Test */
  884. + dev_warn(&dev->dev, "VID from HSOTG Electrical Test Fixture\n");
  885. +
  886. + if (dev->bus && dev->bus->root_hub) {
  887. + struct usb_device *hdev = dev->bus->root_hub;
  888. + dev_warn(&dev->dev, "Got PID 0x%x\n", dev->descriptor.idProduct);
  889. +
  890. + switch (dev->descriptor.idProduct) {
  891. + case 0x0101: /* TEST_SE0_NAK */
  892. + dev_warn(&dev->dev, "TEST_SE0_NAK\n");
  893. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  894. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  895. + USB_PORT_FEAT_TEST, 0x300, NULL, 0, HZ);
  896. + break;
  897. +
  898. + case 0x0102: /* TEST_J */
  899. + dev_warn(&dev->dev, "TEST_J\n");
  900. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  901. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  902. + USB_PORT_FEAT_TEST, 0x100, NULL, 0, HZ);
  903. + break;
  904. +
  905. + case 0x0103: /* TEST_K */
  906. + dev_warn(&dev->dev, "TEST_K\n");
  907. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  908. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  909. + USB_PORT_FEAT_TEST, 0x200, NULL, 0, HZ);
  910. + break;
  911. +
  912. + case 0x0104: /* TEST_PACKET */
  913. + dev_warn(&dev->dev, "TEST_PACKET\n");
  914. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  915. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  916. + USB_PORT_FEAT_TEST, 0x400, NULL, 0, HZ);
  917. + break;
  918. +
  919. + case 0x0105: /* TEST_FORCE_ENABLE */
  920. + dev_warn(&dev->dev, "TEST_FORCE_ENABLE\n");
  921. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  922. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  923. + USB_PORT_FEAT_TEST, 0x500, NULL, 0, HZ);
  924. + break;
  925. +
  926. + case 0x0106: /* HS_HOST_PORT_SUSPEND_RESUME */
  927. + dev_warn(&dev->dev, "HS_HOST_PORT_SUSPEND_RESUME\n");
  928. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  929. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  930. + USB_PORT_FEAT_TEST, 0x600, NULL, 0, 40 * HZ);
  931. + break;
  932. +
  933. + case 0x0107: /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR setup */
  934. + dev_warn(&dev->dev, "SINGLE_STEP_GET_DEVICE_DESCRIPTOR setup\n");
  935. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  936. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  937. + USB_PORT_FEAT_TEST, 0x700, NULL, 0, 40 * HZ);
  938. + break;
  939. +
  940. + case 0x0108: /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR execute */
  941. + dev_warn(&dev->dev, "SINGLE_STEP_GET_DEVICE_DESCRIPTOR execute\n");
  942. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  943. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  944. + USB_PORT_FEAT_TEST, 0x800, NULL, 0, 40 * HZ);
  945. + }
  946. + }
  947. + }
  948. +#endif /* DWC_HS_ELECT_TST */
  949. /* Now that the interfaces are installed, re-enable LPM. */
  950. usb_unlocked_enable_lpm(dev);
  951. --- a/drivers/usb/core/otg_productlist.h
  952. +++ b/drivers/usb/core/otg_productlist.h
  953. @@ -11,33 +11,82 @@
  954. static struct usb_device_id productlist_table[] = {
  955. /* hubs are optional in OTG, but very handy ... */
  956. +#define CERT_WITHOUT_HUBS
  957. +#if defined(CERT_WITHOUT_HUBS)
  958. +{ USB_DEVICE( 0x0000, 0x0000 ), }, /* Root HUB Only*/
  959. +#else
  960. { USB_DEVICE_INFO(USB_CLASS_HUB, 0, 0), },
  961. { USB_DEVICE_INFO(USB_CLASS_HUB, 0, 1), },
  962. +{ USB_DEVICE_INFO(USB_CLASS_HUB, 0, 2), },
  963. +#endif
  964. #ifdef CONFIG_USB_PRINTER /* ignoring nonstatic linkage! */
  965. /* FIXME actually, printers are NOT supposed to use device classes;
  966. * they're supposed to use interface classes...
  967. */
  968. -{ USB_DEVICE_INFO(7, 1, 1) },
  969. -{ USB_DEVICE_INFO(7, 1, 2) },
  970. -{ USB_DEVICE_INFO(7, 1, 3) },
  971. +//{ USB_DEVICE_INFO(7, 1, 1) },
  972. +//{ USB_DEVICE_INFO(7, 1, 2) },
  973. +//{ USB_DEVICE_INFO(7, 1, 3) },
  974. #endif
  975. #ifdef CONFIG_USB_NET_CDCETHER
  976. /* Linux-USB CDC Ethernet gadget */
  977. -{ USB_DEVICE(0x0525, 0xa4a1), },
  978. +//{ USB_DEVICE(0x0525, 0xa4a1), },
  979. /* Linux-USB CDC Ethernet + RNDIS gadget */
  980. -{ USB_DEVICE(0x0525, 0xa4a2), },
  981. +//{ USB_DEVICE(0x0525, 0xa4a2), },
  982. #endif
  983. #if IS_ENABLED(CONFIG_USB_TEST)
  984. /* gadget zero, for testing */
  985. -{ USB_DEVICE(0x0525, 0xa4a0), },
  986. +//{ USB_DEVICE(0x0525, 0xa4a0), },
  987. #endif
  988. +/* OPT Tester */
  989. +{ USB_DEVICE( 0x1a0a, 0x0101 ), }, /* TEST_SE0_NAK */
  990. +{ USB_DEVICE( 0x1a0a, 0x0102 ), }, /* Test_J */
  991. +{ USB_DEVICE( 0x1a0a, 0x0103 ), }, /* Test_K */
  992. +{ USB_DEVICE( 0x1a0a, 0x0104 ), }, /* Test_PACKET */
  993. +{ USB_DEVICE( 0x1a0a, 0x0105 ), }, /* Test_FORCE_ENABLE */
  994. +{ USB_DEVICE( 0x1a0a, 0x0106 ), }, /* HS_PORT_SUSPEND_RESUME */
  995. +{ USB_DEVICE( 0x1a0a, 0x0107 ), }, /* SINGLE_STEP_GET_DESCRIPTOR setup */
  996. +{ USB_DEVICE( 0x1a0a, 0x0108 ), }, /* SINGLE_STEP_GET_DESCRIPTOR execute */
  997. +
  998. +/* Sony cameras */
  999. +{ USB_DEVICE_VER(0x054c,0x0010,0x0410, 0x0500), },
  1000. +
  1001. +/* Memory Devices */
  1002. +//{ USB_DEVICE( 0x0781, 0x5150 ), }, /* SanDisk */
  1003. +//{ USB_DEVICE( 0x05DC, 0x0080 ), }, /* Lexar */
  1004. +//{ USB_DEVICE( 0x4146, 0x9281 ), }, /* IOMEGA */
  1005. +//{ USB_DEVICE( 0x067b, 0x2507 ), }, /* Hammer 20GB External HD */
  1006. +{ USB_DEVICE( 0x0EA0, 0x2168 ), }, /* Ours Technology Inc. (BUFFALO ClipDrive)*/
  1007. +//{ USB_DEVICE( 0x0457, 0x0150 ), }, /* Silicon Integrated Systems Corp. */
  1008. +
  1009. +/* HP Printers */
  1010. +//{ USB_DEVICE( 0x03F0, 0x1102 ), }, /* HP Photosmart 245 */
  1011. +//{ USB_DEVICE( 0x03F0, 0x1302 ), }, /* HP Photosmart 370 Series */
  1012. +
  1013. +/* Speakers */
  1014. +//{ USB_DEVICE( 0x0499, 0x3002 ), }, /* YAMAHA YST-MS35D USB Speakers */
  1015. +//{ USB_DEVICE( 0x0672, 0x1041 ), }, /* Labtec USB Headset */
  1016. +
  1017. { } /* Terminating entry */
  1018. };
  1019. +static inline void report_errors(struct usb_device *dev)
  1020. +{
  1021. + /* OTG MESSAGE: report errors here, customize to match your product */
  1022. + dev_info(&dev->dev, "device Vendor:%04x Product:%04x is not supported\n",
  1023. + le16_to_cpu(dev->descriptor.idVendor),
  1024. + le16_to_cpu(dev->descriptor.idProduct));
  1025. + if (USB_CLASS_HUB == dev->descriptor.bDeviceClass){
  1026. + dev_printk(KERN_CRIT, &dev->dev, "Unsupported Hub Topology\n");
  1027. + } else {
  1028. + dev_printk(KERN_CRIT, &dev->dev, "Attached Device is not Supported\n");
  1029. + }
  1030. +}
  1031. +
  1032. +
  1033. static int is_targeted(struct usb_device *dev)
  1034. {
  1035. struct usb_device_id *id = productlist_table;
  1036. @@ -87,16 +136,57 @@ static int is_targeted(struct usb_device
  1037. continue;
  1038. return 1;
  1039. - }
  1040. + /* NOTE: can't use usb_match_id() since interface caches
  1041. + * aren't set up yet. this is cut/paste from that code.
  1042. + */
  1043. + for (id = productlist_table; id->match_flags; id++) {
  1044. +#ifdef DEBUG
  1045. + dev_dbg(&dev->dev,
  1046. + "ID: V:%04x P:%04x DC:%04x SC:%04x PR:%04x \n",
  1047. + id->idVendor,
  1048. + id->idProduct,
  1049. + id->bDeviceClass,
  1050. + id->bDeviceSubClass,
  1051. + id->bDeviceProtocol);
  1052. +#endif
  1053. - /* add other match criteria here ... */
  1054. + if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
  1055. + id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
  1056. + continue;
  1057. +
  1058. + if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
  1059. + id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
  1060. + continue;
  1061. +
  1062. + /* No need to test id->bcdDevice_lo != 0, since 0 is never
  1063. + greater than any unsigned number. */
  1064. + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
  1065. + (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
  1066. + continue;
  1067. +
  1068. + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
  1069. + (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
  1070. + continue;
  1071. +
  1072. + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
  1073. + (id->bDeviceClass != dev->descriptor.bDeviceClass))
  1074. + continue;
  1075. +
  1076. + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
  1077. + (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass))
  1078. + continue;
  1079. +
  1080. + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
  1081. + (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
  1082. + continue;
  1083. + return 1;
  1084. + }
  1085. + }
  1086. - /* OTG MESSAGE: report errors here, customize to match your product */
  1087. - dev_err(&dev->dev, "device v%04x p%04x is not supported\n",
  1088. - le16_to_cpu(dev->descriptor.idVendor),
  1089. - le16_to_cpu(dev->descriptor.idProduct));
  1090. + /* add other match criteria here ... */
  1091. + report_errors(dev);
  1092. return 0;
  1093. }
  1094. --- /dev/null
  1095. +++ b/drivers/usb/gadget/file_storage.c
  1096. @@ -0,0 +1,3676 @@
  1097. +/*
  1098. + * file_storage.c -- File-backed USB Storage Gadget, for USB development
  1099. + *
  1100. + * Copyright (C) 2003-2008 Alan Stern
  1101. + * All rights reserved.
  1102. + *
  1103. + * Redistribution and use in source and binary forms, with or without
  1104. + * modification, are permitted provided that the following conditions
  1105. + * are met:
  1106. + * 1. Redistributions of source code must retain the above copyright
  1107. + * notice, this list of conditions, and the following disclaimer,
  1108. + * without modification.
  1109. + * 2. Redistributions in binary form must reproduce the above copyright
  1110. + * notice, this list of conditions and the following disclaimer in the
  1111. + * documentation and/or other materials provided with the distribution.
  1112. + * 3. The names of the above-listed copyright holders may not be used
  1113. + * to endorse or promote products derived from this software without
  1114. + * specific prior written permission.
  1115. + *
  1116. + * ALTERNATIVELY, this software may be distributed under the terms of the
  1117. + * GNU General Public License ("GPL") as published by the Free Software
  1118. + * Foundation, either version 2 of that License or (at your option) any
  1119. + * later version.
  1120. + *
  1121. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  1122. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  1123. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  1124. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  1125. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  1126. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  1127. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  1128. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  1129. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  1130. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  1131. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  1132. + */
  1133. +
  1134. +
  1135. +/*
  1136. + * The File-backed Storage Gadget acts as a USB Mass Storage device,
  1137. + * appearing to the host as a disk drive or as a CD-ROM drive. In addition
  1138. + * to providing an example of a genuinely useful gadget driver for a USB
  1139. + * device, it also illustrates a technique of double-buffering for increased
  1140. + * throughput. Last but not least, it gives an easy way to probe the
  1141. + * behavior of the Mass Storage drivers in a USB host.
  1142. + *
  1143. + * Backing storage is provided by a regular file or a block device, specified
  1144. + * by the "file" module parameter. Access can be limited to read-only by
  1145. + * setting the optional "ro" module parameter. (For CD-ROM emulation,
  1146. + * access is always read-only.) The gadget will indicate that it has
  1147. + * removable media if the optional "removable" module parameter is set.
  1148. + *
  1149. + * The gadget supports the Control-Bulk (CB), Control-Bulk-Interrupt (CBI),
  1150. + * and Bulk-Only (also known as Bulk-Bulk-Bulk or BBB) transports, selected
  1151. + * by the optional "transport" module parameter. It also supports the
  1152. + * following protocols: RBC (0x01), ATAPI or SFF-8020i (0x02), QIC-157 (0c03),
  1153. + * UFI (0x04), SFF-8070i (0x05), and transparent SCSI (0x06), selected by
  1154. + * the optional "protocol" module parameter. In addition, the default
  1155. + * Vendor ID, Product ID, release number and serial number can be overridden.
  1156. + *
  1157. + * There is support for multiple logical units (LUNs), each of which has
  1158. + * its own backing file. The number of LUNs can be set using the optional
  1159. + * "luns" module parameter (anywhere from 1 to 8), and the corresponding
  1160. + * files are specified using comma-separated lists for "file" and "ro".
  1161. + * The default number of LUNs is taken from the number of "file" elements;
  1162. + * it is 1 if "file" is not given. If "removable" is not set then a backing
  1163. + * file must be specified for each LUN. If it is set, then an unspecified
  1164. + * or empty backing filename means the LUN's medium is not loaded. Ideally
  1165. + * each LUN would be settable independently as a disk drive or a CD-ROM
  1166. + * drive, but currently all LUNs have to be the same type. The CD-ROM
  1167. + * emulation includes a single data track and no audio tracks; hence there
  1168. + * need be only one backing file per LUN.
  1169. + *
  1170. + * Requirements are modest; only a bulk-in and a bulk-out endpoint are
  1171. + * needed (an interrupt-out endpoint is also needed for CBI). The memory
  1172. + * requirement amounts to two 16K buffers, size configurable by a parameter.
  1173. + * Support is included for both full-speed and high-speed operation.
  1174. + *
  1175. + * Note that the driver is slightly non-portable in that it assumes a
  1176. + * single memory/DMA buffer will be useable for bulk-in, bulk-out, and
  1177. + * interrupt-in endpoints. With most device controllers this isn't an
  1178. + * issue, but there may be some with hardware restrictions that prevent
  1179. + * a buffer from being used by more than one endpoint.
  1180. + *
  1181. + * Module options:
  1182. + *
  1183. + * file=filename[,filename...]
  1184. + * Required if "removable" is not set, names of
  1185. + * the files or block devices used for
  1186. + * backing storage
  1187. + * serial=HHHH... Required serial number (string of hex chars)
  1188. + * ro=b[,b...] Default false, booleans for read-only access
  1189. + * removable Default false, boolean for removable media
  1190. + * luns=N Default N = number of filenames, number of
  1191. + * LUNs to support
  1192. + * nofua=b[,b...] Default false, booleans for ignore FUA flag
  1193. + * in SCSI WRITE(10,12) commands
  1194. + * stall Default determined according to the type of
  1195. + * USB device controller (usually true),
  1196. + * boolean to permit the driver to halt
  1197. + * bulk endpoints
  1198. + * cdrom Default false, boolean for whether to emulate
  1199. + * a CD-ROM drive
  1200. + * transport=XXX Default BBB, transport name (CB, CBI, or BBB)
  1201. + * protocol=YYY Default SCSI, protocol name (RBC, 8020 or
  1202. + * ATAPI, QIC, UFI, 8070, or SCSI;
  1203. + * also 1 - 6)
  1204. + * vendor=0xVVVV Default 0x0525 (NetChip), USB Vendor ID
  1205. + * product=0xPPPP Default 0xa4a5 (FSG), USB Product ID
  1206. + * release=0xRRRR Override the USB release number (bcdDevice)
  1207. + * buflen=N Default N=16384, buffer size used (will be
  1208. + * rounded down to a multiple of
  1209. + * PAGE_CACHE_SIZE)
  1210. + *
  1211. + * If CONFIG_USB_FILE_STORAGE_TEST is not set, only the "file", "serial", "ro",
  1212. + * "removable", "luns", "nofua", "stall", and "cdrom" options are available;
  1213. + * default values are used for everything else.
  1214. + *
  1215. + * The pathnames of the backing files and the ro settings are available in
  1216. + * the attribute files "file", "nofua", and "ro" in the lun<n> subdirectory of
  1217. + * the gadget's sysfs directory. If the "removable" option is set, writing to
  1218. + * these files will simulate ejecting/loading the medium (writing an empty
  1219. + * line means eject) and adjusting a write-enable tab. Changes to the ro
  1220. + * setting are not allowed when the medium is loaded or if CD-ROM emulation
  1221. + * is being used.
  1222. + *
  1223. + * This gadget driver is heavily based on "Gadget Zero" by David Brownell.
  1224. + * The driver's SCSI command interface was based on the "Information
  1225. + * technology - Small Computer System Interface - 2" document from
  1226. + * X3T9.2 Project 375D, Revision 10L, 7-SEP-93, available at
  1227. + * <http://www.t10.org/ftp/t10/drafts/s2/s2-r10l.pdf>. The single exception
  1228. + * is opcode 0x23 (READ FORMAT CAPACITIES), which was based on the
  1229. + * "Universal Serial Bus Mass Storage Class UFI Command Specification"
  1230. + * document, Revision 1.0, December 14, 1998, available at
  1231. + * <http://www.usb.org/developers/devclass_docs/usbmass-ufi10.pdf>.
  1232. + */
  1233. +
  1234. +
  1235. +/*
  1236. + * Driver Design
  1237. + *
  1238. + * The FSG driver is fairly straightforward. There is a main kernel
  1239. + * thread that handles most of the work. Interrupt routines field
  1240. + * callbacks from the controller driver: bulk- and interrupt-request
  1241. + * completion notifications, endpoint-0 events, and disconnect events.
  1242. + * Completion events are passed to the main thread by wakeup calls. Many
  1243. + * ep0 requests are handled at interrupt time, but SetInterface,
  1244. + * SetConfiguration, and device reset requests are forwarded to the
  1245. + * thread in the form of "exceptions" using SIGUSR1 signals (since they
  1246. + * should interrupt any ongoing file I/O operations).
  1247. + *
  1248. + * The thread's main routine implements the standard command/data/status
  1249. + * parts of a SCSI interaction. It and its subroutines are full of tests
  1250. + * for pending signals/exceptions -- all this polling is necessary since
  1251. + * the kernel has no setjmp/longjmp equivalents. (Maybe this is an
  1252. + * indication that the driver really wants to be running in userspace.)
  1253. + * An important point is that so long as the thread is alive it keeps an
  1254. + * open reference to the backing file. This will prevent unmounting
  1255. + * the backing file's underlying filesystem and could cause problems
  1256. + * during system shutdown, for example. To prevent such problems, the
  1257. + * thread catches INT, TERM, and KILL signals and converts them into
  1258. + * an EXIT exception.
  1259. + *
  1260. + * In normal operation the main thread is started during the gadget's
  1261. + * fsg_bind() callback and stopped during fsg_unbind(). But it can also
  1262. + * exit when it receives a signal, and there's no point leaving the
  1263. + * gadget running when the thread is dead. So just before the thread
  1264. + * exits, it deregisters the gadget driver. This makes things a little
  1265. + * tricky: The driver is deregistered at two places, and the exiting
  1266. + * thread can indirectly call fsg_unbind() which in turn can tell the
  1267. + * thread to exit. The first problem is resolved through the use of the
  1268. + * REGISTERED atomic bitflag; the driver will only be deregistered once.
  1269. + * The second problem is resolved by having fsg_unbind() check
  1270. + * fsg->state; it won't try to stop the thread if the state is already
  1271. + * FSG_STATE_TERMINATED.
  1272. + *
  1273. + * To provide maximum throughput, the driver uses a circular pipeline of
  1274. + * buffer heads (struct fsg_buffhd). In principle the pipeline can be
  1275. + * arbitrarily long; in practice the benefits don't justify having more
  1276. + * than 2 stages (i.e., double buffering). But it helps to think of the
  1277. + * pipeline as being a long one. Each buffer head contains a bulk-in and
  1278. + * a bulk-out request pointer (since the buffer can be used for both
  1279. + * output and input -- directions always are given from the host's
  1280. + * point of view) as well as a pointer to the buffer and various state
  1281. + * variables.
  1282. + *
  1283. + * Use of the pipeline follows a simple protocol. There is a variable
  1284. + * (fsg->next_buffhd_to_fill) that points to the next buffer head to use.
  1285. + * At any time that buffer head may still be in use from an earlier
  1286. + * request, so each buffer head has a state variable indicating whether
  1287. + * it is EMPTY, FULL, or BUSY. Typical use involves waiting for the
  1288. + * buffer head to be EMPTY, filling the buffer either by file I/O or by
  1289. + * USB I/O (during which the buffer head is BUSY), and marking the buffer
  1290. + * head FULL when the I/O is complete. Then the buffer will be emptied
  1291. + * (again possibly by USB I/O, during which it is marked BUSY) and
  1292. + * finally marked EMPTY again (possibly by a completion routine).
  1293. + *
  1294. + * A module parameter tells the driver to avoid stalling the bulk
  1295. + * endpoints wherever the transport specification allows. This is
  1296. + * necessary for some UDCs like the SuperH, which cannot reliably clear a
  1297. + * halt on a bulk endpoint. However, under certain circumstances the
  1298. + * Bulk-only specification requires a stall. In such cases the driver
  1299. + * will halt the endpoint and set a flag indicating that it should clear
  1300. + * the halt in software during the next device reset. Hopefully this
  1301. + * will permit everything to work correctly. Furthermore, although the
  1302. + * specification allows the bulk-out endpoint to halt when the host sends
  1303. + * too much data, implementing this would cause an unavoidable race.
  1304. + * The driver will always use the "no-stall" approach for OUT transfers.
  1305. + *
  1306. + * One subtle point concerns sending status-stage responses for ep0
  1307. + * requests. Some of these requests, such as device reset, can involve
  1308. + * interrupting an ongoing file I/O operation, which might take an
  1309. + * arbitrarily long time. During that delay the host might give up on
  1310. + * the original ep0 request and issue a new one. When that happens the
  1311. + * driver should not notify the host about completion of the original
  1312. + * request, as the host will no longer be waiting for it. So the driver
  1313. + * assigns to each ep0 request a unique tag, and it keeps track of the
  1314. + * tag value of the request associated with a long-running exception
  1315. + * (device-reset, interface-change, or configuration-change). When the
  1316. + * exception handler is finished, the status-stage response is submitted
  1317. + * only if the current ep0 request tag is equal to the exception request
  1318. + * tag. Thus only the most recently received ep0 request will get a
  1319. + * status-stage response.
  1320. + *
  1321. + * Warning: This driver source file is too long. It ought to be split up
  1322. + * into a header file plus about 3 separate .c files, to handle the details
  1323. + * of the Gadget, USB Mass Storage, and SCSI protocols.
  1324. + */
  1325. +
  1326. +
  1327. +/* #define VERBOSE_DEBUG */
  1328. +/* #define DUMP_MSGS */
  1329. +
  1330. +
  1331. +#include <linux/blkdev.h>
  1332. +#include <linux/completion.h>
  1333. +#include <linux/dcache.h>
  1334. +#include <linux/delay.h>
  1335. +#include <linux/device.h>
  1336. +#include <linux/fcntl.h>
  1337. +#include <linux/file.h>
  1338. +#include <linux/fs.h>
  1339. +#include <linux/kref.h>
  1340. +#include <linux/kthread.h>
  1341. +#include <linux/limits.h>
  1342. +#include <linux/module.h>
  1343. +#include <linux/rwsem.h>
  1344. +#include <linux/slab.h>
  1345. +#include <linux/spinlock.h>
  1346. +#include <linux/string.h>
  1347. +#include <linux/freezer.h>
  1348. +#include <linux/utsname.h>
  1349. +
  1350. +#include <linux/usb/ch9.h>
  1351. +#include <linux/usb/gadget.h>
  1352. +
  1353. +#include "gadget_chips.h"
  1354. +
  1355. +
  1356. +
  1357. +/*
  1358. + * Kbuild is not very cooperative with respect to linking separately
  1359. + * compiled library objects into one module. So for now we won't use
  1360. + * separate compilation ... ensuring init/exit sections work to shrink
  1361. + * the runtime footprint, and giving us at least some parts of what
  1362. + * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
  1363. + */
  1364. +#include "usbstring.c"
  1365. +#include "config.c"
  1366. +#include "epautoconf.c"
  1367. +
  1368. +/*-------------------------------------------------------------------------*/
  1369. +
  1370. +#define DRIVER_DESC "File-backed Storage Gadget"
  1371. +#define DRIVER_NAME "g_file_storage"
  1372. +#define DRIVER_VERSION "1 September 2010"
  1373. +
  1374. +static char fsg_string_manufacturer[64];
  1375. +static const char fsg_string_product[] = DRIVER_DESC;
  1376. +static const char fsg_string_config[] = "Self-powered";
  1377. +static const char fsg_string_interface[] = "Mass Storage";
  1378. +
  1379. +
  1380. +#include "storage_common.c"
  1381. +
  1382. +
  1383. +MODULE_DESCRIPTION(DRIVER_DESC);
  1384. +MODULE_AUTHOR("Alan Stern");
  1385. +MODULE_LICENSE("Dual BSD/GPL");
  1386. +
  1387. +/*
  1388. + * This driver assumes self-powered hardware and has no way for users to
  1389. + * trigger remote wakeup. It uses autoconfiguration to select endpoints
  1390. + * and endpoint addresses.
  1391. + */
  1392. +
  1393. +
  1394. +/*-------------------------------------------------------------------------*/
  1395. +
  1396. +
  1397. +/* Encapsulate the module parameter settings */
  1398. +
  1399. +static struct {
  1400. + char *file[FSG_MAX_LUNS];
  1401. + char *serial;
  1402. + bool ro[FSG_MAX_LUNS];
  1403. + bool nofua[FSG_MAX_LUNS];
  1404. + unsigned int num_filenames;
  1405. + unsigned int num_ros;
  1406. + unsigned int num_nofuas;
  1407. + unsigned int nluns;
  1408. +
  1409. + bool removable;
  1410. + bool can_stall;
  1411. + bool cdrom;
  1412. +
  1413. + char *transport_parm;
  1414. + char *protocol_parm;
  1415. + unsigned short vendor;
  1416. + unsigned short product;
  1417. + unsigned short release;
  1418. + unsigned int buflen;
  1419. +
  1420. + int transport_type;
  1421. + char *transport_name;
  1422. + int protocol_type;
  1423. + char *protocol_name;
  1424. +
  1425. +} mod_data = { // Default values
  1426. + .transport_parm = "BBB",
  1427. + .protocol_parm = "SCSI",
  1428. + .removable = 0,
  1429. + .can_stall = 1,
  1430. + .cdrom = 0,
  1431. + .vendor = FSG_VENDOR_ID,
  1432. + .product = FSG_PRODUCT_ID,
  1433. + .release = 0xffff, // Use controller chip type
  1434. + .buflen = 16384,
  1435. + };
  1436. +
  1437. +
  1438. +module_param_array_named(file, mod_data.file, charp, &mod_data.num_filenames,
  1439. + S_IRUGO);
  1440. +MODULE_PARM_DESC(file, "names of backing files or devices");
  1441. +
  1442. +module_param_named(serial, mod_data.serial, charp, S_IRUGO);
  1443. +MODULE_PARM_DESC(serial, "USB serial number");
  1444. +
  1445. +module_param_array_named(ro, mod_data.ro, bool, &mod_data.num_ros, S_IRUGO);
  1446. +MODULE_PARM_DESC(ro, "true to force read-only");
  1447. +
  1448. +module_param_array_named(nofua, mod_data.nofua, bool, &mod_data.num_nofuas,
  1449. + S_IRUGO);
  1450. +MODULE_PARM_DESC(nofua, "true to ignore SCSI WRITE(10,12) FUA bit");
  1451. +
  1452. +module_param_named(luns, mod_data.nluns, uint, S_IRUGO);
  1453. +MODULE_PARM_DESC(luns, "number of LUNs");
  1454. +
  1455. +module_param_named(removable, mod_data.removable, bool, S_IRUGO);
  1456. +MODULE_PARM_DESC(removable, "true to simulate removable media");
  1457. +
  1458. +module_param_named(stall, mod_data.can_stall, bool, S_IRUGO);
  1459. +MODULE_PARM_DESC(stall, "false to prevent bulk stalls");
  1460. +
  1461. +module_param_named(cdrom, mod_data.cdrom, bool, S_IRUGO);
  1462. +MODULE_PARM_DESC(cdrom, "true to emulate cdrom instead of disk");
  1463. +
  1464. +/* In the non-TEST version, only the module parameters listed above
  1465. + * are available. */
  1466. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  1467. +
  1468. +module_param_named(transport, mod_data.transport_parm, charp, S_IRUGO);
  1469. +MODULE_PARM_DESC(transport, "type of transport (BBB, CBI, or CB)");
  1470. +
  1471. +module_param_named(protocol, mod_data.protocol_parm, charp, S_IRUGO);
  1472. +MODULE_PARM_DESC(protocol, "type of protocol (RBC, 8020, QIC, UFI, "
  1473. + "8070, or SCSI)");
  1474. +
  1475. +module_param_named(vendor, mod_data.vendor, ushort, S_IRUGO);
  1476. +MODULE_PARM_DESC(vendor, "USB Vendor ID");
  1477. +
  1478. +module_param_named(product, mod_data.product, ushort, S_IRUGO);
  1479. +MODULE_PARM_DESC(product, "USB Product ID");
  1480. +
  1481. +module_param_named(release, mod_data.release, ushort, S_IRUGO);
  1482. +MODULE_PARM_DESC(release, "USB release number");
  1483. +
  1484. +module_param_named(buflen, mod_data.buflen, uint, S_IRUGO);
  1485. +MODULE_PARM_DESC(buflen, "I/O buffer size");
  1486. +
  1487. +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
  1488. +
  1489. +
  1490. +/*
  1491. + * These definitions will permit the compiler to avoid generating code for
  1492. + * parts of the driver that aren't used in the non-TEST version. Even gcc
  1493. + * can recognize when a test of a constant expression yields a dead code
  1494. + * path.
  1495. + */
  1496. +
  1497. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  1498. +
  1499. +#define transport_is_bbb() (mod_data.transport_type == USB_PR_BULK)
  1500. +#define transport_is_cbi() (mod_data.transport_type == USB_PR_CBI)
  1501. +#define protocol_is_scsi() (mod_data.protocol_type == USB_SC_SCSI)
  1502. +
  1503. +#else
  1504. +
  1505. +#define transport_is_bbb() 1
  1506. +#define transport_is_cbi() 0
  1507. +#define protocol_is_scsi() 1
  1508. +
  1509. +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
  1510. +
  1511. +
  1512. +/*-------------------------------------------------------------------------*/
  1513. +
  1514. +
  1515. +struct fsg_dev {
  1516. + /* lock protects: state, all the req_busy's, and cbbuf_cmnd */
  1517. + spinlock_t lock;
  1518. + struct usb_gadget *gadget;
  1519. +
  1520. + /* filesem protects: backing files in use */
  1521. + struct rw_semaphore filesem;
  1522. +
  1523. + /* reference counting: wait until all LUNs are released */
  1524. + struct kref ref;
  1525. +
  1526. + struct usb_ep *ep0; // Handy copy of gadget->ep0
  1527. + struct usb_request *ep0req; // For control responses
  1528. + unsigned int ep0_req_tag;
  1529. + const char *ep0req_name;
  1530. +
  1531. + struct usb_request *intreq; // For interrupt responses
  1532. + int intreq_busy;
  1533. + struct fsg_buffhd *intr_buffhd;
  1534. +
  1535. + unsigned int bulk_out_maxpacket;
  1536. + enum fsg_state state; // For exception handling
  1537. + unsigned int exception_req_tag;
  1538. +
  1539. + u8 config, new_config;
  1540. +
  1541. + unsigned int running : 1;
  1542. + unsigned int bulk_in_enabled : 1;
  1543. + unsigned int bulk_out_enabled : 1;
  1544. + unsigned int intr_in_enabled : 1;
  1545. + unsigned int phase_error : 1;
  1546. + unsigned int short_packet_received : 1;
  1547. + unsigned int bad_lun_okay : 1;
  1548. +
  1549. + unsigned long atomic_bitflags;
  1550. +#define REGISTERED 0
  1551. +#define IGNORE_BULK_OUT 1
  1552. +#define SUSPENDED 2
  1553. +
  1554. + struct usb_ep *bulk_in;
  1555. + struct usb_ep *bulk_out;
  1556. + struct usb_ep *intr_in;
  1557. +
  1558. + struct fsg_buffhd *next_buffhd_to_fill;
  1559. + struct fsg_buffhd *next_buffhd_to_drain;
  1560. +
  1561. + int thread_wakeup_needed;
  1562. + struct completion thread_notifier;
  1563. + struct task_struct *thread_task;
  1564. +
  1565. + int cmnd_size;
  1566. + u8 cmnd[MAX_COMMAND_SIZE];
  1567. + enum data_direction data_dir;
  1568. + u32 data_size;
  1569. + u32 data_size_from_cmnd;
  1570. + u32 tag;
  1571. + unsigned int lun;
  1572. + u32 residue;
  1573. + u32 usb_amount_left;
  1574. +
  1575. + /* The CB protocol offers no way for a host to know when a command
  1576. + * has completed. As a result the next command may arrive early,
  1577. + * and we will still have to handle it. For that reason we need
  1578. + * a buffer to store new commands when using CB (or CBI, which
  1579. + * does not oblige a host to wait for command completion either). */
  1580. + int cbbuf_cmnd_size;
  1581. + u8 cbbuf_cmnd[MAX_COMMAND_SIZE];
  1582. +
  1583. + unsigned int nluns;
  1584. + struct fsg_lun *luns;
  1585. + struct fsg_lun *curlun;
  1586. + /* Must be the last entry */
  1587. + struct fsg_buffhd buffhds[];
  1588. +};
  1589. +
  1590. +typedef void (*fsg_routine_t)(struct fsg_dev *);
  1591. +
  1592. +static int exception_in_progress(struct fsg_dev *fsg)
  1593. +{
  1594. + return (fsg->state > FSG_STATE_IDLE);
  1595. +}
  1596. +
  1597. +/* Make bulk-out requests be divisible by the maxpacket size */
  1598. +static void set_bulk_out_req_length(struct fsg_dev *fsg,
  1599. + struct fsg_buffhd *bh, unsigned int length)
  1600. +{
  1601. + unsigned int rem;
  1602. +
  1603. + bh->bulk_out_intended_length = length;
  1604. + rem = length % fsg->bulk_out_maxpacket;
  1605. + if (rem > 0)
  1606. + length += fsg->bulk_out_maxpacket - rem;
  1607. + bh->outreq->length = length;
  1608. +}
  1609. +
  1610. +static struct fsg_dev *the_fsg;
  1611. +static struct usb_gadget_driver fsg_driver;
  1612. +
  1613. +
  1614. +/*-------------------------------------------------------------------------*/
  1615. +
  1616. +static int fsg_set_halt(struct fsg_dev *fsg, struct usb_ep *ep)
  1617. +{
  1618. + const char *name;
  1619. +
  1620. + if (ep == fsg->bulk_in)
  1621. + name = "bulk-in";
  1622. + else if (ep == fsg->bulk_out)
  1623. + name = "bulk-out";
  1624. + else
  1625. + name = ep->name;
  1626. + DBG(fsg, "%s set halt\n", name);
  1627. + return usb_ep_set_halt(ep);
  1628. +}
  1629. +
  1630. +
  1631. +/*-------------------------------------------------------------------------*/
  1632. +
  1633. +/*
  1634. + * DESCRIPTORS ... most are static, but strings and (full) configuration
  1635. + * descriptors are built on demand. Also the (static) config and interface
  1636. + * descriptors are adjusted during fsg_bind().
  1637. + */
  1638. +
  1639. +/* There is only one configuration. */
  1640. +#define CONFIG_VALUE 1
  1641. +
  1642. +static struct usb_device_descriptor
  1643. +device_desc = {
  1644. + .bLength = sizeof device_desc,
  1645. + .bDescriptorType = USB_DT_DEVICE,
  1646. +
  1647. + .bcdUSB = cpu_to_le16(0x0200),
  1648. + .bDeviceClass = USB_CLASS_PER_INTERFACE,
  1649. +
  1650. + /* The next three values can be overridden by module parameters */
  1651. + .idVendor = cpu_to_le16(FSG_VENDOR_ID),
  1652. + .idProduct = cpu_to_le16(FSG_PRODUCT_ID),
  1653. + .bcdDevice = cpu_to_le16(0xffff),
  1654. +
  1655. + .iManufacturer = FSG_STRING_MANUFACTURER,
  1656. + .iProduct = FSG_STRING_PRODUCT,
  1657. + .iSerialNumber = FSG_STRING_SERIAL,
  1658. + .bNumConfigurations = 1,
  1659. +};
  1660. +
  1661. +static struct usb_config_descriptor
  1662. +config_desc = {
  1663. + .bLength = sizeof config_desc,
  1664. + .bDescriptorType = USB_DT_CONFIG,
  1665. +
  1666. + /* wTotalLength computed by usb_gadget_config_buf() */
  1667. + .bNumInterfaces = 1,
  1668. + .bConfigurationValue = CONFIG_VALUE,
  1669. + .iConfiguration = FSG_STRING_CONFIG,
  1670. + .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
  1671. + .bMaxPower = CONFIG_USB_GADGET_VBUS_DRAW / 2,
  1672. +};
  1673. +
  1674. +
  1675. +static struct usb_qualifier_descriptor
  1676. +dev_qualifier = {
  1677. + .bLength = sizeof dev_qualifier,
  1678. + .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
  1679. +
  1680. + .bcdUSB = cpu_to_le16(0x0200),
  1681. + .bDeviceClass = USB_CLASS_PER_INTERFACE,
  1682. +
  1683. + .bNumConfigurations = 1,
  1684. +};
  1685. +
  1686. +static int populate_bos(struct fsg_dev *fsg, u8 *buf)
  1687. +{
  1688. + memcpy(buf, &fsg_bos_desc, USB_DT_BOS_SIZE);
  1689. + buf += USB_DT_BOS_SIZE;
  1690. +
  1691. + memcpy(buf, &fsg_ext_cap_desc, USB_DT_USB_EXT_CAP_SIZE);
  1692. + buf += USB_DT_USB_EXT_CAP_SIZE;
  1693. +
  1694. + memcpy(buf, &fsg_ss_cap_desc, USB_DT_USB_SS_CAP_SIZE);
  1695. +
  1696. + return USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE
  1697. + + USB_DT_USB_EXT_CAP_SIZE;
  1698. +}
  1699. +
  1700. +/*
  1701. + * Config descriptors must agree with the code that sets configurations
  1702. + * and with code managing interfaces and their altsettings. They must
  1703. + * also handle different speeds and other-speed requests.
  1704. + */
  1705. +static int populate_config_buf(struct usb_gadget *gadget,
  1706. + u8 *buf, u8 type, unsigned index)
  1707. +{
  1708. + enum usb_device_speed speed = gadget->speed;
  1709. + int len;
  1710. + const struct usb_descriptor_header **function;
  1711. +
  1712. + if (index > 0)
  1713. + return -EINVAL;
  1714. +
  1715. + if (gadget_is_dualspeed(gadget) && type == USB_DT_OTHER_SPEED_CONFIG)
  1716. + speed = (USB_SPEED_FULL + USB_SPEED_HIGH) - speed;
  1717. + function = gadget_is_dualspeed(gadget) && speed == USB_SPEED_HIGH
  1718. + ? (const struct usb_descriptor_header **)fsg_hs_function
  1719. + : (const struct usb_descriptor_header **)fsg_fs_function;
  1720. +
  1721. + /* for now, don't advertise srp-only devices */
  1722. + if (!gadget_is_otg(gadget))
  1723. + function++;
  1724. +
  1725. + len = usb_gadget_config_buf(&config_desc, buf, EP0_BUFSIZE, function);
  1726. + ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
  1727. + return len;
  1728. +}
  1729. +
  1730. +
  1731. +/*-------------------------------------------------------------------------*/
  1732. +
  1733. +/* These routines may be called in process context or in_irq */
  1734. +
  1735. +/* Caller must hold fsg->lock */
  1736. +static void wakeup_thread(struct fsg_dev *fsg)
  1737. +{
  1738. + /* Tell the main thread that something has happened */
  1739. + fsg->thread_wakeup_needed = 1;
  1740. + if (fsg->thread_task)
  1741. + wake_up_process(fsg->thread_task);
  1742. +}
  1743. +
  1744. +
  1745. +static void raise_exception(struct fsg_dev *fsg, enum fsg_state new_state)
  1746. +{
  1747. + unsigned long flags;
  1748. +
  1749. + /* Do nothing if a higher-priority exception is already in progress.
  1750. + * If a lower-or-equal priority exception is in progress, preempt it
  1751. + * and notify the main thread by sending it a signal. */
  1752. + spin_lock_irqsave(&fsg->lock, flags);
  1753. + if (fsg->state <= new_state) {
  1754. + fsg->exception_req_tag = fsg->ep0_req_tag;
  1755. + fsg->state = new_state;
  1756. + if (fsg->thread_task)
  1757. + send_sig_info(SIGUSR1, SEND_SIG_FORCED,
  1758. + fsg->thread_task);
  1759. + }
  1760. + spin_unlock_irqrestore(&fsg->lock, flags);
  1761. +}
  1762. +
  1763. +
  1764. +/*-------------------------------------------------------------------------*/
  1765. +
  1766. +/* The disconnect callback and ep0 routines. These always run in_irq,
  1767. + * except that ep0_queue() is called in the main thread to acknowledge
  1768. + * completion of various requests: set config, set interface, and
  1769. + * Bulk-only device reset. */
  1770. +
  1771. +static void fsg_disconnect(struct usb_gadget *gadget)
  1772. +{
  1773. + struct fsg_dev *fsg = get_gadget_data(gadget);
  1774. +
  1775. + DBG(fsg, "disconnect or port reset\n");
  1776. + raise_exception(fsg, FSG_STATE_DISCONNECT);
  1777. +}
  1778. +
  1779. +
  1780. +static int ep0_queue(struct fsg_dev *fsg)
  1781. +{
  1782. + int rc;
  1783. +
  1784. + rc = usb_ep_queue(fsg->ep0, fsg->ep0req, GFP_ATOMIC);
  1785. + if (rc != 0 && rc != -ESHUTDOWN) {
  1786. +
  1787. + /* We can't do much more than wait for a reset */
  1788. + WARNING(fsg, "error in submission: %s --> %d\n",
  1789. + fsg->ep0->name, rc);
  1790. + }
  1791. + return rc;
  1792. +}
  1793. +
  1794. +static void ep0_complete(struct usb_ep *ep, struct usb_request *req)
  1795. +{
  1796. + struct fsg_dev *fsg = ep->driver_data;
  1797. +
  1798. + if (req->actual > 0)
  1799. + dump_msg(fsg, fsg->ep0req_name, req->buf, req->actual);
  1800. + if (req->status || req->actual != req->length)
  1801. + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
  1802. + req->status, req->actual, req->length);
  1803. + if (req->status == -ECONNRESET) // Request was cancelled
  1804. + usb_ep_fifo_flush(ep);
  1805. +
  1806. + if (req->status == 0 && req->context)
  1807. + ((fsg_routine_t) (req->context))(fsg);
  1808. +}
  1809. +
  1810. +
  1811. +/*-------------------------------------------------------------------------*/
  1812. +
  1813. +/* Bulk and interrupt endpoint completion handlers.
  1814. + * These always run in_irq. */
  1815. +
  1816. +static void bulk_in_complete(struct usb_ep *ep, struct usb_request *req)
  1817. +{
  1818. + struct fsg_dev *fsg = ep->driver_data;
  1819. + struct fsg_buffhd *bh = req->context;
  1820. +
  1821. + if (req->status || req->actual != req->length)
  1822. + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
  1823. + req->status, req->actual, req->length);
  1824. + if (req->status == -ECONNRESET) // Request was cancelled
  1825. + usb_ep_fifo_flush(ep);
  1826. +
  1827. + /* Hold the lock while we update the request and buffer states */
  1828. + smp_wmb();
  1829. + spin_lock(&fsg->lock);
  1830. + bh->inreq_busy = 0;
  1831. + bh->state = BUF_STATE_EMPTY;
  1832. + wakeup_thread(fsg);
  1833. + spin_unlock(&fsg->lock);
  1834. +}
  1835. +
  1836. +static void bulk_out_complete(struct usb_ep *ep, struct usb_request *req)
  1837. +{
  1838. + struct fsg_dev *fsg = ep->driver_data;
  1839. + struct fsg_buffhd *bh = req->context;
  1840. +
  1841. + dump_msg(fsg, "bulk-out", req->buf, req->actual);
  1842. + if (req->status || req->actual != bh->bulk_out_intended_length)
  1843. + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
  1844. + req->status, req->actual,
  1845. + bh->bulk_out_intended_length);
  1846. + if (req->status == -ECONNRESET) // Request was cancelled
  1847. + usb_ep_fifo_flush(ep);
  1848. +
  1849. + /* Hold the lock while we update the request and buffer states */
  1850. + smp_wmb();
  1851. + spin_lock(&fsg->lock);
  1852. + bh->outreq_busy = 0;
  1853. + bh->state = BUF_STATE_FULL;
  1854. + wakeup_thread(fsg);
  1855. + spin_unlock(&fsg->lock);
  1856. +}
  1857. +
  1858. +
  1859. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  1860. +static void intr_in_complete(struct usb_ep *ep, struct usb_request *req)
  1861. +{
  1862. + struct fsg_dev *fsg = ep->driver_data;
  1863. + struct fsg_buffhd *bh = req->context;
  1864. +
  1865. + if (req->status || req->actual != req->length)
  1866. + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
  1867. + req->status, req->actual, req->length);
  1868. + if (req->status == -ECONNRESET) // Request was cancelled
  1869. + usb_ep_fifo_flush(ep);
  1870. +
  1871. + /* Hold the lock while we update the request and buffer states */
  1872. + smp_wmb();
  1873. + spin_lock(&fsg->lock);
  1874. + fsg->intreq_busy = 0;
  1875. + bh->state = BUF_STATE_EMPTY;
  1876. + wakeup_thread(fsg);
  1877. + spin_unlock(&fsg->lock);
  1878. +}
  1879. +
  1880. +#else
  1881. +static void intr_in_complete(struct usb_ep *ep, struct usb_request *req)
  1882. +{}
  1883. +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
  1884. +
  1885. +
  1886. +/*-------------------------------------------------------------------------*/
  1887. +
  1888. +/* Ep0 class-specific handlers. These always run in_irq. */
  1889. +
  1890. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  1891. +static void received_cbi_adsc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  1892. +{
  1893. + struct usb_request *req = fsg->ep0req;
  1894. + static u8 cbi_reset_cmnd[6] = {
  1895. + SEND_DIAGNOSTIC, 4, 0xff, 0xff, 0xff, 0xff};
  1896. +
  1897. + /* Error in command transfer? */
  1898. + if (req->status || req->length != req->actual ||
  1899. + req->actual < 6 || req->actual > MAX_COMMAND_SIZE) {
  1900. +
  1901. + /* Not all controllers allow a protocol stall after
  1902. + * receiving control-out data, but we'll try anyway. */
  1903. + fsg_set_halt(fsg, fsg->ep0);
  1904. + return; // Wait for reset
  1905. + }
  1906. +
  1907. + /* Is it the special reset command? */
  1908. + if (req->actual >= sizeof cbi_reset_cmnd &&
  1909. + memcmp(req->buf, cbi_reset_cmnd,
  1910. + sizeof cbi_reset_cmnd) == 0) {
  1911. +
  1912. + /* Raise an exception to stop the current operation
  1913. + * and reinitialize our state. */
  1914. + DBG(fsg, "cbi reset request\n");
  1915. + raise_exception(fsg, FSG_STATE_RESET);
  1916. + return;
  1917. + }
  1918. +
  1919. + VDBG(fsg, "CB[I] accept device-specific command\n");
  1920. + spin_lock(&fsg->lock);
  1921. +
  1922. + /* Save the command for later */
  1923. + if (fsg->cbbuf_cmnd_size)
  1924. + WARNING(fsg, "CB[I] overwriting previous command\n");
  1925. + fsg->cbbuf_cmnd_size = req->actual;
  1926. + memcpy(fsg->cbbuf_cmnd, req->buf, fsg->cbbuf_cmnd_size);
  1927. +
  1928. + wakeup_thread(fsg);
  1929. + spin_unlock(&fsg->lock);
  1930. +}
  1931. +
  1932. +#else
  1933. +static void received_cbi_adsc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  1934. +{}
  1935. +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
  1936. +
  1937. +
  1938. +static int class_setup_req(struct fsg_dev *fsg,
  1939. + const struct usb_ctrlrequest *ctrl)
  1940. +{
  1941. + struct usb_request *req = fsg->ep0req;
  1942. + int value = -EOPNOTSUPP;
  1943. + u16 w_index = le16_to_cpu(ctrl->wIndex);
  1944. + u16 w_value = le16_to_cpu(ctrl->wValue);
  1945. + u16 w_length = le16_to_cpu(ctrl->wLength);
  1946. +
  1947. + if (!fsg->config)
  1948. + return value;
  1949. +
  1950. + /* Handle Bulk-only class-specific requests */
  1951. + if (transport_is_bbb()) {
  1952. + switch (ctrl->bRequest) {
  1953. +
  1954. + case US_BULK_RESET_REQUEST:
  1955. + if (ctrl->bRequestType != (USB_DIR_OUT |
  1956. + USB_TYPE_CLASS | USB_RECIP_INTERFACE))
  1957. + break;
  1958. + if (w_index != 0 || w_value != 0 || w_length != 0) {
  1959. + value = -EDOM;
  1960. + break;
  1961. + }
  1962. +
  1963. + /* Raise an exception to stop the current operation
  1964. + * and reinitialize our state. */
  1965. + DBG(fsg, "bulk reset request\n");
  1966. + raise_exception(fsg, FSG_STATE_RESET);
  1967. + value = DELAYED_STATUS;
  1968. + break;
  1969. +
  1970. + case US_BULK_GET_MAX_LUN:
  1971. + if (ctrl->bRequestType != (USB_DIR_IN |
  1972. + USB_TYPE_CLASS | USB_RECIP_INTERFACE))
  1973. + break;
  1974. + if (w_index != 0 || w_value != 0 || w_length != 1) {
  1975. + value = -EDOM;
  1976. + break;
  1977. + }
  1978. + VDBG(fsg, "get max LUN\n");
  1979. + *(u8 *) req->buf = fsg->nluns - 1;
  1980. + value = 1;
  1981. + break;
  1982. + }
  1983. + }
  1984. +
  1985. + /* Handle CBI class-specific requests */
  1986. + else {
  1987. + switch (ctrl->bRequest) {
  1988. +
  1989. + case USB_CBI_ADSC_REQUEST:
  1990. + if (ctrl->bRequestType != (USB_DIR_OUT |
  1991. + USB_TYPE_CLASS | USB_RECIP_INTERFACE))
  1992. + break;
  1993. + if (w_index != 0 || w_value != 0) {
  1994. + value = -EDOM;
  1995. + break;
  1996. + }
  1997. + if (w_length > MAX_COMMAND_SIZE) {
  1998. + value = -EOVERFLOW;
  1999. + break;
  2000. + }
  2001. + value = w_length;
  2002. + fsg->ep0req->context = received_cbi_adsc;
  2003. + break;
  2004. + }
  2005. + }
  2006. +
  2007. + if (value == -EOPNOTSUPP)
  2008. + VDBG(fsg,
  2009. + "unknown class-specific control req "
  2010. + "%02x.%02x v%04x i%04x l%u\n",
  2011. + ctrl->bRequestType, ctrl->bRequest,
  2012. + le16_to_cpu(ctrl->wValue), w_index, w_length);
  2013. + return value;
  2014. +}
  2015. +
  2016. +
  2017. +/*-------------------------------------------------------------------------*/
  2018. +
  2019. +/* Ep0 standard request handlers. These always run in_irq. */
  2020. +
  2021. +static int standard_setup_req(struct fsg_dev *fsg,
  2022. + const struct usb_ctrlrequest *ctrl)
  2023. +{
  2024. + struct usb_request *req = fsg->ep0req;
  2025. + int value = -EOPNOTSUPP;
  2026. + u16 w_index = le16_to_cpu(ctrl->wIndex);
  2027. + u16 w_value = le16_to_cpu(ctrl->wValue);
  2028. +
  2029. + /* Usually this just stores reply data in the pre-allocated ep0 buffer,
  2030. + * but config change events will also reconfigure hardware. */
  2031. + switch (ctrl->bRequest) {
  2032. +
  2033. + case USB_REQ_GET_DESCRIPTOR:
  2034. + if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
  2035. + USB_RECIP_DEVICE))
  2036. + break;
  2037. + switch (w_value >> 8) {
  2038. +
  2039. + case USB_DT_DEVICE:
  2040. + VDBG(fsg, "get device descriptor\n");
  2041. + device_desc.bMaxPacketSize0 = fsg->ep0->maxpacket;
  2042. + value = sizeof device_desc;
  2043. + memcpy(req->buf, &device_desc, value);
  2044. + break;
  2045. + case USB_DT_DEVICE_QUALIFIER:
  2046. + VDBG(fsg, "get device qualifier\n");
  2047. + if (!gadget_is_dualspeed(fsg->gadget) ||
  2048. + fsg->gadget->speed == USB_SPEED_SUPER)
  2049. + break;
  2050. + /*
  2051. + * Assume ep0 uses the same maxpacket value for both
  2052. + * speeds
  2053. + */
  2054. + dev_qualifier.bMaxPacketSize0 = fsg->ep0->maxpacket;
  2055. + value = sizeof dev_qualifier;
  2056. + memcpy(req->buf, &dev_qualifier, value);
  2057. + break;
  2058. +
  2059. + case USB_DT_OTHER_SPEED_CONFIG:
  2060. + VDBG(fsg, "get other-speed config descriptor\n");
  2061. + if (!gadget_is_dualspeed(fsg->gadget) ||
  2062. + fsg->gadget->speed == USB_SPEED_SUPER)
  2063. + break;
  2064. + goto get_config;
  2065. + case USB_DT_CONFIG:
  2066. + VDBG(fsg, "get configuration descriptor\n");
  2067. +get_config:
  2068. + value = populate_config_buf(fsg->gadget,
  2069. + req->buf,
  2070. + w_value >> 8,
  2071. + w_value & 0xff);
  2072. + break;
  2073. +
  2074. + case USB_DT_STRING:
  2075. + VDBG(fsg, "get string descriptor\n");
  2076. +
  2077. + /* wIndex == language code */
  2078. + value = usb_gadget_get_string(&fsg_stringtab,
  2079. + w_value & 0xff, req->buf);
  2080. + break;
  2081. +
  2082. + case USB_DT_BOS:
  2083. + VDBG(fsg, "get bos descriptor\n");
  2084. +
  2085. + if (gadget_is_superspeed(fsg->gadget))
  2086. + value = populate_bos(fsg, req->buf);
  2087. + break;
  2088. + }
  2089. +
  2090. + break;
  2091. +
  2092. + /* One config, two speeds */
  2093. + case USB_REQ_SET_CONFIGURATION:
  2094. + if (ctrl->bRequestType != (USB_DIR_OUT | USB_TYPE_STANDARD |
  2095. + USB_RECIP_DEVICE))
  2096. + break;
  2097. + VDBG(fsg, "set configuration\n");
  2098. + if (w_value == CONFIG_VALUE || w_value == 0) {
  2099. + fsg->new_config = w_value;
  2100. +
  2101. + /* Raise an exception to wipe out previous transaction
  2102. + * state (queued bufs, etc) and set the new config. */
  2103. + raise_exception(fsg, FSG_STATE_CONFIG_CHANGE);
  2104. + value = DELAYED_STATUS;
  2105. + }
  2106. + break;
  2107. + case USB_REQ_GET_CONFIGURATION:
  2108. + if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
  2109. + USB_RECIP_DEVICE))
  2110. + break;
  2111. + VDBG(fsg, "get configuration\n");
  2112. + *(u8 *) req->buf = fsg->config;
  2113. + value = 1;
  2114. + break;
  2115. +
  2116. + case USB_REQ_SET_INTERFACE:
  2117. + if (ctrl->bRequestType != (USB_DIR_OUT| USB_TYPE_STANDARD |
  2118. + USB_RECIP_INTERFACE))
  2119. + break;
  2120. + if (fsg->config && w_index == 0) {
  2121. +
  2122. + /* Raise an exception to wipe out previous transaction
  2123. + * state (queued bufs, etc) and install the new
  2124. + * interface altsetting. */
  2125. + raise_exception(fsg, FSG_STATE_INTERFACE_CHANGE);
  2126. + value = DELAYED_STATUS;
  2127. + }
  2128. + break;
  2129. + case USB_REQ_GET_INTERFACE:
  2130. + if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
  2131. + USB_RECIP_INTERFACE))
  2132. + break;
  2133. + if (!fsg->config)
  2134. + break;
  2135. + if (w_index != 0) {
  2136. + value = -EDOM;
  2137. + break;
  2138. + }
  2139. + VDBG(fsg, "get interface\n");
  2140. + *(u8 *) req->buf = 0;
  2141. + value = 1;
  2142. + break;
  2143. +
  2144. + default:
  2145. + VDBG(fsg,
  2146. + "unknown control req %02x.%02x v%04x i%04x l%u\n",
  2147. + ctrl->bRequestType, ctrl->bRequest,
  2148. + w_value, w_index, le16_to_cpu(ctrl->wLength));
  2149. + }
  2150. +
  2151. + return value;
  2152. +}
  2153. +
  2154. +
  2155. +static int fsg_setup(struct usb_gadget *gadget,
  2156. + const struct usb_ctrlrequest *ctrl)
  2157. +{
  2158. + struct fsg_dev *fsg = get_gadget_data(gadget);
  2159. + int rc;
  2160. + int w_length = le16_to_cpu(ctrl->wLength);
  2161. +
  2162. + ++fsg->ep0_req_tag; // Record arrival of a new request
  2163. + fsg->ep0req->context = NULL;
  2164. + fsg->ep0req->length = 0;
  2165. + dump_msg(fsg, "ep0-setup", (u8 *) ctrl, sizeof(*ctrl));
  2166. +
  2167. + if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS)
  2168. + rc = class_setup_req(fsg, ctrl);
  2169. + else
  2170. + rc = standard_setup_req(fsg, ctrl);
  2171. +
  2172. + /* Respond with data/status or defer until later? */
  2173. + if (rc >= 0 && rc != DELAYED_STATUS) {
  2174. + rc = min(rc, w_length);
  2175. + fsg->ep0req->length = rc;
  2176. + fsg->ep0req->zero = rc < w_length;
  2177. + fsg->ep0req_name = (ctrl->bRequestType & USB_DIR_IN ?
  2178. + "ep0-in" : "ep0-out");
  2179. + rc = ep0_queue(fsg);
  2180. + }
  2181. +
  2182. + /* Device either stalls (rc < 0) or reports success */
  2183. + return rc;
  2184. +}
  2185. +
  2186. +
  2187. +/*-------------------------------------------------------------------------*/
  2188. +
  2189. +/* All the following routines run in process context */
  2190. +
  2191. +
  2192. +/* Use this for bulk or interrupt transfers, not ep0 */
  2193. +static void start_transfer(struct fsg_dev *fsg, struct usb_ep *ep,
  2194. + struct usb_request *req, int *pbusy,
  2195. + enum fsg_buffer_state *state)
  2196. +{
  2197. + int rc;
  2198. +
  2199. + if (ep == fsg->bulk_in)
  2200. + dump_msg(fsg, "bulk-in", req->buf, req->length);
  2201. + else if (ep == fsg->intr_in)
  2202. + dump_msg(fsg, "intr-in", req->buf, req->length);
  2203. +
  2204. + spin_lock_irq(&fsg->lock);
  2205. + *pbusy = 1;
  2206. + *state = BUF_STATE_BUSY;
  2207. + spin_unlock_irq(&fsg->lock);
  2208. + rc = usb_ep_queue(ep, req, GFP_KERNEL);
  2209. + if (rc != 0) {
  2210. + *pbusy = 0;
  2211. + *state = BUF_STATE_EMPTY;
  2212. +
  2213. + /* We can't do much more than wait for a reset */
  2214. +
  2215. + /* Note: currently the net2280 driver fails zero-length
  2216. + * submissions if DMA is enabled. */
  2217. + if (rc != -ESHUTDOWN && !(rc == -EOPNOTSUPP &&
  2218. + req->length == 0))
  2219. + WARNING(fsg, "error in submission: %s --> %d\n",
  2220. + ep->name, rc);
  2221. + }
  2222. +}
  2223. +
  2224. +
  2225. +static int sleep_thread(struct fsg_dev *fsg)
  2226. +{
  2227. + int rc = 0;
  2228. +
  2229. + /* Wait until a signal arrives or we are woken up */
  2230. + for (;;) {
  2231. + try_to_freeze();
  2232. + set_current_state(TASK_INTERRUPTIBLE);
  2233. + if (signal_pending(current)) {
  2234. + rc = -EINTR;
  2235. + break;
  2236. + }
  2237. + if (fsg->thread_wakeup_needed)
  2238. + break;
  2239. + schedule();
  2240. + }
  2241. + __set_current_state(TASK_RUNNING);
  2242. + fsg->thread_wakeup_needed = 0;
  2243. + return rc;
  2244. +}
  2245. +
  2246. +
  2247. +/*-------------------------------------------------------------------------*/
  2248. +
  2249. +static int do_read(struct fsg_dev *fsg)
  2250. +{
  2251. + struct fsg_lun *curlun = fsg->curlun;
  2252. + u32 lba;
  2253. + struct fsg_buffhd *bh;
  2254. + int rc;
  2255. + u32 amount_left;
  2256. + loff_t file_offset, file_offset_tmp;
  2257. + unsigned int amount;
  2258. + ssize_t nread;
  2259. +
  2260. + /* Get the starting Logical Block Address and check that it's
  2261. + * not too big */
  2262. + if (fsg->cmnd[0] == READ_6)
  2263. + lba = get_unaligned_be24(&fsg->cmnd[1]);
  2264. + else {
  2265. + lba = get_unaligned_be32(&fsg->cmnd[2]);
  2266. +
  2267. + /* We allow DPO (Disable Page Out = don't save data in the
  2268. + * cache) and FUA (Force Unit Access = don't read from the
  2269. + * cache), but we don't implement them. */
  2270. + if ((fsg->cmnd[1] & ~0x18) != 0) {
  2271. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  2272. + return -EINVAL;
  2273. + }
  2274. + }
  2275. + if (lba >= curlun->num_sectors) {
  2276. + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  2277. + return -EINVAL;
  2278. + }
  2279. + file_offset = ((loff_t) lba) << curlun->blkbits;
  2280. +
  2281. + /* Carry out the file reads */
  2282. + amount_left = fsg->data_size_from_cmnd;
  2283. + if (unlikely(amount_left == 0))
  2284. + return -EIO; // No default reply
  2285. +
  2286. + for (;;) {
  2287. +
  2288. + /* Figure out how much we need to read:
  2289. + * Try to read the remaining amount.
  2290. + * But don't read more than the buffer size.
  2291. + * And don't try to read past the end of the file.
  2292. + */
  2293. + amount = min((unsigned int) amount_left, mod_data.buflen);
  2294. + amount = min((loff_t) amount,
  2295. + curlun->file_length - file_offset);
  2296. +
  2297. + /* Wait for the next buffer to become available */
  2298. + bh = fsg->next_buffhd_to_fill;
  2299. + while (bh->state != BUF_STATE_EMPTY) {
  2300. + rc = sleep_thread(fsg);
  2301. + if (rc)
  2302. + return rc;
  2303. + }
  2304. +
  2305. + /* If we were asked to read past the end of file,
  2306. + * end with an empty buffer. */
  2307. + if (amount == 0) {
  2308. + curlun->sense_data =
  2309. + SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  2310. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  2311. + curlun->info_valid = 1;
  2312. + bh->inreq->length = 0;
  2313. + bh->state = BUF_STATE_FULL;
  2314. + break;
  2315. + }
  2316. +
  2317. + /* Perform the read */
  2318. + file_offset_tmp = file_offset;
  2319. + nread = vfs_read(curlun->filp,
  2320. + (char __user *) bh->buf,
  2321. + amount, &file_offset_tmp);
  2322. + VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
  2323. + (unsigned long long) file_offset,
  2324. + (int) nread);
  2325. + if (signal_pending(current))
  2326. + return -EINTR;
  2327. +
  2328. + if (nread < 0) {
  2329. + LDBG(curlun, "error in file read: %d\n",
  2330. + (int) nread);
  2331. + nread = 0;
  2332. + } else if (nread < amount) {
  2333. + LDBG(curlun, "partial file read: %d/%u\n",
  2334. + (int) nread, amount);
  2335. + nread = round_down(nread, curlun->blksize);
  2336. + }
  2337. + file_offset += nread;
  2338. + amount_left -= nread;
  2339. + fsg->residue -= nread;
  2340. +
  2341. + /* Except at the end of the transfer, nread will be
  2342. + * equal to the buffer size, which is divisible by the
  2343. + * bulk-in maxpacket size.
  2344. + */
  2345. + bh->inreq->length = nread;
  2346. + bh->state = BUF_STATE_FULL;
  2347. +
  2348. + /* If an error occurred, report it and its position */
  2349. + if (nread < amount) {
  2350. + curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
  2351. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  2352. + curlun->info_valid = 1;
  2353. + break;
  2354. + }
  2355. +
  2356. + if (amount_left == 0)
  2357. + break; // No more left to read
  2358. +
  2359. + /* Send this buffer and go read some more */
  2360. + bh->inreq->zero = 0;
  2361. + start_transfer(fsg, fsg->bulk_in, bh->inreq,
  2362. + &bh->inreq_busy, &bh->state);
  2363. + fsg->next_buffhd_to_fill = bh->next;
  2364. + }
  2365. +
  2366. + return -EIO; // No default reply
  2367. +}
  2368. +
  2369. +
  2370. +/*-------------------------------------------------------------------------*/
  2371. +
  2372. +static int do_write(struct fsg_dev *fsg)
  2373. +{
  2374. + struct fsg_lun *curlun = fsg->curlun;
  2375. + u32 lba;
  2376. + struct fsg_buffhd *bh;
  2377. + int get_some_more;
  2378. + u32 amount_left_to_req, amount_left_to_write;
  2379. + loff_t usb_offset, file_offset, file_offset_tmp;
  2380. + unsigned int amount;
  2381. + ssize_t nwritten;
  2382. + int rc;
  2383. +
  2384. + if (curlun->ro) {
  2385. + curlun->sense_data = SS_WRITE_PROTECTED;
  2386. + return -EINVAL;
  2387. + }
  2388. + spin_lock(&curlun->filp->f_lock);
  2389. + curlun->filp->f_flags &= ~O_SYNC; // Default is not to wait
  2390. + spin_unlock(&curlun->filp->f_lock);
  2391. +
  2392. + /* Get the starting Logical Block Address and check that it's
  2393. + * not too big */
  2394. + if (fsg->cmnd[0] == WRITE_6)
  2395. + lba = get_unaligned_be24(&fsg->cmnd[1]);
  2396. + else {
  2397. + lba = get_unaligned_be32(&fsg->cmnd[2]);
  2398. +
  2399. + /* We allow DPO (Disable Page Out = don't save data in the
  2400. + * cache) and FUA (Force Unit Access = write directly to the
  2401. + * medium). We don't implement DPO; we implement FUA by
  2402. + * performing synchronous output. */
  2403. + if ((fsg->cmnd[1] & ~0x18) != 0) {
  2404. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  2405. + return -EINVAL;
  2406. + }
  2407. + /* FUA */
  2408. + if (!curlun->nofua && (fsg->cmnd[1] & 0x08)) {
  2409. + spin_lock(&curlun->filp->f_lock);
  2410. + curlun->filp->f_flags |= O_DSYNC;
  2411. + spin_unlock(&curlun->filp->f_lock);
  2412. + }
  2413. + }
  2414. + if (lba >= curlun->num_sectors) {
  2415. + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  2416. + return -EINVAL;
  2417. + }
  2418. +
  2419. + /* Carry out the file writes */
  2420. + get_some_more = 1;
  2421. + file_offset = usb_offset = ((loff_t) lba) << curlun->blkbits;
  2422. + amount_left_to_req = amount_left_to_write = fsg->data_size_from_cmnd;
  2423. +
  2424. + while (amount_left_to_write > 0) {
  2425. +
  2426. + /* Queue a request for more data from the host */
  2427. + bh = fsg->next_buffhd_to_fill;
  2428. + if (bh->state == BUF_STATE_EMPTY && get_some_more) {
  2429. +
  2430. + /* Figure out how much we want to get:
  2431. + * Try to get the remaining amount,
  2432. + * but not more than the buffer size.
  2433. + */
  2434. + amount = min(amount_left_to_req, mod_data.buflen);
  2435. +
  2436. + /* Beyond the end of the backing file? */
  2437. + if (usb_offset >= curlun->file_length) {
  2438. + get_some_more = 0;
  2439. + curlun->sense_data =
  2440. + SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  2441. + curlun->sense_data_info = usb_offset >> curlun->blkbits;
  2442. + curlun->info_valid = 1;
  2443. + continue;
  2444. + }
  2445. +
  2446. + /* Get the next buffer */
  2447. + usb_offset += amount;
  2448. + fsg->usb_amount_left -= amount;
  2449. + amount_left_to_req -= amount;
  2450. + if (amount_left_to_req == 0)
  2451. + get_some_more = 0;
  2452. +
  2453. + /* Except at the end of the transfer, amount will be
  2454. + * equal to the buffer size, which is divisible by
  2455. + * the bulk-out maxpacket size.
  2456. + */
  2457. + set_bulk_out_req_length(fsg, bh, amount);
  2458. + start_transfer(fsg, fsg->bulk_out, bh->outreq,
  2459. + &bh->outreq_busy, &bh->state);
  2460. + fsg->next_buffhd_to_fill = bh->next;
  2461. + continue;
  2462. + }
  2463. +
  2464. + /* Write the received data to the backing file */
  2465. + bh = fsg->next_buffhd_to_drain;
  2466. + if (bh->state == BUF_STATE_EMPTY && !get_some_more)
  2467. + break; // We stopped early
  2468. + if (bh->state == BUF_STATE_FULL) {
  2469. + smp_rmb();
  2470. + fsg->next_buffhd_to_drain = bh->next;
  2471. + bh->state = BUF_STATE_EMPTY;
  2472. +
  2473. + /* Did something go wrong with the transfer? */
  2474. + if (bh->outreq->status != 0) {
  2475. + curlun->sense_data = SS_COMMUNICATION_FAILURE;
  2476. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  2477. + curlun->info_valid = 1;
  2478. + break;
  2479. + }
  2480. +
  2481. + amount = bh->outreq->actual;
  2482. + if (curlun->file_length - file_offset < amount) {
  2483. + LERROR(curlun,
  2484. + "write %u @ %llu beyond end %llu\n",
  2485. + amount, (unsigned long long) file_offset,
  2486. + (unsigned long long) curlun->file_length);
  2487. + amount = curlun->file_length - file_offset;
  2488. + }
  2489. +
  2490. + /* Don't accept excess data. The spec doesn't say
  2491. + * what to do in this case. We'll ignore the error.
  2492. + */
  2493. + amount = min(amount, bh->bulk_out_intended_length);
  2494. +
  2495. + /* Don't write a partial block */
  2496. + amount = round_down(amount, curlun->blksize);
  2497. + if (amount == 0)
  2498. + goto empty_write;
  2499. +
  2500. + /* Perform the write */
  2501. + file_offset_tmp = file_offset;
  2502. + nwritten = vfs_write(curlun->filp,
  2503. + (char __user *) bh->buf,
  2504. + amount, &file_offset_tmp);
  2505. + VLDBG(curlun, "file write %u @ %llu -> %d\n", amount,
  2506. + (unsigned long long) file_offset,
  2507. + (int) nwritten);
  2508. + if (signal_pending(current))
  2509. + return -EINTR; // Interrupted!
  2510. +
  2511. + if (nwritten < 0) {
  2512. + LDBG(curlun, "error in file write: %d\n",
  2513. + (int) nwritten);
  2514. + nwritten = 0;
  2515. + } else if (nwritten < amount) {
  2516. + LDBG(curlun, "partial file write: %d/%u\n",
  2517. + (int) nwritten, amount);
  2518. + nwritten = round_down(nwritten, curlun->blksize);
  2519. + }
  2520. + file_offset += nwritten;
  2521. + amount_left_to_write -= nwritten;
  2522. + fsg->residue -= nwritten;
  2523. +
  2524. + /* If an error occurred, report it and its position */
  2525. + if (nwritten < amount) {
  2526. + curlun->sense_data = SS_WRITE_ERROR;
  2527. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  2528. + curlun->info_valid = 1;
  2529. + break;
  2530. + }
  2531. +
  2532. + empty_write:
  2533. + /* Did the host decide to stop early? */
  2534. + if (bh->outreq->actual < bh->bulk_out_intended_length) {
  2535. + fsg->short_packet_received = 1;
  2536. + break;
  2537. + }
  2538. + continue;
  2539. + }
  2540. +
  2541. + /* Wait for something to happen */
  2542. + rc = sleep_thread(fsg);
  2543. + if (rc)
  2544. + return rc;
  2545. + }
  2546. +
  2547. + return -EIO; // No default reply
  2548. +}
  2549. +
  2550. +
  2551. +/*-------------------------------------------------------------------------*/
  2552. +
  2553. +static int do_synchronize_cache(struct fsg_dev *fsg)
  2554. +{
  2555. + struct fsg_lun *curlun = fsg->curlun;
  2556. + int rc;
  2557. +
  2558. + /* We ignore the requested LBA and write out all file's
  2559. + * dirty data buffers. */
  2560. + rc = fsg_lun_fsync_sub(curlun);
  2561. + if (rc)
  2562. + curlun->sense_data = SS_WRITE_ERROR;
  2563. + return 0;
  2564. +}
  2565. +
  2566. +
  2567. +/*-------------------------------------------------------------------------*/
  2568. +
  2569. +static void invalidate_sub(struct fsg_lun *curlun)
  2570. +{
  2571. + struct file *filp = curlun->filp;
  2572. + struct inode *inode = filp->f_path.dentry->d_inode;
  2573. + unsigned long rc;
  2574. +
  2575. + rc = invalidate_mapping_pages(inode->i_mapping, 0, -1);
  2576. + VLDBG(curlun, "invalidate_mapping_pages -> %ld\n", rc);
  2577. +}
  2578. +
  2579. +static int do_verify(struct fsg_dev *fsg)
  2580. +{
  2581. + struct fsg_lun *curlun = fsg->curlun;
  2582. + u32 lba;
  2583. + u32 verification_length;
  2584. + struct fsg_buffhd *bh = fsg->next_buffhd_to_fill;
  2585. + loff_t file_offset, file_offset_tmp;
  2586. + u32 amount_left;
  2587. + unsigned int amount;
  2588. + ssize_t nread;
  2589. +
  2590. + /* Get the starting Logical Block Address and check that it's
  2591. + * not too big */
  2592. + lba = get_unaligned_be32(&fsg->cmnd[2]);
  2593. + if (lba >= curlun->num_sectors) {
  2594. + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  2595. + return -EINVAL;
  2596. + }
  2597. +
  2598. + /* We allow DPO (Disable Page Out = don't save data in the
  2599. + * cache) but we don't implement it. */
  2600. + if ((fsg->cmnd[1] & ~0x10) != 0) {
  2601. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  2602. + return -EINVAL;
  2603. + }
  2604. +
  2605. + verification_length = get_unaligned_be16(&fsg->cmnd[7]);
  2606. + if (unlikely(verification_length == 0))
  2607. + return -EIO; // No default reply
  2608. +
  2609. + /* Prepare to carry out the file verify */
  2610. + amount_left = verification_length << curlun->blkbits;
  2611. + file_offset = ((loff_t) lba) << curlun->blkbits;
  2612. +
  2613. + /* Write out all the dirty buffers before invalidating them */
  2614. + fsg_lun_fsync_sub(curlun);
  2615. + if (signal_pending(current))
  2616. + return -EINTR;
  2617. +
  2618. + invalidate_sub(curlun);
  2619. + if (signal_pending(current))
  2620. + return -EINTR;
  2621. +
  2622. + /* Just try to read the requested blocks */
  2623. + while (amount_left > 0) {
  2624. +
  2625. + /* Figure out how much we need to read:
  2626. + * Try to read the remaining amount, but not more than
  2627. + * the buffer size.
  2628. + * And don't try to read past the end of the file.
  2629. + */
  2630. + amount = min((unsigned int) amount_left, mod_data.buflen);
  2631. + amount = min((loff_t) amount,
  2632. + curlun->file_length - file_offset);
  2633. + if (amount == 0) {
  2634. + curlun->sense_data =
  2635. + SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  2636. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  2637. + curlun->info_valid = 1;
  2638. + break;
  2639. + }
  2640. +
  2641. + /* Perform the read */
  2642. + file_offset_tmp = file_offset;
  2643. + nread = vfs_read(curlun->filp,
  2644. + (char __user *) bh->buf,
  2645. + amount, &file_offset_tmp);
  2646. + VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
  2647. + (unsigned long long) file_offset,
  2648. + (int) nread);
  2649. + if (signal_pending(current))
  2650. + return -EINTR;
  2651. +
  2652. + if (nread < 0) {
  2653. + LDBG(curlun, "error in file verify: %d\n",
  2654. + (int) nread);
  2655. + nread = 0;
  2656. + } else if (nread < amount) {
  2657. + LDBG(curlun, "partial file verify: %d/%u\n",
  2658. + (int) nread, amount);
  2659. + nread = round_down(nread, curlun->blksize);
  2660. + }
  2661. + if (nread == 0) {
  2662. + curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
  2663. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  2664. + curlun->info_valid = 1;
  2665. + break;
  2666. + }
  2667. + file_offset += nread;
  2668. + amount_left -= nread;
  2669. + }
  2670. + return 0;
  2671. +}
  2672. +
  2673. +
  2674. +/*-------------------------------------------------------------------------*/
  2675. +
  2676. +static int do_inquiry(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  2677. +{
  2678. + u8 *buf = (u8 *) bh->buf;
  2679. +
  2680. + static char vendor_id[] = "Linux ";
  2681. + static char product_disk_id[] = "File-Stor Gadget";
  2682. + static char product_cdrom_id[] = "File-CD Gadget ";
  2683. +
  2684. + if (!fsg->curlun) { // Unsupported LUNs are okay
  2685. + fsg->bad_lun_okay = 1;
  2686. + memset(buf, 0, 36);
  2687. + buf[0] = 0x7f; // Unsupported, no device-type
  2688. + buf[4] = 31; // Additional length
  2689. + return 36;
  2690. + }
  2691. +
  2692. + memset(buf, 0, 8);
  2693. + buf[0] = (mod_data.cdrom ? TYPE_ROM : TYPE_DISK);
  2694. + if (mod_data.removable)
  2695. + buf[1] = 0x80;
  2696. + buf[2] = 2; // ANSI SCSI level 2
  2697. + buf[3] = 2; // SCSI-2 INQUIRY data format
  2698. + buf[4] = 31; // Additional length
  2699. + // No special options
  2700. + sprintf(buf + 8, "%-8s%-16s%04x", vendor_id,
  2701. + (mod_data.cdrom ? product_cdrom_id :
  2702. + product_disk_id),
  2703. + mod_data.release);
  2704. + return 36;
  2705. +}
  2706. +
  2707. +
  2708. +static int do_request_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  2709. +{
  2710. + struct fsg_lun *curlun = fsg->curlun;
  2711. + u8 *buf = (u8 *) bh->buf;
  2712. + u32 sd, sdinfo;
  2713. + int valid;
  2714. +
  2715. + /*
  2716. + * From the SCSI-2 spec., section 7.9 (Unit attention condition):
  2717. + *
  2718. + * If a REQUEST SENSE command is received from an initiator
  2719. + * with a pending unit attention condition (before the target
  2720. + * generates the contingent allegiance condition), then the
  2721. + * target shall either:
  2722. + * a) report any pending sense data and preserve the unit
  2723. + * attention condition on the logical unit, or,
  2724. + * b) report the unit attention condition, may discard any
  2725. + * pending sense data, and clear the unit attention
  2726. + * condition on the logical unit for that initiator.
  2727. + *
  2728. + * FSG normally uses option a); enable this code to use option b).
  2729. + */
  2730. +#if 0
  2731. + if (curlun && curlun->unit_attention_data != SS_NO_SENSE) {
  2732. + curlun->sense_data = curlun->unit_attention_data;
  2733. + curlun->unit_attention_data = SS_NO_SENSE;
  2734. + }
  2735. +#endif
  2736. +
  2737. + if (!curlun) { // Unsupported LUNs are okay
  2738. + fsg->bad_lun_okay = 1;
  2739. + sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
  2740. + sdinfo = 0;
  2741. + valid = 0;
  2742. + } else {
  2743. + sd = curlun->sense_data;
  2744. + sdinfo = curlun->sense_data_info;
  2745. + valid = curlun->info_valid << 7;
  2746. + curlun->sense_data = SS_NO_SENSE;
  2747. + curlun->sense_data_info = 0;
  2748. + curlun->info_valid = 0;
  2749. + }
  2750. +
  2751. + memset(buf, 0, 18);
  2752. + buf[0] = valid | 0x70; // Valid, current error
  2753. + buf[2] = SK(sd);
  2754. + put_unaligned_be32(sdinfo, &buf[3]); /* Sense information */
  2755. + buf[7] = 18 - 8; // Additional sense length
  2756. + buf[12] = ASC(sd);
  2757. + buf[13] = ASCQ(sd);
  2758. + return 18;
  2759. +}
  2760. +
  2761. +
  2762. +static int do_read_capacity(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  2763. +{
  2764. + struct fsg_lun *curlun = fsg->curlun;
  2765. + u32 lba = get_unaligned_be32(&fsg->cmnd[2]);
  2766. + int pmi = fsg->cmnd[8];
  2767. + u8 *buf = (u8 *) bh->buf;
  2768. +
  2769. + /* Check the PMI and LBA fields */
  2770. + if (pmi > 1 || (pmi == 0 && lba != 0)) {
  2771. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  2772. + return -EINVAL;
  2773. + }
  2774. +
  2775. + put_unaligned_be32(curlun->num_sectors - 1, &buf[0]);
  2776. + /* Max logical block */
  2777. + put_unaligned_be32(curlun->blksize, &buf[4]); /* Block length */
  2778. + return 8;
  2779. +}
  2780. +
  2781. +
  2782. +static int do_read_header(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  2783. +{
  2784. + struct fsg_lun *curlun = fsg->curlun;
  2785. + int msf = fsg->cmnd[1] & 0x02;
  2786. + u32 lba = get_unaligned_be32(&fsg->cmnd[2]);
  2787. + u8 *buf = (u8 *) bh->buf;
  2788. +
  2789. + if ((fsg->cmnd[1] & ~0x02) != 0) { /* Mask away MSF */
  2790. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  2791. + return -EINVAL;
  2792. + }
  2793. + if (lba >= curlun->num_sectors) {
  2794. + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  2795. + return -EINVAL;
  2796. + }
  2797. +
  2798. + memset(buf, 0, 8);
  2799. + buf[0] = 0x01; /* 2048 bytes of user data, rest is EC */
  2800. + store_cdrom_address(&buf[4], msf, lba);
  2801. + return 8;
  2802. +}
  2803. +
  2804. +
  2805. +static int do_read_toc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  2806. +{
  2807. + struct fsg_lun *curlun = fsg->curlun;
  2808. + int msf = fsg->cmnd[1] & 0x02;
  2809. + int start_track = fsg->cmnd[6];
  2810. + u8 *buf = (u8 *) bh->buf;
  2811. +
  2812. + if ((fsg->cmnd[1] & ~0x02) != 0 || /* Mask away MSF */
  2813. + start_track > 1) {
  2814. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  2815. + return -EINVAL;
  2816. + }
  2817. +
  2818. + memset(buf, 0, 20);
  2819. + buf[1] = (20-2); /* TOC data length */
  2820. + buf[2] = 1; /* First track number */
  2821. + buf[3] = 1; /* Last track number */
  2822. + buf[5] = 0x16; /* Data track, copying allowed */
  2823. + buf[6] = 0x01; /* Only track is number 1 */
  2824. + store_cdrom_address(&buf[8], msf, 0);
  2825. +
  2826. + buf[13] = 0x16; /* Lead-out track is data */
  2827. + buf[14] = 0xAA; /* Lead-out track number */
  2828. + store_cdrom_address(&buf[16], msf, curlun->num_sectors);
  2829. + return 20;
  2830. +}
  2831. +
  2832. +
  2833. +static int do_mode_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  2834. +{
  2835. + struct fsg_lun *curlun = fsg->curlun;
  2836. + int mscmnd = fsg->cmnd[0];
  2837. + u8 *buf = (u8 *) bh->buf;
  2838. + u8 *buf0 = buf;
  2839. + int pc, page_code;
  2840. + int changeable_values, all_pages;
  2841. + int valid_page = 0;
  2842. + int len, limit;
  2843. +
  2844. + if ((fsg->cmnd[1] & ~0x08) != 0) { // Mask away DBD
  2845. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  2846. + return -EINVAL;
  2847. + }
  2848. + pc = fsg->cmnd[2] >> 6;
  2849. + page_code = fsg->cmnd[2] & 0x3f;
  2850. + if (pc == 3) {
  2851. + curlun->sense_data = SS_SAVING_PARAMETERS_NOT_SUPPORTED;
  2852. + return -EINVAL;
  2853. + }
  2854. + changeable_values = (pc == 1);
  2855. + all_pages = (page_code == 0x3f);
  2856. +
  2857. + /* Write the mode parameter header. Fixed values are: default
  2858. + * medium type, no cache control (DPOFUA), and no block descriptors.
  2859. + * The only variable value is the WriteProtect bit. We will fill in
  2860. + * the mode data length later. */
  2861. + memset(buf, 0, 8);
  2862. + if (mscmnd == MODE_SENSE) {
  2863. + buf[2] = (curlun->ro ? 0x80 : 0x00); // WP, DPOFUA
  2864. + buf += 4;
  2865. + limit = 255;
  2866. + } else { // MODE_SENSE_10
  2867. + buf[3] = (curlun->ro ? 0x80 : 0x00); // WP, DPOFUA
  2868. + buf += 8;
  2869. + limit = 65535; // Should really be mod_data.buflen
  2870. + }
  2871. +
  2872. + /* No block descriptors */
  2873. +
  2874. + /* The mode pages, in numerical order. The only page we support
  2875. + * is the Caching page. */
  2876. + if (page_code == 0x08 || all_pages) {
  2877. + valid_page = 1;
  2878. + buf[0] = 0x08; // Page code
  2879. + buf[1] = 10; // Page length
  2880. + memset(buf+2, 0, 10); // None of the fields are changeable
  2881. +
  2882. + if (!changeable_values) {
  2883. + buf[2] = 0x04; // Write cache enable,
  2884. + // Read cache not disabled
  2885. + // No cache retention priorities
  2886. + put_unaligned_be16(0xffff, &buf[4]);
  2887. + /* Don't disable prefetch */
  2888. + /* Minimum prefetch = 0 */
  2889. + put_unaligned_be16(0xffff, &buf[8]);
  2890. + /* Maximum prefetch */
  2891. + put_unaligned_be16(0xffff, &buf[10]);
  2892. + /* Maximum prefetch ceiling */
  2893. + }
  2894. + buf += 12;
  2895. + }
  2896. +
  2897. + /* Check that a valid page was requested and the mode data length
  2898. + * isn't too long. */
  2899. + len = buf - buf0;
  2900. + if (!valid_page || len > limit) {
  2901. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  2902. + return -EINVAL;
  2903. + }
  2904. +
  2905. + /* Store the mode data length */
  2906. + if (mscmnd == MODE_SENSE)
  2907. + buf0[0] = len - 1;
  2908. + else
  2909. + put_unaligned_be16(len - 2, buf0);
  2910. + return len;
  2911. +}
  2912. +
  2913. +
  2914. +static int do_start_stop(struct fsg_dev *fsg)
  2915. +{
  2916. + struct fsg_lun *curlun = fsg->curlun;
  2917. + int loej, start;
  2918. +
  2919. + if (!mod_data.removable) {
  2920. + curlun->sense_data = SS_INVALID_COMMAND;
  2921. + return -EINVAL;
  2922. + }
  2923. +
  2924. + // int immed = fsg->cmnd[1] & 0x01;
  2925. + loej = fsg->cmnd[4] & 0x02;
  2926. + start = fsg->cmnd[4] & 0x01;
  2927. +
  2928. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  2929. + if ((fsg->cmnd[1] & ~0x01) != 0 || // Mask away Immed
  2930. + (fsg->cmnd[4] & ~0x03) != 0) { // Mask LoEj, Start
  2931. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  2932. + return -EINVAL;
  2933. + }
  2934. +
  2935. + if (!start) {
  2936. +
  2937. + /* Are we allowed to unload the media? */
  2938. + if (curlun->prevent_medium_removal) {
  2939. + LDBG(curlun, "unload attempt prevented\n");
  2940. + curlun->sense_data = SS_MEDIUM_REMOVAL_PREVENTED;
  2941. + return -EINVAL;
  2942. + }
  2943. + if (loej) { // Simulate an unload/eject
  2944. + up_read(&fsg->filesem);
  2945. + down_write(&fsg->filesem);
  2946. + fsg_lun_close(curlun);
  2947. + up_write(&fsg->filesem);
  2948. + down_read(&fsg->filesem);
  2949. + }
  2950. + } else {
  2951. +
  2952. + /* Our emulation doesn't support mounting; the medium is
  2953. + * available for use as soon as it is loaded. */
  2954. + if (!fsg_lun_is_open(curlun)) {
  2955. + curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
  2956. + return -EINVAL;
  2957. + }
  2958. + }
  2959. +#endif
  2960. + return 0;
  2961. +}
  2962. +
  2963. +
  2964. +static int do_prevent_allow(struct fsg_dev *fsg)
  2965. +{
  2966. + struct fsg_lun *curlun = fsg->curlun;
  2967. + int prevent;
  2968. +
  2969. + if (!mod_data.removable) {
  2970. + curlun->sense_data = SS_INVALID_COMMAND;
  2971. + return -EINVAL;
  2972. + }
  2973. +
  2974. + prevent = fsg->cmnd[4] & 0x01;
  2975. + if ((fsg->cmnd[4] & ~0x01) != 0) { // Mask away Prevent
  2976. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  2977. + return -EINVAL;
  2978. + }
  2979. +
  2980. + if (curlun->prevent_medium_removal && !prevent)
  2981. + fsg_lun_fsync_sub(curlun);
  2982. + curlun->prevent_medium_removal = prevent;
  2983. + return 0;
  2984. +}
  2985. +
  2986. +
  2987. +static int do_read_format_capacities(struct fsg_dev *fsg,
  2988. + struct fsg_buffhd *bh)
  2989. +{
  2990. + struct fsg_lun *curlun = fsg->curlun;
  2991. + u8 *buf = (u8 *) bh->buf;
  2992. +
  2993. + buf[0] = buf[1] = buf[2] = 0;
  2994. + buf[3] = 8; // Only the Current/Maximum Capacity Descriptor
  2995. + buf += 4;
  2996. +
  2997. + put_unaligned_be32(curlun->num_sectors, &buf[0]);
  2998. + /* Number of blocks */
  2999. + put_unaligned_be32(curlun->blksize, &buf[4]); /* Block length */
  3000. + buf[4] = 0x02; /* Current capacity */
  3001. + return 12;
  3002. +}
  3003. +
  3004. +
  3005. +static int do_mode_select(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  3006. +{
  3007. + struct fsg_lun *curlun = fsg->curlun;
  3008. +
  3009. + /* We don't support MODE SELECT */
  3010. + curlun->sense_data = SS_INVALID_COMMAND;
  3011. + return -EINVAL;
  3012. +}
  3013. +
  3014. +
  3015. +/*-------------------------------------------------------------------------*/
  3016. +
  3017. +static int halt_bulk_in_endpoint(struct fsg_dev *fsg)
  3018. +{
  3019. + int rc;
  3020. +
  3021. + rc = fsg_set_halt(fsg, fsg->bulk_in);
  3022. + if (rc == -EAGAIN)
  3023. + VDBG(fsg, "delayed bulk-in endpoint halt\n");
  3024. + while (rc != 0) {
  3025. + if (rc != -EAGAIN) {
  3026. + WARNING(fsg, "usb_ep_set_halt -> %d\n", rc);
  3027. + rc = 0;
  3028. + break;
  3029. + }
  3030. +
  3031. + /* Wait for a short time and then try again */
  3032. + if (msleep_interruptible(100) != 0)
  3033. + return -EINTR;
  3034. + rc = usb_ep_set_halt(fsg->bulk_in);
  3035. + }
  3036. + return rc;
  3037. +}
  3038. +
  3039. +static int wedge_bulk_in_endpoint(struct fsg_dev *fsg)
  3040. +{
  3041. + int rc;
  3042. +
  3043. + DBG(fsg, "bulk-in set wedge\n");
  3044. + rc = usb_ep_set_wedge(fsg->bulk_in);
  3045. + if (rc == -EAGAIN)
  3046. + VDBG(fsg, "delayed bulk-in endpoint wedge\n");
  3047. + while (rc != 0) {
  3048. + if (rc != -EAGAIN) {
  3049. + WARNING(fsg, "usb_ep_set_wedge -> %d\n", rc);
  3050. + rc = 0;
  3051. + break;
  3052. + }
  3053. +
  3054. + /* Wait for a short time and then try again */
  3055. + if (msleep_interruptible(100) != 0)
  3056. + return -EINTR;
  3057. + rc = usb_ep_set_wedge(fsg->bulk_in);
  3058. + }
  3059. + return rc;
  3060. +}
  3061. +
  3062. +static int throw_away_data(struct fsg_dev *fsg)
  3063. +{
  3064. + struct fsg_buffhd *bh;
  3065. + u32 amount;
  3066. + int rc;
  3067. +
  3068. + while ((bh = fsg->next_buffhd_to_drain)->state != BUF_STATE_EMPTY ||
  3069. + fsg->usb_amount_left > 0) {
  3070. +
  3071. + /* Throw away the data in a filled buffer */
  3072. + if (bh->state == BUF_STATE_FULL) {
  3073. + smp_rmb();
  3074. + bh->state = BUF_STATE_EMPTY;
  3075. + fsg->next_buffhd_to_drain = bh->next;
  3076. +
  3077. + /* A short packet or an error ends everything */
  3078. + if (bh->outreq->actual < bh->bulk_out_intended_length ||
  3079. + bh->outreq->status != 0) {
  3080. + raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
  3081. + return -EINTR;
  3082. + }
  3083. + continue;
  3084. + }
  3085. +
  3086. + /* Try to submit another request if we need one */
  3087. + bh = fsg->next_buffhd_to_fill;
  3088. + if (bh->state == BUF_STATE_EMPTY && fsg->usb_amount_left > 0) {
  3089. + amount = min(fsg->usb_amount_left,
  3090. + (u32) mod_data.buflen);
  3091. +
  3092. + /* Except at the end of the transfer, amount will be
  3093. + * equal to the buffer size, which is divisible by
  3094. + * the bulk-out maxpacket size.
  3095. + */
  3096. + set_bulk_out_req_length(fsg, bh, amount);
  3097. + start_transfer(fsg, fsg->bulk_out, bh->outreq,
  3098. + &bh->outreq_busy, &bh->state);
  3099. + fsg->next_buffhd_to_fill = bh->next;
  3100. + fsg->usb_amount_left -= amount;
  3101. + continue;
  3102. + }
  3103. +
  3104. + /* Otherwise wait for something to happen */
  3105. + rc = sleep_thread(fsg);
  3106. + if (rc)
  3107. + return rc;
  3108. + }
  3109. + return 0;
  3110. +}
  3111. +
  3112. +
  3113. +static int finish_reply(struct fsg_dev *fsg)
  3114. +{
  3115. + struct fsg_buffhd *bh = fsg->next_buffhd_to_fill;
  3116. + int rc = 0;
  3117. +
  3118. + switch (fsg->data_dir) {
  3119. + case DATA_DIR_NONE:
  3120. + break; // Nothing to send
  3121. +
  3122. + /* If we don't know whether the host wants to read or write,
  3123. + * this must be CB or CBI with an unknown command. We mustn't
  3124. + * try to send or receive any data. So stall both bulk pipes
  3125. + * if we can and wait for a reset. */
  3126. + case DATA_DIR_UNKNOWN:
  3127. + if (mod_data.can_stall) {
  3128. + fsg_set_halt(fsg, fsg->bulk_out);
  3129. + rc = halt_bulk_in_endpoint(fsg);
  3130. + }
  3131. + break;
  3132. +
  3133. + /* All but the last buffer of data must have already been sent */
  3134. + case DATA_DIR_TO_HOST:
  3135. + if (fsg->data_size == 0)
  3136. + ; // Nothing to send
  3137. +
  3138. + /* If there's no residue, simply send the last buffer */
  3139. + else if (fsg->residue == 0) {
  3140. + bh->inreq->zero = 0;
  3141. + start_transfer(fsg, fsg->bulk_in, bh->inreq,
  3142. + &bh->inreq_busy, &bh->state);
  3143. + fsg->next_buffhd_to_fill = bh->next;
  3144. + }
  3145. +
  3146. + /* There is a residue. For CB and CBI, simply mark the end
  3147. + * of the data with a short packet. However, if we are
  3148. + * allowed to stall, there was no data at all (residue ==
  3149. + * data_size), and the command failed (invalid LUN or
  3150. + * sense data is set), then halt the bulk-in endpoint
  3151. + * instead. */
  3152. + else if (!transport_is_bbb()) {
  3153. + if (mod_data.can_stall &&
  3154. + fsg->residue == fsg->data_size &&
  3155. + (!fsg->curlun || fsg->curlun->sense_data != SS_NO_SENSE)) {
  3156. + bh->state = BUF_STATE_EMPTY;
  3157. + rc = halt_bulk_in_endpoint(fsg);
  3158. + } else {
  3159. + bh->inreq->zero = 1;
  3160. + start_transfer(fsg, fsg->bulk_in, bh->inreq,
  3161. + &bh->inreq_busy, &bh->state);
  3162. + fsg->next_buffhd_to_fill = bh->next;
  3163. + }
  3164. + }
  3165. +
  3166. + /*
  3167. + * For Bulk-only, mark the end of the data with a short
  3168. + * packet. If we are allowed to stall, halt the bulk-in
  3169. + * endpoint. (Note: This violates the Bulk-Only Transport
  3170. + * specification, which requires us to pad the data if we
  3171. + * don't halt the endpoint. Presumably nobody will mind.)
  3172. + */
  3173. + else {
  3174. + bh->inreq->zero = 1;
  3175. + start_transfer(fsg, fsg->bulk_in, bh->inreq,
  3176. + &bh->inreq_busy, &bh->state);
  3177. + fsg->next_buffhd_to_fill = bh->next;
  3178. + if (mod_data.can_stall)
  3179. + rc = halt_bulk_in_endpoint(fsg);
  3180. + }
  3181. + break;
  3182. +
  3183. + /* We have processed all we want from the data the host has sent.
  3184. + * There may still be outstanding bulk-out requests. */
  3185. + case DATA_DIR_FROM_HOST:
  3186. + if (fsg->residue == 0)
  3187. + ; // Nothing to receive
  3188. +
  3189. + /* Did the host stop sending unexpectedly early? */
  3190. + else if (fsg->short_packet_received) {
  3191. + raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
  3192. + rc = -EINTR;
  3193. + }
  3194. +
  3195. + /* We haven't processed all the incoming data. Even though
  3196. + * we may be allowed to stall, doing so would cause a race.
  3197. + * The controller may already have ACK'ed all the remaining
  3198. + * bulk-out packets, in which case the host wouldn't see a
  3199. + * STALL. Not realizing the endpoint was halted, it wouldn't
  3200. + * clear the halt -- leading to problems later on. */
  3201. +#if 0
  3202. + else if (mod_data.can_stall) {
  3203. + fsg_set_halt(fsg, fsg->bulk_out);
  3204. + raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
  3205. + rc = -EINTR;
  3206. + }
  3207. +#endif
  3208. +
  3209. + /* We can't stall. Read in the excess data and throw it
  3210. + * all away. */
  3211. + else
  3212. + rc = throw_away_data(fsg);
  3213. + break;
  3214. + }
  3215. + return rc;
  3216. +}
  3217. +
  3218. +
  3219. +static int send_status(struct fsg_dev *fsg)
  3220. +{
  3221. + struct fsg_lun *curlun = fsg->curlun;
  3222. + struct fsg_buffhd *bh;
  3223. + int rc;
  3224. + u8 status = US_BULK_STAT_OK;
  3225. + u32 sd, sdinfo = 0;
  3226. +
  3227. + /* Wait for the next buffer to become available */
  3228. + bh = fsg->next_buffhd_to_fill;
  3229. + while (bh->state != BUF_STATE_EMPTY) {
  3230. + rc = sleep_thread(fsg);
  3231. + if (rc)
  3232. + return rc;
  3233. + }
  3234. +
  3235. + if (curlun) {
  3236. + sd = curlun->sense_data;
  3237. + sdinfo = curlun->sense_data_info;
  3238. + } else if (fsg->bad_lun_okay)
  3239. + sd = SS_NO_SENSE;
  3240. + else
  3241. + sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
  3242. +
  3243. + if (fsg->phase_error) {
  3244. + DBG(fsg, "sending phase-error status\n");
  3245. + status = US_BULK_STAT_PHASE;
  3246. + sd = SS_INVALID_COMMAND;
  3247. + } else if (sd != SS_NO_SENSE) {
  3248. + DBG(fsg, "sending command-failure status\n");
  3249. + status = US_BULK_STAT_FAIL;
  3250. + VDBG(fsg, " sense data: SK x%02x, ASC x%02x, ASCQ x%02x;"
  3251. + " info x%x\n",
  3252. + SK(sd), ASC(sd), ASCQ(sd), sdinfo);
  3253. + }
  3254. +
  3255. + if (transport_is_bbb()) {
  3256. + struct bulk_cs_wrap *csw = bh->buf;
  3257. +
  3258. + /* Store and send the Bulk-only CSW */
  3259. + csw->Signature = cpu_to_le32(US_BULK_CS_SIGN);
  3260. + csw->Tag = fsg->tag;
  3261. + csw->Residue = cpu_to_le32(fsg->residue);
  3262. + csw->Status = status;
  3263. +
  3264. + bh->inreq->length = US_BULK_CS_WRAP_LEN;
  3265. + bh->inreq->zero = 0;
  3266. + start_transfer(fsg, fsg->bulk_in, bh->inreq,
  3267. + &bh->inreq_busy, &bh->state);
  3268. +
  3269. + } else if (mod_data.transport_type == USB_PR_CB) {
  3270. +
  3271. + /* Control-Bulk transport has no status phase! */
  3272. + return 0;
  3273. +
  3274. + } else { // USB_PR_CBI
  3275. + struct interrupt_data *buf = bh->buf;
  3276. +
  3277. + /* Store and send the Interrupt data. UFI sends the ASC
  3278. + * and ASCQ bytes. Everything else sends a Type (which
  3279. + * is always 0) and the status Value. */
  3280. + if (mod_data.protocol_type == USB_SC_UFI) {
  3281. + buf->bType = ASC(sd);
  3282. + buf->bValue = ASCQ(sd);
  3283. + } else {
  3284. + buf->bType = 0;
  3285. + buf->bValue = status;
  3286. + }
  3287. + fsg->intreq->length = CBI_INTERRUPT_DATA_LEN;
  3288. +
  3289. + fsg->intr_buffhd = bh; // Point to the right buffhd
  3290. + fsg->intreq->buf = bh->inreq->buf;
  3291. + fsg->intreq->context = bh;
  3292. + start_transfer(fsg, fsg->intr_in, fsg->intreq,
  3293. + &fsg->intreq_busy, &bh->state);
  3294. + }
  3295. +
  3296. + fsg->next_buffhd_to_fill = bh->next;
  3297. + return 0;
  3298. +}
  3299. +
  3300. +
  3301. +/*-------------------------------------------------------------------------*/
  3302. +
  3303. +/* Check whether the command is properly formed and whether its data size
  3304. + * and direction agree with the values we already have. */
  3305. +static int check_command(struct fsg_dev *fsg, int cmnd_size,
  3306. + enum data_direction data_dir, unsigned int mask,
  3307. + int needs_medium, const char *name)
  3308. +{
  3309. + int i;
  3310. + int lun = fsg->cmnd[1] >> 5;
  3311. + static const char dirletter[4] = {'u', 'o', 'i', 'n'};
  3312. + char hdlen[20];
  3313. + struct fsg_lun *curlun;
  3314. +
  3315. + /* Adjust the expected cmnd_size for protocol encapsulation padding.
  3316. + * Transparent SCSI doesn't pad. */
  3317. + if (protocol_is_scsi())
  3318. + ;
  3319. +
  3320. + /* There's some disagreement as to whether RBC pads commands or not.
  3321. + * We'll play it safe and accept either form. */
  3322. + else if (mod_data.protocol_type == USB_SC_RBC) {
  3323. + if (fsg->cmnd_size == 12)
  3324. + cmnd_size = 12;
  3325. +
  3326. + /* All the other protocols pad to 12 bytes */
  3327. + } else
  3328. + cmnd_size = 12;
  3329. +
  3330. + hdlen[0] = 0;
  3331. + if (fsg->data_dir != DATA_DIR_UNKNOWN)
  3332. + sprintf(hdlen, ", H%c=%u", dirletter[(int) fsg->data_dir],
  3333. + fsg->data_size);
  3334. + VDBG(fsg, "SCSI command: %s; Dc=%d, D%c=%u; Hc=%d%s\n",
  3335. + name, cmnd_size, dirletter[(int) data_dir],
  3336. + fsg->data_size_from_cmnd, fsg->cmnd_size, hdlen);
  3337. +
  3338. + /* We can't reply at all until we know the correct data direction
  3339. + * and size. */
  3340. + if (fsg->data_size_from_cmnd == 0)
  3341. + data_dir = DATA_DIR_NONE;
  3342. + if (fsg->data_dir == DATA_DIR_UNKNOWN) { // CB or CBI
  3343. + fsg->data_dir = data_dir;
  3344. + fsg->data_size = fsg->data_size_from_cmnd;
  3345. +
  3346. + } else { // Bulk-only
  3347. + if (fsg->data_size < fsg->data_size_from_cmnd) {
  3348. +
  3349. + /* Host data size < Device data size is a phase error.
  3350. + * Carry out the command, but only transfer as much
  3351. + * as we are allowed. */
  3352. + fsg->data_size_from_cmnd = fsg->data_size;
  3353. + fsg->phase_error = 1;
  3354. + }
  3355. + }
  3356. + fsg->residue = fsg->usb_amount_left = fsg->data_size;
  3357. +
  3358. + /* Conflicting data directions is a phase error */
  3359. + if (fsg->data_dir != data_dir && fsg->data_size_from_cmnd > 0) {
  3360. + fsg->phase_error = 1;
  3361. + return -EINVAL;
  3362. + }
  3363. +
  3364. + /* Verify the length of the command itself */
  3365. + if (cmnd_size != fsg->cmnd_size) {
  3366. +
  3367. + /* Special case workaround: There are plenty of buggy SCSI
  3368. + * implementations. Many have issues with cbw->Length
  3369. + * field passing a wrong command size. For those cases we
  3370. + * always try to work around the problem by using the length
  3371. + * sent by the host side provided it is at least as large
  3372. + * as the correct command length.
  3373. + * Examples of such cases would be MS-Windows, which issues
  3374. + * REQUEST SENSE with cbw->Length == 12 where it should
  3375. + * be 6, and xbox360 issuing INQUIRY, TEST UNIT READY and
  3376. + * REQUEST SENSE with cbw->Length == 10 where it should
  3377. + * be 6 as well.
  3378. + */
  3379. + if (cmnd_size <= fsg->cmnd_size) {
  3380. + DBG(fsg, "%s is buggy! Expected length %d "
  3381. + "but we got %d\n", name,
  3382. + cmnd_size, fsg->cmnd_size);
  3383. + cmnd_size = fsg->cmnd_size;
  3384. + } else {
  3385. + fsg->phase_error = 1;
  3386. + return -EINVAL;
  3387. + }
  3388. + }
  3389. +
  3390. + /* Check that the LUN values are consistent */
  3391. + if (transport_is_bbb()) {
  3392. + if (fsg->lun != lun)
  3393. + DBG(fsg, "using LUN %d from CBW, "
  3394. + "not LUN %d from CDB\n",
  3395. + fsg->lun, lun);
  3396. + }
  3397. +
  3398. + /* Check the LUN */
  3399. + curlun = fsg->curlun;
  3400. + if (curlun) {
  3401. + if (fsg->cmnd[0] != REQUEST_SENSE) {
  3402. + curlun->sense_data = SS_NO_SENSE;
  3403. + curlun->sense_data_info = 0;
  3404. + curlun->info_valid = 0;
  3405. + }
  3406. + } else {
  3407. + fsg->bad_lun_okay = 0;
  3408. +
  3409. + /* INQUIRY and REQUEST SENSE commands are explicitly allowed
  3410. + * to use unsupported LUNs; all others may not. */
  3411. + if (fsg->cmnd[0] != INQUIRY &&
  3412. + fsg->cmnd[0] != REQUEST_SENSE) {
  3413. + DBG(fsg, "unsupported LUN %d\n", fsg->lun);
  3414. + return -EINVAL;
  3415. + }
  3416. + }
  3417. +
  3418. + /* If a unit attention condition exists, only INQUIRY and
  3419. + * REQUEST SENSE commands are allowed; anything else must fail. */
  3420. + if (curlun && curlun->unit_attention_data != SS_NO_SENSE &&
  3421. + fsg->cmnd[0] != INQUIRY &&
  3422. + fsg->cmnd[0] != REQUEST_SENSE) {
  3423. + curlun->sense_data = curlun->unit_attention_data;
  3424. + curlun->unit_attention_data = SS_NO_SENSE;
  3425. + return -EINVAL;
  3426. + }
  3427. +
  3428. + /* Check that only command bytes listed in the mask are non-zero */
  3429. + fsg->cmnd[1] &= 0x1f; // Mask away the LUN
  3430. + for (i = 1; i < cmnd_size; ++i) {
  3431. + if (fsg->cmnd[i] && !(mask & (1 << i))) {
  3432. + if (curlun)
  3433. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  3434. + return -EINVAL;
  3435. + }
  3436. + }
  3437. +
  3438. + /* If the medium isn't mounted and the command needs to access
  3439. + * it, return an error. */
  3440. + if (curlun && !fsg_lun_is_open(curlun) && needs_medium) {
  3441. + curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
  3442. + return -EINVAL;
  3443. + }
  3444. +
  3445. + return 0;
  3446. +}
  3447. +
  3448. +/* wrapper of check_command for data size in blocks handling */
  3449. +static int check_command_size_in_blocks(struct fsg_dev *fsg, int cmnd_size,
  3450. + enum data_direction data_dir, unsigned int mask,
  3451. + int needs_medium, const char *name)
  3452. +{
  3453. + if (fsg->curlun)
  3454. + fsg->data_size_from_cmnd <<= fsg->curlun->blkbits;
  3455. + return check_command(fsg, cmnd_size, data_dir,
  3456. + mask, needs_medium, name);
  3457. +}
  3458. +
  3459. +static int do_scsi_command(struct fsg_dev *fsg)
  3460. +{
  3461. + struct fsg_buffhd *bh;
  3462. + int rc;
  3463. + int reply = -EINVAL;
  3464. + int i;
  3465. + static char unknown[16];
  3466. +
  3467. + dump_cdb(fsg);
  3468. +
  3469. + /* Wait for the next buffer to become available for data or status */
  3470. + bh = fsg->next_buffhd_to_drain = fsg->next_buffhd_to_fill;
  3471. + while (bh->state != BUF_STATE_EMPTY) {
  3472. + rc = sleep_thread(fsg);
  3473. + if (rc)
  3474. + return rc;
  3475. + }
  3476. + fsg->phase_error = 0;
  3477. + fsg->short_packet_received = 0;
  3478. +
  3479. + down_read(&fsg->filesem); // We're using the backing file
  3480. + switch (fsg->cmnd[0]) {
  3481. +
  3482. + case INQUIRY:
  3483. + fsg->data_size_from_cmnd = fsg->cmnd[4];
  3484. + if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
  3485. + (1<<4), 0,
  3486. + "INQUIRY")) == 0)
  3487. + reply = do_inquiry(fsg, bh);
  3488. + break;
  3489. +
  3490. + case MODE_SELECT:
  3491. + fsg->data_size_from_cmnd = fsg->cmnd[4];
  3492. + if ((reply = check_command(fsg, 6, DATA_DIR_FROM_HOST,
  3493. + (1<<1) | (1<<4), 0,
  3494. + "MODE SELECT(6)")) == 0)
  3495. + reply = do_mode_select(fsg, bh);
  3496. + break;
  3497. +
  3498. + case MODE_SELECT_10:
  3499. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  3500. + if ((reply = check_command(fsg, 10, DATA_DIR_FROM_HOST,
  3501. + (1<<1) | (3<<7), 0,
  3502. + "MODE SELECT(10)")) == 0)
  3503. + reply = do_mode_select(fsg, bh);
  3504. + break;
  3505. +
  3506. + case MODE_SENSE:
  3507. + fsg->data_size_from_cmnd = fsg->cmnd[4];
  3508. + if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
  3509. + (1<<1) | (1<<2) | (1<<4), 0,
  3510. + "MODE SENSE(6)")) == 0)
  3511. + reply = do_mode_sense(fsg, bh);
  3512. + break;
  3513. +
  3514. + case MODE_SENSE_10:
  3515. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  3516. + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
  3517. + (1<<1) | (1<<2) | (3<<7), 0,
  3518. + "MODE SENSE(10)")) == 0)
  3519. + reply = do_mode_sense(fsg, bh);
  3520. + break;
  3521. +
  3522. + case ALLOW_MEDIUM_REMOVAL:
  3523. + fsg->data_size_from_cmnd = 0;
  3524. + if ((reply = check_command(fsg, 6, DATA_DIR_NONE,
  3525. + (1<<4), 0,
  3526. + "PREVENT-ALLOW MEDIUM REMOVAL")) == 0)
  3527. + reply = do_prevent_allow(fsg);
  3528. + break;
  3529. +
  3530. + case READ_6:
  3531. + i = fsg->cmnd[4];
  3532. + fsg->data_size_from_cmnd = (i == 0) ? 256 : i;
  3533. + if ((reply = check_command_size_in_blocks(fsg, 6,
  3534. + DATA_DIR_TO_HOST,
  3535. + (7<<1) | (1<<4), 1,
  3536. + "READ(6)")) == 0)
  3537. + reply = do_read(fsg);
  3538. + break;
  3539. +
  3540. + case READ_10:
  3541. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  3542. + if ((reply = check_command_size_in_blocks(fsg, 10,
  3543. + DATA_DIR_TO_HOST,
  3544. + (1<<1) | (0xf<<2) | (3<<7), 1,
  3545. + "READ(10)")) == 0)
  3546. + reply = do_read(fsg);
  3547. + break;
  3548. +
  3549. + case READ_12:
  3550. + fsg->data_size_from_cmnd = get_unaligned_be32(&fsg->cmnd[6]);
  3551. + if ((reply = check_command_size_in_blocks(fsg, 12,
  3552. + DATA_DIR_TO_HOST,
  3553. + (1<<1) | (0xf<<2) | (0xf<<6), 1,
  3554. + "READ(12)")) == 0)
  3555. + reply = do_read(fsg);
  3556. + break;
  3557. +
  3558. + case READ_CAPACITY:
  3559. + fsg->data_size_from_cmnd = 8;
  3560. + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
  3561. + (0xf<<2) | (1<<8), 1,
  3562. + "READ CAPACITY")) == 0)
  3563. + reply = do_read_capacity(fsg, bh);
  3564. + break;
  3565. +
  3566. + case READ_HEADER:
  3567. + if (!mod_data.cdrom)
  3568. + goto unknown_cmnd;
  3569. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  3570. + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
  3571. + (3<<7) | (0x1f<<1), 1,
  3572. + "READ HEADER")) == 0)
  3573. + reply = do_read_header(fsg, bh);
  3574. + break;
  3575. +
  3576. + case READ_TOC:
  3577. + if (!mod_data.cdrom)
  3578. + goto unknown_cmnd;
  3579. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  3580. + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
  3581. + (7<<6) | (1<<1), 1,
  3582. + "READ TOC")) == 0)
  3583. + reply = do_read_toc(fsg, bh);
  3584. + break;
  3585. +
  3586. + case READ_FORMAT_CAPACITIES:
  3587. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  3588. + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
  3589. + (3<<7), 1,
  3590. + "READ FORMAT CAPACITIES")) == 0)
  3591. + reply = do_read_format_capacities(fsg, bh);
  3592. + break;
  3593. +
  3594. + case REQUEST_SENSE:
  3595. + fsg->data_size_from_cmnd = fsg->cmnd[4];
  3596. + if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
  3597. + (1<<4), 0,
  3598. + "REQUEST SENSE")) == 0)
  3599. + reply = do_request_sense(fsg, bh);
  3600. + break;
  3601. +
  3602. + case START_STOP:
  3603. + fsg->data_size_from_cmnd = 0;
  3604. + if ((reply = check_command(fsg, 6, DATA_DIR_NONE,
  3605. + (1<<1) | (1<<4), 0,
  3606. + "START-STOP UNIT")) == 0)
  3607. + reply = do_start_stop(fsg);
  3608. + break;
  3609. +
  3610. + case SYNCHRONIZE_CACHE:
  3611. + fsg->data_size_from_cmnd = 0;
  3612. + if ((reply = check_command(fsg, 10, DATA_DIR_NONE,
  3613. + (0xf<<2) | (3<<7), 1,
  3614. + "SYNCHRONIZE CACHE")) == 0)
  3615. + reply = do_synchronize_cache(fsg);
  3616. + break;
  3617. +
  3618. + case TEST_UNIT_READY:
  3619. + fsg->data_size_from_cmnd = 0;
  3620. + reply = check_command(fsg, 6, DATA_DIR_NONE,
  3621. + 0, 1,
  3622. + "TEST UNIT READY");
  3623. + break;
  3624. +
  3625. + /* Although optional, this command is used by MS-Windows. We
  3626. + * support a minimal version: BytChk must be 0. */
  3627. + case VERIFY:
  3628. + fsg->data_size_from_cmnd = 0;
  3629. + if ((reply = check_command(fsg, 10, DATA_DIR_NONE,
  3630. + (1<<1) | (0xf<<2) | (3<<7), 1,
  3631. + "VERIFY")) == 0)
  3632. + reply = do_verify(fsg);
  3633. + break;
  3634. +
  3635. + case WRITE_6:
  3636. + i = fsg->cmnd[4];
  3637. + fsg->data_size_from_cmnd = (i == 0) ? 256 : i;
  3638. + if ((reply = check_command_size_in_blocks(fsg, 6,
  3639. + DATA_DIR_FROM_HOST,
  3640. + (7<<1) | (1<<4), 1,
  3641. + "WRITE(6)")) == 0)
  3642. + reply = do_write(fsg);
  3643. + break;
  3644. +
  3645. + case WRITE_10:
  3646. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  3647. + if ((reply = check_command_size_in_blocks(fsg, 10,
  3648. + DATA_DIR_FROM_HOST,
  3649. + (1<<1) | (0xf<<2) | (3<<7), 1,
  3650. + "WRITE(10)")) == 0)
  3651. + reply = do_write(fsg);
  3652. + break;
  3653. +
  3654. + case WRITE_12:
  3655. + fsg->data_size_from_cmnd = get_unaligned_be32(&fsg->cmnd[6]);
  3656. + if ((reply = check_command_size_in_blocks(fsg, 12,
  3657. + DATA_DIR_FROM_HOST,
  3658. + (1<<1) | (0xf<<2) | (0xf<<6), 1,
  3659. + "WRITE(12)")) == 0)
  3660. + reply = do_write(fsg);
  3661. + break;
  3662. +
  3663. + /* Some mandatory commands that we recognize but don't implement.
  3664. + * They don't mean much in this setting. It's left as an exercise
  3665. + * for anyone interested to implement RESERVE and RELEASE in terms
  3666. + * of Posix locks. */
  3667. + case FORMAT_UNIT:
  3668. + case RELEASE:
  3669. + case RESERVE:
  3670. + case SEND_DIAGNOSTIC:
  3671. + // Fall through
  3672. +
  3673. + default:
  3674. + unknown_cmnd:
  3675. + fsg->data_size_from_cmnd = 0;
  3676. + sprintf(unknown, "Unknown x%02x", fsg->cmnd[0]);
  3677. + if ((reply = check_command(fsg, fsg->cmnd_size,
  3678. + DATA_DIR_UNKNOWN, ~0, 0, unknown)) == 0) {
  3679. + fsg->curlun->sense_data = SS_INVALID_COMMAND;
  3680. + reply = -EINVAL;
  3681. + }
  3682. + break;
  3683. + }
  3684. + up_read(&fsg->filesem);
  3685. +
  3686. + if (reply == -EINTR || signal_pending(current))
  3687. + return -EINTR;
  3688. +
  3689. + /* Set up the single reply buffer for finish_reply() */
  3690. + if (reply == -EINVAL)
  3691. + reply = 0; // Error reply length
  3692. + if (reply >= 0 && fsg->data_dir == DATA_DIR_TO_HOST) {
  3693. + reply = min((u32) reply, fsg->data_size_from_cmnd);
  3694. + bh->inreq->length = reply;
  3695. + bh->state = BUF_STATE_FULL;
  3696. + fsg->residue -= reply;
  3697. + } // Otherwise it's already set
  3698. +
  3699. + return 0;
  3700. +}
  3701. +
  3702. +
  3703. +/*-------------------------------------------------------------------------*/
  3704. +
  3705. +static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  3706. +{
  3707. + struct usb_request *req = bh->outreq;
  3708. + struct bulk_cb_wrap *cbw = req->buf;
  3709. +
  3710. + /* Was this a real packet? Should it be ignored? */
  3711. + if (req->status || test_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags))
  3712. + return -EINVAL;
  3713. +
  3714. + /* Is the CBW valid? */
  3715. + if (req->actual != US_BULK_CB_WRAP_LEN ||
  3716. + cbw->Signature != cpu_to_le32(
  3717. + US_BULK_CB_SIGN)) {
  3718. + DBG(fsg, "invalid CBW: len %u sig 0x%x\n",
  3719. + req->actual,
  3720. + le32_to_cpu(cbw->Signature));
  3721. +
  3722. + /* The Bulk-only spec says we MUST stall the IN endpoint
  3723. + * (6.6.1), so it's unavoidable. It also says we must
  3724. + * retain this state until the next reset, but there's
  3725. + * no way to tell the controller driver it should ignore
  3726. + * Clear-Feature(HALT) requests.
  3727. + *
  3728. + * We aren't required to halt the OUT endpoint; instead
  3729. + * we can simply accept and discard any data received
  3730. + * until the next reset. */
  3731. + wedge_bulk_in_endpoint(fsg);
  3732. + set_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
  3733. + return -EINVAL;
  3734. + }
  3735. +
  3736. + /* Is the CBW meaningful? */
  3737. + if (cbw->Lun >= FSG_MAX_LUNS || cbw->Flags & ~US_BULK_FLAG_IN ||
  3738. + cbw->Length <= 0 || cbw->Length > MAX_COMMAND_SIZE) {
  3739. + DBG(fsg, "non-meaningful CBW: lun = %u, flags = 0x%x, "
  3740. + "cmdlen %u\n",
  3741. + cbw->Lun, cbw->Flags, cbw->Length);
  3742. +
  3743. + /* We can do anything we want here, so let's stall the
  3744. + * bulk pipes if we are allowed to. */
  3745. + if (mod_data.can_stall) {
  3746. + fsg_set_halt(fsg, fsg->bulk_out);
  3747. + halt_bulk_in_endpoint(fsg);
  3748. + }
  3749. + return -EINVAL;
  3750. + }
  3751. +
  3752. + /* Save the command for later */
  3753. + fsg->cmnd_size = cbw->Length;
  3754. + memcpy(fsg->cmnd, cbw->CDB, fsg->cmnd_size);
  3755. + if (cbw->Flags & US_BULK_FLAG_IN)
  3756. + fsg->data_dir = DATA_DIR_TO_HOST;
  3757. + else
  3758. + fsg->data_dir = DATA_DIR_FROM_HOST;
  3759. + fsg->data_size = le32_to_cpu(cbw->DataTransferLength);
  3760. + if (fsg->data_size == 0)
  3761. + fsg->data_dir = DATA_DIR_NONE;
  3762. + fsg->lun = cbw->Lun;
  3763. + fsg->tag = cbw->Tag;
  3764. + return 0;
  3765. +}
  3766. +
  3767. +
  3768. +static int get_next_command(struct fsg_dev *fsg)
  3769. +{
  3770. + struct fsg_buffhd *bh;
  3771. + int rc = 0;
  3772. +
  3773. + if (transport_is_bbb()) {
  3774. +
  3775. + /* Wait for the next buffer to become available */
  3776. + bh = fsg->next_buffhd_to_fill;
  3777. + while (bh->state != BUF_STATE_EMPTY) {
  3778. + rc = sleep_thread(fsg);
  3779. + if (rc)
  3780. + return rc;
  3781. + }
  3782. +
  3783. + /* Queue a request to read a Bulk-only CBW */
  3784. + set_bulk_out_req_length(fsg, bh, US_BULK_CB_WRAP_LEN);
  3785. + start_transfer(fsg, fsg->bulk_out, bh->outreq,
  3786. + &bh->outreq_busy, &bh->state);
  3787. +
  3788. + /* We will drain the buffer in software, which means we
  3789. + * can reuse it for the next filling. No need to advance
  3790. + * next_buffhd_to_fill. */
  3791. +
  3792. + /* Wait for the CBW to arrive */
  3793. + while (bh->state != BUF_STATE_FULL) {
  3794. + rc = sleep_thread(fsg);
  3795. + if (rc)
  3796. + return rc;
  3797. + }
  3798. + smp_rmb();
  3799. + rc = received_cbw(fsg, bh);
  3800. + bh->state = BUF_STATE_EMPTY;
  3801. +
  3802. + } else { // USB_PR_CB or USB_PR_CBI
  3803. +
  3804. + /* Wait for the next command to arrive */
  3805. + while (fsg->cbbuf_cmnd_size == 0) {
  3806. + rc = sleep_thread(fsg);
  3807. + if (rc)
  3808. + return rc;
  3809. + }
  3810. +
  3811. + /* Is the previous status interrupt request still busy?
  3812. + * The host is allowed to skip reading the status,
  3813. + * so we must cancel it. */
  3814. + if (fsg->intreq_busy)
  3815. + usb_ep_dequeue(fsg->intr_in, fsg->intreq);
  3816. +
  3817. + /* Copy the command and mark the buffer empty */
  3818. + fsg->data_dir = DATA_DIR_UNKNOWN;
  3819. + spin_lock_irq(&fsg->lock);
  3820. + fsg->cmnd_size = fsg->cbbuf_cmnd_size;
  3821. + memcpy(fsg->cmnd, fsg->cbbuf_cmnd, fsg->cmnd_size);
  3822. + fsg->cbbuf_cmnd_size = 0;
  3823. + spin_unlock_irq(&fsg->lock);
  3824. +
  3825. + /* Use LUN from the command */
  3826. + fsg->lun = fsg->cmnd[1] >> 5;
  3827. + }
  3828. +
  3829. + /* Update current lun */
  3830. + if (fsg->lun >= 0 && fsg->lun < fsg->nluns)
  3831. + fsg->curlun = &fsg->luns[fsg->lun];
  3832. + else
  3833. + fsg->curlun = NULL;
  3834. +
  3835. + return rc;
  3836. +}
  3837. +
  3838. +
  3839. +/*-------------------------------------------------------------------------*/
  3840. +
  3841. +static int enable_endpoint(struct fsg_dev *fsg, struct usb_ep *ep,
  3842. + const struct usb_endpoint_descriptor *d)
  3843. +{
  3844. + int rc;
  3845. +
  3846. + ep->driver_data = fsg;
  3847. + ep->desc = d;
  3848. + rc = usb_ep_enable(ep);
  3849. + if (rc)
  3850. + ERROR(fsg, "can't enable %s, result %d\n", ep->name, rc);
  3851. + return rc;
  3852. +}
  3853. +
  3854. +static int alloc_request(struct fsg_dev *fsg, struct usb_ep *ep,
  3855. + struct usb_request **preq)
  3856. +{
  3857. + *preq = usb_ep_alloc_request(ep, GFP_ATOMIC);
  3858. + if (*preq)
  3859. + return 0;
  3860. + ERROR(fsg, "can't allocate request for %s\n", ep->name);
  3861. + return -ENOMEM;
  3862. +}
  3863. +
  3864. +/*
  3865. + * Reset interface setting and re-init endpoint state (toggle etc).
  3866. + * Call with altsetting < 0 to disable the interface. The only other
  3867. + * available altsetting is 0, which enables the interface.
  3868. + */
  3869. +static int do_set_interface(struct fsg_dev *fsg, int altsetting)
  3870. +{
  3871. + int rc = 0;
  3872. + int i;
  3873. + const struct usb_endpoint_descriptor *d;
  3874. +
  3875. + if (fsg->running)
  3876. + DBG(fsg, "reset interface\n");
  3877. +
  3878. +reset:
  3879. + /* Deallocate the requests */
  3880. + for (i = 0; i < fsg_num_buffers; ++i) {
  3881. + struct fsg_buffhd *bh = &fsg->buffhds[i];
  3882. +
  3883. + if (bh->inreq) {
  3884. + usb_ep_free_request(fsg->bulk_in, bh->inreq);
  3885. + bh->inreq = NULL;
  3886. + }
  3887. + if (bh->outreq) {
  3888. + usb_ep_free_request(fsg->bulk_out, bh->outreq);
  3889. + bh->outreq = NULL;
  3890. + }
  3891. + }
  3892. + if (fsg->intreq) {
  3893. + usb_ep_free_request(fsg->intr_in, fsg->intreq);
  3894. + fsg->intreq = NULL;
  3895. + }
  3896. +
  3897. + /* Disable the endpoints */
  3898. + if (fsg->bulk_in_enabled) {
  3899. + usb_ep_disable(fsg->bulk_in);
  3900. + fsg->bulk_in_enabled = 0;
  3901. + }
  3902. + if (fsg->bulk_out_enabled) {
  3903. + usb_ep_disable(fsg->bulk_out);
  3904. + fsg->bulk_out_enabled = 0;
  3905. + }
  3906. + if (fsg->intr_in_enabled) {
  3907. + usb_ep_disable(fsg->intr_in);
  3908. + fsg->intr_in_enabled = 0;
  3909. + }
  3910. +
  3911. + fsg->running = 0;
  3912. + if (altsetting < 0 || rc != 0)
  3913. + return rc;
  3914. +
  3915. + DBG(fsg, "set interface %d\n", altsetting);
  3916. +
  3917. + /* Enable the endpoints */
  3918. + d = fsg_ep_desc(fsg->gadget,
  3919. + &fsg_fs_bulk_in_desc, &fsg_hs_bulk_in_desc,
  3920. + &fsg_ss_bulk_in_desc);
  3921. + if ((rc = enable_endpoint(fsg, fsg->bulk_in, d)) != 0)
  3922. + goto reset;
  3923. + fsg->bulk_in_enabled = 1;
  3924. +
  3925. + d = fsg_ep_desc(fsg->gadget,
  3926. + &fsg_fs_bulk_out_desc, &fsg_hs_bulk_out_desc,
  3927. + &fsg_ss_bulk_out_desc);
  3928. + if ((rc = enable_endpoint(fsg, fsg->bulk_out, d)) != 0)
  3929. + goto reset;
  3930. + fsg->bulk_out_enabled = 1;
  3931. + fsg->bulk_out_maxpacket = usb_endpoint_maxp(d);
  3932. + clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
  3933. +
  3934. + if (transport_is_cbi()) {
  3935. + d = fsg_ep_desc(fsg->gadget,
  3936. + &fsg_fs_intr_in_desc, &fsg_hs_intr_in_desc,
  3937. + &fsg_ss_intr_in_desc);
  3938. + if ((rc = enable_endpoint(fsg, fsg->intr_in, d)) != 0)
  3939. + goto reset;
  3940. + fsg->intr_in_enabled = 1;
  3941. + }
  3942. +
  3943. + /* Allocate the requests */
  3944. + for (i = 0; i < fsg_num_buffers; ++i) {
  3945. + struct fsg_buffhd *bh = &fsg->buffhds[i];
  3946. +
  3947. + if ((rc = alloc_request(fsg, fsg->bulk_in, &bh->inreq)) != 0)
  3948. + goto reset;
  3949. + if ((rc = alloc_request(fsg, fsg->bulk_out, &bh->outreq)) != 0)
  3950. + goto reset;
  3951. + bh->inreq->buf = bh->outreq->buf = bh->buf;
  3952. + bh->inreq->context = bh->outreq->context = bh;
  3953. + bh->inreq->complete = bulk_in_complete;
  3954. + bh->outreq->complete = bulk_out_complete;
  3955. + }
  3956. + if (transport_is_cbi()) {
  3957. + if ((rc = alloc_request(fsg, fsg->intr_in, &fsg->intreq)) != 0)
  3958. + goto reset;
  3959. + fsg->intreq->complete = intr_in_complete;
  3960. + }
  3961. +
  3962. + fsg->running = 1;
  3963. + for (i = 0; i < fsg->nluns; ++i)
  3964. + fsg->luns[i].unit_attention_data = SS_RESET_OCCURRED;
  3965. + return rc;
  3966. +}
  3967. +
  3968. +
  3969. +/*
  3970. + * Change our operational configuration. This code must agree with the code
  3971. + * that returns config descriptors, and with interface altsetting code.
  3972. + *
  3973. + * It's also responsible for power management interactions. Some
  3974. + * configurations might not work with our current power sources.
  3975. + * For now we just assume the gadget is always self-powered.
  3976. + */
  3977. +static int do_set_config(struct fsg_dev *fsg, u8 new_config)
  3978. +{
  3979. + int rc = 0;
  3980. +
  3981. + /* Disable the single interface */
  3982. + if (fsg->config != 0) {
  3983. + DBG(fsg, "reset config\n");
  3984. + fsg->config = 0;
  3985. + rc = do_set_interface(fsg, -1);
  3986. + }
  3987. +
  3988. + /* Enable the interface */
  3989. + if (new_config != 0) {
  3990. + fsg->config = new_config;
  3991. + if ((rc = do_set_interface(fsg, 0)) != 0)
  3992. + fsg->config = 0; // Reset on errors
  3993. + else
  3994. + INFO(fsg, "%s config #%d\n",
  3995. + usb_speed_string(fsg->gadget->speed),
  3996. + fsg->config);
  3997. + }
  3998. + return rc;
  3999. +}
  4000. +
  4001. +
  4002. +/*-------------------------------------------------------------------------*/
  4003. +
  4004. +static void handle_exception(struct fsg_dev *fsg)
  4005. +{
  4006. + siginfo_t info;
  4007. + int sig;
  4008. + int i;
  4009. + int num_active;
  4010. + struct fsg_buffhd *bh;
  4011. + enum fsg_state old_state;
  4012. + u8 new_config;
  4013. + struct fsg_lun *curlun;
  4014. + unsigned int exception_req_tag;
  4015. + int rc;
  4016. +
  4017. + /* Clear the existing signals. Anything but SIGUSR1 is converted
  4018. + * into a high-priority EXIT exception. */
  4019. + for (;;) {
  4020. + sig = dequeue_signal_lock(current, &current->blocked, &info);
  4021. + if (!sig)
  4022. + break;
  4023. + if (sig != SIGUSR1) {
  4024. + if (fsg->state < FSG_STATE_EXIT)
  4025. + DBG(fsg, "Main thread exiting on signal\n");
  4026. + raise_exception(fsg, FSG_STATE_EXIT);
  4027. + }
  4028. + }
  4029. +
  4030. + /* Cancel all the pending transfers */
  4031. + if (fsg->intreq_busy)
  4032. + usb_ep_dequeue(fsg->intr_in, fsg->intreq);
  4033. + for (i = 0; i < fsg_num_buffers; ++i) {
  4034. + bh = &fsg->buffhds[i];
  4035. + if (bh->inreq_busy)
  4036. + usb_ep_dequeue(fsg->bulk_in, bh->inreq);
  4037. + if (bh->outreq_busy)
  4038. + usb_ep_dequeue(fsg->bulk_out, bh->outreq);
  4039. + }
  4040. +
  4041. + /* Wait until everything is idle */
  4042. + for (;;) {
  4043. + num_active = fsg->intreq_busy;
  4044. + for (i = 0; i < fsg_num_buffers; ++i) {
  4045. + bh = &fsg->buffhds[i];
  4046. + num_active += bh->inreq_busy + bh->outreq_busy;
  4047. + }
  4048. + if (num_active == 0)
  4049. + break;
  4050. + if (sleep_thread(fsg))
  4051. + return;
  4052. + }
  4053. +
  4054. + /* Clear out the controller's fifos */
  4055. + if (fsg->bulk_in_enabled)
  4056. + usb_ep_fifo_flush(fsg->bulk_in);
  4057. + if (fsg->bulk_out_enabled)
  4058. + usb_ep_fifo_flush(fsg->bulk_out);
  4059. + if (fsg->intr_in_enabled)
  4060. + usb_ep_fifo_flush(fsg->intr_in);
  4061. +
  4062. + /* Reset the I/O buffer states and pointers, the SCSI
  4063. + * state, and the exception. Then invoke the handler. */
  4064. + spin_lock_irq(&fsg->lock);
  4065. +
  4066. + for (i = 0; i < fsg_num_buffers; ++i) {
  4067. + bh = &fsg->buffhds[i];
  4068. + bh->state = BUF_STATE_EMPTY;
  4069. + }
  4070. + fsg->next_buffhd_to_fill = fsg->next_buffhd_to_drain =
  4071. + &fsg->buffhds[0];
  4072. +
  4073. + exception_req_tag = fsg->exception_req_tag;
  4074. + new_config = fsg->new_config;
  4075. + old_state = fsg->state;
  4076. +
  4077. + if (old_state == FSG_STATE_ABORT_BULK_OUT)
  4078. + fsg->state = FSG_STATE_STATUS_PHASE;
  4079. + else {
  4080. + for (i = 0; i < fsg->nluns; ++i) {
  4081. + curlun = &fsg->luns[i];
  4082. + curlun->prevent_medium_removal = 0;
  4083. + curlun->sense_data = curlun->unit_attention_data =
  4084. + SS_NO_SENSE;
  4085. + curlun->sense_data_info = 0;
  4086. + curlun->info_valid = 0;
  4087. + }
  4088. + fsg->state = FSG_STATE_IDLE;
  4089. + }
  4090. + spin_unlock_irq(&fsg->lock);
  4091. +
  4092. + /* Carry out any extra actions required for the exception */
  4093. + switch (old_state) {
  4094. + default:
  4095. + break;
  4096. +
  4097. + case FSG_STATE_ABORT_BULK_OUT:
  4098. + send_status(fsg);
  4099. + spin_lock_irq(&fsg->lock);
  4100. + if (fsg->state == FSG_STATE_STATUS_PHASE)
  4101. + fsg->state = FSG_STATE_IDLE;
  4102. + spin_unlock_irq(&fsg->lock);
  4103. + break;
  4104. +
  4105. + case FSG_STATE_RESET:
  4106. + /* In case we were forced against our will to halt a
  4107. + * bulk endpoint, clear the halt now. (The SuperH UDC
  4108. + * requires this.) */
  4109. + if (test_and_clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags))
  4110. + usb_ep_clear_halt(fsg->bulk_in);
  4111. +
  4112. + if (transport_is_bbb()) {
  4113. + if (fsg->ep0_req_tag == exception_req_tag)
  4114. + ep0_queue(fsg); // Complete the status stage
  4115. +
  4116. + } else if (transport_is_cbi())
  4117. + send_status(fsg); // Status by interrupt pipe
  4118. +
  4119. + /* Technically this should go here, but it would only be
  4120. + * a waste of time. Ditto for the INTERFACE_CHANGE and
  4121. + * CONFIG_CHANGE cases. */
  4122. + // for (i = 0; i < fsg->nluns; ++i)
  4123. + // fsg->luns[i].unit_attention_data = SS_RESET_OCCURRED;
  4124. + break;
  4125. +
  4126. + case FSG_STATE_INTERFACE_CHANGE:
  4127. + rc = do_set_interface(fsg, 0);
  4128. + if (fsg->ep0_req_tag != exception_req_tag)
  4129. + break;
  4130. + if (rc != 0) // STALL on errors
  4131. + fsg_set_halt(fsg, fsg->ep0);
  4132. + else // Complete the status stage
  4133. + ep0_queue(fsg);
  4134. + break;
  4135. +
  4136. + case FSG_STATE_CONFIG_CHANGE:
  4137. + rc = do_set_config(fsg, new_config);
  4138. + if (fsg->ep0_req_tag != exception_req_tag)
  4139. + break;
  4140. + if (rc != 0) // STALL on errors
  4141. + fsg_set_halt(fsg, fsg->ep0);
  4142. + else // Complete the status stage
  4143. + ep0_queue(fsg);
  4144. + break;
  4145. +
  4146. + case FSG_STATE_DISCONNECT:
  4147. + for (i = 0; i < fsg->nluns; ++i)
  4148. + fsg_lun_fsync_sub(fsg->luns + i);
  4149. + do_set_config(fsg, 0); // Unconfigured state
  4150. + break;
  4151. +
  4152. + case FSG_STATE_EXIT:
  4153. + case FSG_STATE_TERMINATED:
  4154. + do_set_config(fsg, 0); // Free resources
  4155. + spin_lock_irq(&fsg->lock);
  4156. + fsg->state = FSG_STATE_TERMINATED; // Stop the thread
  4157. + spin_unlock_irq(&fsg->lock);
  4158. + break;
  4159. + }
  4160. +}
  4161. +
  4162. +
  4163. +/*-------------------------------------------------------------------------*/
  4164. +
  4165. +static int fsg_main_thread(void *fsg_)
  4166. +{
  4167. + struct fsg_dev *fsg = fsg_;
  4168. +
  4169. + /* Allow the thread to be killed by a signal, but set the signal mask
  4170. + * to block everything but INT, TERM, KILL, and USR1. */
  4171. + allow_signal(SIGINT);
  4172. + allow_signal(SIGTERM);
  4173. + allow_signal(SIGKILL);
  4174. + allow_signal(SIGUSR1);
  4175. +
  4176. + /* Allow the thread to be frozen */
  4177. + set_freezable();
  4178. +
  4179. + /* Arrange for userspace references to be interpreted as kernel
  4180. + * pointers. That way we can pass a kernel pointer to a routine
  4181. + * that expects a __user pointer and it will work okay. */
  4182. + set_fs(get_ds());
  4183. +
  4184. + /* The main loop */
  4185. + while (fsg->state != FSG_STATE_TERMINATED) {
  4186. + if (exception_in_progress(fsg) || signal_pending(current)) {
  4187. + handle_exception(fsg);
  4188. + continue;
  4189. + }
  4190. +
  4191. + if (!fsg->running) {
  4192. + sleep_thread(fsg);
  4193. + continue;
  4194. + }
  4195. +
  4196. + if (get_next_command(fsg))
  4197. + continue;
  4198. +
  4199. + spin_lock_irq(&fsg->lock);
  4200. + if (!exception_in_progress(fsg))
  4201. + fsg->state = FSG_STATE_DATA_PHASE;
  4202. + spin_unlock_irq(&fsg->lock);
  4203. +
  4204. + if (do_scsi_command(fsg) || finish_reply(fsg))
  4205. + continue;
  4206. +
  4207. + spin_lock_irq(&fsg->lock);
  4208. + if (!exception_in_progress(fsg))
  4209. + fsg->state = FSG_STATE_STATUS_PHASE;
  4210. + spin_unlock_irq(&fsg->lock);
  4211. +
  4212. + if (send_status(fsg))
  4213. + continue;
  4214. +
  4215. + spin_lock_irq(&fsg->lock);
  4216. + if (!exception_in_progress(fsg))
  4217. + fsg->state = FSG_STATE_IDLE;
  4218. + spin_unlock_irq(&fsg->lock);
  4219. + }
  4220. +
  4221. + spin_lock_irq(&fsg->lock);
  4222. + fsg->thread_task = NULL;
  4223. + spin_unlock_irq(&fsg->lock);
  4224. +
  4225. + /* If we are exiting because of a signal, unregister the
  4226. + * gadget driver. */
  4227. + if (test_and_clear_bit(REGISTERED, &fsg->atomic_bitflags))
  4228. + usb_gadget_unregister_driver(&fsg_driver);
  4229. +
  4230. + /* Let the unbind and cleanup routines know the thread has exited */
  4231. + kthread_complete_and_exit(&fsg->thread_notifier, 0);
  4232. +}
  4233. +
  4234. +
  4235. +/*-------------------------------------------------------------------------*/
  4236. +
  4237. +
  4238. +/* The write permissions and store_xxx pointers are set in fsg_bind() */
  4239. +static DEVICE_ATTR(ro, 0444, fsg_show_ro, NULL);
  4240. +static DEVICE_ATTR(nofua, 0644, fsg_show_nofua, NULL);
  4241. +static DEVICE_ATTR(file, 0444, fsg_show_file, NULL);
  4242. +
  4243. +
  4244. +/*-------------------------------------------------------------------------*/
  4245. +
  4246. +static void fsg_release(struct kref *ref)
  4247. +{
  4248. + struct fsg_dev *fsg = container_of(ref, struct fsg_dev, ref);
  4249. +
  4250. + kfree(fsg->luns);
  4251. + kfree(fsg);
  4252. +}
  4253. +
  4254. +static void lun_release(struct device *dev)
  4255. +{
  4256. + struct rw_semaphore *filesem = dev_get_drvdata(dev);
  4257. + struct fsg_dev *fsg =
  4258. + container_of(filesem, struct fsg_dev, filesem);
  4259. +
  4260. + kref_put(&fsg->ref, fsg_release);
  4261. +}
  4262. +
  4263. +static void /* __init_or_exit */ fsg_unbind(struct usb_gadget *gadget)
  4264. +{
  4265. + struct fsg_dev *fsg = get_gadget_data(gadget);
  4266. + int i;
  4267. + struct fsg_lun *curlun;
  4268. + struct usb_request *req = fsg->ep0req;
  4269. +
  4270. + DBG(fsg, "unbind\n");
  4271. + clear_bit(REGISTERED, &fsg->atomic_bitflags);
  4272. +
  4273. + /* If the thread isn't already dead, tell it to exit now */
  4274. + if (fsg->state != FSG_STATE_TERMINATED) {
  4275. + raise_exception(fsg, FSG_STATE_EXIT);
  4276. + wait_for_completion(&fsg->thread_notifier);
  4277. +
  4278. + /* The cleanup routine waits for this completion also */
  4279. + complete(&fsg->thread_notifier);
  4280. + }
  4281. +
  4282. + /* Unregister the sysfs attribute files and the LUNs */
  4283. + for (i = 0; i < fsg->nluns; ++i) {
  4284. + curlun = &fsg->luns[i];
  4285. + if (curlun->registered) {
  4286. + device_remove_file(&curlun->dev, &dev_attr_nofua);
  4287. + device_remove_file(&curlun->dev, &dev_attr_ro);
  4288. + device_remove_file(&curlun->dev, &dev_attr_file);
  4289. + fsg_lun_close(curlun);
  4290. + device_unregister(&curlun->dev);
  4291. + curlun->registered = 0;
  4292. + }
  4293. + }
  4294. +
  4295. + /* Free the data buffers */
  4296. + for (i = 0; i < fsg_num_buffers; ++i)
  4297. + kfree(fsg->buffhds[i].buf);
  4298. +
  4299. + /* Free the request and buffer for endpoint 0 */
  4300. + if (req) {
  4301. + kfree(req->buf);
  4302. + usb_ep_free_request(fsg->ep0, req);
  4303. + }
  4304. +
  4305. + set_gadget_data(gadget, NULL);
  4306. +}
  4307. +
  4308. +
  4309. +static int __init check_parameters(struct fsg_dev *fsg)
  4310. +{
  4311. + int prot;
  4312. + int gcnum;
  4313. +
  4314. + /* Store the default values */
  4315. + mod_data.transport_type = USB_PR_BULK;
  4316. + mod_data.transport_name = "Bulk-only";
  4317. + mod_data.protocol_type = USB_SC_SCSI;
  4318. + mod_data.protocol_name = "Transparent SCSI";
  4319. +
  4320. + /* Some peripheral controllers are known not to be able to
  4321. + * halt bulk endpoints correctly. If one of them is present,
  4322. + * disable stalls.
  4323. + */
  4324. + if (gadget_is_at91(fsg->gadget))
  4325. + mod_data.can_stall = 0;
  4326. +
  4327. + if (mod_data.release == 0xffff) { // Parameter wasn't set
  4328. + gcnum = usb_gadget_controller_number(fsg->gadget);
  4329. + if (gcnum >= 0)
  4330. + mod_data.release = 0x0300 + gcnum;
  4331. + else {
  4332. + WARNING(fsg, "controller '%s' not recognized\n",
  4333. + fsg->gadget->name);
  4334. + mod_data.release = 0x0399;
  4335. + }
  4336. + }
  4337. +
  4338. + prot = simple_strtol(mod_data.protocol_parm, NULL, 0);
  4339. +
  4340. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  4341. + if (strnicmp(mod_data.transport_parm, "BBB", 10) == 0) {
  4342. + ; // Use default setting
  4343. + } else if (strnicmp(mod_data.transport_parm, "CB", 10) == 0) {
  4344. + mod_data.transport_type = USB_PR_CB;
  4345. + mod_data.transport_name = "Control-Bulk";
  4346. + } else if (strnicmp(mod_data.transport_parm, "CBI", 10) == 0) {
  4347. + mod_data.transport_type = USB_PR_CBI;
  4348. + mod_data.transport_name = "Control-Bulk-Interrupt";
  4349. + } else {
  4350. + ERROR(fsg, "invalid transport: %s\n", mod_data.transport_parm);
  4351. + return -EINVAL;
  4352. + }
  4353. +
  4354. + if (strnicmp(mod_data.protocol_parm, "SCSI", 10) == 0 ||
  4355. + prot == USB_SC_SCSI) {
  4356. + ; // Use default setting
  4357. + } else if (strnicmp(mod_data.protocol_parm, "RBC", 10) == 0 ||
  4358. + prot == USB_SC_RBC) {
  4359. + mod_data.protocol_type = USB_SC_RBC;
  4360. + mod_data.protocol_name = "RBC";
  4361. + } else if (strnicmp(mod_data.protocol_parm, "8020", 4) == 0 ||
  4362. + strnicmp(mod_data.protocol_parm, "ATAPI", 10) == 0 ||
  4363. + prot == USB_SC_8020) {
  4364. + mod_data.protocol_type = USB_SC_8020;
  4365. + mod_data.protocol_name = "8020i (ATAPI)";
  4366. + } else if (strnicmp(mod_data.protocol_parm, "QIC", 3) == 0 ||
  4367. + prot == USB_SC_QIC) {
  4368. + mod_data.protocol_type = USB_SC_QIC;
  4369. + mod_data.protocol_name = "QIC-157";
  4370. + } else if (strnicmp(mod_data.protocol_parm, "UFI", 10) == 0 ||
  4371. + prot == USB_SC_UFI) {
  4372. + mod_data.protocol_type = USB_SC_UFI;
  4373. + mod_data.protocol_name = "UFI";
  4374. + } else if (strnicmp(mod_data.protocol_parm, "8070", 4) == 0 ||
  4375. + prot == USB_SC_8070) {
  4376. + mod_data.protocol_type = USB_SC_8070;
  4377. + mod_data.protocol_name = "8070i";
  4378. + } else {
  4379. + ERROR(fsg, "invalid protocol: %s\n", mod_data.protocol_parm);
  4380. + return -EINVAL;
  4381. + }
  4382. +
  4383. + mod_data.buflen &= PAGE_CACHE_MASK;
  4384. + if (mod_data.buflen <= 0) {
  4385. + ERROR(fsg, "invalid buflen\n");
  4386. + return -ETOOSMALL;
  4387. + }
  4388. +
  4389. +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
  4390. +
  4391. + /* Serial string handling.
  4392. + * On a real device, the serial string would be loaded
  4393. + * from permanent storage. */
  4394. + if (mod_data.serial) {
  4395. + const char *ch;
  4396. + unsigned len = 0;
  4397. +
  4398. + /* Sanity check :
  4399. + * The CB[I] specification limits the serial string to
  4400. + * 12 uppercase hexadecimal characters.
  4401. + * BBB need at least 12 uppercase hexadecimal characters,
  4402. + * with a maximum of 126. */
  4403. + for (ch = mod_data.serial; *ch; ++ch) {
  4404. + ++len;
  4405. + if ((*ch < '0' || *ch > '9') &&
  4406. + (*ch < 'A' || *ch > 'F')) { /* not uppercase hex */
  4407. + WARNING(fsg,
  4408. + "Invalid serial string character: %c\n",
  4409. + *ch);
  4410. + goto no_serial;
  4411. + }
  4412. + }
  4413. + if (len > 126 ||
  4414. + (mod_data.transport_type == USB_PR_BULK && len < 12) ||
  4415. + (mod_data.transport_type != USB_PR_BULK && len > 12)) {
  4416. + WARNING(fsg, "Invalid serial string length!\n");
  4417. + goto no_serial;
  4418. + }
  4419. + fsg_strings[FSG_STRING_SERIAL - 1].s = mod_data.serial;
  4420. + } else {
  4421. + WARNING(fsg, "No serial-number string provided!\n");
  4422. + no_serial:
  4423. + device_desc.iSerialNumber = 0;
  4424. + }
  4425. +
  4426. + return 0;
  4427. +}
  4428. +
  4429. +
  4430. +static int __init fsg_bind(struct usb_gadget *gadget)
  4431. +{
  4432. + struct fsg_dev *fsg = the_fsg;
  4433. + int rc;
  4434. + int i;
  4435. + struct fsg_lun *curlun;
  4436. + struct usb_ep *ep;
  4437. + struct usb_request *req;
  4438. + char *pathbuf, *p;
  4439. +
  4440. + fsg->gadget = gadget;
  4441. + set_gadget_data(gadget, fsg);
  4442. + fsg->ep0 = gadget->ep0;
  4443. + fsg->ep0->driver_data = fsg;
  4444. +
  4445. + if ((rc = check_parameters(fsg)) != 0)
  4446. + goto out;
  4447. +
  4448. + if (mod_data.removable) { // Enable the store_xxx attributes
  4449. + dev_attr_file.attr.mode = 0644;
  4450. + dev_attr_file.store = fsg_store_file;
  4451. + if (!mod_data.cdrom) {
  4452. + dev_attr_ro.attr.mode = 0644;
  4453. + dev_attr_ro.store = fsg_store_ro;
  4454. + }
  4455. + }
  4456. +
  4457. + /* Only for removable media? */
  4458. + dev_attr_nofua.attr.mode = 0644;
  4459. + dev_attr_nofua.store = fsg_store_nofua;
  4460. +
  4461. + /* Find out how many LUNs there should be */
  4462. + i = mod_data.nluns;
  4463. + if (i == 0)
  4464. + i = max(mod_data.num_filenames, 1u);
  4465. + if (i > FSG_MAX_LUNS) {
  4466. + ERROR(fsg, "invalid number of LUNs: %d\n", i);
  4467. + rc = -EINVAL;
  4468. + goto out;
  4469. + }
  4470. +
  4471. + /* Create the LUNs, open their backing files, and register the
  4472. + * LUN devices in sysfs. */
  4473. + fsg->luns = kzalloc(i * sizeof(struct fsg_lun), GFP_KERNEL);
  4474. + if (!fsg->luns) {
  4475. + rc = -ENOMEM;
  4476. + goto out;
  4477. + }
  4478. + fsg->nluns = i;
  4479. +
  4480. + for (i = 0; i < fsg->nluns; ++i) {
  4481. + curlun = &fsg->luns[i];
  4482. + curlun->cdrom = !!mod_data.cdrom;
  4483. + curlun->ro = mod_data.cdrom || mod_data.ro[i];
  4484. + curlun->initially_ro = curlun->ro;
  4485. + curlun->removable = mod_data.removable;
  4486. + curlun->nofua = mod_data.nofua[i];
  4487. + curlun->dev.release = lun_release;
  4488. + curlun->dev.parent = &gadget->dev;
  4489. + curlun->dev.driver = &fsg_driver.driver;
  4490. + dev_set_drvdata(&curlun->dev, &fsg->filesem);
  4491. + dev_set_name(&curlun->dev,"%s-lun%d",
  4492. + dev_name(&gadget->dev), i);
  4493. +
  4494. + kref_get(&fsg->ref);
  4495. + rc = device_register(&curlun->dev);
  4496. + if (rc) {
  4497. + INFO(fsg, "failed to register LUN%d: %d\n", i, rc);
  4498. + put_device(&curlun->dev);
  4499. + goto out;
  4500. + }
  4501. + curlun->registered = 1;
  4502. +
  4503. + rc = device_create_file(&curlun->dev, &dev_attr_ro);
  4504. + if (rc)
  4505. + goto out;
  4506. + rc = device_create_file(&curlun->dev, &dev_attr_nofua);
  4507. + if (rc)
  4508. + goto out;
  4509. + rc = device_create_file(&curlun->dev, &dev_attr_file);
  4510. + if (rc)
  4511. + goto out;
  4512. +
  4513. + if (mod_data.file[i] && *mod_data.file[i]) {
  4514. + rc = fsg_lun_open(curlun, mod_data.file[i]);
  4515. + if (rc)
  4516. + goto out;
  4517. + } else if (!mod_data.removable) {
  4518. + ERROR(fsg, "no file given for LUN%d\n", i);
  4519. + rc = -EINVAL;
  4520. + goto out;
  4521. + }
  4522. + }
  4523. +
  4524. + /* Find all the endpoints we will use */
  4525. + usb_ep_autoconfig_reset(gadget);
  4526. + ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_in_desc);
  4527. + if (!ep)
  4528. + goto autoconf_fail;
  4529. + ep->driver_data = fsg; // claim the endpoint
  4530. + fsg->bulk_in = ep;
  4531. +
  4532. + ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_out_desc);
  4533. + if (!ep)
  4534. + goto autoconf_fail;
  4535. + ep->driver_data = fsg; // claim the endpoint
  4536. + fsg->bulk_out = ep;
  4537. +
  4538. + if (transport_is_cbi()) {
  4539. + ep = usb_ep_autoconfig(gadget, &fsg_fs_intr_in_desc);
  4540. + if (!ep)
  4541. + goto autoconf_fail;
  4542. + ep->driver_data = fsg; // claim the endpoint
  4543. + fsg->intr_in = ep;
  4544. + }
  4545. +
  4546. + /* Fix up the descriptors */
  4547. + device_desc.idVendor = cpu_to_le16(mod_data.vendor);
  4548. + device_desc.idProduct = cpu_to_le16(mod_data.product);
  4549. + device_desc.bcdDevice = cpu_to_le16(mod_data.release);
  4550. +
  4551. + i = (transport_is_cbi() ? 3 : 2); // Number of endpoints
  4552. + fsg_intf_desc.bNumEndpoints = i;
  4553. + fsg_intf_desc.bInterfaceSubClass = mod_data.protocol_type;
  4554. + fsg_intf_desc.bInterfaceProtocol = mod_data.transport_type;
  4555. + fsg_fs_function[i + FSG_FS_FUNCTION_PRE_EP_ENTRIES] = NULL;
  4556. +
  4557. + if (gadget_is_dualspeed(gadget)) {
  4558. + fsg_hs_function[i + FSG_HS_FUNCTION_PRE_EP_ENTRIES] = NULL;
  4559. +
  4560. + /* Assume endpoint addresses are the same for both speeds */
  4561. + fsg_hs_bulk_in_desc.bEndpointAddress =
  4562. + fsg_fs_bulk_in_desc.bEndpointAddress;
  4563. + fsg_hs_bulk_out_desc.bEndpointAddress =
  4564. + fsg_fs_bulk_out_desc.bEndpointAddress;
  4565. + fsg_hs_intr_in_desc.bEndpointAddress =
  4566. + fsg_fs_intr_in_desc.bEndpointAddress;
  4567. + }
  4568. +
  4569. + if (gadget_is_superspeed(gadget)) {
  4570. + unsigned max_burst;
  4571. +
  4572. + fsg_ss_function[i + FSG_SS_FUNCTION_PRE_EP_ENTRIES] = NULL;
  4573. +
  4574. + /* Calculate bMaxBurst, we know packet size is 1024 */
  4575. + max_burst = min_t(unsigned, mod_data.buflen / 1024, 15);
  4576. +
  4577. + /* Assume endpoint addresses are the same for both speeds */
  4578. + fsg_ss_bulk_in_desc.bEndpointAddress =
  4579. + fsg_fs_bulk_in_desc.bEndpointAddress;
  4580. + fsg_ss_bulk_in_comp_desc.bMaxBurst = max_burst;
  4581. +
  4582. + fsg_ss_bulk_out_desc.bEndpointAddress =
  4583. + fsg_fs_bulk_out_desc.bEndpointAddress;
  4584. + fsg_ss_bulk_out_comp_desc.bMaxBurst = max_burst;
  4585. + }
  4586. +
  4587. + if (gadget_is_otg(gadget))
  4588. + fsg_otg_desc.bmAttributes |= USB_OTG_HNP;
  4589. +
  4590. + rc = -ENOMEM;
  4591. +
  4592. + /* Allocate the request and buffer for endpoint 0 */
  4593. + fsg->ep0req = req = usb_ep_alloc_request(fsg->ep0, GFP_KERNEL);
  4594. + if (!req)
  4595. + goto out;
  4596. + req->buf = kmalloc(EP0_BUFSIZE, GFP_KERNEL);
  4597. + if (!req->buf)
  4598. + goto out;
  4599. + req->complete = ep0_complete;
  4600. +
  4601. + /* Allocate the data buffers */
  4602. + for (i = 0; i < fsg_num_buffers; ++i) {
  4603. + struct fsg_buffhd *bh = &fsg->buffhds[i];
  4604. +
  4605. + /* Allocate for the bulk-in endpoint. We assume that
  4606. + * the buffer will also work with the bulk-out (and
  4607. + * interrupt-in) endpoint. */
  4608. + bh->buf = kmalloc(mod_data.buflen, GFP_KERNEL);
  4609. + if (!bh->buf)
  4610. + goto out;
  4611. + bh->next = bh + 1;
  4612. + }
  4613. + fsg->buffhds[fsg_num_buffers - 1].next = &fsg->buffhds[0];
  4614. +
  4615. + /* This should reflect the actual gadget power source */
  4616. + usb_gadget_set_selfpowered(gadget);
  4617. +
  4618. + snprintf(fsg_string_manufacturer, sizeof fsg_string_manufacturer,
  4619. + "%s %s with %s",
  4620. + init_utsname()->sysname, init_utsname()->release,
  4621. + gadget->name);
  4622. +
  4623. + fsg->thread_task = kthread_create(fsg_main_thread, fsg,
  4624. + "file-storage-gadget");
  4625. + if (IS_ERR(fsg->thread_task)) {
  4626. + rc = PTR_ERR(fsg->thread_task);
  4627. + goto out;
  4628. + }
  4629. +
  4630. + INFO(fsg, DRIVER_DESC ", version: " DRIVER_VERSION "\n");
  4631. + INFO(fsg, "NOTE: This driver is deprecated. "
  4632. + "Consider using g_mass_storage instead.\n");
  4633. + INFO(fsg, "Number of LUNs=%d\n", fsg->nluns);
  4634. +
  4635. + pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
  4636. + for (i = 0; i < fsg->nluns; ++i) {
  4637. + curlun = &fsg->luns[i];
  4638. + if (fsg_lun_is_open(curlun)) {
  4639. + p = NULL;
  4640. + if (pathbuf) {
  4641. + p = d_path(&curlun->filp->f_path,
  4642. + pathbuf, PATH_MAX);
  4643. + if (IS_ERR(p))
  4644. + p = NULL;
  4645. + }
  4646. + LINFO(curlun, "ro=%d, nofua=%d, file: %s\n",
  4647. + curlun->ro, curlun->nofua, (p ? p : "(error)"));
  4648. + }
  4649. + }
  4650. + kfree(pathbuf);
  4651. +
  4652. + DBG(fsg, "transport=%s (x%02x)\n",
  4653. + mod_data.transport_name, mod_data.transport_type);
  4654. + DBG(fsg, "protocol=%s (x%02x)\n",
  4655. + mod_data.protocol_name, mod_data.protocol_type);
  4656. + DBG(fsg, "VendorID=x%04x, ProductID=x%04x, Release=x%04x\n",
  4657. + mod_data.vendor, mod_data.product, mod_data.release);
  4658. + DBG(fsg, "removable=%d, stall=%d, cdrom=%d, buflen=%u\n",
  4659. + mod_data.removable, mod_data.can_stall,
  4660. + mod_data.cdrom, mod_data.buflen);
  4661. + DBG(fsg, "I/O thread pid: %d\n", task_pid_nr(fsg->thread_task));
  4662. +
  4663. + set_bit(REGISTERED, &fsg->atomic_bitflags);
  4664. +
  4665. + /* Tell the thread to start working */
  4666. + wake_up_process(fsg->thread_task);
  4667. + return 0;
  4668. +
  4669. +autoconf_fail:
  4670. + ERROR(fsg, "unable to autoconfigure all endpoints\n");
  4671. + rc = -ENOTSUPP;
  4672. +
  4673. +out:
  4674. + fsg->state = FSG_STATE_TERMINATED; // The thread is dead
  4675. + fsg_unbind(gadget);
  4676. + complete(&fsg->thread_notifier);
  4677. + return rc;
  4678. +}
  4679. +
  4680. +
  4681. +/*-------------------------------------------------------------------------*/
  4682. +
  4683. +static void fsg_suspend(struct usb_gadget *gadget)
  4684. +{
  4685. + struct fsg_dev *fsg = get_gadget_data(gadget);
  4686. +
  4687. + DBG(fsg, "suspend\n");
  4688. + set_bit(SUSPENDED, &fsg->atomic_bitflags);
  4689. +}
  4690. +
  4691. +static void fsg_resume(struct usb_gadget *gadget)
  4692. +{
  4693. + struct fsg_dev *fsg = get_gadget_data(gadget);
  4694. +
  4695. + DBG(fsg, "resume\n");
  4696. + clear_bit(SUSPENDED, &fsg->atomic_bitflags);
  4697. +}
  4698. +
  4699. +
  4700. +/*-------------------------------------------------------------------------*/
  4701. +
  4702. +static struct usb_gadget_driver fsg_driver = {
  4703. + .max_speed = USB_SPEED_SUPER,
  4704. + .function = (char *) fsg_string_product,
  4705. + .unbind = fsg_unbind,
  4706. + .disconnect = fsg_disconnect,
  4707. + .setup = fsg_setup,
  4708. + .suspend = fsg_suspend,
  4709. + .resume = fsg_resume,
  4710. +
  4711. + .driver = {
  4712. + .name = DRIVER_NAME,
  4713. + .owner = THIS_MODULE,
  4714. + // .release = ...
  4715. + // .suspend = ...
  4716. + // .resume = ...
  4717. + },
  4718. +};
  4719. +
  4720. +
  4721. +static int __init fsg_alloc(void)
  4722. +{
  4723. + struct fsg_dev *fsg;
  4724. +
  4725. + fsg = kzalloc(sizeof *fsg +
  4726. + fsg_num_buffers * sizeof *(fsg->buffhds), GFP_KERNEL);
  4727. +
  4728. + if (!fsg)
  4729. + return -ENOMEM;
  4730. + spin_lock_init(&fsg->lock);
  4731. + init_rwsem(&fsg->filesem);
  4732. + kref_init(&fsg->ref);
  4733. + init_completion(&fsg->thread_notifier);
  4734. +
  4735. + the_fsg = fsg;
  4736. + return 0;
  4737. +}
  4738. +
  4739. +
  4740. +static int __init fsg_init(void)
  4741. +{
  4742. + int rc;
  4743. + struct fsg_dev *fsg;
  4744. +
  4745. + rc = fsg_num_buffers_validate();
  4746. + if (rc != 0)
  4747. + return rc;
  4748. +
  4749. + if ((rc = fsg_alloc()) != 0)
  4750. + return rc;
  4751. + fsg = the_fsg;
  4752. + if ((rc = usb_gadget_probe_driver(&fsg_driver, fsg_bind)) != 0)
  4753. + kref_put(&fsg->ref, fsg_release);
  4754. + return rc;
  4755. +}
  4756. +module_init(fsg_init);
  4757. +
  4758. +
  4759. +static void __exit fsg_cleanup(void)
  4760. +{
  4761. + struct fsg_dev *fsg = the_fsg;
  4762. +
  4763. + /* Unregister the driver iff the thread hasn't already done so */
  4764. + if (test_and_clear_bit(REGISTERED, &fsg->atomic_bitflags))
  4765. + usb_gadget_unregister_driver(&fsg_driver);
  4766. +
  4767. + /* Wait for the thread to finish up */
  4768. + wait_for_completion(&fsg->thread_notifier);
  4769. +
  4770. + kref_put(&fsg->ref, fsg_release);
  4771. +}
  4772. +module_exit(fsg_cleanup);
  4773. --- a/drivers/usb/host/Kconfig
  4774. +++ b/drivers/usb/host/Kconfig
  4775. @@ -742,6 +742,16 @@ config USB_RENESAS_USBHS_HCD
  4776. To compile this driver as a module, choose M here: the
  4777. module will be called renesas-usbhs.
  4778. +config USB_DWCOTG
  4779. + bool "Synopsis DWC host support"
  4780. + depends on USB && (FIQ || ARM64)
  4781. + help
  4782. + The Synopsis DWC controller is a dual-role
  4783. + host/peripheral/OTG ("On The Go") USB controllers.
  4784. +
  4785. + Enable this option to support this IP in host controller mode.
  4786. + If unsure, say N.
  4787. +
  4788. config USB_HCD_BCMA
  4789. tristate "BCMA usb host driver"
  4790. depends on BCMA
  4791. --- a/drivers/usb/host/Makefile
  4792. +++ b/drivers/usb/host/Makefile
  4793. @@ -78,6 +78,7 @@ obj-$(CONFIG_USB_SL811_HCD) += sl811-hcd
  4794. obj-$(CONFIG_USB_SL811_CS) += sl811_cs.o
  4795. obj-$(CONFIG_USB_U132_HCD) += u132-hcd.o
  4796. obj-$(CONFIG_USB_R8A66597_HCD) += r8a66597-hcd.o
  4797. +obj-$(CONFIG_USB_DWCOTG) += dwc_otg/ dwc_common_port/
  4798. obj-$(CONFIG_USB_FSL_USB2) += fsl-mph-dr-of.o
  4799. obj-$(CONFIG_USB_EHCI_FSL) += fsl-mph-dr-of.o
  4800. obj-$(CONFIG_USB_EHCI_FSL) += ehci-fsl.o
  4801. --- /dev/null
  4802. +++ b/drivers/usb/host/dwc_common_port/Makefile
  4803. @@ -0,0 +1,58 @@
  4804. +#
  4805. +# Makefile for DWC_common library
  4806. +#
  4807. +
  4808. +ifneq ($(KERNELRELEASE),)
  4809. +
  4810. +ccflags-y += -DDWC_LINUX
  4811. +#ccflags-y += -DDEBUG
  4812. +#ccflags-y += -DDWC_DEBUG_REGS
  4813. +#ccflags-y += -DDWC_DEBUG_MEMORY
  4814. +
  4815. +ccflags-y += -DDWC_LIBMODULE
  4816. +ccflags-y += -DDWC_CCLIB
  4817. +#ccflags-y += -DDWC_CRYPTOLIB
  4818. +ccflags-y += -DDWC_NOTIFYLIB
  4819. +ccflags-y += -DDWC_UTFLIB
  4820. +
  4821. +obj-$(CONFIG_USB_DWCOTG) += dwc_common_port_lib.o
  4822. +dwc_common_port_lib-objs := dwc_cc.o dwc_modpow.o dwc_dh.o \
  4823. + dwc_crypto.o dwc_notifier.o \
  4824. + dwc_common_linux.o dwc_mem.o
  4825. +
  4826. +kernrelwd := $(subst ., ,$(KERNELRELEASE))
  4827. +kernrel3 := $(word 1,$(kernrelwd)).$(word 2,$(kernrelwd)).$(word 3,$(kernrelwd))
  4828. +
  4829. +ifneq ($(kernrel3),2.6.20)
  4830. +# grayg - I only know that we use ccflags-y in 2.6.31 actually
  4831. +ccflags-y += $(CPPFLAGS)
  4832. +endif
  4833. +
  4834. +else
  4835. +
  4836. +#ifeq ($(KDIR),)
  4837. +#$(error Must give "KDIR=/path/to/kernel/source" on command line or in environment)
  4838. +#endif
  4839. +
  4840. +ifeq ($(ARCH),)
  4841. +$(error Must give "ARCH=<arch>" on command line or in environment. Also, if \
  4842. + cross-compiling, must give "CROSS_COMPILE=/path/to/compiler/plus/tool-prefix-")
  4843. +endif
  4844. +
  4845. +ifeq ($(DOXYGEN),)
  4846. +DOXYGEN := doxygen
  4847. +endif
  4848. +
  4849. +default:
  4850. + $(MAKE) -C$(KDIR) M=$(PWD) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
  4851. +
  4852. +docs: $(wildcard *.[hc]) doc/doxygen.cfg
  4853. + $(DOXYGEN) doc/doxygen.cfg
  4854. +
  4855. +tags: $(wildcard *.[hc])
  4856. + $(CTAGS) -e $(wildcard *.[hc]) $(wildcard linux/*.[hc]) $(wildcard $(KDIR)/include/linux/usb*.h)
  4857. +
  4858. +endif
  4859. +
  4860. +clean:
  4861. + rm -rf *.o *.ko .*.cmd *.mod.c .*.o.d .*.o.tmp modules.order Module.markers Module.symvers .tmp_versions/
  4862. --- /dev/null
  4863. +++ b/drivers/usb/host/dwc_common_port/Makefile.fbsd
  4864. @@ -0,0 +1,17 @@
  4865. +CFLAGS += -I/sys/i386/compile/GENERIC -I/sys/i386/include -I/usr/include
  4866. +CFLAGS += -DDWC_FREEBSD
  4867. +CFLAGS += -DDEBUG
  4868. +#CFLAGS += -DDWC_DEBUG_REGS
  4869. +#CFLAGS += -DDWC_DEBUG_MEMORY
  4870. +
  4871. +#CFLAGS += -DDWC_LIBMODULE
  4872. +#CFLAGS += -DDWC_CCLIB
  4873. +#CFLAGS += -DDWC_CRYPTOLIB
  4874. +#CFLAGS += -DDWC_NOTIFYLIB
  4875. +#CFLAGS += -DDWC_UTFLIB
  4876. +
  4877. +KMOD = dwc_common_port_lib
  4878. +SRCS = dwc_cc.c dwc_modpow.c dwc_dh.c dwc_crypto.c dwc_notifier.c \
  4879. + dwc_common_fbsd.c dwc_mem.c
  4880. +
  4881. +.include <bsd.kmod.mk>
  4882. --- /dev/null
  4883. +++ b/drivers/usb/host/dwc_common_port/Makefile.linux
  4884. @@ -0,0 +1,49 @@
  4885. +#
  4886. +# Makefile for DWC_common library
  4887. +#
  4888. +ifneq ($(KERNELRELEASE),)
  4889. +
  4890. +ccflags-y += -DDWC_LINUX
  4891. +#ccflags-y += -DDEBUG
  4892. +#ccflags-y += -DDWC_DEBUG_REGS
  4893. +#ccflags-y += -DDWC_DEBUG_MEMORY
  4894. +
  4895. +ccflags-y += -DDWC_LIBMODULE
  4896. +ccflags-y += -DDWC_CCLIB
  4897. +ccflags-y += -DDWC_CRYPTOLIB
  4898. +ccflags-y += -DDWC_NOTIFYLIB
  4899. +ccflags-y += -DDWC_UTFLIB
  4900. +
  4901. +obj-m := dwc_common_port_lib.o
  4902. +dwc_common_port_lib-objs := dwc_cc.o dwc_modpow.o dwc_dh.o \
  4903. + dwc_crypto.o dwc_notifier.o \
  4904. + dwc_common_linux.o dwc_mem.o
  4905. +
  4906. +else
  4907. +
  4908. +ifeq ($(KDIR),)
  4909. +$(error Must give "KDIR=/path/to/kernel/source" on command line or in environment)
  4910. +endif
  4911. +
  4912. +ifeq ($(ARCH),)
  4913. +$(error Must give "ARCH=<arch>" on command line or in environment. Also, if \
  4914. + cross-compiling, must give "CROSS_COMPILE=/path/to/compiler/plus/tool-prefix-")
  4915. +endif
  4916. +
  4917. +ifeq ($(DOXYGEN),)
  4918. +DOXYGEN := doxygen
  4919. +endif
  4920. +
  4921. +default:
  4922. + $(MAKE) -C$(KDIR) M=$(PWD) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
  4923. +
  4924. +docs: $(wildcard *.[hc]) doc/doxygen.cfg
  4925. + $(DOXYGEN) doc/doxygen.cfg
  4926. +
  4927. +tags: $(wildcard *.[hc])
  4928. + $(CTAGS) -e $(wildcard *.[hc]) $(wildcard linux/*.[hc]) $(wildcard $(KDIR)/include/linux/usb*.h)
  4929. +
  4930. +endif
  4931. +
  4932. +clean:
  4933. + rm -rf *.o *.ko .*.cmd *.mod.c .*.o.d .*.o.tmp modules.order Module.markers Module.symvers .tmp_versions/
  4934. --- /dev/null
  4935. +++ b/drivers/usb/host/dwc_common_port/changes.txt
  4936. @@ -0,0 +1,174 @@
  4937. +
  4938. +dwc_read_reg32() and friends now take an additional parameter, a pointer to an
  4939. +IO context struct. The IO context struct should live in an os-dependent struct
  4940. +in your driver. As an example, the dwc_usb3 driver has an os-dependent struct
  4941. +named 'os_dep' embedded in the main device struct. So there these calls look
  4942. +like this:
  4943. +
  4944. + dwc_read_reg32(&usb3_dev->os_dep.ioctx, &pcd->dev_global_regs->dcfg);
  4945. +
  4946. + dwc_write_reg32(&usb3_dev->os_dep.ioctx,
  4947. + &pcd->dev_global_regs->dcfg, 0);
  4948. +
  4949. +Note that for the existing Linux driver ports, it is not necessary to actually
  4950. +define the 'ioctx' member in the os-dependent struct. Since Linux does not
  4951. +require an IO context, its macros for dwc_read_reg32() and friends do not
  4952. +use the context pointer, so it is optimized away by the compiler. But it is
  4953. +necessary to add the pointer parameter to all of the call sites, to be ready
  4954. +for any future ports (such as FreeBSD) which do require an IO context.
  4955. +
  4956. +
  4957. +Similarly, dwc_alloc(), dwc_alloc_atomic(), dwc_strdup(), and dwc_free() now
  4958. +take an additional parameter, a pointer to a memory context. Examples:
  4959. +
  4960. + addr = dwc_alloc(&usb3_dev->os_dep.memctx, size);
  4961. +
  4962. + dwc_free(&usb3_dev->os_dep.memctx, addr);
  4963. +
  4964. +Again, for the Linux ports, it is not necessary to actually define the memctx
  4965. +member, but it is necessary to add the pointer parameter to all of the call
  4966. +sites.
  4967. +
  4968. +
  4969. +Same for dwc_dma_alloc() and dwc_dma_free(). Examples:
  4970. +
  4971. + virt_addr = dwc_dma_alloc(&usb3_dev->os_dep.dmactx, size, &phys_addr);
  4972. +
  4973. + dwc_dma_free(&usb3_dev->os_dep.dmactx, size, virt_addr, phys_addr);
  4974. +
  4975. +
  4976. +Same for dwc_mutex_alloc() and dwc_mutex_free(). Examples:
  4977. +
  4978. + mutex = dwc_mutex_alloc(&usb3_dev->os_dep.mtxctx);
  4979. +
  4980. + dwc_mutex_free(&usb3_dev->os_dep.mtxctx, mutex);
  4981. +
  4982. +
  4983. +Same for dwc_spinlock_alloc() and dwc_spinlock_free(). Examples:
  4984. +
  4985. + lock = dwc_spinlock_alloc(&usb3_dev->osdep.splctx);
  4986. +
  4987. + dwc_spinlock_free(&usb3_dev->osdep.splctx, lock);
  4988. +
  4989. +
  4990. +Same for dwc_timer_alloc(). Example:
  4991. +
  4992. + timer = dwc_timer_alloc(&usb3_dev->os_dep.tmrctx, "dwc_usb3_tmr1",
  4993. + cb_func, cb_data);
  4994. +
  4995. +
  4996. +Same for dwc_waitq_alloc(). Example:
  4997. +
  4998. + waitq = dwc_waitq_alloc(&usb3_dev->os_dep.wtqctx);
  4999. +
  5000. +
  5001. +Same for dwc_thread_run(). Example:
  5002. +
  5003. + thread = dwc_thread_run(&usb3_dev->os_dep.thdctx, func,
  5004. + "dwc_usb3_thd1", data);
  5005. +
  5006. +
  5007. +Same for dwc_workq_alloc(). Example:
  5008. +
  5009. + workq = dwc_workq_alloc(&usb3_dev->osdep.wkqctx, "dwc_usb3_wkq1");
  5010. +
  5011. +
  5012. +Same for dwc_task_alloc(). Example:
  5013. +
  5014. + task = dwc_task_alloc(&usb3_dev->os_dep.tskctx, "dwc_usb3_tsk1",
  5015. + cb_func, cb_data);
  5016. +
  5017. +
  5018. +In addition to the context pointer additions, a few core functions have had
  5019. +other changes made to their parameters:
  5020. +
  5021. +The 'flags' parameter to dwc_spinlock_irqsave() and dwc_spinunlock_irqrestore()
  5022. +has been changed from a uint64_t to a dwc_irqflags_t.
  5023. +
  5024. +dwc_thread_should_stop() now takes a 'dwc_thread_t *' parameter, because the
  5025. +FreeBSD equivalent of that function requires it.
  5026. +
  5027. +And, in addition to the context pointer, dwc_task_alloc() also adds a
  5028. +'char *name' parameter, to be consistent with dwc_thread_run() and
  5029. +dwc_workq_alloc(), and because the FreeBSD equivalent of that function
  5030. +requires a unique name.
  5031. +
  5032. +
  5033. +Here is a complete list of the core functions that now take a pointer to a
  5034. +context as their first parameter:
  5035. +
  5036. + dwc_read_reg32
  5037. + dwc_read_reg64
  5038. + dwc_write_reg32
  5039. + dwc_write_reg64
  5040. + dwc_modify_reg32
  5041. + dwc_modify_reg64
  5042. + dwc_alloc
  5043. + dwc_alloc_atomic
  5044. + dwc_strdup
  5045. + dwc_free
  5046. + dwc_dma_alloc
  5047. + dwc_dma_free
  5048. + dwc_mutex_alloc
  5049. + dwc_mutex_free
  5050. + dwc_spinlock_alloc
  5051. + dwc_spinlock_free
  5052. + dwc_timer_alloc
  5053. + dwc_waitq_alloc
  5054. + dwc_thread_run
  5055. + dwc_workq_alloc
  5056. + dwc_task_alloc Also adds a 'char *name' as its 2nd parameter
  5057. +
  5058. +And here are the core functions that have other changes to their parameters:
  5059. +
  5060. + dwc_spinlock_irqsave 'flags' param is now a 'dwc_irqflags_t *'
  5061. + dwc_spinunlock_irqrestore 'flags' param is now a 'dwc_irqflags_t'
  5062. + dwc_thread_should_stop Adds a 'dwc_thread_t *' parameter
  5063. +
  5064. +
  5065. +
  5066. +The changes to the core functions also require some of the other library
  5067. +functions to change:
  5068. +
  5069. + dwc_cc_if_alloc() and dwc_cc_if_free() now take a 'void *memctx'
  5070. + (for memory allocation) as the 1st param and a 'void *mtxctx'
  5071. + (for mutex allocation) as the 2nd param.
  5072. +
  5073. + dwc_cc_clear(), dwc_cc_add(), dwc_cc_change(), dwc_cc_remove(),
  5074. + dwc_cc_data_for_save(), and dwc_cc_restore_from_data() now take a
  5075. + 'void *memctx' as the 1st param.
  5076. +
  5077. + dwc_dh_modpow(), dwc_dh_pk(), and dwc_dh_derive_keys() now take a
  5078. + 'void *memctx' as the 1st param.
  5079. +
  5080. + dwc_modpow() now takes a 'void *memctx' as the 1st param.
  5081. +
  5082. + dwc_alloc_notification_manager() now takes a 'void *memctx' as the
  5083. + 1st param and a 'void *wkqctx' (for work queue allocation) as the 2nd
  5084. + param, and also now returns an integer value that is non-zero if
  5085. + allocation of its data structures or work queue fails.
  5086. +
  5087. + dwc_register_notifier() now takes a 'void *memctx' as the 1st param.
  5088. +
  5089. + dwc_memory_debug_start() now takes a 'void *mem_ctx' as the first
  5090. + param, and also now returns an integer value that is non-zero if
  5091. + allocation of its data structures fails.
  5092. +
  5093. +
  5094. +
  5095. +Other miscellaneous changes:
  5096. +
  5097. +The DEBUG_MEMORY and DEBUG_REGS #define's have been renamed to
  5098. +DWC_DEBUG_MEMORY and DWC_DEBUG_REGS.
  5099. +
  5100. +The following #define's have been added to allow selectively compiling library
  5101. +features:
  5102. +
  5103. + DWC_CCLIB
  5104. + DWC_CRYPTOLIB
  5105. + DWC_NOTIFYLIB
  5106. + DWC_UTFLIB
  5107. +
  5108. +A DWC_LIBMODULE #define has also been added. If this is not defined, then the
  5109. +module code in dwc_common_linux.c is not compiled in. This allows linking the
  5110. +library code directly into a driver module, instead of as a standalone module.
  5111. --- /dev/null
  5112. +++ b/drivers/usb/host/dwc_common_port/doc/doxygen.cfg
  5113. @@ -0,0 +1,270 @@
  5114. +# Doxyfile 1.4.5
  5115. +
  5116. +#---------------------------------------------------------------------------
  5117. +# Project related configuration options
  5118. +#---------------------------------------------------------------------------
  5119. +PROJECT_NAME = "Synopsys DWC Portability and Common Library for UWB"
  5120. +PROJECT_NUMBER =
  5121. +OUTPUT_DIRECTORY = doc
  5122. +CREATE_SUBDIRS = NO
  5123. +OUTPUT_LANGUAGE = English
  5124. +BRIEF_MEMBER_DESC = YES
  5125. +REPEAT_BRIEF = YES
  5126. +ABBREVIATE_BRIEF = "The $name class" \
  5127. + "The $name widget" \
  5128. + "The $name file" \
  5129. + is \
  5130. + provides \
  5131. + specifies \
  5132. + contains \
  5133. + represents \
  5134. + a \
  5135. + an \
  5136. + the
  5137. +ALWAYS_DETAILED_SEC = YES
  5138. +INLINE_INHERITED_MEMB = NO
  5139. +FULL_PATH_NAMES = NO
  5140. +STRIP_FROM_PATH = ..
  5141. +STRIP_FROM_INC_PATH =
  5142. +SHORT_NAMES = NO
  5143. +JAVADOC_AUTOBRIEF = YES
  5144. +MULTILINE_CPP_IS_BRIEF = NO
  5145. +DETAILS_AT_TOP = YES
  5146. +INHERIT_DOCS = YES
  5147. +SEPARATE_MEMBER_PAGES = NO
  5148. +TAB_SIZE = 8
  5149. +ALIASES =
  5150. +OPTIMIZE_OUTPUT_FOR_C = YES
  5151. +OPTIMIZE_OUTPUT_JAVA = NO
  5152. +BUILTIN_STL_SUPPORT = NO
  5153. +DISTRIBUTE_GROUP_DOC = NO
  5154. +SUBGROUPING = NO
  5155. +#---------------------------------------------------------------------------
  5156. +# Build related configuration options
  5157. +#---------------------------------------------------------------------------
  5158. +EXTRACT_ALL = NO
  5159. +EXTRACT_PRIVATE = NO
  5160. +EXTRACT_STATIC = YES
  5161. +EXTRACT_LOCAL_CLASSES = NO
  5162. +EXTRACT_LOCAL_METHODS = NO
  5163. +HIDE_UNDOC_MEMBERS = NO
  5164. +HIDE_UNDOC_CLASSES = NO
  5165. +HIDE_FRIEND_COMPOUNDS = NO
  5166. +HIDE_IN_BODY_DOCS = NO
  5167. +INTERNAL_DOCS = NO
  5168. +CASE_SENSE_NAMES = YES
  5169. +HIDE_SCOPE_NAMES = NO
  5170. +SHOW_INCLUDE_FILES = NO
  5171. +INLINE_INFO = YES
  5172. +SORT_MEMBER_DOCS = NO
  5173. +SORT_BRIEF_DOCS = NO
  5174. +SORT_BY_SCOPE_NAME = NO
  5175. +GENERATE_TODOLIST = YES
  5176. +GENERATE_TESTLIST = YES
  5177. +GENERATE_BUGLIST = YES
  5178. +GENERATE_DEPRECATEDLIST= YES
  5179. +ENABLED_SECTIONS =
  5180. +MAX_INITIALIZER_LINES = 30
  5181. +SHOW_USED_FILES = YES
  5182. +SHOW_DIRECTORIES = YES
  5183. +FILE_VERSION_FILTER =
  5184. +#---------------------------------------------------------------------------
  5185. +# configuration options related to warning and progress messages
  5186. +#---------------------------------------------------------------------------
  5187. +QUIET = YES
  5188. +WARNINGS = YES
  5189. +WARN_IF_UNDOCUMENTED = NO
  5190. +WARN_IF_DOC_ERROR = YES
  5191. +WARN_NO_PARAMDOC = YES
  5192. +WARN_FORMAT = "$file:$line: $text"
  5193. +WARN_LOGFILE =
  5194. +#---------------------------------------------------------------------------
  5195. +# configuration options related to the input files
  5196. +#---------------------------------------------------------------------------
  5197. +INPUT = .
  5198. +FILE_PATTERNS = *.c \
  5199. + *.cc \
  5200. + *.cxx \
  5201. + *.cpp \
  5202. + *.c++ \
  5203. + *.d \
  5204. + *.java \
  5205. + *.ii \
  5206. + *.ixx \
  5207. + *.ipp \
  5208. + *.i++ \
  5209. + *.inl \
  5210. + *.h \
  5211. + *.hh \
  5212. + *.hxx \
  5213. + *.hpp \
  5214. + *.h++ \
  5215. + *.idl \
  5216. + *.odl \
  5217. + *.cs \
  5218. + *.php \
  5219. + *.php3 \
  5220. + *.inc \
  5221. + *.m \
  5222. + *.mm \
  5223. + *.dox \
  5224. + *.py \
  5225. + *.C \
  5226. + *.CC \
  5227. + *.C++ \
  5228. + *.II \
  5229. + *.I++ \
  5230. + *.H \
  5231. + *.HH \
  5232. + *.H++ \
  5233. + *.CS \
  5234. + *.PHP \
  5235. + *.PHP3 \
  5236. + *.M \
  5237. + *.MM \
  5238. + *.PY
  5239. +RECURSIVE = NO
  5240. +EXCLUDE =
  5241. +EXCLUDE_SYMLINKS = NO
  5242. +EXCLUDE_PATTERNS =
  5243. +EXAMPLE_PATH =
  5244. +EXAMPLE_PATTERNS = *
  5245. +EXAMPLE_RECURSIVE = NO
  5246. +IMAGE_PATH =
  5247. +INPUT_FILTER =
  5248. +FILTER_PATTERNS =
  5249. +FILTER_SOURCE_FILES = NO
  5250. +#---------------------------------------------------------------------------
  5251. +# configuration options related to source browsing
  5252. +#---------------------------------------------------------------------------
  5253. +SOURCE_BROWSER = NO
  5254. +INLINE_SOURCES = NO
  5255. +STRIP_CODE_COMMENTS = YES
  5256. +REFERENCED_BY_RELATION = YES
  5257. +REFERENCES_RELATION = YES
  5258. +USE_HTAGS = NO
  5259. +VERBATIM_HEADERS = NO
  5260. +#---------------------------------------------------------------------------
  5261. +# configuration options related to the alphabetical class index
  5262. +#---------------------------------------------------------------------------
  5263. +ALPHABETICAL_INDEX = NO
  5264. +COLS_IN_ALPHA_INDEX = 5
  5265. +IGNORE_PREFIX =
  5266. +#---------------------------------------------------------------------------
  5267. +# configuration options related to the HTML output
  5268. +#---------------------------------------------------------------------------
  5269. +GENERATE_HTML = YES
  5270. +HTML_OUTPUT = html
  5271. +HTML_FILE_EXTENSION = .html
  5272. +HTML_HEADER =
  5273. +HTML_FOOTER =
  5274. +HTML_STYLESHEET =
  5275. +HTML_ALIGN_MEMBERS = YES
  5276. +GENERATE_HTMLHELP = NO
  5277. +CHM_FILE =
  5278. +HHC_LOCATION =
  5279. +GENERATE_CHI = NO
  5280. +BINARY_TOC = NO
  5281. +TOC_EXPAND = NO
  5282. +DISABLE_INDEX = NO
  5283. +ENUM_VALUES_PER_LINE = 4
  5284. +GENERATE_TREEVIEW = YES
  5285. +TREEVIEW_WIDTH = 250
  5286. +#---------------------------------------------------------------------------
  5287. +# configuration options related to the LaTeX output
  5288. +#---------------------------------------------------------------------------
  5289. +GENERATE_LATEX = NO
  5290. +LATEX_OUTPUT = latex
  5291. +LATEX_CMD_NAME = latex
  5292. +MAKEINDEX_CMD_NAME = makeindex
  5293. +COMPACT_LATEX = NO
  5294. +PAPER_TYPE = a4wide
  5295. +EXTRA_PACKAGES =
  5296. +LATEX_HEADER =
  5297. +PDF_HYPERLINKS = NO
  5298. +USE_PDFLATEX = NO
  5299. +LATEX_BATCHMODE = NO
  5300. +LATEX_HIDE_INDICES = NO
  5301. +#---------------------------------------------------------------------------
  5302. +# configuration options related to the RTF output
  5303. +#---------------------------------------------------------------------------
  5304. +GENERATE_RTF = NO
  5305. +RTF_OUTPUT = rtf
  5306. +COMPACT_RTF = NO
  5307. +RTF_HYPERLINKS = NO
  5308. +RTF_STYLESHEET_FILE =
  5309. +RTF_EXTENSIONS_FILE =
  5310. +#---------------------------------------------------------------------------
  5311. +# configuration options related to the man page output
  5312. +#---------------------------------------------------------------------------
  5313. +GENERATE_MAN = NO
  5314. +MAN_OUTPUT = man
  5315. +MAN_EXTENSION = .3
  5316. +MAN_LINKS = NO
  5317. +#---------------------------------------------------------------------------
  5318. +# configuration options related to the XML output
  5319. +#---------------------------------------------------------------------------
  5320. +GENERATE_XML = NO
  5321. +XML_OUTPUT = xml
  5322. +XML_SCHEMA =
  5323. +XML_DTD =
  5324. +XML_PROGRAMLISTING = YES
  5325. +#---------------------------------------------------------------------------
  5326. +# configuration options for the AutoGen Definitions output
  5327. +#---------------------------------------------------------------------------
  5328. +GENERATE_AUTOGEN_DEF = NO
  5329. +#---------------------------------------------------------------------------
  5330. +# configuration options related to the Perl module output
  5331. +#---------------------------------------------------------------------------
  5332. +GENERATE_PERLMOD = NO
  5333. +PERLMOD_LATEX = NO
  5334. +PERLMOD_PRETTY = YES
  5335. +PERLMOD_MAKEVAR_PREFIX =
  5336. +#---------------------------------------------------------------------------
  5337. +# Configuration options related to the preprocessor
  5338. +#---------------------------------------------------------------------------
  5339. +ENABLE_PREPROCESSING = YES
  5340. +MACRO_EXPANSION = NO
  5341. +EXPAND_ONLY_PREDEF = NO
  5342. +SEARCH_INCLUDES = YES
  5343. +INCLUDE_PATH =
  5344. +INCLUDE_FILE_PATTERNS =
  5345. +PREDEFINED = DEBUG DEBUG_MEMORY
  5346. +EXPAND_AS_DEFINED =
  5347. +SKIP_FUNCTION_MACROS = YES
  5348. +#---------------------------------------------------------------------------
  5349. +# Configuration::additions related to external references
  5350. +#---------------------------------------------------------------------------
  5351. +TAGFILES =
  5352. +GENERATE_TAGFILE =
  5353. +ALLEXTERNALS = NO
  5354. +EXTERNAL_GROUPS = YES
  5355. +PERL_PATH = /usr/bin/perl
  5356. +#---------------------------------------------------------------------------
  5357. +# Configuration options related to the dot tool
  5358. +#---------------------------------------------------------------------------
  5359. +CLASS_DIAGRAMS = YES
  5360. +HIDE_UNDOC_RELATIONS = YES
  5361. +HAVE_DOT = NO
  5362. +CLASS_GRAPH = YES
  5363. +COLLABORATION_GRAPH = YES
  5364. +GROUP_GRAPHS = YES
  5365. +UML_LOOK = NO
  5366. +TEMPLATE_RELATIONS = NO
  5367. +INCLUDE_GRAPH = NO
  5368. +INCLUDED_BY_GRAPH = YES
  5369. +CALL_GRAPH = NO
  5370. +GRAPHICAL_HIERARCHY = YES
  5371. +DIRECTORY_GRAPH = YES
  5372. +DOT_IMAGE_FORMAT = png
  5373. +DOT_PATH =
  5374. +DOTFILE_DIRS =
  5375. +MAX_DOT_GRAPH_DEPTH = 1000
  5376. +DOT_TRANSPARENT = NO
  5377. +DOT_MULTI_TARGETS = NO
  5378. +GENERATE_LEGEND = YES
  5379. +DOT_CLEANUP = YES
  5380. +#---------------------------------------------------------------------------
  5381. +# Configuration::additions related to the search engine
  5382. +#---------------------------------------------------------------------------
  5383. +SEARCHENGINE = NO
  5384. --- /dev/null
  5385. +++ b/drivers/usb/host/dwc_common_port/dwc_cc.c
  5386. @@ -0,0 +1,532 @@
  5387. +/* =========================================================================
  5388. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_cc.c $
  5389. + * $Revision: #4 $
  5390. + * $Date: 2010/11/04 $
  5391. + * $Change: 1621692 $
  5392. + *
  5393. + * Synopsys Portability Library Software and documentation
  5394. + * (hereinafter, "Software") is an Unsupported proprietary work of
  5395. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  5396. + * between Synopsys and you.
  5397. + *
  5398. + * The Software IS NOT an item of Licensed Software or Licensed Product
  5399. + * under any End User Software License Agreement or Agreement for
  5400. + * Licensed Product with Synopsys or any supplement thereto. You are
  5401. + * permitted to use and redistribute this Software in source and binary
  5402. + * forms, with or without modification, provided that redistributions
  5403. + * of source code must retain this notice. You may not view, use,
  5404. + * disclose, copy or distribute this file or any information contained
  5405. + * herein except pursuant to this license grant from Synopsys. If you
  5406. + * do not agree with this notice, including the disclaimer below, then
  5407. + * you are not authorized to use the Software.
  5408. + *
  5409. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  5410. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5411. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  5412. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  5413. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  5414. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  5415. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  5416. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  5417. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  5418. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  5419. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  5420. + * DAMAGE.
  5421. + * ========================================================================= */
  5422. +#ifdef DWC_CCLIB
  5423. +
  5424. +#include "dwc_cc.h"
  5425. +
  5426. +typedef struct dwc_cc
  5427. +{
  5428. + uint32_t uid;
  5429. + uint8_t chid[16];
  5430. + uint8_t cdid[16];
  5431. + uint8_t ck[16];
  5432. + uint8_t *name;
  5433. + uint8_t length;
  5434. + DWC_CIRCLEQ_ENTRY(dwc_cc) list_entry;
  5435. +} dwc_cc_t;
  5436. +
  5437. +DWC_CIRCLEQ_HEAD(context_list, dwc_cc);
  5438. +
  5439. +/** The main structure for CC management. */
  5440. +struct dwc_cc_if
  5441. +{
  5442. + dwc_mutex_t *mutex;
  5443. + char *filename;
  5444. +
  5445. + unsigned is_host:1;
  5446. +
  5447. + dwc_notifier_t *notifier;
  5448. +
  5449. + struct context_list list;
  5450. +};
  5451. +
  5452. +#ifdef DEBUG
  5453. +static inline void dump_bytes(char *name, uint8_t *bytes, int len)
  5454. +{
  5455. + int i;
  5456. + DWC_PRINTF("%s: ", name);
  5457. + for (i=0; i<len; i++) {
  5458. + DWC_PRINTF("%02x ", bytes[i]);
  5459. + }
  5460. + DWC_PRINTF("\n");
  5461. +}
  5462. +#else
  5463. +#define dump_bytes(x...)
  5464. +#endif
  5465. +
  5466. +static dwc_cc_t *alloc_cc(void *mem_ctx, uint8_t *name, uint32_t length)
  5467. +{
  5468. + dwc_cc_t *cc = dwc_alloc(mem_ctx, sizeof(dwc_cc_t));
  5469. + if (!cc) {
  5470. + return NULL;
  5471. + }
  5472. + DWC_MEMSET(cc, 0, sizeof(dwc_cc_t));
  5473. +
  5474. + if (name) {
  5475. + cc->length = length;
  5476. + cc->name = dwc_alloc(mem_ctx, length);
  5477. + if (!cc->name) {
  5478. + dwc_free(mem_ctx, cc);
  5479. + return NULL;
  5480. + }
  5481. +
  5482. + DWC_MEMCPY(cc->name, name, length);
  5483. + }
  5484. +
  5485. + return cc;
  5486. +}
  5487. +
  5488. +static void free_cc(void *mem_ctx, dwc_cc_t *cc)
  5489. +{
  5490. + if (cc->name) {
  5491. + dwc_free(mem_ctx, cc->name);
  5492. + }
  5493. + dwc_free(mem_ctx, cc);
  5494. +}
  5495. +
  5496. +static uint32_t next_uid(dwc_cc_if_t *cc_if)
  5497. +{
  5498. + uint32_t uid = 0;
  5499. + dwc_cc_t *cc;
  5500. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  5501. + if (cc->uid > uid) {
  5502. + uid = cc->uid;
  5503. + }
  5504. + }
  5505. +
  5506. + if (uid == 0) {
  5507. + uid = 255;
  5508. + }
  5509. +
  5510. + return uid + 1;
  5511. +}
  5512. +
  5513. +static dwc_cc_t *cc_find(dwc_cc_if_t *cc_if, uint32_t uid)
  5514. +{
  5515. + dwc_cc_t *cc;
  5516. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  5517. + if (cc->uid == uid) {
  5518. + return cc;
  5519. + }
  5520. + }
  5521. + return NULL;
  5522. +}
  5523. +
  5524. +static unsigned int cc_data_size(dwc_cc_if_t *cc_if)
  5525. +{
  5526. + unsigned int size = 0;
  5527. + dwc_cc_t *cc;
  5528. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  5529. + size += (48 + 1);
  5530. + if (cc->name) {
  5531. + size += cc->length;
  5532. + }
  5533. + }
  5534. + return size;
  5535. +}
  5536. +
  5537. +static uint32_t cc_match_chid(dwc_cc_if_t *cc_if, uint8_t *chid)
  5538. +{
  5539. + uint32_t uid = 0;
  5540. + dwc_cc_t *cc;
  5541. +
  5542. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  5543. + if (DWC_MEMCMP(cc->chid, chid, 16) == 0) {
  5544. + uid = cc->uid;
  5545. + break;
  5546. + }
  5547. + }
  5548. + return uid;
  5549. +}
  5550. +static uint32_t cc_match_cdid(dwc_cc_if_t *cc_if, uint8_t *cdid)
  5551. +{
  5552. + uint32_t uid = 0;
  5553. + dwc_cc_t *cc;
  5554. +
  5555. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  5556. + if (DWC_MEMCMP(cc->cdid, cdid, 16) == 0) {
  5557. + uid = cc->uid;
  5558. + break;
  5559. + }
  5560. + }
  5561. + return uid;
  5562. +}
  5563. +
  5564. +/* Internal cc_add */
  5565. +static int32_t cc_add(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *chid,
  5566. + uint8_t *cdid, uint8_t *ck, uint8_t *name, uint8_t length)
  5567. +{
  5568. + dwc_cc_t *cc;
  5569. + uint32_t uid;
  5570. +
  5571. + if (cc_if->is_host) {
  5572. + uid = cc_match_cdid(cc_if, cdid);
  5573. + }
  5574. + else {
  5575. + uid = cc_match_chid(cc_if, chid);
  5576. + }
  5577. +
  5578. + if (uid) {
  5579. + DWC_DEBUGC("Replacing previous connection context id=%d name=%p name_len=%d", uid, name, length);
  5580. + cc = cc_find(cc_if, uid);
  5581. + }
  5582. + else {
  5583. + cc = alloc_cc(mem_ctx, name, length);
  5584. + cc->uid = next_uid(cc_if);
  5585. + DWC_CIRCLEQ_INSERT_TAIL(&cc_if->list, cc, list_entry);
  5586. + }
  5587. +
  5588. + DWC_MEMCPY(&(cc->chid[0]), chid, 16);
  5589. + DWC_MEMCPY(&(cc->cdid[0]), cdid, 16);
  5590. + DWC_MEMCPY(&(cc->ck[0]), ck, 16);
  5591. +
  5592. + DWC_DEBUGC("Added connection context id=%d name=%p name_len=%d", cc->uid, name, length);
  5593. + dump_bytes("CHID", cc->chid, 16);
  5594. + dump_bytes("CDID", cc->cdid, 16);
  5595. + dump_bytes("CK", cc->ck, 16);
  5596. + return cc->uid;
  5597. +}
  5598. +
  5599. +/* Internal cc_clear */
  5600. +static void cc_clear(void *mem_ctx, dwc_cc_if_t *cc_if)
  5601. +{
  5602. + while (!DWC_CIRCLEQ_EMPTY(&cc_if->list)) {
  5603. + dwc_cc_t *cc = DWC_CIRCLEQ_FIRST(&cc_if->list);
  5604. + DWC_CIRCLEQ_REMOVE_INIT(&cc_if->list, cc, list_entry);
  5605. + free_cc(mem_ctx, cc);
  5606. + }
  5607. +}
  5608. +
  5609. +dwc_cc_if_t *dwc_cc_if_alloc(void *mem_ctx, void *mtx_ctx,
  5610. + dwc_notifier_t *notifier, unsigned is_host)
  5611. +{
  5612. + dwc_cc_if_t *cc_if = NULL;
  5613. +
  5614. + /* Allocate a common_cc_if structure */
  5615. + cc_if = dwc_alloc(mem_ctx, sizeof(dwc_cc_if_t));
  5616. +
  5617. + if (!cc_if)
  5618. + return NULL;
  5619. +
  5620. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
  5621. + DWC_MUTEX_ALLOC_LINUX_DEBUG(cc_if->mutex);
  5622. +#else
  5623. + cc_if->mutex = dwc_mutex_alloc(mtx_ctx);
  5624. +#endif
  5625. + if (!cc_if->mutex) {
  5626. + dwc_free(mem_ctx, cc_if);
  5627. + return NULL;
  5628. + }
  5629. +
  5630. + DWC_CIRCLEQ_INIT(&cc_if->list);
  5631. + cc_if->is_host = is_host;
  5632. + cc_if->notifier = notifier;
  5633. + return cc_if;
  5634. +}
  5635. +
  5636. +void dwc_cc_if_free(void *mem_ctx, void *mtx_ctx, dwc_cc_if_t *cc_if)
  5637. +{
  5638. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
  5639. + DWC_MUTEX_FREE(cc_if->mutex);
  5640. +#else
  5641. + dwc_mutex_free(mtx_ctx, cc_if->mutex);
  5642. +#endif
  5643. + cc_clear(mem_ctx, cc_if);
  5644. + dwc_free(mem_ctx, cc_if);
  5645. +}
  5646. +
  5647. +static void cc_changed(dwc_cc_if_t *cc_if)
  5648. +{
  5649. + if (cc_if->notifier) {
  5650. + dwc_notify(cc_if->notifier, DWC_CC_LIST_CHANGED_NOTIFICATION, cc_if);
  5651. + }
  5652. +}
  5653. +
  5654. +void dwc_cc_clear(void *mem_ctx, dwc_cc_if_t *cc_if)
  5655. +{
  5656. + DWC_MUTEX_LOCK(cc_if->mutex);
  5657. + cc_clear(mem_ctx, cc_if);
  5658. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5659. + cc_changed(cc_if);
  5660. +}
  5661. +
  5662. +int32_t dwc_cc_add(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *chid,
  5663. + uint8_t *cdid, uint8_t *ck, uint8_t *name, uint8_t length)
  5664. +{
  5665. + uint32_t uid;
  5666. +
  5667. + DWC_MUTEX_LOCK(cc_if->mutex);
  5668. + uid = cc_add(mem_ctx, cc_if, chid, cdid, ck, name, length);
  5669. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5670. + cc_changed(cc_if);
  5671. +
  5672. + return uid;
  5673. +}
  5674. +
  5675. +void dwc_cc_change(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id, uint8_t *chid,
  5676. + uint8_t *cdid, uint8_t *ck, uint8_t *name, uint8_t length)
  5677. +{
  5678. + dwc_cc_t* cc;
  5679. +
  5680. + DWC_DEBUGC("Change connection context %d", id);
  5681. +
  5682. + DWC_MUTEX_LOCK(cc_if->mutex);
  5683. + cc = cc_find(cc_if, id);
  5684. + if (!cc) {
  5685. + DWC_ERROR("Uid %d not found in cc list\n", id);
  5686. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5687. + return;
  5688. + }
  5689. +
  5690. + if (chid) {
  5691. + DWC_MEMCPY(&(cc->chid[0]), chid, 16);
  5692. + }
  5693. + if (cdid) {
  5694. + DWC_MEMCPY(&(cc->cdid[0]), cdid, 16);
  5695. + }
  5696. + if (ck) {
  5697. + DWC_MEMCPY(&(cc->ck[0]), ck, 16);
  5698. + }
  5699. +
  5700. + if (name) {
  5701. + if (cc->name) {
  5702. + dwc_free(mem_ctx, cc->name);
  5703. + }
  5704. + cc->name = dwc_alloc(mem_ctx, length);
  5705. + if (!cc->name) {
  5706. + DWC_ERROR("Out of memory in dwc_cc_change()\n");
  5707. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5708. + return;
  5709. + }
  5710. + cc->length = length;
  5711. + DWC_MEMCPY(cc->name, name, length);
  5712. + }
  5713. +
  5714. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5715. +
  5716. + cc_changed(cc_if);
  5717. +
  5718. + DWC_DEBUGC("Changed connection context id=%d\n", id);
  5719. + dump_bytes("New CHID", cc->chid, 16);
  5720. + dump_bytes("New CDID", cc->cdid, 16);
  5721. + dump_bytes("New CK", cc->ck, 16);
  5722. +}
  5723. +
  5724. +void dwc_cc_remove(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id)
  5725. +{
  5726. + dwc_cc_t *cc;
  5727. +
  5728. + DWC_DEBUGC("Removing connection context %d", id);
  5729. +
  5730. + DWC_MUTEX_LOCK(cc_if->mutex);
  5731. + cc = cc_find(cc_if, id);
  5732. + if (!cc) {
  5733. + DWC_ERROR("Uid %d not found in cc list\n", id);
  5734. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5735. + return;
  5736. + }
  5737. +
  5738. + DWC_CIRCLEQ_REMOVE_INIT(&cc_if->list, cc, list_entry);
  5739. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5740. + free_cc(mem_ctx, cc);
  5741. +
  5742. + cc_changed(cc_if);
  5743. +}
  5744. +
  5745. +uint8_t *dwc_cc_data_for_save(void *mem_ctx, dwc_cc_if_t *cc_if, unsigned int *length)
  5746. +{
  5747. + uint8_t *buf, *x;
  5748. + uint8_t zero = 0;
  5749. + dwc_cc_t *cc;
  5750. +
  5751. + DWC_MUTEX_LOCK(cc_if->mutex);
  5752. + *length = cc_data_size(cc_if);
  5753. + if (!(*length)) {
  5754. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5755. + return NULL;
  5756. + }
  5757. +
  5758. + DWC_DEBUGC("Creating data for saving (length=%d)", *length);
  5759. +
  5760. + buf = dwc_alloc(mem_ctx, *length);
  5761. + if (!buf) {
  5762. + *length = 0;
  5763. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5764. + return NULL;
  5765. + }
  5766. +
  5767. + x = buf;
  5768. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  5769. + DWC_MEMCPY(x, cc->chid, 16);
  5770. + x += 16;
  5771. + DWC_MEMCPY(x, cc->cdid, 16);
  5772. + x += 16;
  5773. + DWC_MEMCPY(x, cc->ck, 16);
  5774. + x += 16;
  5775. + if (cc->name) {
  5776. + DWC_MEMCPY(x, &cc->length, 1);
  5777. + x += 1;
  5778. + DWC_MEMCPY(x, cc->name, cc->length);
  5779. + x += cc->length;
  5780. + }
  5781. + else {
  5782. + DWC_MEMCPY(x, &zero, 1);
  5783. + x += 1;
  5784. + }
  5785. + }
  5786. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5787. +
  5788. + return buf;
  5789. +}
  5790. +
  5791. +void dwc_cc_restore_from_data(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *data, uint32_t length)
  5792. +{
  5793. + uint8_t name_length;
  5794. + uint8_t *name;
  5795. + uint8_t *chid;
  5796. + uint8_t *cdid;
  5797. + uint8_t *ck;
  5798. + uint32_t i = 0;
  5799. +
  5800. + DWC_MUTEX_LOCK(cc_if->mutex);
  5801. + cc_clear(mem_ctx, cc_if);
  5802. +
  5803. + while (i < length) {
  5804. + chid = &data[i];
  5805. + i += 16;
  5806. + cdid = &data[i];
  5807. + i += 16;
  5808. + ck = &data[i];
  5809. + i += 16;
  5810. +
  5811. + name_length = data[i];
  5812. + i ++;
  5813. +
  5814. + if (name_length) {
  5815. + name = &data[i];
  5816. + i += name_length;
  5817. + }
  5818. + else {
  5819. + name = NULL;
  5820. + }
  5821. +
  5822. + /* check to see if we haven't overflown the buffer */
  5823. + if (i > length) {
  5824. + DWC_ERROR("Data format error while attempting to load CCs "
  5825. + "(nlen=%d, iter=%d, buflen=%d).\n", name_length, i, length);
  5826. + break;
  5827. + }
  5828. +
  5829. + cc_add(mem_ctx, cc_if, chid, cdid, ck, name, name_length);
  5830. + }
  5831. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5832. +
  5833. + cc_changed(cc_if);
  5834. +}
  5835. +
  5836. +uint32_t dwc_cc_match_chid(dwc_cc_if_t *cc_if, uint8_t *chid)
  5837. +{
  5838. + uint32_t uid = 0;
  5839. +
  5840. + DWC_MUTEX_LOCK(cc_if->mutex);
  5841. + uid = cc_match_chid(cc_if, chid);
  5842. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5843. + return uid;
  5844. +}
  5845. +uint32_t dwc_cc_match_cdid(dwc_cc_if_t *cc_if, uint8_t *cdid)
  5846. +{
  5847. + uint32_t uid = 0;
  5848. +
  5849. + DWC_MUTEX_LOCK(cc_if->mutex);
  5850. + uid = cc_match_cdid(cc_if, cdid);
  5851. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5852. + return uid;
  5853. +}
  5854. +
  5855. +uint8_t *dwc_cc_ck(dwc_cc_if_t *cc_if, int32_t id)
  5856. +{
  5857. + uint8_t *ck = NULL;
  5858. + dwc_cc_t *cc;
  5859. +
  5860. + DWC_MUTEX_LOCK(cc_if->mutex);
  5861. + cc = cc_find(cc_if, id);
  5862. + if (cc) {
  5863. + ck = cc->ck;
  5864. + }
  5865. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5866. +
  5867. + return ck;
  5868. +
  5869. +}
  5870. +
  5871. +uint8_t *dwc_cc_chid(dwc_cc_if_t *cc_if, int32_t id)
  5872. +{
  5873. + uint8_t *retval = NULL;
  5874. + dwc_cc_t *cc;
  5875. +
  5876. + DWC_MUTEX_LOCK(cc_if->mutex);
  5877. + cc = cc_find(cc_if, id);
  5878. + if (cc) {
  5879. + retval = cc->chid;
  5880. + }
  5881. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5882. +
  5883. + return retval;
  5884. +}
  5885. +
  5886. +uint8_t *dwc_cc_cdid(dwc_cc_if_t *cc_if, int32_t id)
  5887. +{
  5888. + uint8_t *retval = NULL;
  5889. + dwc_cc_t *cc;
  5890. +
  5891. + DWC_MUTEX_LOCK(cc_if->mutex);
  5892. + cc = cc_find(cc_if, id);
  5893. + if (cc) {
  5894. + retval = cc->cdid;
  5895. + }
  5896. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5897. +
  5898. + return retval;
  5899. +}
  5900. +
  5901. +uint8_t *dwc_cc_name(dwc_cc_if_t *cc_if, int32_t id, uint8_t *length)
  5902. +{
  5903. + uint8_t *retval = NULL;
  5904. + dwc_cc_t *cc;
  5905. +
  5906. + DWC_MUTEX_LOCK(cc_if->mutex);
  5907. + *length = 0;
  5908. + cc = cc_find(cc_if, id);
  5909. + if (cc) {
  5910. + *length = cc->length;
  5911. + retval = cc->name;
  5912. + }
  5913. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5914. +
  5915. + return retval;
  5916. +}
  5917. +
  5918. +#endif /* DWC_CCLIB */
  5919. --- /dev/null
  5920. +++ b/drivers/usb/host/dwc_common_port/dwc_cc.h
  5921. @@ -0,0 +1,224 @@
  5922. +/* =========================================================================
  5923. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_cc.h $
  5924. + * $Revision: #4 $
  5925. + * $Date: 2010/09/28 $
  5926. + * $Change: 1596182 $
  5927. + *
  5928. + * Synopsys Portability Library Software and documentation
  5929. + * (hereinafter, "Software") is an Unsupported proprietary work of
  5930. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  5931. + * between Synopsys and you.
  5932. + *
  5933. + * The Software IS NOT an item of Licensed Software or Licensed Product
  5934. + * under any End User Software License Agreement or Agreement for
  5935. + * Licensed Product with Synopsys or any supplement thereto. You are
  5936. + * permitted to use and redistribute this Software in source and binary
  5937. + * forms, with or without modification, provided that redistributions
  5938. + * of source code must retain this notice. You may not view, use,
  5939. + * disclose, copy or distribute this file or any information contained
  5940. + * herein except pursuant to this license grant from Synopsys. If you
  5941. + * do not agree with this notice, including the disclaimer below, then
  5942. + * you are not authorized to use the Software.
  5943. + *
  5944. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  5945. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5946. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  5947. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  5948. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  5949. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  5950. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  5951. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  5952. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  5953. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  5954. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  5955. + * DAMAGE.
  5956. + * ========================================================================= */
  5957. +#ifndef _DWC_CC_H_
  5958. +#define _DWC_CC_H_
  5959. +
  5960. +#ifdef __cplusplus
  5961. +extern "C" {
  5962. +#endif
  5963. +
  5964. +/** @file
  5965. + *
  5966. + * This file defines the Context Context library.
  5967. + *
  5968. + * The main data structure is dwc_cc_if_t which is returned by either the
  5969. + * dwc_cc_if_alloc function or returned by the module to the user via a provided
  5970. + * function. The data structure is opaque and should only be manipulated via the
  5971. + * functions provied in this API.
  5972. + *
  5973. + * It manages a list of connection contexts and operations can be performed to
  5974. + * add, remove, query, search, and change, those contexts. Additionally,
  5975. + * a dwc_notifier_t object can be requested from the manager so that
  5976. + * the user can be notified whenever the context list has changed.
  5977. + */
  5978. +
  5979. +#include "dwc_os.h"
  5980. +#include "dwc_list.h"
  5981. +#include "dwc_notifier.h"
  5982. +
  5983. +
  5984. +/* Notifications */
  5985. +#define DWC_CC_LIST_CHANGED_NOTIFICATION "DWC_CC_LIST_CHANGED_NOTIFICATION"
  5986. +
  5987. +struct dwc_cc_if;
  5988. +typedef struct dwc_cc_if dwc_cc_if_t;
  5989. +
  5990. +
  5991. +/** @name Connection Context Operations */
  5992. +/** @{ */
  5993. +
  5994. +/** This function allocates memory for a dwc_cc_if_t structure, initializes
  5995. + * fields to default values, and returns a pointer to the structure or NULL on
  5996. + * error. */
  5997. +extern dwc_cc_if_t *dwc_cc_if_alloc(void *mem_ctx, void *mtx_ctx,
  5998. + dwc_notifier_t *notifier, unsigned is_host);
  5999. +
  6000. +/** Frees the memory for the specified CC structure allocated from
  6001. + * dwc_cc_if_alloc(). */
  6002. +extern void dwc_cc_if_free(void *mem_ctx, void *mtx_ctx, dwc_cc_if_t *cc_if);
  6003. +
  6004. +/** Removes all contexts from the connection context list */
  6005. +extern void dwc_cc_clear(void *mem_ctx, dwc_cc_if_t *cc_if);
  6006. +
  6007. +/** Adds a connection context (CHID, CK, CDID, Name) to the connection context list.
  6008. + * If a CHID already exists, the CK and name are overwritten. Statistics are
  6009. + * not overwritten.
  6010. + *
  6011. + * @param cc_if The cc_if structure.
  6012. + * @param chid A pointer to the 16-byte CHID. This value will be copied.
  6013. + * @param ck A pointer to the 16-byte CK. This value will be copied.
  6014. + * @param cdid A pointer to the 16-byte CDID. This value will be copied.
  6015. + * @param name An optional host friendly name as defined in the association model
  6016. + * spec. Must be a UTF16-LE unicode string. Can be NULL to indicated no name.
  6017. + * @param length The length othe unicode string.
  6018. + * @return A unique identifier used to refer to this context that is valid for
  6019. + * as long as this context is still in the list. */
  6020. +extern int32_t dwc_cc_add(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *chid,
  6021. + uint8_t *cdid, uint8_t *ck, uint8_t *name,
  6022. + uint8_t length);
  6023. +
  6024. +/** Changes the CHID, CK, CDID, or Name values of a connection context in the
  6025. + * list, preserving any accumulated statistics. This would typically be called
  6026. + * if the host decideds to change the context with a SET_CONNECTION request.
  6027. + *
  6028. + * @param cc_if The cc_if structure.
  6029. + * @param id The identifier of the connection context.
  6030. + * @param chid A pointer to the 16-byte CHID. This value will be copied. NULL
  6031. + * indicates no change.
  6032. + * @param cdid A pointer to the 16-byte CDID. This value will be copied. NULL
  6033. + * indicates no change.
  6034. + * @param ck A pointer to the 16-byte CK. This value will be copied. NULL
  6035. + * indicates no change.
  6036. + * @param name Host friendly name UTF16-LE. NULL indicates no change.
  6037. + * @param length Length of name. */
  6038. +extern void dwc_cc_change(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id,
  6039. + uint8_t *chid, uint8_t *cdid, uint8_t *ck,
  6040. + uint8_t *name, uint8_t length);
  6041. +
  6042. +/** Remove the specified connection context.
  6043. + * @param cc_if The cc_if structure.
  6044. + * @param id The identifier of the connection context to remove. */
  6045. +extern void dwc_cc_remove(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id);
  6046. +
  6047. +/** Get a binary block of data for the connection context list and attributes.
  6048. + * This data can be used by the OS specific driver to save the connection
  6049. + * context list into non-volatile memory.
  6050. + *
  6051. + * @param cc_if The cc_if structure.
  6052. + * @param length Return the length of the data buffer.
  6053. + * @return A pointer to the data buffer. The memory for this buffer should be
  6054. + * freed with DWC_FREE() after use. */
  6055. +extern uint8_t *dwc_cc_data_for_save(void *mem_ctx, dwc_cc_if_t *cc_if,
  6056. + unsigned int *length);
  6057. +
  6058. +/** Restore the connection context list from the binary data that was previously
  6059. + * returned from a call to dwc_cc_data_for_save. This can be used by the OS specific
  6060. + * driver to load a connection context list from non-volatile memory.
  6061. + *
  6062. + * @param cc_if The cc_if structure.
  6063. + * @param data The data bytes as returned from dwc_cc_data_for_save.
  6064. + * @param length The length of the data. */
  6065. +extern void dwc_cc_restore_from_data(void *mem_ctx, dwc_cc_if_t *cc_if,
  6066. + uint8_t *data, unsigned int length);
  6067. +
  6068. +/** Find the connection context from the specified CHID.
  6069. + *
  6070. + * @param cc_if The cc_if structure.
  6071. + * @param chid A pointer to the CHID data.
  6072. + * @return A non-zero identifier of the connection context if the CHID matches.
  6073. + * Otherwise returns 0. */
  6074. +extern uint32_t dwc_cc_match_chid(dwc_cc_if_t *cc_if, uint8_t *chid);
  6075. +
  6076. +/** Find the connection context from the specified CDID.
  6077. + *
  6078. + * @param cc_if The cc_if structure.
  6079. + * @param cdid A pointer to the CDID data.
  6080. + * @return A non-zero identifier of the connection context if the CHID matches.
  6081. + * Otherwise returns 0. */
  6082. +extern uint32_t dwc_cc_match_cdid(dwc_cc_if_t *cc_if, uint8_t *cdid);
  6083. +
  6084. +/** Retrieve the CK from the specified connection context.
  6085. + *
  6086. + * @param cc_if The cc_if structure.
  6087. + * @param id The identifier of the connection context.
  6088. + * @return A pointer to the CK data. The memory does not need to be freed. */
  6089. +extern uint8_t *dwc_cc_ck(dwc_cc_if_t *cc_if, int32_t id);
  6090. +
  6091. +/** Retrieve the CHID from the specified connection context.
  6092. + *
  6093. + * @param cc_if The cc_if structure.
  6094. + * @param id The identifier of the connection context.
  6095. + * @return A pointer to the CHID data. The memory does not need to be freed. */
  6096. +extern uint8_t *dwc_cc_chid(dwc_cc_if_t *cc_if, int32_t id);
  6097. +
  6098. +/** Retrieve the CDID from the specified connection context.
  6099. + *
  6100. + * @param cc_if The cc_if structure.
  6101. + * @param id The identifier of the connection context.
  6102. + * @return A pointer to the CDID data. The memory does not need to be freed. */
  6103. +extern uint8_t *dwc_cc_cdid(dwc_cc_if_t *cc_if, int32_t id);
  6104. +
  6105. +extern uint8_t *dwc_cc_name(dwc_cc_if_t *cc_if, int32_t id, uint8_t *length);
  6106. +
  6107. +/** Checks a buffer for non-zero.
  6108. + * @param id A pointer to a 16 byte buffer.
  6109. + * @return true if the 16 byte value is non-zero. */
  6110. +static inline unsigned dwc_assoc_is_not_zero_id(uint8_t *id) {
  6111. + int i;
  6112. + for (i=0; i<16; i++) {
  6113. + if (id[i]) return 1;
  6114. + }
  6115. + return 0;
  6116. +}
  6117. +
  6118. +/** Checks a buffer for zero.
  6119. + * @param id A pointer to a 16 byte buffer.
  6120. + * @return true if the 16 byte value is zero. */
  6121. +static inline unsigned dwc_assoc_is_zero_id(uint8_t *id) {
  6122. + return !dwc_assoc_is_not_zero_id(id);
  6123. +}
  6124. +
  6125. +/** Prints an ASCII representation for the 16-byte chid, cdid, or ck, into
  6126. + * buffer. */
  6127. +static inline int dwc_print_id_string(char *buffer, uint8_t *id) {
  6128. + char *ptr = buffer;
  6129. + int i;
  6130. + for (i=0; i<16; i++) {
  6131. + ptr += DWC_SPRINTF(ptr, "%02x", id[i]);
  6132. + if (i < 15) {
  6133. + ptr += DWC_SPRINTF(ptr, " ");
  6134. + }
  6135. + }
  6136. + return ptr - buffer;
  6137. +}
  6138. +
  6139. +/** @} */
  6140. +
  6141. +#ifdef __cplusplus
  6142. +}
  6143. +#endif
  6144. +
  6145. +#endif /* _DWC_CC_H_ */
  6146. --- /dev/null
  6147. +++ b/drivers/usb/host/dwc_common_port/dwc_common_fbsd.c
  6148. @@ -0,0 +1,1308 @@
  6149. +#include "dwc_os.h"
  6150. +#include "dwc_list.h"
  6151. +
  6152. +#ifdef DWC_CCLIB
  6153. +# include "dwc_cc.h"
  6154. +#endif
  6155. +
  6156. +#ifdef DWC_CRYPTOLIB
  6157. +# include "dwc_modpow.h"
  6158. +# include "dwc_dh.h"
  6159. +# include "dwc_crypto.h"
  6160. +#endif
  6161. +
  6162. +#ifdef DWC_NOTIFYLIB
  6163. +# include "dwc_notifier.h"
  6164. +#endif
  6165. +
  6166. +/* OS-Level Implementations */
  6167. +
  6168. +/* This is the FreeBSD 7.0 kernel implementation of the DWC platform library. */
  6169. +
  6170. +
  6171. +/* MISC */
  6172. +
  6173. +void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size)
  6174. +{
  6175. + return memset(dest, byte, size);
  6176. +}
  6177. +
  6178. +void *DWC_MEMCPY(void *dest, void const *src, uint32_t size)
  6179. +{
  6180. + return memcpy(dest, src, size);
  6181. +}
  6182. +
  6183. +void *DWC_MEMMOVE(void *dest, void *src, uint32_t size)
  6184. +{
  6185. + bcopy(src, dest, size);
  6186. + return dest;
  6187. +}
  6188. +
  6189. +int DWC_MEMCMP(void *m1, void *m2, uint32_t size)
  6190. +{
  6191. + return memcmp(m1, m2, size);
  6192. +}
  6193. +
  6194. +int DWC_STRNCMP(void *s1, void *s2, uint32_t size)
  6195. +{
  6196. + return strncmp(s1, s2, size);
  6197. +}
  6198. +
  6199. +int DWC_STRCMP(void *s1, void *s2)
  6200. +{
  6201. + return strcmp(s1, s2);
  6202. +}
  6203. +
  6204. +int DWC_STRLEN(char const *str)
  6205. +{
  6206. + return strlen(str);
  6207. +}
  6208. +
  6209. +char *DWC_STRCPY(char *to, char const *from)
  6210. +{
  6211. + return strcpy(to, from);
  6212. +}
  6213. +
  6214. +char *DWC_STRDUP(char const *str)
  6215. +{
  6216. + int len = DWC_STRLEN(str) + 1;
  6217. + char *new = DWC_ALLOC_ATOMIC(len);
  6218. +
  6219. + if (!new) {
  6220. + return NULL;
  6221. + }
  6222. +
  6223. + DWC_MEMCPY(new, str, len);
  6224. + return new;
  6225. +}
  6226. +
  6227. +int DWC_ATOI(char *str, int32_t *value)
  6228. +{
  6229. + char *end = NULL;
  6230. +
  6231. + *value = strtol(str, &end, 0);
  6232. + if (*end == '\0') {
  6233. + return 0;
  6234. + }
  6235. +
  6236. + return -1;
  6237. +}
  6238. +
  6239. +int DWC_ATOUI(char *str, uint32_t *value)
  6240. +{
  6241. + char *end = NULL;
  6242. +
  6243. + *value = strtoul(str, &end, 0);
  6244. + if (*end == '\0') {
  6245. + return 0;
  6246. + }
  6247. +
  6248. + return -1;
  6249. +}
  6250. +
  6251. +
  6252. +#ifdef DWC_UTFLIB
  6253. +/* From usbstring.c */
  6254. +
  6255. +int DWC_UTF8_TO_UTF16LE(uint8_t const *s, uint16_t *cp, unsigned len)
  6256. +{
  6257. + int count = 0;
  6258. + u8 c;
  6259. + u16 uchar;
  6260. +
  6261. + /* this insists on correct encodings, though not minimal ones.
  6262. + * BUT it currently rejects legit 4-byte UTF-8 code points,
  6263. + * which need surrogate pairs. (Unicode 3.1 can use them.)
  6264. + */
  6265. + while (len != 0 && (c = (u8) *s++) != 0) {
  6266. + if (unlikely(c & 0x80)) {
  6267. + // 2-byte sequence:
  6268. + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
  6269. + if ((c & 0xe0) == 0xc0) {
  6270. + uchar = (c & 0x1f) << 6;
  6271. +
  6272. + c = (u8) *s++;
  6273. + if ((c & 0xc0) != 0xc0)
  6274. + goto fail;
  6275. + c &= 0x3f;
  6276. + uchar |= c;
  6277. +
  6278. + // 3-byte sequence (most CJKV characters):
  6279. + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
  6280. + } else if ((c & 0xf0) == 0xe0) {
  6281. + uchar = (c & 0x0f) << 12;
  6282. +
  6283. + c = (u8) *s++;
  6284. + if ((c & 0xc0) != 0xc0)
  6285. + goto fail;
  6286. + c &= 0x3f;
  6287. + uchar |= c << 6;
  6288. +
  6289. + c = (u8) *s++;
  6290. + if ((c & 0xc0) != 0xc0)
  6291. + goto fail;
  6292. + c &= 0x3f;
  6293. + uchar |= c;
  6294. +
  6295. + /* no bogus surrogates */
  6296. + if (0xd800 <= uchar && uchar <= 0xdfff)
  6297. + goto fail;
  6298. +
  6299. + // 4-byte sequence (surrogate pairs, currently rare):
  6300. + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
  6301. + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
  6302. + // (uuuuu = wwww + 1)
  6303. + // FIXME accept the surrogate code points (only)
  6304. + } else
  6305. + goto fail;
  6306. + } else
  6307. + uchar = c;
  6308. + put_unaligned (cpu_to_le16 (uchar), cp++);
  6309. + count++;
  6310. + len--;
  6311. + }
  6312. + return count;
  6313. +fail:
  6314. + return -1;
  6315. +}
  6316. +
  6317. +#endif /* DWC_UTFLIB */
  6318. +
  6319. +
  6320. +/* dwc_debug.h */
  6321. +
  6322. +dwc_bool_t DWC_IN_IRQ(void)
  6323. +{
  6324. +// return in_irq();
  6325. + return 0;
  6326. +}
  6327. +
  6328. +dwc_bool_t DWC_IN_BH(void)
  6329. +{
  6330. +// return in_softirq();
  6331. + return 0;
  6332. +}
  6333. +
  6334. +void DWC_VPRINTF(char *format, va_list args)
  6335. +{
  6336. + vprintf(format, args);
  6337. +}
  6338. +
  6339. +int DWC_VSNPRINTF(char *str, int size, char *format, va_list args)
  6340. +{
  6341. + return vsnprintf(str, size, format, args);
  6342. +}
  6343. +
  6344. +void DWC_PRINTF(char *format, ...)
  6345. +{
  6346. + va_list args;
  6347. +
  6348. + va_start(args, format);
  6349. + DWC_VPRINTF(format, args);
  6350. + va_end(args);
  6351. +}
  6352. +
  6353. +int DWC_SPRINTF(char *buffer, char *format, ...)
  6354. +{
  6355. + int retval;
  6356. + va_list args;
  6357. +
  6358. + va_start(args, format);
  6359. + retval = vsprintf(buffer, format, args);
  6360. + va_end(args);
  6361. + return retval;
  6362. +}
  6363. +
  6364. +int DWC_SNPRINTF(char *buffer, int size, char *format, ...)
  6365. +{
  6366. + int retval;
  6367. + va_list args;
  6368. +
  6369. + va_start(args, format);
  6370. + retval = vsnprintf(buffer, size, format, args);
  6371. + va_end(args);
  6372. + return retval;
  6373. +}
  6374. +
  6375. +void __DWC_WARN(char *format, ...)
  6376. +{
  6377. + va_list args;
  6378. +
  6379. + va_start(args, format);
  6380. + DWC_VPRINTF(format, args);
  6381. + va_end(args);
  6382. +}
  6383. +
  6384. +void __DWC_ERROR(char *format, ...)
  6385. +{
  6386. + va_list args;
  6387. +
  6388. + va_start(args, format);
  6389. + DWC_VPRINTF(format, args);
  6390. + va_end(args);
  6391. +}
  6392. +
  6393. +void DWC_EXCEPTION(char *format, ...)
  6394. +{
  6395. + va_list args;
  6396. +
  6397. + va_start(args, format);
  6398. + DWC_VPRINTF(format, args);
  6399. + va_end(args);
  6400. +// BUG_ON(1); ???
  6401. +}
  6402. +
  6403. +#ifdef DEBUG
  6404. +void __DWC_DEBUG(char *format, ...)
  6405. +{
  6406. + va_list args;
  6407. +
  6408. + va_start(args, format);
  6409. + DWC_VPRINTF(format, args);
  6410. + va_end(args);
  6411. +}
  6412. +#endif
  6413. +
  6414. +
  6415. +/* dwc_mem.h */
  6416. +
  6417. +#if 0
  6418. +dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size,
  6419. + uint32_t align,
  6420. + uint32_t alloc)
  6421. +{
  6422. + struct dma_pool *pool = dma_pool_create("Pool", NULL,
  6423. + size, align, alloc);
  6424. + return (dwc_pool_t *)pool;
  6425. +}
  6426. +
  6427. +void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool)
  6428. +{
  6429. + dma_pool_destroy((struct dma_pool *)pool);
  6430. +}
  6431. +
  6432. +void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  6433. +{
  6434. +// return dma_pool_alloc((struct dma_pool *)pool, GFP_KERNEL, dma_addr);
  6435. + return dma_pool_alloc((struct dma_pool *)pool, M_WAITOK, dma_addr);
  6436. +}
  6437. +
  6438. +void *DWC_DMA_POOL_ZALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  6439. +{
  6440. + void *vaddr = DWC_DMA_POOL_ALLOC(pool, dma_addr);
  6441. + memset(..);
  6442. +}
  6443. +
  6444. +void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr)
  6445. +{
  6446. + dma_pool_free(pool, vaddr, daddr);
  6447. +}
  6448. +#endif
  6449. +
  6450. +static void dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
  6451. +{
  6452. + if (error)
  6453. + return;
  6454. + *(bus_addr_t *)arg = segs[0].ds_addr;
  6455. +}
  6456. +
  6457. +void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
  6458. +{
  6459. + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
  6460. + int error;
  6461. +
  6462. + error = bus_dma_tag_create(
  6463. +#if __FreeBSD_version >= 700000
  6464. + bus_get_dma_tag(dma->dev), /* parent */
  6465. +#else
  6466. + NULL, /* parent */
  6467. +#endif
  6468. + 4, 0, /* alignment, bounds */
  6469. + BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
  6470. + BUS_SPACE_MAXADDR, /* highaddr */
  6471. + NULL, NULL, /* filter, filterarg */
  6472. + size, /* maxsize */
  6473. + 1, /* nsegments */
  6474. + size, /* maxsegsize */
  6475. + 0, /* flags */
  6476. + NULL, /* lockfunc */
  6477. + NULL, /* lockarg */
  6478. + &dma->dma_tag);
  6479. + if (error) {
  6480. + device_printf(dma->dev, "%s: bus_dma_tag_create failed: %d\n",
  6481. + __func__, error);
  6482. + goto fail_0;
  6483. + }
  6484. +
  6485. + error = bus_dmamem_alloc(dma->dma_tag, &dma->dma_vaddr,
  6486. + BUS_DMA_NOWAIT | BUS_DMA_COHERENT, &dma->dma_map);
  6487. + if (error) {
  6488. + device_printf(dma->dev, "%s: bus_dmamem_alloc(%ju) failed: %d\n",
  6489. + __func__, (uintmax_t)size, error);
  6490. + goto fail_1;
  6491. + }
  6492. +
  6493. + dma->dma_paddr = 0;
  6494. + error = bus_dmamap_load(dma->dma_tag, dma->dma_map, dma->dma_vaddr, size,
  6495. + dmamap_cb, &dma->dma_paddr, BUS_DMA_NOWAIT);
  6496. + if (error || dma->dma_paddr == 0) {
  6497. + device_printf(dma->dev, "%s: bus_dmamap_load failed: %d\n",
  6498. + __func__, error);
  6499. + goto fail_2;
  6500. + }
  6501. +
  6502. + *dma_addr = dma->dma_paddr;
  6503. + return dma->dma_vaddr;
  6504. +
  6505. +fail_2:
  6506. + bus_dmamap_unload(dma->dma_tag, dma->dma_map);
  6507. +fail_1:
  6508. + bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
  6509. + bus_dma_tag_destroy(dma->dma_tag);
  6510. +fail_0:
  6511. + dma->dma_map = NULL;
  6512. + dma->dma_tag = NULL;
  6513. +
  6514. + return NULL;
  6515. +}
  6516. +
  6517. +void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr)
  6518. +{
  6519. + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
  6520. +
  6521. + if (dma->dma_tag == NULL)
  6522. + return;
  6523. + if (dma->dma_map != NULL) {
  6524. + bus_dmamap_sync(dma->dma_tag, dma->dma_map,
  6525. + BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
  6526. + bus_dmamap_unload(dma->dma_tag, dma->dma_map);
  6527. + bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
  6528. + dma->dma_map = NULL;
  6529. + }
  6530. +
  6531. + bus_dma_tag_destroy(dma->dma_tag);
  6532. + dma->dma_tag = NULL;
  6533. +}
  6534. +
  6535. +void *__DWC_ALLOC(void *mem_ctx, uint32_t size)
  6536. +{
  6537. + return malloc(size, M_DEVBUF, M_WAITOK | M_ZERO);
  6538. +}
  6539. +
  6540. +void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size)
  6541. +{
  6542. + return malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
  6543. +}
  6544. +
  6545. +void __DWC_FREE(void *mem_ctx, void *addr)
  6546. +{
  6547. + free(addr, M_DEVBUF);
  6548. +}
  6549. +
  6550. +
  6551. +#ifdef DWC_CRYPTOLIB
  6552. +/* dwc_crypto.h */
  6553. +
  6554. +void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length)
  6555. +{
  6556. + get_random_bytes(buffer, length);
  6557. +}
  6558. +
  6559. +int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out)
  6560. +{
  6561. + struct crypto_blkcipher *tfm;
  6562. + struct blkcipher_desc desc;
  6563. + struct scatterlist sgd;
  6564. + struct scatterlist sgs;
  6565. +
  6566. + tfm = crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
  6567. + if (tfm == NULL) {
  6568. + printk("failed to load transform for aes CBC\n");
  6569. + return -1;
  6570. + }
  6571. +
  6572. + crypto_blkcipher_setkey(tfm, key, keylen);
  6573. + crypto_blkcipher_set_iv(tfm, iv, 16);
  6574. +
  6575. + sg_init_one(&sgd, out, messagelen);
  6576. + sg_init_one(&sgs, message, messagelen);
  6577. +
  6578. + desc.tfm = tfm;
  6579. + desc.flags = 0;
  6580. +
  6581. + if (crypto_blkcipher_encrypt(&desc, &sgd, &sgs, messagelen)) {
  6582. + crypto_free_blkcipher(tfm);
  6583. + DWC_ERROR("AES CBC encryption failed");
  6584. + return -1;
  6585. + }
  6586. +
  6587. + crypto_free_blkcipher(tfm);
  6588. + return 0;
  6589. +}
  6590. +
  6591. +int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out)
  6592. +{
  6593. + struct crypto_hash *tfm;
  6594. + struct hash_desc desc;
  6595. + struct scatterlist sg;
  6596. +
  6597. + tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
  6598. + if (IS_ERR(tfm)) {
  6599. + DWC_ERROR("Failed to load transform for sha256: %ld", PTR_ERR(tfm));
  6600. + return 0;
  6601. + }
  6602. + desc.tfm = tfm;
  6603. + desc.flags = 0;
  6604. +
  6605. + sg_init_one(&sg, message, len);
  6606. + crypto_hash_digest(&desc, &sg, len, out);
  6607. + crypto_free_hash(tfm);
  6608. +
  6609. + return 1;
  6610. +}
  6611. +
  6612. +int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen,
  6613. + uint8_t *key, uint32_t keylen, uint8_t *out)
  6614. +{
  6615. + struct crypto_hash *tfm;
  6616. + struct hash_desc desc;
  6617. + struct scatterlist sg;
  6618. +
  6619. + tfm = crypto_alloc_hash("hmac(sha256)", 0, CRYPTO_ALG_ASYNC);
  6620. + if (IS_ERR(tfm)) {
  6621. + DWC_ERROR("Failed to load transform for hmac(sha256): %ld", PTR_ERR(tfm));
  6622. + return 0;
  6623. + }
  6624. + desc.tfm = tfm;
  6625. + desc.flags = 0;
  6626. +
  6627. + sg_init_one(&sg, message, messagelen);
  6628. + crypto_hash_setkey(tfm, key, keylen);
  6629. + crypto_hash_digest(&desc, &sg, messagelen, out);
  6630. + crypto_free_hash(tfm);
  6631. +
  6632. + return 1;
  6633. +}
  6634. +
  6635. +#endif /* DWC_CRYPTOLIB */
  6636. +
  6637. +
  6638. +/* Byte Ordering Conversions */
  6639. +
  6640. +uint32_t DWC_CPU_TO_LE32(uint32_t *p)
  6641. +{
  6642. +#ifdef __LITTLE_ENDIAN
  6643. + return *p;
  6644. +#else
  6645. + uint8_t *u_p = (uint8_t *)p;
  6646. +
  6647. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  6648. +#endif
  6649. +}
  6650. +
  6651. +uint32_t DWC_CPU_TO_BE32(uint32_t *p)
  6652. +{
  6653. +#ifdef __BIG_ENDIAN
  6654. + return *p;
  6655. +#else
  6656. + uint8_t *u_p = (uint8_t *)p;
  6657. +
  6658. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  6659. +#endif
  6660. +}
  6661. +
  6662. +uint32_t DWC_LE32_TO_CPU(uint32_t *p)
  6663. +{
  6664. +#ifdef __LITTLE_ENDIAN
  6665. + return *p;
  6666. +#else
  6667. + uint8_t *u_p = (uint8_t *)p;
  6668. +
  6669. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  6670. +#endif
  6671. +}
  6672. +
  6673. +uint32_t DWC_BE32_TO_CPU(uint32_t *p)
  6674. +{
  6675. +#ifdef __BIG_ENDIAN
  6676. + return *p;
  6677. +#else
  6678. + uint8_t *u_p = (uint8_t *)p;
  6679. +
  6680. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  6681. +#endif
  6682. +}
  6683. +
  6684. +uint16_t DWC_CPU_TO_LE16(uint16_t *p)
  6685. +{
  6686. +#ifdef __LITTLE_ENDIAN
  6687. + return *p;
  6688. +#else
  6689. + uint8_t *u_p = (uint8_t *)p;
  6690. + return (u_p[1] | (u_p[0] << 8));
  6691. +#endif
  6692. +}
  6693. +
  6694. +uint16_t DWC_CPU_TO_BE16(uint16_t *p)
  6695. +{
  6696. +#ifdef __BIG_ENDIAN
  6697. + return *p;
  6698. +#else
  6699. + uint8_t *u_p = (uint8_t *)p;
  6700. + return (u_p[1] | (u_p[0] << 8));
  6701. +#endif
  6702. +}
  6703. +
  6704. +uint16_t DWC_LE16_TO_CPU(uint16_t *p)
  6705. +{
  6706. +#ifdef __LITTLE_ENDIAN
  6707. + return *p;
  6708. +#else
  6709. + uint8_t *u_p = (uint8_t *)p;
  6710. + return (u_p[1] | (u_p[0] << 8));
  6711. +#endif
  6712. +}
  6713. +
  6714. +uint16_t DWC_BE16_TO_CPU(uint16_t *p)
  6715. +{
  6716. +#ifdef __BIG_ENDIAN
  6717. + return *p;
  6718. +#else
  6719. + uint8_t *u_p = (uint8_t *)p;
  6720. + return (u_p[1] | (u_p[0] << 8));
  6721. +#endif
  6722. +}
  6723. +
  6724. +
  6725. +/* Registers */
  6726. +
  6727. +uint32_t DWC_READ_REG32(void *io_ctx, uint32_t volatile *reg)
  6728. +{
  6729. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  6730. + bus_size_t ior = (bus_size_t)reg;
  6731. +
  6732. + return bus_space_read_4(io->iot, io->ioh, ior);
  6733. +}
  6734. +
  6735. +#if 0
  6736. +uint64_t DWC_READ_REG64(void *io_ctx, uint64_t volatile *reg)
  6737. +{
  6738. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  6739. + bus_size_t ior = (bus_size_t)reg;
  6740. +
  6741. + return bus_space_read_8(io->iot, io->ioh, ior);
  6742. +}
  6743. +#endif
  6744. +
  6745. +void DWC_WRITE_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t value)
  6746. +{
  6747. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  6748. + bus_size_t ior = (bus_size_t)reg;
  6749. +
  6750. + bus_space_write_4(io->iot, io->ioh, ior, value);
  6751. +}
  6752. +
  6753. +#if 0
  6754. +void DWC_WRITE_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t value)
  6755. +{
  6756. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  6757. + bus_size_t ior = (bus_size_t)reg;
  6758. +
  6759. + bus_space_write_8(io->iot, io->ioh, ior, value);
  6760. +}
  6761. +#endif
  6762. +
  6763. +void DWC_MODIFY_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t clear_mask,
  6764. + uint32_t set_mask)
  6765. +{
  6766. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  6767. + bus_size_t ior = (bus_size_t)reg;
  6768. +
  6769. + bus_space_write_4(io->iot, io->ioh, ior,
  6770. + (bus_space_read_4(io->iot, io->ioh, ior) &
  6771. + ~clear_mask) | set_mask);
  6772. +}
  6773. +
  6774. +#if 0
  6775. +void DWC_MODIFY_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t clear_mask,
  6776. + uint64_t set_mask)
  6777. +{
  6778. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  6779. + bus_size_t ior = (bus_size_t)reg;
  6780. +
  6781. + bus_space_write_8(io->iot, io->ioh, ior,
  6782. + (bus_space_read_8(io->iot, io->ioh, ior) &
  6783. + ~clear_mask) | set_mask);
  6784. +}
  6785. +#endif
  6786. +
  6787. +
  6788. +/* Locking */
  6789. +
  6790. +dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void)
  6791. +{
  6792. + struct mtx *sl = DWC_ALLOC(sizeof(*sl));
  6793. +
  6794. + if (!sl) {
  6795. + DWC_ERROR("Cannot allocate memory for spinlock");
  6796. + return NULL;
  6797. + }
  6798. +
  6799. + mtx_init(sl, "dw3spn", NULL, MTX_SPIN);
  6800. + return (dwc_spinlock_t *)sl;
  6801. +}
  6802. +
  6803. +void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock)
  6804. +{
  6805. + struct mtx *sl = (struct mtx *)lock;
  6806. +
  6807. + mtx_destroy(sl);
  6808. + DWC_FREE(sl);
  6809. +}
  6810. +
  6811. +void DWC_SPINLOCK(dwc_spinlock_t *lock)
  6812. +{
  6813. + mtx_lock_spin((struct mtx *)lock); // ???
  6814. +}
  6815. +
  6816. +void DWC_SPINUNLOCK(dwc_spinlock_t *lock)
  6817. +{
  6818. + mtx_unlock_spin((struct mtx *)lock); // ???
  6819. +}
  6820. +
  6821. +void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags)
  6822. +{
  6823. + mtx_lock_spin((struct mtx *)lock);
  6824. +}
  6825. +
  6826. +void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags)
  6827. +{
  6828. + mtx_unlock_spin((struct mtx *)lock);
  6829. +}
  6830. +
  6831. +dwc_mutex_t *DWC_MUTEX_ALLOC(void)
  6832. +{
  6833. + struct mtx *m;
  6834. + dwc_mutex_t *mutex = (dwc_mutex_t *)DWC_ALLOC(sizeof(struct mtx));
  6835. +
  6836. + if (!mutex) {
  6837. + DWC_ERROR("Cannot allocate memory for mutex");
  6838. + return NULL;
  6839. + }
  6840. +
  6841. + m = (struct mtx *)mutex;
  6842. + mtx_init(m, "dw3mtx", NULL, MTX_DEF);
  6843. + return mutex;
  6844. +}
  6845. +
  6846. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
  6847. +#else
  6848. +void DWC_MUTEX_FREE(dwc_mutex_t *mutex)
  6849. +{
  6850. + mtx_destroy((struct mtx *)mutex);
  6851. + DWC_FREE(mutex);
  6852. +}
  6853. +#endif
  6854. +
  6855. +void DWC_MUTEX_LOCK(dwc_mutex_t *mutex)
  6856. +{
  6857. + struct mtx *m = (struct mtx *)mutex;
  6858. +
  6859. + mtx_lock(m);
  6860. +}
  6861. +
  6862. +int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex)
  6863. +{
  6864. + struct mtx *m = (struct mtx *)mutex;
  6865. +
  6866. + return mtx_trylock(m);
  6867. +}
  6868. +
  6869. +void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex)
  6870. +{
  6871. + struct mtx *m = (struct mtx *)mutex;
  6872. +
  6873. + mtx_unlock(m);
  6874. +}
  6875. +
  6876. +
  6877. +/* Timing */
  6878. +
  6879. +void DWC_UDELAY(uint32_t usecs)
  6880. +{
  6881. + DELAY(usecs);
  6882. +}
  6883. +
  6884. +void DWC_MDELAY(uint32_t msecs)
  6885. +{
  6886. + do {
  6887. + DELAY(1000);
  6888. + } while (--msecs);
  6889. +}
  6890. +
  6891. +void DWC_MSLEEP(uint32_t msecs)
  6892. +{
  6893. + struct timeval tv;
  6894. +
  6895. + tv.tv_sec = msecs / 1000;
  6896. + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
  6897. + pause("dw3slp", tvtohz(&tv));
  6898. +}
  6899. +
  6900. +uint32_t DWC_TIME(void)
  6901. +{
  6902. + struct timeval tv;
  6903. +
  6904. + microuptime(&tv); // or getmicrouptime? (less precise, but faster)
  6905. + return tv.tv_sec * 1000 + tv.tv_usec / 1000;
  6906. +}
  6907. +
  6908. +
  6909. +/* Timers */
  6910. +
  6911. +struct dwc_timer {
  6912. + struct callout t;
  6913. + char *name;
  6914. + dwc_spinlock_t *lock;
  6915. + dwc_timer_callback_t cb;
  6916. + void *data;
  6917. +};
  6918. +
  6919. +dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data)
  6920. +{
  6921. + dwc_timer_t *t = DWC_ALLOC(sizeof(*t));
  6922. +
  6923. + if (!t) {
  6924. + DWC_ERROR("Cannot allocate memory for timer");
  6925. + return NULL;
  6926. + }
  6927. +
  6928. + callout_init(&t->t, 1);
  6929. +
  6930. + t->name = DWC_STRDUP(name);
  6931. + if (!t->name) {
  6932. + DWC_ERROR("Cannot allocate memory for timer->name");
  6933. + goto no_name;
  6934. + }
  6935. +
  6936. + t->lock = DWC_SPINLOCK_ALLOC();
  6937. + if (!t->lock) {
  6938. + DWC_ERROR("Cannot allocate memory for lock");
  6939. + goto no_lock;
  6940. + }
  6941. +
  6942. + t->cb = cb;
  6943. + t->data = data;
  6944. +
  6945. + return t;
  6946. +
  6947. + no_lock:
  6948. + DWC_FREE(t->name);
  6949. + no_name:
  6950. + DWC_FREE(t);
  6951. +
  6952. + return NULL;
  6953. +}
  6954. +
  6955. +void DWC_TIMER_FREE(dwc_timer_t *timer)
  6956. +{
  6957. + callout_stop(&timer->t);
  6958. + DWC_SPINLOCK_FREE(timer->lock);
  6959. + DWC_FREE(timer->name);
  6960. + DWC_FREE(timer);
  6961. +}
  6962. +
  6963. +void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time)
  6964. +{
  6965. + struct timeval tv;
  6966. +
  6967. + tv.tv_sec = time / 1000;
  6968. + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
  6969. + callout_reset(&timer->t, tvtohz(&tv), timer->cb, timer->data);
  6970. +}
  6971. +
  6972. +void DWC_TIMER_CANCEL(dwc_timer_t *timer)
  6973. +{
  6974. + callout_stop(&timer->t);
  6975. +}
  6976. +
  6977. +
  6978. +/* Wait Queues */
  6979. +
  6980. +struct dwc_waitq {
  6981. + struct mtx lock;
  6982. + int abort;
  6983. +};
  6984. +
  6985. +dwc_waitq_t *DWC_WAITQ_ALLOC(void)
  6986. +{
  6987. + dwc_waitq_t *wq = DWC_ALLOC(sizeof(*wq));
  6988. +
  6989. + if (!wq) {
  6990. + DWC_ERROR("Cannot allocate memory for waitqueue");
  6991. + return NULL;
  6992. + }
  6993. +
  6994. + mtx_init(&wq->lock, "dw3wtq", NULL, MTX_DEF);
  6995. + wq->abort = 0;
  6996. +
  6997. + return wq;
  6998. +}
  6999. +
  7000. +void DWC_WAITQ_FREE(dwc_waitq_t *wq)
  7001. +{
  7002. + mtx_destroy(&wq->lock);
  7003. + DWC_FREE(wq);
  7004. +}
  7005. +
  7006. +int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data)
  7007. +{
  7008. +// intrmask_t ipl;
  7009. + int result = 0;
  7010. +
  7011. + mtx_lock(&wq->lock);
  7012. +// ipl = splbio();
  7013. +
  7014. + /* Skip the sleep if already aborted or triggered */
  7015. + if (!wq->abort && !cond(data)) {
  7016. +// splx(ipl);
  7017. + result = msleep(wq, &wq->lock, PCATCH, "dw3wat", 0); // infinite timeout
  7018. +// ipl = splbio();
  7019. + }
  7020. +
  7021. + if (result == ERESTART) { // signaled - restart
  7022. + result = -DWC_E_RESTART;
  7023. +
  7024. + } else if (result == EINTR) { // signaled - interrupt
  7025. + result = -DWC_E_ABORT;
  7026. +
  7027. + } else if (wq->abort) {
  7028. + result = -DWC_E_ABORT;
  7029. +
  7030. + } else {
  7031. + result = 0;
  7032. + }
  7033. +
  7034. + wq->abort = 0;
  7035. +// splx(ipl);
  7036. + mtx_unlock(&wq->lock);
  7037. + return result;
  7038. +}
  7039. +
  7040. +int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
  7041. + void *data, int32_t msecs)
  7042. +{
  7043. + struct timeval tv, tv1, tv2;
  7044. +// intrmask_t ipl;
  7045. + int result = 0;
  7046. +
  7047. + tv.tv_sec = msecs / 1000;
  7048. + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
  7049. +
  7050. + mtx_lock(&wq->lock);
  7051. +// ipl = splbio();
  7052. +
  7053. + /* Skip the sleep if already aborted or triggered */
  7054. + if (!wq->abort && !cond(data)) {
  7055. +// splx(ipl);
  7056. + getmicrouptime(&tv1);
  7057. + result = msleep(wq, &wq->lock, PCATCH, "dw3wto", tvtohz(&tv));
  7058. + getmicrouptime(&tv2);
  7059. +// ipl = splbio();
  7060. + }
  7061. +
  7062. + if (result == 0) { // awoken
  7063. + if (wq->abort) {
  7064. + result = -DWC_E_ABORT;
  7065. + } else {
  7066. + tv2.tv_usec -= tv1.tv_usec;
  7067. + if (tv2.tv_usec < 0) {
  7068. + tv2.tv_usec += 1000000;
  7069. + tv2.tv_sec--;
  7070. + }
  7071. +
  7072. + tv2.tv_sec -= tv1.tv_sec;
  7073. + result = tv2.tv_sec * 1000 + tv2.tv_usec / 1000;
  7074. + result = msecs - result;
  7075. + if (result <= 0)
  7076. + result = 1;
  7077. + }
  7078. + } else if (result == ERESTART) { // signaled - restart
  7079. + result = -DWC_E_RESTART;
  7080. +
  7081. + } else if (result == EINTR) { // signaled - interrupt
  7082. + result = -DWC_E_ABORT;
  7083. +
  7084. + } else { // timed out
  7085. + result = -DWC_E_TIMEOUT;
  7086. + }
  7087. +
  7088. + wq->abort = 0;
  7089. +// splx(ipl);
  7090. + mtx_unlock(&wq->lock);
  7091. + return result;
  7092. +}
  7093. +
  7094. +void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq)
  7095. +{
  7096. + wakeup(wq);
  7097. +}
  7098. +
  7099. +void DWC_WAITQ_ABORT(dwc_waitq_t *wq)
  7100. +{
  7101. +// intrmask_t ipl;
  7102. +
  7103. + mtx_lock(&wq->lock);
  7104. +// ipl = splbio();
  7105. + wq->abort = 1;
  7106. + wakeup(wq);
  7107. +// splx(ipl);
  7108. + mtx_unlock(&wq->lock);
  7109. +}
  7110. +
  7111. +
  7112. +/* Threading */
  7113. +
  7114. +struct dwc_thread {
  7115. + struct proc *proc;
  7116. + int abort;
  7117. +};
  7118. +
  7119. +dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data)
  7120. +{
  7121. + int retval;
  7122. + dwc_thread_t *thread = DWC_ALLOC(sizeof(*thread));
  7123. +
  7124. + if (!thread) {
  7125. + return NULL;
  7126. + }
  7127. +
  7128. + thread->abort = 0;
  7129. + retval = kthread_create((void (*)(void *))func, data, &thread->proc,
  7130. + RFPROC | RFNOWAIT, 0, "%s", name);
  7131. + if (retval) {
  7132. + DWC_FREE(thread);
  7133. + return NULL;
  7134. + }
  7135. +
  7136. + return thread;
  7137. +}
  7138. +
  7139. +int DWC_THREAD_STOP(dwc_thread_t *thread)
  7140. +{
  7141. + int retval;
  7142. +
  7143. + thread->abort = 1;
  7144. + retval = tsleep(&thread->abort, 0, "dw3stp", 60 * hz);
  7145. +
  7146. + if (retval == 0) {
  7147. + /* DWC_THREAD_EXIT() will free the thread struct */
  7148. + return 0;
  7149. + }
  7150. +
  7151. + /* NOTE: We leak the thread struct if thread doesn't die */
  7152. +
  7153. + if (retval == EWOULDBLOCK) {
  7154. + return -DWC_E_TIMEOUT;
  7155. + }
  7156. +
  7157. + return -DWC_E_UNKNOWN;
  7158. +}
  7159. +
  7160. +dwc_bool_t DWC_THREAD_SHOULD_STOP(dwc_thread_t *thread)
  7161. +{
  7162. + return thread->abort;
  7163. +}
  7164. +
  7165. +void DWC_THREAD_EXIT(dwc_thread_t *thread)
  7166. +{
  7167. + wakeup(&thread->abort);
  7168. + DWC_FREE(thread);
  7169. + kthread_exit(0);
  7170. +}
  7171. +
  7172. +
  7173. +/* tasklets
  7174. + - Runs in interrupt context (cannot sleep)
  7175. + - Each tasklet runs on a single CPU [ How can we ensure this on FreeBSD? Does it matter? ]
  7176. + - Different tasklets can be running simultaneously on different CPUs [ shouldn't matter ]
  7177. + */
  7178. +struct dwc_tasklet {
  7179. + struct task t;
  7180. + dwc_tasklet_callback_t cb;
  7181. + void *data;
  7182. +};
  7183. +
  7184. +static void tasklet_callback(void *data, int pending) // what to do with pending ???
  7185. +{
  7186. + dwc_tasklet_t *task = (dwc_tasklet_t *)data;
  7187. +
  7188. + task->cb(task->data);
  7189. +}
  7190. +
  7191. +dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data)
  7192. +{
  7193. + dwc_tasklet_t *task = DWC_ALLOC(sizeof(*task));
  7194. +
  7195. + if (task) {
  7196. + task->cb = cb;
  7197. + task->data = data;
  7198. + TASK_INIT(&task->t, 0, tasklet_callback, task);
  7199. + } else {
  7200. + DWC_ERROR("Cannot allocate memory for tasklet");
  7201. + }
  7202. +
  7203. + return task;
  7204. +}
  7205. +
  7206. +void DWC_TASK_FREE(dwc_tasklet_t *task)
  7207. +{
  7208. + taskqueue_drain(taskqueue_fast, &task->t); // ???
  7209. + DWC_FREE(task);
  7210. +}
  7211. +
  7212. +void DWC_TASK_SCHEDULE(dwc_tasklet_t *task)
  7213. +{
  7214. + /* Uses predefined system queue */
  7215. + taskqueue_enqueue_fast(taskqueue_fast, &task->t);
  7216. +}
  7217. +
  7218. +
  7219. +/* workqueues
  7220. + - Runs in process context (can sleep)
  7221. + */
  7222. +typedef struct work_container {
  7223. + dwc_work_callback_t cb;
  7224. + void *data;
  7225. + dwc_workq_t *wq;
  7226. + char *name;
  7227. + int hz;
  7228. +
  7229. +#ifdef DEBUG
  7230. + DWC_CIRCLEQ_ENTRY(work_container) entry;
  7231. +#endif
  7232. + struct task task;
  7233. +} work_container_t;
  7234. +
  7235. +#ifdef DEBUG
  7236. +DWC_CIRCLEQ_HEAD(work_container_queue, work_container);
  7237. +#endif
  7238. +
  7239. +struct dwc_workq {
  7240. + struct taskqueue *taskq;
  7241. + dwc_spinlock_t *lock;
  7242. + dwc_waitq_t *waitq;
  7243. + int pending;
  7244. +
  7245. +#ifdef DEBUG
  7246. + struct work_container_queue entries;
  7247. +#endif
  7248. +};
  7249. +
  7250. +static void do_work(void *data, int pending) // what to do with pending ???
  7251. +{
  7252. + work_container_t *container = (work_container_t *)data;
  7253. + dwc_workq_t *wq = container->wq;
  7254. + dwc_irqflags_t flags;
  7255. +
  7256. + if (container->hz) {
  7257. + pause("dw3wrk", container->hz);
  7258. + }
  7259. +
  7260. + container->cb(container->data);
  7261. + DWC_DEBUG("Work done: %s, container=%p", container->name, container);
  7262. +
  7263. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  7264. +
  7265. +#ifdef DEBUG
  7266. + DWC_CIRCLEQ_REMOVE(&wq->entries, container, entry);
  7267. +#endif
  7268. + if (container->name)
  7269. + DWC_FREE(container->name);
  7270. + DWC_FREE(container);
  7271. + wq->pending--;
  7272. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  7273. + DWC_WAITQ_TRIGGER(wq->waitq);
  7274. +}
  7275. +
  7276. +static int work_done(void *data)
  7277. +{
  7278. + dwc_workq_t *workq = (dwc_workq_t *)data;
  7279. +
  7280. + return workq->pending == 0;
  7281. +}
  7282. +
  7283. +int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout)
  7284. +{
  7285. + return DWC_WAITQ_WAIT_TIMEOUT(workq->waitq, work_done, workq, timeout);
  7286. +}
  7287. +
  7288. +dwc_workq_t *DWC_WORKQ_ALLOC(char *name)
  7289. +{
  7290. + dwc_workq_t *wq = DWC_ALLOC(sizeof(*wq));
  7291. +
  7292. + if (!wq) {
  7293. + DWC_ERROR("Cannot allocate memory for workqueue");
  7294. + return NULL;
  7295. + }
  7296. +
  7297. + wq->taskq = taskqueue_create(name, M_NOWAIT, taskqueue_thread_enqueue, &wq->taskq);
  7298. + if (!wq->taskq) {
  7299. + DWC_ERROR("Cannot allocate memory for taskqueue");
  7300. + goto no_taskq;
  7301. + }
  7302. +
  7303. + wq->pending = 0;
  7304. +
  7305. + wq->lock = DWC_SPINLOCK_ALLOC();
  7306. + if (!wq->lock) {
  7307. + DWC_ERROR("Cannot allocate memory for spinlock");
  7308. + goto no_lock;
  7309. + }
  7310. +
  7311. + wq->waitq = DWC_WAITQ_ALLOC();
  7312. + if (!wq->waitq) {
  7313. + DWC_ERROR("Cannot allocate memory for waitqueue");
  7314. + goto no_waitq;
  7315. + }
  7316. +
  7317. + taskqueue_start_threads(&wq->taskq, 1, PWAIT, "%s taskq", "dw3tsk");
  7318. +
  7319. +#ifdef DEBUG
  7320. + DWC_CIRCLEQ_INIT(&wq->entries);
  7321. +#endif
  7322. + return wq;
  7323. +
  7324. + no_waitq:
  7325. + DWC_SPINLOCK_FREE(wq->lock);
  7326. + no_lock:
  7327. + taskqueue_free(wq->taskq);
  7328. + no_taskq:
  7329. + DWC_FREE(wq);
  7330. +
  7331. + return NULL;
  7332. +}
  7333. +
  7334. +void DWC_WORKQ_FREE(dwc_workq_t *wq)
  7335. +{
  7336. +#ifdef DEBUG
  7337. + dwc_irqflags_t flags;
  7338. +
  7339. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  7340. +
  7341. + if (wq->pending != 0) {
  7342. + struct work_container *container;
  7343. +
  7344. + DWC_ERROR("Destroying work queue with pending work");
  7345. +
  7346. + DWC_CIRCLEQ_FOREACH(container, &wq->entries, entry) {
  7347. + DWC_ERROR("Work %s still pending", container->name);
  7348. + }
  7349. + }
  7350. +
  7351. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  7352. +#endif
  7353. + DWC_WAITQ_FREE(wq->waitq);
  7354. + DWC_SPINLOCK_FREE(wq->lock);
  7355. + taskqueue_free(wq->taskq);
  7356. + DWC_FREE(wq);
  7357. +}
  7358. +
  7359. +void DWC_WORKQ_SCHEDULE(dwc_workq_t *wq, dwc_work_callback_t cb, void *data,
  7360. + char *format, ...)
  7361. +{
  7362. + dwc_irqflags_t flags;
  7363. + work_container_t *container;
  7364. + static char name[128];
  7365. + va_list args;
  7366. +
  7367. + va_start(args, format);
  7368. + DWC_VSNPRINTF(name, 128, format, args);
  7369. + va_end(args);
  7370. +
  7371. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  7372. + wq->pending++;
  7373. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  7374. + DWC_WAITQ_TRIGGER(wq->waitq);
  7375. +
  7376. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  7377. + if (!container) {
  7378. + DWC_ERROR("Cannot allocate memory for container");
  7379. + return;
  7380. + }
  7381. +
  7382. + container->name = DWC_STRDUP(name);
  7383. + if (!container->name) {
  7384. + DWC_ERROR("Cannot allocate memory for container->name");
  7385. + DWC_FREE(container);
  7386. + return;
  7387. + }
  7388. +
  7389. + container->cb = cb;
  7390. + container->data = data;
  7391. + container->wq = wq;
  7392. + container->hz = 0;
  7393. +
  7394. + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
  7395. +
  7396. + TASK_INIT(&container->task, 0, do_work, container);
  7397. +
  7398. +#ifdef DEBUG
  7399. + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
  7400. +#endif
  7401. + taskqueue_enqueue_fast(wq->taskq, &container->task);
  7402. +}
  7403. +
  7404. +void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *wq, dwc_work_callback_t cb,
  7405. + void *data, uint32_t time, char *format, ...)
  7406. +{
  7407. + dwc_irqflags_t flags;
  7408. + work_container_t *container;
  7409. + static char name[128];
  7410. + struct timeval tv;
  7411. + va_list args;
  7412. +
  7413. + va_start(args, format);
  7414. + DWC_VSNPRINTF(name, 128, format, args);
  7415. + va_end(args);
  7416. +
  7417. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  7418. + wq->pending++;
  7419. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  7420. + DWC_WAITQ_TRIGGER(wq->waitq);
  7421. +
  7422. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  7423. + if (!container) {
  7424. + DWC_ERROR("Cannot allocate memory for container");
  7425. + return;
  7426. + }
  7427. +
  7428. + container->name = DWC_STRDUP(name);
  7429. + if (!container->name) {
  7430. + DWC_ERROR("Cannot allocate memory for container->name");
  7431. + DWC_FREE(container);
  7432. + return;
  7433. + }
  7434. +
  7435. + container->cb = cb;
  7436. + container->data = data;
  7437. + container->wq = wq;
  7438. +
  7439. + tv.tv_sec = time / 1000;
  7440. + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
  7441. + container->hz = tvtohz(&tv);
  7442. +
  7443. + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
  7444. +
  7445. + TASK_INIT(&container->task, 0, do_work, container);
  7446. +
  7447. +#ifdef DEBUG
  7448. + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
  7449. +#endif
  7450. + taskqueue_enqueue_fast(wq->taskq, &container->task);
  7451. +}
  7452. +
  7453. +int DWC_WORKQ_PENDING(dwc_workq_t *wq)
  7454. +{
  7455. + return wq->pending;
  7456. +}
  7457. --- /dev/null
  7458. +++ b/drivers/usb/host/dwc_common_port/dwc_common_linux.c
  7459. @@ -0,0 +1,1409 @@
  7460. +#include <linux/kernel.h>
  7461. +#include <linux/init.h>
  7462. +#include <linux/module.h>
  7463. +#include <linux/kthread.h>
  7464. +
  7465. +#ifdef DWC_CCLIB
  7466. +# include "dwc_cc.h"
  7467. +#endif
  7468. +
  7469. +#ifdef DWC_CRYPTOLIB
  7470. +# include "dwc_modpow.h"
  7471. +# include "dwc_dh.h"
  7472. +# include "dwc_crypto.h"
  7473. +#endif
  7474. +
  7475. +#ifdef DWC_NOTIFYLIB
  7476. +# include "dwc_notifier.h"
  7477. +#endif
  7478. +
  7479. +/* OS-Level Implementations */
  7480. +
  7481. +/* This is the Linux kernel implementation of the DWC platform library. */
  7482. +#include <linux/moduleparam.h>
  7483. +#include <linux/ctype.h>
  7484. +#include <linux/crypto.h>
  7485. +#include <linux/delay.h>
  7486. +#include <linux/device.h>
  7487. +#include <linux/dma-mapping.h>
  7488. +#include <linux/cdev.h>
  7489. +#include <linux/errno.h>
  7490. +#include <linux/interrupt.h>
  7491. +#include <linux/jiffies.h>
  7492. +#include <linux/list.h>
  7493. +#include <linux/pci.h>
  7494. +#include <linux/random.h>
  7495. +#include <linux/scatterlist.h>
  7496. +#include <linux/slab.h>
  7497. +#include <linux/stat.h>
  7498. +#include <linux/string.h>
  7499. +#include <linux/timer.h>
  7500. +#include <linux/usb.h>
  7501. +
  7502. +#include <linux/version.h>
  7503. +
  7504. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
  7505. +# include <linux/usb/gadget.h>
  7506. +#else
  7507. +# include <linux/usb_gadget.h>
  7508. +#endif
  7509. +
  7510. +#include <asm/io.h>
  7511. +#include <asm/page.h>
  7512. +#include <asm/uaccess.h>
  7513. +#include <asm/unaligned.h>
  7514. +
  7515. +#include "dwc_os.h"
  7516. +#include "dwc_list.h"
  7517. +
  7518. +
  7519. +/* MISC */
  7520. +
  7521. +void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size)
  7522. +{
  7523. + return memset(dest, byte, size);
  7524. +}
  7525. +
  7526. +void *DWC_MEMCPY(void *dest, void const *src, uint32_t size)
  7527. +{
  7528. + return memcpy(dest, src, size);
  7529. +}
  7530. +
  7531. +void *DWC_MEMMOVE(void *dest, void *src, uint32_t size)
  7532. +{
  7533. + return memmove(dest, src, size);
  7534. +}
  7535. +
  7536. +int DWC_MEMCMP(void *m1, void *m2, uint32_t size)
  7537. +{
  7538. + return memcmp(m1, m2, size);
  7539. +}
  7540. +
  7541. +int DWC_STRNCMP(void *s1, void *s2, uint32_t size)
  7542. +{
  7543. + return strncmp(s1, s2, size);
  7544. +}
  7545. +
  7546. +int DWC_STRCMP(void *s1, void *s2)
  7547. +{
  7548. + return strcmp(s1, s2);
  7549. +}
  7550. +
  7551. +int DWC_STRLEN(char const *str)
  7552. +{
  7553. + return strlen(str);
  7554. +}
  7555. +
  7556. +char *DWC_STRCPY(char *to, char const *from)
  7557. +{
  7558. + return strcpy(to, from);
  7559. +}
  7560. +
  7561. +char *DWC_STRDUP(char const *str)
  7562. +{
  7563. + int len = DWC_STRLEN(str) + 1;
  7564. + char *new = DWC_ALLOC_ATOMIC(len);
  7565. +
  7566. + if (!new) {
  7567. + return NULL;
  7568. + }
  7569. +
  7570. + DWC_MEMCPY(new, str, len);
  7571. + return new;
  7572. +}
  7573. +
  7574. +int DWC_ATOI(const char *str, int32_t *value)
  7575. +{
  7576. + char *end = NULL;
  7577. +
  7578. + *value = simple_strtol(str, &end, 0);
  7579. + if (*end == '\0') {
  7580. + return 0;
  7581. + }
  7582. +
  7583. + return -1;
  7584. +}
  7585. +
  7586. +int DWC_ATOUI(const char *str, uint32_t *value)
  7587. +{
  7588. + char *end = NULL;
  7589. +
  7590. + *value = simple_strtoul(str, &end, 0);
  7591. + if (*end == '\0') {
  7592. + return 0;
  7593. + }
  7594. +
  7595. + return -1;
  7596. +}
  7597. +
  7598. +
  7599. +#ifdef DWC_UTFLIB
  7600. +/* From usbstring.c */
  7601. +
  7602. +int DWC_UTF8_TO_UTF16LE(uint8_t const *s, uint16_t *cp, unsigned len)
  7603. +{
  7604. + int count = 0;
  7605. + u8 c;
  7606. + u16 uchar;
  7607. +
  7608. + /* this insists on correct encodings, though not minimal ones.
  7609. + * BUT it currently rejects legit 4-byte UTF-8 code points,
  7610. + * which need surrogate pairs. (Unicode 3.1 can use them.)
  7611. + */
  7612. + while (len != 0 && (c = (u8) *s++) != 0) {
  7613. + if (unlikely(c & 0x80)) {
  7614. + // 2-byte sequence:
  7615. + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
  7616. + if ((c & 0xe0) == 0xc0) {
  7617. + uchar = (c & 0x1f) << 6;
  7618. +
  7619. + c = (u8) *s++;
  7620. + if ((c & 0xc0) != 0xc0)
  7621. + goto fail;
  7622. + c &= 0x3f;
  7623. + uchar |= c;
  7624. +
  7625. + // 3-byte sequence (most CJKV characters):
  7626. + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
  7627. + } else if ((c & 0xf0) == 0xe0) {
  7628. + uchar = (c & 0x0f) << 12;
  7629. +
  7630. + c = (u8) *s++;
  7631. + if ((c & 0xc0) != 0xc0)
  7632. + goto fail;
  7633. + c &= 0x3f;
  7634. + uchar |= c << 6;
  7635. +
  7636. + c = (u8) *s++;
  7637. + if ((c & 0xc0) != 0xc0)
  7638. + goto fail;
  7639. + c &= 0x3f;
  7640. + uchar |= c;
  7641. +
  7642. + /* no bogus surrogates */
  7643. + if (0xd800 <= uchar && uchar <= 0xdfff)
  7644. + goto fail;
  7645. +
  7646. + // 4-byte sequence (surrogate pairs, currently rare):
  7647. + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
  7648. + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
  7649. + // (uuuuu = wwww + 1)
  7650. + // FIXME accept the surrogate code points (only)
  7651. + } else
  7652. + goto fail;
  7653. + } else
  7654. + uchar = c;
  7655. + put_unaligned (cpu_to_le16 (uchar), cp++);
  7656. + count++;
  7657. + len--;
  7658. + }
  7659. + return count;
  7660. +fail:
  7661. + return -1;
  7662. +}
  7663. +#endif /* DWC_UTFLIB */
  7664. +
  7665. +
  7666. +/* dwc_debug.h */
  7667. +
  7668. +dwc_bool_t DWC_IN_IRQ(void)
  7669. +{
  7670. + return in_irq();
  7671. +}
  7672. +
  7673. +dwc_bool_t DWC_IN_BH(void)
  7674. +{
  7675. + return in_softirq();
  7676. +}
  7677. +
  7678. +void DWC_VPRINTF(char *format, va_list args)
  7679. +{
  7680. + vprintk(format, args);
  7681. +}
  7682. +
  7683. +int DWC_VSNPRINTF(char *str, int size, char *format, va_list args)
  7684. +{
  7685. + return vsnprintf(str, size, format, args);
  7686. +}
  7687. +
  7688. +void DWC_PRINTF(char *format, ...)
  7689. +{
  7690. + va_list args;
  7691. +
  7692. + va_start(args, format);
  7693. + DWC_VPRINTF(format, args);
  7694. + va_end(args);
  7695. +}
  7696. +
  7697. +int DWC_SPRINTF(char *buffer, char *format, ...)
  7698. +{
  7699. + int retval;
  7700. + va_list args;
  7701. +
  7702. + va_start(args, format);
  7703. + retval = vsprintf(buffer, format, args);
  7704. + va_end(args);
  7705. + return retval;
  7706. +}
  7707. +
  7708. +int DWC_SNPRINTF(char *buffer, int size, char *format, ...)
  7709. +{
  7710. + int retval;
  7711. + va_list args;
  7712. +
  7713. + va_start(args, format);
  7714. + retval = vsnprintf(buffer, size, format, args);
  7715. + va_end(args);
  7716. + return retval;
  7717. +}
  7718. +
  7719. +void __DWC_WARN(char *format, ...)
  7720. +{
  7721. + va_list args;
  7722. +
  7723. + va_start(args, format);
  7724. + DWC_PRINTF(KERN_WARNING);
  7725. + DWC_VPRINTF(format, args);
  7726. + va_end(args);
  7727. +}
  7728. +
  7729. +void __DWC_ERROR(char *format, ...)
  7730. +{
  7731. + va_list args;
  7732. +
  7733. + va_start(args, format);
  7734. + DWC_PRINTF(KERN_ERR);
  7735. + DWC_VPRINTF(format, args);
  7736. + va_end(args);
  7737. +}
  7738. +
  7739. +void DWC_EXCEPTION(char *format, ...)
  7740. +{
  7741. + va_list args;
  7742. +
  7743. + va_start(args, format);
  7744. + DWC_PRINTF(KERN_ERR);
  7745. + DWC_VPRINTF(format, args);
  7746. + va_end(args);
  7747. + BUG_ON(1);
  7748. +}
  7749. +
  7750. +#ifdef DEBUG
  7751. +void __DWC_DEBUG(char *format, ...)
  7752. +{
  7753. + va_list args;
  7754. +
  7755. + va_start(args, format);
  7756. + DWC_PRINTF(KERN_DEBUG);
  7757. + DWC_VPRINTF(format, args);
  7758. + va_end(args);
  7759. +}
  7760. +#endif
  7761. +
  7762. +
  7763. +/* dwc_mem.h */
  7764. +
  7765. +#if 0
  7766. +dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size,
  7767. + uint32_t align,
  7768. + uint32_t alloc)
  7769. +{
  7770. + struct dma_pool *pool = dma_pool_create("Pool", NULL,
  7771. + size, align, alloc);
  7772. + return (dwc_pool_t *)pool;
  7773. +}
  7774. +
  7775. +void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool)
  7776. +{
  7777. + dma_pool_destroy((struct dma_pool *)pool);
  7778. +}
  7779. +
  7780. +void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  7781. +{
  7782. + return dma_pool_alloc((struct dma_pool *)pool, GFP_KERNEL, dma_addr);
  7783. +}
  7784. +
  7785. +void *DWC_DMA_POOL_ZALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  7786. +{
  7787. + void *vaddr = DWC_DMA_POOL_ALLOC(pool, dma_addr);
  7788. + memset(..);
  7789. +}
  7790. +
  7791. +void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr)
  7792. +{
  7793. + dma_pool_free(pool, vaddr, daddr);
  7794. +}
  7795. +#endif
  7796. +
  7797. +void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
  7798. +{
  7799. + return dma_alloc_coherent(dma_ctx, size, dma_addr, GFP_KERNEL | GFP_DMA32);
  7800. +}
  7801. +
  7802. +void *__DWC_DMA_ALLOC_ATOMIC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
  7803. +{
  7804. + return dma_alloc_coherent(dma_ctx, size, dma_addr, GFP_ATOMIC);
  7805. +}
  7806. +
  7807. +void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr)
  7808. +{
  7809. + dma_free_coherent(dma_ctx, size, virt_addr, dma_addr);
  7810. +}
  7811. +
  7812. +void *__DWC_ALLOC(void *mem_ctx, uint32_t size)
  7813. +{
  7814. + return kzalloc(size, GFP_KERNEL);
  7815. +}
  7816. +
  7817. +void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size)
  7818. +{
  7819. + return kzalloc(size, GFP_ATOMIC);
  7820. +}
  7821. +
  7822. +void __DWC_FREE(void *mem_ctx, void *addr)
  7823. +{
  7824. + kfree(addr);
  7825. +}
  7826. +
  7827. +
  7828. +#ifdef DWC_CRYPTOLIB
  7829. +/* dwc_crypto.h */
  7830. +
  7831. +void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length)
  7832. +{
  7833. + get_random_bytes(buffer, length);
  7834. +}
  7835. +
  7836. +int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out)
  7837. +{
  7838. + struct crypto_blkcipher *tfm;
  7839. + struct blkcipher_desc desc;
  7840. + struct scatterlist sgd;
  7841. + struct scatterlist sgs;
  7842. +
  7843. + tfm = crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
  7844. + if (tfm == NULL) {
  7845. + printk("failed to load transform for aes CBC\n");
  7846. + return -1;
  7847. + }
  7848. +
  7849. + crypto_blkcipher_setkey(tfm, key, keylen);
  7850. + crypto_blkcipher_set_iv(tfm, iv, 16);
  7851. +
  7852. + sg_init_one(&sgd, out, messagelen);
  7853. + sg_init_one(&sgs, message, messagelen);
  7854. +
  7855. + desc.tfm = tfm;
  7856. + desc.flags = 0;
  7857. +
  7858. + if (crypto_blkcipher_encrypt(&desc, &sgd, &sgs, messagelen)) {
  7859. + crypto_free_blkcipher(tfm);
  7860. + DWC_ERROR("AES CBC encryption failed");
  7861. + return -1;
  7862. + }
  7863. +
  7864. + crypto_free_blkcipher(tfm);
  7865. + return 0;
  7866. +}
  7867. +
  7868. +int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out)
  7869. +{
  7870. + struct crypto_hash *tfm;
  7871. + struct hash_desc desc;
  7872. + struct scatterlist sg;
  7873. +
  7874. + tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
  7875. + if (IS_ERR(tfm)) {
  7876. + DWC_ERROR("Failed to load transform for sha256: %ld\n", PTR_ERR(tfm));
  7877. + return 0;
  7878. + }
  7879. + desc.tfm = tfm;
  7880. + desc.flags = 0;
  7881. +
  7882. + sg_init_one(&sg, message, len);
  7883. + crypto_hash_digest(&desc, &sg, len, out);
  7884. + crypto_free_hash(tfm);
  7885. +
  7886. + return 1;
  7887. +}
  7888. +
  7889. +int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen,
  7890. + uint8_t *key, uint32_t keylen, uint8_t *out)
  7891. +{
  7892. + struct crypto_hash *tfm;
  7893. + struct hash_desc desc;
  7894. + struct scatterlist sg;
  7895. +
  7896. + tfm = crypto_alloc_hash("hmac(sha256)", 0, CRYPTO_ALG_ASYNC);
  7897. + if (IS_ERR(tfm)) {
  7898. + DWC_ERROR("Failed to load transform for hmac(sha256): %ld\n", PTR_ERR(tfm));
  7899. + return 0;
  7900. + }
  7901. + desc.tfm = tfm;
  7902. + desc.flags = 0;
  7903. +
  7904. + sg_init_one(&sg, message, messagelen);
  7905. + crypto_hash_setkey(tfm, key, keylen);
  7906. + crypto_hash_digest(&desc, &sg, messagelen, out);
  7907. + crypto_free_hash(tfm);
  7908. +
  7909. + return 1;
  7910. +}
  7911. +#endif /* DWC_CRYPTOLIB */
  7912. +
  7913. +
  7914. +/* Byte Ordering Conversions */
  7915. +
  7916. +uint32_t DWC_CPU_TO_LE32(uint32_t *p)
  7917. +{
  7918. +#ifdef __LITTLE_ENDIAN
  7919. + return *p;
  7920. +#else
  7921. + uint8_t *u_p = (uint8_t *)p;
  7922. +
  7923. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  7924. +#endif
  7925. +}
  7926. +
  7927. +uint32_t DWC_CPU_TO_BE32(uint32_t *p)
  7928. +{
  7929. +#ifdef __BIG_ENDIAN
  7930. + return *p;
  7931. +#else
  7932. + uint8_t *u_p = (uint8_t *)p;
  7933. +
  7934. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  7935. +#endif
  7936. +}
  7937. +
  7938. +uint32_t DWC_LE32_TO_CPU(uint32_t *p)
  7939. +{
  7940. +#ifdef __LITTLE_ENDIAN
  7941. + return *p;
  7942. +#else
  7943. + uint8_t *u_p = (uint8_t *)p;
  7944. +
  7945. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  7946. +#endif
  7947. +}
  7948. +
  7949. +uint32_t DWC_BE32_TO_CPU(uint32_t *p)
  7950. +{
  7951. +#ifdef __BIG_ENDIAN
  7952. + return *p;
  7953. +#else
  7954. + uint8_t *u_p = (uint8_t *)p;
  7955. +
  7956. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  7957. +#endif
  7958. +}
  7959. +
  7960. +uint16_t DWC_CPU_TO_LE16(uint16_t *p)
  7961. +{
  7962. +#ifdef __LITTLE_ENDIAN
  7963. + return *p;
  7964. +#else
  7965. + uint8_t *u_p = (uint8_t *)p;
  7966. + return (u_p[1] | (u_p[0] << 8));
  7967. +#endif
  7968. +}
  7969. +
  7970. +uint16_t DWC_CPU_TO_BE16(uint16_t *p)
  7971. +{
  7972. +#ifdef __BIG_ENDIAN
  7973. + return *p;
  7974. +#else
  7975. + uint8_t *u_p = (uint8_t *)p;
  7976. + return (u_p[1] | (u_p[0] << 8));
  7977. +#endif
  7978. +}
  7979. +
  7980. +uint16_t DWC_LE16_TO_CPU(uint16_t *p)
  7981. +{
  7982. +#ifdef __LITTLE_ENDIAN
  7983. + return *p;
  7984. +#else
  7985. + uint8_t *u_p = (uint8_t *)p;
  7986. + return (u_p[1] | (u_p[0] << 8));
  7987. +#endif
  7988. +}
  7989. +
  7990. +uint16_t DWC_BE16_TO_CPU(uint16_t *p)
  7991. +{
  7992. +#ifdef __BIG_ENDIAN
  7993. + return *p;
  7994. +#else
  7995. + uint8_t *u_p = (uint8_t *)p;
  7996. + return (u_p[1] | (u_p[0] << 8));
  7997. +#endif
  7998. +}
  7999. +
  8000. +
  8001. +/* Registers */
  8002. +
  8003. +uint32_t DWC_READ_REG32(uint32_t volatile *reg)
  8004. +{
  8005. + return readl(reg);
  8006. +}
  8007. +
  8008. +#if 0
  8009. +uint64_t DWC_READ_REG64(uint64_t volatile *reg)
  8010. +{
  8011. +}
  8012. +#endif
  8013. +
  8014. +void DWC_WRITE_REG32(uint32_t volatile *reg, uint32_t value)
  8015. +{
  8016. + writel(value, reg);
  8017. +}
  8018. +
  8019. +#if 0
  8020. +void DWC_WRITE_REG64(uint64_t volatile *reg, uint64_t value)
  8021. +{
  8022. +}
  8023. +#endif
  8024. +
  8025. +void DWC_MODIFY_REG32(uint32_t volatile *reg, uint32_t clear_mask, uint32_t set_mask)
  8026. +{
  8027. + writel((readl(reg) & ~clear_mask) | set_mask, reg);
  8028. +}
  8029. +
  8030. +#if 0
  8031. +void DWC_MODIFY_REG64(uint64_t volatile *reg, uint64_t clear_mask, uint64_t set_mask)
  8032. +{
  8033. +}
  8034. +#endif
  8035. +
  8036. +
  8037. +/* Locking */
  8038. +
  8039. +dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void)
  8040. +{
  8041. + spinlock_t *sl = (spinlock_t *)1;
  8042. +
  8043. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  8044. + sl = DWC_ALLOC(sizeof(*sl));
  8045. + if (!sl) {
  8046. + DWC_ERROR("Cannot allocate memory for spinlock\n");
  8047. + return NULL;
  8048. + }
  8049. +
  8050. + spin_lock_init(sl);
  8051. +#endif
  8052. + return (dwc_spinlock_t *)sl;
  8053. +}
  8054. +
  8055. +void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock)
  8056. +{
  8057. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  8058. + DWC_FREE(lock);
  8059. +#endif
  8060. +}
  8061. +
  8062. +void DWC_SPINLOCK(dwc_spinlock_t *lock)
  8063. +{
  8064. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  8065. + spin_lock((spinlock_t *)lock);
  8066. +#endif
  8067. +}
  8068. +
  8069. +void DWC_SPINUNLOCK(dwc_spinlock_t *lock)
  8070. +{
  8071. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  8072. + spin_unlock((spinlock_t *)lock);
  8073. +#endif
  8074. +}
  8075. +
  8076. +void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags)
  8077. +{
  8078. + dwc_irqflags_t f;
  8079. +
  8080. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  8081. + spin_lock_irqsave((spinlock_t *)lock, f);
  8082. +#else
  8083. + local_irq_save(f);
  8084. +#endif
  8085. + *flags = f;
  8086. +}
  8087. +
  8088. +void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags)
  8089. +{
  8090. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  8091. + spin_unlock_irqrestore((spinlock_t *)lock, flags);
  8092. +#else
  8093. + local_irq_restore(flags);
  8094. +#endif
  8095. +}
  8096. +
  8097. +dwc_mutex_t *DWC_MUTEX_ALLOC(void)
  8098. +{
  8099. + struct mutex *m;
  8100. + dwc_mutex_t *mutex = (dwc_mutex_t *)DWC_ALLOC(sizeof(struct mutex));
  8101. +
  8102. + if (!mutex) {
  8103. + DWC_ERROR("Cannot allocate memory for mutex\n");
  8104. + return NULL;
  8105. + }
  8106. +
  8107. + m = (struct mutex *)mutex;
  8108. + mutex_init(m);
  8109. + return mutex;
  8110. +}
  8111. +
  8112. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
  8113. +#else
  8114. +void DWC_MUTEX_FREE(dwc_mutex_t *mutex)
  8115. +{
  8116. + mutex_destroy((struct mutex *)mutex);
  8117. + DWC_FREE(mutex);
  8118. +}
  8119. +#endif
  8120. +
  8121. +void DWC_MUTEX_LOCK(dwc_mutex_t *mutex)
  8122. +{
  8123. + struct mutex *m = (struct mutex *)mutex;
  8124. + mutex_lock(m);
  8125. +}
  8126. +
  8127. +int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex)
  8128. +{
  8129. + struct mutex *m = (struct mutex *)mutex;
  8130. + return mutex_trylock(m);
  8131. +}
  8132. +
  8133. +void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex)
  8134. +{
  8135. + struct mutex *m = (struct mutex *)mutex;
  8136. + mutex_unlock(m);
  8137. +}
  8138. +
  8139. +
  8140. +/* Timing */
  8141. +
  8142. +void DWC_UDELAY(uint32_t usecs)
  8143. +{
  8144. + udelay(usecs);
  8145. +}
  8146. +
  8147. +void DWC_MDELAY(uint32_t msecs)
  8148. +{
  8149. + mdelay(msecs);
  8150. +}
  8151. +
  8152. +void DWC_MSLEEP(uint32_t msecs)
  8153. +{
  8154. + msleep(msecs);
  8155. +}
  8156. +
  8157. +uint32_t DWC_TIME(void)
  8158. +{
  8159. + return jiffies_to_msecs(jiffies);
  8160. +}
  8161. +
  8162. +
  8163. +/* Timers */
  8164. +
  8165. +struct dwc_timer {
  8166. + struct timer_list t;
  8167. + char *name;
  8168. + dwc_timer_callback_t cb;
  8169. + void *data;
  8170. + uint8_t scheduled;
  8171. + dwc_spinlock_t *lock;
  8172. +};
  8173. +
  8174. +static void timer_callback(struct timer_list *tt)
  8175. +{
  8176. + dwc_timer_t *timer = from_timer(timer, tt, t);
  8177. + dwc_irqflags_t flags;
  8178. +
  8179. + DWC_SPINLOCK_IRQSAVE(timer->lock, &flags);
  8180. + timer->scheduled = 0;
  8181. + DWC_SPINUNLOCK_IRQRESTORE(timer->lock, flags);
  8182. + DWC_DEBUGC("Timer %s callback", timer->name);
  8183. + timer->cb(timer->data);
  8184. +}
  8185. +
  8186. +dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data)
  8187. +{
  8188. + dwc_timer_t *t = DWC_ALLOC(sizeof(*t));
  8189. +
  8190. + if (!t) {
  8191. + DWC_ERROR("Cannot allocate memory for timer");
  8192. + return NULL;
  8193. + }
  8194. +
  8195. + t->name = DWC_STRDUP(name);
  8196. + if (!t->name) {
  8197. + DWC_ERROR("Cannot allocate memory for timer->name");
  8198. + goto no_name;
  8199. + }
  8200. +
  8201. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_SPINLOCK))
  8202. + DWC_SPINLOCK_ALLOC_LINUX_DEBUG(t->lock);
  8203. +#else
  8204. + t->lock = DWC_SPINLOCK_ALLOC();
  8205. +#endif
  8206. + if (!t->lock) {
  8207. + DWC_ERROR("Cannot allocate memory for lock");
  8208. + goto no_lock;
  8209. + }
  8210. +
  8211. + t->scheduled = 0;
  8212. + t->t.expires = jiffies;
  8213. + timer_setup(&t->t, timer_callback, 0);
  8214. +
  8215. + t->cb = cb;
  8216. + t->data = data;
  8217. +
  8218. + return t;
  8219. +
  8220. + no_lock:
  8221. + DWC_FREE(t->name);
  8222. + no_name:
  8223. + DWC_FREE(t);
  8224. + return NULL;
  8225. +}
  8226. +
  8227. +void DWC_TIMER_FREE(dwc_timer_t *timer)
  8228. +{
  8229. + dwc_irqflags_t flags;
  8230. +
  8231. + DWC_SPINLOCK_IRQSAVE(timer->lock, &flags);
  8232. +
  8233. + if (timer->scheduled) {
  8234. + del_timer(&timer->t);
  8235. + timer->scheduled = 0;
  8236. + }
  8237. +
  8238. + DWC_SPINUNLOCK_IRQRESTORE(timer->lock, flags);
  8239. + DWC_SPINLOCK_FREE(timer->lock);
  8240. + DWC_FREE(timer->name);
  8241. + DWC_FREE(timer);
  8242. +}
  8243. +
  8244. +void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time)
  8245. +{
  8246. + dwc_irqflags_t flags;
  8247. +
  8248. + DWC_SPINLOCK_IRQSAVE(timer->lock, &flags);
  8249. +
  8250. + if (!timer->scheduled) {
  8251. + timer->scheduled = 1;
  8252. + DWC_DEBUGC("Scheduling timer %s to expire in +%d msec", timer->name, time);
  8253. + timer->t.expires = jiffies + msecs_to_jiffies(time);
  8254. + add_timer(&timer->t);
  8255. + } else {
  8256. + DWC_DEBUGC("Modifying timer %s to expire in +%d msec", timer->name, time);
  8257. + mod_timer(&timer->t, jiffies + msecs_to_jiffies(time));
  8258. + }
  8259. +
  8260. + DWC_SPINUNLOCK_IRQRESTORE(timer->lock, flags);
  8261. +}
  8262. +
  8263. +void DWC_TIMER_CANCEL(dwc_timer_t *timer)
  8264. +{
  8265. + del_timer(&timer->t);
  8266. +}
  8267. +
  8268. +
  8269. +/* Wait Queues */
  8270. +
  8271. +struct dwc_waitq {
  8272. + wait_queue_head_t queue;
  8273. + int abort;
  8274. +};
  8275. +
  8276. +dwc_waitq_t *DWC_WAITQ_ALLOC(void)
  8277. +{
  8278. + dwc_waitq_t *wq = DWC_ALLOC(sizeof(*wq));
  8279. +
  8280. + if (!wq) {
  8281. + DWC_ERROR("Cannot allocate memory for waitqueue\n");
  8282. + return NULL;
  8283. + }
  8284. +
  8285. + init_waitqueue_head(&wq->queue);
  8286. + wq->abort = 0;
  8287. + return wq;
  8288. +}
  8289. +
  8290. +void DWC_WAITQ_FREE(dwc_waitq_t *wq)
  8291. +{
  8292. + DWC_FREE(wq);
  8293. +}
  8294. +
  8295. +int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data)
  8296. +{
  8297. + int result = wait_event_interruptible(wq->queue,
  8298. + cond(data) || wq->abort);
  8299. + if (result == -ERESTARTSYS) {
  8300. + wq->abort = 0;
  8301. + return -DWC_E_RESTART;
  8302. + }
  8303. +
  8304. + if (wq->abort == 1) {
  8305. + wq->abort = 0;
  8306. + return -DWC_E_ABORT;
  8307. + }
  8308. +
  8309. + wq->abort = 0;
  8310. +
  8311. + if (result == 0) {
  8312. + return 0;
  8313. + }
  8314. +
  8315. + return -DWC_E_UNKNOWN;
  8316. +}
  8317. +
  8318. +int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
  8319. + void *data, int32_t msecs)
  8320. +{
  8321. + int32_t tmsecs;
  8322. + int result = wait_event_interruptible_timeout(wq->queue,
  8323. + cond(data) || wq->abort,
  8324. + msecs_to_jiffies(msecs));
  8325. + if (result == -ERESTARTSYS) {
  8326. + wq->abort = 0;
  8327. + return -DWC_E_RESTART;
  8328. + }
  8329. +
  8330. + if (wq->abort == 1) {
  8331. + wq->abort = 0;
  8332. + return -DWC_E_ABORT;
  8333. + }
  8334. +
  8335. + wq->abort = 0;
  8336. +
  8337. + if (result > 0) {
  8338. + tmsecs = jiffies_to_msecs(result);
  8339. + if (!tmsecs) {
  8340. + return 1;
  8341. + }
  8342. +
  8343. + return tmsecs;
  8344. + }
  8345. +
  8346. + if (result == 0) {
  8347. + return -DWC_E_TIMEOUT;
  8348. + }
  8349. +
  8350. + return -DWC_E_UNKNOWN;
  8351. +}
  8352. +
  8353. +void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq)
  8354. +{
  8355. + wq->abort = 0;
  8356. + wake_up_interruptible(&wq->queue);
  8357. +}
  8358. +
  8359. +void DWC_WAITQ_ABORT(dwc_waitq_t *wq)
  8360. +{
  8361. + wq->abort = 1;
  8362. + wake_up_interruptible(&wq->queue);
  8363. +}
  8364. +
  8365. +
  8366. +/* Threading */
  8367. +
  8368. +dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data)
  8369. +{
  8370. + struct task_struct *thread = kthread_run(func, data, name);
  8371. +
  8372. + if (thread == ERR_PTR(-ENOMEM)) {
  8373. + return NULL;
  8374. + }
  8375. +
  8376. + return (dwc_thread_t *)thread;
  8377. +}
  8378. +
  8379. +int DWC_THREAD_STOP(dwc_thread_t *thread)
  8380. +{
  8381. + return kthread_stop((struct task_struct *)thread);
  8382. +}
  8383. +
  8384. +dwc_bool_t DWC_THREAD_SHOULD_STOP(void)
  8385. +{
  8386. + return kthread_should_stop();
  8387. +}
  8388. +
  8389. +
  8390. +/* tasklets
  8391. + - run in interrupt context (cannot sleep)
  8392. + - each tasklet runs on a single CPU
  8393. + - different tasklets can be running simultaneously on different CPUs
  8394. + */
  8395. +struct dwc_tasklet {
  8396. + struct tasklet_struct t;
  8397. + dwc_tasklet_callback_t cb;
  8398. + void *data;
  8399. +};
  8400. +
  8401. +static void tasklet_callback(unsigned long data)
  8402. +{
  8403. + dwc_tasklet_t *t = (dwc_tasklet_t *)data;
  8404. + t->cb(t->data);
  8405. +}
  8406. +
  8407. +dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data)
  8408. +{
  8409. + dwc_tasklet_t *t = DWC_ALLOC(sizeof(*t));
  8410. +
  8411. + if (t) {
  8412. + t->cb = cb;
  8413. + t->data = data;
  8414. + tasklet_init(&t->t, tasklet_callback, (unsigned long)t);
  8415. + } else {
  8416. + DWC_ERROR("Cannot allocate memory for tasklet\n");
  8417. + }
  8418. +
  8419. + return t;
  8420. +}
  8421. +
  8422. +void DWC_TASK_FREE(dwc_tasklet_t *task)
  8423. +{
  8424. + DWC_FREE(task);
  8425. +}
  8426. +
  8427. +void DWC_TASK_SCHEDULE(dwc_tasklet_t *task)
  8428. +{
  8429. + tasklet_schedule(&task->t);
  8430. +}
  8431. +
  8432. +void DWC_TASK_HI_SCHEDULE(dwc_tasklet_t *task)
  8433. +{
  8434. + tasklet_hi_schedule(&task->t);
  8435. +}
  8436. +
  8437. +
  8438. +/* workqueues
  8439. + - run in process context (can sleep)
  8440. + */
  8441. +typedef struct work_container {
  8442. + dwc_work_callback_t cb;
  8443. + void *data;
  8444. + dwc_workq_t *wq;
  8445. + char *name;
  8446. +
  8447. +#ifdef DEBUG
  8448. + DWC_CIRCLEQ_ENTRY(work_container) entry;
  8449. +#endif
  8450. + struct delayed_work work;
  8451. +} work_container_t;
  8452. +
  8453. +#ifdef DEBUG
  8454. +DWC_CIRCLEQ_HEAD(work_container_queue, work_container);
  8455. +#endif
  8456. +
  8457. +struct dwc_workq {
  8458. + struct workqueue_struct *wq;
  8459. + dwc_spinlock_t *lock;
  8460. + dwc_waitq_t *waitq;
  8461. + int pending;
  8462. +
  8463. +#ifdef DEBUG
  8464. + struct work_container_queue entries;
  8465. +#endif
  8466. +};
  8467. +
  8468. +static void do_work(struct work_struct *work)
  8469. +{
  8470. + dwc_irqflags_t flags;
  8471. + struct delayed_work *dw = container_of(work, struct delayed_work, work);
  8472. + work_container_t *container = container_of(dw, struct work_container, work);
  8473. + dwc_workq_t *wq = container->wq;
  8474. +
  8475. + container->cb(container->data);
  8476. +
  8477. +#ifdef DEBUG
  8478. + DWC_CIRCLEQ_REMOVE(&wq->entries, container, entry);
  8479. +#endif
  8480. + DWC_DEBUGC("Work done: %s, container=%p", container->name, container);
  8481. + if (container->name) {
  8482. + DWC_FREE(container->name);
  8483. + }
  8484. + DWC_FREE(container);
  8485. +
  8486. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  8487. + wq->pending--;
  8488. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  8489. + DWC_WAITQ_TRIGGER(wq->waitq);
  8490. +}
  8491. +
  8492. +static int work_done(void *data)
  8493. +{
  8494. + dwc_workq_t *workq = (dwc_workq_t *)data;
  8495. + return workq->pending == 0;
  8496. +}
  8497. +
  8498. +int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout)
  8499. +{
  8500. + return DWC_WAITQ_WAIT_TIMEOUT(workq->waitq, work_done, workq, timeout);
  8501. +}
  8502. +
  8503. +dwc_workq_t *DWC_WORKQ_ALLOC(char *name)
  8504. +{
  8505. + dwc_workq_t *wq = DWC_ALLOC(sizeof(*wq));
  8506. +
  8507. + if (!wq) {
  8508. + return NULL;
  8509. + }
  8510. +
  8511. + wq->wq = create_singlethread_workqueue(name);
  8512. + if (!wq->wq) {
  8513. + goto no_wq;
  8514. + }
  8515. +
  8516. + wq->pending = 0;
  8517. +
  8518. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_SPINLOCK))
  8519. + DWC_SPINLOCK_ALLOC_LINUX_DEBUG(wq->lock);
  8520. +#else
  8521. + wq->lock = DWC_SPINLOCK_ALLOC();
  8522. +#endif
  8523. + if (!wq->lock) {
  8524. + goto no_lock;
  8525. + }
  8526. +
  8527. + wq->waitq = DWC_WAITQ_ALLOC();
  8528. + if (!wq->waitq) {
  8529. + goto no_waitq;
  8530. + }
  8531. +
  8532. +#ifdef DEBUG
  8533. + DWC_CIRCLEQ_INIT(&wq->entries);
  8534. +#endif
  8535. + return wq;
  8536. +
  8537. + no_waitq:
  8538. + DWC_SPINLOCK_FREE(wq->lock);
  8539. + no_lock:
  8540. + destroy_workqueue(wq->wq);
  8541. + no_wq:
  8542. + DWC_FREE(wq);
  8543. +
  8544. + return NULL;
  8545. +}
  8546. +
  8547. +void DWC_WORKQ_FREE(dwc_workq_t *wq)
  8548. +{
  8549. +#ifdef DEBUG
  8550. + if (wq->pending != 0) {
  8551. + struct work_container *wc;
  8552. + DWC_ERROR("Destroying work queue with pending work");
  8553. + DWC_CIRCLEQ_FOREACH(wc, &wq->entries, entry) {
  8554. + DWC_ERROR("Work %s still pending", wc->name);
  8555. + }
  8556. + }
  8557. +#endif
  8558. + destroy_workqueue(wq->wq);
  8559. + DWC_SPINLOCK_FREE(wq->lock);
  8560. + DWC_WAITQ_FREE(wq->waitq);
  8561. + DWC_FREE(wq);
  8562. +}
  8563. +
  8564. +void DWC_WORKQ_SCHEDULE(dwc_workq_t *wq, dwc_work_callback_t cb, void *data,
  8565. + char *format, ...)
  8566. +{
  8567. + dwc_irqflags_t flags;
  8568. + work_container_t *container;
  8569. + static char name[128];
  8570. + va_list args;
  8571. +
  8572. + va_start(args, format);
  8573. + DWC_VSNPRINTF(name, 128, format, args);
  8574. + va_end(args);
  8575. +
  8576. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  8577. + wq->pending++;
  8578. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  8579. + DWC_WAITQ_TRIGGER(wq->waitq);
  8580. +
  8581. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  8582. + if (!container) {
  8583. + DWC_ERROR("Cannot allocate memory for container\n");
  8584. + return;
  8585. + }
  8586. +
  8587. + container->name = DWC_STRDUP(name);
  8588. + if (!container->name) {
  8589. + DWC_ERROR("Cannot allocate memory for container->name\n");
  8590. + DWC_FREE(container);
  8591. + return;
  8592. + }
  8593. +
  8594. + container->cb = cb;
  8595. + container->data = data;
  8596. + container->wq = wq;
  8597. + DWC_DEBUGC("Queueing work: %s, container=%p", container->name, container);
  8598. + INIT_WORK(&container->work.work, do_work);
  8599. +
  8600. +#ifdef DEBUG
  8601. + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
  8602. +#endif
  8603. + queue_work(wq->wq, &container->work.work);
  8604. +}
  8605. +
  8606. +void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *wq, dwc_work_callback_t cb,
  8607. + void *data, uint32_t time, char *format, ...)
  8608. +{
  8609. + dwc_irqflags_t flags;
  8610. + work_container_t *container;
  8611. + static char name[128];
  8612. + va_list args;
  8613. +
  8614. + va_start(args, format);
  8615. + DWC_VSNPRINTF(name, 128, format, args);
  8616. + va_end(args);
  8617. +
  8618. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  8619. + wq->pending++;
  8620. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  8621. + DWC_WAITQ_TRIGGER(wq->waitq);
  8622. +
  8623. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  8624. + if (!container) {
  8625. + DWC_ERROR("Cannot allocate memory for container\n");
  8626. + return;
  8627. + }
  8628. +
  8629. + container->name = DWC_STRDUP(name);
  8630. + if (!container->name) {
  8631. + DWC_ERROR("Cannot allocate memory for container->name\n");
  8632. + DWC_FREE(container);
  8633. + return;
  8634. + }
  8635. +
  8636. + container->cb = cb;
  8637. + container->data = data;
  8638. + container->wq = wq;
  8639. + DWC_DEBUGC("Queueing work: %s, container=%p", container->name, container);
  8640. + INIT_DELAYED_WORK(&container->work, do_work);
  8641. +
  8642. +#ifdef DEBUG
  8643. + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
  8644. +#endif
  8645. + queue_delayed_work(wq->wq, &container->work, msecs_to_jiffies(time));
  8646. +}
  8647. +
  8648. +int DWC_WORKQ_PENDING(dwc_workq_t *wq)
  8649. +{
  8650. + return wq->pending;
  8651. +}
  8652. +
  8653. +
  8654. +#ifdef DWC_LIBMODULE
  8655. +
  8656. +#ifdef DWC_CCLIB
  8657. +/* CC */
  8658. +EXPORT_SYMBOL(dwc_cc_if_alloc);
  8659. +EXPORT_SYMBOL(dwc_cc_if_free);
  8660. +EXPORT_SYMBOL(dwc_cc_clear);
  8661. +EXPORT_SYMBOL(dwc_cc_add);
  8662. +EXPORT_SYMBOL(dwc_cc_remove);
  8663. +EXPORT_SYMBOL(dwc_cc_change);
  8664. +EXPORT_SYMBOL(dwc_cc_data_for_save);
  8665. +EXPORT_SYMBOL(dwc_cc_restore_from_data);
  8666. +EXPORT_SYMBOL(dwc_cc_match_chid);
  8667. +EXPORT_SYMBOL(dwc_cc_match_cdid);
  8668. +EXPORT_SYMBOL(dwc_cc_ck);
  8669. +EXPORT_SYMBOL(dwc_cc_chid);
  8670. +EXPORT_SYMBOL(dwc_cc_cdid);
  8671. +EXPORT_SYMBOL(dwc_cc_name);
  8672. +#endif /* DWC_CCLIB */
  8673. +
  8674. +#ifdef DWC_CRYPTOLIB
  8675. +# ifndef CONFIG_MACH_IPMATE
  8676. +/* Modpow */
  8677. +EXPORT_SYMBOL(dwc_modpow);
  8678. +
  8679. +/* DH */
  8680. +EXPORT_SYMBOL(dwc_dh_modpow);
  8681. +EXPORT_SYMBOL(dwc_dh_derive_keys);
  8682. +EXPORT_SYMBOL(dwc_dh_pk);
  8683. +# endif /* CONFIG_MACH_IPMATE */
  8684. +
  8685. +/* Crypto */
  8686. +EXPORT_SYMBOL(dwc_wusb_aes_encrypt);
  8687. +EXPORT_SYMBOL(dwc_wusb_cmf);
  8688. +EXPORT_SYMBOL(dwc_wusb_prf);
  8689. +EXPORT_SYMBOL(dwc_wusb_fill_ccm_nonce);
  8690. +EXPORT_SYMBOL(dwc_wusb_gen_nonce);
  8691. +EXPORT_SYMBOL(dwc_wusb_gen_key);
  8692. +EXPORT_SYMBOL(dwc_wusb_gen_mic);
  8693. +#endif /* DWC_CRYPTOLIB */
  8694. +
  8695. +/* Notification */
  8696. +#ifdef DWC_NOTIFYLIB
  8697. +EXPORT_SYMBOL(dwc_alloc_notification_manager);
  8698. +EXPORT_SYMBOL(dwc_free_notification_manager);
  8699. +EXPORT_SYMBOL(dwc_register_notifier);
  8700. +EXPORT_SYMBOL(dwc_unregister_notifier);
  8701. +EXPORT_SYMBOL(dwc_add_observer);
  8702. +EXPORT_SYMBOL(dwc_remove_observer);
  8703. +EXPORT_SYMBOL(dwc_notify);
  8704. +#endif
  8705. +
  8706. +/* Memory Debugging Routines */
  8707. +#ifdef DWC_DEBUG_MEMORY
  8708. +EXPORT_SYMBOL(dwc_alloc_debug);
  8709. +EXPORT_SYMBOL(dwc_alloc_atomic_debug);
  8710. +EXPORT_SYMBOL(dwc_free_debug);
  8711. +EXPORT_SYMBOL(dwc_dma_alloc_debug);
  8712. +EXPORT_SYMBOL(dwc_dma_free_debug);
  8713. +#endif
  8714. +
  8715. +EXPORT_SYMBOL(DWC_MEMSET);
  8716. +EXPORT_SYMBOL(DWC_MEMCPY);
  8717. +EXPORT_SYMBOL(DWC_MEMMOVE);
  8718. +EXPORT_SYMBOL(DWC_MEMCMP);
  8719. +EXPORT_SYMBOL(DWC_STRNCMP);
  8720. +EXPORT_SYMBOL(DWC_STRCMP);
  8721. +EXPORT_SYMBOL(DWC_STRLEN);
  8722. +EXPORT_SYMBOL(DWC_STRCPY);
  8723. +EXPORT_SYMBOL(DWC_STRDUP);
  8724. +EXPORT_SYMBOL(DWC_ATOI);
  8725. +EXPORT_SYMBOL(DWC_ATOUI);
  8726. +
  8727. +#ifdef DWC_UTFLIB
  8728. +EXPORT_SYMBOL(DWC_UTF8_TO_UTF16LE);
  8729. +#endif /* DWC_UTFLIB */
  8730. +
  8731. +EXPORT_SYMBOL(DWC_IN_IRQ);
  8732. +EXPORT_SYMBOL(DWC_IN_BH);
  8733. +EXPORT_SYMBOL(DWC_VPRINTF);
  8734. +EXPORT_SYMBOL(DWC_VSNPRINTF);
  8735. +EXPORT_SYMBOL(DWC_PRINTF);
  8736. +EXPORT_SYMBOL(DWC_SPRINTF);
  8737. +EXPORT_SYMBOL(DWC_SNPRINTF);
  8738. +EXPORT_SYMBOL(__DWC_WARN);
  8739. +EXPORT_SYMBOL(__DWC_ERROR);
  8740. +EXPORT_SYMBOL(DWC_EXCEPTION);
  8741. +
  8742. +#ifdef DEBUG
  8743. +EXPORT_SYMBOL(__DWC_DEBUG);
  8744. +#endif
  8745. +
  8746. +EXPORT_SYMBOL(__DWC_DMA_ALLOC);
  8747. +EXPORT_SYMBOL(__DWC_DMA_ALLOC_ATOMIC);
  8748. +EXPORT_SYMBOL(__DWC_DMA_FREE);
  8749. +EXPORT_SYMBOL(__DWC_ALLOC);
  8750. +EXPORT_SYMBOL(__DWC_ALLOC_ATOMIC);
  8751. +EXPORT_SYMBOL(__DWC_FREE);
  8752. +
  8753. +#ifdef DWC_CRYPTOLIB
  8754. +EXPORT_SYMBOL(DWC_RANDOM_BYTES);
  8755. +EXPORT_SYMBOL(DWC_AES_CBC);
  8756. +EXPORT_SYMBOL(DWC_SHA256);
  8757. +EXPORT_SYMBOL(DWC_HMAC_SHA256);
  8758. +#endif
  8759. +
  8760. +EXPORT_SYMBOL(DWC_CPU_TO_LE32);
  8761. +EXPORT_SYMBOL(DWC_CPU_TO_BE32);
  8762. +EXPORT_SYMBOL(DWC_LE32_TO_CPU);
  8763. +EXPORT_SYMBOL(DWC_BE32_TO_CPU);
  8764. +EXPORT_SYMBOL(DWC_CPU_TO_LE16);
  8765. +EXPORT_SYMBOL(DWC_CPU_TO_BE16);
  8766. +EXPORT_SYMBOL(DWC_LE16_TO_CPU);
  8767. +EXPORT_SYMBOL(DWC_BE16_TO_CPU);
  8768. +EXPORT_SYMBOL(DWC_READ_REG32);
  8769. +EXPORT_SYMBOL(DWC_WRITE_REG32);
  8770. +EXPORT_SYMBOL(DWC_MODIFY_REG32);
  8771. +
  8772. +#if 0
  8773. +EXPORT_SYMBOL(DWC_READ_REG64);
  8774. +EXPORT_SYMBOL(DWC_WRITE_REG64);
  8775. +EXPORT_SYMBOL(DWC_MODIFY_REG64);
  8776. +#endif
  8777. +
  8778. +EXPORT_SYMBOL(DWC_SPINLOCK_ALLOC);
  8779. +EXPORT_SYMBOL(DWC_SPINLOCK_FREE);
  8780. +EXPORT_SYMBOL(DWC_SPINLOCK);
  8781. +EXPORT_SYMBOL(DWC_SPINUNLOCK);
  8782. +EXPORT_SYMBOL(DWC_SPINLOCK_IRQSAVE);
  8783. +EXPORT_SYMBOL(DWC_SPINUNLOCK_IRQRESTORE);
  8784. +EXPORT_SYMBOL(DWC_MUTEX_ALLOC);
  8785. +
  8786. +#if (!defined(DWC_LINUX) || !defined(CONFIG_DEBUG_MUTEXES))
  8787. +EXPORT_SYMBOL(DWC_MUTEX_FREE);
  8788. +#endif
  8789. +
  8790. +EXPORT_SYMBOL(DWC_MUTEX_LOCK);
  8791. +EXPORT_SYMBOL(DWC_MUTEX_TRYLOCK);
  8792. +EXPORT_SYMBOL(DWC_MUTEX_UNLOCK);
  8793. +EXPORT_SYMBOL(DWC_UDELAY);
  8794. +EXPORT_SYMBOL(DWC_MDELAY);
  8795. +EXPORT_SYMBOL(DWC_MSLEEP);
  8796. +EXPORT_SYMBOL(DWC_TIME);
  8797. +EXPORT_SYMBOL(DWC_TIMER_ALLOC);
  8798. +EXPORT_SYMBOL(DWC_TIMER_FREE);
  8799. +EXPORT_SYMBOL(DWC_TIMER_SCHEDULE);
  8800. +EXPORT_SYMBOL(DWC_TIMER_CANCEL);
  8801. +EXPORT_SYMBOL(DWC_WAITQ_ALLOC);
  8802. +EXPORT_SYMBOL(DWC_WAITQ_FREE);
  8803. +EXPORT_SYMBOL(DWC_WAITQ_WAIT);
  8804. +EXPORT_SYMBOL(DWC_WAITQ_WAIT_TIMEOUT);
  8805. +EXPORT_SYMBOL(DWC_WAITQ_TRIGGER);
  8806. +EXPORT_SYMBOL(DWC_WAITQ_ABORT);
  8807. +EXPORT_SYMBOL(DWC_THREAD_RUN);
  8808. +EXPORT_SYMBOL(DWC_THREAD_STOP);
  8809. +EXPORT_SYMBOL(DWC_THREAD_SHOULD_STOP);
  8810. +EXPORT_SYMBOL(DWC_TASK_ALLOC);
  8811. +EXPORT_SYMBOL(DWC_TASK_FREE);
  8812. +EXPORT_SYMBOL(DWC_TASK_SCHEDULE);
  8813. +EXPORT_SYMBOL(DWC_WORKQ_WAIT_WORK_DONE);
  8814. +EXPORT_SYMBOL(DWC_WORKQ_ALLOC);
  8815. +EXPORT_SYMBOL(DWC_WORKQ_FREE);
  8816. +EXPORT_SYMBOL(DWC_WORKQ_SCHEDULE);
  8817. +EXPORT_SYMBOL(DWC_WORKQ_SCHEDULE_DELAYED);
  8818. +EXPORT_SYMBOL(DWC_WORKQ_PENDING);
  8819. +
  8820. +static int dwc_common_port_init_module(void)
  8821. +{
  8822. + int result = 0;
  8823. +
  8824. + printk(KERN_DEBUG "Module dwc_common_port init\n" );
  8825. +
  8826. +#ifdef DWC_DEBUG_MEMORY
  8827. + result = dwc_memory_debug_start(NULL);
  8828. + if (result) {
  8829. + printk(KERN_ERR
  8830. + "dwc_memory_debug_start() failed with error %d\n",
  8831. + result);
  8832. + return result;
  8833. + }
  8834. +#endif
  8835. +
  8836. +#ifdef DWC_NOTIFYLIB
  8837. + result = dwc_alloc_notification_manager(NULL, NULL);
  8838. + if (result) {
  8839. + printk(KERN_ERR
  8840. + "dwc_alloc_notification_manager() failed with error %d\n",
  8841. + result);
  8842. + return result;
  8843. + }
  8844. +#endif
  8845. + return result;
  8846. +}
  8847. +
  8848. +static void dwc_common_port_exit_module(void)
  8849. +{
  8850. + printk(KERN_DEBUG "Module dwc_common_port exit\n" );
  8851. +
  8852. +#ifdef DWC_NOTIFYLIB
  8853. + dwc_free_notification_manager();
  8854. +#endif
  8855. +
  8856. +#ifdef DWC_DEBUG_MEMORY
  8857. + dwc_memory_debug_stop();
  8858. +#endif
  8859. +}
  8860. +
  8861. +module_init(dwc_common_port_init_module);
  8862. +module_exit(dwc_common_port_exit_module);
  8863. +
  8864. +MODULE_DESCRIPTION("DWC Common Library - Portable version");
  8865. +MODULE_AUTHOR("Synopsys Inc.");
  8866. +MODULE_LICENSE ("GPL");
  8867. +
  8868. +#endif /* DWC_LIBMODULE */
  8869. --- /dev/null
  8870. +++ b/drivers/usb/host/dwc_common_port/dwc_common_nbsd.c
  8871. @@ -0,0 +1,1275 @@
  8872. +#include "dwc_os.h"
  8873. +#include "dwc_list.h"
  8874. +
  8875. +#ifdef DWC_CCLIB
  8876. +# include "dwc_cc.h"
  8877. +#endif
  8878. +
  8879. +#ifdef DWC_CRYPTOLIB
  8880. +# include "dwc_modpow.h"
  8881. +# include "dwc_dh.h"
  8882. +# include "dwc_crypto.h"
  8883. +#endif
  8884. +
  8885. +#ifdef DWC_NOTIFYLIB
  8886. +# include "dwc_notifier.h"
  8887. +#endif
  8888. +
  8889. +/* OS-Level Implementations */
  8890. +
  8891. +/* This is the NetBSD 4.0.1 kernel implementation of the DWC platform library. */
  8892. +
  8893. +
  8894. +/* MISC */
  8895. +
  8896. +void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size)
  8897. +{
  8898. + return memset(dest, byte, size);
  8899. +}
  8900. +
  8901. +void *DWC_MEMCPY(void *dest, void const *src, uint32_t size)
  8902. +{
  8903. + return memcpy(dest, src, size);
  8904. +}
  8905. +
  8906. +void *DWC_MEMMOVE(void *dest, void *src, uint32_t size)
  8907. +{
  8908. + bcopy(src, dest, size);
  8909. + return dest;
  8910. +}
  8911. +
  8912. +int DWC_MEMCMP(void *m1, void *m2, uint32_t size)
  8913. +{
  8914. + return memcmp(m1, m2, size);
  8915. +}
  8916. +
  8917. +int DWC_STRNCMP(void *s1, void *s2, uint32_t size)
  8918. +{
  8919. + return strncmp(s1, s2, size);
  8920. +}
  8921. +
  8922. +int DWC_STRCMP(void *s1, void *s2)
  8923. +{
  8924. + return strcmp(s1, s2);
  8925. +}
  8926. +
  8927. +int DWC_STRLEN(char const *str)
  8928. +{
  8929. + return strlen(str);
  8930. +}
  8931. +
  8932. +char *DWC_STRCPY(char *to, char const *from)
  8933. +{
  8934. + return strcpy(to, from);
  8935. +}
  8936. +
  8937. +char *DWC_STRDUP(char const *str)
  8938. +{
  8939. + int len = DWC_STRLEN(str) + 1;
  8940. + char *new = DWC_ALLOC_ATOMIC(len);
  8941. +
  8942. + if (!new) {
  8943. + return NULL;
  8944. + }
  8945. +
  8946. + DWC_MEMCPY(new, str, len);
  8947. + return new;
  8948. +}
  8949. +
  8950. +int DWC_ATOI(char *str, int32_t *value)
  8951. +{
  8952. + char *end = NULL;
  8953. +
  8954. + /* NetBSD doesn't have 'strtol' in the kernel, but 'strtoul'
  8955. + * should be equivalent on 2's complement machines
  8956. + */
  8957. + *value = strtoul(str, &end, 0);
  8958. + if (*end == '\0') {
  8959. + return 0;
  8960. + }
  8961. +
  8962. + return -1;
  8963. +}
  8964. +
  8965. +int DWC_ATOUI(char *str, uint32_t *value)
  8966. +{
  8967. + char *end = NULL;
  8968. +
  8969. + *value = strtoul(str, &end, 0);
  8970. + if (*end == '\0') {
  8971. + return 0;
  8972. + }
  8973. +
  8974. + return -1;
  8975. +}
  8976. +
  8977. +
  8978. +#ifdef DWC_UTFLIB
  8979. +/* From usbstring.c */
  8980. +
  8981. +int DWC_UTF8_TO_UTF16LE(uint8_t const *s, uint16_t *cp, unsigned len)
  8982. +{
  8983. + int count = 0;
  8984. + u8 c;
  8985. + u16 uchar;
  8986. +
  8987. + /* this insists on correct encodings, though not minimal ones.
  8988. + * BUT it currently rejects legit 4-byte UTF-8 code points,
  8989. + * which need surrogate pairs. (Unicode 3.1 can use them.)
  8990. + */
  8991. + while (len != 0 && (c = (u8) *s++) != 0) {
  8992. + if (unlikely(c & 0x80)) {
  8993. + // 2-byte sequence:
  8994. + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
  8995. + if ((c & 0xe0) == 0xc0) {
  8996. + uchar = (c & 0x1f) << 6;
  8997. +
  8998. + c = (u8) *s++;
  8999. + if ((c & 0xc0) != 0xc0)
  9000. + goto fail;
  9001. + c &= 0x3f;
  9002. + uchar |= c;
  9003. +
  9004. + // 3-byte sequence (most CJKV characters):
  9005. + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
  9006. + } else if ((c & 0xf0) == 0xe0) {
  9007. + uchar = (c & 0x0f) << 12;
  9008. +
  9009. + c = (u8) *s++;
  9010. + if ((c & 0xc0) != 0xc0)
  9011. + goto fail;
  9012. + c &= 0x3f;
  9013. + uchar |= c << 6;
  9014. +
  9015. + c = (u8) *s++;
  9016. + if ((c & 0xc0) != 0xc0)
  9017. + goto fail;
  9018. + c &= 0x3f;
  9019. + uchar |= c;
  9020. +
  9021. + /* no bogus surrogates */
  9022. + if (0xd800 <= uchar && uchar <= 0xdfff)
  9023. + goto fail;
  9024. +
  9025. + // 4-byte sequence (surrogate pairs, currently rare):
  9026. + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
  9027. + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
  9028. + // (uuuuu = wwww + 1)
  9029. + // FIXME accept the surrogate code points (only)
  9030. + } else
  9031. + goto fail;
  9032. + } else
  9033. + uchar = c;
  9034. + put_unaligned (cpu_to_le16 (uchar), cp++);
  9035. + count++;
  9036. + len--;
  9037. + }
  9038. + return count;
  9039. +fail:
  9040. + return -1;
  9041. +}
  9042. +
  9043. +#endif /* DWC_UTFLIB */
  9044. +
  9045. +
  9046. +/* dwc_debug.h */
  9047. +
  9048. +dwc_bool_t DWC_IN_IRQ(void)
  9049. +{
  9050. +// return in_irq();
  9051. + return 0;
  9052. +}
  9053. +
  9054. +dwc_bool_t DWC_IN_BH(void)
  9055. +{
  9056. +// return in_softirq();
  9057. + return 0;
  9058. +}
  9059. +
  9060. +void DWC_VPRINTF(char *format, va_list args)
  9061. +{
  9062. + vprintf(format, args);
  9063. +}
  9064. +
  9065. +int DWC_VSNPRINTF(char *str, int size, char *format, va_list args)
  9066. +{
  9067. + return vsnprintf(str, size, format, args);
  9068. +}
  9069. +
  9070. +void DWC_PRINTF(char *format, ...)
  9071. +{
  9072. + va_list args;
  9073. +
  9074. + va_start(args, format);
  9075. + DWC_VPRINTF(format, args);
  9076. + va_end(args);
  9077. +}
  9078. +
  9079. +int DWC_SPRINTF(char *buffer, char *format, ...)
  9080. +{
  9081. + int retval;
  9082. + va_list args;
  9083. +
  9084. + va_start(args, format);
  9085. + retval = vsprintf(buffer, format, args);
  9086. + va_end(args);
  9087. + return retval;
  9088. +}
  9089. +
  9090. +int DWC_SNPRINTF(char *buffer, int size, char *format, ...)
  9091. +{
  9092. + int retval;
  9093. + va_list args;
  9094. +
  9095. + va_start(args, format);
  9096. + retval = vsnprintf(buffer, size, format, args);
  9097. + va_end(args);
  9098. + return retval;
  9099. +}
  9100. +
  9101. +void __DWC_WARN(char *format, ...)
  9102. +{
  9103. + va_list args;
  9104. +
  9105. + va_start(args, format);
  9106. + DWC_VPRINTF(format, args);
  9107. + va_end(args);
  9108. +}
  9109. +
  9110. +void __DWC_ERROR(char *format, ...)
  9111. +{
  9112. + va_list args;
  9113. +
  9114. + va_start(args, format);
  9115. + DWC_VPRINTF(format, args);
  9116. + va_end(args);
  9117. +}
  9118. +
  9119. +void DWC_EXCEPTION(char *format, ...)
  9120. +{
  9121. + va_list args;
  9122. +
  9123. + va_start(args, format);
  9124. + DWC_VPRINTF(format, args);
  9125. + va_end(args);
  9126. +// BUG_ON(1); ???
  9127. +}
  9128. +
  9129. +#ifdef DEBUG
  9130. +void __DWC_DEBUG(char *format, ...)
  9131. +{
  9132. + va_list args;
  9133. +
  9134. + va_start(args, format);
  9135. + DWC_VPRINTF(format, args);
  9136. + va_end(args);
  9137. +}
  9138. +#endif
  9139. +
  9140. +
  9141. +/* dwc_mem.h */
  9142. +
  9143. +#if 0
  9144. +dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size,
  9145. + uint32_t align,
  9146. + uint32_t alloc)
  9147. +{
  9148. + struct dma_pool *pool = dma_pool_create("Pool", NULL,
  9149. + size, align, alloc);
  9150. + return (dwc_pool_t *)pool;
  9151. +}
  9152. +
  9153. +void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool)
  9154. +{
  9155. + dma_pool_destroy((struct dma_pool *)pool);
  9156. +}
  9157. +
  9158. +void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  9159. +{
  9160. +// return dma_pool_alloc((struct dma_pool *)pool, GFP_KERNEL, dma_addr);
  9161. + return dma_pool_alloc((struct dma_pool *)pool, M_WAITOK, dma_addr);
  9162. +}
  9163. +
  9164. +void *DWC_DMA_POOL_ZALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  9165. +{
  9166. + void *vaddr = DWC_DMA_POOL_ALLOC(pool, dma_addr);
  9167. + memset(..);
  9168. +}
  9169. +
  9170. +void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr)
  9171. +{
  9172. + dma_pool_free(pool, vaddr, daddr);
  9173. +}
  9174. +#endif
  9175. +
  9176. +void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
  9177. +{
  9178. + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
  9179. + int error;
  9180. +
  9181. + error = bus_dmamem_alloc(dma->dma_tag, size, 1, size, dma->segs,
  9182. + sizeof(dma->segs) / sizeof(dma->segs[0]),
  9183. + &dma->nsegs, BUS_DMA_NOWAIT);
  9184. + if (error) {
  9185. + printf("%s: bus_dmamem_alloc(%ju) failed: %d\n", __func__,
  9186. + (uintmax_t)size, error);
  9187. + goto fail_0;
  9188. + }
  9189. +
  9190. + error = bus_dmamem_map(dma->dma_tag, dma->segs, dma->nsegs, size,
  9191. + (caddr_t *)&dma->dma_vaddr,
  9192. + BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
  9193. + if (error) {
  9194. + printf("%s: bus_dmamem_map failed: %d\n", __func__, error);
  9195. + goto fail_1;
  9196. + }
  9197. +
  9198. + error = bus_dmamap_create(dma->dma_tag, size, 1, size, 0,
  9199. + BUS_DMA_NOWAIT, &dma->dma_map);
  9200. + if (error) {
  9201. + printf("%s: bus_dmamap_create failed: %d\n", __func__, error);
  9202. + goto fail_2;
  9203. + }
  9204. +
  9205. + error = bus_dmamap_load(dma->dma_tag, dma->dma_map, dma->dma_vaddr,
  9206. + size, NULL, BUS_DMA_NOWAIT);
  9207. + if (error) {
  9208. + printf("%s: bus_dmamap_load failed: %d\n", __func__, error);
  9209. + goto fail_3;
  9210. + }
  9211. +
  9212. + dma->dma_paddr = (bus_addr_t)dma->segs[0].ds_addr;
  9213. + *dma_addr = dma->dma_paddr;
  9214. + return dma->dma_vaddr;
  9215. +
  9216. +fail_3:
  9217. + bus_dmamap_destroy(dma->dma_tag, dma->dma_map);
  9218. +fail_2:
  9219. + bus_dmamem_unmap(dma->dma_tag, dma->dma_vaddr, size);
  9220. +fail_1:
  9221. + bus_dmamem_free(dma->dma_tag, dma->segs, dma->nsegs);
  9222. +fail_0:
  9223. + dma->dma_map = NULL;
  9224. + dma->dma_vaddr = NULL;
  9225. + dma->nsegs = 0;
  9226. +
  9227. + return NULL;
  9228. +}
  9229. +
  9230. +void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr)
  9231. +{
  9232. + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
  9233. +
  9234. + if (dma->dma_map != NULL) {
  9235. + bus_dmamap_sync(dma->dma_tag, dma->dma_map, 0, size,
  9236. + BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
  9237. + bus_dmamap_unload(dma->dma_tag, dma->dma_map);
  9238. + bus_dmamap_destroy(dma->dma_tag, dma->dma_map);
  9239. + bus_dmamem_unmap(dma->dma_tag, dma->dma_vaddr, size);
  9240. + bus_dmamem_free(dma->dma_tag, dma->segs, dma->nsegs);
  9241. + dma->dma_paddr = 0;
  9242. + dma->dma_map = NULL;
  9243. + dma->dma_vaddr = NULL;
  9244. + dma->nsegs = 0;
  9245. + }
  9246. +}
  9247. +
  9248. +void *__DWC_ALLOC(void *mem_ctx, uint32_t size)
  9249. +{
  9250. + return malloc(size, M_DEVBUF, M_WAITOK | M_ZERO);
  9251. +}
  9252. +
  9253. +void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size)
  9254. +{
  9255. + return malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
  9256. +}
  9257. +
  9258. +void __DWC_FREE(void *mem_ctx, void *addr)
  9259. +{
  9260. + free(addr, M_DEVBUF);
  9261. +}
  9262. +
  9263. +
  9264. +#ifdef DWC_CRYPTOLIB
  9265. +/* dwc_crypto.h */
  9266. +
  9267. +void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length)
  9268. +{
  9269. + get_random_bytes(buffer, length);
  9270. +}
  9271. +
  9272. +int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out)
  9273. +{
  9274. + struct crypto_blkcipher *tfm;
  9275. + struct blkcipher_desc desc;
  9276. + struct scatterlist sgd;
  9277. + struct scatterlist sgs;
  9278. +
  9279. + tfm = crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
  9280. + if (tfm == NULL) {
  9281. + printk("failed to load transform for aes CBC\n");
  9282. + return -1;
  9283. + }
  9284. +
  9285. + crypto_blkcipher_setkey(tfm, key, keylen);
  9286. + crypto_blkcipher_set_iv(tfm, iv, 16);
  9287. +
  9288. + sg_init_one(&sgd, out, messagelen);
  9289. + sg_init_one(&sgs, message, messagelen);
  9290. +
  9291. + desc.tfm = tfm;
  9292. + desc.flags = 0;
  9293. +
  9294. + if (crypto_blkcipher_encrypt(&desc, &sgd, &sgs, messagelen)) {
  9295. + crypto_free_blkcipher(tfm);
  9296. + DWC_ERROR("AES CBC encryption failed");
  9297. + return -1;
  9298. + }
  9299. +
  9300. + crypto_free_blkcipher(tfm);
  9301. + return 0;
  9302. +}
  9303. +
  9304. +int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out)
  9305. +{
  9306. + struct crypto_hash *tfm;
  9307. + struct hash_desc desc;
  9308. + struct scatterlist sg;
  9309. +
  9310. + tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
  9311. + if (IS_ERR(tfm)) {
  9312. + DWC_ERROR("Failed to load transform for sha256: %ld", PTR_ERR(tfm));
  9313. + return 0;
  9314. + }
  9315. + desc.tfm = tfm;
  9316. + desc.flags = 0;
  9317. +
  9318. + sg_init_one(&sg, message, len);
  9319. + crypto_hash_digest(&desc, &sg, len, out);
  9320. + crypto_free_hash(tfm);
  9321. +
  9322. + return 1;
  9323. +}
  9324. +
  9325. +int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen,
  9326. + uint8_t *key, uint32_t keylen, uint8_t *out)
  9327. +{
  9328. + struct crypto_hash *tfm;
  9329. + struct hash_desc desc;
  9330. + struct scatterlist sg;
  9331. +
  9332. + tfm = crypto_alloc_hash("hmac(sha256)", 0, CRYPTO_ALG_ASYNC);
  9333. + if (IS_ERR(tfm)) {
  9334. + DWC_ERROR("Failed to load transform for hmac(sha256): %ld", PTR_ERR(tfm));
  9335. + return 0;
  9336. + }
  9337. + desc.tfm = tfm;
  9338. + desc.flags = 0;
  9339. +
  9340. + sg_init_one(&sg, message, messagelen);
  9341. + crypto_hash_setkey(tfm, key, keylen);
  9342. + crypto_hash_digest(&desc, &sg, messagelen, out);
  9343. + crypto_free_hash(tfm);
  9344. +
  9345. + return 1;
  9346. +}
  9347. +
  9348. +#endif /* DWC_CRYPTOLIB */
  9349. +
  9350. +
  9351. +/* Byte Ordering Conversions */
  9352. +
  9353. +uint32_t DWC_CPU_TO_LE32(uint32_t *p)
  9354. +{
  9355. +#ifdef __LITTLE_ENDIAN
  9356. + return *p;
  9357. +#else
  9358. + uint8_t *u_p = (uint8_t *)p;
  9359. +
  9360. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  9361. +#endif
  9362. +}
  9363. +
  9364. +uint32_t DWC_CPU_TO_BE32(uint32_t *p)
  9365. +{
  9366. +#ifdef __BIG_ENDIAN
  9367. + return *p;
  9368. +#else
  9369. + uint8_t *u_p = (uint8_t *)p;
  9370. +
  9371. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  9372. +#endif
  9373. +}
  9374. +
  9375. +uint32_t DWC_LE32_TO_CPU(uint32_t *p)
  9376. +{
  9377. +#ifdef __LITTLE_ENDIAN
  9378. + return *p;
  9379. +#else
  9380. + uint8_t *u_p = (uint8_t *)p;
  9381. +
  9382. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  9383. +#endif
  9384. +}
  9385. +
  9386. +uint32_t DWC_BE32_TO_CPU(uint32_t *p)
  9387. +{
  9388. +#ifdef __BIG_ENDIAN
  9389. + return *p;
  9390. +#else
  9391. + uint8_t *u_p = (uint8_t *)p;
  9392. +
  9393. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  9394. +#endif
  9395. +}
  9396. +
  9397. +uint16_t DWC_CPU_TO_LE16(uint16_t *p)
  9398. +{
  9399. +#ifdef __LITTLE_ENDIAN
  9400. + return *p;
  9401. +#else
  9402. + uint8_t *u_p = (uint8_t *)p;
  9403. + return (u_p[1] | (u_p[0] << 8));
  9404. +#endif
  9405. +}
  9406. +
  9407. +uint16_t DWC_CPU_TO_BE16(uint16_t *p)
  9408. +{
  9409. +#ifdef __BIG_ENDIAN
  9410. + return *p;
  9411. +#else
  9412. + uint8_t *u_p = (uint8_t *)p;
  9413. + return (u_p[1] | (u_p[0] << 8));
  9414. +#endif
  9415. +}
  9416. +
  9417. +uint16_t DWC_LE16_TO_CPU(uint16_t *p)
  9418. +{
  9419. +#ifdef __LITTLE_ENDIAN
  9420. + return *p;
  9421. +#else
  9422. + uint8_t *u_p = (uint8_t *)p;
  9423. + return (u_p[1] | (u_p[0] << 8));
  9424. +#endif
  9425. +}
  9426. +
  9427. +uint16_t DWC_BE16_TO_CPU(uint16_t *p)
  9428. +{
  9429. +#ifdef __BIG_ENDIAN
  9430. + return *p;
  9431. +#else
  9432. + uint8_t *u_p = (uint8_t *)p;
  9433. + return (u_p[1] | (u_p[0] << 8));
  9434. +#endif
  9435. +}
  9436. +
  9437. +
  9438. +/* Registers */
  9439. +
  9440. +uint32_t DWC_READ_REG32(void *io_ctx, uint32_t volatile *reg)
  9441. +{
  9442. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  9443. + bus_size_t ior = (bus_size_t)reg;
  9444. +
  9445. + return bus_space_read_4(io->iot, io->ioh, ior);
  9446. +}
  9447. +
  9448. +#if 0
  9449. +uint64_t DWC_READ_REG64(void *io_ctx, uint64_t volatile *reg)
  9450. +{
  9451. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  9452. + bus_size_t ior = (bus_size_t)reg;
  9453. +
  9454. + return bus_space_read_8(io->iot, io->ioh, ior);
  9455. +}
  9456. +#endif
  9457. +
  9458. +void DWC_WRITE_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t value)
  9459. +{
  9460. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  9461. + bus_size_t ior = (bus_size_t)reg;
  9462. +
  9463. + bus_space_write_4(io->iot, io->ioh, ior, value);
  9464. +}
  9465. +
  9466. +#if 0
  9467. +void DWC_WRITE_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t value)
  9468. +{
  9469. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  9470. + bus_size_t ior = (bus_size_t)reg;
  9471. +
  9472. + bus_space_write_8(io->iot, io->ioh, ior, value);
  9473. +}
  9474. +#endif
  9475. +
  9476. +void DWC_MODIFY_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t clear_mask,
  9477. + uint32_t set_mask)
  9478. +{
  9479. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  9480. + bus_size_t ior = (bus_size_t)reg;
  9481. +
  9482. + bus_space_write_4(io->iot, io->ioh, ior,
  9483. + (bus_space_read_4(io->iot, io->ioh, ior) &
  9484. + ~clear_mask) | set_mask);
  9485. +}
  9486. +
  9487. +#if 0
  9488. +void DWC_MODIFY_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t clear_mask,
  9489. + uint64_t set_mask)
  9490. +{
  9491. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  9492. + bus_size_t ior = (bus_size_t)reg;
  9493. +
  9494. + bus_space_write_8(io->iot, io->ioh, ior,
  9495. + (bus_space_read_8(io->iot, io->ioh, ior) &
  9496. + ~clear_mask) | set_mask);
  9497. +}
  9498. +#endif
  9499. +
  9500. +
  9501. +/* Locking */
  9502. +
  9503. +dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void)
  9504. +{
  9505. + struct simplelock *sl = DWC_ALLOC(sizeof(*sl));
  9506. +
  9507. + if (!sl) {
  9508. + DWC_ERROR("Cannot allocate memory for spinlock");
  9509. + return NULL;
  9510. + }
  9511. +
  9512. + simple_lock_init(sl);
  9513. + return (dwc_spinlock_t *)sl;
  9514. +}
  9515. +
  9516. +void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock)
  9517. +{
  9518. + struct simplelock *sl = (struct simplelock *)lock;
  9519. +
  9520. + DWC_FREE(sl);
  9521. +}
  9522. +
  9523. +void DWC_SPINLOCK(dwc_spinlock_t *lock)
  9524. +{
  9525. + simple_lock((struct simplelock *)lock);
  9526. +}
  9527. +
  9528. +void DWC_SPINUNLOCK(dwc_spinlock_t *lock)
  9529. +{
  9530. + simple_unlock((struct simplelock *)lock);
  9531. +}
  9532. +
  9533. +void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags)
  9534. +{
  9535. + simple_lock((struct simplelock *)lock);
  9536. + *flags = splbio();
  9537. +}
  9538. +
  9539. +void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags)
  9540. +{
  9541. + splx(flags);
  9542. + simple_unlock((struct simplelock *)lock);
  9543. +}
  9544. +
  9545. +dwc_mutex_t *DWC_MUTEX_ALLOC(void)
  9546. +{
  9547. + dwc_mutex_t *mutex = DWC_ALLOC(sizeof(struct lock));
  9548. +
  9549. + if (!mutex) {
  9550. + DWC_ERROR("Cannot allocate memory for mutex");
  9551. + return NULL;
  9552. + }
  9553. +
  9554. + lockinit((struct lock *)mutex, 0, "dw3mtx", 0, 0);
  9555. + return mutex;
  9556. +}
  9557. +
  9558. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
  9559. +#else
  9560. +void DWC_MUTEX_FREE(dwc_mutex_t *mutex)
  9561. +{
  9562. + DWC_FREE(mutex);
  9563. +}
  9564. +#endif
  9565. +
  9566. +void DWC_MUTEX_LOCK(dwc_mutex_t *mutex)
  9567. +{
  9568. + lockmgr((struct lock *)mutex, LK_EXCLUSIVE, NULL);
  9569. +}
  9570. +
  9571. +int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex)
  9572. +{
  9573. + int status;
  9574. +
  9575. + status = lockmgr((struct lock *)mutex, LK_EXCLUSIVE | LK_NOWAIT, NULL);
  9576. + return status == 0;
  9577. +}
  9578. +
  9579. +void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex)
  9580. +{
  9581. + lockmgr((struct lock *)mutex, LK_RELEASE, NULL);
  9582. +}
  9583. +
  9584. +
  9585. +/* Timing */
  9586. +
  9587. +void DWC_UDELAY(uint32_t usecs)
  9588. +{
  9589. + DELAY(usecs);
  9590. +}
  9591. +
  9592. +void DWC_MDELAY(uint32_t msecs)
  9593. +{
  9594. + do {
  9595. + DELAY(1000);
  9596. + } while (--msecs);
  9597. +}
  9598. +
  9599. +void DWC_MSLEEP(uint32_t msecs)
  9600. +{
  9601. + struct timeval tv;
  9602. +
  9603. + tv.tv_sec = msecs / 1000;
  9604. + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
  9605. + tsleep(&tv, 0, "dw3slp", tvtohz(&tv));
  9606. +}
  9607. +
  9608. +uint32_t DWC_TIME(void)
  9609. +{
  9610. + struct timeval tv;
  9611. +
  9612. + microuptime(&tv); // or getmicrouptime? (less precise, but faster)
  9613. + return tv.tv_sec * 1000 + tv.tv_usec / 1000;
  9614. +}
  9615. +
  9616. +
  9617. +/* Timers */
  9618. +
  9619. +struct dwc_timer {
  9620. + struct callout t;
  9621. + char *name;
  9622. + dwc_spinlock_t *lock;
  9623. + dwc_timer_callback_t cb;
  9624. + void *data;
  9625. +};
  9626. +
  9627. +dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data)
  9628. +{
  9629. + dwc_timer_t *t = DWC_ALLOC(sizeof(*t));
  9630. +
  9631. + if (!t) {
  9632. + DWC_ERROR("Cannot allocate memory for timer");
  9633. + return NULL;
  9634. + }
  9635. +
  9636. + callout_init(&t->t);
  9637. +
  9638. + t->name = DWC_STRDUP(name);
  9639. + if (!t->name) {
  9640. + DWC_ERROR("Cannot allocate memory for timer->name");
  9641. + goto no_name;
  9642. + }
  9643. +
  9644. + t->lock = DWC_SPINLOCK_ALLOC();
  9645. + if (!t->lock) {
  9646. + DWC_ERROR("Cannot allocate memory for timer->lock");
  9647. + goto no_lock;
  9648. + }
  9649. +
  9650. + t->cb = cb;
  9651. + t->data = data;
  9652. +
  9653. + return t;
  9654. +
  9655. + no_lock:
  9656. + DWC_FREE(t->name);
  9657. + no_name:
  9658. + DWC_FREE(t);
  9659. +
  9660. + return NULL;
  9661. +}
  9662. +
  9663. +void DWC_TIMER_FREE(dwc_timer_t *timer)
  9664. +{
  9665. + callout_stop(&timer->t);
  9666. + DWC_SPINLOCK_FREE(timer->lock);
  9667. + DWC_FREE(timer->name);
  9668. + DWC_FREE(timer);
  9669. +}
  9670. +
  9671. +void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time)
  9672. +{
  9673. + struct timeval tv;
  9674. +
  9675. + tv.tv_sec = time / 1000;
  9676. + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
  9677. + callout_reset(&timer->t, tvtohz(&tv), timer->cb, timer->data);
  9678. +}
  9679. +
  9680. +void DWC_TIMER_CANCEL(dwc_timer_t *timer)
  9681. +{
  9682. + callout_stop(&timer->t);
  9683. +}
  9684. +
  9685. +
  9686. +/* Wait Queues */
  9687. +
  9688. +struct dwc_waitq {
  9689. + struct simplelock lock;
  9690. + int abort;
  9691. +};
  9692. +
  9693. +dwc_waitq_t *DWC_WAITQ_ALLOC(void)
  9694. +{
  9695. + dwc_waitq_t *wq = DWC_ALLOC(sizeof(*wq));
  9696. +
  9697. + if (!wq) {
  9698. + DWC_ERROR("Cannot allocate memory for waitqueue");
  9699. + return NULL;
  9700. + }
  9701. +
  9702. + simple_lock_init(&wq->lock);
  9703. + wq->abort = 0;
  9704. +
  9705. + return wq;
  9706. +}
  9707. +
  9708. +void DWC_WAITQ_FREE(dwc_waitq_t *wq)
  9709. +{
  9710. + DWC_FREE(wq);
  9711. +}
  9712. +
  9713. +int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data)
  9714. +{
  9715. + int ipl;
  9716. + int result = 0;
  9717. +
  9718. + simple_lock(&wq->lock);
  9719. + ipl = splbio();
  9720. +
  9721. + /* Skip the sleep if already aborted or triggered */
  9722. + if (!wq->abort && !cond(data)) {
  9723. + splx(ipl);
  9724. + result = ltsleep(wq, PCATCH, "dw3wat", 0, &wq->lock); // infinite timeout
  9725. + ipl = splbio();
  9726. + }
  9727. +
  9728. + if (result == 0) { // awoken
  9729. + if (wq->abort) {
  9730. + wq->abort = 0;
  9731. + result = -DWC_E_ABORT;
  9732. + } else {
  9733. + result = 0;
  9734. + }
  9735. +
  9736. + splx(ipl);
  9737. + simple_unlock(&wq->lock);
  9738. + } else {
  9739. + wq->abort = 0;
  9740. + splx(ipl);
  9741. + simple_unlock(&wq->lock);
  9742. +
  9743. + if (result == ERESTART) { // signaled - restart
  9744. + result = -DWC_E_RESTART;
  9745. + } else { // signaled - must be EINTR
  9746. + result = -DWC_E_ABORT;
  9747. + }
  9748. + }
  9749. +
  9750. + return result;
  9751. +}
  9752. +
  9753. +int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
  9754. + void *data, int32_t msecs)
  9755. +{
  9756. + struct timeval tv, tv1, tv2;
  9757. + int ipl;
  9758. + int result = 0;
  9759. +
  9760. + tv.tv_sec = msecs / 1000;
  9761. + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
  9762. +
  9763. + simple_lock(&wq->lock);
  9764. + ipl = splbio();
  9765. +
  9766. + /* Skip the sleep if already aborted or triggered */
  9767. + if (!wq->abort && !cond(data)) {
  9768. + splx(ipl);
  9769. + getmicrouptime(&tv1);
  9770. + result = ltsleep(wq, PCATCH, "dw3wto", tvtohz(&tv), &wq->lock);
  9771. + getmicrouptime(&tv2);
  9772. + ipl = splbio();
  9773. + }
  9774. +
  9775. + if (result == 0) { // awoken
  9776. + if (wq->abort) {
  9777. + wq->abort = 0;
  9778. + splx(ipl);
  9779. + simple_unlock(&wq->lock);
  9780. + result = -DWC_E_ABORT;
  9781. + } else {
  9782. + splx(ipl);
  9783. + simple_unlock(&wq->lock);
  9784. +
  9785. + tv2.tv_usec -= tv1.tv_usec;
  9786. + if (tv2.tv_usec < 0) {
  9787. + tv2.tv_usec += 1000000;
  9788. + tv2.tv_sec--;
  9789. + }
  9790. +
  9791. + tv2.tv_sec -= tv1.tv_sec;
  9792. + result = tv2.tv_sec * 1000 + tv2.tv_usec / 1000;
  9793. + result = msecs - result;
  9794. + if (result <= 0)
  9795. + result = 1;
  9796. + }
  9797. + } else {
  9798. + wq->abort = 0;
  9799. + splx(ipl);
  9800. + simple_unlock(&wq->lock);
  9801. +
  9802. + if (result == ERESTART) { // signaled - restart
  9803. + result = -DWC_E_RESTART;
  9804. +
  9805. + } else if (result == EINTR) { // signaled - interrupt
  9806. + result = -DWC_E_ABORT;
  9807. +
  9808. + } else { // timed out
  9809. + result = -DWC_E_TIMEOUT;
  9810. + }
  9811. + }
  9812. +
  9813. + return result;
  9814. +}
  9815. +
  9816. +void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq)
  9817. +{
  9818. + wakeup(wq);
  9819. +}
  9820. +
  9821. +void DWC_WAITQ_ABORT(dwc_waitq_t *wq)
  9822. +{
  9823. + int ipl;
  9824. +
  9825. + simple_lock(&wq->lock);
  9826. + ipl = splbio();
  9827. + wq->abort = 1;
  9828. + wakeup(wq);
  9829. + splx(ipl);
  9830. + simple_unlock(&wq->lock);
  9831. +}
  9832. +
  9833. +
  9834. +/* Threading */
  9835. +
  9836. +struct dwc_thread {
  9837. + struct proc *proc;
  9838. + int abort;
  9839. +};
  9840. +
  9841. +dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data)
  9842. +{
  9843. + int retval;
  9844. + dwc_thread_t *thread = DWC_ALLOC(sizeof(*thread));
  9845. +
  9846. + if (!thread) {
  9847. + return NULL;
  9848. + }
  9849. +
  9850. + thread->abort = 0;
  9851. + retval = kthread_create1((void (*)(void *))func, data, &thread->proc,
  9852. + "%s", name);
  9853. + if (retval) {
  9854. + DWC_FREE(thread);
  9855. + return NULL;
  9856. + }
  9857. +
  9858. + return thread;
  9859. +}
  9860. +
  9861. +int DWC_THREAD_STOP(dwc_thread_t *thread)
  9862. +{
  9863. + int retval;
  9864. +
  9865. + thread->abort = 1;
  9866. + retval = tsleep(&thread->abort, 0, "dw3stp", 60 * hz);
  9867. +
  9868. + if (retval == 0) {
  9869. + /* DWC_THREAD_EXIT() will free the thread struct */
  9870. + return 0;
  9871. + }
  9872. +
  9873. + /* NOTE: We leak the thread struct if thread doesn't die */
  9874. +
  9875. + if (retval == EWOULDBLOCK) {
  9876. + return -DWC_E_TIMEOUT;
  9877. + }
  9878. +
  9879. + return -DWC_E_UNKNOWN;
  9880. +}
  9881. +
  9882. +dwc_bool_t DWC_THREAD_SHOULD_STOP(dwc_thread_t *thread)
  9883. +{
  9884. + return thread->abort;
  9885. +}
  9886. +
  9887. +void DWC_THREAD_EXIT(dwc_thread_t *thread)
  9888. +{
  9889. + wakeup(&thread->abort);
  9890. + DWC_FREE(thread);
  9891. + kthread_exit(0);
  9892. +}
  9893. +
  9894. +/* tasklets
  9895. + - Runs in interrupt context (cannot sleep)
  9896. + - Each tasklet runs on a single CPU
  9897. + - Different tasklets can be running simultaneously on different CPUs
  9898. + [ On NetBSD there is no corresponding mechanism, drivers don't have bottom-
  9899. + halves. So we just call the callback directly from DWC_TASK_SCHEDULE() ]
  9900. + */
  9901. +struct dwc_tasklet {
  9902. + dwc_tasklet_callback_t cb;
  9903. + void *data;
  9904. +};
  9905. +
  9906. +static void tasklet_callback(void *data)
  9907. +{
  9908. + dwc_tasklet_t *task = (dwc_tasklet_t *)data;
  9909. +
  9910. + task->cb(task->data);
  9911. +}
  9912. +
  9913. +dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data)
  9914. +{
  9915. + dwc_tasklet_t *task = DWC_ALLOC(sizeof(*task));
  9916. +
  9917. + if (task) {
  9918. + task->cb = cb;
  9919. + task->data = data;
  9920. + } else {
  9921. + DWC_ERROR("Cannot allocate memory for tasklet");
  9922. + }
  9923. +
  9924. + return task;
  9925. +}
  9926. +
  9927. +void DWC_TASK_FREE(dwc_tasklet_t *task)
  9928. +{
  9929. + DWC_FREE(task);
  9930. +}
  9931. +
  9932. +void DWC_TASK_SCHEDULE(dwc_tasklet_t *task)
  9933. +{
  9934. + tasklet_callback(task);
  9935. +}
  9936. +
  9937. +
  9938. +/* workqueues
  9939. + - Runs in process context (can sleep)
  9940. + */
  9941. +typedef struct work_container {
  9942. + dwc_work_callback_t cb;
  9943. + void *data;
  9944. + dwc_workq_t *wq;
  9945. + char *name;
  9946. + int hz;
  9947. + struct work task;
  9948. +} work_container_t;
  9949. +
  9950. +struct dwc_workq {
  9951. + struct workqueue *taskq;
  9952. + dwc_spinlock_t *lock;
  9953. + dwc_waitq_t *waitq;
  9954. + int pending;
  9955. + struct work_container *container;
  9956. +};
  9957. +
  9958. +static void do_work(struct work *task, void *data)
  9959. +{
  9960. + dwc_workq_t *wq = (dwc_workq_t *)data;
  9961. + work_container_t *container = wq->container;
  9962. + dwc_irqflags_t flags;
  9963. +
  9964. + if (container->hz) {
  9965. + tsleep(container, 0, "dw3wrk", container->hz);
  9966. + }
  9967. +
  9968. + container->cb(container->data);
  9969. + DWC_DEBUG("Work done: %s, container=%p", container->name, container);
  9970. +
  9971. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  9972. + if (container->name)
  9973. + DWC_FREE(container->name);
  9974. + DWC_FREE(container);
  9975. + wq->pending--;
  9976. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  9977. + DWC_WAITQ_TRIGGER(wq->waitq);
  9978. +}
  9979. +
  9980. +static int work_done(void *data)
  9981. +{
  9982. + dwc_workq_t *workq = (dwc_workq_t *)data;
  9983. +
  9984. + return workq->pending == 0;
  9985. +}
  9986. +
  9987. +int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout)
  9988. +{
  9989. + return DWC_WAITQ_WAIT_TIMEOUT(workq->waitq, work_done, workq, timeout);
  9990. +}
  9991. +
  9992. +dwc_workq_t *DWC_WORKQ_ALLOC(char *name)
  9993. +{
  9994. + int result;
  9995. + dwc_workq_t *wq = DWC_ALLOC(sizeof(*wq));
  9996. +
  9997. + if (!wq) {
  9998. + DWC_ERROR("Cannot allocate memory for workqueue");
  9999. + return NULL;
  10000. + }
  10001. +
  10002. + result = workqueue_create(&wq->taskq, name, do_work, wq, 0 /*PWAIT*/,
  10003. + IPL_BIO, 0);
  10004. + if (result) {
  10005. + DWC_ERROR("Cannot create workqueue");
  10006. + goto no_taskq;
  10007. + }
  10008. +
  10009. + wq->pending = 0;
  10010. +
  10011. + wq->lock = DWC_SPINLOCK_ALLOC();
  10012. + if (!wq->lock) {
  10013. + DWC_ERROR("Cannot allocate memory for spinlock");
  10014. + goto no_lock;
  10015. + }
  10016. +
  10017. + wq->waitq = DWC_WAITQ_ALLOC();
  10018. + if (!wq->waitq) {
  10019. + DWC_ERROR("Cannot allocate memory for waitqueue");
  10020. + goto no_waitq;
  10021. + }
  10022. +
  10023. + return wq;
  10024. +
  10025. + no_waitq:
  10026. + DWC_SPINLOCK_FREE(wq->lock);
  10027. + no_lock:
  10028. + workqueue_destroy(wq->taskq);
  10029. + no_taskq:
  10030. + DWC_FREE(wq);
  10031. +
  10032. + return NULL;
  10033. +}
  10034. +
  10035. +void DWC_WORKQ_FREE(dwc_workq_t *wq)
  10036. +{
  10037. +#ifdef DEBUG
  10038. + dwc_irqflags_t flags;
  10039. +
  10040. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  10041. +
  10042. + if (wq->pending != 0) {
  10043. + struct work_container *container = wq->container;
  10044. +
  10045. + DWC_ERROR("Destroying work queue with pending work");
  10046. +
  10047. + if (container && container->name) {
  10048. + DWC_ERROR("Work %s still pending", container->name);
  10049. + }
  10050. + }
  10051. +
  10052. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  10053. +#endif
  10054. + DWC_WAITQ_FREE(wq->waitq);
  10055. + DWC_SPINLOCK_FREE(wq->lock);
  10056. + workqueue_destroy(wq->taskq);
  10057. + DWC_FREE(wq);
  10058. +}
  10059. +
  10060. +void DWC_WORKQ_SCHEDULE(dwc_workq_t *wq, dwc_work_callback_t cb, void *data,
  10061. + char *format, ...)
  10062. +{
  10063. + dwc_irqflags_t flags;
  10064. + work_container_t *container;
  10065. + static char name[128];
  10066. + va_list args;
  10067. +
  10068. + va_start(args, format);
  10069. + DWC_VSNPRINTF(name, 128, format, args);
  10070. + va_end(args);
  10071. +
  10072. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  10073. + wq->pending++;
  10074. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  10075. + DWC_WAITQ_TRIGGER(wq->waitq);
  10076. +
  10077. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  10078. + if (!container) {
  10079. + DWC_ERROR("Cannot allocate memory for container");
  10080. + return;
  10081. + }
  10082. +
  10083. + container->name = DWC_STRDUP(name);
  10084. + if (!container->name) {
  10085. + DWC_ERROR("Cannot allocate memory for container->name");
  10086. + DWC_FREE(container);
  10087. + return;
  10088. + }
  10089. +
  10090. + container->cb = cb;
  10091. + container->data = data;
  10092. + container->wq = wq;
  10093. + container->hz = 0;
  10094. + wq->container = container;
  10095. +
  10096. + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
  10097. + workqueue_enqueue(wq->taskq, &container->task);
  10098. +}
  10099. +
  10100. +void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *wq, dwc_work_callback_t cb,
  10101. + void *data, uint32_t time, char *format, ...)
  10102. +{
  10103. + dwc_irqflags_t flags;
  10104. + work_container_t *container;
  10105. + static char name[128];
  10106. + struct timeval tv;
  10107. + va_list args;
  10108. +
  10109. + va_start(args, format);
  10110. + DWC_VSNPRINTF(name, 128, format, args);
  10111. + va_end(args);
  10112. +
  10113. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  10114. + wq->pending++;
  10115. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  10116. + DWC_WAITQ_TRIGGER(wq->waitq);
  10117. +
  10118. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  10119. + if (!container) {
  10120. + DWC_ERROR("Cannot allocate memory for container");
  10121. + return;
  10122. + }
  10123. +
  10124. + container->name = DWC_STRDUP(name);
  10125. + if (!container->name) {
  10126. + DWC_ERROR("Cannot allocate memory for container->name");
  10127. + DWC_FREE(container);
  10128. + return;
  10129. + }
  10130. +
  10131. + container->cb = cb;
  10132. + container->data = data;
  10133. + container->wq = wq;
  10134. + tv.tv_sec = time / 1000;
  10135. + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
  10136. + container->hz = tvtohz(&tv);
  10137. + wq->container = container;
  10138. +
  10139. + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
  10140. + workqueue_enqueue(wq->taskq, &container->task);
  10141. +}
  10142. +
  10143. +int DWC_WORKQ_PENDING(dwc_workq_t *wq)
  10144. +{
  10145. + return wq->pending;
  10146. +}
  10147. --- /dev/null
  10148. +++ b/drivers/usb/host/dwc_common_port/dwc_crypto.c
  10149. @@ -0,0 +1,308 @@
  10150. +/* =========================================================================
  10151. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_crypto.c $
  10152. + * $Revision: #5 $
  10153. + * $Date: 2010/09/28 $
  10154. + * $Change: 1596182 $
  10155. + *
  10156. + * Synopsys Portability Library Software and documentation
  10157. + * (hereinafter, "Software") is an Unsupported proprietary work of
  10158. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  10159. + * between Synopsys and you.
  10160. + *
  10161. + * The Software IS NOT an item of Licensed Software or Licensed Product
  10162. + * under any End User Software License Agreement or Agreement for
  10163. + * Licensed Product with Synopsys or any supplement thereto. You are
  10164. + * permitted to use and redistribute this Software in source and binary
  10165. + * forms, with or without modification, provided that redistributions
  10166. + * of source code must retain this notice. You may not view, use,
  10167. + * disclose, copy or distribute this file or any information contained
  10168. + * herein except pursuant to this license grant from Synopsys. If you
  10169. + * do not agree with this notice, including the disclaimer below, then
  10170. + * you are not authorized to use the Software.
  10171. + *
  10172. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  10173. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  10174. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  10175. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  10176. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  10177. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  10178. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  10179. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  10180. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  10181. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  10182. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  10183. + * DAMAGE.
  10184. + * ========================================================================= */
  10185. +
  10186. +/** @file
  10187. + * This file contains the WUSB cryptographic routines.
  10188. + */
  10189. +
  10190. +#ifdef DWC_CRYPTOLIB
  10191. +
  10192. +#include "dwc_crypto.h"
  10193. +#include "usb.h"
  10194. +
  10195. +#ifdef DEBUG
  10196. +static inline void dump_bytes(char *name, uint8_t *bytes, int len)
  10197. +{
  10198. + int i;
  10199. + DWC_PRINTF("%s: ", name);
  10200. + for (i=0; i<len; i++) {
  10201. + DWC_PRINTF("%02x ", bytes[i]);
  10202. + }
  10203. + DWC_PRINTF("\n");
  10204. +}
  10205. +#else
  10206. +#define dump_bytes(x...)
  10207. +#endif
  10208. +
  10209. +/* Display a block */
  10210. +void show_block(const u8 *blk, const char *prefix, const char *suffix, int a)
  10211. +{
  10212. +#ifdef DWC_DEBUG_CRYPTO
  10213. + int i, blksize = 16;
  10214. +
  10215. + DWC_DEBUG("%s", prefix);
  10216. +
  10217. + if (suffix == NULL) {
  10218. + suffix = "\n";
  10219. + blksize = a;
  10220. + }
  10221. +
  10222. + for (i = 0; i < blksize; i++)
  10223. + DWC_PRINT("%02x%s", *blk++, ((i & 3) == 3) ? " " : " ");
  10224. + DWC_PRINT(suffix);
  10225. +#endif
  10226. +}
  10227. +
  10228. +/**
  10229. + * Encrypts an array of bytes using the AES encryption engine.
  10230. + * If <code>dst</code> == <code>src</code>, then the bytes will be encrypted
  10231. + * in-place.
  10232. + *
  10233. + * @return 0 on success, negative error code on error.
  10234. + */
  10235. +int dwc_wusb_aes_encrypt(u8 *src, u8 *key, u8 *dst)
  10236. +{
  10237. + u8 block_t[16];
  10238. + DWC_MEMSET(block_t, 0, 16);
  10239. +
  10240. + return DWC_AES_CBC(src, 16, key, 16, block_t, dst);
  10241. +}
  10242. +
  10243. +/**
  10244. + * The CCM-MAC-FUNCTION described in section 6.5 of the WUSB spec.
  10245. + * This function takes a data string and returns the encrypted CBC
  10246. + * Counter-mode MIC.
  10247. + *
  10248. + * @param key The 128-bit symmetric key.
  10249. + * @param nonce The CCM nonce.
  10250. + * @param label The unique 14-byte ASCII text label.
  10251. + * @param bytes The byte array to be encrypted.
  10252. + * @param len Length of the byte array.
  10253. + * @param result Byte array to receive the 8-byte encrypted MIC.
  10254. + */
  10255. +void dwc_wusb_cmf(u8 *key, u8 *nonce,
  10256. + char *label, u8 *bytes, int len, u8 *result)
  10257. +{
  10258. + u8 block_m[16];
  10259. + u8 block_x[16];
  10260. + u8 block_t[8];
  10261. + int idx, blkNum;
  10262. + u16 la = (u16)(len + 14);
  10263. +
  10264. + /* Set the AES-128 key */
  10265. + //dwc_aes_setkey(tfm, key, 16);
  10266. +
  10267. + /* Fill block B0 from flags = 0x59, N, and l(m) = 0 */
  10268. + block_m[0] = 0x59;
  10269. + for (idx = 0; idx < 13; idx++)
  10270. + block_m[idx + 1] = nonce[idx];
  10271. + block_m[14] = 0;
  10272. + block_m[15] = 0;
  10273. +
  10274. + /* Produce the CBC IV */
  10275. + dwc_wusb_aes_encrypt(block_m, key, block_x);
  10276. + show_block(block_m, "CBC IV in: ", "\n", 0);
  10277. + show_block(block_x, "CBC IV out:", "\n", 0);
  10278. +
  10279. + /* Fill block B1 from l(a) = Blen + 14, and A */
  10280. + block_x[0] ^= (u8)(la >> 8);
  10281. + block_x[1] ^= (u8)la;
  10282. + for (idx = 0; idx < 14; idx++)
  10283. + block_x[idx + 2] ^= label[idx];
  10284. + show_block(block_x, "After xor: ", "b1\n", 16);
  10285. +
  10286. + dwc_wusb_aes_encrypt(block_x, key, block_x);
  10287. + show_block(block_x, "After AES: ", "b1\n", 16);
  10288. +
  10289. + idx = 0;
  10290. + blkNum = 0;
  10291. +
  10292. + /* Fill remaining blocks with B */
  10293. + while (len-- > 0) {
  10294. + block_x[idx] ^= *bytes++;
  10295. + if (++idx >= 16) {
  10296. + idx = 0;
  10297. + show_block(block_x, "After xor: ", "\n", blkNum);
  10298. + dwc_wusb_aes_encrypt(block_x, key, block_x);
  10299. + show_block(block_x, "After AES: ", "\n", blkNum);
  10300. + blkNum++;
  10301. + }
  10302. + }
  10303. +
  10304. + /* Handle partial last block */
  10305. + if (idx > 0) {
  10306. + show_block(block_x, "After xor: ", "\n", blkNum);
  10307. + dwc_wusb_aes_encrypt(block_x, key, block_x);
  10308. + show_block(block_x, "After AES: ", "\n", blkNum);
  10309. + }
  10310. +
  10311. + /* Save the MIC tag */
  10312. + DWC_MEMCPY(block_t, block_x, 8);
  10313. + show_block(block_t, "MIC tag : ", NULL, 8);
  10314. +
  10315. + /* Fill block A0 from flags = 0x01, N, and counter = 0 */
  10316. + block_m[0] = 0x01;
  10317. + block_m[14] = 0;
  10318. + block_m[15] = 0;
  10319. +
  10320. + /* Encrypt the counter */
  10321. + dwc_wusb_aes_encrypt(block_m, key, block_x);
  10322. + show_block(block_x, "CTR[MIC] : ", NULL, 8);
  10323. +
  10324. + /* XOR with MIC tag */
  10325. + for (idx = 0; idx < 8; idx++) {
  10326. + block_t[idx] ^= block_x[idx];
  10327. + }
  10328. +
  10329. + /* Return result to caller */
  10330. + DWC_MEMCPY(result, block_t, 8);
  10331. + show_block(result, "CCM-MIC : ", NULL, 8);
  10332. +
  10333. +}
  10334. +
  10335. +/**
  10336. + * The PRF function described in section 6.5 of the WUSB spec. This function
  10337. + * concatenates MIC values returned from dwc_cmf() to create a value of
  10338. + * the requested length.
  10339. + *
  10340. + * @param prf_len Length of the PRF function in bits (64, 128, or 256).
  10341. + * @param key, nonce, label, bytes, len Same as for dwc_cmf().
  10342. + * @param result Byte array to receive the result.
  10343. + */
  10344. +void dwc_wusb_prf(int prf_len, u8 *key,
  10345. + u8 *nonce, char *label, u8 *bytes, int len, u8 *result)
  10346. +{
  10347. + int i;
  10348. +
  10349. + nonce[0] = 0;
  10350. + for (i = 0; i < prf_len >> 6; i++, nonce[0]++) {
  10351. + dwc_wusb_cmf(key, nonce, label, bytes, len, result);
  10352. + result += 8;
  10353. + }
  10354. +}
  10355. +
  10356. +/**
  10357. + * Fills in CCM Nonce per the WUSB spec.
  10358. + *
  10359. + * @param[in] haddr Host address.
  10360. + * @param[in] daddr Device address.
  10361. + * @param[in] tkid Session Key(PTK) identifier.
  10362. + * @param[out] nonce Pointer to where the CCM Nonce output is to be written.
  10363. + */
  10364. +void dwc_wusb_fill_ccm_nonce(uint16_t haddr, uint16_t daddr, uint8_t *tkid,
  10365. + uint8_t *nonce)
  10366. +{
  10367. +
  10368. + DWC_DEBUG("%s %x %x\n", __func__, daddr, haddr);
  10369. +
  10370. + DWC_MEMSET(&nonce[0], 0, 16);
  10371. +
  10372. + DWC_MEMCPY(&nonce[6], tkid, 3);
  10373. + nonce[9] = daddr & 0xFF;
  10374. + nonce[10] = (daddr >> 8) & 0xFF;
  10375. + nonce[11] = haddr & 0xFF;
  10376. + nonce[12] = (haddr >> 8) & 0xFF;
  10377. +
  10378. + dump_bytes("CCM nonce", nonce, 16);
  10379. +}
  10380. +
  10381. +/**
  10382. + * Generates a 16-byte cryptographic-grade random number for the Host/Device
  10383. + * Nonce.
  10384. + */
  10385. +void dwc_wusb_gen_nonce(uint16_t addr, uint8_t *nonce)
  10386. +{
  10387. + uint8_t inonce[16];
  10388. + uint32_t temp[4];
  10389. +
  10390. + /* Fill in the Nonce */
  10391. + DWC_MEMSET(&inonce[0], 0, sizeof(inonce));
  10392. + inonce[9] = addr & 0xFF;
  10393. + inonce[10] = (addr >> 8) & 0xFF;
  10394. + inonce[11] = inonce[9];
  10395. + inonce[12] = inonce[10];
  10396. +
  10397. + /* Collect "randomness samples" */
  10398. + DWC_RANDOM_BYTES((uint8_t *)temp, 16);
  10399. +
  10400. + dwc_wusb_prf_128((uint8_t *)temp, nonce,
  10401. + "Random Numbers", (uint8_t *)temp, sizeof(temp),
  10402. + nonce);
  10403. +}
  10404. +
  10405. +/**
  10406. + * Generates the Session Key (PTK) and Key Confirmation Key (KCK) per the
  10407. + * WUSB spec.
  10408. + *
  10409. + * @param[in] ccm_nonce Pointer to CCM Nonce.
  10410. + * @param[in] mk Master Key to derive the session from
  10411. + * @param[in] hnonce Pointer to Host Nonce.
  10412. + * @param[in] dnonce Pointer to Device Nonce.
  10413. + * @param[out] kck Pointer to where the KCK output is to be written.
  10414. + * @param[out] ptk Pointer to where the PTK output is to be written.
  10415. + */
  10416. +void dwc_wusb_gen_key(uint8_t *ccm_nonce, uint8_t *mk, uint8_t *hnonce,
  10417. + uint8_t *dnonce, uint8_t *kck, uint8_t *ptk)
  10418. +{
  10419. + uint8_t idata[32];
  10420. + uint8_t odata[32];
  10421. +
  10422. + dump_bytes("ck", mk, 16);
  10423. + dump_bytes("hnonce", hnonce, 16);
  10424. + dump_bytes("dnonce", dnonce, 16);
  10425. +
  10426. + /* The data is the HNonce and DNonce concatenated */
  10427. + DWC_MEMCPY(&idata[0], hnonce, 16);
  10428. + DWC_MEMCPY(&idata[16], dnonce, 16);
  10429. +
  10430. + dwc_wusb_prf_256(mk, ccm_nonce, "Pair-wise keys", idata, 32, odata);
  10431. +
  10432. + /* Low 16 bytes of the result is the KCK, high 16 is the PTK */
  10433. + DWC_MEMCPY(kck, &odata[0], 16);
  10434. + DWC_MEMCPY(ptk, &odata[16], 16);
  10435. +
  10436. + dump_bytes("kck", kck, 16);
  10437. + dump_bytes("ptk", ptk, 16);
  10438. +}
  10439. +
  10440. +/**
  10441. + * Generates the Message Integrity Code over the Handshake data per the
  10442. + * WUSB spec.
  10443. + *
  10444. + * @param ccm_nonce Pointer to CCM Nonce.
  10445. + * @param kck Pointer to Key Confirmation Key.
  10446. + * @param data Pointer to Handshake data to be checked.
  10447. + * @param mic Pointer to where the MIC output is to be written.
  10448. + */
  10449. +void dwc_wusb_gen_mic(uint8_t *ccm_nonce, uint8_t *kck,
  10450. + uint8_t *data, uint8_t *mic)
  10451. +{
  10452. +
  10453. + dwc_wusb_prf_64(kck, ccm_nonce, "out-of-bandMIC",
  10454. + data, WUSB_HANDSHAKE_LEN_FOR_MIC, mic);
  10455. +}
  10456. +
  10457. +#endif /* DWC_CRYPTOLIB */
  10458. --- /dev/null
  10459. +++ b/drivers/usb/host/dwc_common_port/dwc_crypto.h
  10460. @@ -0,0 +1,111 @@
  10461. +/* =========================================================================
  10462. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_crypto.h $
  10463. + * $Revision: #3 $
  10464. + * $Date: 2010/09/28 $
  10465. + * $Change: 1596182 $
  10466. + *
  10467. + * Synopsys Portability Library Software and documentation
  10468. + * (hereinafter, "Software") is an Unsupported proprietary work of
  10469. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  10470. + * between Synopsys and you.
  10471. + *
  10472. + * The Software IS NOT an item of Licensed Software or Licensed Product
  10473. + * under any End User Software License Agreement or Agreement for
  10474. + * Licensed Product with Synopsys or any supplement thereto. You are
  10475. + * permitted to use and redistribute this Software in source and binary
  10476. + * forms, with or without modification, provided that redistributions
  10477. + * of source code must retain this notice. You may not view, use,
  10478. + * disclose, copy or distribute this file or any information contained
  10479. + * herein except pursuant to this license grant from Synopsys. If you
  10480. + * do not agree with this notice, including the disclaimer below, then
  10481. + * you are not authorized to use the Software.
  10482. + *
  10483. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  10484. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  10485. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  10486. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  10487. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  10488. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  10489. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  10490. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  10491. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  10492. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  10493. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  10494. + * DAMAGE.
  10495. + * ========================================================================= */
  10496. +
  10497. +#ifndef _DWC_CRYPTO_H_
  10498. +#define _DWC_CRYPTO_H_
  10499. +
  10500. +#ifdef __cplusplus
  10501. +extern "C" {
  10502. +#endif
  10503. +
  10504. +/** @file
  10505. + *
  10506. + * This file contains declarations for the WUSB Cryptographic routines as
  10507. + * defined in the WUSB spec. They are only to be used internally by the DWC UWB
  10508. + * modules.
  10509. + */
  10510. +
  10511. +#include "dwc_os.h"
  10512. +
  10513. +int dwc_wusb_aes_encrypt(u8 *src, u8 *key, u8 *dst);
  10514. +
  10515. +void dwc_wusb_cmf(u8 *key, u8 *nonce,
  10516. + char *label, u8 *bytes, int len, u8 *result);
  10517. +void dwc_wusb_prf(int prf_len, u8 *key,
  10518. + u8 *nonce, char *label, u8 *bytes, int len, u8 *result);
  10519. +
  10520. +/**
  10521. + * The PRF-64 function described in section 6.5 of the WUSB spec.
  10522. + *
  10523. + * @param key, nonce, label, bytes, len, result Same as for dwc_prf().
  10524. + */
  10525. +static inline void dwc_wusb_prf_64(u8 *key, u8 *nonce,
  10526. + char *label, u8 *bytes, int len, u8 *result)
  10527. +{
  10528. + dwc_wusb_prf(64, key, nonce, label, bytes, len, result);
  10529. +}
  10530. +
  10531. +/**
  10532. + * The PRF-128 function described in section 6.5 of the WUSB spec.
  10533. + *
  10534. + * @param key, nonce, label, bytes, len, result Same as for dwc_prf().
  10535. + */
  10536. +static inline void dwc_wusb_prf_128(u8 *key, u8 *nonce,
  10537. + char *label, u8 *bytes, int len, u8 *result)
  10538. +{
  10539. + dwc_wusb_prf(128, key, nonce, label, bytes, len, result);
  10540. +}
  10541. +
  10542. +/**
  10543. + * The PRF-256 function described in section 6.5 of the WUSB spec.
  10544. + *
  10545. + * @param key, nonce, label, bytes, len, result Same as for dwc_prf().
  10546. + */
  10547. +static inline void dwc_wusb_prf_256(u8 *key, u8 *nonce,
  10548. + char *label, u8 *bytes, int len, u8 *result)
  10549. +{
  10550. + dwc_wusb_prf(256, key, nonce, label, bytes, len, result);
  10551. +}
  10552. +
  10553. +
  10554. +void dwc_wusb_fill_ccm_nonce(uint16_t haddr, uint16_t daddr, uint8_t *tkid,
  10555. + uint8_t *nonce);
  10556. +void dwc_wusb_gen_nonce(uint16_t addr,
  10557. + uint8_t *nonce);
  10558. +
  10559. +void dwc_wusb_gen_key(uint8_t *ccm_nonce, uint8_t *mk,
  10560. + uint8_t *hnonce, uint8_t *dnonce,
  10561. + uint8_t *kck, uint8_t *ptk);
  10562. +
  10563. +
  10564. +void dwc_wusb_gen_mic(uint8_t *ccm_nonce, uint8_t
  10565. + *kck, uint8_t *data, uint8_t *mic);
  10566. +
  10567. +#ifdef __cplusplus
  10568. +}
  10569. +#endif
  10570. +
  10571. +#endif /* _DWC_CRYPTO_H_ */
  10572. --- /dev/null
  10573. +++ b/drivers/usb/host/dwc_common_port/dwc_dh.c
  10574. @@ -0,0 +1,291 @@
  10575. +/* =========================================================================
  10576. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_dh.c $
  10577. + * $Revision: #3 $
  10578. + * $Date: 2010/09/28 $
  10579. + * $Change: 1596182 $
  10580. + *
  10581. + * Synopsys Portability Library Software and documentation
  10582. + * (hereinafter, "Software") is an Unsupported proprietary work of
  10583. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  10584. + * between Synopsys and you.
  10585. + *
  10586. + * The Software IS NOT an item of Licensed Software or Licensed Product
  10587. + * under any End User Software License Agreement or Agreement for
  10588. + * Licensed Product with Synopsys or any supplement thereto. You are
  10589. + * permitted to use and redistribute this Software in source and binary
  10590. + * forms, with or without modification, provided that redistributions
  10591. + * of source code must retain this notice. You may not view, use,
  10592. + * disclose, copy or distribute this file or any information contained
  10593. + * herein except pursuant to this license grant from Synopsys. If you
  10594. + * do not agree with this notice, including the disclaimer below, then
  10595. + * you are not authorized to use the Software.
  10596. + *
  10597. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  10598. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  10599. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  10600. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  10601. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  10602. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  10603. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  10604. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  10605. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  10606. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  10607. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  10608. + * DAMAGE.
  10609. + * ========================================================================= */
  10610. +#ifdef DWC_CRYPTOLIB
  10611. +
  10612. +#ifndef CONFIG_MACH_IPMATE
  10613. +
  10614. +#include "dwc_dh.h"
  10615. +#include "dwc_modpow.h"
  10616. +
  10617. +#ifdef DEBUG
  10618. +/* This function prints out a buffer in the format described in the Association
  10619. + * Model specification. */
  10620. +static void dh_dump(char *str, void *_num, int len)
  10621. +{
  10622. + uint8_t *num = _num;
  10623. + int i;
  10624. + DWC_PRINTF("%s\n", str);
  10625. + for (i = 0; i < len; i ++) {
  10626. + DWC_PRINTF("%02x", num[i]);
  10627. + if (((i + 1) % 2) == 0) DWC_PRINTF(" ");
  10628. + if (((i + 1) % 26) == 0) DWC_PRINTF("\n");
  10629. + }
  10630. +
  10631. + DWC_PRINTF("\n");
  10632. +}
  10633. +#else
  10634. +#define dh_dump(_x...) do {; } while(0)
  10635. +#endif
  10636. +
  10637. +/* Constant g value */
  10638. +static __u32 dh_g[] = {
  10639. + 0x02000000,
  10640. +};
  10641. +
  10642. +/* Constant p value */
  10643. +static __u32 dh_p[] = {
  10644. + 0xFFFFFFFF, 0xFFFFFFFF, 0xA2DA0FC9, 0x34C26821, 0x8B62C6C4, 0xD11CDC80, 0x084E0229, 0x74CC678A,
  10645. + 0xA6BE0B02, 0x229B133B, 0x79084A51, 0xDD04348E, 0xB31995EF, 0x1B433ACD, 0x6D0A2B30, 0x37145FF2,
  10646. + 0x6D35E14F, 0x45C2516D, 0x76B585E4, 0xC67E5E62, 0xE9424CF4, 0x6BED37A6, 0xB65CFF0B, 0xEDB706F4,
  10647. + 0xFB6B38EE, 0xA59F895A, 0x11249FAE, 0xE61F4B7C, 0x51662849, 0x3D5BE4EC, 0xB87C00C2, 0x05BF63A1,
  10648. + 0x3648DA98, 0x9AD3551C, 0xA83F1669, 0x5FCF24FD, 0x235D6583, 0x96ADA3DC, 0x56F3621C, 0xBB528520,
  10649. + 0x0729D59E, 0x6D969670, 0x4E350C67, 0x0498BC4A, 0x086C74F1, 0x7C2118CA, 0x465E9032, 0x3BCE362E,
  10650. + 0x2C779EE3, 0x03860E18, 0xA283279B, 0x8FA207EC, 0xF05DC5B5, 0xC9524C6F, 0xF6CB2BDE, 0x18175895,
  10651. + 0x7C499539, 0xE56A95EA, 0x1826D215, 0x1005FA98, 0x5A8E7215, 0x2DC4AA8A, 0x0D1733AD, 0x337A5004,
  10652. + 0xAB2155A8, 0x64BA1CDF, 0x0485FBEC, 0x0AEFDB58, 0x5771EA8A, 0x7D0C065D, 0x850F97B3, 0xC7E4E1A6,
  10653. + 0x8CAEF5AB, 0xD73309DB, 0xE0948C1E, 0x9D61254A, 0x26D2E3CE, 0x6BEED21A, 0x06FA2FF1, 0x64088AD9,
  10654. + 0x730276D8, 0x646AC83E, 0x182B1F52, 0x0C207B17, 0x5717E1BB, 0x6C5D617A, 0xC0880977, 0xE246D9BA,
  10655. + 0xA04FE208, 0x31ABE574, 0xFC5BDB43, 0x8E10FDE0, 0x20D1824B, 0xCAD23AA9, 0xFFFFFFFF, 0xFFFFFFFF,
  10656. +};
  10657. +
  10658. +static void dh_swap_bytes(void *_in, void *_out, uint32_t len)
  10659. +{
  10660. + uint8_t *in = _in;
  10661. + uint8_t *out = _out;
  10662. + int i;
  10663. + for (i=0; i<len; i++) {
  10664. + out[i] = in[len-1-i];
  10665. + }
  10666. +}
  10667. +
  10668. +/* Computes the modular exponentiation (num^exp % mod). num, exp, and mod are
  10669. + * big endian numbers of size len, in bytes. Each len value must be a multiple
  10670. + * of 4. */
  10671. +int dwc_dh_modpow(void *mem_ctx, void *num, uint32_t num_len,
  10672. + void *exp, uint32_t exp_len,
  10673. + void *mod, uint32_t mod_len,
  10674. + void *out)
  10675. +{
  10676. + /* modpow() takes little endian numbers. AM uses big-endian. This
  10677. + * function swaps bytes of numbers before passing onto modpow. */
  10678. +
  10679. + int retval = 0;
  10680. + uint32_t *result;
  10681. +
  10682. + uint32_t *bignum_num = dwc_alloc(mem_ctx, num_len + 4);
  10683. + uint32_t *bignum_exp = dwc_alloc(mem_ctx, exp_len + 4);
  10684. + uint32_t *bignum_mod = dwc_alloc(mem_ctx, mod_len + 4);
  10685. +
  10686. + dh_swap_bytes(num, &bignum_num[1], num_len);
  10687. + bignum_num[0] = num_len / 4;
  10688. +
  10689. + dh_swap_bytes(exp, &bignum_exp[1], exp_len);
  10690. + bignum_exp[0] = exp_len / 4;
  10691. +
  10692. + dh_swap_bytes(mod, &bignum_mod[1], mod_len);
  10693. + bignum_mod[0] = mod_len / 4;
  10694. +
  10695. + result = dwc_modpow(mem_ctx, bignum_num, bignum_exp, bignum_mod);
  10696. + if (!result) {
  10697. + retval = -1;
  10698. + goto dh_modpow_nomem;
  10699. + }
  10700. +
  10701. + dh_swap_bytes(&result[1], out, result[0] * 4);
  10702. + dwc_free(mem_ctx, result);
  10703. +
  10704. + dh_modpow_nomem:
  10705. + dwc_free(mem_ctx, bignum_num);
  10706. + dwc_free(mem_ctx, bignum_exp);
  10707. + dwc_free(mem_ctx, bignum_mod);
  10708. + return retval;
  10709. +}
  10710. +
  10711. +
  10712. +int dwc_dh_pk(void *mem_ctx, uint8_t nd, uint8_t *exp, uint8_t *pk, uint8_t *hash)
  10713. +{
  10714. + int retval;
  10715. + uint8_t m3[385];
  10716. +
  10717. +#ifndef DH_TEST_VECTORS
  10718. + DWC_RANDOM_BYTES(exp, 32);
  10719. +#endif
  10720. +
  10721. + /* Compute the pkd */
  10722. + if ((retval = dwc_dh_modpow(mem_ctx, dh_g, 4,
  10723. + exp, 32,
  10724. + dh_p, 384, pk))) {
  10725. + return retval;
  10726. + }
  10727. +
  10728. + m3[384] = nd;
  10729. + DWC_MEMCPY(&m3[0], pk, 384);
  10730. + DWC_SHA256(m3, 385, hash);
  10731. +
  10732. + dh_dump("PK", pk, 384);
  10733. + dh_dump("SHA-256(M3)", hash, 32);
  10734. + return 0;
  10735. +}
  10736. +
  10737. +int dwc_dh_derive_keys(void *mem_ctx, uint8_t nd, uint8_t *pkh, uint8_t *pkd,
  10738. + uint8_t *exp, int is_host,
  10739. + char *dd, uint8_t *ck, uint8_t *kdk)
  10740. +{
  10741. + int retval;
  10742. + uint8_t mv[784];
  10743. + uint8_t sha_result[32];
  10744. + uint8_t dhkey[384];
  10745. + uint8_t shared_secret[384];
  10746. + char *message;
  10747. + uint32_t vd;
  10748. +
  10749. + uint8_t *pk;
  10750. +
  10751. + if (is_host) {
  10752. + pk = pkd;
  10753. + }
  10754. + else {
  10755. + pk = pkh;
  10756. + }
  10757. +
  10758. + if ((retval = dwc_dh_modpow(mem_ctx, pk, 384,
  10759. + exp, 32,
  10760. + dh_p, 384, shared_secret))) {
  10761. + return retval;
  10762. + }
  10763. + dh_dump("Shared Secret", shared_secret, 384);
  10764. +
  10765. + DWC_SHA256(shared_secret, 384, dhkey);
  10766. + dh_dump("DHKEY", dhkey, 384);
  10767. +
  10768. + DWC_MEMCPY(&mv[0], pkd, 384);
  10769. + DWC_MEMCPY(&mv[384], pkh, 384);
  10770. + DWC_MEMCPY(&mv[768], "displayed digest", 16);
  10771. + dh_dump("MV", mv, 784);
  10772. +
  10773. + DWC_SHA256(mv, 784, sha_result);
  10774. + dh_dump("SHA-256(MV)", sha_result, 32);
  10775. + dh_dump("First 32-bits of SHA-256(MV)", sha_result, 4);
  10776. +
  10777. + dh_swap_bytes(sha_result, &vd, 4);
  10778. +#ifdef DEBUG
  10779. + DWC_PRINTF("Vd (decimal) = %d\n", vd);
  10780. +#endif
  10781. +
  10782. + switch (nd) {
  10783. + case 2:
  10784. + vd = vd % 100;
  10785. + DWC_SPRINTF(dd, "%02d", vd);
  10786. + break;
  10787. + case 3:
  10788. + vd = vd % 1000;
  10789. + DWC_SPRINTF(dd, "%03d", vd);
  10790. + break;
  10791. + case 4:
  10792. + vd = vd % 10000;
  10793. + DWC_SPRINTF(dd, "%04d", vd);
  10794. + break;
  10795. + }
  10796. +#ifdef DEBUG
  10797. + DWC_PRINTF("Display Digits: %s\n", dd);
  10798. +#endif
  10799. +
  10800. + message = "connection key";
  10801. + DWC_HMAC_SHA256(message, DWC_STRLEN(message), dhkey, 32, sha_result);
  10802. + dh_dump("HMAC(SHA-256, DHKey, connection key)", sha_result, 32);
  10803. + DWC_MEMCPY(ck, sha_result, 16);
  10804. +
  10805. + message = "key derivation key";
  10806. + DWC_HMAC_SHA256(message, DWC_STRLEN(message), dhkey, 32, sha_result);
  10807. + dh_dump("HMAC(SHA-256, DHKey, key derivation key)", sha_result, 32);
  10808. + DWC_MEMCPY(kdk, sha_result, 32);
  10809. +
  10810. + return 0;
  10811. +}
  10812. +
  10813. +
  10814. +#ifdef DH_TEST_VECTORS
  10815. +
  10816. +static __u8 dh_a[] = {
  10817. + 0x44, 0x00, 0x51, 0xd6,
  10818. + 0xf0, 0xb5, 0x5e, 0xa9,
  10819. + 0x67, 0xab, 0x31, 0xc6,
  10820. + 0x8a, 0x8b, 0x5e, 0x37,
  10821. + 0xd9, 0x10, 0xda, 0xe0,
  10822. + 0xe2, 0xd4, 0x59, 0xa4,
  10823. + 0x86, 0x45, 0x9c, 0xaa,
  10824. + 0xdf, 0x36, 0x75, 0x16,
  10825. +};
  10826. +
  10827. +static __u8 dh_b[] = {
  10828. + 0x5d, 0xae, 0xc7, 0x86,
  10829. + 0x79, 0x80, 0xa3, 0x24,
  10830. + 0x8c, 0xe3, 0x57, 0x8f,
  10831. + 0xc7, 0x5f, 0x1b, 0x0f,
  10832. + 0x2d, 0xf8, 0x9d, 0x30,
  10833. + 0x6f, 0xa4, 0x52, 0xcd,
  10834. + 0xe0, 0x7a, 0x04, 0x8a,
  10835. + 0xde, 0xd9, 0x26, 0x56,
  10836. +};
  10837. +
  10838. +void dwc_run_dh_test_vectors(void *mem_ctx)
  10839. +{
  10840. + uint8_t pkd[384];
  10841. + uint8_t pkh[384];
  10842. + uint8_t hashd[32];
  10843. + uint8_t hashh[32];
  10844. + uint8_t ck[16];
  10845. + uint8_t kdk[32];
  10846. + char dd[5];
  10847. +
  10848. + DWC_PRINTF("\n\n\nDH_TEST_VECTORS\n\n");
  10849. +
  10850. + /* compute the PKd and SHA-256(PKd || Nd) */
  10851. + DWC_PRINTF("Computing PKd\n");
  10852. + dwc_dh_pk(mem_ctx, 2, dh_a, pkd, hashd);
  10853. +
  10854. + /* compute the PKd and SHA-256(PKh || Nd) */
  10855. + DWC_PRINTF("Computing PKh\n");
  10856. + dwc_dh_pk(mem_ctx, 2, dh_b, pkh, hashh);
  10857. +
  10858. + /* compute the dhkey */
  10859. + dwc_dh_derive_keys(mem_ctx, 2, pkh, pkd, dh_a, 0, dd, ck, kdk);
  10860. +}
  10861. +#endif /* DH_TEST_VECTORS */
  10862. +
  10863. +#endif /* !CONFIG_MACH_IPMATE */
  10864. +
  10865. +#endif /* DWC_CRYPTOLIB */
  10866. --- /dev/null
  10867. +++ b/drivers/usb/host/dwc_common_port/dwc_dh.h
  10868. @@ -0,0 +1,106 @@
  10869. +/* =========================================================================
  10870. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_dh.h $
  10871. + * $Revision: #4 $
  10872. + * $Date: 2010/09/28 $
  10873. + * $Change: 1596182 $
  10874. + *
  10875. + * Synopsys Portability Library Software and documentation
  10876. + * (hereinafter, "Software") is an Unsupported proprietary work of
  10877. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  10878. + * between Synopsys and you.
  10879. + *
  10880. + * The Software IS NOT an item of Licensed Software or Licensed Product
  10881. + * under any End User Software License Agreement or Agreement for
  10882. + * Licensed Product with Synopsys or any supplement thereto. You are
  10883. + * permitted to use and redistribute this Software in source and binary
  10884. + * forms, with or without modification, provided that redistributions
  10885. + * of source code must retain this notice. You may not view, use,
  10886. + * disclose, copy or distribute this file or any information contained
  10887. + * herein except pursuant to this license grant from Synopsys. If you
  10888. + * do not agree with this notice, including the disclaimer below, then
  10889. + * you are not authorized to use the Software.
  10890. + *
  10891. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  10892. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  10893. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  10894. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  10895. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  10896. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  10897. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  10898. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  10899. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  10900. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  10901. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  10902. + * DAMAGE.
  10903. + * ========================================================================= */
  10904. +#ifndef _DWC_DH_H_
  10905. +#define _DWC_DH_H_
  10906. +
  10907. +#ifdef __cplusplus
  10908. +extern "C" {
  10909. +#endif
  10910. +
  10911. +#include "dwc_os.h"
  10912. +
  10913. +/** @file
  10914. + *
  10915. + * This file defines the common functions on device and host for performing
  10916. + * numeric association as defined in the WUSB spec. They are only to be
  10917. + * used internally by the DWC UWB modules. */
  10918. +
  10919. +extern int dwc_dh_sha256(uint8_t *message, uint32_t len, uint8_t *out);
  10920. +extern int dwc_dh_hmac_sha256(uint8_t *message, uint32_t messagelen,
  10921. + uint8_t *key, uint32_t keylen,
  10922. + uint8_t *out);
  10923. +extern int dwc_dh_modpow(void *mem_ctx, void *num, uint32_t num_len,
  10924. + void *exp, uint32_t exp_len,
  10925. + void *mod, uint32_t mod_len,
  10926. + void *out);
  10927. +
  10928. +/** Computes PKD or PKH, and SHA-256(PKd || Nd)
  10929. + *
  10930. + * PK = g^exp mod p.
  10931. + *
  10932. + * Input:
  10933. + * Nd = Number of digits on the device.
  10934. + *
  10935. + * Output:
  10936. + * exp = A 32-byte buffer to be filled with a randomly generated number.
  10937. + * used as either A or B.
  10938. + * pk = A 384-byte buffer to be filled with the PKH or PKD.
  10939. + * hash = A 32-byte buffer to be filled with SHA-256(PK || ND).
  10940. + */
  10941. +extern int dwc_dh_pk(void *mem_ctx, uint8_t nd, uint8_t *exp, uint8_t *pkd, uint8_t *hash);
  10942. +
  10943. +/** Computes the DHKEY, and VD.
  10944. + *
  10945. + * If called from host, then it will comput DHKEY=PKD^exp % p.
  10946. + * If called from device, then it will comput DHKEY=PKH^exp % p.
  10947. + *
  10948. + * Input:
  10949. + * pkd = The PKD value.
  10950. + * pkh = The PKH value.
  10951. + * exp = The A value (if device) or B value (if host) generated in dwc_wudev_dh_pk.
  10952. + * is_host = Set to non zero if a WUSB host is calling this function.
  10953. + *
  10954. + * Output:
  10955. +
  10956. + * dd = A pointer to an buffer to be set to the displayed digits string to be shown
  10957. + * to the user. This buffer should be at 5 bytes long to hold 4 digits plus a
  10958. + * null termination character. This buffer can be used directly for display.
  10959. + * ck = A 16-byte buffer to be filled with the CK.
  10960. + * kdk = A 32-byte buffer to be filled with the KDK.
  10961. + */
  10962. +extern int dwc_dh_derive_keys(void *mem_ctx, uint8_t nd, uint8_t *pkh, uint8_t *pkd,
  10963. + uint8_t *exp, int is_host,
  10964. + char *dd, uint8_t *ck, uint8_t *kdk);
  10965. +
  10966. +#ifdef DH_TEST_VECTORS
  10967. +extern void dwc_run_dh_test_vectors(void);
  10968. +#endif
  10969. +
  10970. +#ifdef __cplusplus
  10971. +}
  10972. +#endif
  10973. +
  10974. +#endif /* _DWC_DH_H_ */
  10975. --- /dev/null
  10976. +++ b/drivers/usb/host/dwc_common_port/dwc_list.h
  10977. @@ -0,0 +1,594 @@
  10978. +/* $OpenBSD: queue.h,v 1.26 2004/05/04 16:59:32 grange Exp $ */
  10979. +/* $NetBSD: queue.h,v 1.11 1996/05/16 05:17:14 mycroft Exp $ */
  10980. +
  10981. +/*
  10982. + * Copyright (c) 1991, 1993
  10983. + * The Regents of the University of California. All rights reserved.
  10984. + *
  10985. + * Redistribution and use in source and binary forms, with or without
  10986. + * modification, are permitted provided that the following conditions
  10987. + * are met:
  10988. + * 1. Redistributions of source code must retain the above copyright
  10989. + * notice, this list of conditions and the following disclaimer.
  10990. + * 2. Redistributions in binary form must reproduce the above copyright
  10991. + * notice, this list of conditions and the following disclaimer in the
  10992. + * documentation and/or other materials provided with the distribution.
  10993. + * 3. Neither the name of the University nor the names of its contributors
  10994. + * may be used to endorse or promote products derived from this software
  10995. + * without specific prior written permission.
  10996. + *
  10997. + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  10998. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  10999. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  11000. + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  11001. + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  11002. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  11003. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  11004. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  11005. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  11006. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  11007. + * SUCH DAMAGE.
  11008. + *
  11009. + * @(#)queue.h 8.5 (Berkeley) 8/20/94
  11010. + */
  11011. +
  11012. +#ifndef _DWC_LIST_H_
  11013. +#define _DWC_LIST_H_
  11014. +
  11015. +#ifdef __cplusplus
  11016. +extern "C" {
  11017. +#endif
  11018. +
  11019. +/** @file
  11020. + *
  11021. + * This file defines linked list operations. It is derived from BSD with
  11022. + * only the MACRO names being prefixed with DWC_. This is because a few of
  11023. + * these names conflict with those on Linux. For documentation on use, see the
  11024. + * inline comments in the source code. The original license for this source
  11025. + * code applies and is preserved in the dwc_list.h source file.
  11026. + */
  11027. +
  11028. +/*
  11029. + * This file defines five types of data structures: singly-linked lists,
  11030. + * lists, simple queues, tail queues, and circular queues.
  11031. + *
  11032. + *
  11033. + * A singly-linked list is headed by a single forward pointer. The elements
  11034. + * are singly linked for minimum space and pointer manipulation overhead at
  11035. + * the expense of O(n) removal for arbitrary elements. New elements can be
  11036. + * added to the list after an existing element or at the head of the list.
  11037. + * Elements being removed from the head of the list should use the explicit
  11038. + * macro for this purpose for optimum efficiency. A singly-linked list may
  11039. + * only be traversed in the forward direction. Singly-linked lists are ideal
  11040. + * for applications with large datasets and few or no removals or for
  11041. + * implementing a LIFO queue.
  11042. + *
  11043. + * A list is headed by a single forward pointer (or an array of forward
  11044. + * pointers for a hash table header). The elements are doubly linked
  11045. + * so that an arbitrary element can be removed without a need to
  11046. + * traverse the list. New elements can be added to the list before
  11047. + * or after an existing element or at the head of the list. A list
  11048. + * may only be traversed in the forward direction.
  11049. + *
  11050. + * A simple queue is headed by a pair of pointers, one the head of the
  11051. + * list and the other to the tail of the list. The elements are singly
  11052. + * linked to save space, so elements can only be removed from the
  11053. + * head of the list. New elements can be added to the list before or after
  11054. + * an existing element, at the head of the list, or at the end of the
  11055. + * list. A simple queue may only be traversed in the forward direction.
  11056. + *
  11057. + * A tail queue is headed by a pair of pointers, one to the head of the
  11058. + * list and the other to the tail of the list. The elements are doubly
  11059. + * linked so that an arbitrary element can be removed without a need to
  11060. + * traverse the list. New elements can be added to the list before or
  11061. + * after an existing element, at the head of the list, or at the end of
  11062. + * the list. A tail queue may be traversed in either direction.
  11063. + *
  11064. + * A circle queue is headed by a pair of pointers, one to the head of the
  11065. + * list and the other to the tail of the list. The elements are doubly
  11066. + * linked so that an arbitrary element can be removed without a need to
  11067. + * traverse the list. New elements can be added to the list before or after
  11068. + * an existing element, at the head of the list, or at the end of the list.
  11069. + * A circle queue may be traversed in either direction, but has a more
  11070. + * complex end of list detection.
  11071. + *
  11072. + * For details on the use of these macros, see the queue(3) manual page.
  11073. + */
  11074. +
  11075. +/*
  11076. + * Double-linked List.
  11077. + */
  11078. +
  11079. +typedef struct dwc_list_link {
  11080. + struct dwc_list_link *next;
  11081. + struct dwc_list_link *prev;
  11082. +} dwc_list_link_t;
  11083. +
  11084. +#define DWC_LIST_INIT(link) do { \
  11085. + (link)->next = (link); \
  11086. + (link)->prev = (link); \
  11087. +} while (0)
  11088. +
  11089. +#define DWC_LIST_FIRST(link) ((link)->next)
  11090. +#define DWC_LIST_LAST(link) ((link)->prev)
  11091. +#define DWC_LIST_END(link) (link)
  11092. +#define DWC_LIST_NEXT(link) ((link)->next)
  11093. +#define DWC_LIST_PREV(link) ((link)->prev)
  11094. +#define DWC_LIST_EMPTY(link) \
  11095. + (DWC_LIST_FIRST(link) == DWC_LIST_END(link))
  11096. +#define DWC_LIST_ENTRY(link, type, field) \
  11097. + (type *)((uint8_t *)(link) - (size_t)(&((type *)0)->field))
  11098. +
  11099. +#if 0
  11100. +#define DWC_LIST_INSERT_HEAD(list, link) do { \
  11101. + (link)->next = (list)->next; \
  11102. + (link)->prev = (list); \
  11103. + (list)->next->prev = (link); \
  11104. + (list)->next = (link); \
  11105. +} while (0)
  11106. +
  11107. +#define DWC_LIST_INSERT_TAIL(list, link) do { \
  11108. + (link)->next = (list); \
  11109. + (link)->prev = (list)->prev; \
  11110. + (list)->prev->next = (link); \
  11111. + (list)->prev = (link); \
  11112. +} while (0)
  11113. +#else
  11114. +#define DWC_LIST_INSERT_HEAD(list, link) do { \
  11115. + dwc_list_link_t *__next__ = (list)->next; \
  11116. + __next__->prev = (link); \
  11117. + (link)->next = __next__; \
  11118. + (link)->prev = (list); \
  11119. + (list)->next = (link); \
  11120. +} while (0)
  11121. +
  11122. +#define DWC_LIST_INSERT_TAIL(list, link) do { \
  11123. + dwc_list_link_t *__prev__ = (list)->prev; \
  11124. + (list)->prev = (link); \
  11125. + (link)->next = (list); \
  11126. + (link)->prev = __prev__; \
  11127. + __prev__->next = (link); \
  11128. +} while (0)
  11129. +#endif
  11130. +
  11131. +#if 0
  11132. +static inline void __list_add(struct list_head *new,
  11133. + struct list_head *prev,
  11134. + struct list_head *next)
  11135. +{
  11136. + next->prev = new;
  11137. + new->next = next;
  11138. + new->prev = prev;
  11139. + prev->next = new;
  11140. +}
  11141. +
  11142. +static inline void list_add(struct list_head *new, struct list_head *head)
  11143. +{
  11144. + __list_add(new, head, head->next);
  11145. +}
  11146. +
  11147. +static inline void list_add_tail(struct list_head *new, struct list_head *head)
  11148. +{
  11149. + __list_add(new, head->prev, head);
  11150. +}
  11151. +
  11152. +static inline void __list_del(struct list_head * prev, struct list_head * next)
  11153. +{
  11154. + next->prev = prev;
  11155. + prev->next = next;
  11156. +}
  11157. +
  11158. +static inline void list_del(struct list_head *entry)
  11159. +{
  11160. + __list_del(entry->prev, entry->next);
  11161. + entry->next = LIST_POISON1;
  11162. + entry->prev = LIST_POISON2;
  11163. +}
  11164. +#endif
  11165. +
  11166. +#define DWC_LIST_REMOVE(link) do { \
  11167. + (link)->next->prev = (link)->prev; \
  11168. + (link)->prev->next = (link)->next; \
  11169. +} while (0)
  11170. +
  11171. +#define DWC_LIST_REMOVE_INIT(link) do { \
  11172. + DWC_LIST_REMOVE(link); \
  11173. + DWC_LIST_INIT(link); \
  11174. +} while (0)
  11175. +
  11176. +#define DWC_LIST_MOVE_HEAD(list, link) do { \
  11177. + DWC_LIST_REMOVE(link); \
  11178. + DWC_LIST_INSERT_HEAD(list, link); \
  11179. +} while (0)
  11180. +
  11181. +#define DWC_LIST_MOVE_TAIL(list, link) do { \
  11182. + DWC_LIST_REMOVE(link); \
  11183. + DWC_LIST_INSERT_TAIL(list, link); \
  11184. +} while (0)
  11185. +
  11186. +#define DWC_LIST_FOREACH(var, list) \
  11187. + for((var) = DWC_LIST_FIRST(list); \
  11188. + (var) != DWC_LIST_END(list); \
  11189. + (var) = DWC_LIST_NEXT(var))
  11190. +
  11191. +#define DWC_LIST_FOREACH_SAFE(var, var2, list) \
  11192. + for((var) = DWC_LIST_FIRST(list), (var2) = DWC_LIST_NEXT(var); \
  11193. + (var) != DWC_LIST_END(list); \
  11194. + (var) = (var2), (var2) = DWC_LIST_NEXT(var2))
  11195. +
  11196. +#define DWC_LIST_FOREACH_REVERSE(var, list) \
  11197. + for((var) = DWC_LIST_LAST(list); \
  11198. + (var) != DWC_LIST_END(list); \
  11199. + (var) = DWC_LIST_PREV(var))
  11200. +
  11201. +/*
  11202. + * Singly-linked List definitions.
  11203. + */
  11204. +#define DWC_SLIST_HEAD(name, type) \
  11205. +struct name { \
  11206. + struct type *slh_first; /* first element */ \
  11207. +}
  11208. +
  11209. +#define DWC_SLIST_HEAD_INITIALIZER(head) \
  11210. + { NULL }
  11211. +
  11212. +#define DWC_SLIST_ENTRY(type) \
  11213. +struct { \
  11214. + struct type *sle_next; /* next element */ \
  11215. +}
  11216. +
  11217. +/*
  11218. + * Singly-linked List access methods.
  11219. + */
  11220. +#define DWC_SLIST_FIRST(head) ((head)->slh_first)
  11221. +#define DWC_SLIST_END(head) NULL
  11222. +#define DWC_SLIST_EMPTY(head) (SLIST_FIRST(head) == SLIST_END(head))
  11223. +#define DWC_SLIST_NEXT(elm, field) ((elm)->field.sle_next)
  11224. +
  11225. +#define DWC_SLIST_FOREACH(var, head, field) \
  11226. + for((var) = SLIST_FIRST(head); \
  11227. + (var) != SLIST_END(head); \
  11228. + (var) = SLIST_NEXT(var, field))
  11229. +
  11230. +#define DWC_SLIST_FOREACH_PREVPTR(var, varp, head, field) \
  11231. + for((varp) = &SLIST_FIRST((head)); \
  11232. + ((var) = *(varp)) != SLIST_END(head); \
  11233. + (varp) = &SLIST_NEXT((var), field))
  11234. +
  11235. +/*
  11236. + * Singly-linked List functions.
  11237. + */
  11238. +#define DWC_SLIST_INIT(head) { \
  11239. + SLIST_FIRST(head) = SLIST_END(head); \
  11240. +}
  11241. +
  11242. +#define DWC_SLIST_INSERT_AFTER(slistelm, elm, field) do { \
  11243. + (elm)->field.sle_next = (slistelm)->field.sle_next; \
  11244. + (slistelm)->field.sle_next = (elm); \
  11245. +} while (0)
  11246. +
  11247. +#define DWC_SLIST_INSERT_HEAD(head, elm, field) do { \
  11248. + (elm)->field.sle_next = (head)->slh_first; \
  11249. + (head)->slh_first = (elm); \
  11250. +} while (0)
  11251. +
  11252. +#define DWC_SLIST_REMOVE_NEXT(head, elm, field) do { \
  11253. + (elm)->field.sle_next = (elm)->field.sle_next->field.sle_next; \
  11254. +} while (0)
  11255. +
  11256. +#define DWC_SLIST_REMOVE_HEAD(head, field) do { \
  11257. + (head)->slh_first = (head)->slh_first->field.sle_next; \
  11258. +} while (0)
  11259. +
  11260. +#define DWC_SLIST_REMOVE(head, elm, type, field) do { \
  11261. + if ((head)->slh_first == (elm)) { \
  11262. + SLIST_REMOVE_HEAD((head), field); \
  11263. + } \
  11264. + else { \
  11265. + struct type *curelm = (head)->slh_first; \
  11266. + while( curelm->field.sle_next != (elm) ) \
  11267. + curelm = curelm->field.sle_next; \
  11268. + curelm->field.sle_next = \
  11269. + curelm->field.sle_next->field.sle_next; \
  11270. + } \
  11271. +} while (0)
  11272. +
  11273. +/*
  11274. + * Simple queue definitions.
  11275. + */
  11276. +#define DWC_SIMPLEQ_HEAD(name, type) \
  11277. +struct name { \
  11278. + struct type *sqh_first; /* first element */ \
  11279. + struct type **sqh_last; /* addr of last next element */ \
  11280. +}
  11281. +
  11282. +#define DWC_SIMPLEQ_HEAD_INITIALIZER(head) \
  11283. + { NULL, &(head).sqh_first }
  11284. +
  11285. +#define DWC_SIMPLEQ_ENTRY(type) \
  11286. +struct { \
  11287. + struct type *sqe_next; /* next element */ \
  11288. +}
  11289. +
  11290. +/*
  11291. + * Simple queue access methods.
  11292. + */
  11293. +#define DWC_SIMPLEQ_FIRST(head) ((head)->sqh_first)
  11294. +#define DWC_SIMPLEQ_END(head) NULL
  11295. +#define DWC_SIMPLEQ_EMPTY(head) (SIMPLEQ_FIRST(head) == SIMPLEQ_END(head))
  11296. +#define DWC_SIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next)
  11297. +
  11298. +#define DWC_SIMPLEQ_FOREACH(var, head, field) \
  11299. + for((var) = SIMPLEQ_FIRST(head); \
  11300. + (var) != SIMPLEQ_END(head); \
  11301. + (var) = SIMPLEQ_NEXT(var, field))
  11302. +
  11303. +/*
  11304. + * Simple queue functions.
  11305. + */
  11306. +#define DWC_SIMPLEQ_INIT(head) do { \
  11307. + (head)->sqh_first = NULL; \
  11308. + (head)->sqh_last = &(head)->sqh_first; \
  11309. +} while (0)
  11310. +
  11311. +#define DWC_SIMPLEQ_INSERT_HEAD(head, elm, field) do { \
  11312. + if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \
  11313. + (head)->sqh_last = &(elm)->field.sqe_next; \
  11314. + (head)->sqh_first = (elm); \
  11315. +} while (0)
  11316. +
  11317. +#define DWC_SIMPLEQ_INSERT_TAIL(head, elm, field) do { \
  11318. + (elm)->field.sqe_next = NULL; \
  11319. + *(head)->sqh_last = (elm); \
  11320. + (head)->sqh_last = &(elm)->field.sqe_next; \
  11321. +} while (0)
  11322. +
  11323. +#define DWC_SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
  11324. + if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)\
  11325. + (head)->sqh_last = &(elm)->field.sqe_next; \
  11326. + (listelm)->field.sqe_next = (elm); \
  11327. +} while (0)
  11328. +
  11329. +#define DWC_SIMPLEQ_REMOVE_HEAD(head, field) do { \
  11330. + if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL) \
  11331. + (head)->sqh_last = &(head)->sqh_first; \
  11332. +} while (0)
  11333. +
  11334. +/*
  11335. + * Tail queue definitions.
  11336. + */
  11337. +#define DWC_TAILQ_HEAD(name, type) \
  11338. +struct name { \
  11339. + struct type *tqh_first; /* first element */ \
  11340. + struct type **tqh_last; /* addr of last next element */ \
  11341. +}
  11342. +
  11343. +#define DWC_TAILQ_HEAD_INITIALIZER(head) \
  11344. + { NULL, &(head).tqh_first }
  11345. +
  11346. +#define DWC_TAILQ_ENTRY(type) \
  11347. +struct { \
  11348. + struct type *tqe_next; /* next element */ \
  11349. + struct type **tqe_prev; /* address of previous next element */ \
  11350. +}
  11351. +
  11352. +/*
  11353. + * tail queue access methods
  11354. + */
  11355. +#define DWC_TAILQ_FIRST(head) ((head)->tqh_first)
  11356. +#define DWC_TAILQ_END(head) NULL
  11357. +#define DWC_TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
  11358. +#define DWC_TAILQ_LAST(head, headname) \
  11359. + (*(((struct headname *)((head)->tqh_last))->tqh_last))
  11360. +/* XXX */
  11361. +#define DWC_TAILQ_PREV(elm, headname, field) \
  11362. + (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
  11363. +#define DWC_TAILQ_EMPTY(head) \
  11364. + (DWC_TAILQ_FIRST(head) == DWC_TAILQ_END(head))
  11365. +
  11366. +#define DWC_TAILQ_FOREACH(var, head, field) \
  11367. + for ((var) = DWC_TAILQ_FIRST(head); \
  11368. + (var) != DWC_TAILQ_END(head); \
  11369. + (var) = DWC_TAILQ_NEXT(var, field))
  11370. +
  11371. +#define DWC_TAILQ_FOREACH_REVERSE(var, head, headname, field) \
  11372. + for ((var) = DWC_TAILQ_LAST(head, headname); \
  11373. + (var) != DWC_TAILQ_END(head); \
  11374. + (var) = DWC_TAILQ_PREV(var, headname, field))
  11375. +
  11376. +/*
  11377. + * Tail queue functions.
  11378. + */
  11379. +#define DWC_TAILQ_INIT(head) do { \
  11380. + (head)->tqh_first = NULL; \
  11381. + (head)->tqh_last = &(head)->tqh_first; \
  11382. +} while (0)
  11383. +
  11384. +#define DWC_TAILQ_INSERT_HEAD(head, elm, field) do { \
  11385. + if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \
  11386. + (head)->tqh_first->field.tqe_prev = \
  11387. + &(elm)->field.tqe_next; \
  11388. + else \
  11389. + (head)->tqh_last = &(elm)->field.tqe_next; \
  11390. + (head)->tqh_first = (elm); \
  11391. + (elm)->field.tqe_prev = &(head)->tqh_first; \
  11392. +} while (0)
  11393. +
  11394. +#define DWC_TAILQ_INSERT_TAIL(head, elm, field) do { \
  11395. + (elm)->field.tqe_next = NULL; \
  11396. + (elm)->field.tqe_prev = (head)->tqh_last; \
  11397. + *(head)->tqh_last = (elm); \
  11398. + (head)->tqh_last = &(elm)->field.tqe_next; \
  11399. +} while (0)
  11400. +
  11401. +#define DWC_TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
  11402. + if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
  11403. + (elm)->field.tqe_next->field.tqe_prev = \
  11404. + &(elm)->field.tqe_next; \
  11405. + else \
  11406. + (head)->tqh_last = &(elm)->field.tqe_next; \
  11407. + (listelm)->field.tqe_next = (elm); \
  11408. + (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \
  11409. +} while (0)
  11410. +
  11411. +#define DWC_TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
  11412. + (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
  11413. + (elm)->field.tqe_next = (listelm); \
  11414. + *(listelm)->field.tqe_prev = (elm); \
  11415. + (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \
  11416. +} while (0)
  11417. +
  11418. +#define DWC_TAILQ_REMOVE(head, elm, field) do { \
  11419. + if (((elm)->field.tqe_next) != NULL) \
  11420. + (elm)->field.tqe_next->field.tqe_prev = \
  11421. + (elm)->field.tqe_prev; \
  11422. + else \
  11423. + (head)->tqh_last = (elm)->field.tqe_prev; \
  11424. + *(elm)->field.tqe_prev = (elm)->field.tqe_next; \
  11425. +} while (0)
  11426. +
  11427. +#define DWC_TAILQ_REPLACE(head, elm, elm2, field) do { \
  11428. + if (((elm2)->field.tqe_next = (elm)->field.tqe_next) != NULL) \
  11429. + (elm2)->field.tqe_next->field.tqe_prev = \
  11430. + &(elm2)->field.tqe_next; \
  11431. + else \
  11432. + (head)->tqh_last = &(elm2)->field.tqe_next; \
  11433. + (elm2)->field.tqe_prev = (elm)->field.tqe_prev; \
  11434. + *(elm2)->field.tqe_prev = (elm2); \
  11435. +} while (0)
  11436. +
  11437. +/*
  11438. + * Circular queue definitions.
  11439. + */
  11440. +#define DWC_CIRCLEQ_HEAD(name, type) \
  11441. +struct name { \
  11442. + struct type *cqh_first; /* first element */ \
  11443. + struct type *cqh_last; /* last element */ \
  11444. +}
  11445. +
  11446. +#define DWC_CIRCLEQ_HEAD_INITIALIZER(head) \
  11447. + { DWC_CIRCLEQ_END(&head), DWC_CIRCLEQ_END(&head) }
  11448. +
  11449. +#define DWC_CIRCLEQ_ENTRY(type) \
  11450. +struct { \
  11451. + struct type *cqe_next; /* next element */ \
  11452. + struct type *cqe_prev; /* previous element */ \
  11453. +}
  11454. +
  11455. +/*
  11456. + * Circular queue access methods
  11457. + */
  11458. +#define DWC_CIRCLEQ_FIRST(head) ((head)->cqh_first)
  11459. +#define DWC_CIRCLEQ_LAST(head) ((head)->cqh_last)
  11460. +#define DWC_CIRCLEQ_END(head) ((void *)(head))
  11461. +#define DWC_CIRCLEQ_NEXT(elm, field) ((elm)->field.cqe_next)
  11462. +#define DWC_CIRCLEQ_PREV(elm, field) ((elm)->field.cqe_prev)
  11463. +#define DWC_CIRCLEQ_EMPTY(head) \
  11464. + (DWC_CIRCLEQ_FIRST(head) == DWC_CIRCLEQ_END(head))
  11465. +
  11466. +#define DWC_CIRCLEQ_EMPTY_ENTRY(elm, field) (((elm)->field.cqe_next == NULL) && ((elm)->field.cqe_prev == NULL))
  11467. +
  11468. +#define DWC_CIRCLEQ_FOREACH(var, head, field) \
  11469. + for((var) = DWC_CIRCLEQ_FIRST(head); \
  11470. + (var) != DWC_CIRCLEQ_END(head); \
  11471. + (var) = DWC_CIRCLEQ_NEXT(var, field))
  11472. +
  11473. +#define DWC_CIRCLEQ_FOREACH_SAFE(var, var2, head, field) \
  11474. + for((var) = DWC_CIRCLEQ_FIRST(head), var2 = DWC_CIRCLEQ_NEXT(var, field); \
  11475. + (var) != DWC_CIRCLEQ_END(head); \
  11476. + (var) = var2, var2 = DWC_CIRCLEQ_NEXT(var, field))
  11477. +
  11478. +#define DWC_CIRCLEQ_FOREACH_REVERSE(var, head, field) \
  11479. + for((var) = DWC_CIRCLEQ_LAST(head); \
  11480. + (var) != DWC_CIRCLEQ_END(head); \
  11481. + (var) = DWC_CIRCLEQ_PREV(var, field))
  11482. +
  11483. +/*
  11484. + * Circular queue functions.
  11485. + */
  11486. +#define DWC_CIRCLEQ_INIT(head) do { \
  11487. + (head)->cqh_first = DWC_CIRCLEQ_END(head); \
  11488. + (head)->cqh_last = DWC_CIRCLEQ_END(head); \
  11489. +} while (0)
  11490. +
  11491. +#define DWC_CIRCLEQ_INIT_ENTRY(elm, field) do { \
  11492. + (elm)->field.cqe_next = NULL; \
  11493. + (elm)->field.cqe_prev = NULL; \
  11494. +} while (0)
  11495. +
  11496. +#define DWC_CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
  11497. + (elm)->field.cqe_next = (listelm)->field.cqe_next; \
  11498. + (elm)->field.cqe_prev = (listelm); \
  11499. + if ((listelm)->field.cqe_next == DWC_CIRCLEQ_END(head)) \
  11500. + (head)->cqh_last = (elm); \
  11501. + else \
  11502. + (listelm)->field.cqe_next->field.cqe_prev = (elm); \
  11503. + (listelm)->field.cqe_next = (elm); \
  11504. +} while (0)
  11505. +
  11506. +#define DWC_CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do { \
  11507. + (elm)->field.cqe_next = (listelm); \
  11508. + (elm)->field.cqe_prev = (listelm)->field.cqe_prev; \
  11509. + if ((listelm)->field.cqe_prev == DWC_CIRCLEQ_END(head)) \
  11510. + (head)->cqh_first = (elm); \
  11511. + else \
  11512. + (listelm)->field.cqe_prev->field.cqe_next = (elm); \
  11513. + (listelm)->field.cqe_prev = (elm); \
  11514. +} while (0)
  11515. +
  11516. +#define DWC_CIRCLEQ_INSERT_HEAD(head, elm, field) do { \
  11517. + (elm)->field.cqe_next = (head)->cqh_first; \
  11518. + (elm)->field.cqe_prev = DWC_CIRCLEQ_END(head); \
  11519. + if ((head)->cqh_last == DWC_CIRCLEQ_END(head)) \
  11520. + (head)->cqh_last = (elm); \
  11521. + else \
  11522. + (head)->cqh_first->field.cqe_prev = (elm); \
  11523. + (head)->cqh_first = (elm); \
  11524. +} while (0)
  11525. +
  11526. +#define DWC_CIRCLEQ_INSERT_TAIL(head, elm, field) do { \
  11527. + (elm)->field.cqe_next = DWC_CIRCLEQ_END(head); \
  11528. + (elm)->field.cqe_prev = (head)->cqh_last; \
  11529. + if ((head)->cqh_first == DWC_CIRCLEQ_END(head)) \
  11530. + (head)->cqh_first = (elm); \
  11531. + else \
  11532. + (head)->cqh_last->field.cqe_next = (elm); \
  11533. + (head)->cqh_last = (elm); \
  11534. +} while (0)
  11535. +
  11536. +#define DWC_CIRCLEQ_REMOVE(head, elm, field) do { \
  11537. + if ((elm)->field.cqe_next == DWC_CIRCLEQ_END(head)) \
  11538. + (head)->cqh_last = (elm)->field.cqe_prev; \
  11539. + else \
  11540. + (elm)->field.cqe_next->field.cqe_prev = \
  11541. + (elm)->field.cqe_prev; \
  11542. + if ((elm)->field.cqe_prev == DWC_CIRCLEQ_END(head)) \
  11543. + (head)->cqh_first = (elm)->field.cqe_next; \
  11544. + else \
  11545. + (elm)->field.cqe_prev->field.cqe_next = \
  11546. + (elm)->field.cqe_next; \
  11547. +} while (0)
  11548. +
  11549. +#define DWC_CIRCLEQ_REMOVE_INIT(head, elm, field) do { \
  11550. + DWC_CIRCLEQ_REMOVE(head, elm, field); \
  11551. + DWC_CIRCLEQ_INIT_ENTRY(elm, field); \
  11552. +} while (0)
  11553. +
  11554. +#define DWC_CIRCLEQ_REPLACE(head, elm, elm2, field) do { \
  11555. + if (((elm2)->field.cqe_next = (elm)->field.cqe_next) == \
  11556. + DWC_CIRCLEQ_END(head)) \
  11557. + (head).cqh_last = (elm2); \
  11558. + else \
  11559. + (elm2)->field.cqe_next->field.cqe_prev = (elm2); \
  11560. + if (((elm2)->field.cqe_prev = (elm)->field.cqe_prev) == \
  11561. + DWC_CIRCLEQ_END(head)) \
  11562. + (head).cqh_first = (elm2); \
  11563. + else \
  11564. + (elm2)->field.cqe_prev->field.cqe_next = (elm2); \
  11565. +} while (0)
  11566. +
  11567. +#ifdef __cplusplus
  11568. +}
  11569. +#endif
  11570. +
  11571. +#endif /* _DWC_LIST_H_ */
  11572. --- /dev/null
  11573. +++ b/drivers/usb/host/dwc_common_port/dwc_mem.c
  11574. @@ -0,0 +1,245 @@
  11575. +/* Memory Debugging */
  11576. +#ifdef DWC_DEBUG_MEMORY
  11577. +
  11578. +#include "dwc_os.h"
  11579. +#include "dwc_list.h"
  11580. +
  11581. +struct allocation {
  11582. + void *addr;
  11583. + void *ctx;
  11584. + char *func;
  11585. + int line;
  11586. + uint32_t size;
  11587. + int dma;
  11588. + DWC_CIRCLEQ_ENTRY(allocation) entry;
  11589. +};
  11590. +
  11591. +DWC_CIRCLEQ_HEAD(allocation_queue, allocation);
  11592. +
  11593. +struct allocation_manager {
  11594. + void *mem_ctx;
  11595. + struct allocation_queue allocations;
  11596. +
  11597. + /* statistics */
  11598. + int num;
  11599. + int num_freed;
  11600. + int num_active;
  11601. + uint32_t total;
  11602. + uint32_t cur;
  11603. + uint32_t max;
  11604. +};
  11605. +
  11606. +static struct allocation_manager *manager = NULL;
  11607. +
  11608. +static int add_allocation(void *ctx, uint32_t size, char const *func, int line, void *addr,
  11609. + int dma)
  11610. +{
  11611. + struct allocation *a;
  11612. +
  11613. + DWC_ASSERT(manager != NULL, "manager not allocated");
  11614. +
  11615. + a = __DWC_ALLOC_ATOMIC(manager->mem_ctx, sizeof(*a));
  11616. + if (!a) {
  11617. + return -DWC_E_NO_MEMORY;
  11618. + }
  11619. +
  11620. + a->func = __DWC_ALLOC_ATOMIC(manager->mem_ctx, DWC_STRLEN(func) + 1);
  11621. + if (!a->func) {
  11622. + __DWC_FREE(manager->mem_ctx, a);
  11623. + return -DWC_E_NO_MEMORY;
  11624. + }
  11625. +
  11626. + DWC_MEMCPY(a->func, func, DWC_STRLEN(func) + 1);
  11627. + a->addr = addr;
  11628. + a->ctx = ctx;
  11629. + a->line = line;
  11630. + a->size = size;
  11631. + a->dma = dma;
  11632. + DWC_CIRCLEQ_INSERT_TAIL(&manager->allocations, a, entry);
  11633. +
  11634. + /* Update stats */
  11635. + manager->num++;
  11636. + manager->num_active++;
  11637. + manager->total += size;
  11638. + manager->cur += size;
  11639. +
  11640. + if (manager->max < manager->cur) {
  11641. + manager->max = manager->cur;
  11642. + }
  11643. +
  11644. + return 0;
  11645. +}
  11646. +
  11647. +static struct allocation *find_allocation(void *ctx, void *addr)
  11648. +{
  11649. + struct allocation *a;
  11650. +
  11651. + DWC_CIRCLEQ_FOREACH(a, &manager->allocations, entry) {
  11652. + if (a->ctx == ctx && a->addr == addr) {
  11653. + return a;
  11654. + }
  11655. + }
  11656. +
  11657. + return NULL;
  11658. +}
  11659. +
  11660. +static void free_allocation(void *ctx, void *addr, char const *func, int line)
  11661. +{
  11662. + struct allocation *a = find_allocation(ctx, addr);
  11663. +
  11664. + if (!a) {
  11665. + DWC_ASSERT(0,
  11666. + "Free of address %p that was never allocated or already freed %s:%d",
  11667. + addr, func, line);
  11668. + return;
  11669. + }
  11670. +
  11671. + DWC_CIRCLEQ_REMOVE(&manager->allocations, a, entry);
  11672. +
  11673. + manager->num_active--;
  11674. + manager->num_freed++;
  11675. + manager->cur -= a->size;
  11676. + __DWC_FREE(manager->mem_ctx, a->func);
  11677. + __DWC_FREE(manager->mem_ctx, a);
  11678. +}
  11679. +
  11680. +int dwc_memory_debug_start(void *mem_ctx)
  11681. +{
  11682. + DWC_ASSERT(manager == NULL, "Memory debugging has already started\n");
  11683. +
  11684. + if (manager) {
  11685. + return -DWC_E_BUSY;
  11686. + }
  11687. +
  11688. + manager = __DWC_ALLOC(mem_ctx, sizeof(*manager));
  11689. + if (!manager) {
  11690. + return -DWC_E_NO_MEMORY;
  11691. + }
  11692. +
  11693. + DWC_CIRCLEQ_INIT(&manager->allocations);
  11694. + manager->mem_ctx = mem_ctx;
  11695. + manager->num = 0;
  11696. + manager->num_freed = 0;
  11697. + manager->num_active = 0;
  11698. + manager->total = 0;
  11699. + manager->cur = 0;
  11700. + manager->max = 0;
  11701. +
  11702. + return 0;
  11703. +}
  11704. +
  11705. +void dwc_memory_debug_stop(void)
  11706. +{
  11707. + struct allocation *a;
  11708. +
  11709. + dwc_memory_debug_report();
  11710. +
  11711. + DWC_CIRCLEQ_FOREACH(a, &manager->allocations, entry) {
  11712. + DWC_ERROR("Memory leaked from %s:%d\n", a->func, a->line);
  11713. + free_allocation(a->ctx, a->addr, NULL, -1);
  11714. + }
  11715. +
  11716. + __DWC_FREE(manager->mem_ctx, manager);
  11717. +}
  11718. +
  11719. +void dwc_memory_debug_report(void)
  11720. +{
  11721. + struct allocation *a;
  11722. +
  11723. + DWC_PRINTF("\n\n\n----------------- Memory Debugging Report -----------------\n\n");
  11724. + DWC_PRINTF("Num Allocations = %d\n", manager->num);
  11725. + DWC_PRINTF("Freed = %d\n", manager->num_freed);
  11726. + DWC_PRINTF("Active = %d\n", manager->num_active);
  11727. + DWC_PRINTF("Current Memory Used = %d\n", manager->cur);
  11728. + DWC_PRINTF("Total Memory Used = %d\n", manager->total);
  11729. + DWC_PRINTF("Maximum Memory Used at Once = %d\n", manager->max);
  11730. + DWC_PRINTF("Unfreed allocations:\n");
  11731. +
  11732. + DWC_CIRCLEQ_FOREACH(a, &manager->allocations, entry) {
  11733. + DWC_PRINTF(" addr=%p, size=%d from %s:%d, DMA=%d\n",
  11734. + a->addr, a->size, a->func, a->line, a->dma);
  11735. + }
  11736. +}
  11737. +
  11738. +/* The replacement functions */
  11739. +void *dwc_alloc_debug(void *mem_ctx, uint32_t size, char const *func, int line)
  11740. +{
  11741. + void *addr = __DWC_ALLOC(mem_ctx, size);
  11742. +
  11743. + if (!addr) {
  11744. + return NULL;
  11745. + }
  11746. +
  11747. + if (add_allocation(mem_ctx, size, func, line, addr, 0)) {
  11748. + __DWC_FREE(mem_ctx, addr);
  11749. + return NULL;
  11750. + }
  11751. +
  11752. + return addr;
  11753. +}
  11754. +
  11755. +void *dwc_alloc_atomic_debug(void *mem_ctx, uint32_t size, char const *func,
  11756. + int line)
  11757. +{
  11758. + void *addr = __DWC_ALLOC_ATOMIC(mem_ctx, size);
  11759. +
  11760. + if (!addr) {
  11761. + return NULL;
  11762. + }
  11763. +
  11764. + if (add_allocation(mem_ctx, size, func, line, addr, 0)) {
  11765. + __DWC_FREE(mem_ctx, addr);
  11766. + return NULL;
  11767. + }
  11768. +
  11769. + return addr;
  11770. +}
  11771. +
  11772. +void dwc_free_debug(void *mem_ctx, void *addr, char const *func, int line)
  11773. +{
  11774. + free_allocation(mem_ctx, addr, func, line);
  11775. + __DWC_FREE(mem_ctx, addr);
  11776. +}
  11777. +
  11778. +void *dwc_dma_alloc_debug(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr,
  11779. + char const *func, int line)
  11780. +{
  11781. + void *addr = __DWC_DMA_ALLOC(dma_ctx, size, dma_addr);
  11782. +
  11783. + if (!addr) {
  11784. + return NULL;
  11785. + }
  11786. +
  11787. + if (add_allocation(dma_ctx, size, func, line, addr, 1)) {
  11788. + __DWC_DMA_FREE(dma_ctx, size, addr, *dma_addr);
  11789. + return NULL;
  11790. + }
  11791. +
  11792. + return addr;
  11793. +}
  11794. +
  11795. +void *dwc_dma_alloc_atomic_debug(void *dma_ctx, uint32_t size,
  11796. + dwc_dma_t *dma_addr, char const *func, int line)
  11797. +{
  11798. + void *addr = __DWC_DMA_ALLOC_ATOMIC(dma_ctx, size, dma_addr);
  11799. +
  11800. + if (!addr) {
  11801. + return NULL;
  11802. + }
  11803. +
  11804. + if (add_allocation(dma_ctx, size, func, line, addr, 1)) {
  11805. + __DWC_DMA_FREE(dma_ctx, size, addr, *dma_addr);
  11806. + return NULL;
  11807. + }
  11808. +
  11809. + return addr;
  11810. +}
  11811. +
  11812. +void dwc_dma_free_debug(void *dma_ctx, uint32_t size, void *virt_addr,
  11813. + dwc_dma_t dma_addr, char const *func, int line)
  11814. +{
  11815. + free_allocation(dma_ctx, virt_addr, func, line);
  11816. + __DWC_DMA_FREE(dma_ctx, size, virt_addr, dma_addr);
  11817. +}
  11818. +
  11819. +#endif /* DWC_DEBUG_MEMORY */
  11820. --- /dev/null
  11821. +++ b/drivers/usb/host/dwc_common_port/dwc_modpow.c
  11822. @@ -0,0 +1,636 @@
  11823. +/* Bignum routines adapted from PUTTY sources. PuTTY copyright notice follows.
  11824. + *
  11825. + * PuTTY is copyright 1997-2007 Simon Tatham.
  11826. + *
  11827. + * Portions copyright Robert de Bath, Joris van Rantwijk, Delian
  11828. + * Delchev, Andreas Schultz, Jeroen Massar, Wez Furlong, Nicolas Barry,
  11829. + * Justin Bradford, Ben Harris, Malcolm Smith, Ahmad Khalifa, Markus
  11830. + * Kuhn, and CORE SDI S.A.
  11831. + *
  11832. + * Permission is hereby granted, free of charge, to any person
  11833. + * obtaining a copy of this software and associated documentation files
  11834. + * (the "Software"), to deal in the Software without restriction,
  11835. + * including without limitation the rights to use, copy, modify, merge,
  11836. + * publish, distribute, sublicense, and/or sell copies of the Software,
  11837. + * and to permit persons to whom the Software is furnished to do so,
  11838. + * subject to the following conditions:
  11839. + *
  11840. + * The above copyright notice and this permission notice shall be
  11841. + * included in all copies or substantial portions of the Software.
  11842. +
  11843. + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  11844. + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  11845. + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  11846. + * NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE
  11847. + * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  11848. + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  11849. + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  11850. + *
  11851. + */
  11852. +#ifdef DWC_CRYPTOLIB
  11853. +
  11854. +#ifndef CONFIG_MACH_IPMATE
  11855. +
  11856. +#include "dwc_modpow.h"
  11857. +
  11858. +#define BIGNUM_INT_MASK 0xFFFFFFFFUL
  11859. +#define BIGNUM_TOP_BIT 0x80000000UL
  11860. +#define BIGNUM_INT_BITS 32
  11861. +
  11862. +
  11863. +static void *snmalloc(void *mem_ctx, size_t n, size_t size)
  11864. +{
  11865. + void *p;
  11866. + size *= n;
  11867. + if (size == 0) size = 1;
  11868. + p = dwc_alloc(mem_ctx, size);
  11869. + return p;
  11870. +}
  11871. +
  11872. +#define snewn(ctx, n, type) ((type *)snmalloc((ctx), (n), sizeof(type)))
  11873. +#define sfree dwc_free
  11874. +
  11875. +/*
  11876. + * Usage notes:
  11877. + * * Do not call the DIVMOD_WORD macro with expressions such as array
  11878. + * subscripts, as some implementations object to this (see below).
  11879. + * * Note that none of the division methods below will cope if the
  11880. + * quotient won't fit into BIGNUM_INT_BITS. Callers should be careful
  11881. + * to avoid this case.
  11882. + * If this condition occurs, in the case of the x86 DIV instruction,
  11883. + * an overflow exception will occur, which (according to a correspondent)
  11884. + * will manifest on Windows as something like
  11885. + * 0xC0000095: Integer overflow
  11886. + * The C variant won't give the right answer, either.
  11887. + */
  11888. +
  11889. +#define MUL_WORD(w1, w2) ((BignumDblInt)w1 * w2)
  11890. +
  11891. +#if defined __GNUC__ && defined __i386__
  11892. +#define DIVMOD_WORD(q, r, hi, lo, w) \
  11893. + __asm__("div %2" : \
  11894. + "=d" (r), "=a" (q) : \
  11895. + "r" (w), "d" (hi), "a" (lo))
  11896. +#else
  11897. +#define DIVMOD_WORD(q, r, hi, lo, w) do { \
  11898. + BignumDblInt n = (((BignumDblInt)hi) << BIGNUM_INT_BITS) | lo; \
  11899. + q = n / w; \
  11900. + r = n % w; \
  11901. +} while (0)
  11902. +#endif
  11903. +
  11904. +// q = n / w;
  11905. +// r = n % w;
  11906. +
  11907. +#define BIGNUM_INT_BYTES (BIGNUM_INT_BITS / 8)
  11908. +
  11909. +#define BIGNUM_INTERNAL
  11910. +
  11911. +static Bignum newbn(void *mem_ctx, int length)
  11912. +{
  11913. + Bignum b = snewn(mem_ctx, length + 1, BignumInt);
  11914. + //if (!b)
  11915. + //abort(); /* FIXME */
  11916. + DWC_MEMSET(b, 0, (length + 1) * sizeof(*b));
  11917. + b[0] = length;
  11918. + return b;
  11919. +}
  11920. +
  11921. +void freebn(void *mem_ctx, Bignum b)
  11922. +{
  11923. + /*
  11924. + * Burn the evidence, just in case.
  11925. + */
  11926. + DWC_MEMSET(b, 0, sizeof(b[0]) * (b[0] + 1));
  11927. + sfree(mem_ctx, b);
  11928. +}
  11929. +
  11930. +/*
  11931. + * Compute c = a * b.
  11932. + * Input is in the first len words of a and b.
  11933. + * Result is returned in the first 2*len words of c.
  11934. + */
  11935. +static void internal_mul(BignumInt *a, BignumInt *b,
  11936. + BignumInt *c, int len)
  11937. +{
  11938. + int i, j;
  11939. + BignumDblInt t;
  11940. +
  11941. + for (j = 0; j < 2 * len; j++)
  11942. + c[j] = 0;
  11943. +
  11944. + for (i = len - 1; i >= 0; i--) {
  11945. + t = 0;
  11946. + for (j = len - 1; j >= 0; j--) {
  11947. + t += MUL_WORD(a[i], (BignumDblInt) b[j]);
  11948. + t += (BignumDblInt) c[i + j + 1];
  11949. + c[i + j + 1] = (BignumInt) t;
  11950. + t = t >> BIGNUM_INT_BITS;
  11951. + }
  11952. + c[i] = (BignumInt) t;
  11953. + }
  11954. +}
  11955. +
  11956. +static void internal_add_shifted(BignumInt *number,
  11957. + unsigned n, int shift)
  11958. +{
  11959. + int word = 1 + (shift / BIGNUM_INT_BITS);
  11960. + int bshift = shift % BIGNUM_INT_BITS;
  11961. + BignumDblInt addend;
  11962. +
  11963. + addend = (BignumDblInt)n << bshift;
  11964. +
  11965. + while (addend) {
  11966. + addend += number[word];
  11967. + number[word] = (BignumInt) addend & BIGNUM_INT_MASK;
  11968. + addend >>= BIGNUM_INT_BITS;
  11969. + word++;
  11970. + }
  11971. +}
  11972. +
  11973. +/*
  11974. + * Compute a = a % m.
  11975. + * Input in first alen words of a and first mlen words of m.
  11976. + * Output in first alen words of a
  11977. + * (of which first alen-mlen words will be zero).
  11978. + * The MSW of m MUST have its high bit set.
  11979. + * Quotient is accumulated in the `quotient' array, which is a Bignum
  11980. + * rather than the internal bigendian format. Quotient parts are shifted
  11981. + * left by `qshift' before adding into quot.
  11982. + */
  11983. +static void internal_mod(BignumInt *a, int alen,
  11984. + BignumInt *m, int mlen,
  11985. + BignumInt *quot, int qshift)
  11986. +{
  11987. + BignumInt m0, m1;
  11988. + unsigned int h;
  11989. + int i, k;
  11990. +
  11991. + m0 = m[0];
  11992. + if (mlen > 1)
  11993. + m1 = m[1];
  11994. + else
  11995. + m1 = 0;
  11996. +
  11997. + for (i = 0; i <= alen - mlen; i++) {
  11998. + BignumDblInt t;
  11999. + unsigned int q, r, c, ai1;
  12000. +
  12001. + if (i == 0) {
  12002. + h = 0;
  12003. + } else {
  12004. + h = a[i - 1];
  12005. + a[i - 1] = 0;
  12006. + }
  12007. +
  12008. + if (i == alen - 1)
  12009. + ai1 = 0;
  12010. + else
  12011. + ai1 = a[i + 1];
  12012. +
  12013. + /* Find q = h:a[i] / m0 */
  12014. + if (h >= m0) {
  12015. + /*
  12016. + * Special case.
  12017. + *
  12018. + * To illustrate it, suppose a BignumInt is 8 bits, and
  12019. + * we are dividing (say) A1:23:45:67 by A1:B2:C3. Then
  12020. + * our initial division will be 0xA123 / 0xA1, which
  12021. + * will give a quotient of 0x100 and a divide overflow.
  12022. + * However, the invariants in this division algorithm
  12023. + * are not violated, since the full number A1:23:... is
  12024. + * _less_ than the quotient prefix A1:B2:... and so the
  12025. + * following correction loop would have sorted it out.
  12026. + *
  12027. + * In this situation we set q to be the largest
  12028. + * quotient we _can_ stomach (0xFF, of course).
  12029. + */
  12030. + q = BIGNUM_INT_MASK;
  12031. + } else {
  12032. + /* Macro doesn't want an array subscript expression passed
  12033. + * into it (see definition), so use a temporary. */
  12034. + BignumInt tmplo = a[i];
  12035. + DIVMOD_WORD(q, r, h, tmplo, m0);
  12036. +
  12037. + /* Refine our estimate of q by looking at
  12038. + h:a[i]:a[i+1] / m0:m1 */
  12039. + t = MUL_WORD(m1, q);
  12040. + if (t > ((BignumDblInt) r << BIGNUM_INT_BITS) + ai1) {
  12041. + q--;
  12042. + t -= m1;
  12043. + r = (r + m0) & BIGNUM_INT_MASK; /* overflow? */
  12044. + if (r >= (BignumDblInt) m0 &&
  12045. + t > ((BignumDblInt) r << BIGNUM_INT_BITS) + ai1) q--;
  12046. + }
  12047. + }
  12048. +
  12049. + /* Subtract q * m from a[i...] */
  12050. + c = 0;
  12051. + for (k = mlen - 1; k >= 0; k--) {
  12052. + t = MUL_WORD(q, m[k]);
  12053. + t += c;
  12054. + c = (unsigned)(t >> BIGNUM_INT_BITS);
  12055. + if ((BignumInt) t > a[i + k])
  12056. + c++;
  12057. + a[i + k] -= (BignumInt) t;
  12058. + }
  12059. +
  12060. + /* Add back m in case of borrow */
  12061. + if (c != h) {
  12062. + t = 0;
  12063. + for (k = mlen - 1; k >= 0; k--) {
  12064. + t += m[k];
  12065. + t += a[i + k];
  12066. + a[i + k] = (BignumInt) t;
  12067. + t = t >> BIGNUM_INT_BITS;
  12068. + }
  12069. + q--;
  12070. + }
  12071. + if (quot)
  12072. + internal_add_shifted(quot, q, qshift + BIGNUM_INT_BITS * (alen - mlen - i));
  12073. + }
  12074. +}
  12075. +
  12076. +/*
  12077. + * Compute p % mod.
  12078. + * The most significant word of mod MUST be non-zero.
  12079. + * We assume that the result array is the same size as the mod array.
  12080. + * We optionally write out a quotient if `quotient' is non-NULL.
  12081. + * We can avoid writing out the result if `result' is NULL.
  12082. + */
  12083. +void bigdivmod(void *mem_ctx, Bignum p, Bignum mod, Bignum result, Bignum quotient)
  12084. +{
  12085. + BignumInt *n, *m;
  12086. + int mshift;
  12087. + int plen, mlen, i, j;
  12088. +
  12089. + /* Allocate m of size mlen, copy mod to m */
  12090. + /* We use big endian internally */
  12091. + mlen = mod[0];
  12092. + m = snewn(mem_ctx, mlen, BignumInt);
  12093. + //if (!m)
  12094. + //abort(); /* FIXME */
  12095. + for (j = 0; j < mlen; j++)
  12096. + m[j] = mod[mod[0] - j];
  12097. +
  12098. + /* Shift m left to make msb bit set */
  12099. + for (mshift = 0; mshift < BIGNUM_INT_BITS-1; mshift++)
  12100. + if ((m[0] << mshift) & BIGNUM_TOP_BIT)
  12101. + break;
  12102. + if (mshift) {
  12103. + for (i = 0; i < mlen - 1; i++)
  12104. + m[i] = (m[i] << mshift) | (m[i + 1] >> (BIGNUM_INT_BITS - mshift));
  12105. + m[mlen - 1] = m[mlen - 1] << mshift;
  12106. + }
  12107. +
  12108. + plen = p[0];
  12109. + /* Ensure plen > mlen */
  12110. + if (plen <= mlen)
  12111. + plen = mlen + 1;
  12112. +
  12113. + /* Allocate n of size plen, copy p to n */
  12114. + n = snewn(mem_ctx, plen, BignumInt);
  12115. + //if (!n)
  12116. + //abort(); /* FIXME */
  12117. + for (j = 0; j < plen; j++)
  12118. + n[j] = 0;
  12119. + for (j = 1; j <= (int)p[0]; j++)
  12120. + n[plen - j] = p[j];
  12121. +
  12122. + /* Main computation */
  12123. + internal_mod(n, plen, m, mlen, quotient, mshift);
  12124. +
  12125. + /* Fixup result in case the modulus was shifted */
  12126. + if (mshift) {
  12127. + for (i = plen - mlen - 1; i < plen - 1; i++)
  12128. + n[i] = (n[i] << mshift) | (n[i + 1] >> (BIGNUM_INT_BITS - mshift));
  12129. + n[plen - 1] = n[plen - 1] << mshift;
  12130. + internal_mod(n, plen, m, mlen, quotient, 0);
  12131. + for (i = plen - 1; i >= plen - mlen; i--)
  12132. + n[i] = (n[i] >> mshift) | (n[i - 1] << (BIGNUM_INT_BITS - mshift));
  12133. + }
  12134. +
  12135. + /* Copy result to buffer */
  12136. + if (result) {
  12137. + for (i = 1; i <= (int)result[0]; i++) {
  12138. + int j = plen - i;
  12139. + result[i] = j >= 0 ? n[j] : 0;
  12140. + }
  12141. + }
  12142. +
  12143. + /* Free temporary arrays */
  12144. + for (i = 0; i < mlen; i++)
  12145. + m[i] = 0;
  12146. + sfree(mem_ctx, m);
  12147. + for (i = 0; i < plen; i++)
  12148. + n[i] = 0;
  12149. + sfree(mem_ctx, n);
  12150. +}
  12151. +
  12152. +/*
  12153. + * Simple remainder.
  12154. + */
  12155. +Bignum bigmod(void *mem_ctx, Bignum a, Bignum b)
  12156. +{
  12157. + Bignum r = newbn(mem_ctx, b[0]);
  12158. + bigdivmod(mem_ctx, a, b, r, NULL);
  12159. + return r;
  12160. +}
  12161. +
  12162. +/*
  12163. + * Compute (base ^ exp) % mod.
  12164. + */
  12165. +Bignum dwc_modpow(void *mem_ctx, Bignum base_in, Bignum exp, Bignum mod)
  12166. +{
  12167. + BignumInt *a, *b, *n, *m;
  12168. + int mshift;
  12169. + int mlen, i, j;
  12170. + Bignum base, result;
  12171. +
  12172. + /*
  12173. + * The most significant word of mod needs to be non-zero. It
  12174. + * should already be, but let's make sure.
  12175. + */
  12176. + //assert(mod[mod[0]] != 0);
  12177. +
  12178. + /*
  12179. + * Make sure the base is smaller than the modulus, by reducing
  12180. + * it modulo the modulus if not.
  12181. + */
  12182. + base = bigmod(mem_ctx, base_in, mod);
  12183. +
  12184. + /* Allocate m of size mlen, copy mod to m */
  12185. + /* We use big endian internally */
  12186. + mlen = mod[0];
  12187. + m = snewn(mem_ctx, mlen, BignumInt);
  12188. + //if (!m)
  12189. + //abort(); /* FIXME */
  12190. + for (j = 0; j < mlen; j++)
  12191. + m[j] = mod[mod[0] - j];
  12192. +
  12193. + /* Shift m left to make msb bit set */
  12194. + for (mshift = 0; mshift < BIGNUM_INT_BITS - 1; mshift++)
  12195. + if ((m[0] << mshift) & BIGNUM_TOP_BIT)
  12196. + break;
  12197. + if (mshift) {
  12198. + for (i = 0; i < mlen - 1; i++)
  12199. + m[i] =
  12200. + (m[i] << mshift) | (m[i + 1] >>
  12201. + (BIGNUM_INT_BITS - mshift));
  12202. + m[mlen - 1] = m[mlen - 1] << mshift;
  12203. + }
  12204. +
  12205. + /* Allocate n of size mlen, copy base to n */
  12206. + n = snewn(mem_ctx, mlen, BignumInt);
  12207. + //if (!n)
  12208. + //abort(); /* FIXME */
  12209. + i = mlen - base[0];
  12210. + for (j = 0; j < i; j++)
  12211. + n[j] = 0;
  12212. + for (j = 0; j < base[0]; j++)
  12213. + n[i + j] = base[base[0] - j];
  12214. +
  12215. + /* Allocate a and b of size 2*mlen. Set a = 1 */
  12216. + a = snewn(mem_ctx, 2 * mlen, BignumInt);
  12217. + //if (!a)
  12218. + //abort(); /* FIXME */
  12219. + b = snewn(mem_ctx, 2 * mlen, BignumInt);
  12220. + //if (!b)
  12221. + //abort(); /* FIXME */
  12222. + for (i = 0; i < 2 * mlen; i++)
  12223. + a[i] = 0;
  12224. + a[2 * mlen - 1] = 1;
  12225. +
  12226. + /* Skip leading zero bits of exp. */
  12227. + i = 0;
  12228. + j = BIGNUM_INT_BITS - 1;
  12229. + while (i < exp[0] && (exp[exp[0] - i] & (1 << j)) == 0) {
  12230. + j--;
  12231. + if (j < 0) {
  12232. + i++;
  12233. + j = BIGNUM_INT_BITS - 1;
  12234. + }
  12235. + }
  12236. +
  12237. + /* Main computation */
  12238. + while (i < exp[0]) {
  12239. + while (j >= 0) {
  12240. + internal_mul(a + mlen, a + mlen, b, mlen);
  12241. + internal_mod(b, mlen * 2, m, mlen, NULL, 0);
  12242. + if ((exp[exp[0] - i] & (1 << j)) != 0) {
  12243. + internal_mul(b + mlen, n, a, mlen);
  12244. + internal_mod(a, mlen * 2, m, mlen, NULL, 0);
  12245. + } else {
  12246. + BignumInt *t;
  12247. + t = a;
  12248. + a = b;
  12249. + b = t;
  12250. + }
  12251. + j--;
  12252. + }
  12253. + i++;
  12254. + j = BIGNUM_INT_BITS - 1;
  12255. + }
  12256. +
  12257. + /* Fixup result in case the modulus was shifted */
  12258. + if (mshift) {
  12259. + for (i = mlen - 1; i < 2 * mlen - 1; i++)
  12260. + a[i] =
  12261. + (a[i] << mshift) | (a[i + 1] >>
  12262. + (BIGNUM_INT_BITS - mshift));
  12263. + a[2 * mlen - 1] = a[2 * mlen - 1] << mshift;
  12264. + internal_mod(a, mlen * 2, m, mlen, NULL, 0);
  12265. + for (i = 2 * mlen - 1; i >= mlen; i--)
  12266. + a[i] =
  12267. + (a[i] >> mshift) | (a[i - 1] <<
  12268. + (BIGNUM_INT_BITS - mshift));
  12269. + }
  12270. +
  12271. + /* Copy result to buffer */
  12272. + result = newbn(mem_ctx, mod[0]);
  12273. + for (i = 0; i < mlen; i++)
  12274. + result[result[0] - i] = a[i + mlen];
  12275. + while (result[0] > 1 && result[result[0]] == 0)
  12276. + result[0]--;
  12277. +
  12278. + /* Free temporary arrays */
  12279. + for (i = 0; i < 2 * mlen; i++)
  12280. + a[i] = 0;
  12281. + sfree(mem_ctx, a);
  12282. + for (i = 0; i < 2 * mlen; i++)
  12283. + b[i] = 0;
  12284. + sfree(mem_ctx, b);
  12285. + for (i = 0; i < mlen; i++)
  12286. + m[i] = 0;
  12287. + sfree(mem_ctx, m);
  12288. + for (i = 0; i < mlen; i++)
  12289. + n[i] = 0;
  12290. + sfree(mem_ctx, n);
  12291. +
  12292. + freebn(mem_ctx, base);
  12293. +
  12294. + return result;
  12295. +}
  12296. +
  12297. +
  12298. +#ifdef UNITTEST
  12299. +
  12300. +static __u32 dh_p[] = {
  12301. + 96,
  12302. + 0xFFFFFFFF,
  12303. + 0xFFFFFFFF,
  12304. + 0xA93AD2CA,
  12305. + 0x4B82D120,
  12306. + 0xE0FD108E,
  12307. + 0x43DB5BFC,
  12308. + 0x74E5AB31,
  12309. + 0x08E24FA0,
  12310. + 0xBAD946E2,
  12311. + 0x770988C0,
  12312. + 0x7A615D6C,
  12313. + 0xBBE11757,
  12314. + 0x177B200C,
  12315. + 0x521F2B18,
  12316. + 0x3EC86A64,
  12317. + 0xD8760273,
  12318. + 0xD98A0864,
  12319. + 0xF12FFA06,
  12320. + 0x1AD2EE6B,
  12321. + 0xCEE3D226,
  12322. + 0x4A25619D,
  12323. + 0x1E8C94E0,
  12324. + 0xDB0933D7,
  12325. + 0xABF5AE8C,
  12326. + 0xA6E1E4C7,
  12327. + 0xB3970F85,
  12328. + 0x5D060C7D,
  12329. + 0x8AEA7157,
  12330. + 0x58DBEF0A,
  12331. + 0xECFB8504,
  12332. + 0xDF1CBA64,
  12333. + 0xA85521AB,
  12334. + 0x04507A33,
  12335. + 0xAD33170D,
  12336. + 0x8AAAC42D,
  12337. + 0x15728E5A,
  12338. + 0x98FA0510,
  12339. + 0x15D22618,
  12340. + 0xEA956AE5,
  12341. + 0x3995497C,
  12342. + 0x95581718,
  12343. + 0xDE2BCBF6,
  12344. + 0x6F4C52C9,
  12345. + 0xB5C55DF0,
  12346. + 0xEC07A28F,
  12347. + 0x9B2783A2,
  12348. + 0x180E8603,
  12349. + 0xE39E772C,
  12350. + 0x2E36CE3B,
  12351. + 0x32905E46,
  12352. + 0xCA18217C,
  12353. + 0xF1746C08,
  12354. + 0x4ABC9804,
  12355. + 0x670C354E,
  12356. + 0x7096966D,
  12357. + 0x9ED52907,
  12358. + 0x208552BB,
  12359. + 0x1C62F356,
  12360. + 0xDCA3AD96,
  12361. + 0x83655D23,
  12362. + 0xFD24CF5F,
  12363. + 0x69163FA8,
  12364. + 0x1C55D39A,
  12365. + 0x98DA4836,
  12366. + 0xA163BF05,
  12367. + 0xC2007CB8,
  12368. + 0xECE45B3D,
  12369. + 0x49286651,
  12370. + 0x7C4B1FE6,
  12371. + 0xAE9F2411,
  12372. + 0x5A899FA5,
  12373. + 0xEE386BFB,
  12374. + 0xF406B7ED,
  12375. + 0x0BFF5CB6,
  12376. + 0xA637ED6B,
  12377. + 0xF44C42E9,
  12378. + 0x625E7EC6,
  12379. + 0xE485B576,
  12380. + 0x6D51C245,
  12381. + 0x4FE1356D,
  12382. + 0xF25F1437,
  12383. + 0x302B0A6D,
  12384. + 0xCD3A431B,
  12385. + 0xEF9519B3,
  12386. + 0x8E3404DD,
  12387. + 0x514A0879,
  12388. + 0x3B139B22,
  12389. + 0x020BBEA6,
  12390. + 0x8A67CC74,
  12391. + 0x29024E08,
  12392. + 0x80DC1CD1,
  12393. + 0xC4C6628B,
  12394. + 0x2168C234,
  12395. + 0xC90FDAA2,
  12396. + 0xFFFFFFFF,
  12397. + 0xFFFFFFFF,
  12398. +};
  12399. +
  12400. +static __u32 dh_a[] = {
  12401. + 8,
  12402. + 0xdf367516,
  12403. + 0x86459caa,
  12404. + 0xe2d459a4,
  12405. + 0xd910dae0,
  12406. + 0x8a8b5e37,
  12407. + 0x67ab31c6,
  12408. + 0xf0b55ea9,
  12409. + 0x440051d6,
  12410. +};
  12411. +
  12412. +static __u32 dh_b[] = {
  12413. + 8,
  12414. + 0xded92656,
  12415. + 0xe07a048a,
  12416. + 0x6fa452cd,
  12417. + 0x2df89d30,
  12418. + 0xc75f1b0f,
  12419. + 0x8ce3578f,
  12420. + 0x7980a324,
  12421. + 0x5daec786,
  12422. +};
  12423. +
  12424. +static __u32 dh_g[] = {
  12425. + 1,
  12426. + 2,
  12427. +};
  12428. +
  12429. +int main(void)
  12430. +{
  12431. + int i;
  12432. + __u32 *k;
  12433. + k = dwc_modpow(NULL, dh_g, dh_a, dh_p);
  12434. +
  12435. + printf("\n\n");
  12436. + for (i=0; i<k[0]; i++) {
  12437. + __u32 word32 = k[k[0] - i];
  12438. + __u16 l = word32 & 0xffff;
  12439. + __u16 m = (word32 & 0xffff0000) >> 16;
  12440. + printf("%04x %04x ", m, l);
  12441. + if (!((i + 1)%13)) printf("\n");
  12442. + }
  12443. + printf("\n\n");
  12444. +
  12445. + if ((k[0] == 0x60) && (k[1] == 0x28e490e5) && (k[0x60] == 0x5a0d3d4e)) {
  12446. + printf("PASS\n\n");
  12447. + }
  12448. + else {
  12449. + printf("FAIL\n\n");
  12450. + }
  12451. +
  12452. +}
  12453. +
  12454. +#endif /* UNITTEST */
  12455. +
  12456. +#endif /* CONFIG_MACH_IPMATE */
  12457. +
  12458. +#endif /*DWC_CRYPTOLIB */
  12459. --- /dev/null
  12460. +++ b/drivers/usb/host/dwc_common_port/dwc_modpow.h
  12461. @@ -0,0 +1,34 @@
  12462. +/*
  12463. + * dwc_modpow.h
  12464. + * See dwc_modpow.c for license and changes
  12465. + */
  12466. +#ifndef _DWC_MODPOW_H
  12467. +#define _DWC_MODPOW_H
  12468. +
  12469. +#ifdef __cplusplus
  12470. +extern "C" {
  12471. +#endif
  12472. +
  12473. +#include "dwc_os.h"
  12474. +
  12475. +/** @file
  12476. + *
  12477. + * This file defines the module exponentiation function which is only used
  12478. + * internally by the DWC UWB modules for calculation of PKs during numeric
  12479. + * association. The routine is taken from the PUTTY, an open source terminal
  12480. + * emulator. The PUTTY License is preserved in the dwc_modpow.c file.
  12481. + *
  12482. + */
  12483. +
  12484. +typedef uint32_t BignumInt;
  12485. +typedef uint64_t BignumDblInt;
  12486. +typedef BignumInt *Bignum;
  12487. +
  12488. +/* Compute modular exponentiaion */
  12489. +extern Bignum dwc_modpow(void *mem_ctx, Bignum base_in, Bignum exp, Bignum mod);
  12490. +
  12491. +#ifdef __cplusplus
  12492. +}
  12493. +#endif
  12494. +
  12495. +#endif /* _LINUX_BIGNUM_H */
  12496. --- /dev/null
  12497. +++ b/drivers/usb/host/dwc_common_port/dwc_notifier.c
  12498. @@ -0,0 +1,319 @@
  12499. +#ifdef DWC_NOTIFYLIB
  12500. +
  12501. +#include "dwc_notifier.h"
  12502. +#include "dwc_list.h"
  12503. +
  12504. +typedef struct dwc_observer {
  12505. + void *observer;
  12506. + dwc_notifier_callback_t callback;
  12507. + void *data;
  12508. + char *notification;
  12509. + DWC_CIRCLEQ_ENTRY(dwc_observer) list_entry;
  12510. +} observer_t;
  12511. +
  12512. +DWC_CIRCLEQ_HEAD(observer_queue, dwc_observer);
  12513. +
  12514. +typedef struct dwc_notifier {
  12515. + void *mem_ctx;
  12516. + void *object;
  12517. + struct observer_queue observers;
  12518. + DWC_CIRCLEQ_ENTRY(dwc_notifier) list_entry;
  12519. +} notifier_t;
  12520. +
  12521. +DWC_CIRCLEQ_HEAD(notifier_queue, dwc_notifier);
  12522. +
  12523. +typedef struct manager {
  12524. + void *mem_ctx;
  12525. + void *wkq_ctx;
  12526. + dwc_workq_t *wq;
  12527. +// dwc_mutex_t *mutex;
  12528. + struct notifier_queue notifiers;
  12529. +} manager_t;
  12530. +
  12531. +static manager_t *manager = NULL;
  12532. +
  12533. +static int create_manager(void *mem_ctx, void *wkq_ctx)
  12534. +{
  12535. + manager = dwc_alloc(mem_ctx, sizeof(manager_t));
  12536. + if (!manager) {
  12537. + return -DWC_E_NO_MEMORY;
  12538. + }
  12539. +
  12540. + DWC_CIRCLEQ_INIT(&manager->notifiers);
  12541. +
  12542. + manager->wq = dwc_workq_alloc(wkq_ctx, "DWC Notification WorkQ");
  12543. + if (!manager->wq) {
  12544. + return -DWC_E_NO_MEMORY;
  12545. + }
  12546. +
  12547. + return 0;
  12548. +}
  12549. +
  12550. +static void free_manager(void)
  12551. +{
  12552. + dwc_workq_free(manager->wq);
  12553. +
  12554. + /* All notifiers must have unregistered themselves before this module
  12555. + * can be removed. Hitting this assertion indicates a programmer
  12556. + * error. */
  12557. + DWC_ASSERT(DWC_CIRCLEQ_EMPTY(&manager->notifiers),
  12558. + "Notification manager being freed before all notifiers have been removed");
  12559. + dwc_free(manager->mem_ctx, manager);
  12560. +}
  12561. +
  12562. +#ifdef DEBUG
  12563. +static void dump_manager(void)
  12564. +{
  12565. + notifier_t *n;
  12566. + observer_t *o;
  12567. +
  12568. + DWC_ASSERT(manager, "Notification manager not found");
  12569. +
  12570. + DWC_DEBUG("List of all notifiers and observers:\n");
  12571. + DWC_CIRCLEQ_FOREACH(n, &manager->notifiers, list_entry) {
  12572. + DWC_DEBUG("Notifier %p has observers:\n", n->object);
  12573. + DWC_CIRCLEQ_FOREACH(o, &n->observers, list_entry) {
  12574. + DWC_DEBUG(" %p watching %s\n", o->observer, o->notification);
  12575. + }
  12576. + }
  12577. +}
  12578. +#else
  12579. +#define dump_manager(...)
  12580. +#endif
  12581. +
  12582. +static observer_t *alloc_observer(void *mem_ctx, void *observer, char *notification,
  12583. + dwc_notifier_callback_t callback, void *data)
  12584. +{
  12585. + observer_t *new_observer = dwc_alloc(mem_ctx, sizeof(observer_t));
  12586. +
  12587. + if (!new_observer) {
  12588. + return NULL;
  12589. + }
  12590. +
  12591. + DWC_CIRCLEQ_INIT_ENTRY(new_observer, list_entry);
  12592. + new_observer->observer = observer;
  12593. + new_observer->notification = notification;
  12594. + new_observer->callback = callback;
  12595. + new_observer->data = data;
  12596. + return new_observer;
  12597. +}
  12598. +
  12599. +static void free_observer(void *mem_ctx, observer_t *observer)
  12600. +{
  12601. + dwc_free(mem_ctx, observer);
  12602. +}
  12603. +
  12604. +static notifier_t *alloc_notifier(void *mem_ctx, void *object)
  12605. +{
  12606. + notifier_t *notifier;
  12607. +
  12608. + if (!object) {
  12609. + return NULL;
  12610. + }
  12611. +
  12612. + notifier = dwc_alloc(mem_ctx, sizeof(notifier_t));
  12613. + if (!notifier) {
  12614. + return NULL;
  12615. + }
  12616. +
  12617. + DWC_CIRCLEQ_INIT(&notifier->observers);
  12618. + DWC_CIRCLEQ_INIT_ENTRY(notifier, list_entry);
  12619. +
  12620. + notifier->mem_ctx = mem_ctx;
  12621. + notifier->object = object;
  12622. + return notifier;
  12623. +}
  12624. +
  12625. +static void free_notifier(notifier_t *notifier)
  12626. +{
  12627. + observer_t *observer;
  12628. +
  12629. + DWC_CIRCLEQ_FOREACH(observer, &notifier->observers, list_entry) {
  12630. + free_observer(notifier->mem_ctx, observer);
  12631. + }
  12632. +
  12633. + dwc_free(notifier->mem_ctx, notifier);
  12634. +}
  12635. +
  12636. +static notifier_t *find_notifier(void *object)
  12637. +{
  12638. + notifier_t *notifier;
  12639. +
  12640. + DWC_ASSERT(manager, "Notification manager not found");
  12641. +
  12642. + if (!object) {
  12643. + return NULL;
  12644. + }
  12645. +
  12646. + DWC_CIRCLEQ_FOREACH(notifier, &manager->notifiers, list_entry) {
  12647. + if (notifier->object == object) {
  12648. + return notifier;
  12649. + }
  12650. + }
  12651. +
  12652. + return NULL;
  12653. +}
  12654. +
  12655. +int dwc_alloc_notification_manager(void *mem_ctx, void *wkq_ctx)
  12656. +{
  12657. + return create_manager(mem_ctx, wkq_ctx);
  12658. +}
  12659. +
  12660. +void dwc_free_notification_manager(void)
  12661. +{
  12662. + free_manager();
  12663. +}
  12664. +
  12665. +dwc_notifier_t *dwc_register_notifier(void *mem_ctx, void *object)
  12666. +{
  12667. + notifier_t *notifier;
  12668. +
  12669. + DWC_ASSERT(manager, "Notification manager not found");
  12670. +
  12671. + notifier = find_notifier(object);
  12672. + if (notifier) {
  12673. + DWC_ERROR("Notifier %p is already registered\n", object);
  12674. + return NULL;
  12675. + }
  12676. +
  12677. + notifier = alloc_notifier(mem_ctx, object);
  12678. + if (!notifier) {
  12679. + return NULL;
  12680. + }
  12681. +
  12682. + DWC_CIRCLEQ_INSERT_TAIL(&manager->notifiers, notifier, list_entry);
  12683. +
  12684. + DWC_INFO("Notifier %p registered", object);
  12685. + dump_manager();
  12686. +
  12687. + return notifier;
  12688. +}
  12689. +
  12690. +void dwc_unregister_notifier(dwc_notifier_t *notifier)
  12691. +{
  12692. + DWC_ASSERT(manager, "Notification manager not found");
  12693. +
  12694. + if (!DWC_CIRCLEQ_EMPTY(&notifier->observers)) {
  12695. + observer_t *o;
  12696. +
  12697. + DWC_ERROR("Notifier %p has active observers when removing\n", notifier->object);
  12698. + DWC_CIRCLEQ_FOREACH(o, &notifier->observers, list_entry) {
  12699. + DWC_DEBUGC(" %p watching %s\n", o->observer, o->notification);
  12700. + }
  12701. +
  12702. + DWC_ASSERT(DWC_CIRCLEQ_EMPTY(&notifier->observers),
  12703. + "Notifier %p has active observers when removing", notifier);
  12704. + }
  12705. +
  12706. + DWC_CIRCLEQ_REMOVE_INIT(&manager->notifiers, notifier, list_entry);
  12707. + free_notifier(notifier);
  12708. +
  12709. + DWC_INFO("Notifier unregistered");
  12710. + dump_manager();
  12711. +}
  12712. +
  12713. +/* Add an observer to observe the notifier for a particular state, event, or notification. */
  12714. +int dwc_add_observer(void *observer, void *object, char *notification,
  12715. + dwc_notifier_callback_t callback, void *data)
  12716. +{
  12717. + notifier_t *notifier = find_notifier(object);
  12718. + observer_t *new_observer;
  12719. +
  12720. + if (!notifier) {
  12721. + DWC_ERROR("Notifier %p is not found when adding observer\n", object);
  12722. + return -DWC_E_INVALID;
  12723. + }
  12724. +
  12725. + new_observer = alloc_observer(notifier->mem_ctx, observer, notification, callback, data);
  12726. + if (!new_observer) {
  12727. + return -DWC_E_NO_MEMORY;
  12728. + }
  12729. +
  12730. + DWC_CIRCLEQ_INSERT_TAIL(&notifier->observers, new_observer, list_entry);
  12731. +
  12732. + DWC_INFO("Added observer %p to notifier %p observing notification %s, callback=%p, data=%p",
  12733. + observer, object, notification, callback, data);
  12734. +
  12735. + dump_manager();
  12736. + return 0;
  12737. +}
  12738. +
  12739. +int dwc_remove_observer(void *observer)
  12740. +{
  12741. + notifier_t *n;
  12742. +
  12743. + DWC_ASSERT(manager, "Notification manager not found");
  12744. +
  12745. + DWC_CIRCLEQ_FOREACH(n, &manager->notifiers, list_entry) {
  12746. + observer_t *o;
  12747. + observer_t *o2;
  12748. +
  12749. + DWC_CIRCLEQ_FOREACH_SAFE(o, o2, &n->observers, list_entry) {
  12750. + if (o->observer == observer) {
  12751. + DWC_CIRCLEQ_REMOVE_INIT(&n->observers, o, list_entry);
  12752. + DWC_INFO("Removing observer %p from notifier %p watching notification %s:",
  12753. + o->observer, n->object, o->notification);
  12754. + free_observer(n->mem_ctx, o);
  12755. + }
  12756. + }
  12757. + }
  12758. +
  12759. + dump_manager();
  12760. + return 0;
  12761. +}
  12762. +
  12763. +typedef struct callback_data {
  12764. + void *mem_ctx;
  12765. + dwc_notifier_callback_t cb;
  12766. + void *observer;
  12767. + void *data;
  12768. + void *object;
  12769. + char *notification;
  12770. + void *notification_data;
  12771. +} cb_data_t;
  12772. +
  12773. +static void cb_task(void *data)
  12774. +{
  12775. + cb_data_t *cb = (cb_data_t *)data;
  12776. +
  12777. + cb->cb(cb->object, cb->notification, cb->observer, cb->notification_data, cb->data);
  12778. + dwc_free(cb->mem_ctx, cb);
  12779. +}
  12780. +
  12781. +void dwc_notify(dwc_notifier_t *notifier, char *notification, void *notification_data)
  12782. +{
  12783. + observer_t *o;
  12784. +
  12785. + DWC_ASSERT(manager, "Notification manager not found");
  12786. +
  12787. + DWC_CIRCLEQ_FOREACH(o, &notifier->observers, list_entry) {
  12788. + int len = DWC_STRLEN(notification);
  12789. +
  12790. + if (DWC_STRLEN(o->notification) != len) {
  12791. + continue;
  12792. + }
  12793. +
  12794. + if (DWC_STRNCMP(o->notification, notification, len) == 0) {
  12795. + cb_data_t *cb_data = dwc_alloc(notifier->mem_ctx, sizeof(cb_data_t));
  12796. +
  12797. + if (!cb_data) {
  12798. + DWC_ERROR("Failed to allocate callback data\n");
  12799. + return;
  12800. + }
  12801. +
  12802. + cb_data->mem_ctx = notifier->mem_ctx;
  12803. + cb_data->cb = o->callback;
  12804. + cb_data->observer = o->observer;
  12805. + cb_data->data = o->data;
  12806. + cb_data->object = notifier->object;
  12807. + cb_data->notification = notification;
  12808. + cb_data->notification_data = notification_data;
  12809. + DWC_DEBUGC("Observer found %p for notification %s\n", o->observer, notification);
  12810. + DWC_WORKQ_SCHEDULE(manager->wq, cb_task, cb_data,
  12811. + "Notify callback from %p for Notification %s, to observer %p",
  12812. + cb_data->object, notification, cb_data->observer);
  12813. + }
  12814. + }
  12815. +}
  12816. +
  12817. +#endif /* DWC_NOTIFYLIB */
  12818. --- /dev/null
  12819. +++ b/drivers/usb/host/dwc_common_port/dwc_notifier.h
  12820. @@ -0,0 +1,122 @@
  12821. +
  12822. +#ifndef __DWC_NOTIFIER_H__
  12823. +#define __DWC_NOTIFIER_H__
  12824. +
  12825. +#ifdef __cplusplus
  12826. +extern "C" {
  12827. +#endif
  12828. +
  12829. +#include "dwc_os.h"
  12830. +
  12831. +/** @file
  12832. + *
  12833. + * A simple implementation of the Observer pattern. Any "module" can
  12834. + * register as an observer or notifier. The notion of "module" is abstract and
  12835. + * can mean anything used to identify either an observer or notifier. Usually
  12836. + * it will be a pointer to a data structure which contains some state, ie an
  12837. + * object.
  12838. + *
  12839. + * Before any notifiers can be added, the global notification manager must be
  12840. + * brought up with dwc_alloc_notification_manager().
  12841. + * dwc_free_notification_manager() will bring it down and free all resources.
  12842. + * These would typically be called upon module load and unload. The
  12843. + * notification manager is a single global instance that handles all registered
  12844. + * observable modules and observers so this should be done only once.
  12845. + *
  12846. + * A module can be observable by using Notifications to publicize some general
  12847. + * information about it's state or operation. It does not care who listens, or
  12848. + * even if anyone listens, or what they do with the information. The observable
  12849. + * modules do not need to know any information about it's observers or their
  12850. + * interface, or their state or data.
  12851. + *
  12852. + * Any module can register to emit Notifications. It should publish a list of
  12853. + * notifications that it can emit and their behavior, such as when they will get
  12854. + * triggered, and what information will be provided to the observer. Then it
  12855. + * should register itself as an observable module. See dwc_register_notifier().
  12856. + *
  12857. + * Any module can observe any observable, registered module, provided it has a
  12858. + * handle to the other module and knows what notifications to observe. See
  12859. + * dwc_add_observer().
  12860. + *
  12861. + * A function of type dwc_notifier_callback_t is called whenever a notification
  12862. + * is triggered with one or more observers observing it. This function is
  12863. + * called in it's own process so it may sleep or block if needed. It is
  12864. + * guaranteed to be called sometime after the notification has occurred and will
  12865. + * be called once per each time the notification is triggered. It will NOT be
  12866. + * called in the same process context used to trigger the notification.
  12867. + *
  12868. + * @section Limitiations
  12869. + *
  12870. + * Keep in mind that Notifications that can be triggered in rapid sucession may
  12871. + * schedule too many processes too handle. Be aware of this limitation when
  12872. + * designing to use notifications, and only add notifications for appropriate
  12873. + * observable information.
  12874. + *
  12875. + * Also Notification callbacks are not synchronous. If you need to synchronize
  12876. + * the behavior between module/observer you must use other means. And perhaps
  12877. + * that will mean Notifications are not the proper solution.
  12878. + */
  12879. +
  12880. +struct dwc_notifier;
  12881. +typedef struct dwc_notifier dwc_notifier_t;
  12882. +
  12883. +/** The callback function must be of this type.
  12884. + *
  12885. + * @param object This is the object that is being observed.
  12886. + * @param notification This is the notification that was triggered.
  12887. + * @param observer This is the observer
  12888. + * @param notification_data This is notification-specific data that the notifier
  12889. + * has included in this notification. The value of this should be published in
  12890. + * the documentation of the observable module with the notifications.
  12891. + * @param user_data This is any custom data that the observer provided when
  12892. + * adding itself as an observer to the notification. */
  12893. +typedef void (*dwc_notifier_callback_t)(void *object, char *notification, void *observer,
  12894. + void *notification_data, void *user_data);
  12895. +
  12896. +/** Brings up the notification manager. */
  12897. +extern int dwc_alloc_notification_manager(void *mem_ctx, void *wkq_ctx);
  12898. +/** Brings down the notification manager. */
  12899. +extern void dwc_free_notification_manager(void);
  12900. +
  12901. +/** This function registers an observable module. A dwc_notifier_t object is
  12902. + * returned to the observable module. This is an opaque object that is used by
  12903. + * the observable module to trigger notifications. This object should only be
  12904. + * accessible to functions that are authorized to trigger notifications for this
  12905. + * module. Observers do not need this object. */
  12906. +extern dwc_notifier_t *dwc_register_notifier(void *mem_ctx, void *object);
  12907. +
  12908. +/** This function unregisters an observable module. All observers have to be
  12909. + * removed prior to unregistration. */
  12910. +extern void dwc_unregister_notifier(dwc_notifier_t *notifier);
  12911. +
  12912. +/** Add a module as an observer to the observable module. The observable module
  12913. + * needs to have previously registered with the notification manager.
  12914. + *
  12915. + * @param observer The observer module
  12916. + * @param object The module to observe
  12917. + * @param notification The notification to observe
  12918. + * @param callback The callback function to call
  12919. + * @param user_data Any additional user data to pass into the callback function */
  12920. +extern int dwc_add_observer(void *observer, void *object, char *notification,
  12921. + dwc_notifier_callback_t callback, void *user_data);
  12922. +
  12923. +/** Removes the specified observer from all notifications that it is currently
  12924. + * observing. */
  12925. +extern int dwc_remove_observer(void *observer);
  12926. +
  12927. +/** This function triggers a Notification. It should be called by the
  12928. + * observable module, or any module or library which the observable module
  12929. + * allows to trigger notification on it's behalf. Such as the dwc_cc_t.
  12930. + *
  12931. + * dwc_notify is a non-blocking function. Callbacks are scheduled called in
  12932. + * their own process context for each trigger. Callbacks can be blocking.
  12933. + * dwc_notify can be called from interrupt context if needed.
  12934. + *
  12935. + */
  12936. +void dwc_notify(dwc_notifier_t *notifier, char *notification, void *notification_data);
  12937. +
  12938. +#ifdef __cplusplus
  12939. +}
  12940. +#endif
  12941. +
  12942. +#endif /* __DWC_NOTIFIER_H__ */
  12943. --- /dev/null
  12944. +++ b/drivers/usb/host/dwc_common_port/dwc_os.h
  12945. @@ -0,0 +1,1275 @@
  12946. +/* =========================================================================
  12947. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_os.h $
  12948. + * $Revision: #14 $
  12949. + * $Date: 2010/11/04 $
  12950. + * $Change: 1621695 $
  12951. + *
  12952. + * Synopsys Portability Library Software and documentation
  12953. + * (hereinafter, "Software") is an Unsupported proprietary work of
  12954. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  12955. + * between Synopsys and you.
  12956. + *
  12957. + * The Software IS NOT an item of Licensed Software or Licensed Product
  12958. + * under any End User Software License Agreement or Agreement for
  12959. + * Licensed Product with Synopsys or any supplement thereto. You are
  12960. + * permitted to use and redistribute this Software in source and binary
  12961. + * forms, with or without modification, provided that redistributions
  12962. + * of source code must retain this notice. You may not view, use,
  12963. + * disclose, copy or distribute this file or any information contained
  12964. + * herein except pursuant to this license grant from Synopsys. If you
  12965. + * do not agree with this notice, including the disclaimer below, then
  12966. + * you are not authorized to use the Software.
  12967. + *
  12968. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  12969. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  12970. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  12971. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  12972. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  12973. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  12974. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  12975. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  12976. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12977. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  12978. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  12979. + * DAMAGE.
  12980. + * ========================================================================= */
  12981. +#ifndef _DWC_OS_H_
  12982. +#define _DWC_OS_H_
  12983. +
  12984. +#ifdef __cplusplus
  12985. +extern "C" {
  12986. +#endif
  12987. +
  12988. +/** @file
  12989. + *
  12990. + * DWC portability library, low level os-wrapper functions
  12991. + *
  12992. + */
  12993. +
  12994. +/* These basic types need to be defined by some OS header file or custom header
  12995. + * file for your specific target architecture.
  12996. + *
  12997. + * uint8_t, int8_t, uint16_t, int16_t, uint32_t, int32_t, uint64_t, int64_t
  12998. + *
  12999. + * Any custom or alternate header file must be added and enabled here.
  13000. + */
  13001. +
  13002. +#ifdef DWC_LINUX
  13003. +# include <linux/types.h>
  13004. +# ifdef CONFIG_DEBUG_MUTEXES
  13005. +# include <linux/mutex.h>
  13006. +# endif
  13007. +# include <linux/spinlock.h>
  13008. +# include <linux/errno.h>
  13009. +#endif
  13010. +
  13011. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  13012. +# include <os_dep.h>
  13013. +#endif
  13014. +
  13015. +
  13016. +/** @name Primitive Types and Values */
  13017. +
  13018. +/** We define a boolean type for consistency. Can be either YES or NO */
  13019. +typedef uint8_t dwc_bool_t;
  13020. +#define YES 1
  13021. +#define NO 0
  13022. +
  13023. +#ifdef DWC_LINUX
  13024. +
  13025. +/** @name Error Codes */
  13026. +#define DWC_E_INVALID EINVAL
  13027. +#define DWC_E_NO_MEMORY ENOMEM
  13028. +#define DWC_E_NO_DEVICE ENODEV
  13029. +#define DWC_E_NOT_SUPPORTED EOPNOTSUPP
  13030. +#define DWC_E_TIMEOUT ETIMEDOUT
  13031. +#define DWC_E_BUSY EBUSY
  13032. +#define DWC_E_AGAIN EAGAIN
  13033. +#define DWC_E_RESTART ERESTART
  13034. +#define DWC_E_ABORT ECONNABORTED
  13035. +#define DWC_E_SHUTDOWN ESHUTDOWN
  13036. +#define DWC_E_NO_DATA ENODATA
  13037. +#define DWC_E_DISCONNECT ECONNRESET
  13038. +#define DWC_E_UNKNOWN EINVAL
  13039. +#define DWC_E_NO_STREAM_RES ENOSR
  13040. +#define DWC_E_COMMUNICATION ECOMM
  13041. +#define DWC_E_OVERFLOW EOVERFLOW
  13042. +#define DWC_E_PROTOCOL EPROTO
  13043. +#define DWC_E_IN_PROGRESS EINPROGRESS
  13044. +#define DWC_E_PIPE EPIPE
  13045. +#define DWC_E_IO EIO
  13046. +#define DWC_E_NO_SPACE ENOSPC
  13047. +
  13048. +#else
  13049. +
  13050. +/** @name Error Codes */
  13051. +#define DWC_E_INVALID 1001
  13052. +#define DWC_E_NO_MEMORY 1002
  13053. +#define DWC_E_NO_DEVICE 1003
  13054. +#define DWC_E_NOT_SUPPORTED 1004
  13055. +#define DWC_E_TIMEOUT 1005
  13056. +#define DWC_E_BUSY 1006
  13057. +#define DWC_E_AGAIN 1007
  13058. +#define DWC_E_RESTART 1008
  13059. +#define DWC_E_ABORT 1009
  13060. +#define DWC_E_SHUTDOWN 1010
  13061. +#define DWC_E_NO_DATA 1011
  13062. +#define DWC_E_DISCONNECT 2000
  13063. +#define DWC_E_UNKNOWN 3000
  13064. +#define DWC_E_NO_STREAM_RES 4001
  13065. +#define DWC_E_COMMUNICATION 4002
  13066. +#define DWC_E_OVERFLOW 4003
  13067. +#define DWC_E_PROTOCOL 4004
  13068. +#define DWC_E_IN_PROGRESS 4005
  13069. +#define DWC_E_PIPE 4006
  13070. +#define DWC_E_IO 4007
  13071. +#define DWC_E_NO_SPACE 4008
  13072. +
  13073. +#endif
  13074. +
  13075. +
  13076. +/** @name Tracing/Logging Functions
  13077. + *
  13078. + * These function provide the capability to add tracing, debugging, and error
  13079. + * messages, as well exceptions as assertions. The WUDEV uses these
  13080. + * extensively. These could be logged to the main console, the serial port, an
  13081. + * internal buffer, etc. These functions could also be no-op if they are too
  13082. + * expensive on your system. By default undefining the DEBUG macro already
  13083. + * no-ops some of these functions. */
  13084. +
  13085. +/** Returns non-zero if in interrupt context. */
  13086. +extern dwc_bool_t DWC_IN_IRQ(void);
  13087. +#define dwc_in_irq DWC_IN_IRQ
  13088. +
  13089. +/** Returns "IRQ" if DWC_IN_IRQ is true. */
  13090. +static inline char *dwc_irq(void) {
  13091. + return DWC_IN_IRQ() ? "IRQ" : "";
  13092. +}
  13093. +
  13094. +/** Returns non-zero if in bottom-half context. */
  13095. +extern dwc_bool_t DWC_IN_BH(void);
  13096. +#define dwc_in_bh DWC_IN_BH
  13097. +
  13098. +/** Returns "BH" if DWC_IN_BH is true. */
  13099. +static inline char *dwc_bh(void) {
  13100. + return DWC_IN_BH() ? "BH" : "";
  13101. +}
  13102. +
  13103. +/**
  13104. + * A vprintf() clone. Just call vprintf if you've got it.
  13105. + */
  13106. +extern void DWC_VPRINTF(char *format, va_list args);
  13107. +#define dwc_vprintf DWC_VPRINTF
  13108. +
  13109. +/**
  13110. + * A vsnprintf() clone. Just call vprintf if you've got it.
  13111. + */
  13112. +extern int DWC_VSNPRINTF(char *str, int size, char *format, va_list args);
  13113. +#define dwc_vsnprintf DWC_VSNPRINTF
  13114. +
  13115. +/**
  13116. + * printf() clone. Just call printf if you've go it.
  13117. + */
  13118. +extern void DWC_PRINTF(char *format, ...)
  13119. +/* This provides compiler level static checking of the parameters if you're
  13120. + * using GCC. */
  13121. +#ifdef __GNUC__
  13122. + __attribute__ ((format(printf, 1, 2)));
  13123. +#else
  13124. + ;
  13125. +#endif
  13126. +#define dwc_printf DWC_PRINTF
  13127. +
  13128. +/**
  13129. + * sprintf() clone. Just call sprintf if you've got it.
  13130. + */
  13131. +extern int DWC_SPRINTF(char *string, char *format, ...)
  13132. +#ifdef __GNUC__
  13133. + __attribute__ ((format(printf, 2, 3)));
  13134. +#else
  13135. + ;
  13136. +#endif
  13137. +#define dwc_sprintf DWC_SPRINTF
  13138. +
  13139. +/**
  13140. + * snprintf() clone. Just call snprintf if you've got it.
  13141. + */
  13142. +extern int DWC_SNPRINTF(char *string, int size, char *format, ...)
  13143. +#ifdef __GNUC__
  13144. + __attribute__ ((format(printf, 3, 4)));
  13145. +#else
  13146. + ;
  13147. +#endif
  13148. +#define dwc_snprintf DWC_SNPRINTF
  13149. +
  13150. +/**
  13151. + * Prints a WARNING message. On systems that don't differentiate between
  13152. + * warnings and regular log messages, just print it. Indicates that something
  13153. + * may be wrong with the driver. Works like printf().
  13154. + *
  13155. + * Use the DWC_WARN macro to call this function.
  13156. + */
  13157. +extern void __DWC_WARN(char *format, ...)
  13158. +#ifdef __GNUC__
  13159. + __attribute__ ((format(printf, 1, 2)));
  13160. +#else
  13161. + ;
  13162. +#endif
  13163. +
  13164. +/**
  13165. + * Prints an error message. On systems that don't differentiate between errors
  13166. + * and regular log messages, just print it. Indicates that something went wrong
  13167. + * with the driver. Works like printf().
  13168. + *
  13169. + * Use the DWC_ERROR macro to call this function.
  13170. + */
  13171. +extern void __DWC_ERROR(char *format, ...)
  13172. +#ifdef __GNUC__
  13173. + __attribute__ ((format(printf, 1, 2)));
  13174. +#else
  13175. + ;
  13176. +#endif
  13177. +
  13178. +/**
  13179. + * Prints an exception error message and takes some user-defined action such as
  13180. + * print out a backtrace or trigger a breakpoint. Indicates that something went
  13181. + * abnormally wrong with the driver such as programmer error, or other
  13182. + * exceptional condition. It should not be ignored so even on systems without
  13183. + * printing capability, some action should be taken to notify the developer of
  13184. + * it. Works like printf().
  13185. + */
  13186. +extern void DWC_EXCEPTION(char *format, ...)
  13187. +#ifdef __GNUC__
  13188. + __attribute__ ((format(printf, 1, 2)));
  13189. +#else
  13190. + ;
  13191. +#endif
  13192. +#define dwc_exception DWC_EXCEPTION
  13193. +
  13194. +#ifndef DWC_OTG_DEBUG_LEV
  13195. +#define DWC_OTG_DEBUG_LEV 0
  13196. +#endif
  13197. +
  13198. +#ifdef DEBUG
  13199. +/**
  13200. + * Prints out a debug message. Used for logging/trace messages.
  13201. + *
  13202. + * Use the DWC_DEBUG macro to call this function
  13203. + */
  13204. +extern void __DWC_DEBUG(char *format, ...)
  13205. +#ifdef __GNUC__
  13206. + __attribute__ ((format(printf, 1, 2)));
  13207. +#else
  13208. + ;
  13209. +#endif
  13210. +#else
  13211. +#define __DWC_DEBUG printk
  13212. +#endif
  13213. +
  13214. +/**
  13215. + * Prints out a Debug message.
  13216. + */
  13217. +#define DWC_DEBUG(_format, _args...) __DWC_DEBUG("DEBUG:%s:%s: " _format "\n", \
  13218. + __func__, dwc_irq(), ## _args)
  13219. +#define dwc_debug DWC_DEBUG
  13220. +/**
  13221. + * Prints out a Debug message if enabled at compile time.
  13222. + */
  13223. +#if DWC_OTG_DEBUG_LEV > 0
  13224. +#define DWC_DEBUGC(_format, _args...) DWC_DEBUG(_format, ##_args )
  13225. +#else
  13226. +#define DWC_DEBUGC(_format, _args...)
  13227. +#endif
  13228. +#define dwc_debugc DWC_DEBUGC
  13229. +/**
  13230. + * Prints out an informative message.
  13231. + */
  13232. +#define DWC_INFO(_format, _args...) DWC_PRINTF("INFO:%s: " _format "\n", \
  13233. + dwc_irq(), ## _args)
  13234. +#define dwc_info DWC_INFO
  13235. +/**
  13236. + * Prints out an informative message if enabled at compile time.
  13237. + */
  13238. +#if DWC_OTG_DEBUG_LEV > 1
  13239. +#define DWC_INFOC(_format, _args...) DWC_INFO(_format, ##_args )
  13240. +#else
  13241. +#define DWC_INFOC(_format, _args...)
  13242. +#endif
  13243. +#define dwc_infoc DWC_INFOC
  13244. +/**
  13245. + * Prints out a warning message.
  13246. + */
  13247. +#define DWC_WARN(_format, _args...) __DWC_WARN("WARN:%s:%s:%d: " _format "\n", \
  13248. + dwc_irq(), __func__, __LINE__, ## _args)
  13249. +#define dwc_warn DWC_WARN
  13250. +/**
  13251. + * Prints out an error message.
  13252. + */
  13253. +#define DWC_ERROR(_format, _args...) __DWC_ERROR("ERROR:%s:%s:%d: " _format "\n", \
  13254. + dwc_irq(), __func__, __LINE__, ## _args)
  13255. +#define dwc_error DWC_ERROR
  13256. +
  13257. +#define DWC_PROTO_ERROR(_format, _args...) __DWC_WARN("ERROR:%s:%s:%d: " _format "\n", \
  13258. + dwc_irq(), __func__, __LINE__, ## _args)
  13259. +#define dwc_proto_error DWC_PROTO_ERROR
  13260. +
  13261. +#ifdef DEBUG
  13262. +/** Prints out a exception error message if the _expr expression fails. Disabled
  13263. + * if DEBUG is not enabled. */
  13264. +#define DWC_ASSERT(_expr, _format, _args...) do { \
  13265. + if (!(_expr)) { DWC_EXCEPTION("%s:%s:%d: " _format "\n", dwc_irq(), \
  13266. + __FILE__, __LINE__, ## _args); } \
  13267. + } while (0)
  13268. +#else
  13269. +#define DWC_ASSERT(_x...)
  13270. +#endif
  13271. +#define dwc_assert DWC_ASSERT
  13272. +
  13273. +
  13274. +/** @name Byte Ordering
  13275. + * The following functions are for conversions between processor's byte ordering
  13276. + * and specific ordering you want.
  13277. + */
  13278. +
  13279. +/** Converts 32 bit data in CPU byte ordering to little endian. */
  13280. +extern uint32_t DWC_CPU_TO_LE32(uint32_t *p);
  13281. +#define dwc_cpu_to_le32 DWC_CPU_TO_LE32
  13282. +
  13283. +/** Converts 32 bit data in CPU byte orderint to big endian. */
  13284. +extern uint32_t DWC_CPU_TO_BE32(uint32_t *p);
  13285. +#define dwc_cpu_to_be32 DWC_CPU_TO_BE32
  13286. +
  13287. +/** Converts 32 bit little endian data to CPU byte ordering. */
  13288. +extern uint32_t DWC_LE32_TO_CPU(uint32_t *p);
  13289. +#define dwc_le32_to_cpu DWC_LE32_TO_CPU
  13290. +
  13291. +/** Converts 32 bit big endian data to CPU byte ordering. */
  13292. +extern uint32_t DWC_BE32_TO_CPU(uint32_t *p);
  13293. +#define dwc_be32_to_cpu DWC_BE32_TO_CPU
  13294. +
  13295. +/** Converts 16 bit data in CPU byte ordering to little endian. */
  13296. +extern uint16_t DWC_CPU_TO_LE16(uint16_t *p);
  13297. +#define dwc_cpu_to_le16 DWC_CPU_TO_LE16
  13298. +
  13299. +/** Converts 16 bit data in CPU byte orderint to big endian. */
  13300. +extern uint16_t DWC_CPU_TO_BE16(uint16_t *p);
  13301. +#define dwc_cpu_to_be16 DWC_CPU_TO_BE16
  13302. +
  13303. +/** Converts 16 bit little endian data to CPU byte ordering. */
  13304. +extern uint16_t DWC_LE16_TO_CPU(uint16_t *p);
  13305. +#define dwc_le16_to_cpu DWC_LE16_TO_CPU
  13306. +
  13307. +/** Converts 16 bit bi endian data to CPU byte ordering. */
  13308. +extern uint16_t DWC_BE16_TO_CPU(uint16_t *p);
  13309. +#define dwc_be16_to_cpu DWC_BE16_TO_CPU
  13310. +
  13311. +
  13312. +/** @name Register Read/Write
  13313. + *
  13314. + * The following six functions should be implemented to read/write registers of
  13315. + * 32-bit and 64-bit sizes. All modules use this to read/write register values.
  13316. + * The reg value is a pointer to the register calculated from the void *base
  13317. + * variable passed into the driver when it is started. */
  13318. +
  13319. +#ifdef DWC_LINUX
  13320. +/* Linux doesn't need any extra parameters for register read/write, so we
  13321. + * just throw away the IO context parameter.
  13322. + */
  13323. +/** Reads the content of a 32-bit register. */
  13324. +extern uint32_t DWC_READ_REG32(uint32_t volatile *reg);
  13325. +#define dwc_read_reg32(_ctx_,_reg_) DWC_READ_REG32(_reg_)
  13326. +
  13327. +/** Reads the content of a 64-bit register. */
  13328. +extern uint64_t DWC_READ_REG64(uint64_t volatile *reg);
  13329. +#define dwc_read_reg64(_ctx_,_reg_) DWC_READ_REG64(_reg_)
  13330. +
  13331. +/** Writes to a 32-bit register. */
  13332. +extern void DWC_WRITE_REG32(uint32_t volatile *reg, uint32_t value);
  13333. +#define dwc_write_reg32(_ctx_,_reg_,_val_) DWC_WRITE_REG32(_reg_, _val_)
  13334. +
  13335. +/** Writes to a 64-bit register. */
  13336. +extern void DWC_WRITE_REG64(uint64_t volatile *reg, uint64_t value);
  13337. +#define dwc_write_reg64(_ctx_,_reg_,_val_) DWC_WRITE_REG64(_reg_, _val_)
  13338. +
  13339. +/**
  13340. + * Modify bit values in a register. Using the
  13341. + * algorithm: (reg_contents & ~clear_mask) | set_mask.
  13342. + */
  13343. +extern void DWC_MODIFY_REG32(uint32_t volatile *reg, uint32_t clear_mask, uint32_t set_mask);
  13344. +#define dwc_modify_reg32(_ctx_,_reg_,_cmsk_,_smsk_) DWC_MODIFY_REG32(_reg_,_cmsk_,_smsk_)
  13345. +extern void DWC_MODIFY_REG64(uint64_t volatile *reg, uint64_t clear_mask, uint64_t set_mask);
  13346. +#define dwc_modify_reg64(_ctx_,_reg_,_cmsk_,_smsk_) DWC_MODIFY_REG64(_reg_,_cmsk_,_smsk_)
  13347. +
  13348. +#endif /* DWC_LINUX */
  13349. +
  13350. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  13351. +typedef struct dwc_ioctx {
  13352. + struct device *dev;
  13353. + bus_space_tag_t iot;
  13354. + bus_space_handle_t ioh;
  13355. +} dwc_ioctx_t;
  13356. +
  13357. +/** BSD needs two extra parameters for register read/write, so we pass
  13358. + * them in using the IO context parameter.
  13359. + */
  13360. +/** Reads the content of a 32-bit register. */
  13361. +extern uint32_t DWC_READ_REG32(void *io_ctx, uint32_t volatile *reg);
  13362. +#define dwc_read_reg32 DWC_READ_REG32
  13363. +
  13364. +/** Reads the content of a 64-bit register. */
  13365. +extern uint64_t DWC_READ_REG64(void *io_ctx, uint64_t volatile *reg);
  13366. +#define dwc_read_reg64 DWC_READ_REG64
  13367. +
  13368. +/** Writes to a 32-bit register. */
  13369. +extern void DWC_WRITE_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t value);
  13370. +#define dwc_write_reg32 DWC_WRITE_REG32
  13371. +
  13372. +/** Writes to a 64-bit register. */
  13373. +extern void DWC_WRITE_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t value);
  13374. +#define dwc_write_reg64 DWC_WRITE_REG64
  13375. +
  13376. +/**
  13377. + * Modify bit values in a register. Using the
  13378. + * algorithm: (reg_contents & ~clear_mask) | set_mask.
  13379. + */
  13380. +extern void DWC_MODIFY_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t clear_mask, uint32_t set_mask);
  13381. +#define dwc_modify_reg32 DWC_MODIFY_REG32
  13382. +extern void DWC_MODIFY_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t clear_mask, uint64_t set_mask);
  13383. +#define dwc_modify_reg64 DWC_MODIFY_REG64
  13384. +
  13385. +#endif /* DWC_FREEBSD || DWC_NETBSD */
  13386. +
  13387. +/** @cond */
  13388. +
  13389. +/** @name Some convenience MACROS used internally. Define DWC_DEBUG_REGS to log the
  13390. + * register writes. */
  13391. +
  13392. +#ifdef DWC_LINUX
  13393. +
  13394. +# ifdef DWC_DEBUG_REGS
  13395. +
  13396. +#define dwc_define_read_write_reg_n(_reg,_container_type) \
  13397. +static inline uint32_t dwc_read_##_reg##_n(_container_type *container, int num) { \
  13398. + return DWC_READ_REG32(&container->regs->_reg[num]); \
  13399. +} \
  13400. +static inline void dwc_write_##_reg##_n(_container_type *container, int num, uint32_t data) { \
  13401. + DWC_DEBUG("WRITING %8s[%d]: %p: %08x", #_reg, num, \
  13402. + &(((uint32_t*)container->regs->_reg)[num]), data); \
  13403. + DWC_WRITE_REG32(&(((uint32_t*)container->regs->_reg)[num]), data); \
  13404. +}
  13405. +
  13406. +#define dwc_define_read_write_reg(_reg,_container_type) \
  13407. +static inline uint32_t dwc_read_##_reg(_container_type *container) { \
  13408. + return DWC_READ_REG32(&container->regs->_reg); \
  13409. +} \
  13410. +static inline void dwc_write_##_reg(_container_type *container, uint32_t data) { \
  13411. + DWC_DEBUG("WRITING %11s: %p: %08x", #_reg, &container->regs->_reg, data); \
  13412. + DWC_WRITE_REG32(&container->regs->_reg, data); \
  13413. +}
  13414. +
  13415. +# else /* DWC_DEBUG_REGS */
  13416. +
  13417. +#define dwc_define_read_write_reg_n(_reg,_container_type) \
  13418. +static inline uint32_t dwc_read_##_reg##_n(_container_type *container, int num) { \
  13419. + return DWC_READ_REG32(&container->regs->_reg[num]); \
  13420. +} \
  13421. +static inline void dwc_write_##_reg##_n(_container_type *container, int num, uint32_t data) { \
  13422. + DWC_WRITE_REG32(&(((uint32_t*)container->regs->_reg)[num]), data); \
  13423. +}
  13424. +
  13425. +#define dwc_define_read_write_reg(_reg,_container_type) \
  13426. +static inline uint32_t dwc_read_##_reg(_container_type *container) { \
  13427. + return DWC_READ_REG32(&container->regs->_reg); \
  13428. +} \
  13429. +static inline void dwc_write_##_reg(_container_type *container, uint32_t data) { \
  13430. + DWC_WRITE_REG32(&container->regs->_reg, data); \
  13431. +}
  13432. +
  13433. +# endif /* DWC_DEBUG_REGS */
  13434. +
  13435. +#endif /* DWC_LINUX */
  13436. +
  13437. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  13438. +
  13439. +# ifdef DWC_DEBUG_REGS
  13440. +
  13441. +#define dwc_define_read_write_reg_n(_reg,_container_type) \
  13442. +static inline uint32_t dwc_read_##_reg##_n(void *io_ctx, _container_type *container, int num) { \
  13443. + return DWC_READ_REG32(io_ctx, &container->regs->_reg[num]); \
  13444. +} \
  13445. +static inline void dwc_write_##_reg##_n(void *io_ctx, _container_type *container, int num, uint32_t data) { \
  13446. + DWC_DEBUG("WRITING %8s[%d]: %p: %08x", #_reg, num, \
  13447. + &(((uint32_t*)container->regs->_reg)[num]), data); \
  13448. + DWC_WRITE_REG32(io_ctx, &(((uint32_t*)container->regs->_reg)[num]), data); \
  13449. +}
  13450. +
  13451. +#define dwc_define_read_write_reg(_reg,_container_type) \
  13452. +static inline uint32_t dwc_read_##_reg(void *io_ctx, _container_type *container) { \
  13453. + return DWC_READ_REG32(io_ctx, &container->regs->_reg); \
  13454. +} \
  13455. +static inline void dwc_write_##_reg(void *io_ctx, _container_type *container, uint32_t data) { \
  13456. + DWC_DEBUG("WRITING %11s: %p: %08x", #_reg, &container->regs->_reg, data); \
  13457. + DWC_WRITE_REG32(io_ctx, &container->regs->_reg, data); \
  13458. +}
  13459. +
  13460. +# else /* DWC_DEBUG_REGS */
  13461. +
  13462. +#define dwc_define_read_write_reg_n(_reg,_container_type) \
  13463. +static inline uint32_t dwc_read_##_reg##_n(void *io_ctx, _container_type *container, int num) { \
  13464. + return DWC_READ_REG32(io_ctx, &container->regs->_reg[num]); \
  13465. +} \
  13466. +static inline void dwc_write_##_reg##_n(void *io_ctx, _container_type *container, int num, uint32_t data) { \
  13467. + DWC_WRITE_REG32(io_ctx, &(((uint32_t*)container->regs->_reg)[num]), data); \
  13468. +}
  13469. +
  13470. +#define dwc_define_read_write_reg(_reg,_container_type) \
  13471. +static inline uint32_t dwc_read_##_reg(void *io_ctx, _container_type *container) { \
  13472. + return DWC_READ_REG32(io_ctx, &container->regs->_reg); \
  13473. +} \
  13474. +static inline void dwc_write_##_reg(void *io_ctx, _container_type *container, uint32_t data) { \
  13475. + DWC_WRITE_REG32(io_ctx, &container->regs->_reg, data); \
  13476. +}
  13477. +
  13478. +# endif /* DWC_DEBUG_REGS */
  13479. +
  13480. +#endif /* DWC_FREEBSD || DWC_NETBSD */
  13481. +
  13482. +/** @endcond */
  13483. +
  13484. +
  13485. +#ifdef DWC_CRYPTOLIB
  13486. +/** @name Crypto Functions
  13487. + *
  13488. + * These are the low-level cryptographic functions used by the driver. */
  13489. +
  13490. +/** Perform AES CBC */
  13491. +extern int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out);
  13492. +#define dwc_aes_cbc DWC_AES_CBC
  13493. +
  13494. +/** Fill the provided buffer with random bytes. These should be cryptographic grade random numbers. */
  13495. +extern void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length);
  13496. +#define dwc_random_bytes DWC_RANDOM_BYTES
  13497. +
  13498. +/** Perform the SHA-256 hash function */
  13499. +extern int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out);
  13500. +#define dwc_sha256 DWC_SHA256
  13501. +
  13502. +/** Calculated the HMAC-SHA256 */
  13503. +extern int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t *out);
  13504. +#define dwc_hmac_sha256 DWC_HMAC_SHA256
  13505. +
  13506. +#endif /* DWC_CRYPTOLIB */
  13507. +
  13508. +
  13509. +/** @name Memory Allocation
  13510. + *
  13511. + * These function provide access to memory allocation. There are only 2 DMA
  13512. + * functions and 3 Regular memory functions that need to be implemented. None
  13513. + * of the memory debugging routines need to be implemented. The allocation
  13514. + * routines all ZERO the contents of the memory.
  13515. + *
  13516. + * Defining DWC_DEBUG_MEMORY turns on memory debugging and statistic gathering.
  13517. + * This checks for memory leaks, keeping track of alloc/free pairs. It also
  13518. + * keeps track of how much memory the driver is using at any given time. */
  13519. +
  13520. +#define DWC_PAGE_SIZE 4096
  13521. +#define DWC_PAGE_OFFSET(addr) (((uint32_t)addr) & 0xfff)
  13522. +#define DWC_PAGE_ALIGNED(addr) ((((uint32_t)addr) & 0xfff) == 0)
  13523. +
  13524. +#define DWC_INVALID_DMA_ADDR 0x0
  13525. +
  13526. +#ifdef DWC_LINUX
  13527. +/** Type for a DMA address */
  13528. +typedef dma_addr_t dwc_dma_t;
  13529. +#endif
  13530. +
  13531. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  13532. +typedef bus_addr_t dwc_dma_t;
  13533. +#endif
  13534. +
  13535. +#ifdef DWC_FREEBSD
  13536. +typedef struct dwc_dmactx {
  13537. + struct device *dev;
  13538. + bus_dma_tag_t dma_tag;
  13539. + bus_dmamap_t dma_map;
  13540. + bus_addr_t dma_paddr;
  13541. + void *dma_vaddr;
  13542. +} dwc_dmactx_t;
  13543. +#endif
  13544. +
  13545. +#ifdef DWC_NETBSD
  13546. +typedef struct dwc_dmactx {
  13547. + struct device *dev;
  13548. + bus_dma_tag_t dma_tag;
  13549. + bus_dmamap_t dma_map;
  13550. + bus_dma_segment_t segs[1];
  13551. + int nsegs;
  13552. + bus_addr_t dma_paddr;
  13553. + void *dma_vaddr;
  13554. +} dwc_dmactx_t;
  13555. +#endif
  13556. +
  13557. +/* @todo these functions will be added in the future */
  13558. +#if 0
  13559. +/**
  13560. + * Creates a DMA pool from which you can allocate DMA buffers. Buffers
  13561. + * allocated from this pool will be guaranteed to meet the size, alignment, and
  13562. + * boundary requirements specified.
  13563. + *
  13564. + * @param[in] size Specifies the size of the buffers that will be allocated from
  13565. + * this pool.
  13566. + * @param[in] align Specifies the byte alignment requirements of the buffers
  13567. + * allocated from this pool. Must be a power of 2.
  13568. + * @param[in] boundary Specifies the N-byte boundary that buffers allocated from
  13569. + * this pool must not cross.
  13570. + *
  13571. + * @returns A pointer to an internal opaque structure which is not to be
  13572. + * accessed outside of these library functions. Use this handle to specify
  13573. + * which pools to allocate/free DMA buffers from and also to destroy the pool,
  13574. + * when you are done with it.
  13575. + */
  13576. +extern dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size, uint32_t align, uint32_t boundary);
  13577. +
  13578. +/**
  13579. + * Destroy a DMA pool. All buffers allocated from that pool must be freed first.
  13580. + */
  13581. +extern void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool);
  13582. +
  13583. +/**
  13584. + * Allocate a buffer from the specified DMA pool and zeros its contents.
  13585. + */
  13586. +extern void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr);
  13587. +
  13588. +/**
  13589. + * Free a previously allocated buffer from the DMA pool.
  13590. + */
  13591. +extern void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr);
  13592. +#endif
  13593. +
  13594. +/** Allocates a DMA capable buffer and zeroes its contents. */
  13595. +extern void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr);
  13596. +
  13597. +/** Allocates a DMA capable buffer and zeroes its contents in atomic contest */
  13598. +extern void *__DWC_DMA_ALLOC_ATOMIC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr);
  13599. +
  13600. +/** Frees a previously allocated buffer. */
  13601. +extern void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr);
  13602. +
  13603. +/** Allocates a block of memory and zeroes its contents. */
  13604. +extern void *__DWC_ALLOC(void *mem_ctx, uint32_t size);
  13605. +
  13606. +/** Allocates a block of memory and zeroes its contents, in an atomic manner
  13607. + * which can be used inside interrupt context. The size should be sufficiently
  13608. + * small, a few KB at most, such that failures are not likely to occur. Can just call
  13609. + * __DWC_ALLOC if it is atomic. */
  13610. +extern void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size);
  13611. +
  13612. +/** Frees a previously allocated buffer. */
  13613. +extern void __DWC_FREE(void *mem_ctx, void *addr);
  13614. +
  13615. +#ifndef DWC_DEBUG_MEMORY
  13616. +
  13617. +#define DWC_ALLOC(_size_) __DWC_ALLOC(NULL, _size_)
  13618. +#define DWC_ALLOC_ATOMIC(_size_) __DWC_ALLOC_ATOMIC(NULL, _size_)
  13619. +#define DWC_FREE(_addr_) __DWC_FREE(NULL, _addr_)
  13620. +
  13621. +# ifdef DWC_LINUX
  13622. +#define DWC_DMA_ALLOC(_dev, _size_, _dma_) __DWC_DMA_ALLOC(_dev, _size_, _dma_)
  13623. +#define DWC_DMA_ALLOC_ATOMIC(_dev, _size_, _dma_) __DWC_DMA_ALLOC_ATOMIC(_dev, _size_, _dma_)
  13624. +#define DWC_DMA_FREE(_dev, _size_,_virt_, _dma_) __DWC_DMA_FREE(_dev, _size_, _virt_, _dma_)
  13625. +# endif
  13626. +
  13627. +# if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  13628. +#define DWC_DMA_ALLOC __DWC_DMA_ALLOC
  13629. +#define DWC_DMA_FREE __DWC_DMA_FREE
  13630. +# endif
  13631. +extern void *dwc_dma_alloc_atomic_debug(uint32_t size, dwc_dma_t *dma_addr, char const *func, int line);
  13632. +
  13633. +#else /* DWC_DEBUG_MEMORY */
  13634. +
  13635. +extern void *dwc_alloc_debug(void *mem_ctx, uint32_t size, char const *func, int line);
  13636. +extern void *dwc_alloc_atomic_debug(void *mem_ctx, uint32_t size, char const *func, int line);
  13637. +extern void dwc_free_debug(void *mem_ctx, void *addr, char const *func, int line);
  13638. +extern void *dwc_dma_alloc_debug(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr,
  13639. + char const *func, int line);
  13640. +extern void *dwc_dma_alloc_atomic_debug(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr,
  13641. + char const *func, int line);
  13642. +extern void dwc_dma_free_debug(void *dma_ctx, uint32_t size, void *virt_addr,
  13643. + dwc_dma_t dma_addr, char const *func, int line);
  13644. +
  13645. +extern int dwc_memory_debug_start(void *mem_ctx);
  13646. +extern void dwc_memory_debug_stop(void);
  13647. +extern void dwc_memory_debug_report(void);
  13648. +
  13649. +#define DWC_ALLOC(_size_) dwc_alloc_debug(NULL, _size_, __func__, __LINE__)
  13650. +#define DWC_ALLOC_ATOMIC(_size_) dwc_alloc_atomic_debug(NULL, _size_, \
  13651. + __func__, __LINE__)
  13652. +#define DWC_FREE(_addr_) dwc_free_debug(NULL, _addr_, __func__, __LINE__)
  13653. +
  13654. +# ifdef DWC_LINUX
  13655. +#define DWC_DMA_ALLOC(_dev, _size_, _dma_) \
  13656. + dwc_dma_alloc_debug(_dev, _size_, _dma_, __func__, __LINE__)
  13657. +#define DWC_DMA_ALLOC_ATOMIC(_dev, _size_, _dma_) \
  13658. + dwc_dma_alloc_atomic_debug(_dev, _size_, _dma_, __func__, __LINE__)
  13659. +#define DWC_DMA_FREE(_dev, _size_, _virt_, _dma_) \
  13660. + dwc_dma_free_debug(_dev, _size_, _virt_, _dma_, __func__, __LINE__)
  13661. +# endif
  13662. +
  13663. +# if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  13664. +#define DWC_DMA_ALLOC(_ctx_,_size_,_dma_) dwc_dma_alloc_debug(_ctx_, _size_, \
  13665. + _dma_, __func__, __LINE__)
  13666. +#define DWC_DMA_FREE(_ctx_,_size_,_virt_,_dma_) dwc_dma_free_debug(_ctx_, _size_, \
  13667. + _virt_, _dma_, __func__, __LINE__)
  13668. +# endif
  13669. +
  13670. +#endif /* DWC_DEBUG_MEMORY */
  13671. +
  13672. +#define dwc_alloc(_ctx_,_size_) DWC_ALLOC(_size_)
  13673. +#define dwc_alloc_atomic(_ctx_,_size_) DWC_ALLOC_ATOMIC(_size_)
  13674. +#define dwc_free(_ctx_,_addr_) DWC_FREE(_addr_)
  13675. +
  13676. +#ifdef DWC_LINUX
  13677. +/* Linux doesn't need any extra parameters for DMA buffer allocation, so we
  13678. + * just throw away the DMA context parameter.
  13679. + */
  13680. +#define dwc_dma_alloc(_ctx_,_size_,_dma_) DWC_DMA_ALLOC(_size_, _dma_)
  13681. +#define dwc_dma_alloc_atomic(_ctx_,_size_,_dma_) DWC_DMA_ALLOC_ATOMIC(_size_, _dma_)
  13682. +#define dwc_dma_free(_ctx_,_size_,_virt_,_dma_) DWC_DMA_FREE(_size_, _virt_, _dma_)
  13683. +#endif
  13684. +
  13685. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  13686. +/** BSD needs several extra parameters for DMA buffer allocation, so we pass
  13687. + * them in using the DMA context parameter.
  13688. + */
  13689. +#define dwc_dma_alloc DWC_DMA_ALLOC
  13690. +#define dwc_dma_free DWC_DMA_FREE
  13691. +#endif
  13692. +
  13693. +
  13694. +/** @name Memory and String Processing */
  13695. +
  13696. +/** memset() clone */
  13697. +extern void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size);
  13698. +#define dwc_memset DWC_MEMSET
  13699. +
  13700. +/** memcpy() clone */
  13701. +extern void *DWC_MEMCPY(void *dest, void const *src, uint32_t size);
  13702. +#define dwc_memcpy DWC_MEMCPY
  13703. +
  13704. +/** memmove() clone */
  13705. +extern void *DWC_MEMMOVE(void *dest, void *src, uint32_t size);
  13706. +#define dwc_memmove DWC_MEMMOVE
  13707. +
  13708. +/** memcmp() clone */
  13709. +extern int DWC_MEMCMP(void *m1, void *m2, uint32_t size);
  13710. +#define dwc_memcmp DWC_MEMCMP
  13711. +
  13712. +/** strcmp() clone */
  13713. +extern int DWC_STRCMP(void *s1, void *s2);
  13714. +#define dwc_strcmp DWC_STRCMP
  13715. +
  13716. +/** strncmp() clone */
  13717. +extern int DWC_STRNCMP(void *s1, void *s2, uint32_t size);
  13718. +#define dwc_strncmp DWC_STRNCMP
  13719. +
  13720. +/** strlen() clone, for NULL terminated ASCII strings */
  13721. +extern int DWC_STRLEN(char const *str);
  13722. +#define dwc_strlen DWC_STRLEN
  13723. +
  13724. +/** strcpy() clone, for NULL terminated ASCII strings */
  13725. +extern char *DWC_STRCPY(char *to, const char *from);
  13726. +#define dwc_strcpy DWC_STRCPY
  13727. +
  13728. +/** strdup() clone. If you wish to use memory allocation debugging, this
  13729. + * implementation of strdup should use the DWC_* memory routines instead of
  13730. + * calling a predefined strdup. Otherwise the memory allocated by this routine
  13731. + * will not be seen by the debugging routines. */
  13732. +extern char *DWC_STRDUP(char const *str);
  13733. +#define dwc_strdup(_ctx_,_str_) DWC_STRDUP(_str_)
  13734. +
  13735. +/** NOT an atoi() clone. Read the description carefully. Returns an integer
  13736. + * converted from the string str in base 10 unless the string begins with a "0x"
  13737. + * in which case it is base 16. String must be a NULL terminated sequence of
  13738. + * ASCII characters and may optionally begin with whitespace, a + or -, and a
  13739. + * "0x" prefix if base 16. The remaining characters must be valid digits for
  13740. + * the number and end with a NULL character. If any invalid characters are
  13741. + * encountered or it returns with a negative error code and the results of the
  13742. + * conversion are undefined. On sucess it returns 0. Overflow conditions are
  13743. + * undefined. An example implementation using atoi() can be referenced from the
  13744. + * Linux implementation. */
  13745. +extern int DWC_ATOI(const char *str, int32_t *value);
  13746. +#define dwc_atoi DWC_ATOI
  13747. +
  13748. +/** Same as above but for unsigned. */
  13749. +extern int DWC_ATOUI(const char *str, uint32_t *value);
  13750. +#define dwc_atoui DWC_ATOUI
  13751. +
  13752. +#ifdef DWC_UTFLIB
  13753. +/** This routine returns a UTF16LE unicode encoded string from a UTF8 string. */
  13754. +extern int DWC_UTF8_TO_UTF16LE(uint8_t const *utf8string, uint16_t *utf16string, unsigned len);
  13755. +#define dwc_utf8_to_utf16le DWC_UTF8_TO_UTF16LE
  13756. +#endif
  13757. +
  13758. +
  13759. +/** @name Wait queues
  13760. + *
  13761. + * Wait queues provide a means of synchronizing between threads or processes. A
  13762. + * process can block on a waitq if some condition is not true, waiting for it to
  13763. + * become true. When the waitq is triggered all waiting process will get
  13764. + * unblocked and the condition will be check again. Waitqs should be triggered
  13765. + * every time a condition can potentially change.*/
  13766. +struct dwc_waitq;
  13767. +
  13768. +/** Type for a waitq */
  13769. +typedef struct dwc_waitq dwc_waitq_t;
  13770. +
  13771. +/** The type of waitq condition callback function. This is called every time
  13772. + * condition is evaluated. */
  13773. +typedef int (*dwc_waitq_condition_t)(void *data);
  13774. +
  13775. +/** Allocate a waitq */
  13776. +extern dwc_waitq_t *DWC_WAITQ_ALLOC(void);
  13777. +#define dwc_waitq_alloc(_ctx_) DWC_WAITQ_ALLOC()
  13778. +
  13779. +/** Free a waitq */
  13780. +extern void DWC_WAITQ_FREE(dwc_waitq_t *wq);
  13781. +#define dwc_waitq_free DWC_WAITQ_FREE
  13782. +
  13783. +/** Check the condition and if it is false, block on the waitq. When unblocked, check the
  13784. + * condition again. The function returns when the condition becomes true. The return value
  13785. + * is 0 on condition true, DWC_WAITQ_ABORTED on abort or killed, or DWC_WAITQ_UNKNOWN on error. */
  13786. +extern int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data);
  13787. +#define dwc_waitq_wait DWC_WAITQ_WAIT
  13788. +
  13789. +/** Check the condition and if it is false, block on the waitq. When unblocked,
  13790. + * check the condition again. The function returns when the condition become
  13791. + * true or the timeout has passed. The return value is 0 on condition true or
  13792. + * DWC_TIMED_OUT on timeout, or DWC_WAITQ_ABORTED, or DWC_WAITQ_UNKNOWN on
  13793. + * error. */
  13794. +extern int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
  13795. + void *data, int32_t msecs);
  13796. +#define dwc_waitq_wait_timeout DWC_WAITQ_WAIT_TIMEOUT
  13797. +
  13798. +/** Trigger a waitq, unblocking all processes. This should be called whenever a condition
  13799. + * has potentially changed. */
  13800. +extern void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq);
  13801. +#define dwc_waitq_trigger DWC_WAITQ_TRIGGER
  13802. +
  13803. +/** Unblock all processes waiting on the waitq with an ABORTED result. */
  13804. +extern void DWC_WAITQ_ABORT(dwc_waitq_t *wq);
  13805. +#define dwc_waitq_abort DWC_WAITQ_ABORT
  13806. +
  13807. +
  13808. +/** @name Threads
  13809. + *
  13810. + * A thread must be explicitly stopped. It must check DWC_THREAD_SHOULD_STOP
  13811. + * whenever it is woken up, and then return. The DWC_THREAD_STOP function
  13812. + * returns the value from the thread.
  13813. + */
  13814. +
  13815. +struct dwc_thread;
  13816. +
  13817. +/** Type for a thread */
  13818. +typedef struct dwc_thread dwc_thread_t;
  13819. +
  13820. +/** The thread function */
  13821. +typedef int (*dwc_thread_function_t)(void *data);
  13822. +
  13823. +/** Create a thread and start it running the thread_function. Returns a handle
  13824. + * to the thread */
  13825. +extern dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data);
  13826. +#define dwc_thread_run(_ctx_,_func_,_name_,_data_) DWC_THREAD_RUN(_func_, _name_, _data_)
  13827. +
  13828. +/** Stops a thread. Return the value returned by the thread. Or will return
  13829. + * DWC_ABORT if the thread never started. */
  13830. +extern int DWC_THREAD_STOP(dwc_thread_t *thread);
  13831. +#define dwc_thread_stop DWC_THREAD_STOP
  13832. +
  13833. +/** Signifies to the thread that it must stop. */
  13834. +#ifdef DWC_LINUX
  13835. +/* Linux doesn't need any parameters for kthread_should_stop() */
  13836. +extern dwc_bool_t DWC_THREAD_SHOULD_STOP(void);
  13837. +#define dwc_thread_should_stop(_thrd_) DWC_THREAD_SHOULD_STOP()
  13838. +
  13839. +/* No thread_exit function in Linux */
  13840. +#define dwc_thread_exit(_thrd_)
  13841. +#endif
  13842. +
  13843. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  13844. +/** BSD needs the thread pointer for kthread_suspend_check() */
  13845. +extern dwc_bool_t DWC_THREAD_SHOULD_STOP(dwc_thread_t *thread);
  13846. +#define dwc_thread_should_stop DWC_THREAD_SHOULD_STOP
  13847. +
  13848. +/** The thread must call this to exit. */
  13849. +extern void DWC_THREAD_EXIT(dwc_thread_t *thread);
  13850. +#define dwc_thread_exit DWC_THREAD_EXIT
  13851. +#endif
  13852. +
  13853. +
  13854. +/** @name Work queues
  13855. + *
  13856. + * Workqs are used to queue a callback function to be called at some later time,
  13857. + * in another thread. */
  13858. +struct dwc_workq;
  13859. +
  13860. +/** Type for a workq */
  13861. +typedef struct dwc_workq dwc_workq_t;
  13862. +
  13863. +/** The type of the callback function to be called. */
  13864. +typedef void (*dwc_work_callback_t)(void *data);
  13865. +
  13866. +/** Allocate a workq */
  13867. +extern dwc_workq_t *DWC_WORKQ_ALLOC(char *name);
  13868. +#define dwc_workq_alloc(_ctx_,_name_) DWC_WORKQ_ALLOC(_name_)
  13869. +
  13870. +/** Free a workq. All work must be completed before being freed. */
  13871. +extern void DWC_WORKQ_FREE(dwc_workq_t *workq);
  13872. +#define dwc_workq_free DWC_WORKQ_FREE
  13873. +
  13874. +/** Schedule a callback on the workq, passing in data. The function will be
  13875. + * scheduled at some later time. */
  13876. +extern void DWC_WORKQ_SCHEDULE(dwc_workq_t *workq, dwc_work_callback_t cb,
  13877. + void *data, char *format, ...)
  13878. +#ifdef __GNUC__
  13879. + __attribute__ ((format(printf, 4, 5)));
  13880. +#else
  13881. + ;
  13882. +#endif
  13883. +#define dwc_workq_schedule DWC_WORKQ_SCHEDULE
  13884. +
  13885. +/** Schedule a callback on the workq, that will be called until at least
  13886. + * given number miliseconds have passed. */
  13887. +extern void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *workq, dwc_work_callback_t cb,
  13888. + void *data, uint32_t time, char *format, ...)
  13889. +#ifdef __GNUC__
  13890. + __attribute__ ((format(printf, 5, 6)));
  13891. +#else
  13892. + ;
  13893. +#endif
  13894. +#define dwc_workq_schedule_delayed DWC_WORKQ_SCHEDULE_DELAYED
  13895. +
  13896. +/** The number of processes in the workq */
  13897. +extern int DWC_WORKQ_PENDING(dwc_workq_t *workq);
  13898. +#define dwc_workq_pending DWC_WORKQ_PENDING
  13899. +
  13900. +/** Blocks until all the work in the workq is complete or timed out. Returns <
  13901. + * 0 on timeout. */
  13902. +extern int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout);
  13903. +#define dwc_workq_wait_work_done DWC_WORKQ_WAIT_WORK_DONE
  13904. +
  13905. +
  13906. +/** @name Tasklets
  13907. + *
  13908. + */
  13909. +struct dwc_tasklet;
  13910. +
  13911. +/** Type for a tasklet */
  13912. +typedef struct dwc_tasklet dwc_tasklet_t;
  13913. +
  13914. +/** The type of the callback function to be called */
  13915. +typedef void (*dwc_tasklet_callback_t)(void *data);
  13916. +
  13917. +/** Allocates a tasklet */
  13918. +extern dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data);
  13919. +#define dwc_task_alloc(_ctx_,_name_,_cb_,_data_) DWC_TASK_ALLOC(_name_, _cb_, _data_)
  13920. +
  13921. +/** Frees a tasklet */
  13922. +extern void DWC_TASK_FREE(dwc_tasklet_t *task);
  13923. +#define dwc_task_free DWC_TASK_FREE
  13924. +
  13925. +/** Schedules a tasklet to run */
  13926. +extern void DWC_TASK_SCHEDULE(dwc_tasklet_t *task);
  13927. +#define dwc_task_schedule DWC_TASK_SCHEDULE
  13928. +
  13929. +extern void DWC_TASK_HI_SCHEDULE(dwc_tasklet_t *task);
  13930. +#define dwc_task_hi_schedule DWC_TASK_HI_SCHEDULE
  13931. +
  13932. +/** @name Timer
  13933. + *
  13934. + * Callbacks must be small and atomic.
  13935. + */
  13936. +struct dwc_timer;
  13937. +
  13938. +/** Type for a timer */
  13939. +typedef struct dwc_timer dwc_timer_t;
  13940. +
  13941. +/** The type of the callback function to be called */
  13942. +typedef void (*dwc_timer_callback_t)(void *data);
  13943. +
  13944. +/** Allocates a timer */
  13945. +extern dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data);
  13946. +#define dwc_timer_alloc(_ctx_,_name_,_cb_,_data_) DWC_TIMER_ALLOC(_name_,_cb_,_data_)
  13947. +
  13948. +/** Frees a timer */
  13949. +extern void DWC_TIMER_FREE(dwc_timer_t *timer);
  13950. +#define dwc_timer_free DWC_TIMER_FREE
  13951. +
  13952. +/** Schedules the timer to run at time ms from now. And will repeat at every
  13953. + * repeat_interval msec therafter
  13954. + *
  13955. + * Modifies a timer that is still awaiting execution to a new expiration time.
  13956. + * The mod_time is added to the old time. */
  13957. +extern void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time);
  13958. +#define dwc_timer_schedule DWC_TIMER_SCHEDULE
  13959. +
  13960. +/** Disables the timer from execution. */
  13961. +extern void DWC_TIMER_CANCEL(dwc_timer_t *timer);
  13962. +#define dwc_timer_cancel DWC_TIMER_CANCEL
  13963. +
  13964. +
  13965. +/** @name Spinlocks
  13966. + *
  13967. + * These locks are used when the work between the lock/unlock is atomic and
  13968. + * short. Interrupts are also disabled during the lock/unlock and thus they are
  13969. + * suitable to lock between interrupt/non-interrupt context. They also lock
  13970. + * between processes if you have multiple CPUs or Preemption. If you don't have
  13971. + * multiple CPUS or Preemption, then the you can simply implement the
  13972. + * DWC_SPINLOCK and DWC_SPINUNLOCK to disable and enable interrupts. Because
  13973. + * the work between the lock/unlock is atomic, the process context will never
  13974. + * change, and so you never have to lock between processes. */
  13975. +
  13976. +struct dwc_spinlock;
  13977. +
  13978. +/** Type for a spinlock */
  13979. +typedef struct dwc_spinlock dwc_spinlock_t;
  13980. +
  13981. +/** Type for the 'flags' argument to spinlock funtions */
  13982. +typedef unsigned long dwc_irqflags_t;
  13983. +
  13984. +/** Returns an initialized lock variable. This function should allocate and
  13985. + * initialize the OS-specific data structure used for locking. This data
  13986. + * structure is to be used for the DWC_LOCK and DWC_UNLOCK functions and should
  13987. + * be freed by the DWC_FREE_LOCK when it is no longer used.
  13988. + *
  13989. + * For Linux Spinlock Debugging make it macro because the debugging routines use
  13990. + * the symbol name to determine recursive locking. Using a wrapper function
  13991. + * makes it falsely think recursive locking occurs. */
  13992. +#if defined(DWC_LINUX) && defined(CONFIG_DEBUG_SPINLOCK)
  13993. +#define DWC_SPINLOCK_ALLOC_LINUX_DEBUG(lock) ({ \
  13994. + lock = DWC_ALLOC(sizeof(spinlock_t)); \
  13995. + if (lock) { \
  13996. + spin_lock_init((spinlock_t *)lock); \
  13997. + } \
  13998. +})
  13999. +#else
  14000. +extern dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void);
  14001. +#define dwc_spinlock_alloc(_ctx_) DWC_SPINLOCK_ALLOC()
  14002. +#endif
  14003. +
  14004. +/** Frees an initialized lock variable. */
  14005. +extern void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock);
  14006. +#define dwc_spinlock_free(_ctx_,_lock_) DWC_SPINLOCK_FREE(_lock_)
  14007. +
  14008. +/** Disables interrupts and blocks until it acquires the lock.
  14009. + *
  14010. + * @param lock Pointer to the spinlock.
  14011. + * @param flags Unsigned long for irq flags storage.
  14012. + */
  14013. +extern void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags);
  14014. +#define dwc_spinlock_irqsave DWC_SPINLOCK_IRQSAVE
  14015. +
  14016. +/** Re-enables the interrupt and releases the lock.
  14017. + *
  14018. + * @param lock Pointer to the spinlock.
  14019. + * @param flags Unsigned long for irq flags storage. Must be the same as was
  14020. + * passed into DWC_LOCK.
  14021. + */
  14022. +extern void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags);
  14023. +#define dwc_spinunlock_irqrestore DWC_SPINUNLOCK_IRQRESTORE
  14024. +
  14025. +/** Blocks until it acquires the lock.
  14026. + *
  14027. + * @param lock Pointer to the spinlock.
  14028. + */
  14029. +extern void DWC_SPINLOCK(dwc_spinlock_t *lock);
  14030. +#define dwc_spinlock DWC_SPINLOCK
  14031. +
  14032. +/** Releases the lock.
  14033. + *
  14034. + * @param lock Pointer to the spinlock.
  14035. + */
  14036. +extern void DWC_SPINUNLOCK(dwc_spinlock_t *lock);
  14037. +#define dwc_spinunlock DWC_SPINUNLOCK
  14038. +
  14039. +
  14040. +/** @name Mutexes
  14041. + *
  14042. + * Unlike spinlocks Mutexes lock only between processes and the work between the
  14043. + * lock/unlock CAN block, therefore it CANNOT be called from interrupt context.
  14044. + */
  14045. +
  14046. +struct dwc_mutex;
  14047. +
  14048. +/** Type for a mutex */
  14049. +typedef struct dwc_mutex dwc_mutex_t;
  14050. +
  14051. +/* For Linux Mutex Debugging make it inline because the debugging routines use
  14052. + * the symbol to determine recursive locking. This makes it falsely think
  14053. + * recursive locking occurs. */
  14054. +#if defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES)
  14055. +#define DWC_MUTEX_ALLOC_LINUX_DEBUG(__mutexp) ({ \
  14056. + __mutexp = (dwc_mutex_t *)DWC_ALLOC(sizeof(struct mutex)); \
  14057. + mutex_init((struct mutex *)__mutexp); \
  14058. +})
  14059. +#endif
  14060. +
  14061. +/** Allocate a mutex */
  14062. +extern dwc_mutex_t *DWC_MUTEX_ALLOC(void);
  14063. +#define dwc_mutex_alloc(_ctx_) DWC_MUTEX_ALLOC()
  14064. +
  14065. +/* For memory leak debugging when using Linux Mutex Debugging */
  14066. +#if defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES)
  14067. +#define DWC_MUTEX_FREE(__mutexp) do { \
  14068. + mutex_destroy((struct mutex *)__mutexp); \
  14069. + DWC_FREE(__mutexp); \
  14070. +} while(0)
  14071. +#else
  14072. +/** Free a mutex */
  14073. +extern void DWC_MUTEX_FREE(dwc_mutex_t *mutex);
  14074. +#define dwc_mutex_free(_ctx_,_mutex_) DWC_MUTEX_FREE(_mutex_)
  14075. +#endif
  14076. +
  14077. +/** Lock a mutex */
  14078. +extern void DWC_MUTEX_LOCK(dwc_mutex_t *mutex);
  14079. +#define dwc_mutex_lock DWC_MUTEX_LOCK
  14080. +
  14081. +/** Non-blocking lock returns 1 on successful lock. */
  14082. +extern int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex);
  14083. +#define dwc_mutex_trylock DWC_MUTEX_TRYLOCK
  14084. +
  14085. +/** Unlock a mutex */
  14086. +extern void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex);
  14087. +#define dwc_mutex_unlock DWC_MUTEX_UNLOCK
  14088. +
  14089. +
  14090. +/** @name Time */
  14091. +
  14092. +/** Microsecond delay.
  14093. + *
  14094. + * @param usecs Microseconds to delay.
  14095. + */
  14096. +extern void DWC_UDELAY(uint32_t usecs);
  14097. +#define dwc_udelay DWC_UDELAY
  14098. +
  14099. +/** Millisecond delay.
  14100. + *
  14101. + * @param msecs Milliseconds to delay.
  14102. + */
  14103. +extern void DWC_MDELAY(uint32_t msecs);
  14104. +#define dwc_mdelay DWC_MDELAY
  14105. +
  14106. +/** Non-busy waiting.
  14107. + * Sleeps for specified number of milliseconds.
  14108. + *
  14109. + * @param msecs Milliseconds to sleep.
  14110. + */
  14111. +extern void DWC_MSLEEP(uint32_t msecs);
  14112. +#define dwc_msleep DWC_MSLEEP
  14113. +
  14114. +/**
  14115. + * Returns number of milliseconds since boot.
  14116. + */
  14117. +extern uint32_t DWC_TIME(void);
  14118. +#define dwc_time DWC_TIME
  14119. +
  14120. +
  14121. +
  14122. +
  14123. +/* @mainpage DWC Portability and Common Library
  14124. + *
  14125. + * This is the documentation for the DWC Portability and Common Library.
  14126. + *
  14127. + * @section intro Introduction
  14128. + *
  14129. + * The DWC Portability library consists of wrapper calls and data structures to
  14130. + * all low-level functions which are typically provided by the OS. The WUDEV
  14131. + * driver uses only these functions. In order to port the WUDEV driver, only
  14132. + * the functions in this library need to be re-implemented, with the same
  14133. + * behavior as documented here.
  14134. + *
  14135. + * The Common library consists of higher level functions, which rely only on
  14136. + * calling the functions from the DWC Portability library. These common
  14137. + * routines are shared across modules. Some of the common libraries need to be
  14138. + * used directly by the driver programmer when porting WUDEV. Such as the
  14139. + * parameter and notification libraries.
  14140. + *
  14141. + * @section low Portability Library OS Wrapper Functions
  14142. + *
  14143. + * Any function starting with DWC and in all CAPS is a low-level OS-wrapper that
  14144. + * needs to be implemented when porting, for example DWC_MUTEX_ALLOC(). All of
  14145. + * these functions are included in the dwc_os.h file.
  14146. + *
  14147. + * There are many functions here covering a wide array of OS services. Please
  14148. + * see dwc_os.h for details, and implementation notes for each function.
  14149. + *
  14150. + * @section common Common Library Functions
  14151. + *
  14152. + * Any function starting with dwc and in all lowercase is a common library
  14153. + * routine. These functions have a portable implementation and do not need to
  14154. + * be reimplemented when porting. The common routines can be used by any
  14155. + * driver, and some must be used by the end user to control the drivers. For
  14156. + * example, you must use the Parameter common library in order to set the
  14157. + * parameters in the WUDEV module.
  14158. + *
  14159. + * The common libraries consist of the following:
  14160. + *
  14161. + * - Connection Contexts - Used internally and can be used by end-user. See dwc_cc.h
  14162. + * - Parameters - Used internally and can be used by end-user. See dwc_params.h
  14163. + * - Notifications - Used internally and can be used by end-user. See dwc_notifier.h
  14164. + * - Lists - Used internally and can be used by end-user. See dwc_list.h
  14165. + * - Memory Debugging - Used internally and can be used by end-user. See dwc_os.h
  14166. + * - Modpow - Used internally only. See dwc_modpow.h
  14167. + * - DH - Used internally only. See dwc_dh.h
  14168. + * - Crypto - Used internally only. See dwc_crypto.h
  14169. + *
  14170. + *
  14171. + * @section prereq Prerequistes For dwc_os.h
  14172. + * @subsection types Data Types
  14173. + *
  14174. + * The dwc_os.h file assumes that several low-level data types are pre defined for the
  14175. + * compilation environment. These data types are:
  14176. + *
  14177. + * - uint8_t - unsigned 8-bit data type
  14178. + * - int8_t - signed 8-bit data type
  14179. + * - uint16_t - unsigned 16-bit data type
  14180. + * - int16_t - signed 16-bit data type
  14181. + * - uint32_t - unsigned 32-bit data type
  14182. + * - int32_t - signed 32-bit data type
  14183. + * - uint64_t - unsigned 64-bit data type
  14184. + * - int64_t - signed 64-bit data type
  14185. + *
  14186. + * Ensure that these are defined before using dwc_os.h. The easiest way to do
  14187. + * that is to modify the top of the file to include the appropriate header.
  14188. + * This is already done for the Linux environment. If the DWC_LINUX macro is
  14189. + * defined, the correct header will be added. A standard header <stdint.h> is
  14190. + * also used for environments where standard C headers are available.
  14191. + *
  14192. + * @subsection stdarg Variable Arguments
  14193. + *
  14194. + * Variable arguments are provided by a standard C header <stdarg.h>. it is
  14195. + * available in Both the Linux and ANSI C enviornment. An equivalent must be
  14196. + * provided in your enviornment in order to use dwc_os.h with the debug and
  14197. + * tracing message functionality.
  14198. + *
  14199. + * @subsection thread Threading
  14200. + *
  14201. + * WUDEV Core must be run on an operating system that provides for multiple
  14202. + * threads/processes. Threading can be implemented in many ways, even in
  14203. + * embedded systems without an operating system. At the bare minimum, the
  14204. + * system should be able to start any number of processes at any time to handle
  14205. + * special work. It need not be a pre-emptive system. Process context can
  14206. + * change upon a call to a blocking function. The hardware interrupt context
  14207. + * that calls the module's ISR() function must be differentiable from process
  14208. + * context, even if your processes are impemented via a hardware interrupt.
  14209. + * Further locking mechanism between process must exist (or be implemented), and
  14210. + * process context must have a way to disable interrupts for a period of time to
  14211. + * lock them out. If all of this exists, the functions in dwc_os.h related to
  14212. + * threading should be able to be implemented with the defined behavior.
  14213. + *
  14214. + */
  14215. +
  14216. +#ifdef __cplusplus
  14217. +}
  14218. +#endif
  14219. +
  14220. +#endif /* _DWC_OS_H_ */
  14221. --- /dev/null
  14222. +++ b/drivers/usb/host/dwc_common_port/usb.h
  14223. @@ -0,0 +1,275 @@
  14224. +/*
  14225. + * Copyright (c) 1998 The NetBSD Foundation, Inc.
  14226. + * All rights reserved.
  14227. + *
  14228. + * This code is derived from software contributed to The NetBSD Foundation
  14229. + * by Lennart Augustsson ([email protected]) at
  14230. + * Carlstedt Research & Technology.
  14231. + *
  14232. + * Redistribution and use in source and binary forms, with or without
  14233. + * modification, are permitted provided that the following conditions
  14234. + * are met:
  14235. + * 1. Redistributions of source code must retain the above copyright
  14236. + * notice, this list of conditions and the following disclaimer.
  14237. + * 2. Redistributions in binary form must reproduce the above copyright
  14238. + * notice, this list of conditions and the following disclaimer in the
  14239. + * documentation and/or other materials provided with the distribution.
  14240. + *
  14241. + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  14242. + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  14243. + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  14244. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  14245. + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  14246. + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  14247. + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  14248. + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  14249. + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  14250. + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  14251. + * POSSIBILITY OF SUCH DAMAGE.
  14252. + */
  14253. +
  14254. +/* Modified by Synopsys, Inc, 12/12/2007 */
  14255. +
  14256. +
  14257. +#ifndef _USB_H_
  14258. +#define _USB_H_
  14259. +
  14260. +#ifdef __cplusplus
  14261. +extern "C" {
  14262. +#endif
  14263. +
  14264. +/*
  14265. + * The USB records contain some unaligned little-endian word
  14266. + * components. The U[SG]ETW macros take care of both the alignment
  14267. + * and endian problem and should always be used to access non-byte
  14268. + * values.
  14269. + */
  14270. +typedef u_int8_t uByte;
  14271. +typedef u_int8_t uWord[2];
  14272. +typedef u_int8_t uDWord[4];
  14273. +
  14274. +#define UGETW(w) ((w)[0] | ((w)[1] << 8))
  14275. +#define USETW(w,v) ((w)[0] = (u_int8_t)(v), (w)[1] = (u_int8_t)((v) >> 8))
  14276. +#define UGETDW(w) ((w)[0] | ((w)[1] << 8) | ((w)[2] << 16) | ((w)[3] << 24))
  14277. +#define USETDW(w,v) ((w)[0] = (u_int8_t)(v), \
  14278. + (w)[1] = (u_int8_t)((v) >> 8), \
  14279. + (w)[2] = (u_int8_t)((v) >> 16), \
  14280. + (w)[3] = (u_int8_t)((v) >> 24))
  14281. +
  14282. +#define UPACKED __attribute__((__packed__))
  14283. +
  14284. +typedef struct {
  14285. + uByte bmRequestType;
  14286. + uByte bRequest;
  14287. + uWord wValue;
  14288. + uWord wIndex;
  14289. + uWord wLength;
  14290. +} UPACKED usb_device_request_t;
  14291. +
  14292. +#define UT_GET_DIR(a) ((a) & 0x80)
  14293. +#define UT_WRITE 0x00
  14294. +#define UT_READ 0x80
  14295. +
  14296. +#define UT_GET_TYPE(a) ((a) & 0x60)
  14297. +#define UT_STANDARD 0x00
  14298. +#define UT_CLASS 0x20
  14299. +#define UT_VENDOR 0x40
  14300. +
  14301. +#define UT_GET_RECIPIENT(a) ((a) & 0x1f)
  14302. +#define UT_DEVICE 0x00
  14303. +#define UT_INTERFACE 0x01
  14304. +#define UT_ENDPOINT 0x02
  14305. +#define UT_OTHER 0x03
  14306. +
  14307. +/* Requests */
  14308. +#define UR_GET_STATUS 0x00
  14309. +#define USTAT_STANDARD_STATUS 0x00
  14310. +#define WUSTAT_WUSB_FEATURE 0x01
  14311. +#define WUSTAT_CHANNEL_INFO 0x02
  14312. +#define WUSTAT_RECEIVED_DATA 0x03
  14313. +#define WUSTAT_MAS_AVAILABILITY 0x04
  14314. +#define WUSTAT_CURRENT_TRANSMIT_POWER 0x05
  14315. +#define UR_CLEAR_FEATURE 0x01
  14316. +#define UR_SET_FEATURE 0x03
  14317. +#define UR_SET_AND_TEST_FEATURE 0x0c
  14318. +#define UR_SET_ADDRESS 0x05
  14319. +#define UR_GET_DESCRIPTOR 0x06
  14320. +#define UDESC_DEVICE 0x01
  14321. +#define UDESC_CONFIG 0x02
  14322. +#define UDESC_STRING 0x03
  14323. +#define UDESC_INTERFACE 0x04
  14324. +#define UDESC_ENDPOINT 0x05
  14325. +#define UDESC_SS_USB_COMPANION 0x30
  14326. +#define UDESC_DEVICE_QUALIFIER 0x06
  14327. +#define UDESC_OTHER_SPEED_CONFIGURATION 0x07
  14328. +#define UDESC_INTERFACE_POWER 0x08
  14329. +#define UDESC_OTG 0x09
  14330. +#define WUDESC_SECURITY 0x0c
  14331. +#define WUDESC_KEY 0x0d
  14332. +#define WUD_GET_KEY_INDEX(_wValue_) ((_wValue_) & 0xf)
  14333. +#define WUD_GET_KEY_TYPE(_wValue_) (((_wValue_) & 0x30) >> 4)
  14334. +#define WUD_KEY_TYPE_ASSOC 0x01
  14335. +#define WUD_KEY_TYPE_GTK 0x02
  14336. +#define WUD_GET_KEY_ORIGIN(_wValue_) (((_wValue_) & 0x40) >> 6)
  14337. +#define WUD_KEY_ORIGIN_HOST 0x00
  14338. +#define WUD_KEY_ORIGIN_DEVICE 0x01
  14339. +#define WUDESC_ENCRYPTION_TYPE 0x0e
  14340. +#define WUDESC_BOS 0x0f
  14341. +#define WUDESC_DEVICE_CAPABILITY 0x10
  14342. +#define WUDESC_WIRELESS_ENDPOINT_COMPANION 0x11
  14343. +#define UDESC_BOS 0x0f
  14344. +#define UDESC_DEVICE_CAPABILITY 0x10
  14345. +#define UDESC_CS_DEVICE 0x21 /* class specific */
  14346. +#define UDESC_CS_CONFIG 0x22
  14347. +#define UDESC_CS_STRING 0x23
  14348. +#define UDESC_CS_INTERFACE 0x24
  14349. +#define UDESC_CS_ENDPOINT 0x25
  14350. +#define UDESC_HUB 0x29
  14351. +#define UR_SET_DESCRIPTOR 0x07
  14352. +#define UR_GET_CONFIG 0x08
  14353. +#define UR_SET_CONFIG 0x09
  14354. +#define UR_GET_INTERFACE 0x0a
  14355. +#define UR_SET_INTERFACE 0x0b
  14356. +#define UR_SYNCH_FRAME 0x0c
  14357. +#define WUR_SET_ENCRYPTION 0x0d
  14358. +#define WUR_GET_ENCRYPTION 0x0e
  14359. +#define WUR_SET_HANDSHAKE 0x0f
  14360. +#define WUR_GET_HANDSHAKE 0x10
  14361. +#define WUR_SET_CONNECTION 0x11
  14362. +#define WUR_SET_SECURITY_DATA 0x12
  14363. +#define WUR_GET_SECURITY_DATA 0x13
  14364. +#define WUR_SET_WUSB_DATA 0x14
  14365. +#define WUDATA_DRPIE_INFO 0x01
  14366. +#define WUDATA_TRANSMIT_DATA 0x02
  14367. +#define WUDATA_TRANSMIT_PARAMS 0x03
  14368. +#define WUDATA_RECEIVE_PARAMS 0x04
  14369. +#define WUDATA_TRANSMIT_POWER 0x05
  14370. +#define WUR_LOOPBACK_DATA_WRITE 0x15
  14371. +#define WUR_LOOPBACK_DATA_READ 0x16
  14372. +#define WUR_SET_INTERFACE_DS 0x17
  14373. +
  14374. +/* Feature numbers */
  14375. +#define UF_ENDPOINT_HALT 0
  14376. +#define UF_DEVICE_REMOTE_WAKEUP 1
  14377. +#define UF_TEST_MODE 2
  14378. +#define UF_DEVICE_B_HNP_ENABLE 3
  14379. +#define UF_DEVICE_A_HNP_SUPPORT 4
  14380. +#define UF_DEVICE_A_ALT_HNP_SUPPORT 5
  14381. +#define WUF_WUSB 3
  14382. +#define WUF_TX_DRPIE 0x0
  14383. +#define WUF_DEV_XMIT_PACKET 0x1
  14384. +#define WUF_COUNT_PACKETS 0x2
  14385. +#define WUF_CAPTURE_PACKETS 0x3
  14386. +#define UF_FUNCTION_SUSPEND 0
  14387. +#define UF_U1_ENABLE 48
  14388. +#define UF_U2_ENABLE 49
  14389. +#define UF_LTM_ENABLE 50
  14390. +
  14391. +/* Class requests from the USB 2.0 hub spec, table 11-15 */
  14392. +#define UCR_CLEAR_HUB_FEATURE (0x2000 | UR_CLEAR_FEATURE)
  14393. +#define UCR_CLEAR_PORT_FEATURE (0x2300 | UR_CLEAR_FEATURE)
  14394. +#define UCR_GET_HUB_DESCRIPTOR (0xa000 | UR_GET_DESCRIPTOR)
  14395. +#define UCR_GET_HUB_STATUS (0xa000 | UR_GET_STATUS)
  14396. +#define UCR_GET_PORT_STATUS (0xa300 | UR_GET_STATUS)
  14397. +#define UCR_SET_HUB_FEATURE (0x2000 | UR_SET_FEATURE)
  14398. +#define UCR_SET_PORT_FEATURE (0x2300 | UR_SET_FEATURE)
  14399. +#define UCR_SET_AND_TEST_PORT_FEATURE (0xa300 | UR_SET_AND_TEST_FEATURE)
  14400. +
  14401. +#ifdef _MSC_VER
  14402. +#include <pshpack1.h>
  14403. +#endif
  14404. +
  14405. +typedef struct {
  14406. + uByte bLength;
  14407. + uByte bDescriptorType;
  14408. + uByte bEndpointAddress;
  14409. +#define UE_GET_DIR(a) ((a) & 0x80)
  14410. +#define UE_SET_DIR(a,d) ((a) | (((d)&1) << 7))
  14411. +#define UE_DIR_IN 0x80
  14412. +#define UE_DIR_OUT 0x00
  14413. +#define UE_ADDR 0x0f
  14414. +#define UE_GET_ADDR(a) ((a) & UE_ADDR)
  14415. + uByte bmAttributes;
  14416. +#define UE_XFERTYPE 0x03
  14417. +#define UE_CONTROL 0x00
  14418. +#define UE_ISOCHRONOUS 0x01
  14419. +#define UE_BULK 0x02
  14420. +#define UE_INTERRUPT 0x03
  14421. +#define UE_GET_XFERTYPE(a) ((a) & UE_XFERTYPE)
  14422. +#define UE_ISO_TYPE 0x0c
  14423. +#define UE_ISO_ASYNC 0x04
  14424. +#define UE_ISO_ADAPT 0x08
  14425. +#define UE_ISO_SYNC 0x0c
  14426. +#define UE_GET_ISO_TYPE(a) ((a) & UE_ISO_TYPE)
  14427. + uWord wMaxPacketSize;
  14428. + uByte bInterval;
  14429. +} UPACKED usb_endpoint_descriptor_t;
  14430. +#define USB_ENDPOINT_DESCRIPTOR_SIZE 7
  14431. +
  14432. +/* Hub specific request */
  14433. +#define UR_GET_BUS_STATE 0x02
  14434. +#define UR_CLEAR_TT_BUFFER 0x08
  14435. +#define UR_RESET_TT 0x09
  14436. +#define UR_GET_TT_STATE 0x0a
  14437. +#define UR_STOP_TT 0x0b
  14438. +
  14439. +/* Hub features */
  14440. +#define UHF_C_HUB_LOCAL_POWER 0
  14441. +#define UHF_C_HUB_OVER_CURRENT 1
  14442. +#define UHF_PORT_CONNECTION 0
  14443. +#define UHF_PORT_ENABLE 1
  14444. +#define UHF_PORT_SUSPEND 2
  14445. +#define UHF_PORT_OVER_CURRENT 3
  14446. +#define UHF_PORT_RESET 4
  14447. +#define UHF_PORT_L1 5
  14448. +#define UHF_PORT_POWER 8
  14449. +#define UHF_PORT_LOW_SPEED 9
  14450. +#define UHF_PORT_HIGH_SPEED 10
  14451. +#define UHF_C_PORT_CONNECTION 16
  14452. +#define UHF_C_PORT_ENABLE 17
  14453. +#define UHF_C_PORT_SUSPEND 18
  14454. +#define UHF_C_PORT_OVER_CURRENT 19
  14455. +#define UHF_C_PORT_RESET 20
  14456. +#define UHF_C_PORT_L1 23
  14457. +#define UHF_PORT_TEST 21
  14458. +#define UHF_PORT_INDICATOR 22
  14459. +
  14460. +typedef struct {
  14461. + uByte bDescLength;
  14462. + uByte bDescriptorType;
  14463. + uByte bNbrPorts;
  14464. + uWord wHubCharacteristics;
  14465. +#define UHD_PWR 0x0003
  14466. +#define UHD_PWR_GANGED 0x0000
  14467. +#define UHD_PWR_INDIVIDUAL 0x0001
  14468. +#define UHD_PWR_NO_SWITCH 0x0002
  14469. +#define UHD_COMPOUND 0x0004
  14470. +#define UHD_OC 0x0018
  14471. +#define UHD_OC_GLOBAL 0x0000
  14472. +#define UHD_OC_INDIVIDUAL 0x0008
  14473. +#define UHD_OC_NONE 0x0010
  14474. +#define UHD_TT_THINK 0x0060
  14475. +#define UHD_TT_THINK_8 0x0000
  14476. +#define UHD_TT_THINK_16 0x0020
  14477. +#define UHD_TT_THINK_24 0x0040
  14478. +#define UHD_TT_THINK_32 0x0060
  14479. +#define UHD_PORT_IND 0x0080
  14480. + uByte bPwrOn2PwrGood; /* delay in 2 ms units */
  14481. +#define UHD_PWRON_FACTOR 2
  14482. + uByte bHubContrCurrent;
  14483. + uByte DeviceRemovable[32]; /* max 255 ports */
  14484. +#define UHD_NOT_REMOV(desc, i) \
  14485. + (((desc)->DeviceRemovable[(i)/8] >> ((i) % 8)) & 1)
  14486. + /* deprecated */ uByte PortPowerCtrlMask[1];
  14487. +} UPACKED usb_hub_descriptor_t;
  14488. +#define USB_HUB_DESCRIPTOR_SIZE 9 /* includes deprecated PortPowerCtrlMask */
  14489. +
  14490. +#ifdef _MSC_VER
  14491. +#include <poppack.h>
  14492. +#endif
  14493. +
  14494. +#ifdef __cplusplus
  14495. +}
  14496. +#endif
  14497. +
  14498. +#endif /* _USB_H_ */
  14499. --- /dev/null
  14500. +++ b/drivers/usb/host/dwc_otg/Makefile
  14501. @@ -0,0 +1,85 @@
  14502. +#
  14503. +# Makefile for DWC_otg Highspeed USB controller driver
  14504. +#
  14505. +
  14506. +ifneq ($(KERNELRELEASE),)
  14507. +
  14508. +# Use the BUS_INTERFACE variable to compile the software for either
  14509. +# PCI(PCI_INTERFACE) or LM(LM_INTERFACE) bus.
  14510. +ifeq ($(BUS_INTERFACE),)
  14511. +# BUS_INTERFACE = -DPCI_INTERFACE
  14512. +# BUS_INTERFACE = -DLM_INTERFACE
  14513. + BUS_INTERFACE = -DPLATFORM_INTERFACE
  14514. +endif
  14515. +
  14516. +#ccflags-y += -DDEBUG
  14517. +#ccflags-y += -DDWC_OTG_DEBUGLEV=1 # reduce common debug msgs
  14518. +
  14519. +# Use one of the following flags to compile the software in host-only or
  14520. +# device-only mode.
  14521. +#ccflags-y += -DDWC_HOST_ONLY
  14522. +#ccflags-y += -DDWC_DEVICE_ONLY
  14523. +
  14524. +ccflags-y += -Dlinux -DDWC_HS_ELECT_TST
  14525. +#ccflags-y += -DDWC_EN_ISOC
  14526. +ccflags-y += -I$(srctree)/drivers/usb/host/dwc_common_port
  14527. +#ccflags-y += -I$(PORTLIB)
  14528. +ccflags-y += -DDWC_LINUX
  14529. +ccflags-y += $(CFI)
  14530. +ccflags-y += $(BUS_INTERFACE)
  14531. +#ccflags-y += -DDWC_DEV_SRPCAP
  14532. +
  14533. +obj-$(CONFIG_USB_DWCOTG) += dwc_otg.o
  14534. +
  14535. +dwc_otg-objs := dwc_otg_driver.o dwc_otg_attr.o
  14536. +dwc_otg-objs += dwc_otg_cil.o dwc_otg_cil_intr.o
  14537. +dwc_otg-objs += dwc_otg_pcd_linux.o dwc_otg_pcd.o dwc_otg_pcd_intr.o
  14538. +dwc_otg-objs += dwc_otg_hcd.o dwc_otg_hcd_linux.o dwc_otg_hcd_intr.o dwc_otg_hcd_queue.o dwc_otg_hcd_ddma.o
  14539. +dwc_otg-objs += dwc_otg_adp.o
  14540. +dwc_otg-objs += dwc_otg_fiq_fsm.o
  14541. +ifneq ($(CONFIG_ARM64),y)
  14542. +dwc_otg-objs += dwc_otg_fiq_stub.o
  14543. +endif
  14544. +
  14545. +ifneq ($(CFI),)
  14546. +dwc_otg-objs += dwc_otg_cfi.o
  14547. +endif
  14548. +
  14549. +kernrelwd := $(subst ., ,$(KERNELRELEASE))
  14550. +kernrel3 := $(word 1,$(kernrelwd)).$(word 2,$(kernrelwd)).$(word 3,$(kernrelwd))
  14551. +
  14552. +ifneq ($(kernrel3),2.6.20)
  14553. +ccflags-y += $(CPPFLAGS)
  14554. +endif
  14555. +
  14556. +else
  14557. +
  14558. +PWD := $(shell pwd)
  14559. +PORTLIB := $(PWD)/../dwc_common_port
  14560. +
  14561. +# Command paths
  14562. +CTAGS := $(CTAGS)
  14563. +DOXYGEN := $(DOXYGEN)
  14564. +
  14565. +default: portlib
  14566. + $(MAKE) -C$(KDIR) M=$(PWD) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
  14567. +
  14568. +install: default
  14569. + $(MAKE) -C$(KDIR) M=$(PORTLIB) modules_install
  14570. + $(MAKE) -C$(KDIR) M=$(PWD) modules_install
  14571. +
  14572. +portlib:
  14573. + $(MAKE) -C$(KDIR) M=$(PORTLIB) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
  14574. + cp $(PORTLIB)/Module.symvers $(PWD)/
  14575. +
  14576. +docs: $(wildcard *.[hc]) doc/doxygen.cfg
  14577. + $(DOXYGEN) doc/doxygen.cfg
  14578. +
  14579. +tags: $(wildcard *.[hc])
  14580. + $(CTAGS) -e $(wildcard *.[hc]) $(wildcard linux/*.[hc]) $(wildcard $(KDIR)/include/linux/usb*.h)
  14581. +
  14582. +
  14583. +clean:
  14584. + rm -rf *.o *.ko .*cmd *.mod.c .tmp_versions Module.symvers
  14585. +
  14586. +endif
  14587. --- /dev/null
  14588. +++ b/drivers/usb/host/dwc_otg/doc/doxygen.cfg
  14589. @@ -0,0 +1,224 @@
  14590. +# Doxyfile 1.3.9.1
  14591. +
  14592. +#---------------------------------------------------------------------------
  14593. +# Project related configuration options
  14594. +#---------------------------------------------------------------------------
  14595. +PROJECT_NAME = "DesignWare USB 2.0 OTG Controller (DWC_otg) Device Driver"
  14596. +PROJECT_NUMBER = v3.00a
  14597. +OUTPUT_DIRECTORY = ./doc/
  14598. +CREATE_SUBDIRS = NO
  14599. +OUTPUT_LANGUAGE = English
  14600. +BRIEF_MEMBER_DESC = YES
  14601. +REPEAT_BRIEF = YES
  14602. +ABBREVIATE_BRIEF = "The $name class" \
  14603. + "The $name widget" \
  14604. + "The $name file" \
  14605. + is \
  14606. + provides \
  14607. + specifies \
  14608. + contains \
  14609. + represents \
  14610. + a \
  14611. + an \
  14612. + the
  14613. +ALWAYS_DETAILED_SEC = NO
  14614. +INLINE_INHERITED_MEMB = NO
  14615. +FULL_PATH_NAMES = NO
  14616. +STRIP_FROM_PATH =
  14617. +STRIP_FROM_INC_PATH =
  14618. +SHORT_NAMES = NO
  14619. +JAVADOC_AUTOBRIEF = YES
  14620. +MULTILINE_CPP_IS_BRIEF = NO
  14621. +INHERIT_DOCS = YES
  14622. +DISTRIBUTE_GROUP_DOC = NO
  14623. +TAB_SIZE = 8
  14624. +ALIASES =
  14625. +OPTIMIZE_OUTPUT_FOR_C = YES
  14626. +OPTIMIZE_OUTPUT_JAVA = NO
  14627. +SUBGROUPING = YES
  14628. +#---------------------------------------------------------------------------
  14629. +# Build related configuration options
  14630. +#---------------------------------------------------------------------------
  14631. +EXTRACT_ALL = NO
  14632. +EXTRACT_PRIVATE = YES
  14633. +EXTRACT_STATIC = YES
  14634. +EXTRACT_LOCAL_CLASSES = YES
  14635. +EXTRACT_LOCAL_METHODS = NO
  14636. +HIDE_UNDOC_MEMBERS = NO
  14637. +HIDE_UNDOC_CLASSES = NO
  14638. +HIDE_FRIEND_COMPOUNDS = NO
  14639. +HIDE_IN_BODY_DOCS = NO
  14640. +INTERNAL_DOCS = NO
  14641. +CASE_SENSE_NAMES = NO
  14642. +HIDE_SCOPE_NAMES = NO
  14643. +SHOW_INCLUDE_FILES = YES
  14644. +INLINE_INFO = YES
  14645. +SORT_MEMBER_DOCS = NO
  14646. +SORT_BRIEF_DOCS = NO
  14647. +SORT_BY_SCOPE_NAME = NO
  14648. +GENERATE_TODOLIST = YES
  14649. +GENERATE_TESTLIST = YES
  14650. +GENERATE_BUGLIST = YES
  14651. +GENERATE_DEPRECATEDLIST= YES
  14652. +ENABLED_SECTIONS =
  14653. +MAX_INITIALIZER_LINES = 30
  14654. +SHOW_USED_FILES = YES
  14655. +SHOW_DIRECTORIES = YES
  14656. +#---------------------------------------------------------------------------
  14657. +# configuration options related to warning and progress messages
  14658. +#---------------------------------------------------------------------------
  14659. +QUIET = YES
  14660. +WARNINGS = YES
  14661. +WARN_IF_UNDOCUMENTED = NO
  14662. +WARN_IF_DOC_ERROR = YES
  14663. +WARN_FORMAT = "$file:$line: $text"
  14664. +WARN_LOGFILE =
  14665. +#---------------------------------------------------------------------------
  14666. +# configuration options related to the input files
  14667. +#---------------------------------------------------------------------------
  14668. +INPUT = .
  14669. +FILE_PATTERNS = *.c \
  14670. + *.h \
  14671. + ./linux/*.c \
  14672. + ./linux/*.h
  14673. +RECURSIVE = NO
  14674. +EXCLUDE = ./test/ \
  14675. + ./dwc_otg/.AppleDouble/
  14676. +EXCLUDE_SYMLINKS = YES
  14677. +EXCLUDE_PATTERNS = *.mod.*
  14678. +EXAMPLE_PATH =
  14679. +EXAMPLE_PATTERNS = *
  14680. +EXAMPLE_RECURSIVE = NO
  14681. +IMAGE_PATH =
  14682. +INPUT_FILTER =
  14683. +FILTER_PATTERNS =
  14684. +FILTER_SOURCE_FILES = NO
  14685. +#---------------------------------------------------------------------------
  14686. +# configuration options related to source browsing
  14687. +#---------------------------------------------------------------------------
  14688. +SOURCE_BROWSER = YES
  14689. +INLINE_SOURCES = NO
  14690. +STRIP_CODE_COMMENTS = YES
  14691. +REFERENCED_BY_RELATION = NO
  14692. +REFERENCES_RELATION = NO
  14693. +VERBATIM_HEADERS = NO
  14694. +#---------------------------------------------------------------------------
  14695. +# configuration options related to the alphabetical class index
  14696. +#---------------------------------------------------------------------------
  14697. +ALPHABETICAL_INDEX = NO
  14698. +COLS_IN_ALPHA_INDEX = 5
  14699. +IGNORE_PREFIX =
  14700. +#---------------------------------------------------------------------------
  14701. +# configuration options related to the HTML output
  14702. +#---------------------------------------------------------------------------
  14703. +GENERATE_HTML = YES
  14704. +HTML_OUTPUT = html
  14705. +HTML_FILE_EXTENSION = .html
  14706. +HTML_HEADER =
  14707. +HTML_FOOTER =
  14708. +HTML_STYLESHEET =
  14709. +HTML_ALIGN_MEMBERS = YES
  14710. +GENERATE_HTMLHELP = NO
  14711. +CHM_FILE =
  14712. +HHC_LOCATION =
  14713. +GENERATE_CHI = NO
  14714. +BINARY_TOC = NO
  14715. +TOC_EXPAND = NO
  14716. +DISABLE_INDEX = NO
  14717. +ENUM_VALUES_PER_LINE = 4
  14718. +GENERATE_TREEVIEW = YES
  14719. +TREEVIEW_WIDTH = 250
  14720. +#---------------------------------------------------------------------------
  14721. +# configuration options related to the LaTeX output
  14722. +#---------------------------------------------------------------------------
  14723. +GENERATE_LATEX = NO
  14724. +LATEX_OUTPUT = latex
  14725. +LATEX_CMD_NAME = latex
  14726. +MAKEINDEX_CMD_NAME = makeindex
  14727. +COMPACT_LATEX = NO
  14728. +PAPER_TYPE = a4wide
  14729. +EXTRA_PACKAGES =
  14730. +LATEX_HEADER =
  14731. +PDF_HYPERLINKS = NO
  14732. +USE_PDFLATEX = NO
  14733. +LATEX_BATCHMODE = NO
  14734. +LATEX_HIDE_INDICES = NO
  14735. +#---------------------------------------------------------------------------
  14736. +# configuration options related to the RTF output
  14737. +#---------------------------------------------------------------------------
  14738. +GENERATE_RTF = NO
  14739. +RTF_OUTPUT = rtf
  14740. +COMPACT_RTF = NO
  14741. +RTF_HYPERLINKS = NO
  14742. +RTF_STYLESHEET_FILE =
  14743. +RTF_EXTENSIONS_FILE =
  14744. +#---------------------------------------------------------------------------
  14745. +# configuration options related to the man page output
  14746. +#---------------------------------------------------------------------------
  14747. +GENERATE_MAN = NO
  14748. +MAN_OUTPUT = man
  14749. +MAN_EXTENSION = .3
  14750. +MAN_LINKS = NO
  14751. +#---------------------------------------------------------------------------
  14752. +# configuration options related to the XML output
  14753. +#---------------------------------------------------------------------------
  14754. +GENERATE_XML = NO
  14755. +XML_OUTPUT = xml
  14756. +XML_SCHEMA =
  14757. +XML_DTD =
  14758. +XML_PROGRAMLISTING = YES
  14759. +#---------------------------------------------------------------------------
  14760. +# configuration options for the AutoGen Definitions output
  14761. +#---------------------------------------------------------------------------
  14762. +GENERATE_AUTOGEN_DEF = NO
  14763. +#---------------------------------------------------------------------------
  14764. +# configuration options related to the Perl module output
  14765. +#---------------------------------------------------------------------------
  14766. +GENERATE_PERLMOD = NO
  14767. +PERLMOD_LATEX = NO
  14768. +PERLMOD_PRETTY = YES
  14769. +PERLMOD_MAKEVAR_PREFIX =
  14770. +#---------------------------------------------------------------------------
  14771. +# Configuration options related to the preprocessor
  14772. +#---------------------------------------------------------------------------
  14773. +ENABLE_PREPROCESSING = YES
  14774. +MACRO_EXPANSION = YES
  14775. +EXPAND_ONLY_PREDEF = YES
  14776. +SEARCH_INCLUDES = YES
  14777. +INCLUDE_PATH =
  14778. +INCLUDE_FILE_PATTERNS =
  14779. +PREDEFINED = DEVICE_ATTR DWC_EN_ISOC
  14780. +EXPAND_AS_DEFINED = DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW DWC_OTG_DEVICE_ATTR_BITFIELD_STORE DWC_OTG_DEVICE_ATTR_BITFIELD_RW DWC_OTG_DEVICE_ATTR_BITFIELD_RO DWC_OTG_DEVICE_ATTR_REG_SHOW DWC_OTG_DEVICE_ATTR_REG_STORE DWC_OTG_DEVICE_ATTR_REG32_RW DWC_OTG_DEVICE_ATTR_REG32_RO DWC_EN_ISOC
  14781. +SKIP_FUNCTION_MACROS = NO
  14782. +#---------------------------------------------------------------------------
  14783. +# Configuration::additions related to external references
  14784. +#---------------------------------------------------------------------------
  14785. +TAGFILES =
  14786. +GENERATE_TAGFILE =
  14787. +ALLEXTERNALS = NO
  14788. +EXTERNAL_GROUPS = YES
  14789. +PERL_PATH = /usr/bin/perl
  14790. +#---------------------------------------------------------------------------
  14791. +# Configuration options related to the dot tool
  14792. +#---------------------------------------------------------------------------
  14793. +CLASS_DIAGRAMS = YES
  14794. +HIDE_UNDOC_RELATIONS = YES
  14795. +HAVE_DOT = NO
  14796. +CLASS_GRAPH = YES
  14797. +COLLABORATION_GRAPH = YES
  14798. +UML_LOOK = NO
  14799. +TEMPLATE_RELATIONS = NO
  14800. +INCLUDE_GRAPH = YES
  14801. +INCLUDED_BY_GRAPH = YES
  14802. +CALL_GRAPH = NO
  14803. +GRAPHICAL_HIERARCHY = YES
  14804. +DOT_IMAGE_FORMAT = png
  14805. +DOT_PATH =
  14806. +DOTFILE_DIRS =
  14807. +MAX_DOT_GRAPH_DEPTH = 1000
  14808. +GENERATE_LEGEND = YES
  14809. +DOT_CLEANUP = YES
  14810. +#---------------------------------------------------------------------------
  14811. +# Configuration::additions related to the search engine
  14812. +#---------------------------------------------------------------------------
  14813. +SEARCHENGINE = NO
  14814. --- /dev/null
  14815. +++ b/drivers/usb/host/dwc_otg/dummy_audio.c
  14816. @@ -0,0 +1,1574 @@
  14817. +/*
  14818. + * zero.c -- Gadget Zero, for USB development
  14819. + *
  14820. + * Copyright (C) 2003-2004 David Brownell
  14821. + * All rights reserved.
  14822. + *
  14823. + * Redistribution and use in source and binary forms, with or without
  14824. + * modification, are permitted provided that the following conditions
  14825. + * are met:
  14826. + * 1. Redistributions of source code must retain the above copyright
  14827. + * notice, this list of conditions, and the following disclaimer,
  14828. + * without modification.
  14829. + * 2. Redistributions in binary form must reproduce the above copyright
  14830. + * notice, this list of conditions and the following disclaimer in the
  14831. + * documentation and/or other materials provided with the distribution.
  14832. + * 3. The names of the above-listed copyright holders may not be used
  14833. + * to endorse or promote products derived from this software without
  14834. + * specific prior written permission.
  14835. + *
  14836. + * ALTERNATIVELY, this software may be distributed under the terms of the
  14837. + * GNU General Public License ("GPL") as published by the Free Software
  14838. + * Foundation, either version 2 of that License or (at your option) any
  14839. + * later version.
  14840. + *
  14841. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  14842. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  14843. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  14844. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  14845. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  14846. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  14847. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  14848. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  14849. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  14850. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  14851. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14852. + */
  14853. +
  14854. +
  14855. +/*
  14856. + * Gadget Zero only needs two bulk endpoints, and is an example of how you
  14857. + * can write a hardware-agnostic gadget driver running inside a USB device.
  14858. + *
  14859. + * Hardware details are visible (see CONFIG_USB_ZERO_* below) but don't
  14860. + * affect most of the driver.
  14861. + *
  14862. + * Use it with the Linux host/master side "usbtest" driver to get a basic
  14863. + * functional test of your device-side usb stack, or with "usb-skeleton".
  14864. + *
  14865. + * It supports two similar configurations. One sinks whatever the usb host
  14866. + * writes, and in return sources zeroes. The other loops whatever the host
  14867. + * writes back, so the host can read it. Module options include:
  14868. + *
  14869. + * buflen=N default N=4096, buffer size used
  14870. + * qlen=N default N=32, how many buffers in the loopback queue
  14871. + * loopdefault default false, list loopback config first
  14872. + *
  14873. + * Many drivers will only have one configuration, letting them be much
  14874. + * simpler if they also don't support high speed operation (like this
  14875. + * driver does).
  14876. + */
  14877. +
  14878. +#include <linux/config.h>
  14879. +#include <linux/module.h>
  14880. +#include <linux/kernel.h>
  14881. +#include <linux/delay.h>
  14882. +#include <linux/ioport.h>
  14883. +#include <linux/sched.h>
  14884. +#include <linux/slab.h>
  14885. +#include <linux/smp_lock.h>
  14886. +#include <linux/errno.h>
  14887. +#include <linux/init.h>
  14888. +#include <linux/timer.h>
  14889. +#include <linux/list.h>
  14890. +#include <linux/interrupt.h>
  14891. +#include <linux/uts.h>
  14892. +#include <linux/version.h>
  14893. +#include <linux/device.h>
  14894. +#include <linux/moduleparam.h>
  14895. +#include <linux/proc_fs.h>
  14896. +
  14897. +#include <asm/byteorder.h>
  14898. +#include <asm/io.h>
  14899. +#include <asm/irq.h>
  14900. +#include <asm/system.h>
  14901. +#include <asm/unaligned.h>
  14902. +
  14903. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
  14904. +# include <linux/usb/ch9.h>
  14905. +#else
  14906. +# include <linux/usb_ch9.h>
  14907. +#endif
  14908. +
  14909. +#include <linux/usb_gadget.h>
  14910. +
  14911. +
  14912. +/*-------------------------------------------------------------------------*/
  14913. +/*-------------------------------------------------------------------------*/
  14914. +
  14915. +
  14916. +static int utf8_to_utf16le(const char *s, u16 *cp, unsigned len)
  14917. +{
  14918. + int count = 0;
  14919. + u8 c;
  14920. + u16 uchar;
  14921. +
  14922. + /* this insists on correct encodings, though not minimal ones.
  14923. + * BUT it currently rejects legit 4-byte UTF-8 code points,
  14924. + * which need surrogate pairs. (Unicode 3.1 can use them.)
  14925. + */
  14926. + while (len != 0 && (c = (u8) *s++) != 0) {
  14927. + if (unlikely(c & 0x80)) {
  14928. + // 2-byte sequence:
  14929. + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
  14930. + if ((c & 0xe0) == 0xc0) {
  14931. + uchar = (c & 0x1f) << 6;
  14932. +
  14933. + c = (u8) *s++;
  14934. + if ((c & 0xc0) != 0xc0)
  14935. + goto fail;
  14936. + c &= 0x3f;
  14937. + uchar |= c;
  14938. +
  14939. + // 3-byte sequence (most CJKV characters):
  14940. + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
  14941. + } else if ((c & 0xf0) == 0xe0) {
  14942. + uchar = (c & 0x0f) << 12;
  14943. +
  14944. + c = (u8) *s++;
  14945. + if ((c & 0xc0) != 0xc0)
  14946. + goto fail;
  14947. + c &= 0x3f;
  14948. + uchar |= c << 6;
  14949. +
  14950. + c = (u8) *s++;
  14951. + if ((c & 0xc0) != 0xc0)
  14952. + goto fail;
  14953. + c &= 0x3f;
  14954. + uchar |= c;
  14955. +
  14956. + /* no bogus surrogates */
  14957. + if (0xd800 <= uchar && uchar <= 0xdfff)
  14958. + goto fail;
  14959. +
  14960. + // 4-byte sequence (surrogate pairs, currently rare):
  14961. + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
  14962. + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
  14963. + // (uuuuu = wwww + 1)
  14964. + // FIXME accept the surrogate code points (only)
  14965. +
  14966. + } else
  14967. + goto fail;
  14968. + } else
  14969. + uchar = c;
  14970. + put_unaligned (cpu_to_le16 (uchar), cp++);
  14971. + count++;
  14972. + len--;
  14973. + }
  14974. + return count;
  14975. +fail:
  14976. + return -1;
  14977. +}
  14978. +
  14979. +
  14980. +/**
  14981. + * usb_gadget_get_string - fill out a string descriptor
  14982. + * @table: of c strings encoded using UTF-8
  14983. + * @id: string id, from low byte of wValue in get string descriptor
  14984. + * @buf: at least 256 bytes
  14985. + *
  14986. + * Finds the UTF-8 string matching the ID, and converts it into a
  14987. + * string descriptor in utf16-le.
  14988. + * Returns length of descriptor (always even) or negative errno
  14989. + *
  14990. + * If your driver needs stings in multiple languages, you'll probably
  14991. + * "switch (wIndex) { ... }" in your ep0 string descriptor logic,
  14992. + * using this routine after choosing which set of UTF-8 strings to use.
  14993. + * Note that US-ASCII is a strict subset of UTF-8; any string bytes with
  14994. + * the eighth bit set will be multibyte UTF-8 characters, not ISO-8859/1
  14995. + * characters (which are also widely used in C strings).
  14996. + */
  14997. +int
  14998. +usb_gadget_get_string (struct usb_gadget_strings *table, int id, u8 *buf)
  14999. +{
  15000. + struct usb_string *s;
  15001. + int len;
  15002. +
  15003. + /* descriptor 0 has the language id */
  15004. + if (id == 0) {
  15005. + buf [0] = 4;
  15006. + buf [1] = USB_DT_STRING;
  15007. + buf [2] = (u8) table->language;
  15008. + buf [3] = (u8) (table->language >> 8);
  15009. + return 4;
  15010. + }
  15011. + for (s = table->strings; s && s->s; s++)
  15012. + if (s->id == id)
  15013. + break;
  15014. +
  15015. + /* unrecognized: stall. */
  15016. + if (!s || !s->s)
  15017. + return -EINVAL;
  15018. +
  15019. + /* string descriptors have length, tag, then UTF16-LE text */
  15020. + len = min ((size_t) 126, strlen (s->s));
  15021. + memset (buf + 2, 0, 2 * len); /* zero all the bytes */
  15022. + len = utf8_to_utf16le(s->s, (u16 *)&buf[2], len);
  15023. + if (len < 0)
  15024. + return -EINVAL;
  15025. + buf [0] = (len + 1) * 2;
  15026. + buf [1] = USB_DT_STRING;
  15027. + return buf [0];
  15028. +}
  15029. +
  15030. +
  15031. +/*-------------------------------------------------------------------------*/
  15032. +/*-------------------------------------------------------------------------*/
  15033. +
  15034. +
  15035. +/**
  15036. + * usb_descriptor_fillbuf - fill buffer with descriptors
  15037. + * @buf: Buffer to be filled
  15038. + * @buflen: Size of buf
  15039. + * @src: Array of descriptor pointers, terminated by null pointer.
  15040. + *
  15041. + * Copies descriptors into the buffer, returning the length or a
  15042. + * negative error code if they can't all be copied. Useful when
  15043. + * assembling descriptors for an associated set of interfaces used
  15044. + * as part of configuring a composite device; or in other cases where
  15045. + * sets of descriptors need to be marshaled.
  15046. + */
  15047. +int
  15048. +usb_descriptor_fillbuf(void *buf, unsigned buflen,
  15049. + const struct usb_descriptor_header **src)
  15050. +{
  15051. + u8 *dest = buf;
  15052. +
  15053. + if (!src)
  15054. + return -EINVAL;
  15055. +
  15056. + /* fill buffer from src[] until null descriptor ptr */
  15057. + for (; 0 != *src; src++) {
  15058. + unsigned len = (*src)->bLength;
  15059. +
  15060. + if (len > buflen)
  15061. + return -EINVAL;
  15062. + memcpy(dest, *src, len);
  15063. + buflen -= len;
  15064. + dest += len;
  15065. + }
  15066. + return dest - (u8 *)buf;
  15067. +}
  15068. +
  15069. +
  15070. +/**
  15071. + * usb_gadget_config_buf - builts a complete configuration descriptor
  15072. + * @config: Header for the descriptor, including characteristics such
  15073. + * as power requirements and number of interfaces.
  15074. + * @desc: Null-terminated vector of pointers to the descriptors (interface,
  15075. + * endpoint, etc) defining all functions in this device configuration.
  15076. + * @buf: Buffer for the resulting configuration descriptor.
  15077. + * @length: Length of buffer. If this is not big enough to hold the
  15078. + * entire configuration descriptor, an error code will be returned.
  15079. + *
  15080. + * This copies descriptors into the response buffer, building a descriptor
  15081. + * for that configuration. It returns the buffer length or a negative
  15082. + * status code. The config.wTotalLength field is set to match the length
  15083. + * of the result, but other descriptor fields (including power usage and
  15084. + * interface count) must be set by the caller.
  15085. + *
  15086. + * Gadget drivers could use this when constructing a config descriptor
  15087. + * in response to USB_REQ_GET_DESCRIPTOR. They will need to patch the
  15088. + * resulting bDescriptorType value if USB_DT_OTHER_SPEED_CONFIG is needed.
  15089. + */
  15090. +int usb_gadget_config_buf(
  15091. + const struct usb_config_descriptor *config,
  15092. + void *buf,
  15093. + unsigned length,
  15094. + const struct usb_descriptor_header **desc
  15095. +)
  15096. +{
  15097. + struct usb_config_descriptor *cp = buf;
  15098. + int len;
  15099. +
  15100. + /* config descriptor first */
  15101. + if (length < USB_DT_CONFIG_SIZE || !desc)
  15102. + return -EINVAL;
  15103. + *cp = *config;
  15104. +
  15105. + /* then interface/endpoint/class/vendor/... */
  15106. + len = usb_descriptor_fillbuf(USB_DT_CONFIG_SIZE + (u8*)buf,
  15107. + length - USB_DT_CONFIG_SIZE, desc);
  15108. + if (len < 0)
  15109. + return len;
  15110. + len += USB_DT_CONFIG_SIZE;
  15111. + if (len > 0xffff)
  15112. + return -EINVAL;
  15113. +
  15114. + /* patch up the config descriptor */
  15115. + cp->bLength = USB_DT_CONFIG_SIZE;
  15116. + cp->bDescriptorType = USB_DT_CONFIG;
  15117. + cp->wTotalLength = cpu_to_le16(len);
  15118. + cp->bmAttributes |= USB_CONFIG_ATT_ONE;
  15119. + return len;
  15120. +}
  15121. +
  15122. +/*-------------------------------------------------------------------------*/
  15123. +/*-------------------------------------------------------------------------*/
  15124. +
  15125. +
  15126. +#define RBUF_LEN (1024*1024)
  15127. +static int rbuf_start;
  15128. +static int rbuf_len;
  15129. +static __u8 rbuf[RBUF_LEN];
  15130. +
  15131. +/*-------------------------------------------------------------------------*/
  15132. +
  15133. +#define DRIVER_VERSION "St Patrick's Day 2004"
  15134. +
  15135. +static const char shortname [] = "zero";
  15136. +static const char longname [] = "YAMAHA YST-MS35D USB Speaker ";
  15137. +
  15138. +static const char source_sink [] = "source and sink data";
  15139. +static const char loopback [] = "loop input to output";
  15140. +
  15141. +/*-------------------------------------------------------------------------*/
  15142. +
  15143. +/*
  15144. + * driver assumes self-powered hardware, and
  15145. + * has no way for users to trigger remote wakeup.
  15146. + *
  15147. + * this version autoconfigures as much as possible,
  15148. + * which is reasonable for most "bulk-only" drivers.
  15149. + */
  15150. +static const char *EP_IN_NAME; /* source */
  15151. +static const char *EP_OUT_NAME; /* sink */
  15152. +
  15153. +/*-------------------------------------------------------------------------*/
  15154. +
  15155. +/* big enough to hold our biggest descriptor */
  15156. +#define USB_BUFSIZ 512
  15157. +
  15158. +struct zero_dev {
  15159. + spinlock_t lock;
  15160. + struct usb_gadget *gadget;
  15161. + struct usb_request *req; /* for control responses */
  15162. +
  15163. + /* when configured, we have one of two configs:
  15164. + * - source data (in to host) and sink it (out from host)
  15165. + * - or loop it back (out from host back in to host)
  15166. + */
  15167. + u8 config;
  15168. + struct usb_ep *in_ep, *out_ep;
  15169. +
  15170. + /* autoresume timer */
  15171. + struct timer_list resume;
  15172. +};
  15173. +
  15174. +#define xprintk(d,level,fmt,args...) \
  15175. + dev_printk(level , &(d)->gadget->dev , fmt , ## args)
  15176. +
  15177. +#ifdef DEBUG
  15178. +#define DBG(dev,fmt,args...) \
  15179. + xprintk(dev , KERN_DEBUG , fmt , ## args)
  15180. +#else
  15181. +#define DBG(dev,fmt,args...) \
  15182. + do { } while (0)
  15183. +#endif /* DEBUG */
  15184. +
  15185. +#ifdef VERBOSE
  15186. +#define VDBG DBG
  15187. +#else
  15188. +#define VDBG(dev,fmt,args...) \
  15189. + do { } while (0)
  15190. +#endif /* VERBOSE */
  15191. +
  15192. +#define ERROR(dev,fmt,args...) \
  15193. + xprintk(dev , KERN_ERR , fmt , ## args)
  15194. +#define WARN(dev,fmt,args...) \
  15195. + xprintk(dev , KERN_WARNING , fmt , ## args)
  15196. +#define INFO(dev,fmt,args...) \
  15197. + xprintk(dev , KERN_INFO , fmt , ## args)
  15198. +
  15199. +/*-------------------------------------------------------------------------*/
  15200. +
  15201. +static unsigned buflen = 4096;
  15202. +static unsigned qlen = 32;
  15203. +static unsigned pattern = 0;
  15204. +
  15205. +module_param (buflen, uint, S_IRUGO|S_IWUSR);
  15206. +module_param (qlen, uint, S_IRUGO|S_IWUSR);
  15207. +module_param (pattern, uint, S_IRUGO|S_IWUSR);
  15208. +
  15209. +/*
  15210. + * if it's nonzero, autoresume says how many seconds to wait
  15211. + * before trying to wake up the host after suspend.
  15212. + */
  15213. +static unsigned autoresume = 0;
  15214. +module_param (autoresume, uint, 0);
  15215. +
  15216. +/*
  15217. + * Normally the "loopback" configuration is second (index 1) so
  15218. + * it's not the default. Here's where to change that order, to
  15219. + * work better with hosts where config changes are problematic.
  15220. + * Or controllers (like superh) that only support one config.
  15221. + */
  15222. +static int loopdefault = 0;
  15223. +
  15224. +module_param (loopdefault, bool, S_IRUGO|S_IWUSR);
  15225. +
  15226. +/*-------------------------------------------------------------------------*/
  15227. +
  15228. +/* Thanks to NetChip Technologies for donating this product ID.
  15229. + *
  15230. + * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
  15231. + * Instead: allocate your own, using normal USB-IF procedures.
  15232. + */
  15233. +#ifndef CONFIG_USB_ZERO_HNPTEST
  15234. +#define DRIVER_VENDOR_NUM 0x0525 /* NetChip */
  15235. +#define DRIVER_PRODUCT_NUM 0xa4a0 /* Linux-USB "Gadget Zero" */
  15236. +#else
  15237. +#define DRIVER_VENDOR_NUM 0x1a0a /* OTG test device IDs */
  15238. +#define DRIVER_PRODUCT_NUM 0xbadd
  15239. +#endif
  15240. +
  15241. +/*-------------------------------------------------------------------------*/
  15242. +
  15243. +/*
  15244. + * DESCRIPTORS ... most are static, but strings and (full)
  15245. + * configuration descriptors are built on demand.
  15246. + */
  15247. +
  15248. +/*
  15249. +#define STRING_MANUFACTURER 25
  15250. +#define STRING_PRODUCT 42
  15251. +#define STRING_SERIAL 101
  15252. +*/
  15253. +#define STRING_MANUFACTURER 1
  15254. +#define STRING_PRODUCT 2
  15255. +#define STRING_SERIAL 3
  15256. +
  15257. +#define STRING_SOURCE_SINK 250
  15258. +#define STRING_LOOPBACK 251
  15259. +
  15260. +/*
  15261. + * This device advertises two configurations; these numbers work
  15262. + * on a pxa250 as well as more flexible hardware.
  15263. + */
  15264. +#define CONFIG_SOURCE_SINK 3
  15265. +#define CONFIG_LOOPBACK 2
  15266. +
  15267. +/*
  15268. +static struct usb_device_descriptor
  15269. +device_desc = {
  15270. + .bLength = sizeof device_desc,
  15271. + .bDescriptorType = USB_DT_DEVICE,
  15272. +
  15273. + .bcdUSB = __constant_cpu_to_le16 (0x0200),
  15274. + .bDeviceClass = USB_CLASS_VENDOR_SPEC,
  15275. +
  15276. + .idVendor = __constant_cpu_to_le16 (DRIVER_VENDOR_NUM),
  15277. + .idProduct = __constant_cpu_to_le16 (DRIVER_PRODUCT_NUM),
  15278. + .iManufacturer = STRING_MANUFACTURER,
  15279. + .iProduct = STRING_PRODUCT,
  15280. + .iSerialNumber = STRING_SERIAL,
  15281. + .bNumConfigurations = 2,
  15282. +};
  15283. +*/
  15284. +static struct usb_device_descriptor
  15285. +device_desc = {
  15286. + .bLength = sizeof device_desc,
  15287. + .bDescriptorType = USB_DT_DEVICE,
  15288. + .bcdUSB = __constant_cpu_to_le16 (0x0100),
  15289. + .bDeviceClass = USB_CLASS_PER_INTERFACE,
  15290. + .bDeviceSubClass = 0,
  15291. + .bDeviceProtocol = 0,
  15292. + .bMaxPacketSize0 = 64,
  15293. + .bcdDevice = __constant_cpu_to_le16 (0x0100),
  15294. + .idVendor = __constant_cpu_to_le16 (0x0499),
  15295. + .idProduct = __constant_cpu_to_le16 (0x3002),
  15296. + .iManufacturer = STRING_MANUFACTURER,
  15297. + .iProduct = STRING_PRODUCT,
  15298. + .iSerialNumber = STRING_SERIAL,
  15299. + .bNumConfigurations = 1,
  15300. +};
  15301. +
  15302. +static struct usb_config_descriptor
  15303. +z_config = {
  15304. + .bLength = sizeof z_config,
  15305. + .bDescriptorType = USB_DT_CONFIG,
  15306. +
  15307. + /* compute wTotalLength on the fly */
  15308. + .bNumInterfaces = 2,
  15309. + .bConfigurationValue = 1,
  15310. + .iConfiguration = 0,
  15311. + .bmAttributes = 0x40,
  15312. + .bMaxPower = 0, /* self-powered */
  15313. +};
  15314. +
  15315. +
  15316. +static struct usb_otg_descriptor
  15317. +otg_descriptor = {
  15318. + .bLength = sizeof otg_descriptor,
  15319. + .bDescriptorType = USB_DT_OTG,
  15320. +
  15321. + .bmAttributes = USB_OTG_SRP,
  15322. +};
  15323. +
  15324. +/* one interface in each configuration */
  15325. +#ifdef CONFIG_USB_GADGET_DUALSPEED
  15326. +
  15327. +/*
  15328. + * usb 2.0 devices need to expose both high speed and full speed
  15329. + * descriptors, unless they only run at full speed.
  15330. + *
  15331. + * that means alternate endpoint descriptors (bigger packets)
  15332. + * and a "device qualifier" ... plus more construction options
  15333. + * for the config descriptor.
  15334. + */
  15335. +
  15336. +static struct usb_qualifier_descriptor
  15337. +dev_qualifier = {
  15338. + .bLength = sizeof dev_qualifier,
  15339. + .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
  15340. +
  15341. + .bcdUSB = __constant_cpu_to_le16 (0x0200),
  15342. + .bDeviceClass = USB_CLASS_VENDOR_SPEC,
  15343. +
  15344. + .bNumConfigurations = 2,
  15345. +};
  15346. +
  15347. +
  15348. +struct usb_cs_as_general_descriptor {
  15349. + __u8 bLength;
  15350. + __u8 bDescriptorType;
  15351. +
  15352. + __u8 bDescriptorSubType;
  15353. + __u8 bTerminalLink;
  15354. + __u8 bDelay;
  15355. + __u16 wFormatTag;
  15356. +} __attribute__ ((packed));
  15357. +
  15358. +struct usb_cs_as_format_descriptor {
  15359. + __u8 bLength;
  15360. + __u8 bDescriptorType;
  15361. +
  15362. + __u8 bDescriptorSubType;
  15363. + __u8 bFormatType;
  15364. + __u8 bNrChannels;
  15365. + __u8 bSubframeSize;
  15366. + __u8 bBitResolution;
  15367. + __u8 bSamfreqType;
  15368. + __u8 tLowerSamFreq[3];
  15369. + __u8 tUpperSamFreq[3];
  15370. +} __attribute__ ((packed));
  15371. +
  15372. +static const struct usb_interface_descriptor
  15373. +z_audio_control_if_desc = {
  15374. + .bLength = sizeof z_audio_control_if_desc,
  15375. + .bDescriptorType = USB_DT_INTERFACE,
  15376. + .bInterfaceNumber = 0,
  15377. + .bAlternateSetting = 0,
  15378. + .bNumEndpoints = 0,
  15379. + .bInterfaceClass = USB_CLASS_AUDIO,
  15380. + .bInterfaceSubClass = 0x1,
  15381. + .bInterfaceProtocol = 0,
  15382. + .iInterface = 0,
  15383. +};
  15384. +
  15385. +static const struct usb_interface_descriptor
  15386. +z_audio_if_desc = {
  15387. + .bLength = sizeof z_audio_if_desc,
  15388. + .bDescriptorType = USB_DT_INTERFACE,
  15389. + .bInterfaceNumber = 1,
  15390. + .bAlternateSetting = 0,
  15391. + .bNumEndpoints = 0,
  15392. + .bInterfaceClass = USB_CLASS_AUDIO,
  15393. + .bInterfaceSubClass = 0x2,
  15394. + .bInterfaceProtocol = 0,
  15395. + .iInterface = 0,
  15396. +};
  15397. +
  15398. +static const struct usb_interface_descriptor
  15399. +z_audio_if_desc2 = {
  15400. + .bLength = sizeof z_audio_if_desc,
  15401. + .bDescriptorType = USB_DT_INTERFACE,
  15402. + .bInterfaceNumber = 1,
  15403. + .bAlternateSetting = 1,
  15404. + .bNumEndpoints = 1,
  15405. + .bInterfaceClass = USB_CLASS_AUDIO,
  15406. + .bInterfaceSubClass = 0x2,
  15407. + .bInterfaceProtocol = 0,
  15408. + .iInterface = 0,
  15409. +};
  15410. +
  15411. +static const struct usb_cs_as_general_descriptor
  15412. +z_audio_cs_as_if_desc = {
  15413. + .bLength = 7,
  15414. + .bDescriptorType = 0x24,
  15415. +
  15416. + .bDescriptorSubType = 0x01,
  15417. + .bTerminalLink = 0x01,
  15418. + .bDelay = 0x0,
  15419. + .wFormatTag = __constant_cpu_to_le16 (0x0001)
  15420. +};
  15421. +
  15422. +
  15423. +static const struct usb_cs_as_format_descriptor
  15424. +z_audio_cs_as_format_desc = {
  15425. + .bLength = 0xe,
  15426. + .bDescriptorType = 0x24,
  15427. +
  15428. + .bDescriptorSubType = 2,
  15429. + .bFormatType = 1,
  15430. + .bNrChannels = 1,
  15431. + .bSubframeSize = 1,
  15432. + .bBitResolution = 8,
  15433. + .bSamfreqType = 0,
  15434. + .tLowerSamFreq = {0x7e, 0x13, 0x00},
  15435. + .tUpperSamFreq = {0xe2, 0xd6, 0x00},
  15436. +};
  15437. +
  15438. +static const struct usb_endpoint_descriptor
  15439. +z_iso_ep = {
  15440. + .bLength = 0x09,
  15441. + .bDescriptorType = 0x05,
  15442. + .bEndpointAddress = 0x04,
  15443. + .bmAttributes = 0x09,
  15444. + .wMaxPacketSize = 0x0038,
  15445. + .bInterval = 0x01,
  15446. + .bRefresh = 0x00,
  15447. + .bSynchAddress = 0x00,
  15448. +};
  15449. +
  15450. +static char z_iso_ep2[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  15451. +
  15452. +// 9 bytes
  15453. +static char z_ac_interface_header_desc[] =
  15454. +{ 0x09, 0x24, 0x01, 0x00, 0x01, 0x2b, 0x00, 0x01, 0x01 };
  15455. +
  15456. +// 12 bytes
  15457. +static char z_0[] = {0x0c, 0x24, 0x02, 0x01, 0x01, 0x01, 0x00, 0x02,
  15458. + 0x03, 0x00, 0x00, 0x00};
  15459. +// 13 bytes
  15460. +static char z_1[] = {0x0d, 0x24, 0x06, 0x02, 0x01, 0x02, 0x15, 0x00,
  15461. + 0x02, 0x00, 0x02, 0x00, 0x00};
  15462. +// 9 bytes
  15463. +static char z_2[] = {0x09, 0x24, 0x03, 0x03, 0x01, 0x03, 0x00, 0x02,
  15464. + 0x00};
  15465. +
  15466. +static char za_0[] = {0x09, 0x04, 0x01, 0x02, 0x01, 0x01, 0x02, 0x00,
  15467. + 0x00};
  15468. +
  15469. +static char za_1[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
  15470. +
  15471. +static char za_2[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x01, 0x08, 0x00,
  15472. + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
  15473. +
  15474. +static char za_3[] = {0x09, 0x05, 0x04, 0x09, 0x70, 0x00, 0x01, 0x00,
  15475. + 0x00};
  15476. +
  15477. +static char za_4[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  15478. +
  15479. +static char za_5[] = {0x09, 0x04, 0x01, 0x03, 0x01, 0x01, 0x02, 0x00,
  15480. + 0x00};
  15481. +
  15482. +static char za_6[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
  15483. +
  15484. +static char za_7[] = {0x0e, 0x24, 0x02, 0x01, 0x01, 0x02, 0x10, 0x00,
  15485. + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
  15486. +
  15487. +static char za_8[] = {0x09, 0x05, 0x04, 0x09, 0x70, 0x00, 0x01, 0x00,
  15488. + 0x00};
  15489. +
  15490. +static char za_9[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  15491. +
  15492. +static char za_10[] = {0x09, 0x04, 0x01, 0x04, 0x01, 0x01, 0x02, 0x00,
  15493. + 0x00};
  15494. +
  15495. +static char za_11[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
  15496. +
  15497. +static char za_12[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x02, 0x10, 0x00,
  15498. + 0x73, 0x13, 0x00, 0xe2, 0xd6, 0x00};
  15499. +
  15500. +static char za_13[] = {0x09, 0x05, 0x04, 0x09, 0xe0, 0x00, 0x01, 0x00,
  15501. + 0x00};
  15502. +
  15503. +static char za_14[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  15504. +
  15505. +static char za_15[] = {0x09, 0x04, 0x01, 0x05, 0x01, 0x01, 0x02, 0x00,
  15506. + 0x00};
  15507. +
  15508. +static char za_16[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
  15509. +
  15510. +static char za_17[] = {0x0e, 0x24, 0x02, 0x01, 0x01, 0x03, 0x14, 0x00,
  15511. + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
  15512. +
  15513. +static char za_18[] = {0x09, 0x05, 0x04, 0x09, 0xa8, 0x00, 0x01, 0x00,
  15514. + 0x00};
  15515. +
  15516. +static char za_19[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  15517. +
  15518. +static char za_20[] = {0x09, 0x04, 0x01, 0x06, 0x01, 0x01, 0x02, 0x00,
  15519. + 0x00};
  15520. +
  15521. +static char za_21[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
  15522. +
  15523. +static char za_22[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x03, 0x14, 0x00,
  15524. + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
  15525. +
  15526. +static char za_23[] = {0x09, 0x05, 0x04, 0x09, 0x50, 0x01, 0x01, 0x00,
  15527. + 0x00};
  15528. +
  15529. +static char za_24[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  15530. +
  15531. +
  15532. +
  15533. +static const struct usb_descriptor_header *z_function [] = {
  15534. + (struct usb_descriptor_header *) &z_audio_control_if_desc,
  15535. + (struct usb_descriptor_header *) &z_ac_interface_header_desc,
  15536. + (struct usb_descriptor_header *) &z_0,
  15537. + (struct usb_descriptor_header *) &z_1,
  15538. + (struct usb_descriptor_header *) &z_2,
  15539. + (struct usb_descriptor_header *) &z_audio_if_desc,
  15540. + (struct usb_descriptor_header *) &z_audio_if_desc2,
  15541. + (struct usb_descriptor_header *) &z_audio_cs_as_if_desc,
  15542. + (struct usb_descriptor_header *) &z_audio_cs_as_format_desc,
  15543. + (struct usb_descriptor_header *) &z_iso_ep,
  15544. + (struct usb_descriptor_header *) &z_iso_ep2,
  15545. + (struct usb_descriptor_header *) &za_0,
  15546. + (struct usb_descriptor_header *) &za_1,
  15547. + (struct usb_descriptor_header *) &za_2,
  15548. + (struct usb_descriptor_header *) &za_3,
  15549. + (struct usb_descriptor_header *) &za_4,
  15550. + (struct usb_descriptor_header *) &za_5,
  15551. + (struct usb_descriptor_header *) &za_6,
  15552. + (struct usb_descriptor_header *) &za_7,
  15553. + (struct usb_descriptor_header *) &za_8,
  15554. + (struct usb_descriptor_header *) &za_9,
  15555. + (struct usb_descriptor_header *) &za_10,
  15556. + (struct usb_descriptor_header *) &za_11,
  15557. + (struct usb_descriptor_header *) &za_12,
  15558. + (struct usb_descriptor_header *) &za_13,
  15559. + (struct usb_descriptor_header *) &za_14,
  15560. + (struct usb_descriptor_header *) &za_15,
  15561. + (struct usb_descriptor_header *) &za_16,
  15562. + (struct usb_descriptor_header *) &za_17,
  15563. + (struct usb_descriptor_header *) &za_18,
  15564. + (struct usb_descriptor_header *) &za_19,
  15565. + (struct usb_descriptor_header *) &za_20,
  15566. + (struct usb_descriptor_header *) &za_21,
  15567. + (struct usb_descriptor_header *) &za_22,
  15568. + (struct usb_descriptor_header *) &za_23,
  15569. + (struct usb_descriptor_header *) &za_24,
  15570. + NULL,
  15571. +};
  15572. +
  15573. +/* maxpacket and other transfer characteristics vary by speed. */
  15574. +#define ep_desc(g,hs,fs) (((g)->speed==USB_SPEED_HIGH)?(hs):(fs))
  15575. +
  15576. +#else
  15577. +
  15578. +/* if there's no high speed support, maxpacket doesn't change. */
  15579. +#define ep_desc(g,hs,fs) fs
  15580. +
  15581. +#endif /* !CONFIG_USB_GADGET_DUALSPEED */
  15582. +
  15583. +static char manufacturer [40];
  15584. +//static char serial [40];
  15585. +static char serial [] = "Ser 00 em";
  15586. +
  15587. +/* static strings, in UTF-8 */
  15588. +static struct usb_string strings [] = {
  15589. + { STRING_MANUFACTURER, manufacturer, },
  15590. + { STRING_PRODUCT, longname, },
  15591. + { STRING_SERIAL, serial, },
  15592. + { STRING_LOOPBACK, loopback, },
  15593. + { STRING_SOURCE_SINK, source_sink, },
  15594. + { } /* end of list */
  15595. +};
  15596. +
  15597. +static struct usb_gadget_strings stringtab = {
  15598. + .language = 0x0409, /* en-us */
  15599. + .strings = strings,
  15600. +};
  15601. +
  15602. +/*
  15603. + * config descriptors are also handcrafted. these must agree with code
  15604. + * that sets configurations, and with code managing interfaces and their
  15605. + * altsettings. other complexity may come from:
  15606. + *
  15607. + * - high speed support, including "other speed config" rules
  15608. + * - multiple configurations
  15609. + * - interfaces with alternate settings
  15610. + * - embedded class or vendor-specific descriptors
  15611. + *
  15612. + * this handles high speed, and has a second config that could as easily
  15613. + * have been an alternate interface setting (on most hardware).
  15614. + *
  15615. + * NOTE: to demonstrate (and test) more USB capabilities, this driver
  15616. + * should include an altsetting to test interrupt transfers, including
  15617. + * high bandwidth modes at high speed. (Maybe work like Intel's test
  15618. + * device?)
  15619. + */
  15620. +static int
  15621. +config_buf (struct usb_gadget *gadget, u8 *buf, u8 type, unsigned index)
  15622. +{
  15623. + int len;
  15624. + const struct usb_descriptor_header **function;
  15625. +
  15626. + function = z_function;
  15627. + len = usb_gadget_config_buf (&z_config, buf, USB_BUFSIZ, function);
  15628. + if (len < 0)
  15629. + return len;
  15630. + ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
  15631. + return len;
  15632. +}
  15633. +
  15634. +/*-------------------------------------------------------------------------*/
  15635. +
  15636. +static struct usb_request *
  15637. +alloc_ep_req (struct usb_ep *ep, unsigned length)
  15638. +{
  15639. + struct usb_request *req;
  15640. +
  15641. + req = usb_ep_alloc_request (ep, GFP_ATOMIC);
  15642. + if (req) {
  15643. + req->length = length;
  15644. + req->buf = usb_ep_alloc_buffer (ep, length,
  15645. + &req->dma, GFP_ATOMIC);
  15646. + if (!req->buf) {
  15647. + usb_ep_free_request (ep, req);
  15648. + req = NULL;
  15649. + }
  15650. + }
  15651. + return req;
  15652. +}
  15653. +
  15654. +static void free_ep_req (struct usb_ep *ep, struct usb_request *req)
  15655. +{
  15656. + if (req->buf)
  15657. + usb_ep_free_buffer (ep, req->buf, req->dma, req->length);
  15658. + usb_ep_free_request (ep, req);
  15659. +}
  15660. +
  15661. +/*-------------------------------------------------------------------------*/
  15662. +
  15663. +/* optionally require specific source/sink data patterns */
  15664. +
  15665. +static int
  15666. +check_read_data (
  15667. + struct zero_dev *dev,
  15668. + struct usb_ep *ep,
  15669. + struct usb_request *req
  15670. +)
  15671. +{
  15672. + unsigned i;
  15673. + u8 *buf = req->buf;
  15674. +
  15675. + for (i = 0; i < req->actual; i++, buf++) {
  15676. + switch (pattern) {
  15677. + /* all-zeroes has no synchronization issues */
  15678. + case 0:
  15679. + if (*buf == 0)
  15680. + continue;
  15681. + break;
  15682. + /* mod63 stays in sync with short-terminated transfers,
  15683. + * or otherwise when host and gadget agree on how large
  15684. + * each usb transfer request should be. resync is done
  15685. + * with set_interface or set_config.
  15686. + */
  15687. + case 1:
  15688. + if (*buf == (u8)(i % 63))
  15689. + continue;
  15690. + break;
  15691. + }
  15692. + ERROR (dev, "bad OUT byte, buf [%d] = %d\n", i, *buf);
  15693. + usb_ep_set_halt (ep);
  15694. + return -EINVAL;
  15695. + }
  15696. + return 0;
  15697. +}
  15698. +
  15699. +/*-------------------------------------------------------------------------*/
  15700. +
  15701. +static void zero_reset_config (struct zero_dev *dev)
  15702. +{
  15703. + if (dev->config == 0)
  15704. + return;
  15705. +
  15706. + DBG (dev, "reset config\n");
  15707. +
  15708. + /* just disable endpoints, forcing completion of pending i/o.
  15709. + * all our completion handlers free their requests in this case.
  15710. + */
  15711. + if (dev->in_ep) {
  15712. + usb_ep_disable (dev->in_ep);
  15713. + dev->in_ep = NULL;
  15714. + }
  15715. + if (dev->out_ep) {
  15716. + usb_ep_disable (dev->out_ep);
  15717. + dev->out_ep = NULL;
  15718. + }
  15719. + dev->config = 0;
  15720. + del_timer (&dev->resume);
  15721. +}
  15722. +
  15723. +#define _write(f, buf, sz) (f->f_op->write(f, buf, sz, &f->f_pos))
  15724. +
  15725. +static void
  15726. +zero_isoc_complete (struct usb_ep *ep, struct usb_request *req)
  15727. +{
  15728. + struct zero_dev *dev = ep->driver_data;
  15729. + int status = req->status;
  15730. + int i, j;
  15731. +
  15732. + switch (status) {
  15733. +
  15734. + case 0: /* normal completion? */
  15735. + //printk ("\nzero ---------------> isoc normal completion %d bytes\n", req->actual);
  15736. + for (i=0, j=rbuf_start; i<req->actual; i++) {
  15737. + //printk ("%02x ", ((__u8*)req->buf)[i]);
  15738. + rbuf[j] = ((__u8*)req->buf)[i];
  15739. + j++;
  15740. + if (j >= RBUF_LEN) j=0;
  15741. + }
  15742. + rbuf_start = j;
  15743. + //printk ("\n\n");
  15744. +
  15745. + if (rbuf_len < RBUF_LEN) {
  15746. + rbuf_len += req->actual;
  15747. + if (rbuf_len > RBUF_LEN) {
  15748. + rbuf_len = RBUF_LEN;
  15749. + }
  15750. + }
  15751. +
  15752. + break;
  15753. +
  15754. + /* this endpoint is normally active while we're configured */
  15755. + case -ECONNABORTED: /* hardware forced ep reset */
  15756. + case -ECONNRESET: /* request dequeued */
  15757. + case -ESHUTDOWN: /* disconnect from host */
  15758. + VDBG (dev, "%s gone (%d), %d/%d\n", ep->name, status,
  15759. + req->actual, req->length);
  15760. + if (ep == dev->out_ep)
  15761. + check_read_data (dev, ep, req);
  15762. + free_ep_req (ep, req);
  15763. + return;
  15764. +
  15765. + case -EOVERFLOW: /* buffer overrun on read means that
  15766. + * we didn't provide a big enough
  15767. + * buffer.
  15768. + */
  15769. + default:
  15770. +#if 1
  15771. + DBG (dev, "%s complete --> %d, %d/%d\n", ep->name,
  15772. + status, req->actual, req->length);
  15773. +#endif
  15774. + case -EREMOTEIO: /* short read */
  15775. + break;
  15776. + }
  15777. +
  15778. + status = usb_ep_queue (ep, req, GFP_ATOMIC);
  15779. + if (status) {
  15780. + ERROR (dev, "kill %s: resubmit %d bytes --> %d\n",
  15781. + ep->name, req->length, status);
  15782. + usb_ep_set_halt (ep);
  15783. + /* FIXME recover later ... somehow */
  15784. + }
  15785. +}
  15786. +
  15787. +static struct usb_request *
  15788. +zero_start_isoc_ep (struct usb_ep *ep, int gfp_flags)
  15789. +{
  15790. + struct usb_request *req;
  15791. + int status;
  15792. +
  15793. + req = alloc_ep_req (ep, 512);
  15794. + if (!req)
  15795. + return NULL;
  15796. +
  15797. + req->complete = zero_isoc_complete;
  15798. +
  15799. + status = usb_ep_queue (ep, req, gfp_flags);
  15800. + if (status) {
  15801. + struct zero_dev *dev = ep->driver_data;
  15802. +
  15803. + ERROR (dev, "start %s --> %d\n", ep->name, status);
  15804. + free_ep_req (ep, req);
  15805. + req = NULL;
  15806. + }
  15807. +
  15808. + return req;
  15809. +}
  15810. +
  15811. +/* change our operational config. this code must agree with the code
  15812. + * that returns config descriptors, and altsetting code.
  15813. + *
  15814. + * it's also responsible for power management interactions. some
  15815. + * configurations might not work with our current power sources.
  15816. + *
  15817. + * note that some device controller hardware will constrain what this
  15818. + * code can do, perhaps by disallowing more than one configuration or
  15819. + * by limiting configuration choices (like the pxa2xx).
  15820. + */
  15821. +static int
  15822. +zero_set_config (struct zero_dev *dev, unsigned number, int gfp_flags)
  15823. +{
  15824. + int result = 0;
  15825. + struct usb_gadget *gadget = dev->gadget;
  15826. + const struct usb_endpoint_descriptor *d;
  15827. + struct usb_ep *ep;
  15828. +
  15829. + if (number == dev->config)
  15830. + return 0;
  15831. +
  15832. + zero_reset_config (dev);
  15833. +
  15834. + gadget_for_each_ep (ep, gadget) {
  15835. +
  15836. + if (strcmp (ep->name, "ep4") == 0) {
  15837. +
  15838. + d = (struct usb_endpoint_descripter *)&za_23; // isoc ep desc for audio i/f alt setting 6
  15839. + result = usb_ep_enable (ep, d);
  15840. +
  15841. + if (result == 0) {
  15842. + ep->driver_data = dev;
  15843. + dev->in_ep = ep;
  15844. +
  15845. + if (zero_start_isoc_ep (ep, gfp_flags) != 0) {
  15846. +
  15847. + dev->in_ep = ep;
  15848. + continue;
  15849. + }
  15850. +
  15851. + usb_ep_disable (ep);
  15852. + result = -EIO;
  15853. + }
  15854. + }
  15855. +
  15856. + }
  15857. +
  15858. + dev->config = number;
  15859. + return result;
  15860. +}
  15861. +
  15862. +/*-------------------------------------------------------------------------*/
  15863. +
  15864. +static void zero_setup_complete (struct usb_ep *ep, struct usb_request *req)
  15865. +{
  15866. + if (req->status || req->actual != req->length)
  15867. + DBG ((struct zero_dev *) ep->driver_data,
  15868. + "setup complete --> %d, %d/%d\n",
  15869. + req->status, req->actual, req->length);
  15870. +}
  15871. +
  15872. +/*
  15873. + * The setup() callback implements all the ep0 functionality that's
  15874. + * not handled lower down, in hardware or the hardware driver (like
  15875. + * device and endpoint feature flags, and their status). It's all
  15876. + * housekeeping for the gadget function we're implementing. Most of
  15877. + * the work is in config-specific setup.
  15878. + */
  15879. +static int
  15880. +zero_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
  15881. +{
  15882. + struct zero_dev *dev = get_gadget_data (gadget);
  15883. + struct usb_request *req = dev->req;
  15884. + int value = -EOPNOTSUPP;
  15885. +
  15886. + /* usually this stores reply data in the pre-allocated ep0 buffer,
  15887. + * but config change events will reconfigure hardware.
  15888. + */
  15889. + req->zero = 0;
  15890. + switch (ctrl->bRequest) {
  15891. +
  15892. + case USB_REQ_GET_DESCRIPTOR:
  15893. +
  15894. + switch (ctrl->wValue >> 8) {
  15895. +
  15896. + case USB_DT_DEVICE:
  15897. + value = min (ctrl->wLength, (u16) sizeof device_desc);
  15898. + memcpy (req->buf, &device_desc, value);
  15899. + break;
  15900. +#ifdef CONFIG_USB_GADGET_DUALSPEED
  15901. + case USB_DT_DEVICE_QUALIFIER:
  15902. + if (!gadget->is_dualspeed)
  15903. + break;
  15904. + value = min (ctrl->wLength, (u16) sizeof dev_qualifier);
  15905. + memcpy (req->buf, &dev_qualifier, value);
  15906. + break;
  15907. +
  15908. + case USB_DT_OTHER_SPEED_CONFIG:
  15909. + if (!gadget->is_dualspeed)
  15910. + break;
  15911. + // FALLTHROUGH
  15912. +#endif /* CONFIG_USB_GADGET_DUALSPEED */
  15913. + case USB_DT_CONFIG:
  15914. + value = config_buf (gadget, req->buf,
  15915. + ctrl->wValue >> 8,
  15916. + ctrl->wValue & 0xff);
  15917. + if (value >= 0)
  15918. + value = min (ctrl->wLength, (u16) value);
  15919. + break;
  15920. +
  15921. + case USB_DT_STRING:
  15922. + /* wIndex == language code.
  15923. + * this driver only handles one language, you can
  15924. + * add string tables for other languages, using
  15925. + * any UTF-8 characters
  15926. + */
  15927. + value = usb_gadget_get_string (&stringtab,
  15928. + ctrl->wValue & 0xff, req->buf);
  15929. + if (value >= 0) {
  15930. + value = min (ctrl->wLength, (u16) value);
  15931. + }
  15932. + break;
  15933. + }
  15934. + break;
  15935. +
  15936. + /* currently two configs, two speeds */
  15937. + case USB_REQ_SET_CONFIGURATION:
  15938. + if (ctrl->bRequestType != 0)
  15939. + goto unknown;
  15940. +
  15941. + spin_lock (&dev->lock);
  15942. + value = zero_set_config (dev, ctrl->wValue, GFP_ATOMIC);
  15943. + spin_unlock (&dev->lock);
  15944. + break;
  15945. + case USB_REQ_GET_CONFIGURATION:
  15946. + if (ctrl->bRequestType != USB_DIR_IN)
  15947. + goto unknown;
  15948. + *(u8 *)req->buf = dev->config;
  15949. + value = min (ctrl->wLength, (u16) 1);
  15950. + break;
  15951. +
  15952. + /* until we add altsetting support, or other interfaces,
  15953. + * only 0/0 are possible. pxa2xx only supports 0/0 (poorly)
  15954. + * and already killed pending endpoint I/O.
  15955. + */
  15956. + case USB_REQ_SET_INTERFACE:
  15957. +
  15958. + if (ctrl->bRequestType != USB_RECIP_INTERFACE)
  15959. + goto unknown;
  15960. + spin_lock (&dev->lock);
  15961. + if (dev->config) {
  15962. + u8 config = dev->config;
  15963. +
  15964. + /* resets interface configuration, forgets about
  15965. + * previous transaction state (queued bufs, etc)
  15966. + * and re-inits endpoint state (toggle etc)
  15967. + * no response queued, just zero status == success.
  15968. + * if we had more than one interface we couldn't
  15969. + * use this "reset the config" shortcut.
  15970. + */
  15971. + zero_reset_config (dev);
  15972. + zero_set_config (dev, config, GFP_ATOMIC);
  15973. + value = 0;
  15974. + }
  15975. + spin_unlock (&dev->lock);
  15976. + break;
  15977. + case USB_REQ_GET_INTERFACE:
  15978. + if ((ctrl->bRequestType == 0x21) && (ctrl->wIndex == 0x02)) {
  15979. + value = ctrl->wLength;
  15980. + break;
  15981. + }
  15982. + else {
  15983. + if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
  15984. + goto unknown;
  15985. + if (!dev->config)
  15986. + break;
  15987. + if (ctrl->wIndex != 0) {
  15988. + value = -EDOM;
  15989. + break;
  15990. + }
  15991. + *(u8 *)req->buf = 0;
  15992. + value = min (ctrl->wLength, (u16) 1);
  15993. + }
  15994. + break;
  15995. +
  15996. + /*
  15997. + * These are the same vendor-specific requests supported by
  15998. + * Intel's USB 2.0 compliance test devices. We exceed that
  15999. + * device spec by allowing multiple-packet requests.
  16000. + */
  16001. + case 0x5b: /* control WRITE test -- fill the buffer */
  16002. + if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
  16003. + goto unknown;
  16004. + if (ctrl->wValue || ctrl->wIndex)
  16005. + break;
  16006. + /* just read that many bytes into the buffer */
  16007. + if (ctrl->wLength > USB_BUFSIZ)
  16008. + break;
  16009. + value = ctrl->wLength;
  16010. + break;
  16011. + case 0x5c: /* control READ test -- return the buffer */
  16012. + if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
  16013. + goto unknown;
  16014. + if (ctrl->wValue || ctrl->wIndex)
  16015. + break;
  16016. + /* expect those bytes are still in the buffer; send back */
  16017. + if (ctrl->wLength > USB_BUFSIZ
  16018. + || ctrl->wLength != req->length)
  16019. + break;
  16020. + value = ctrl->wLength;
  16021. + break;
  16022. +
  16023. + case 0x01: // SET_CUR
  16024. + case 0x02:
  16025. + case 0x03:
  16026. + case 0x04:
  16027. + case 0x05:
  16028. + value = ctrl->wLength;
  16029. + break;
  16030. + case 0x81:
  16031. + switch (ctrl->wValue) {
  16032. + case 0x0201:
  16033. + case 0x0202:
  16034. + ((u8*)req->buf)[0] = 0x00;
  16035. + ((u8*)req->buf)[1] = 0xe3;
  16036. + break;
  16037. + case 0x0300:
  16038. + case 0x0500:
  16039. + ((u8*)req->buf)[0] = 0x00;
  16040. + break;
  16041. + }
  16042. + //((u8*)req->buf)[0] = 0x81;
  16043. + //((u8*)req->buf)[1] = 0x81;
  16044. + value = ctrl->wLength;
  16045. + break;
  16046. + case 0x82:
  16047. + switch (ctrl->wValue) {
  16048. + case 0x0201:
  16049. + case 0x0202:
  16050. + ((u8*)req->buf)[0] = 0x00;
  16051. + ((u8*)req->buf)[1] = 0xc3;
  16052. + break;
  16053. + case 0x0300:
  16054. + case 0x0500:
  16055. + ((u8*)req->buf)[0] = 0x00;
  16056. + break;
  16057. + }
  16058. + //((u8*)req->buf)[0] = 0x82;
  16059. + //((u8*)req->buf)[1] = 0x82;
  16060. + value = ctrl->wLength;
  16061. + break;
  16062. + case 0x83:
  16063. + switch (ctrl->wValue) {
  16064. + case 0x0201:
  16065. + case 0x0202:
  16066. + ((u8*)req->buf)[0] = 0x00;
  16067. + ((u8*)req->buf)[1] = 0x00;
  16068. + break;
  16069. + case 0x0300:
  16070. + ((u8*)req->buf)[0] = 0x60;
  16071. + break;
  16072. + case 0x0500:
  16073. + ((u8*)req->buf)[0] = 0x18;
  16074. + break;
  16075. + }
  16076. + //((u8*)req->buf)[0] = 0x83;
  16077. + //((u8*)req->buf)[1] = 0x83;
  16078. + value = ctrl->wLength;
  16079. + break;
  16080. + case 0x84:
  16081. + switch (ctrl->wValue) {
  16082. + case 0x0201:
  16083. + case 0x0202:
  16084. + ((u8*)req->buf)[0] = 0x00;
  16085. + ((u8*)req->buf)[1] = 0x01;
  16086. + break;
  16087. + case 0x0300:
  16088. + case 0x0500:
  16089. + ((u8*)req->buf)[0] = 0x08;
  16090. + break;
  16091. + }
  16092. + //((u8*)req->buf)[0] = 0x84;
  16093. + //((u8*)req->buf)[1] = 0x84;
  16094. + value = ctrl->wLength;
  16095. + break;
  16096. + case 0x85:
  16097. + ((u8*)req->buf)[0] = 0x85;
  16098. + ((u8*)req->buf)[1] = 0x85;
  16099. + value = ctrl->wLength;
  16100. + break;
  16101. +
  16102. +
  16103. + default:
  16104. +unknown:
  16105. + printk("unknown control req%02x.%02x v%04x i%04x l%d\n",
  16106. + ctrl->bRequestType, ctrl->bRequest,
  16107. + ctrl->wValue, ctrl->wIndex, ctrl->wLength);
  16108. + }
  16109. +
  16110. + /* respond with data transfer before status phase? */
  16111. + if (value >= 0) {
  16112. + req->length = value;
  16113. + req->zero = value < ctrl->wLength
  16114. + && (value % gadget->ep0->maxpacket) == 0;
  16115. + value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
  16116. + if (value < 0) {
  16117. + DBG (dev, "ep_queue < 0 --> %d\n", value);
  16118. + req->status = 0;
  16119. + zero_setup_complete (gadget->ep0, req);
  16120. + }
  16121. + }
  16122. +
  16123. + /* device either stalls (value < 0) or reports success */
  16124. + return value;
  16125. +}
  16126. +
  16127. +static void
  16128. +zero_disconnect (struct usb_gadget *gadget)
  16129. +{
  16130. + struct zero_dev *dev = get_gadget_data (gadget);
  16131. + unsigned long flags;
  16132. +
  16133. + spin_lock_irqsave (&dev->lock, flags);
  16134. + zero_reset_config (dev);
  16135. +
  16136. + /* a more significant application might have some non-usb
  16137. + * activities to quiesce here, saving resources like power
  16138. + * or pushing the notification up a network stack.
  16139. + */
  16140. + spin_unlock_irqrestore (&dev->lock, flags);
  16141. +
  16142. + /* next we may get setup() calls to enumerate new connections;
  16143. + * or an unbind() during shutdown (including removing module).
  16144. + */
  16145. +}
  16146. +
  16147. +static void
  16148. +zero_autoresume (unsigned long _dev)
  16149. +{
  16150. + struct zero_dev *dev = (struct zero_dev *) _dev;
  16151. + int status;
  16152. +
  16153. + /* normally the host would be woken up for something
  16154. + * more significant than just a timer firing...
  16155. + */
  16156. + if (dev->gadget->speed != USB_SPEED_UNKNOWN) {
  16157. + status = usb_gadget_wakeup (dev->gadget);
  16158. + DBG (dev, "wakeup --> %d\n", status);
  16159. + }
  16160. +}
  16161. +
  16162. +/*-------------------------------------------------------------------------*/
  16163. +
  16164. +static void
  16165. +zero_unbind (struct usb_gadget *gadget)
  16166. +{
  16167. + struct zero_dev *dev = get_gadget_data (gadget);
  16168. +
  16169. + DBG (dev, "unbind\n");
  16170. +
  16171. + /* we've already been disconnected ... no i/o is active */
  16172. + if (dev->req)
  16173. + free_ep_req (gadget->ep0, dev->req);
  16174. + del_timer_sync (&dev->resume);
  16175. + kfree (dev);
  16176. + set_gadget_data (gadget, NULL);
  16177. +}
  16178. +
  16179. +static int
  16180. +zero_bind (struct usb_gadget *gadget)
  16181. +{
  16182. + struct zero_dev *dev;
  16183. + //struct usb_ep *ep;
  16184. +
  16185. + printk("binding\n");
  16186. + /*
  16187. + * DRIVER POLICY CHOICE: you may want to do this differently.
  16188. + * One thing to avoid is reusing a bcdDevice revision code
  16189. + * with different host-visible configurations or behavior
  16190. + * restrictions -- using ep1in/ep2out vs ep1out/ep3in, etc
  16191. + */
  16192. + //device_desc.bcdDevice = __constant_cpu_to_le16 (0x0201);
  16193. +
  16194. +
  16195. + /* ok, we made sense of the hardware ... */
  16196. + dev = kzalloc (sizeof *dev, SLAB_KERNEL);
  16197. + if (!dev)
  16198. + return -ENOMEM;
  16199. + spin_lock_init (&dev->lock);
  16200. + dev->gadget = gadget;
  16201. + set_gadget_data (gadget, dev);
  16202. +
  16203. + /* preallocate control response and buffer */
  16204. + dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
  16205. + if (!dev->req)
  16206. + goto enomem;
  16207. + dev->req->buf = usb_ep_alloc_buffer (gadget->ep0, USB_BUFSIZ,
  16208. + &dev->req->dma, GFP_KERNEL);
  16209. + if (!dev->req->buf)
  16210. + goto enomem;
  16211. +
  16212. + dev->req->complete = zero_setup_complete;
  16213. +
  16214. + device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
  16215. +
  16216. +#ifdef CONFIG_USB_GADGET_DUALSPEED
  16217. + /* assume ep0 uses the same value for both speeds ... */
  16218. + dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
  16219. +
  16220. + /* and that all endpoints are dual-speed */
  16221. + //hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
  16222. + //hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
  16223. +#endif
  16224. +
  16225. + usb_gadget_set_selfpowered (gadget);
  16226. +
  16227. + init_timer (&dev->resume);
  16228. + dev->resume.function = zero_autoresume;
  16229. + dev->resume.data = (unsigned long) dev;
  16230. +
  16231. + gadget->ep0->driver_data = dev;
  16232. +
  16233. + INFO (dev, "%s, version: " DRIVER_VERSION "\n", longname);
  16234. + INFO (dev, "using %s, OUT %s IN %s\n", gadget->name,
  16235. + EP_OUT_NAME, EP_IN_NAME);
  16236. +
  16237. + snprintf (manufacturer, sizeof manufacturer,
  16238. + UTS_SYSNAME " " UTS_RELEASE " with %s",
  16239. + gadget->name);
  16240. +
  16241. + return 0;
  16242. +
  16243. +enomem:
  16244. + zero_unbind (gadget);
  16245. + return -ENOMEM;
  16246. +}
  16247. +
  16248. +/*-------------------------------------------------------------------------*/
  16249. +
  16250. +static void
  16251. +zero_suspend (struct usb_gadget *gadget)
  16252. +{
  16253. + struct zero_dev *dev = get_gadget_data (gadget);
  16254. +
  16255. + if (gadget->speed == USB_SPEED_UNKNOWN)
  16256. + return;
  16257. +
  16258. + if (autoresume) {
  16259. + mod_timer (&dev->resume, jiffies + (HZ * autoresume));
  16260. + DBG (dev, "suspend, wakeup in %d seconds\n", autoresume);
  16261. + } else
  16262. + DBG (dev, "suspend\n");
  16263. +}
  16264. +
  16265. +static void
  16266. +zero_resume (struct usb_gadget *gadget)
  16267. +{
  16268. + struct zero_dev *dev = get_gadget_data (gadget);
  16269. +
  16270. + DBG (dev, "resume\n");
  16271. + del_timer (&dev->resume);
  16272. +}
  16273. +
  16274. +
  16275. +/*-------------------------------------------------------------------------*/
  16276. +
  16277. +static struct usb_gadget_driver zero_driver = {
  16278. +#ifdef CONFIG_USB_GADGET_DUALSPEED
  16279. + .speed = USB_SPEED_HIGH,
  16280. +#else
  16281. + .speed = USB_SPEED_FULL,
  16282. +#endif
  16283. + .function = (char *) longname,
  16284. + .bind = zero_bind,
  16285. + .unbind = zero_unbind,
  16286. +
  16287. + .setup = zero_setup,
  16288. + .disconnect = zero_disconnect,
  16289. +
  16290. + .suspend = zero_suspend,
  16291. + .resume = zero_resume,
  16292. +
  16293. + .driver = {
  16294. + .name = (char *) shortname,
  16295. + // .shutdown = ...
  16296. + // .suspend = ...
  16297. + // .resume = ...
  16298. + },
  16299. +};
  16300. +
  16301. +MODULE_AUTHOR ("David Brownell");
  16302. +MODULE_LICENSE ("Dual BSD/GPL");
  16303. +
  16304. +static struct proc_dir_entry *pdir, *pfile;
  16305. +
  16306. +static int isoc_read_data (char *page, char **start,
  16307. + off_t off, int count,
  16308. + int *eof, void *data)
  16309. +{
  16310. + int i;
  16311. + static int c = 0;
  16312. + static int done = 0;
  16313. + static int s = 0;
  16314. +
  16315. +/*
  16316. + printk ("\ncount: %d\n", count);
  16317. + printk ("rbuf_start: %d\n", rbuf_start);
  16318. + printk ("rbuf_len: %d\n", rbuf_len);
  16319. + printk ("off: %d\n", off);
  16320. + printk ("start: %p\n\n", *start);
  16321. +*/
  16322. + if (done) {
  16323. + c = 0;
  16324. + done = 0;
  16325. + *eof = 1;
  16326. + return 0;
  16327. + }
  16328. +
  16329. + if (c == 0) {
  16330. + if (rbuf_len == RBUF_LEN)
  16331. + s = rbuf_start;
  16332. + else s = 0;
  16333. + }
  16334. +
  16335. + for (i=0; i<count && c<rbuf_len; i++, c++) {
  16336. + page[i] = rbuf[(c+s) % RBUF_LEN];
  16337. + }
  16338. + *start = page;
  16339. +
  16340. + if (c >= rbuf_len) {
  16341. + *eof = 1;
  16342. + done = 1;
  16343. + }
  16344. +
  16345. +
  16346. + return i;
  16347. +}
  16348. +
  16349. +static int __init init (void)
  16350. +{
  16351. +
  16352. + int retval = 0;
  16353. +
  16354. + pdir = proc_mkdir("isoc_test", NULL);
  16355. + if(pdir == NULL) {
  16356. + retval = -ENOMEM;
  16357. + printk("Error creating dir\n");
  16358. + goto done;
  16359. + }
  16360. + pdir->owner = THIS_MODULE;
  16361. +
  16362. + pfile = create_proc_read_entry("isoc_data",
  16363. + 0444, pdir,
  16364. + isoc_read_data,
  16365. + NULL);
  16366. + if (pfile == NULL) {
  16367. + retval = -ENOMEM;
  16368. + printk("Error creating file\n");
  16369. + goto no_file;
  16370. + }
  16371. + pfile->owner = THIS_MODULE;
  16372. +
  16373. + return usb_gadget_register_driver (&zero_driver);
  16374. +
  16375. + no_file:
  16376. + remove_proc_entry("isoc_data", NULL);
  16377. + done:
  16378. + return retval;
  16379. +}
  16380. +module_init (init);
  16381. +
  16382. +static void __exit cleanup (void)
  16383. +{
  16384. +
  16385. + usb_gadget_unregister_driver (&zero_driver);
  16386. +
  16387. + remove_proc_entry("isoc_data", pdir);
  16388. + remove_proc_entry("isoc_test", NULL);
  16389. +}
  16390. +module_exit (cleanup);
  16391. --- /dev/null
  16392. +++ b/drivers/usb/host/dwc_otg/dwc_cfi_common.h
  16393. @@ -0,0 +1,142 @@
  16394. +/* ==========================================================================
  16395. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  16396. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  16397. + * otherwise expressly agreed to in writing between Synopsys and you.
  16398. + *
  16399. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  16400. + * any End User Software License Agreement or Agreement for Licensed Product
  16401. + * with Synopsys or any supplement thereto. You are permitted to use and
  16402. + * redistribute this Software in source and binary forms, with or without
  16403. + * modification, provided that redistributions of source code must retain this
  16404. + * notice. You may not view, use, disclose, copy or distribute this file or
  16405. + * any information contained herein except pursuant to this license grant from
  16406. + * Synopsys. If you do not agree with this notice, including the disclaimer
  16407. + * below, then you are not authorized to use the Software.
  16408. + *
  16409. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  16410. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16411. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16412. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  16413. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  16414. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  16415. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  16416. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  16417. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  16418. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  16419. + * DAMAGE.
  16420. + * ========================================================================== */
  16421. +
  16422. +#if !defined(__DWC_CFI_COMMON_H__)
  16423. +#define __DWC_CFI_COMMON_H__
  16424. +
  16425. +//#include <linux/types.h>
  16426. +
  16427. +/**
  16428. + * @file
  16429. + *
  16430. + * This file contains the CFI specific common constants, interfaces
  16431. + * (functions and macros) and structures for Linux. No PCD specific
  16432. + * data structure or definition is to be included in this file.
  16433. + *
  16434. + */
  16435. +
  16436. +/** This is a request for all Core Features */
  16437. +#define VEN_CORE_GET_FEATURES 0xB1
  16438. +
  16439. +/** This is a request to get the value of a specific Core Feature */
  16440. +#define VEN_CORE_GET_FEATURE 0xB2
  16441. +
  16442. +/** This command allows the host to set the value of a specific Core Feature */
  16443. +#define VEN_CORE_SET_FEATURE 0xB3
  16444. +
  16445. +/** This command allows the host to set the default values of
  16446. + * either all or any specific Core Feature
  16447. + */
  16448. +#define VEN_CORE_RESET_FEATURES 0xB4
  16449. +
  16450. +/** This command forces the PCD to write the deferred values of a Core Features */
  16451. +#define VEN_CORE_ACTIVATE_FEATURES 0xB5
  16452. +
  16453. +/** This request reads a DWORD value from a register at the specified offset */
  16454. +#define VEN_CORE_READ_REGISTER 0xB6
  16455. +
  16456. +/** This request writes a DWORD value into a register at the specified offset */
  16457. +#define VEN_CORE_WRITE_REGISTER 0xB7
  16458. +
  16459. +/** This structure is the header of the Core Features dataset returned to
  16460. + * the Host
  16461. + */
  16462. +struct cfi_all_features_header {
  16463. +/** The features header structure length is */
  16464. +#define CFI_ALL_FEATURES_HDR_LEN 8
  16465. + /**
  16466. + * The total length of the features dataset returned to the Host
  16467. + */
  16468. + uint16_t wTotalLen;
  16469. +
  16470. + /**
  16471. + * CFI version number inBinary-Coded Decimal (i.e., 1.00 is 100H).
  16472. + * This field identifies the version of the CFI Specification with which
  16473. + * the device is compliant.
  16474. + */
  16475. + uint16_t wVersion;
  16476. +
  16477. + /** The ID of the Core */
  16478. + uint16_t wCoreID;
  16479. +#define CFI_CORE_ID_UDC 1
  16480. +#define CFI_CORE_ID_OTG 2
  16481. +#define CFI_CORE_ID_WUDEV 3
  16482. +
  16483. + /** Number of features returned by VEN_CORE_GET_FEATURES request */
  16484. + uint16_t wNumFeatures;
  16485. +} UPACKED;
  16486. +
  16487. +typedef struct cfi_all_features_header cfi_all_features_header_t;
  16488. +
  16489. +/** This structure is a header of the Core Feature descriptor dataset returned to
  16490. + * the Host after the VEN_CORE_GET_FEATURES request
  16491. + */
  16492. +struct cfi_feature_desc_header {
  16493. +#define CFI_FEATURE_DESC_HDR_LEN 8
  16494. +
  16495. + /** The feature ID */
  16496. + uint16_t wFeatureID;
  16497. +
  16498. + /** Length of this feature descriptor in bytes - including the
  16499. + * length of the feature name string
  16500. + */
  16501. + uint16_t wLength;
  16502. +
  16503. + /** The data length of this feature in bytes */
  16504. + uint16_t wDataLength;
  16505. +
  16506. + /**
  16507. + * Attributes of this features
  16508. + * D0: Access rights
  16509. + * 0 - Read/Write
  16510. + * 1 - Read only
  16511. + */
  16512. + uint8_t bmAttributes;
  16513. +#define CFI_FEATURE_ATTR_RO 1
  16514. +#define CFI_FEATURE_ATTR_RW 0
  16515. +
  16516. + /** Length of the feature name in bytes */
  16517. + uint8_t bNameLen;
  16518. +
  16519. + /** The feature name buffer */
  16520. + //uint8_t *name;
  16521. +} UPACKED;
  16522. +
  16523. +typedef struct cfi_feature_desc_header cfi_feature_desc_header_t;
  16524. +
  16525. +/**
  16526. + * This structure describes a NULL terminated string referenced by its id field.
  16527. + * It is very similar to usb_string structure but has the id field type set to 16-bit.
  16528. + */
  16529. +struct cfi_string {
  16530. + uint16_t id;
  16531. + const uint8_t *s;
  16532. +};
  16533. +typedef struct cfi_string cfi_string_t;
  16534. +
  16535. +#endif
  16536. --- /dev/null
  16537. +++ b/drivers/usb/host/dwc_otg/dwc_otg_adp.c
  16538. @@ -0,0 +1,854 @@
  16539. +/* ==========================================================================
  16540. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_adp.c $
  16541. + * $Revision: #12 $
  16542. + * $Date: 2011/10/26 $
  16543. + * $Change: 1873028 $
  16544. + *
  16545. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  16546. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  16547. + * otherwise expressly agreed to in writing between Synopsys and you.
  16548. + *
  16549. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  16550. + * any End User Software License Agreement or Agreement for Licensed Product
  16551. + * with Synopsys or any supplement thereto. You are permitted to use and
  16552. + * redistribute this Software in source and binary forms, with or without
  16553. + * modification, provided that redistributions of source code must retain this
  16554. + * notice. You may not view, use, disclose, copy or distribute this file or
  16555. + * any information contained herein except pursuant to this license grant from
  16556. + * Synopsys. If you do not agree with this notice, including the disclaimer
  16557. + * below, then you are not authorized to use the Software.
  16558. + *
  16559. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  16560. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16561. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16562. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  16563. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  16564. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  16565. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  16566. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  16567. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  16568. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  16569. + * DAMAGE.
  16570. + * ========================================================================== */
  16571. +
  16572. +#include "dwc_os.h"
  16573. +#include "dwc_otg_regs.h"
  16574. +#include "dwc_otg_cil.h"
  16575. +#include "dwc_otg_adp.h"
  16576. +
  16577. +/** @file
  16578. + *
  16579. + * This file contains the most of the Attach Detect Protocol implementation for
  16580. + * the driver to support OTG Rev2.0.
  16581. + *
  16582. + */
  16583. +
  16584. +void dwc_otg_adp_write_reg(dwc_otg_core_if_t * core_if, uint32_t value)
  16585. +{
  16586. + adpctl_data_t adpctl;
  16587. +
  16588. + adpctl.d32 = value;
  16589. + adpctl.b.ar = 0x2;
  16590. +
  16591. + DWC_WRITE_REG32(&core_if->core_global_regs->adpctl, adpctl.d32);
  16592. +
  16593. + while (adpctl.b.ar) {
  16594. + adpctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->adpctl);
  16595. + }
  16596. +
  16597. +}
  16598. +
  16599. +/**
  16600. + * Function is called to read ADP registers
  16601. + */
  16602. +uint32_t dwc_otg_adp_read_reg(dwc_otg_core_if_t * core_if)
  16603. +{
  16604. + adpctl_data_t adpctl;
  16605. +
  16606. + adpctl.d32 = 0;
  16607. + adpctl.b.ar = 0x1;
  16608. +
  16609. + DWC_WRITE_REG32(&core_if->core_global_regs->adpctl, adpctl.d32);
  16610. +
  16611. + while (adpctl.b.ar) {
  16612. + adpctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->adpctl);
  16613. + }
  16614. +
  16615. + return adpctl.d32;
  16616. +}
  16617. +
  16618. +/**
  16619. + * Function is called to read ADPCTL register and filter Write-clear bits
  16620. + */
  16621. +uint32_t dwc_otg_adp_read_reg_filter(dwc_otg_core_if_t * core_if)
  16622. +{
  16623. + adpctl_data_t adpctl;
  16624. +
  16625. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  16626. + adpctl.b.adp_tmout_int = 0;
  16627. + adpctl.b.adp_prb_int = 0;
  16628. + adpctl.b.adp_tmout_int = 0;
  16629. +
  16630. + return adpctl.d32;
  16631. +}
  16632. +
  16633. +/**
  16634. + * Function is called to write ADP registers
  16635. + */
  16636. +void dwc_otg_adp_modify_reg(dwc_otg_core_if_t * core_if, uint32_t clr,
  16637. + uint32_t set)
  16638. +{
  16639. + dwc_otg_adp_write_reg(core_if,
  16640. + (dwc_otg_adp_read_reg(core_if) & (~clr)) | set);
  16641. +}
  16642. +
  16643. +static void adp_sense_timeout(void *ptr)
  16644. +{
  16645. + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
  16646. + core_if->adp.sense_timer_started = 0;
  16647. + DWC_PRINTF("ADP SENSE TIMEOUT\n");
  16648. + if (core_if->adp_enable) {
  16649. + dwc_otg_adp_sense_stop(core_if);
  16650. + dwc_otg_adp_probe_start(core_if);
  16651. + }
  16652. +}
  16653. +
  16654. +/**
  16655. + * This function is called when the ADP vbus timer expires. Timeout is 1.1s.
  16656. + */
  16657. +static void adp_vbuson_timeout(void *ptr)
  16658. +{
  16659. + gpwrdn_data_t gpwrdn;
  16660. + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
  16661. + hprt0_data_t hprt0 = {.d32 = 0 };
  16662. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  16663. + DWC_PRINTF("%s: 1.1 seconds expire after turning on VBUS\n",__FUNCTION__);
  16664. + if (core_if) {
  16665. + core_if->adp.vbuson_timer_started = 0;
  16666. + /* Turn off vbus */
  16667. + hprt0.b.prtpwr = 1;
  16668. + DWC_MODIFY_REG32(core_if->host_if->hprt0, hprt0.d32, 0);
  16669. + gpwrdn.d32 = 0;
  16670. +
  16671. + /* Power off the core */
  16672. + if (core_if->power_down == 2) {
  16673. + /* Enable Wakeup Logic */
  16674. +// gpwrdn.b.wkupactiv = 1;
  16675. + gpwrdn.b.pmuactv = 0;
  16676. + gpwrdn.b.pwrdnrstn = 1;
  16677. + gpwrdn.b.pwrdnclmp = 1;
  16678. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
  16679. + gpwrdn.d32);
  16680. +
  16681. + /* Suspend the Phy Clock */
  16682. + pcgcctl.b.stoppclk = 1;
  16683. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  16684. +
  16685. + /* Switch on VDD */
  16686. +// gpwrdn.b.wkupactiv = 1;
  16687. + gpwrdn.b.pmuactv = 1;
  16688. + gpwrdn.b.pwrdnrstn = 1;
  16689. + gpwrdn.b.pwrdnclmp = 1;
  16690. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
  16691. + gpwrdn.d32);
  16692. + } else {
  16693. + /* Enable Power Down Logic */
  16694. + gpwrdn.b.pmuintsel = 1;
  16695. + gpwrdn.b.pmuactv = 1;
  16696. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  16697. + }
  16698. +
  16699. + /* Power off the core */
  16700. + if (core_if->power_down == 2) {
  16701. + gpwrdn.d32 = 0;
  16702. + gpwrdn.b.pwrdnswtch = 1;
  16703. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn,
  16704. + gpwrdn.d32, 0);
  16705. + }
  16706. +
  16707. + /* Unmask SRP detected interrupt from Power Down Logic */
  16708. + gpwrdn.d32 = 0;
  16709. + gpwrdn.b.srp_det_msk = 1;
  16710. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  16711. +
  16712. + dwc_otg_adp_probe_start(core_if);
  16713. + dwc_otg_dump_global_registers(core_if);
  16714. + dwc_otg_dump_host_registers(core_if);
  16715. + }
  16716. +
  16717. +}
  16718. +
  16719. +/**
  16720. + * Start the ADP Initial Probe timer to detect if Port Connected interrupt is
  16721. + * not asserted within 1.1 seconds.
  16722. + *
  16723. + * @param core_if the pointer to core_if strucure.
  16724. + */
  16725. +void dwc_otg_adp_vbuson_timer_start(dwc_otg_core_if_t * core_if)
  16726. +{
  16727. + core_if->adp.vbuson_timer_started = 1;
  16728. + if (core_if->adp.vbuson_timer)
  16729. + {
  16730. + DWC_PRINTF("SCHEDULING VBUSON TIMER\n");
  16731. + /* 1.1 secs + 60ms necessary for cil_hcd_start*/
  16732. + DWC_TIMER_SCHEDULE(core_if->adp.vbuson_timer, 1160);
  16733. + } else {
  16734. + DWC_WARN("VBUSON_TIMER = %p\n",core_if->adp.vbuson_timer);
  16735. + }
  16736. +}
  16737. +
  16738. +#if 0
  16739. +/**
  16740. + * Masks all DWC OTG core interrupts
  16741. + *
  16742. + */
  16743. +static void mask_all_interrupts(dwc_otg_core_if_t * core_if)
  16744. +{
  16745. + int i;
  16746. + gahbcfg_data_t ahbcfg = {.d32 = 0 };
  16747. +
  16748. + /* Mask Host Interrupts */
  16749. +
  16750. + /* Clear and disable HCINTs */
  16751. + for (i = 0; i < core_if->core_params->host_channels; i++) {
  16752. + DWC_WRITE_REG32(&core_if->host_if->hc_regs[i]->hcintmsk, 0);
  16753. + DWC_WRITE_REG32(&core_if->host_if->hc_regs[i]->hcint, 0xFFFFFFFF);
  16754. +
  16755. + }
  16756. +
  16757. + /* Clear and disable HAINT */
  16758. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->haintmsk, 0x0000);
  16759. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->haint, 0xFFFFFFFF);
  16760. +
  16761. + /* Mask Device Interrupts */
  16762. + if (!core_if->multiproc_int_enable) {
  16763. + /* Clear and disable IN Endpoint interrupts */
  16764. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->diepmsk, 0);
  16765. + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  16766. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->
  16767. + diepint, 0xFFFFFFFF);
  16768. + }
  16769. +
  16770. + /* Clear and disable OUT Endpoint interrupts */
  16771. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->doepmsk, 0);
  16772. + for (i = 0; i <= core_if->dev_if->num_out_eps; i++) {
  16773. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->
  16774. + doepint, 0xFFFFFFFF);
  16775. + }
  16776. +
  16777. + /* Clear and disable DAINT */
  16778. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->daint,
  16779. + 0xFFFFFFFF);
  16780. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->daintmsk, 0);
  16781. + } else {
  16782. + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
  16783. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
  16784. + diepeachintmsk[i], 0);
  16785. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->
  16786. + diepint, 0xFFFFFFFF);
  16787. + }
  16788. +
  16789. + for (i = 0; i < core_if->dev_if->num_out_eps; ++i) {
  16790. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
  16791. + doepeachintmsk[i], 0);
  16792. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->
  16793. + doepint, 0xFFFFFFFF);
  16794. + }
  16795. +
  16796. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->deachintmsk,
  16797. + 0);
  16798. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->deachint,
  16799. + 0xFFFFFFFF);
  16800. +
  16801. + }
  16802. +
  16803. + /* Disable interrupts */
  16804. + ahbcfg.b.glblintrmsk = 1;
  16805. + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, ahbcfg.d32, 0);
  16806. +
  16807. + /* Disable all interrupts. */
  16808. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, 0);
  16809. +
  16810. + /* Clear any pending interrupts */
  16811. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  16812. +
  16813. + /* Clear any pending OTG Interrupts */
  16814. + DWC_WRITE_REG32(&core_if->core_global_regs->gotgint, 0xFFFFFFFF);
  16815. +}
  16816. +
  16817. +/**
  16818. + * Unmask Port Connection Detected interrupt
  16819. + *
  16820. + */
  16821. +static void unmask_conn_det_intr(dwc_otg_core_if_t * core_if)
  16822. +{
  16823. + gintmsk_data_t gintmsk = {.d32 = 0,.b.portintr = 1 };
  16824. +
  16825. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32);
  16826. +}
  16827. +#endif
  16828. +
  16829. +/**
  16830. + * Starts the ADP Probing
  16831. + *
  16832. + * @param core_if the pointer to core_if structure.
  16833. + */
  16834. +uint32_t dwc_otg_adp_probe_start(dwc_otg_core_if_t * core_if)
  16835. +{
  16836. +
  16837. + adpctl_data_t adpctl = {.d32 = 0};
  16838. + gpwrdn_data_t gpwrdn;
  16839. +#if 0
  16840. + adpctl_data_t adpctl_int = {.d32 = 0, .b.adp_prb_int = 1,
  16841. + .b.adp_sns_int = 1, b.adp_tmout_int};
  16842. +#endif
  16843. + dwc_otg_disable_global_interrupts(core_if);
  16844. + DWC_PRINTF("ADP Probe Start\n");
  16845. + core_if->adp.probe_enabled = 1;
  16846. +
  16847. + adpctl.b.adpres = 1;
  16848. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  16849. +
  16850. + while (adpctl.b.adpres) {
  16851. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  16852. + }
  16853. +
  16854. + adpctl.d32 = 0;
  16855. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  16856. +
  16857. + /* In Host mode unmask SRP detected interrupt */
  16858. + gpwrdn.d32 = 0;
  16859. + gpwrdn.b.sts_chngint_msk = 1;
  16860. + if (!gpwrdn.b.idsts) {
  16861. + gpwrdn.b.srp_det_msk = 1;
  16862. + }
  16863. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  16864. +
  16865. + adpctl.b.adp_tmout_int_msk = 1;
  16866. + adpctl.b.adp_prb_int_msk = 1;
  16867. + adpctl.b.prb_dschg = 1;
  16868. + adpctl.b.prb_delta = 1;
  16869. + adpctl.b.prb_per = 1;
  16870. + adpctl.b.adpen = 1;
  16871. + adpctl.b.enaprb = 1;
  16872. +
  16873. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  16874. + DWC_PRINTF("ADP Probe Finish\n");
  16875. + return 0;
  16876. +}
  16877. +
  16878. +/**
  16879. + * Starts the ADP Sense timer to detect if ADP Sense interrupt is not asserted
  16880. + * within 3 seconds.
  16881. + *
  16882. + * @param core_if the pointer to core_if strucure.
  16883. + */
  16884. +void dwc_otg_adp_sense_timer_start(dwc_otg_core_if_t * core_if)
  16885. +{
  16886. + core_if->adp.sense_timer_started = 1;
  16887. + DWC_TIMER_SCHEDULE(core_if->adp.sense_timer, 3000 /* 3 secs */ );
  16888. +}
  16889. +
  16890. +/**
  16891. + * Starts the ADP Sense
  16892. + *
  16893. + * @param core_if the pointer to core_if strucure.
  16894. + */
  16895. +uint32_t dwc_otg_adp_sense_start(dwc_otg_core_if_t * core_if)
  16896. +{
  16897. + adpctl_data_t adpctl;
  16898. +
  16899. + DWC_PRINTF("ADP Sense Start\n");
  16900. +
  16901. + /* Unmask ADP sense interrupt and mask all other from the core */
  16902. + adpctl.d32 = dwc_otg_adp_read_reg_filter(core_if);
  16903. + adpctl.b.adp_sns_int_msk = 1;
  16904. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  16905. + dwc_otg_disable_global_interrupts(core_if); // vahrama
  16906. +
  16907. + /* Set ADP reset bit*/
  16908. + adpctl.d32 = dwc_otg_adp_read_reg_filter(core_if);
  16909. + adpctl.b.adpres = 1;
  16910. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  16911. +
  16912. + while (adpctl.b.adpres) {
  16913. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  16914. + }
  16915. +
  16916. + adpctl.b.adpres = 0;
  16917. + adpctl.b.adpen = 1;
  16918. + adpctl.b.enasns = 1;
  16919. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  16920. +
  16921. + dwc_otg_adp_sense_timer_start(core_if);
  16922. +
  16923. + return 0;
  16924. +}
  16925. +
  16926. +/**
  16927. + * Stops the ADP Probing
  16928. + *
  16929. + * @param core_if the pointer to core_if strucure.
  16930. + */
  16931. +uint32_t dwc_otg_adp_probe_stop(dwc_otg_core_if_t * core_if)
  16932. +{
  16933. +
  16934. + adpctl_data_t adpctl;
  16935. + DWC_PRINTF("Stop ADP probe\n");
  16936. + core_if->adp.probe_enabled = 0;
  16937. + core_if->adp.probe_counter = 0;
  16938. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  16939. +
  16940. + adpctl.b.adpen = 0;
  16941. + adpctl.b.adp_prb_int = 1;
  16942. + adpctl.b.adp_tmout_int = 1;
  16943. + adpctl.b.adp_sns_int = 1;
  16944. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  16945. +
  16946. + return 0;
  16947. +}
  16948. +
  16949. +/**
  16950. + * Stops the ADP Sensing
  16951. + *
  16952. + * @param core_if the pointer to core_if strucure.
  16953. + */
  16954. +uint32_t dwc_otg_adp_sense_stop(dwc_otg_core_if_t * core_if)
  16955. +{
  16956. + adpctl_data_t adpctl;
  16957. +
  16958. + core_if->adp.sense_enabled = 0;
  16959. +
  16960. + adpctl.d32 = dwc_otg_adp_read_reg_filter(core_if);
  16961. + adpctl.b.enasns = 0;
  16962. + adpctl.b.adp_sns_int = 1;
  16963. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  16964. +
  16965. + return 0;
  16966. +}
  16967. +
  16968. +/**
  16969. + * Called to turn on the VBUS after initial ADP probe in host mode.
  16970. + * If port power was already enabled in cil_hcd_start function then
  16971. + * only schedule a timer.
  16972. + *
  16973. + * @param core_if the pointer to core_if structure.
  16974. + */
  16975. +void dwc_otg_adp_turnon_vbus(dwc_otg_core_if_t * core_if)
  16976. +{
  16977. + hprt0_data_t hprt0 = {.d32 = 0 };
  16978. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  16979. + DWC_PRINTF("Turn on VBUS for 1.1s, port power is %d\n", hprt0.b.prtpwr);
  16980. +
  16981. + if (hprt0.b.prtpwr == 0) {
  16982. + hprt0.b.prtpwr = 1;
  16983. + //DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  16984. + }
  16985. +
  16986. + dwc_otg_adp_vbuson_timer_start(core_if);
  16987. +}
  16988. +
  16989. +/**
  16990. + * Called right after driver is loaded
  16991. + * to perform initial actions for ADP
  16992. + *
  16993. + * @param core_if the pointer to core_if structure.
  16994. + * @param is_host - flag for current mode of operation either from GINTSTS or GPWRDN
  16995. + */
  16996. +void dwc_otg_adp_start(dwc_otg_core_if_t * core_if, uint8_t is_host)
  16997. +{
  16998. + gpwrdn_data_t gpwrdn;
  16999. +
  17000. + DWC_PRINTF("ADP Initial Start\n");
  17001. + core_if->adp.adp_started = 1;
  17002. +
  17003. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  17004. + dwc_otg_disable_global_interrupts(core_if);
  17005. + if (is_host) {
  17006. + DWC_PRINTF("HOST MODE\n");
  17007. + /* Enable Power Down Logic Interrupt*/
  17008. + gpwrdn.d32 = 0;
  17009. + gpwrdn.b.pmuintsel = 1;
  17010. + gpwrdn.b.pmuactv = 1;
  17011. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  17012. + /* Initialize first ADP probe to obtain Ramp Time value */
  17013. + core_if->adp.initial_probe = 1;
  17014. + dwc_otg_adp_probe_start(core_if);
  17015. + } else {
  17016. + gotgctl_data_t gotgctl;
  17017. + gotgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  17018. + DWC_PRINTF("DEVICE MODE\n");
  17019. + if (gotgctl.b.bsesvld == 0) {
  17020. + /* Enable Power Down Logic Interrupt*/
  17021. + gpwrdn.d32 = 0;
  17022. + DWC_PRINTF("VBUS is not valid - start ADP probe\n");
  17023. + gpwrdn.b.pmuintsel = 1;
  17024. + gpwrdn.b.pmuactv = 1;
  17025. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  17026. + core_if->adp.initial_probe = 1;
  17027. + dwc_otg_adp_probe_start(core_if);
  17028. + } else {
  17029. + DWC_PRINTF("VBUS is valid - initialize core as a Device\n");
  17030. + core_if->op_state = B_PERIPHERAL;
  17031. + dwc_otg_core_init(core_if);
  17032. + dwc_otg_enable_global_interrupts(core_if);
  17033. + cil_pcd_start(core_if);
  17034. + dwc_otg_dump_global_registers(core_if);
  17035. + dwc_otg_dump_dev_registers(core_if);
  17036. + }
  17037. + }
  17038. +}
  17039. +
  17040. +void dwc_otg_adp_init(dwc_otg_core_if_t * core_if)
  17041. +{
  17042. + core_if->adp.adp_started = 0;
  17043. + core_if->adp.initial_probe = 0;
  17044. + core_if->adp.probe_timer_values[0] = -1;
  17045. + core_if->adp.probe_timer_values[1] = -1;
  17046. + core_if->adp.probe_enabled = 0;
  17047. + core_if->adp.sense_enabled = 0;
  17048. + core_if->adp.sense_timer_started = 0;
  17049. + core_if->adp.vbuson_timer_started = 0;
  17050. + core_if->adp.probe_counter = 0;
  17051. + core_if->adp.gpwrdn = 0;
  17052. + core_if->adp.attached = DWC_OTG_ADP_UNKOWN;
  17053. + /* Initialize timers */
  17054. + core_if->adp.sense_timer =
  17055. + DWC_TIMER_ALLOC("ADP SENSE TIMER", adp_sense_timeout, core_if);
  17056. + core_if->adp.vbuson_timer =
  17057. + DWC_TIMER_ALLOC("ADP VBUS ON TIMER", adp_vbuson_timeout, core_if);
  17058. + if (!core_if->adp.sense_timer || !core_if->adp.vbuson_timer)
  17059. + {
  17060. + DWC_ERROR("Could not allocate memory for ADP timers\n");
  17061. + }
  17062. +}
  17063. +
  17064. +void dwc_otg_adp_remove(dwc_otg_core_if_t * core_if)
  17065. +{
  17066. + gpwrdn_data_t gpwrdn = { .d32 = 0 };
  17067. + gpwrdn.b.pmuintsel = 1;
  17068. + gpwrdn.b.pmuactv = 1;
  17069. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  17070. +
  17071. + if (core_if->adp.probe_enabled)
  17072. + dwc_otg_adp_probe_stop(core_if);
  17073. + if (core_if->adp.sense_enabled)
  17074. + dwc_otg_adp_sense_stop(core_if);
  17075. + if (core_if->adp.sense_timer_started)
  17076. + DWC_TIMER_CANCEL(core_if->adp.sense_timer);
  17077. + if (core_if->adp.vbuson_timer_started)
  17078. + DWC_TIMER_CANCEL(core_if->adp.vbuson_timer);
  17079. + DWC_TIMER_FREE(core_if->adp.sense_timer);
  17080. + DWC_TIMER_FREE(core_if->adp.vbuson_timer);
  17081. +}
  17082. +
  17083. +/////////////////////////////////////////////////////////////////////
  17084. +////////////// ADP Interrupt Handlers ///////////////////////////////
  17085. +/////////////////////////////////////////////////////////////////////
  17086. +/**
  17087. + * This function sets Ramp Timer values
  17088. + */
  17089. +static uint32_t set_timer_value(dwc_otg_core_if_t * core_if, uint32_t val)
  17090. +{
  17091. + if (core_if->adp.probe_timer_values[0] == -1) {
  17092. + core_if->adp.probe_timer_values[0] = val;
  17093. + core_if->adp.probe_timer_values[1] = -1;
  17094. + return 1;
  17095. + } else {
  17096. + core_if->adp.probe_timer_values[1] =
  17097. + core_if->adp.probe_timer_values[0];
  17098. + core_if->adp.probe_timer_values[0] = val;
  17099. + return 0;
  17100. + }
  17101. +}
  17102. +
  17103. +/**
  17104. + * This function compares Ramp Timer values
  17105. + */
  17106. +static uint32_t compare_timer_values(dwc_otg_core_if_t * core_if)
  17107. +{
  17108. + uint32_t diff;
  17109. + if (core_if->adp.probe_timer_values[0]>=core_if->adp.probe_timer_values[1])
  17110. + diff = core_if->adp.probe_timer_values[0]-core_if->adp.probe_timer_values[1];
  17111. + else
  17112. + diff = core_if->adp.probe_timer_values[1]-core_if->adp.probe_timer_values[0];
  17113. + if(diff < 2) {
  17114. + return 0;
  17115. + } else {
  17116. + return 1;
  17117. + }
  17118. +}
  17119. +
  17120. +/**
  17121. + * This function handles ADP Probe Interrupts
  17122. + */
  17123. +static int32_t dwc_otg_adp_handle_prb_intr(dwc_otg_core_if_t * core_if,
  17124. + uint32_t val)
  17125. +{
  17126. + adpctl_data_t adpctl = {.d32 = 0 };
  17127. + gpwrdn_data_t gpwrdn, temp;
  17128. + adpctl.d32 = val;
  17129. +
  17130. + temp.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  17131. + core_if->adp.probe_counter++;
  17132. + core_if->adp.gpwrdn = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  17133. + if (adpctl.b.rtim == 0 && !temp.b.idsts){
  17134. + DWC_PRINTF("RTIM value is 0\n");
  17135. + goto exit;
  17136. + }
  17137. + if (set_timer_value(core_if, adpctl.b.rtim) &&
  17138. + core_if->adp.initial_probe) {
  17139. + core_if->adp.initial_probe = 0;
  17140. + dwc_otg_adp_probe_stop(core_if);
  17141. + gpwrdn.d32 = 0;
  17142. + gpwrdn.b.pmuactv = 1;
  17143. + gpwrdn.b.pmuintsel = 1;
  17144. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  17145. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  17146. +
  17147. + /* check which value is for device mode and which for Host mode */
  17148. + if (!temp.b.idsts) { /* considered host mode value is 0 */
  17149. + /*
  17150. + * Turn on VBUS after initial ADP probe.
  17151. + */
  17152. + core_if->op_state = A_HOST;
  17153. + dwc_otg_enable_global_interrupts(core_if);
  17154. + DWC_SPINUNLOCK(core_if->lock);
  17155. + cil_hcd_start(core_if);
  17156. + dwc_otg_adp_turnon_vbus(core_if);
  17157. + DWC_SPINLOCK(core_if->lock);
  17158. + } else {
  17159. + /*
  17160. + * Initiate SRP after initial ADP probe.
  17161. + */
  17162. + dwc_otg_enable_global_interrupts(core_if);
  17163. + dwc_otg_initiate_srp(core_if);
  17164. + }
  17165. + } else if (core_if->adp.probe_counter > 2){
  17166. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  17167. + if (compare_timer_values(core_if)) {
  17168. + DWC_PRINTF("Difference in timer values !!! \n");
  17169. +// core_if->adp.attached = DWC_OTG_ADP_ATTACHED;
  17170. + dwc_otg_adp_probe_stop(core_if);
  17171. +
  17172. + /* Power on the core */
  17173. + if (core_if->power_down == 2) {
  17174. + gpwrdn.b.pwrdnswtch = 1;
  17175. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  17176. + gpwrdn, 0, gpwrdn.d32);
  17177. + }
  17178. +
  17179. + /* check which value is for device mode and which for Host mode */
  17180. + if (!temp.b.idsts) { /* considered host mode value is 0 */
  17181. + /* Disable Interrupt from Power Down Logic */
  17182. + gpwrdn.d32 = 0;
  17183. + gpwrdn.b.pmuintsel = 1;
  17184. + gpwrdn.b.pmuactv = 1;
  17185. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  17186. + gpwrdn, gpwrdn.d32, 0);
  17187. +
  17188. + /*
  17189. + * Initialize the Core for Host mode.
  17190. + */
  17191. + core_if->op_state = A_HOST;
  17192. + dwc_otg_core_init(core_if);
  17193. + dwc_otg_enable_global_interrupts(core_if);
  17194. + cil_hcd_start(core_if);
  17195. + } else {
  17196. + gotgctl_data_t gotgctl;
  17197. + /* Mask SRP detected interrupt from Power Down Logic */
  17198. + gpwrdn.d32 = 0;
  17199. + gpwrdn.b.srp_det_msk = 1;
  17200. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  17201. + gpwrdn, gpwrdn.d32, 0);
  17202. +
  17203. + /* Disable Power Down Logic */
  17204. + gpwrdn.d32 = 0;
  17205. + gpwrdn.b.pmuintsel = 1;
  17206. + gpwrdn.b.pmuactv = 1;
  17207. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  17208. + gpwrdn, gpwrdn.d32, 0);
  17209. +
  17210. + /*
  17211. + * Initialize the Core for Device mode.
  17212. + */
  17213. + core_if->op_state = B_PERIPHERAL;
  17214. + dwc_otg_core_init(core_if);
  17215. + dwc_otg_enable_global_interrupts(core_if);
  17216. + cil_pcd_start(core_if);
  17217. +
  17218. + gotgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  17219. + if (!gotgctl.b.bsesvld) {
  17220. + dwc_otg_initiate_srp(core_if);
  17221. + }
  17222. + }
  17223. + }
  17224. + if (core_if->power_down == 2) {
  17225. + if (gpwrdn.b.bsessvld) {
  17226. + /* Mask SRP detected interrupt from Power Down Logic */
  17227. + gpwrdn.d32 = 0;
  17228. + gpwrdn.b.srp_det_msk = 1;
  17229. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  17230. +
  17231. + /* Disable Power Down Logic */
  17232. + gpwrdn.d32 = 0;
  17233. + gpwrdn.b.pmuactv = 1;
  17234. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  17235. +
  17236. + /*
  17237. + * Initialize the Core for Device mode.
  17238. + */
  17239. + core_if->op_state = B_PERIPHERAL;
  17240. + dwc_otg_core_init(core_if);
  17241. + dwc_otg_enable_global_interrupts(core_if);
  17242. + cil_pcd_start(core_if);
  17243. + }
  17244. + }
  17245. + }
  17246. +exit:
  17247. + /* Clear interrupt */
  17248. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  17249. + adpctl.b.adp_prb_int = 1;
  17250. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  17251. +
  17252. + return 0;
  17253. +}
  17254. +
  17255. +/**
  17256. + * This function hadles ADP Sense Interrupt
  17257. + */
  17258. +static int32_t dwc_otg_adp_handle_sns_intr(dwc_otg_core_if_t * core_if)
  17259. +{
  17260. + adpctl_data_t adpctl;
  17261. + /* Stop ADP Sense timer */
  17262. + DWC_TIMER_CANCEL(core_if->adp.sense_timer);
  17263. +
  17264. + /* Restart ADP Sense timer */
  17265. + dwc_otg_adp_sense_timer_start(core_if);
  17266. +
  17267. + /* Clear interrupt */
  17268. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  17269. + adpctl.b.adp_sns_int = 1;
  17270. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  17271. +
  17272. + return 0;
  17273. +}
  17274. +
  17275. +/**
  17276. + * This function handles ADP Probe Interrupts
  17277. + */
  17278. +static int32_t dwc_otg_adp_handle_prb_tmout_intr(dwc_otg_core_if_t * core_if,
  17279. + uint32_t val)
  17280. +{
  17281. + adpctl_data_t adpctl = {.d32 = 0 };
  17282. + adpctl.d32 = val;
  17283. + set_timer_value(core_if, adpctl.b.rtim);
  17284. +
  17285. + /* Clear interrupt */
  17286. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  17287. + adpctl.b.adp_tmout_int = 1;
  17288. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  17289. +
  17290. + return 0;
  17291. +}
  17292. +
  17293. +/**
  17294. + * ADP Interrupt handler.
  17295. + *
  17296. + */
  17297. +int32_t dwc_otg_adp_handle_intr(dwc_otg_core_if_t * core_if)
  17298. +{
  17299. + int retval = 0;
  17300. + adpctl_data_t adpctl = {.d32 = 0};
  17301. +
  17302. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  17303. + DWC_PRINTF("ADPCTL = %08x\n",adpctl.d32);
  17304. +
  17305. + if (adpctl.b.adp_sns_int & adpctl.b.adp_sns_int_msk) {
  17306. + DWC_PRINTF("ADP Sense interrupt\n");
  17307. + retval |= dwc_otg_adp_handle_sns_intr(core_if);
  17308. + }
  17309. + if (adpctl.b.adp_tmout_int & adpctl.b.adp_tmout_int_msk) {
  17310. + DWC_PRINTF("ADP timeout interrupt\n");
  17311. + retval |= dwc_otg_adp_handle_prb_tmout_intr(core_if, adpctl.d32);
  17312. + }
  17313. + if (adpctl.b.adp_prb_int & adpctl.b.adp_prb_int_msk) {
  17314. + DWC_PRINTF("ADP Probe interrupt\n");
  17315. + adpctl.b.adp_prb_int = 1;
  17316. + retval |= dwc_otg_adp_handle_prb_intr(core_if, adpctl.d32);
  17317. + }
  17318. +
  17319. +// dwc_otg_adp_modify_reg(core_if, adpctl.d32, 0);
  17320. + //dwc_otg_adp_write_reg(core_if, adpctl.d32);
  17321. + DWC_PRINTF("RETURN FROM ADP ISR\n");
  17322. +
  17323. + return retval;
  17324. +}
  17325. +
  17326. +/**
  17327. + *
  17328. + * @param core_if Programming view of DWC_otg controller.
  17329. + */
  17330. +int32_t dwc_otg_adp_handle_srp_intr(dwc_otg_core_if_t * core_if)
  17331. +{
  17332. +
  17333. +#ifndef DWC_HOST_ONLY
  17334. + hprt0_data_t hprt0;
  17335. + gpwrdn_data_t gpwrdn;
  17336. + DWC_DEBUGPL(DBG_ANY, "++ Power Down Logic Session Request Interrupt++\n");
  17337. +
  17338. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  17339. + /* check which value is for device mode and which for Host mode */
  17340. + if (!gpwrdn.b.idsts) { /* considered host mode value is 0 */
  17341. + DWC_PRINTF("SRP: Host mode\n");
  17342. +
  17343. + if (core_if->adp_enable) {
  17344. + dwc_otg_adp_probe_stop(core_if);
  17345. +
  17346. + /* Power on the core */
  17347. + if (core_if->power_down == 2) {
  17348. + gpwrdn.b.pwrdnswtch = 1;
  17349. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  17350. + gpwrdn, 0, gpwrdn.d32);
  17351. + }
  17352. +
  17353. + core_if->op_state = A_HOST;
  17354. + dwc_otg_core_init(core_if);
  17355. + dwc_otg_enable_global_interrupts(core_if);
  17356. + cil_hcd_start(core_if);
  17357. + }
  17358. +
  17359. + /* Turn on the port power bit. */
  17360. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  17361. + hprt0.b.prtpwr = 1;
  17362. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  17363. +
  17364. + /* Start the Connection timer. So a message can be displayed
  17365. + * if connect does not occur within 10 seconds. */
  17366. + cil_hcd_session_start(core_if);
  17367. + } else {
  17368. + DWC_PRINTF("SRP: Device mode %s\n", __FUNCTION__);
  17369. + if (core_if->adp_enable) {
  17370. + dwc_otg_adp_probe_stop(core_if);
  17371. +
  17372. + /* Power on the core */
  17373. + if (core_if->power_down == 2) {
  17374. + gpwrdn.b.pwrdnswtch = 1;
  17375. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  17376. + gpwrdn, 0, gpwrdn.d32);
  17377. + }
  17378. +
  17379. + gpwrdn.d32 = 0;
  17380. + gpwrdn.b.pmuactv = 0;
  17381. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
  17382. + gpwrdn.d32);
  17383. +
  17384. + core_if->op_state = B_PERIPHERAL;
  17385. + dwc_otg_core_init(core_if);
  17386. + dwc_otg_enable_global_interrupts(core_if);
  17387. + cil_pcd_start(core_if);
  17388. + }
  17389. + }
  17390. +#endif
  17391. + return 1;
  17392. +}
  17393. --- /dev/null
  17394. +++ b/drivers/usb/host/dwc_otg/dwc_otg_adp.h
  17395. @@ -0,0 +1,80 @@
  17396. +/* ==========================================================================
  17397. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_adp.h $
  17398. + * $Revision: #7 $
  17399. + * $Date: 2011/10/24 $
  17400. + * $Change: 1871159 $
  17401. + *
  17402. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  17403. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  17404. + * otherwise expressly agreed to in writing between Synopsys and you.
  17405. + *
  17406. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  17407. + * any End User Software License Agreement or Agreement for Licensed Product
  17408. + * with Synopsys or any supplement thereto. You are permitted to use and
  17409. + * redistribute this Software in source and binary forms, with or without
  17410. + * modification, provided that redistributions of source code must retain this
  17411. + * notice. You may not view, use, disclose, copy or distribute this file or
  17412. + * any information contained herein except pursuant to this license grant from
  17413. + * Synopsys. If you do not agree with this notice, including the disclaimer
  17414. + * below, then you are not authorized to use the Software.
  17415. + *
  17416. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  17417. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17418. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17419. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  17420. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  17421. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  17422. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  17423. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  17424. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  17425. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  17426. + * DAMAGE.
  17427. + * ========================================================================== */
  17428. +
  17429. +#ifndef __DWC_OTG_ADP_H__
  17430. +#define __DWC_OTG_ADP_H__
  17431. +
  17432. +/**
  17433. + * @file
  17434. + *
  17435. + * This file contains the Attach Detect Protocol interfaces and defines
  17436. + * (functions) and structures for Linux.
  17437. + *
  17438. + */
  17439. +
  17440. +#define DWC_OTG_ADP_UNATTACHED 0
  17441. +#define DWC_OTG_ADP_ATTACHED 1
  17442. +#define DWC_OTG_ADP_UNKOWN 2
  17443. +
  17444. +typedef struct dwc_otg_adp {
  17445. + uint32_t adp_started;
  17446. + uint32_t initial_probe;
  17447. + int32_t probe_timer_values[2];
  17448. + uint32_t probe_enabled;
  17449. + uint32_t sense_enabled;
  17450. + dwc_timer_t *sense_timer;
  17451. + uint32_t sense_timer_started;
  17452. + dwc_timer_t *vbuson_timer;
  17453. + uint32_t vbuson_timer_started;
  17454. + uint32_t attached;
  17455. + uint32_t probe_counter;
  17456. + uint32_t gpwrdn;
  17457. +} dwc_otg_adp_t;
  17458. +
  17459. +/**
  17460. + * Attach Detect Protocol functions
  17461. + */
  17462. +
  17463. +extern void dwc_otg_adp_write_reg(dwc_otg_core_if_t * core_if, uint32_t value);
  17464. +extern uint32_t dwc_otg_adp_read_reg(dwc_otg_core_if_t * core_if);
  17465. +extern uint32_t dwc_otg_adp_probe_start(dwc_otg_core_if_t * core_if);
  17466. +extern uint32_t dwc_otg_adp_sense_start(dwc_otg_core_if_t * core_if);
  17467. +extern uint32_t dwc_otg_adp_probe_stop(dwc_otg_core_if_t * core_if);
  17468. +extern uint32_t dwc_otg_adp_sense_stop(dwc_otg_core_if_t * core_if);
  17469. +extern void dwc_otg_adp_start(dwc_otg_core_if_t * core_if, uint8_t is_host);
  17470. +extern void dwc_otg_adp_init(dwc_otg_core_if_t * core_if);
  17471. +extern void dwc_otg_adp_remove(dwc_otg_core_if_t * core_if);
  17472. +extern int32_t dwc_otg_adp_handle_intr(dwc_otg_core_if_t * core_if);
  17473. +extern int32_t dwc_otg_adp_handle_srp_intr(dwc_otg_core_if_t * core_if);
  17474. +
  17475. +#endif //__DWC_OTG_ADP_H__
  17476. --- /dev/null
  17477. +++ b/drivers/usb/host/dwc_otg/dwc_otg_attr.c
  17478. @@ -0,0 +1,1212 @@
  17479. +/* ==========================================================================
  17480. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_attr.c $
  17481. + * $Revision: #44 $
  17482. + * $Date: 2010/11/29 $
  17483. + * $Change: 1636033 $
  17484. + *
  17485. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  17486. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  17487. + * otherwise expressly agreed to in writing between Synopsys and you.
  17488. + *
  17489. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  17490. + * any End User Software License Agreement or Agreement for Licensed Product
  17491. + * with Synopsys or any supplement thereto. You are permitted to use and
  17492. + * redistribute this Software in source and binary forms, with or without
  17493. + * modification, provided that redistributions of source code must retain this
  17494. + * notice. You may not view, use, disclose, copy or distribute this file or
  17495. + * any information contained herein except pursuant to this license grant from
  17496. + * Synopsys. If you do not agree with this notice, including the disclaimer
  17497. + * below, then you are not authorized to use the Software.
  17498. + *
  17499. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  17500. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17501. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17502. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  17503. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  17504. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  17505. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  17506. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  17507. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  17508. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  17509. + * DAMAGE.
  17510. + * ========================================================================== */
  17511. +
  17512. +/** @file
  17513. + *
  17514. + * The diagnostic interface will provide access to the controller for
  17515. + * bringing up the hardware and testing. The Linux driver attributes
  17516. + * feature will be used to provide the Linux Diagnostic
  17517. + * Interface. These attributes are accessed through sysfs.
  17518. + */
  17519. +
  17520. +/** @page "Linux Module Attributes"
  17521. + *
  17522. + * The Linux module attributes feature is used to provide the Linux
  17523. + * Diagnostic Interface. These attributes are accessed through sysfs.
  17524. + * The diagnostic interface will provide access to the controller for
  17525. + * bringing up the hardware and testing.
  17526. +
  17527. + The following table shows the attributes.
  17528. + <table>
  17529. + <tr>
  17530. + <td><b> Name</b></td>
  17531. + <td><b> Description</b></td>
  17532. + <td><b> Access</b></td>
  17533. + </tr>
  17534. +
  17535. + <tr>
  17536. + <td> mode </td>
  17537. + <td> Returns the current mode: 0 for device mode, 1 for host mode</td>
  17538. + <td> Read</td>
  17539. + </tr>
  17540. +
  17541. + <tr>
  17542. + <td> hnpcapable </td>
  17543. + <td> Gets or sets the "HNP-capable" bit in the Core USB Configuraton Register.
  17544. + Read returns the current value.</td>
  17545. + <td> Read/Write</td>
  17546. + </tr>
  17547. +
  17548. + <tr>
  17549. + <td> srpcapable </td>
  17550. + <td> Gets or sets the "SRP-capable" bit in the Core USB Configuraton Register.
  17551. + Read returns the current value.</td>
  17552. + <td> Read/Write</td>
  17553. + </tr>
  17554. +
  17555. + <tr>
  17556. + <td> hsic_connect </td>
  17557. + <td> Gets or sets the "HSIC-Connect" bit in the GLPMCFG Register.
  17558. + Read returns the current value.</td>
  17559. + <td> Read/Write</td>
  17560. + </tr>
  17561. +
  17562. + <tr>
  17563. + <td> inv_sel_hsic </td>
  17564. + <td> Gets or sets the "Invert Select HSIC" bit in the GLPMFG Register.
  17565. + Read returns the current value.</td>
  17566. + <td> Read/Write</td>
  17567. + </tr>
  17568. +
  17569. + <tr>
  17570. + <td> hnp </td>
  17571. + <td> Initiates the Host Negotiation Protocol. Read returns the status.</td>
  17572. + <td> Read/Write</td>
  17573. + </tr>
  17574. +
  17575. + <tr>
  17576. + <td> srp </td>
  17577. + <td> Initiates the Session Request Protocol. Read returns the status.</td>
  17578. + <td> Read/Write</td>
  17579. + </tr>
  17580. +
  17581. + <tr>
  17582. + <td> buspower </td>
  17583. + <td> Gets or sets the Power State of the bus (0 - Off or 1 - On)</td>
  17584. + <td> Read/Write</td>
  17585. + </tr>
  17586. +
  17587. + <tr>
  17588. + <td> bussuspend </td>
  17589. + <td> Suspends the USB bus.</td>
  17590. + <td> Read/Write</td>
  17591. + </tr>
  17592. +
  17593. + <tr>
  17594. + <td> busconnected </td>
  17595. + <td> Gets the connection status of the bus</td>
  17596. + <td> Read</td>
  17597. + </tr>
  17598. +
  17599. + <tr>
  17600. + <td> gotgctl </td>
  17601. + <td> Gets or sets the Core Control Status Register.</td>
  17602. + <td> Read/Write</td>
  17603. + </tr>
  17604. +
  17605. + <tr>
  17606. + <td> gusbcfg </td>
  17607. + <td> Gets or sets the Core USB Configuration Register</td>
  17608. + <td> Read/Write</td>
  17609. + </tr>
  17610. +
  17611. + <tr>
  17612. + <td> grxfsiz </td>
  17613. + <td> Gets or sets the Receive FIFO Size Register</td>
  17614. + <td> Read/Write</td>
  17615. + </tr>
  17616. +
  17617. + <tr>
  17618. + <td> gnptxfsiz </td>
  17619. + <td> Gets or sets the non-periodic Transmit Size Register</td>
  17620. + <td> Read/Write</td>
  17621. + </tr>
  17622. +
  17623. + <tr>
  17624. + <td> gpvndctl </td>
  17625. + <td> Gets or sets the PHY Vendor Control Register</td>
  17626. + <td> Read/Write</td>
  17627. + </tr>
  17628. +
  17629. + <tr>
  17630. + <td> ggpio </td>
  17631. + <td> Gets the value in the lower 16-bits of the General Purpose IO Register
  17632. + or sets the upper 16 bits.</td>
  17633. + <td> Read/Write</td>
  17634. + </tr>
  17635. +
  17636. + <tr>
  17637. + <td> guid </td>
  17638. + <td> Gets or sets the value of the User ID Register</td>
  17639. + <td> Read/Write</td>
  17640. + </tr>
  17641. +
  17642. + <tr>
  17643. + <td> gsnpsid </td>
  17644. + <td> Gets the value of the Synopsys ID Regester</td>
  17645. + <td> Read</td>
  17646. + </tr>
  17647. +
  17648. + <tr>
  17649. + <td> devspeed </td>
  17650. + <td> Gets or sets the device speed setting in the DCFG register</td>
  17651. + <td> Read/Write</td>
  17652. + </tr>
  17653. +
  17654. + <tr>
  17655. + <td> enumspeed </td>
  17656. + <td> Gets the device enumeration Speed.</td>
  17657. + <td> Read</td>
  17658. + </tr>
  17659. +
  17660. + <tr>
  17661. + <td> hptxfsiz </td>
  17662. + <td> Gets the value of the Host Periodic Transmit FIFO</td>
  17663. + <td> Read</td>
  17664. + </tr>
  17665. +
  17666. + <tr>
  17667. + <td> hprt0 </td>
  17668. + <td> Gets or sets the value in the Host Port Control and Status Register</td>
  17669. + <td> Read/Write</td>
  17670. + </tr>
  17671. +
  17672. + <tr>
  17673. + <td> regoffset </td>
  17674. + <td> Sets the register offset for the next Register Access</td>
  17675. + <td> Read/Write</td>
  17676. + </tr>
  17677. +
  17678. + <tr>
  17679. + <td> regvalue </td>
  17680. + <td> Gets or sets the value of the register at the offset in the regoffset attribute.</td>
  17681. + <td> Read/Write</td>
  17682. + </tr>
  17683. +
  17684. + <tr>
  17685. + <td> remote_wakeup </td>
  17686. + <td> On read, shows the status of Remote Wakeup. On write, initiates a remote
  17687. + wakeup of the host. When bit 0 is 1 and Remote Wakeup is enabled, the Remote
  17688. + Wakeup signalling bit in the Device Control Register is set for 1
  17689. + milli-second.</td>
  17690. + <td> Read/Write</td>
  17691. + </tr>
  17692. +
  17693. + <tr>
  17694. + <td> rem_wakeup_pwrdn </td>
  17695. + <td> On read, shows the status core - hibernated or not. On write, initiates
  17696. + a remote wakeup of the device from Hibernation. </td>
  17697. + <td> Read/Write</td>
  17698. + </tr>
  17699. +
  17700. + <tr>
  17701. + <td> mode_ch_tim_en </td>
  17702. + <td> This bit is used to enable or disable the host core to wait for 200 PHY
  17703. + clock cycles at the end of Resume to change the opmode signal to the PHY to 00
  17704. + after Suspend or LPM. </td>
  17705. + <td> Read/Write</td>
  17706. + </tr>
  17707. +
  17708. + <tr>
  17709. + <td> fr_interval </td>
  17710. + <td> On read, shows the value of HFIR Frame Interval. On write, dynamically
  17711. + reload HFIR register during runtime. The application can write a value to this
  17712. + register only after the Port Enable bit of the Host Port Control and Status
  17713. + register (HPRT.PrtEnaPort) has been set </td>
  17714. + <td> Read/Write</td>
  17715. + </tr>
  17716. +
  17717. + <tr>
  17718. + <td> disconnect_us </td>
  17719. + <td> On read, shows the status of disconnect_device_us. On write, sets disconnect_us
  17720. + which causes soft disconnect for 100us. Applicable only for device mode of operation.</td>
  17721. + <td> Read/Write</td>
  17722. + </tr>
  17723. +
  17724. + <tr>
  17725. + <td> regdump </td>
  17726. + <td> Dumps the contents of core registers.</td>
  17727. + <td> Read</td>
  17728. + </tr>
  17729. +
  17730. + <tr>
  17731. + <td> spramdump </td>
  17732. + <td> Dumps the contents of core registers.</td>
  17733. + <td> Read</td>
  17734. + </tr>
  17735. +
  17736. + <tr>
  17737. + <td> hcddump </td>
  17738. + <td> Dumps the current HCD state.</td>
  17739. + <td> Read</td>
  17740. + </tr>
  17741. +
  17742. + <tr>
  17743. + <td> hcd_frrem </td>
  17744. + <td> Shows the average value of the Frame Remaining
  17745. + field in the Host Frame Number/Frame Remaining register when an SOF interrupt
  17746. + occurs. This can be used to determine the average interrupt latency. Also
  17747. + shows the average Frame Remaining value for start_transfer and the "a" and
  17748. + "b" sample points. The "a" and "b" sample points may be used during debugging
  17749. + bto determine how long it takes to execute a section of the HCD code.</td>
  17750. + <td> Read</td>
  17751. + </tr>
  17752. +
  17753. + <tr>
  17754. + <td> rd_reg_test </td>
  17755. + <td> Displays the time required to read the GNPTXFSIZ register many times
  17756. + (the output shows the number of times the register is read).
  17757. + <td> Read</td>
  17758. + </tr>
  17759. +
  17760. + <tr>
  17761. + <td> wr_reg_test </td>
  17762. + <td> Displays the time required to write the GNPTXFSIZ register many times
  17763. + (the output shows the number of times the register is written).
  17764. + <td> Read</td>
  17765. + </tr>
  17766. +
  17767. + <tr>
  17768. + <td> lpm_response </td>
  17769. + <td> Gets or sets lpm_response mode. Applicable only in device mode.
  17770. + <td> Write</td>
  17771. + </tr>
  17772. +
  17773. + <tr>
  17774. + <td> sleep_status </td>
  17775. + <td> Shows sleep status of device.
  17776. + <td> Read</td>
  17777. + </tr>
  17778. +
  17779. + </table>
  17780. +
  17781. + Example usage:
  17782. + To get the current mode:
  17783. + cat /sys/devices/lm0/mode
  17784. +
  17785. + To power down the USB:
  17786. + echo 0 > /sys/devices/lm0/buspower
  17787. + */
  17788. +
  17789. +#include "dwc_otg_os_dep.h"
  17790. +#include "dwc_os.h"
  17791. +#include "dwc_otg_driver.h"
  17792. +#include "dwc_otg_attr.h"
  17793. +#include "dwc_otg_core_if.h"
  17794. +#include "dwc_otg_pcd_if.h"
  17795. +#include "dwc_otg_hcd_if.h"
  17796. +
  17797. +/*
  17798. + * MACROs for defining sysfs attribute
  17799. + */
  17800. +#ifdef LM_INTERFACE
  17801. +
  17802. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
  17803. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  17804. +{ \
  17805. + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
  17806. + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
  17807. + uint32_t val; \
  17808. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  17809. + return sprintf (buf, "%s = 0x%x\n", _string_, val); \
  17810. +}
  17811. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
  17812. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  17813. + const char *buf, size_t count) \
  17814. +{ \
  17815. + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
  17816. + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
  17817. + uint32_t set = simple_strtoul(buf, NULL, 16); \
  17818. + dwc_otg_set_##_otg_attr_name_(otg_dev->core_if, set);\
  17819. + return count; \
  17820. +}
  17821. +
  17822. +#elif defined(PCI_INTERFACE)
  17823. +
  17824. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
  17825. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  17826. +{ \
  17827. + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
  17828. + uint32_t val; \
  17829. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  17830. + return sprintf (buf, "%s = 0x%x\n", _string_, val); \
  17831. +}
  17832. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
  17833. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  17834. + const char *buf, size_t count) \
  17835. +{ \
  17836. + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
  17837. + uint32_t set = simple_strtoul(buf, NULL, 16); \
  17838. + dwc_otg_set_##_otg_attr_name_(otg_dev->core_if, set);\
  17839. + return count; \
  17840. +}
  17841. +
  17842. +#elif defined(PLATFORM_INTERFACE)
  17843. +
  17844. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
  17845. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  17846. +{ \
  17847. + struct platform_device *platform_dev = \
  17848. + container_of(_dev, struct platform_device, dev); \
  17849. + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
  17850. + uint32_t val; \
  17851. + DWC_PRINTF("%s(%p) -> platform_dev %p, otg_dev %p\n", \
  17852. + __func__, _dev, platform_dev, otg_dev); \
  17853. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  17854. + return sprintf (buf, "%s = 0x%x\n", _string_, val); \
  17855. +}
  17856. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
  17857. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  17858. + const char *buf, size_t count) \
  17859. +{ \
  17860. + struct platform_device *platform_dev = container_of(_dev, struct platform_device, dev); \
  17861. + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
  17862. + uint32_t set = simple_strtoul(buf, NULL, 16); \
  17863. + dwc_otg_set_##_otg_attr_name_(otg_dev->core_if, set);\
  17864. + return count; \
  17865. +}
  17866. +#endif
  17867. +
  17868. +/*
  17869. + * MACROs for defining sysfs attribute for 32-bit registers
  17870. + */
  17871. +#ifdef LM_INTERFACE
  17872. +#define DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
  17873. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  17874. +{ \
  17875. + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
  17876. + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
  17877. + uint32_t val; \
  17878. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  17879. + return sprintf (buf, "%s = 0x%08x\n", _string_, val); \
  17880. +}
  17881. +#define DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_string_) \
  17882. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  17883. + const char *buf, size_t count) \
  17884. +{ \
  17885. + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
  17886. + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
  17887. + uint32_t val = simple_strtoul(buf, NULL, 16); \
  17888. + dwc_otg_set_##_otg_attr_name_ (otg_dev->core_if, val); \
  17889. + return count; \
  17890. +}
  17891. +#elif defined(PCI_INTERFACE)
  17892. +#define DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
  17893. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  17894. +{ \
  17895. + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
  17896. + uint32_t val; \
  17897. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  17898. + return sprintf (buf, "%s = 0x%08x\n", _string_, val); \
  17899. +}
  17900. +#define DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_string_) \
  17901. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  17902. + const char *buf, size_t count) \
  17903. +{ \
  17904. + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
  17905. + uint32_t val = simple_strtoul(buf, NULL, 16); \
  17906. + dwc_otg_set_##_otg_attr_name_ (otg_dev->core_if, val); \
  17907. + return count; \
  17908. +}
  17909. +
  17910. +#elif defined(PLATFORM_INTERFACE)
  17911. +#include "dwc_otg_dbg.h"
  17912. +#define DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
  17913. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  17914. +{ \
  17915. + struct platform_device *platform_dev = container_of(_dev, struct platform_device, dev); \
  17916. + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
  17917. + uint32_t val; \
  17918. + DWC_PRINTF("%s(%p) -> platform_dev %p, otg_dev %p\n", \
  17919. + __func__, _dev, platform_dev, otg_dev); \
  17920. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  17921. + return sprintf (buf, "%s = 0x%08x\n", _string_, val); \
  17922. +}
  17923. +#define DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_string_) \
  17924. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  17925. + const char *buf, size_t count) \
  17926. +{ \
  17927. + struct platform_device *platform_dev = container_of(_dev, struct platform_device, dev); \
  17928. + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
  17929. + uint32_t val = simple_strtoul(buf, NULL, 16); \
  17930. + dwc_otg_set_##_otg_attr_name_ (otg_dev->core_if, val); \
  17931. + return count; \
  17932. +}
  17933. +
  17934. +#endif
  17935. +
  17936. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_RW(_otg_attr_name_,_string_) \
  17937. +DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
  17938. +DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
  17939. +DEVICE_ATTR(_otg_attr_name_,0644,_otg_attr_name_##_show,_otg_attr_name_##_store);
  17940. +
  17941. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_RO(_otg_attr_name_,_string_) \
  17942. +DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
  17943. +DEVICE_ATTR(_otg_attr_name_,0444,_otg_attr_name_##_show,NULL);
  17944. +
  17945. +#define DWC_OTG_DEVICE_ATTR_REG32_RW(_otg_attr_name_,_addr_,_string_) \
  17946. +DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
  17947. +DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_string_) \
  17948. +DEVICE_ATTR(_otg_attr_name_,0644,_otg_attr_name_##_show,_otg_attr_name_##_store);
  17949. +
  17950. +#define DWC_OTG_DEVICE_ATTR_REG32_RO(_otg_attr_name_,_addr_,_string_) \
  17951. +DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
  17952. +DEVICE_ATTR(_otg_attr_name_,0444,_otg_attr_name_##_show,NULL);
  17953. +
  17954. +/** @name Functions for Show/Store of Attributes */
  17955. +/**@{*/
  17956. +
  17957. +/**
  17958. + * Helper function returning the otg_device structure of the given device
  17959. + */
  17960. +static dwc_otg_device_t *dwc_otg_drvdev(struct device *_dev)
  17961. +{
  17962. + dwc_otg_device_t *otg_dev;
  17963. + DWC_OTG_GETDRVDEV(otg_dev, _dev);
  17964. + return otg_dev;
  17965. +}
  17966. +
  17967. +/**
  17968. + * Show the register offset of the Register Access.
  17969. + */
  17970. +static ssize_t regoffset_show(struct device *_dev,
  17971. + struct device_attribute *attr, char *buf)
  17972. +{
  17973. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  17974. + return snprintf(buf, sizeof("0xFFFFFFFF\n") + 1, "0x%08x\n",
  17975. + otg_dev->os_dep.reg_offset);
  17976. +}
  17977. +
  17978. +/**
  17979. + * Set the register offset for the next Register Access Read/Write
  17980. + */
  17981. +static ssize_t regoffset_store(struct device *_dev,
  17982. + struct device_attribute *attr,
  17983. + const char *buf, size_t count)
  17984. +{
  17985. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  17986. + uint32_t offset = simple_strtoul(buf, NULL, 16);
  17987. +#if defined(LM_INTERFACE) || defined(PLATFORM_INTERFACE)
  17988. + if (offset < SZ_256K) {
  17989. +#elif defined(PCI_INTERFACE)
  17990. + if (offset < 0x00040000) {
  17991. +#endif
  17992. + otg_dev->os_dep.reg_offset = offset;
  17993. + } else {
  17994. + dev_err(_dev, "invalid offset\n");
  17995. + }
  17996. +
  17997. + return count;
  17998. +}
  17999. +
  18000. +DEVICE_ATTR(regoffset, S_IRUGO | S_IWUSR, regoffset_show, regoffset_store);
  18001. +
  18002. +/**
  18003. + * Show the value of the register at the offset in the reg_offset
  18004. + * attribute.
  18005. + */
  18006. +static ssize_t regvalue_show(struct device *_dev,
  18007. + struct device_attribute *attr, char *buf)
  18008. +{
  18009. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18010. + uint32_t val;
  18011. + volatile uint32_t *addr;
  18012. +
  18013. + if (otg_dev->os_dep.reg_offset != 0xFFFFFFFF && 0 != otg_dev->os_dep.base) {
  18014. + /* Calculate the address */
  18015. + addr = (uint32_t *) (otg_dev->os_dep.reg_offset +
  18016. + (uint8_t *) otg_dev->os_dep.base);
  18017. + val = DWC_READ_REG32(addr);
  18018. + return snprintf(buf,
  18019. + sizeof("Reg@0xFFFFFFFF = 0xFFFFFFFF\n") + 1,
  18020. + "Reg@0x%06x = 0x%08x\n", otg_dev->os_dep.reg_offset,
  18021. + val);
  18022. + } else {
  18023. + dev_err(_dev, "Invalid offset (0x%0x)\n", otg_dev->os_dep.reg_offset);
  18024. + return sprintf(buf, "invalid offset\n");
  18025. + }
  18026. +}
  18027. +
  18028. +/**
  18029. + * Store the value in the register at the offset in the reg_offset
  18030. + * attribute.
  18031. + *
  18032. + */
  18033. +static ssize_t regvalue_store(struct device *_dev,
  18034. + struct device_attribute *attr,
  18035. + const char *buf, size_t count)
  18036. +{
  18037. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18038. + volatile uint32_t *addr;
  18039. + uint32_t val = simple_strtoul(buf, NULL, 16);
  18040. + //dev_dbg(_dev, "Offset=0x%08x Val=0x%08x\n", otg_dev->reg_offset, val);
  18041. + if (otg_dev->os_dep.reg_offset != 0xFFFFFFFF && 0 != otg_dev->os_dep.base) {
  18042. + /* Calculate the address */
  18043. + addr = (uint32_t *) (otg_dev->os_dep.reg_offset +
  18044. + (uint8_t *) otg_dev->os_dep.base);
  18045. + DWC_WRITE_REG32(addr, val);
  18046. + } else {
  18047. + dev_err(_dev, "Invalid Register Offset (0x%08x)\n",
  18048. + otg_dev->os_dep.reg_offset);
  18049. + }
  18050. + return count;
  18051. +}
  18052. +
  18053. +DEVICE_ATTR(regvalue, S_IRUGO | S_IWUSR, regvalue_show, regvalue_store);
  18054. +
  18055. +/*
  18056. + * Attributes
  18057. + */
  18058. +DWC_OTG_DEVICE_ATTR_BITFIELD_RO(mode, "Mode");
  18059. +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(hnpcapable, "HNPCapable");
  18060. +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(srpcapable, "SRPCapable");
  18061. +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(hsic_connect, "HSIC Connect");
  18062. +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(inv_sel_hsic, "Invert Select HSIC");
  18063. +
  18064. +//DWC_OTG_DEVICE_ATTR_BITFIELD_RW(buspower,&(otg_dev->core_if->core_global_regs->gotgctl),(1<<8),8,"Mode");
  18065. +//DWC_OTG_DEVICE_ATTR_BITFIELD_RW(bussuspend,&(otg_dev->core_if->core_global_regs->gotgctl),(1<<8),8,"Mode");
  18066. +DWC_OTG_DEVICE_ATTR_BITFIELD_RO(busconnected, "Bus Connected");
  18067. +
  18068. +DWC_OTG_DEVICE_ATTR_REG32_RW(gotgctl, 0, "GOTGCTL");
  18069. +DWC_OTG_DEVICE_ATTR_REG32_RW(gusbcfg,
  18070. + &(otg_dev->core_if->core_global_regs->gusbcfg),
  18071. + "GUSBCFG");
  18072. +DWC_OTG_DEVICE_ATTR_REG32_RW(grxfsiz,
  18073. + &(otg_dev->core_if->core_global_regs->grxfsiz),
  18074. + "GRXFSIZ");
  18075. +DWC_OTG_DEVICE_ATTR_REG32_RW(gnptxfsiz,
  18076. + &(otg_dev->core_if->core_global_regs->gnptxfsiz),
  18077. + "GNPTXFSIZ");
  18078. +DWC_OTG_DEVICE_ATTR_REG32_RW(gpvndctl,
  18079. + &(otg_dev->core_if->core_global_regs->gpvndctl),
  18080. + "GPVNDCTL");
  18081. +DWC_OTG_DEVICE_ATTR_REG32_RW(ggpio,
  18082. + &(otg_dev->core_if->core_global_regs->ggpio),
  18083. + "GGPIO");
  18084. +DWC_OTG_DEVICE_ATTR_REG32_RW(guid, &(otg_dev->core_if->core_global_regs->guid),
  18085. + "GUID");
  18086. +DWC_OTG_DEVICE_ATTR_REG32_RO(gsnpsid,
  18087. + &(otg_dev->core_if->core_global_regs->gsnpsid),
  18088. + "GSNPSID");
  18089. +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(devspeed, "Device Speed");
  18090. +DWC_OTG_DEVICE_ATTR_BITFIELD_RO(enumspeed, "Device Enumeration Speed");
  18091. +
  18092. +DWC_OTG_DEVICE_ATTR_REG32_RO(hptxfsiz,
  18093. + &(otg_dev->core_if->core_global_regs->hptxfsiz),
  18094. + "HPTXFSIZ");
  18095. +DWC_OTG_DEVICE_ATTR_REG32_RW(hprt0, otg_dev->core_if->host_if->hprt0, "HPRT0");
  18096. +
  18097. +/**
  18098. + * @todo Add code to initiate the HNP.
  18099. + */
  18100. +/**
  18101. + * Show the HNP status bit
  18102. + */
  18103. +static ssize_t hnp_show(struct device *_dev,
  18104. + struct device_attribute *attr, char *buf)
  18105. +{
  18106. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18107. + return sprintf(buf, "HstNegScs = 0x%x\n",
  18108. + dwc_otg_get_hnpstatus(otg_dev->core_if));
  18109. +}
  18110. +
  18111. +/**
  18112. + * Set the HNP Request bit
  18113. + */
  18114. +static ssize_t hnp_store(struct device *_dev,
  18115. + struct device_attribute *attr,
  18116. + const char *buf, size_t count)
  18117. +{
  18118. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18119. + uint32_t in = simple_strtoul(buf, NULL, 16);
  18120. + dwc_otg_set_hnpreq(otg_dev->core_if, in);
  18121. + return count;
  18122. +}
  18123. +
  18124. +DEVICE_ATTR(hnp, 0644, hnp_show, hnp_store);
  18125. +
  18126. +/**
  18127. + * @todo Add code to initiate the SRP.
  18128. + */
  18129. +/**
  18130. + * Show the SRP status bit
  18131. + */
  18132. +static ssize_t srp_show(struct device *_dev,
  18133. + struct device_attribute *attr, char *buf)
  18134. +{
  18135. +#ifndef DWC_HOST_ONLY
  18136. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18137. + return sprintf(buf, "SesReqScs = 0x%x\n",
  18138. + dwc_otg_get_srpstatus(otg_dev->core_if));
  18139. +#else
  18140. + return sprintf(buf, "Host Only Mode!\n");
  18141. +#endif
  18142. +}
  18143. +
  18144. +/**
  18145. + * Set the SRP Request bit
  18146. + */
  18147. +static ssize_t srp_store(struct device *_dev,
  18148. + struct device_attribute *attr,
  18149. + const char *buf, size_t count)
  18150. +{
  18151. +#ifndef DWC_HOST_ONLY
  18152. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18153. + dwc_otg_pcd_initiate_srp(otg_dev->pcd);
  18154. +#endif
  18155. + return count;
  18156. +}
  18157. +
  18158. +DEVICE_ATTR(srp, 0644, srp_show, srp_store);
  18159. +
  18160. +/**
  18161. + * @todo Need to do more for power on/off?
  18162. + */
  18163. +/**
  18164. + * Show the Bus Power status
  18165. + */
  18166. +static ssize_t buspower_show(struct device *_dev,
  18167. + struct device_attribute *attr, char *buf)
  18168. +{
  18169. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18170. + return sprintf(buf, "Bus Power = 0x%x\n",
  18171. + dwc_otg_get_prtpower(otg_dev->core_if));
  18172. +}
  18173. +
  18174. +/**
  18175. + * Set the Bus Power status
  18176. + */
  18177. +static ssize_t buspower_store(struct device *_dev,
  18178. + struct device_attribute *attr,
  18179. + const char *buf, size_t count)
  18180. +{
  18181. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18182. + uint32_t on = simple_strtoul(buf, NULL, 16);
  18183. + dwc_otg_set_prtpower(otg_dev->core_if, on);
  18184. + return count;
  18185. +}
  18186. +
  18187. +DEVICE_ATTR(buspower, 0644, buspower_show, buspower_store);
  18188. +
  18189. +/**
  18190. + * @todo Need to do more for suspend?
  18191. + */
  18192. +/**
  18193. + * Show the Bus Suspend status
  18194. + */
  18195. +static ssize_t bussuspend_show(struct device *_dev,
  18196. + struct device_attribute *attr, char *buf)
  18197. +{
  18198. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18199. + return sprintf(buf, "Bus Suspend = 0x%x\n",
  18200. + dwc_otg_get_prtsuspend(otg_dev->core_if));
  18201. +}
  18202. +
  18203. +/**
  18204. + * Set the Bus Suspend status
  18205. + */
  18206. +static ssize_t bussuspend_store(struct device *_dev,
  18207. + struct device_attribute *attr,
  18208. + const char *buf, size_t count)
  18209. +{
  18210. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18211. + uint32_t in = simple_strtoul(buf, NULL, 16);
  18212. + dwc_otg_set_prtsuspend(otg_dev->core_if, in);
  18213. + return count;
  18214. +}
  18215. +
  18216. +DEVICE_ATTR(bussuspend, 0644, bussuspend_show, bussuspend_store);
  18217. +
  18218. +/**
  18219. + * Show the Mode Change Ready Timer status
  18220. + */
  18221. +static ssize_t mode_ch_tim_en_show(struct device *_dev,
  18222. + struct device_attribute *attr, char *buf)
  18223. +{
  18224. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18225. + return sprintf(buf, "Mode Change Ready Timer Enable = 0x%x\n",
  18226. + dwc_otg_get_mode_ch_tim(otg_dev->core_if));
  18227. +}
  18228. +
  18229. +/**
  18230. + * Set the Mode Change Ready Timer status
  18231. + */
  18232. +static ssize_t mode_ch_tim_en_store(struct device *_dev,
  18233. + struct device_attribute *attr,
  18234. + const char *buf, size_t count)
  18235. +{
  18236. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18237. + uint32_t in = simple_strtoul(buf, NULL, 16);
  18238. + dwc_otg_set_mode_ch_tim(otg_dev->core_if, in);
  18239. + return count;
  18240. +}
  18241. +
  18242. +DEVICE_ATTR(mode_ch_tim_en, 0644, mode_ch_tim_en_show, mode_ch_tim_en_store);
  18243. +
  18244. +/**
  18245. + * Show the value of HFIR Frame Interval bitfield
  18246. + */
  18247. +static ssize_t fr_interval_show(struct device *_dev,
  18248. + struct device_attribute *attr, char *buf)
  18249. +{
  18250. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18251. + return sprintf(buf, "Frame Interval = 0x%x\n",
  18252. + dwc_otg_get_fr_interval(otg_dev->core_if));
  18253. +}
  18254. +
  18255. +/**
  18256. + * Set the HFIR Frame Interval value
  18257. + */
  18258. +static ssize_t fr_interval_store(struct device *_dev,
  18259. + struct device_attribute *attr,
  18260. + const char *buf, size_t count)
  18261. +{
  18262. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18263. + uint32_t in = simple_strtoul(buf, NULL, 10);
  18264. + dwc_otg_set_fr_interval(otg_dev->core_if, in);
  18265. + return count;
  18266. +}
  18267. +
  18268. +DEVICE_ATTR(fr_interval, 0644, fr_interval_show, fr_interval_store);
  18269. +
  18270. +/**
  18271. + * Show the status of Remote Wakeup.
  18272. + */
  18273. +static ssize_t remote_wakeup_show(struct device *_dev,
  18274. + struct device_attribute *attr, char *buf)
  18275. +{
  18276. +#ifndef DWC_HOST_ONLY
  18277. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18278. +
  18279. + return sprintf(buf,
  18280. + "Remote Wakeup Sig = %d Enabled = %d LPM Remote Wakeup = %d\n",
  18281. + dwc_otg_get_remotewakesig(otg_dev->core_if),
  18282. + dwc_otg_pcd_get_rmwkup_enable(otg_dev->pcd),
  18283. + dwc_otg_get_lpm_remotewakeenabled(otg_dev->core_if));
  18284. +#else
  18285. + return sprintf(buf, "Host Only Mode!\n");
  18286. +#endif /* DWC_HOST_ONLY */
  18287. +}
  18288. +
  18289. +/**
  18290. + * Initiate a remote wakeup of the host. The Device control register
  18291. + * Remote Wakeup Signal bit is written if the PCD Remote wakeup enable
  18292. + * flag is set.
  18293. + *
  18294. + */
  18295. +static ssize_t remote_wakeup_store(struct device *_dev,
  18296. + struct device_attribute *attr,
  18297. + const char *buf, size_t count)
  18298. +{
  18299. +#ifndef DWC_HOST_ONLY
  18300. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18301. + uint32_t val = simple_strtoul(buf, NULL, 16);
  18302. +
  18303. + if (val & 1) {
  18304. + dwc_otg_pcd_remote_wakeup(otg_dev->pcd, 1);
  18305. + } else {
  18306. + dwc_otg_pcd_remote_wakeup(otg_dev->pcd, 0);
  18307. + }
  18308. +#endif /* DWC_HOST_ONLY */
  18309. + return count;
  18310. +}
  18311. +
  18312. +DEVICE_ATTR(remote_wakeup, S_IRUGO | S_IWUSR, remote_wakeup_show,
  18313. + remote_wakeup_store);
  18314. +
  18315. +/**
  18316. + * Show the whether core is hibernated or not.
  18317. + */
  18318. +static ssize_t rem_wakeup_pwrdn_show(struct device *_dev,
  18319. + struct device_attribute *attr, char *buf)
  18320. +{
  18321. +#ifndef DWC_HOST_ONLY
  18322. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18323. +
  18324. + if (dwc_otg_get_core_state(otg_dev->core_if)) {
  18325. + DWC_PRINTF("Core is in hibernation\n");
  18326. + } else {
  18327. + DWC_PRINTF("Core is not in hibernation\n");
  18328. + }
  18329. +#endif /* DWC_HOST_ONLY */
  18330. + return 0;
  18331. +}
  18332. +
  18333. +extern int dwc_otg_device_hibernation_restore(dwc_otg_core_if_t * core_if,
  18334. + int rem_wakeup, int reset);
  18335. +
  18336. +/**
  18337. + * Initiate a remote wakeup of the device to exit from hibernation.
  18338. + */
  18339. +static ssize_t rem_wakeup_pwrdn_store(struct device *_dev,
  18340. + struct device_attribute *attr,
  18341. + const char *buf, size_t count)
  18342. +{
  18343. +#ifndef DWC_HOST_ONLY
  18344. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18345. + dwc_otg_device_hibernation_restore(otg_dev->core_if, 1, 0);
  18346. +#endif
  18347. + return count;
  18348. +}
  18349. +
  18350. +DEVICE_ATTR(rem_wakeup_pwrdn, S_IRUGO | S_IWUSR, rem_wakeup_pwrdn_show,
  18351. + rem_wakeup_pwrdn_store);
  18352. +
  18353. +static ssize_t disconnect_us(struct device *_dev,
  18354. + struct device_attribute *attr,
  18355. + const char *buf, size_t count)
  18356. +{
  18357. +
  18358. +#ifndef DWC_HOST_ONLY
  18359. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18360. + uint32_t val = simple_strtoul(buf, NULL, 16);
  18361. + DWC_PRINTF("The Passed value is %04x\n", val);
  18362. +
  18363. + dwc_otg_pcd_disconnect_us(otg_dev->pcd, 50);
  18364. +
  18365. +#endif /* DWC_HOST_ONLY */
  18366. + return count;
  18367. +}
  18368. +
  18369. +DEVICE_ATTR(disconnect_us, S_IWUSR, 0, disconnect_us);
  18370. +
  18371. +/**
  18372. + * Dump global registers and either host or device registers (depending on the
  18373. + * current mode of the core).
  18374. + */
  18375. +static ssize_t regdump_show(struct device *_dev,
  18376. + struct device_attribute *attr, char *buf)
  18377. +{
  18378. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18379. +
  18380. + dwc_otg_dump_global_registers(otg_dev->core_if);
  18381. + if (dwc_otg_is_host_mode(otg_dev->core_if)) {
  18382. + dwc_otg_dump_host_registers(otg_dev->core_if);
  18383. + } else {
  18384. + dwc_otg_dump_dev_registers(otg_dev->core_if);
  18385. +
  18386. + }
  18387. + return sprintf(buf, "Register Dump\n");
  18388. +}
  18389. +
  18390. +DEVICE_ATTR(regdump, S_IRUGO, regdump_show, 0);
  18391. +
  18392. +/**
  18393. + * Dump global registers and either host or device registers (depending on the
  18394. + * current mode of the core).
  18395. + */
  18396. +static ssize_t spramdump_show(struct device *_dev,
  18397. + struct device_attribute *attr, char *buf)
  18398. +{
  18399. +#if 0
  18400. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18401. +
  18402. + dwc_otg_dump_spram(otg_dev->core_if);
  18403. +#endif
  18404. +
  18405. + return sprintf(buf, "SPRAM Dump\n");
  18406. +}
  18407. +
  18408. +DEVICE_ATTR(spramdump, S_IRUGO, spramdump_show, 0);
  18409. +
  18410. +/**
  18411. + * Dump the current hcd state.
  18412. + */
  18413. +static ssize_t hcddump_show(struct device *_dev,
  18414. + struct device_attribute *attr, char *buf)
  18415. +{
  18416. +#ifndef DWC_DEVICE_ONLY
  18417. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18418. + dwc_otg_hcd_dump_state(otg_dev->hcd);
  18419. +#endif /* DWC_DEVICE_ONLY */
  18420. + return sprintf(buf, "HCD Dump\n");
  18421. +}
  18422. +
  18423. +DEVICE_ATTR(hcddump, S_IRUGO, hcddump_show, 0);
  18424. +
  18425. +/**
  18426. + * Dump the average frame remaining at SOF. This can be used to
  18427. + * determine average interrupt latency. Frame remaining is also shown for
  18428. + * start transfer and two additional sample points.
  18429. + */
  18430. +static ssize_t hcd_frrem_show(struct device *_dev,
  18431. + struct device_attribute *attr, char *buf)
  18432. +{
  18433. +#ifndef DWC_DEVICE_ONLY
  18434. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18435. +
  18436. + dwc_otg_hcd_dump_frrem(otg_dev->hcd);
  18437. +#endif /* DWC_DEVICE_ONLY */
  18438. + return sprintf(buf, "HCD Dump Frame Remaining\n");
  18439. +}
  18440. +
  18441. +DEVICE_ATTR(hcd_frrem, S_IRUGO, hcd_frrem_show, 0);
  18442. +
  18443. +/**
  18444. + * Displays the time required to read the GNPTXFSIZ register many times (the
  18445. + * output shows the number of times the register is read).
  18446. + */
  18447. +#define RW_REG_COUNT 10000000
  18448. +#define MSEC_PER_JIFFIE 1000/HZ
  18449. +static ssize_t rd_reg_test_show(struct device *_dev,
  18450. + struct device_attribute *attr, char *buf)
  18451. +{
  18452. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18453. + int i;
  18454. + int time;
  18455. + int start_jiffies;
  18456. +
  18457. + printk("HZ %d, MSEC_PER_JIFFIE %d, loops_per_jiffy %lu\n",
  18458. + HZ, MSEC_PER_JIFFIE, loops_per_jiffy);
  18459. + start_jiffies = jiffies;
  18460. + for (i = 0; i < RW_REG_COUNT; i++) {
  18461. + dwc_otg_get_gnptxfsiz(otg_dev->core_if);
  18462. + }
  18463. + time = jiffies - start_jiffies;
  18464. + return sprintf(buf,
  18465. + "Time to read GNPTXFSIZ reg %d times: %d msecs (%d jiffies)\n",
  18466. + RW_REG_COUNT, time * MSEC_PER_JIFFIE, time);
  18467. +}
  18468. +
  18469. +DEVICE_ATTR(rd_reg_test, S_IRUGO, rd_reg_test_show, 0);
  18470. +
  18471. +/**
  18472. + * Displays the time required to write the GNPTXFSIZ register many times (the
  18473. + * output shows the number of times the register is written).
  18474. + */
  18475. +static ssize_t wr_reg_test_show(struct device *_dev,
  18476. + struct device_attribute *attr, char *buf)
  18477. +{
  18478. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18479. + uint32_t reg_val;
  18480. + int i;
  18481. + int time;
  18482. + int start_jiffies;
  18483. +
  18484. + printk("HZ %d, MSEC_PER_JIFFIE %d, loops_per_jiffy %lu\n",
  18485. + HZ, MSEC_PER_JIFFIE, loops_per_jiffy);
  18486. + reg_val = dwc_otg_get_gnptxfsiz(otg_dev->core_if);
  18487. + start_jiffies = jiffies;
  18488. + for (i = 0; i < RW_REG_COUNT; i++) {
  18489. + dwc_otg_set_gnptxfsiz(otg_dev->core_if, reg_val);
  18490. + }
  18491. + time = jiffies - start_jiffies;
  18492. + return sprintf(buf,
  18493. + "Time to write GNPTXFSIZ reg %d times: %d msecs (%d jiffies)\n",
  18494. + RW_REG_COUNT, time * MSEC_PER_JIFFIE, time);
  18495. +}
  18496. +
  18497. +DEVICE_ATTR(wr_reg_test, S_IRUGO, wr_reg_test_show, 0);
  18498. +
  18499. +#ifdef CONFIG_USB_DWC_OTG_LPM
  18500. +
  18501. +/**
  18502. +* Show the lpm_response attribute.
  18503. +*/
  18504. +static ssize_t lpmresp_show(struct device *_dev,
  18505. + struct device_attribute *attr, char *buf)
  18506. +{
  18507. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18508. +
  18509. + if (!dwc_otg_get_param_lpm_enable(otg_dev->core_if))
  18510. + return sprintf(buf, "** LPM is DISABLED **\n");
  18511. +
  18512. + if (!dwc_otg_is_device_mode(otg_dev->core_if)) {
  18513. + return sprintf(buf, "** Current mode is not device mode\n");
  18514. + }
  18515. + return sprintf(buf, "lpm_response = %d\n",
  18516. + dwc_otg_get_lpmresponse(otg_dev->core_if));
  18517. +}
  18518. +
  18519. +/**
  18520. +* Store the lpm_response attribute.
  18521. +*/
  18522. +static ssize_t lpmresp_store(struct device *_dev,
  18523. + struct device_attribute *attr,
  18524. + const char *buf, size_t count)
  18525. +{
  18526. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18527. + uint32_t val = simple_strtoul(buf, NULL, 16);
  18528. +
  18529. + if (!dwc_otg_get_param_lpm_enable(otg_dev->core_if)) {
  18530. + return 0;
  18531. + }
  18532. +
  18533. + if (!dwc_otg_is_device_mode(otg_dev->core_if)) {
  18534. + return 0;
  18535. + }
  18536. +
  18537. + dwc_otg_set_lpmresponse(otg_dev->core_if, val);
  18538. + return count;
  18539. +}
  18540. +
  18541. +DEVICE_ATTR(lpm_response, S_IRUGO | S_IWUSR, lpmresp_show, lpmresp_store);
  18542. +
  18543. +/**
  18544. +* Show the sleep_status attribute.
  18545. +*/
  18546. +static ssize_t sleepstatus_show(struct device *_dev,
  18547. + struct device_attribute *attr, char *buf)
  18548. +{
  18549. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18550. + return sprintf(buf, "Sleep Status = %d\n",
  18551. + dwc_otg_get_lpm_portsleepstatus(otg_dev->core_if));
  18552. +}
  18553. +
  18554. +/**
  18555. + * Store the sleep_status attribure.
  18556. + */
  18557. +static ssize_t sleepstatus_store(struct device *_dev,
  18558. + struct device_attribute *attr,
  18559. + const char *buf, size_t count)
  18560. +{
  18561. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18562. + dwc_otg_core_if_t *core_if = otg_dev->core_if;
  18563. +
  18564. + if (dwc_otg_get_lpm_portsleepstatus(otg_dev->core_if)) {
  18565. + if (dwc_otg_is_host_mode(core_if)) {
  18566. +
  18567. + DWC_PRINTF("Host initiated resume\n");
  18568. + dwc_otg_set_prtresume(otg_dev->core_if, 1);
  18569. + }
  18570. + }
  18571. +
  18572. + return count;
  18573. +}
  18574. +
  18575. +DEVICE_ATTR(sleep_status, S_IRUGO | S_IWUSR, sleepstatus_show,
  18576. + sleepstatus_store);
  18577. +
  18578. +#endif /* CONFIG_USB_DWC_OTG_LPM_ENABLE */
  18579. +
  18580. +/**@}*/
  18581. +
  18582. +/**
  18583. + * Create the device files
  18584. + */
  18585. +void dwc_otg_attr_create(
  18586. +#ifdef LM_INTERFACE
  18587. + struct lm_device *dev
  18588. +#elif defined(PCI_INTERFACE)
  18589. + struct pci_dev *dev
  18590. +#elif defined(PLATFORM_INTERFACE)
  18591. + struct platform_device *dev
  18592. +#endif
  18593. + )
  18594. +{
  18595. + int error;
  18596. +
  18597. + error = device_create_file(&dev->dev, &dev_attr_regoffset);
  18598. + error = device_create_file(&dev->dev, &dev_attr_regvalue);
  18599. + error = device_create_file(&dev->dev, &dev_attr_mode);
  18600. + error = device_create_file(&dev->dev, &dev_attr_hnpcapable);
  18601. + error = device_create_file(&dev->dev, &dev_attr_srpcapable);
  18602. + error = device_create_file(&dev->dev, &dev_attr_hsic_connect);
  18603. + error = device_create_file(&dev->dev, &dev_attr_inv_sel_hsic);
  18604. + error = device_create_file(&dev->dev, &dev_attr_hnp);
  18605. + error = device_create_file(&dev->dev, &dev_attr_srp);
  18606. + error = device_create_file(&dev->dev, &dev_attr_buspower);
  18607. + error = device_create_file(&dev->dev, &dev_attr_bussuspend);
  18608. + error = device_create_file(&dev->dev, &dev_attr_mode_ch_tim_en);
  18609. + error = device_create_file(&dev->dev, &dev_attr_fr_interval);
  18610. + error = device_create_file(&dev->dev, &dev_attr_busconnected);
  18611. + error = device_create_file(&dev->dev, &dev_attr_gotgctl);
  18612. + error = device_create_file(&dev->dev, &dev_attr_gusbcfg);
  18613. + error = device_create_file(&dev->dev, &dev_attr_grxfsiz);
  18614. + error = device_create_file(&dev->dev, &dev_attr_gnptxfsiz);
  18615. + error = device_create_file(&dev->dev, &dev_attr_gpvndctl);
  18616. + error = device_create_file(&dev->dev, &dev_attr_ggpio);
  18617. + error = device_create_file(&dev->dev, &dev_attr_guid);
  18618. + error = device_create_file(&dev->dev, &dev_attr_gsnpsid);
  18619. + error = device_create_file(&dev->dev, &dev_attr_devspeed);
  18620. + error = device_create_file(&dev->dev, &dev_attr_enumspeed);
  18621. + error = device_create_file(&dev->dev, &dev_attr_hptxfsiz);
  18622. + error = device_create_file(&dev->dev, &dev_attr_hprt0);
  18623. + error = device_create_file(&dev->dev, &dev_attr_remote_wakeup);
  18624. + error = device_create_file(&dev->dev, &dev_attr_rem_wakeup_pwrdn);
  18625. + error = device_create_file(&dev->dev, &dev_attr_disconnect_us);
  18626. + error = device_create_file(&dev->dev, &dev_attr_regdump);
  18627. + error = device_create_file(&dev->dev, &dev_attr_spramdump);
  18628. + error = device_create_file(&dev->dev, &dev_attr_hcddump);
  18629. + error = device_create_file(&dev->dev, &dev_attr_hcd_frrem);
  18630. + error = device_create_file(&dev->dev, &dev_attr_rd_reg_test);
  18631. + error = device_create_file(&dev->dev, &dev_attr_wr_reg_test);
  18632. +#ifdef CONFIG_USB_DWC_OTG_LPM
  18633. + error = device_create_file(&dev->dev, &dev_attr_lpm_response);
  18634. + error = device_create_file(&dev->dev, &dev_attr_sleep_status);
  18635. +#endif
  18636. +}
  18637. +
  18638. +/**
  18639. + * Remove the device files
  18640. + */
  18641. +void dwc_otg_attr_remove(
  18642. +#ifdef LM_INTERFACE
  18643. + struct lm_device *dev
  18644. +#elif defined(PCI_INTERFACE)
  18645. + struct pci_dev *dev
  18646. +#elif defined(PLATFORM_INTERFACE)
  18647. + struct platform_device *dev
  18648. +#endif
  18649. + )
  18650. +{
  18651. + device_remove_file(&dev->dev, &dev_attr_regoffset);
  18652. + device_remove_file(&dev->dev, &dev_attr_regvalue);
  18653. + device_remove_file(&dev->dev, &dev_attr_mode);
  18654. + device_remove_file(&dev->dev, &dev_attr_hnpcapable);
  18655. + device_remove_file(&dev->dev, &dev_attr_srpcapable);
  18656. + device_remove_file(&dev->dev, &dev_attr_hsic_connect);
  18657. + device_remove_file(&dev->dev, &dev_attr_inv_sel_hsic);
  18658. + device_remove_file(&dev->dev, &dev_attr_hnp);
  18659. + device_remove_file(&dev->dev, &dev_attr_srp);
  18660. + device_remove_file(&dev->dev, &dev_attr_buspower);
  18661. + device_remove_file(&dev->dev, &dev_attr_bussuspend);
  18662. + device_remove_file(&dev->dev, &dev_attr_mode_ch_tim_en);
  18663. + device_remove_file(&dev->dev, &dev_attr_fr_interval);
  18664. + device_remove_file(&dev->dev, &dev_attr_busconnected);
  18665. + device_remove_file(&dev->dev, &dev_attr_gotgctl);
  18666. + device_remove_file(&dev->dev, &dev_attr_gusbcfg);
  18667. + device_remove_file(&dev->dev, &dev_attr_grxfsiz);
  18668. + device_remove_file(&dev->dev, &dev_attr_gnptxfsiz);
  18669. + device_remove_file(&dev->dev, &dev_attr_gpvndctl);
  18670. + device_remove_file(&dev->dev, &dev_attr_ggpio);
  18671. + device_remove_file(&dev->dev, &dev_attr_guid);
  18672. + device_remove_file(&dev->dev, &dev_attr_gsnpsid);
  18673. + device_remove_file(&dev->dev, &dev_attr_devspeed);
  18674. + device_remove_file(&dev->dev, &dev_attr_enumspeed);
  18675. + device_remove_file(&dev->dev, &dev_attr_hptxfsiz);
  18676. + device_remove_file(&dev->dev, &dev_attr_hprt0);
  18677. + device_remove_file(&dev->dev, &dev_attr_remote_wakeup);
  18678. + device_remove_file(&dev->dev, &dev_attr_rem_wakeup_pwrdn);
  18679. + device_remove_file(&dev->dev, &dev_attr_disconnect_us);
  18680. + device_remove_file(&dev->dev, &dev_attr_regdump);
  18681. + device_remove_file(&dev->dev, &dev_attr_spramdump);
  18682. + device_remove_file(&dev->dev, &dev_attr_hcddump);
  18683. + device_remove_file(&dev->dev, &dev_attr_hcd_frrem);
  18684. + device_remove_file(&dev->dev, &dev_attr_rd_reg_test);
  18685. + device_remove_file(&dev->dev, &dev_attr_wr_reg_test);
  18686. +#ifdef CONFIG_USB_DWC_OTG_LPM
  18687. + device_remove_file(&dev->dev, &dev_attr_lpm_response);
  18688. + device_remove_file(&dev->dev, &dev_attr_sleep_status);
  18689. +#endif
  18690. +}
  18691. --- /dev/null
  18692. +++ b/drivers/usb/host/dwc_otg/dwc_otg_attr.h
  18693. @@ -0,0 +1,89 @@
  18694. +/* ==========================================================================
  18695. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_attr.h $
  18696. + * $Revision: #13 $
  18697. + * $Date: 2010/06/21 $
  18698. + * $Change: 1532021 $
  18699. + *
  18700. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  18701. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  18702. + * otherwise expressly agreed to in writing between Synopsys and you.
  18703. + *
  18704. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  18705. + * any End User Software License Agreement or Agreement for Licensed Product
  18706. + * with Synopsys or any supplement thereto. You are permitted to use and
  18707. + * redistribute this Software in source and binary forms, with or without
  18708. + * modification, provided that redistributions of source code must retain this
  18709. + * notice. You may not view, use, disclose, copy or distribute this file or
  18710. + * any information contained herein except pursuant to this license grant from
  18711. + * Synopsys. If you do not agree with this notice, including the disclaimer
  18712. + * below, then you are not authorized to use the Software.
  18713. + *
  18714. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  18715. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18716. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18717. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  18718. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  18719. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  18720. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  18721. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  18722. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  18723. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  18724. + * DAMAGE.
  18725. + * ========================================================================== */
  18726. +
  18727. +#if !defined(__DWC_OTG_ATTR_H__)
  18728. +#define __DWC_OTG_ATTR_H__
  18729. +
  18730. +/** @file
  18731. + * This file contains the interface to the Linux device attributes.
  18732. + */
  18733. +extern struct device_attribute dev_attr_regoffset;
  18734. +extern struct device_attribute dev_attr_regvalue;
  18735. +
  18736. +extern struct device_attribute dev_attr_mode;
  18737. +extern struct device_attribute dev_attr_hnpcapable;
  18738. +extern struct device_attribute dev_attr_srpcapable;
  18739. +extern struct device_attribute dev_attr_hnp;
  18740. +extern struct device_attribute dev_attr_srp;
  18741. +extern struct device_attribute dev_attr_buspower;
  18742. +extern struct device_attribute dev_attr_bussuspend;
  18743. +extern struct device_attribute dev_attr_mode_ch_tim_en;
  18744. +extern struct device_attribute dev_attr_fr_interval;
  18745. +extern struct device_attribute dev_attr_busconnected;
  18746. +extern struct device_attribute dev_attr_gotgctl;
  18747. +extern struct device_attribute dev_attr_gusbcfg;
  18748. +extern struct device_attribute dev_attr_grxfsiz;
  18749. +extern struct device_attribute dev_attr_gnptxfsiz;
  18750. +extern struct device_attribute dev_attr_gpvndctl;
  18751. +extern struct device_attribute dev_attr_ggpio;
  18752. +extern struct device_attribute dev_attr_guid;
  18753. +extern struct device_attribute dev_attr_gsnpsid;
  18754. +extern struct device_attribute dev_attr_devspeed;
  18755. +extern struct device_attribute dev_attr_enumspeed;
  18756. +extern struct device_attribute dev_attr_hptxfsiz;
  18757. +extern struct device_attribute dev_attr_hprt0;
  18758. +#ifdef CONFIG_USB_DWC_OTG_LPM
  18759. +extern struct device_attribute dev_attr_lpm_response;
  18760. +extern struct device_attribute devi_attr_sleep_status;
  18761. +#endif
  18762. +
  18763. +void dwc_otg_attr_create(
  18764. +#ifdef LM_INTERFACE
  18765. + struct lm_device *dev
  18766. +#elif defined(PCI_INTERFACE)
  18767. + struct pci_dev *dev
  18768. +#elif defined(PLATFORM_INTERFACE)
  18769. + struct platform_device *dev
  18770. +#endif
  18771. + );
  18772. +
  18773. +void dwc_otg_attr_remove(
  18774. +#ifdef LM_INTERFACE
  18775. + struct lm_device *dev
  18776. +#elif defined(PCI_INTERFACE)
  18777. + struct pci_dev *dev
  18778. +#elif defined(PLATFORM_INTERFACE)
  18779. + struct platform_device *dev
  18780. +#endif
  18781. + );
  18782. +#endif
  18783. --- /dev/null
  18784. +++ b/drivers/usb/host/dwc_otg/dwc_otg_cfi.c
  18785. @@ -0,0 +1,1876 @@
  18786. +/* ==========================================================================
  18787. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  18788. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  18789. + * otherwise expressly agreed to in writing between Synopsys and you.
  18790. + *
  18791. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  18792. + * any End User Software License Agreement or Agreement for Licensed Product
  18793. + * with Synopsys or any supplement thereto. You are permitted to use and
  18794. + * redistribute this Software in source and binary forms, with or without
  18795. + * modification, provided that redistributions of source code must retain this
  18796. + * notice. You may not view, use, disclose, copy or distribute this file or
  18797. + * any information contained herein except pursuant to this license grant from
  18798. + * Synopsys. If you do not agree with this notice, including the disclaimer
  18799. + * below, then you are not authorized to use the Software.
  18800. + *
  18801. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  18802. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18803. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18804. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  18805. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  18806. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  18807. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  18808. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  18809. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  18810. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  18811. + * DAMAGE.
  18812. + * ========================================================================== */
  18813. +
  18814. +/** @file
  18815. + *
  18816. + * This file contains the most of the CFI(Core Feature Interface)
  18817. + * implementation for the OTG.
  18818. + */
  18819. +
  18820. +#ifdef DWC_UTE_CFI
  18821. +
  18822. +#include "dwc_otg_pcd.h"
  18823. +#include "dwc_otg_cfi.h"
  18824. +
  18825. +/** This definition should actually migrate to the Portability Library */
  18826. +#define DWC_CONSTANT_CPU_TO_LE16(x) (x)
  18827. +
  18828. +extern dwc_otg_pcd_ep_t *get_ep_by_addr(dwc_otg_pcd_t * pcd, u16 wIndex);
  18829. +
  18830. +static int cfi_core_features_buf(uint8_t * buf, uint16_t buflen);
  18831. +static int cfi_get_feature_value(uint8_t * buf, uint16_t buflen,
  18832. + struct dwc_otg_pcd *pcd,
  18833. + struct cfi_usb_ctrlrequest *ctrl_req);
  18834. +static int cfi_set_feature_value(struct dwc_otg_pcd *pcd);
  18835. +static int cfi_ep_get_sg_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  18836. + struct cfi_usb_ctrlrequest *req);
  18837. +static int cfi_ep_get_concat_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  18838. + struct cfi_usb_ctrlrequest *req);
  18839. +static int cfi_ep_get_align_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  18840. + struct cfi_usb_ctrlrequest *req);
  18841. +static int cfi_preproc_reset(struct dwc_otg_pcd *pcd,
  18842. + struct cfi_usb_ctrlrequest *req);
  18843. +static void cfi_free_ep_bs_dyn_data(cfi_ep_t * cfiep);
  18844. +
  18845. +static uint16_t get_dfifo_size(dwc_otg_core_if_t * core_if);
  18846. +static int32_t get_rxfifo_size(dwc_otg_core_if_t * core_if, uint16_t wValue);
  18847. +static int32_t get_txfifo_size(struct dwc_otg_pcd *pcd, uint16_t wValue);
  18848. +
  18849. +static uint8_t resize_fifos(dwc_otg_core_if_t * core_if);
  18850. +
  18851. +/** This is the header of the all features descriptor */
  18852. +static cfi_all_features_header_t all_props_desc_header = {
  18853. + .wVersion = DWC_CONSTANT_CPU_TO_LE16(0x100),
  18854. + .wCoreID = DWC_CONSTANT_CPU_TO_LE16(CFI_CORE_ID_OTG),
  18855. + .wNumFeatures = DWC_CONSTANT_CPU_TO_LE16(9),
  18856. +};
  18857. +
  18858. +/** This is an array of statically allocated feature descriptors */
  18859. +static cfi_feature_desc_header_t prop_descs[] = {
  18860. +
  18861. + /* FT_ID_DMA_MODE */
  18862. + {
  18863. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_MODE),
  18864. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  18865. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(1),
  18866. + },
  18867. +
  18868. + /* FT_ID_DMA_BUFFER_SETUP */
  18869. + {
  18870. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_BUFFER_SETUP),
  18871. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  18872. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
  18873. + },
  18874. +
  18875. + /* FT_ID_DMA_BUFF_ALIGN */
  18876. + {
  18877. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_BUFF_ALIGN),
  18878. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  18879. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
  18880. + },
  18881. +
  18882. + /* FT_ID_DMA_CONCAT_SETUP */
  18883. + {
  18884. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_CONCAT_SETUP),
  18885. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  18886. + //.wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
  18887. + },
  18888. +
  18889. + /* FT_ID_DMA_CIRCULAR */
  18890. + {
  18891. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_CIRCULAR),
  18892. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  18893. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
  18894. + },
  18895. +
  18896. + /* FT_ID_THRESHOLD_SETUP */
  18897. + {
  18898. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_THRESHOLD_SETUP),
  18899. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  18900. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
  18901. + },
  18902. +
  18903. + /* FT_ID_DFIFO_DEPTH */
  18904. + {
  18905. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DFIFO_DEPTH),
  18906. + .bmAttributes = CFI_FEATURE_ATTR_RO,
  18907. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
  18908. + },
  18909. +
  18910. + /* FT_ID_TX_FIFO_DEPTH */
  18911. + {
  18912. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_TX_FIFO_DEPTH),
  18913. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  18914. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
  18915. + },
  18916. +
  18917. + /* FT_ID_RX_FIFO_DEPTH */
  18918. + {
  18919. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_RX_FIFO_DEPTH),
  18920. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  18921. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
  18922. + }
  18923. +};
  18924. +
  18925. +/** The table of feature names */
  18926. +cfi_string_t prop_name_table[] = {
  18927. + {FT_ID_DMA_MODE, "dma_mode"},
  18928. + {FT_ID_DMA_BUFFER_SETUP, "buffer_setup"},
  18929. + {FT_ID_DMA_BUFF_ALIGN, "buffer_align"},
  18930. + {FT_ID_DMA_CONCAT_SETUP, "concat_setup"},
  18931. + {FT_ID_DMA_CIRCULAR, "buffer_circular"},
  18932. + {FT_ID_THRESHOLD_SETUP, "threshold_setup"},
  18933. + {FT_ID_DFIFO_DEPTH, "dfifo_depth"},
  18934. + {FT_ID_TX_FIFO_DEPTH, "txfifo_depth"},
  18935. + {FT_ID_RX_FIFO_DEPTH, "rxfifo_depth"},
  18936. + {}
  18937. +};
  18938. +
  18939. +/************************************************************************/
  18940. +
  18941. +/**
  18942. + * Returns the name of the feature by its ID
  18943. + * or NULL if no featute ID matches.
  18944. + *
  18945. + */
  18946. +const uint8_t *get_prop_name(uint16_t prop_id, int *len)
  18947. +{
  18948. + cfi_string_t *pstr;
  18949. + *len = 0;
  18950. +
  18951. + for (pstr = prop_name_table; pstr && pstr->s; pstr++) {
  18952. + if (pstr->id == prop_id) {
  18953. + *len = DWC_STRLEN(pstr->s);
  18954. + return pstr->s;
  18955. + }
  18956. + }
  18957. + return NULL;
  18958. +}
  18959. +
  18960. +/**
  18961. + * This function handles all CFI specific control requests.
  18962. + *
  18963. + * Return a negative value to stall the DCE.
  18964. + */
  18965. +int cfi_setup(struct dwc_otg_pcd *pcd, struct cfi_usb_ctrlrequest *ctrl)
  18966. +{
  18967. + int retval = 0;
  18968. + dwc_otg_pcd_ep_t *ep = NULL;
  18969. + cfiobject_t *cfi = pcd->cfi;
  18970. + struct dwc_otg_core_if *coreif = GET_CORE_IF(pcd);
  18971. + uint16_t wLen = DWC_LE16_TO_CPU(&ctrl->wLength);
  18972. + uint16_t wValue = DWC_LE16_TO_CPU(&ctrl->wValue);
  18973. + uint16_t wIndex = DWC_LE16_TO_CPU(&ctrl->wIndex);
  18974. + uint32_t regaddr = 0;
  18975. + uint32_t regval = 0;
  18976. +
  18977. + /* Save this Control Request in the CFI object.
  18978. + * The data field will be assigned in the data stage completion CB function.
  18979. + */
  18980. + cfi->ctrl_req = *ctrl;
  18981. + cfi->ctrl_req.data = NULL;
  18982. +
  18983. + cfi->need_gadget_att = 0;
  18984. + cfi->need_status_in_complete = 0;
  18985. +
  18986. + switch (ctrl->bRequest) {
  18987. + case VEN_CORE_GET_FEATURES:
  18988. + retval = cfi_core_features_buf(cfi->buf_in.buf, CFI_IN_BUF_LEN);
  18989. + if (retval >= 0) {
  18990. + //dump_msg(cfi->buf_in.buf, retval);
  18991. + ep = &pcd->ep0;
  18992. +
  18993. + retval = min((uint16_t) retval, wLen);
  18994. + /* Transfer this buffer to the host through the EP0-IN EP */
  18995. + ep->dwc_ep.dma_addr = cfi->buf_in.addr;
  18996. + ep->dwc_ep.start_xfer_buff = cfi->buf_in.buf;
  18997. + ep->dwc_ep.xfer_buff = cfi->buf_in.buf;
  18998. + ep->dwc_ep.xfer_len = retval;
  18999. + ep->dwc_ep.xfer_count = 0;
  19000. + ep->dwc_ep.sent_zlp = 0;
  19001. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  19002. +
  19003. + pcd->ep0_pending = 1;
  19004. + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
  19005. + }
  19006. + retval = 0;
  19007. + break;
  19008. +
  19009. + case VEN_CORE_GET_FEATURE:
  19010. + CFI_INFO("VEN_CORE_GET_FEATURE\n");
  19011. + retval = cfi_get_feature_value(cfi->buf_in.buf, CFI_IN_BUF_LEN,
  19012. + pcd, ctrl);
  19013. + if (retval >= 0) {
  19014. + ep = &pcd->ep0;
  19015. +
  19016. + retval = min((uint16_t) retval, wLen);
  19017. + /* Transfer this buffer to the host through the EP0-IN EP */
  19018. + ep->dwc_ep.dma_addr = cfi->buf_in.addr;
  19019. + ep->dwc_ep.start_xfer_buff = cfi->buf_in.buf;
  19020. + ep->dwc_ep.xfer_buff = cfi->buf_in.buf;
  19021. + ep->dwc_ep.xfer_len = retval;
  19022. + ep->dwc_ep.xfer_count = 0;
  19023. + ep->dwc_ep.sent_zlp = 0;
  19024. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  19025. +
  19026. + pcd->ep0_pending = 1;
  19027. + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
  19028. + }
  19029. + CFI_INFO("VEN_CORE_GET_FEATURE=%d\n", retval);
  19030. + dump_msg(cfi->buf_in.buf, retval);
  19031. + break;
  19032. +
  19033. + case VEN_CORE_SET_FEATURE:
  19034. + CFI_INFO("VEN_CORE_SET_FEATURE\n");
  19035. + /* Set up an XFER to get the data stage of the control request,
  19036. + * which is the new value of the feature to be modified.
  19037. + */
  19038. + ep = &pcd->ep0;
  19039. + ep->dwc_ep.is_in = 0;
  19040. + ep->dwc_ep.dma_addr = cfi->buf_out.addr;
  19041. + ep->dwc_ep.start_xfer_buff = cfi->buf_out.buf;
  19042. + ep->dwc_ep.xfer_buff = cfi->buf_out.buf;
  19043. + ep->dwc_ep.xfer_len = wLen;
  19044. + ep->dwc_ep.xfer_count = 0;
  19045. + ep->dwc_ep.sent_zlp = 0;
  19046. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  19047. +
  19048. + pcd->ep0_pending = 1;
  19049. + /* Read the control write's data stage */
  19050. + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
  19051. + retval = 0;
  19052. + break;
  19053. +
  19054. + case VEN_CORE_RESET_FEATURES:
  19055. + CFI_INFO("VEN_CORE_RESET_FEATURES\n");
  19056. + cfi->need_gadget_att = 1;
  19057. + cfi->need_status_in_complete = 1;
  19058. + retval = cfi_preproc_reset(pcd, ctrl);
  19059. + CFI_INFO("VEN_CORE_RESET_FEATURES = (%d)\n", retval);
  19060. + break;
  19061. +
  19062. + case VEN_CORE_ACTIVATE_FEATURES:
  19063. + CFI_INFO("VEN_CORE_ACTIVATE_FEATURES\n");
  19064. + break;
  19065. +
  19066. + case VEN_CORE_READ_REGISTER:
  19067. + CFI_INFO("VEN_CORE_READ_REGISTER\n");
  19068. + /* wValue optionally contains the HI WORD of the register offset and
  19069. + * wIndex contains the LOW WORD of the register offset
  19070. + */
  19071. + if (wValue == 0) {
  19072. + /* @TODO - MAS - fix the access to the base field */
  19073. + regaddr = 0;
  19074. + //regaddr = (uint32_t) pcd->otg_dev->os_dep.base;
  19075. + //GET_CORE_IF(pcd)->co
  19076. + regaddr |= wIndex;
  19077. + } else {
  19078. + regaddr = (wValue << 16) | wIndex;
  19079. + }
  19080. +
  19081. + /* Read a 32-bit value of the memory at the regaddr */
  19082. + regval = DWC_READ_REG32((uint32_t *) regaddr);
  19083. +
  19084. + ep = &pcd->ep0;
  19085. + dwc_memcpy(cfi->buf_in.buf, &regval, sizeof(uint32_t));
  19086. + ep->dwc_ep.is_in = 1;
  19087. + ep->dwc_ep.dma_addr = cfi->buf_in.addr;
  19088. + ep->dwc_ep.start_xfer_buff = cfi->buf_in.buf;
  19089. + ep->dwc_ep.xfer_buff = cfi->buf_in.buf;
  19090. + ep->dwc_ep.xfer_len = wLen;
  19091. + ep->dwc_ep.xfer_count = 0;
  19092. + ep->dwc_ep.sent_zlp = 0;
  19093. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  19094. +
  19095. + pcd->ep0_pending = 1;
  19096. + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
  19097. + cfi->need_gadget_att = 0;
  19098. + retval = 0;
  19099. + break;
  19100. +
  19101. + case VEN_CORE_WRITE_REGISTER:
  19102. + CFI_INFO("VEN_CORE_WRITE_REGISTER\n");
  19103. + /* Set up an XFER to get the data stage of the control request,
  19104. + * which is the new value of the register to be modified.
  19105. + */
  19106. + ep = &pcd->ep0;
  19107. + ep->dwc_ep.is_in = 0;
  19108. + ep->dwc_ep.dma_addr = cfi->buf_out.addr;
  19109. + ep->dwc_ep.start_xfer_buff = cfi->buf_out.buf;
  19110. + ep->dwc_ep.xfer_buff = cfi->buf_out.buf;
  19111. + ep->dwc_ep.xfer_len = wLen;
  19112. + ep->dwc_ep.xfer_count = 0;
  19113. + ep->dwc_ep.sent_zlp = 0;
  19114. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  19115. +
  19116. + pcd->ep0_pending = 1;
  19117. + /* Read the control write's data stage */
  19118. + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
  19119. + retval = 0;
  19120. + break;
  19121. +
  19122. + default:
  19123. + retval = -DWC_E_NOT_SUPPORTED;
  19124. + break;
  19125. + }
  19126. +
  19127. + return retval;
  19128. +}
  19129. +
  19130. +/**
  19131. + * This function prepares the core features descriptors and copies its
  19132. + * raw representation into the buffer <buf>.
  19133. + *
  19134. + * The buffer structure is as follows:
  19135. + * all_features_header (8 bytes)
  19136. + * features_#1 (8 bytes + feature name string length)
  19137. + * features_#2 (8 bytes + feature name string length)
  19138. + * .....
  19139. + * features_#n - where n=the total count of feature descriptors
  19140. + */
  19141. +static int cfi_core_features_buf(uint8_t * buf, uint16_t buflen)
  19142. +{
  19143. + cfi_feature_desc_header_t *prop_hdr = prop_descs;
  19144. + cfi_feature_desc_header_t *prop;
  19145. + cfi_all_features_header_t *all_props_hdr = &all_props_desc_header;
  19146. + cfi_all_features_header_t *tmp;
  19147. + uint8_t *tmpbuf = buf;
  19148. + const uint8_t *pname = NULL;
  19149. + int i, j, namelen = 0, totlen;
  19150. +
  19151. + /* Prepare and copy the core features into the buffer */
  19152. + CFI_INFO("%s:\n", __func__);
  19153. +
  19154. + tmp = (cfi_all_features_header_t *) tmpbuf;
  19155. + *tmp = *all_props_hdr;
  19156. + tmpbuf += CFI_ALL_FEATURES_HDR_LEN;
  19157. +
  19158. + j = sizeof(prop_descs) / sizeof(cfi_all_features_header_t);
  19159. + for (i = 0; i < j; i++, prop_hdr++) {
  19160. + pname = get_prop_name(prop_hdr->wFeatureID, &namelen);
  19161. + prop = (cfi_feature_desc_header_t *) tmpbuf;
  19162. + *prop = *prop_hdr;
  19163. +
  19164. + prop->bNameLen = namelen;
  19165. + prop->wLength =
  19166. + DWC_CONSTANT_CPU_TO_LE16(CFI_FEATURE_DESC_HDR_LEN +
  19167. + namelen);
  19168. +
  19169. + tmpbuf += CFI_FEATURE_DESC_HDR_LEN;
  19170. + dwc_memcpy(tmpbuf, pname, namelen);
  19171. + tmpbuf += namelen;
  19172. + }
  19173. +
  19174. + totlen = tmpbuf - buf;
  19175. +
  19176. + if (totlen > 0) {
  19177. + tmp = (cfi_all_features_header_t *) buf;
  19178. + tmp->wTotalLen = DWC_CONSTANT_CPU_TO_LE16(totlen);
  19179. + }
  19180. +
  19181. + return totlen;
  19182. +}
  19183. +
  19184. +/**
  19185. + * This function releases all the dynamic memory in the CFI object.
  19186. + */
  19187. +static void cfi_release(cfiobject_t * cfiobj)
  19188. +{
  19189. + cfi_ep_t *cfiep;
  19190. + dwc_list_link_t *tmp;
  19191. +
  19192. + CFI_INFO("%s\n", __func__);
  19193. +
  19194. + if (cfiobj->buf_in.buf) {
  19195. + DWC_DMA_FREE(CFI_IN_BUF_LEN, cfiobj->buf_in.buf,
  19196. + cfiobj->buf_in.addr);
  19197. + cfiobj->buf_in.buf = NULL;
  19198. + }
  19199. +
  19200. + if (cfiobj->buf_out.buf) {
  19201. + DWC_DMA_FREE(CFI_OUT_BUF_LEN, cfiobj->buf_out.buf,
  19202. + cfiobj->buf_out.addr);
  19203. + cfiobj->buf_out.buf = NULL;
  19204. + }
  19205. +
  19206. + /* Free the Buffer Setup values for each EP */
  19207. + //list_for_each_entry(cfiep, &cfiobj->active_eps, lh) {
  19208. + DWC_LIST_FOREACH(tmp, &cfiobj->active_eps) {
  19209. + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  19210. + cfi_free_ep_bs_dyn_data(cfiep);
  19211. + }
  19212. +}
  19213. +
  19214. +/**
  19215. + * This function frees the dynamically allocated EP buffer setup data.
  19216. + */
  19217. +static void cfi_free_ep_bs_dyn_data(cfi_ep_t * cfiep)
  19218. +{
  19219. + if (cfiep->bm_sg) {
  19220. + DWC_FREE(cfiep->bm_sg);
  19221. + cfiep->bm_sg = NULL;
  19222. + }
  19223. +
  19224. + if (cfiep->bm_align) {
  19225. + DWC_FREE(cfiep->bm_align);
  19226. + cfiep->bm_align = NULL;
  19227. + }
  19228. +
  19229. + if (cfiep->bm_concat) {
  19230. + if (NULL != cfiep->bm_concat->wTxBytes) {
  19231. + DWC_FREE(cfiep->bm_concat->wTxBytes);
  19232. + cfiep->bm_concat->wTxBytes = NULL;
  19233. + }
  19234. + DWC_FREE(cfiep->bm_concat);
  19235. + cfiep->bm_concat = NULL;
  19236. + }
  19237. +}
  19238. +
  19239. +/**
  19240. + * This function initializes the default values of the features
  19241. + * for a specific endpoint and should be called only once when
  19242. + * the EP is enabled first time.
  19243. + */
  19244. +static int cfi_ep_init_defaults(struct dwc_otg_pcd *pcd, cfi_ep_t * cfiep)
  19245. +{
  19246. + int retval = 0;
  19247. +
  19248. + cfiep->bm_sg = DWC_ALLOC(sizeof(ddma_sg_buffer_setup_t));
  19249. + if (NULL == cfiep->bm_sg) {
  19250. + CFI_INFO("Failed to allocate memory for SG feature value\n");
  19251. + return -DWC_E_NO_MEMORY;
  19252. + }
  19253. + dwc_memset(cfiep->bm_sg, 0, sizeof(ddma_sg_buffer_setup_t));
  19254. +
  19255. + /* For the Concatenation feature's default value we do not allocate
  19256. + * memory for the wTxBytes field - it will be done in the set_feature_value
  19257. + * request handler.
  19258. + */
  19259. + cfiep->bm_concat = DWC_ALLOC(sizeof(ddma_concat_buffer_setup_t));
  19260. + if (NULL == cfiep->bm_concat) {
  19261. + CFI_INFO
  19262. + ("Failed to allocate memory for CONCATENATION feature value\n");
  19263. + DWC_FREE(cfiep->bm_sg);
  19264. + return -DWC_E_NO_MEMORY;
  19265. + }
  19266. + dwc_memset(cfiep->bm_concat, 0, sizeof(ddma_concat_buffer_setup_t));
  19267. +
  19268. + cfiep->bm_align = DWC_ALLOC(sizeof(ddma_align_buffer_setup_t));
  19269. + if (NULL == cfiep->bm_align) {
  19270. + CFI_INFO
  19271. + ("Failed to allocate memory for Alignment feature value\n");
  19272. + DWC_FREE(cfiep->bm_sg);
  19273. + DWC_FREE(cfiep->bm_concat);
  19274. + return -DWC_E_NO_MEMORY;
  19275. + }
  19276. + dwc_memset(cfiep->bm_align, 0, sizeof(ddma_align_buffer_setup_t));
  19277. +
  19278. + return retval;
  19279. +}
  19280. +
  19281. +/**
  19282. + * The callback function that notifies the CFI on the activation of
  19283. + * an endpoint in the PCD. The following steps are done in this function:
  19284. + *
  19285. + * Create a dynamically allocated cfi_ep_t object (a CFI wrapper to the PCD's
  19286. + * active endpoint)
  19287. + * Create MAX_DMA_DESCS_PER_EP count DMA Descriptors for the EP
  19288. + * Set the Buffer Mode to standard
  19289. + * Initialize the default values for all EP modes (SG, Circular, Concat, Align)
  19290. + * Add the cfi_ep_t object to the list of active endpoints in the CFI object
  19291. + */
  19292. +static int cfi_ep_enable(struct cfiobject *cfi, struct dwc_otg_pcd *pcd,
  19293. + struct dwc_otg_pcd_ep *ep)
  19294. +{
  19295. + cfi_ep_t *cfiep;
  19296. + int retval = -DWC_E_NOT_SUPPORTED;
  19297. +
  19298. + CFI_INFO("%s: epname=%s; epnum=0x%02x\n", __func__,
  19299. + "EP_" /*ep->ep.name */ , ep->desc->bEndpointAddress);
  19300. + /* MAS - Check whether this endpoint already is in the list */
  19301. + cfiep = get_cfi_ep_by_pcd_ep(cfi, ep);
  19302. +
  19303. + if (NULL == cfiep) {
  19304. + /* Allocate a cfi_ep_t object */
  19305. + cfiep = DWC_ALLOC(sizeof(cfi_ep_t));
  19306. + if (NULL == cfiep) {
  19307. + CFI_INFO
  19308. + ("Unable to allocate memory for <cfiep> in function %s\n",
  19309. + __func__);
  19310. + return -DWC_E_NO_MEMORY;
  19311. + }
  19312. + dwc_memset(cfiep, 0, sizeof(cfi_ep_t));
  19313. +
  19314. + /* Save the dwc_otg_pcd_ep pointer in the cfiep object */
  19315. + cfiep->ep = ep;
  19316. +
  19317. + /* Allocate the DMA Descriptors chain of MAX_DMA_DESCS_PER_EP count */
  19318. + ep->dwc_ep.descs =
  19319. + DWC_DMA_ALLOC(MAX_DMA_DESCS_PER_EP *
  19320. + sizeof(dwc_otg_dma_desc_t),
  19321. + &ep->dwc_ep.descs_dma_addr);
  19322. +
  19323. + if (NULL == ep->dwc_ep.descs) {
  19324. + DWC_FREE(cfiep);
  19325. + return -DWC_E_NO_MEMORY;
  19326. + }
  19327. +
  19328. + DWC_LIST_INIT(&cfiep->lh);
  19329. +
  19330. + /* Set the buffer mode to BM_STANDARD. It will be modified
  19331. + * when building descriptors for a specific buffer mode */
  19332. + ep->dwc_ep.buff_mode = BM_STANDARD;
  19333. +
  19334. + /* Create and initialize the default values for this EP's Buffer modes */
  19335. + if ((retval = cfi_ep_init_defaults(pcd, cfiep)) < 0)
  19336. + return retval;
  19337. +
  19338. + /* Add the cfi_ep_t object to the CFI object's list of active endpoints */
  19339. + DWC_LIST_INSERT_TAIL(&cfi->active_eps, &cfiep->lh);
  19340. + retval = 0;
  19341. + } else { /* The sought EP already is in the list */
  19342. + CFI_INFO("%s: The sought EP already is in the list\n",
  19343. + __func__);
  19344. + }
  19345. +
  19346. + return retval;
  19347. +}
  19348. +
  19349. +/**
  19350. + * This function is called when the data stage of a 3-stage Control Write request
  19351. + * is complete.
  19352. + *
  19353. + */
  19354. +static int cfi_ctrl_write_complete(struct cfiobject *cfi,
  19355. + struct dwc_otg_pcd *pcd)
  19356. +{
  19357. + uint32_t addr, reg_value;
  19358. + uint16_t wIndex, wValue;
  19359. + uint8_t bRequest;
  19360. + uint8_t *buf = cfi->buf_out.buf;
  19361. + //struct usb_ctrlrequest *ctrl_req = &cfi->ctrl_req_saved;
  19362. + struct cfi_usb_ctrlrequest *ctrl_req = &cfi->ctrl_req;
  19363. + int retval = -DWC_E_NOT_SUPPORTED;
  19364. +
  19365. + CFI_INFO("%s\n", __func__);
  19366. +
  19367. + bRequest = ctrl_req->bRequest;
  19368. + wIndex = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wIndex);
  19369. + wValue = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wValue);
  19370. +
  19371. + /*
  19372. + * Save the pointer to the data stage in the ctrl_req's <data> field.
  19373. + * The request should be already saved in the command stage by now.
  19374. + */
  19375. + ctrl_req->data = cfi->buf_out.buf;
  19376. + cfi->need_status_in_complete = 0;
  19377. + cfi->need_gadget_att = 0;
  19378. +
  19379. + switch (bRequest) {
  19380. + case VEN_CORE_WRITE_REGISTER:
  19381. + /* The buffer contains raw data of the new value for the register */
  19382. + reg_value = *((uint32_t *) buf);
  19383. + if (wValue == 0) {
  19384. + addr = 0;
  19385. + //addr = (uint32_t) pcd->otg_dev->os_dep.base;
  19386. + addr += wIndex;
  19387. + } else {
  19388. + addr = (wValue << 16) | wIndex;
  19389. + }
  19390. +
  19391. + //writel(reg_value, addr);
  19392. +
  19393. + retval = 0;
  19394. + cfi->need_status_in_complete = 1;
  19395. + break;
  19396. +
  19397. + case VEN_CORE_SET_FEATURE:
  19398. + /* The buffer contains raw data of the new value of the feature */
  19399. + retval = cfi_set_feature_value(pcd);
  19400. + if (retval < 0)
  19401. + return retval;
  19402. +
  19403. + cfi->need_status_in_complete = 1;
  19404. + break;
  19405. +
  19406. + default:
  19407. + break;
  19408. + }
  19409. +
  19410. + return retval;
  19411. +}
  19412. +
  19413. +/**
  19414. + * This function builds the DMA descriptors for the SG buffer mode.
  19415. + */
  19416. +static void cfi_build_sg_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
  19417. + dwc_otg_pcd_request_t * req)
  19418. +{
  19419. + struct dwc_otg_pcd_ep *ep = cfiep->ep;
  19420. + ddma_sg_buffer_setup_t *sgval = cfiep->bm_sg;
  19421. + struct dwc_otg_dma_desc *desc = cfiep->ep->dwc_ep.descs;
  19422. + struct dwc_otg_dma_desc *desc_last = cfiep->ep->dwc_ep.descs;
  19423. + dma_addr_t buff_addr = req->dma;
  19424. + int i;
  19425. + uint32_t txsize, off;
  19426. +
  19427. + txsize = sgval->wSize;
  19428. + off = sgval->bOffset;
  19429. +
  19430. +// CFI_INFO("%s: %s TXSIZE=0x%08x; OFFSET=0x%08x\n",
  19431. +// __func__, cfiep->ep->ep.name, txsize, off);
  19432. +
  19433. + for (i = 0; i < sgval->bCount; i++) {
  19434. + desc->status.b.bs = BS_HOST_BUSY;
  19435. + desc->buf = buff_addr;
  19436. + desc->status.b.l = 0;
  19437. + desc->status.b.ioc = 0;
  19438. + desc->status.b.sp = 0;
  19439. + desc->status.b.bytes = txsize;
  19440. + desc->status.b.bs = BS_HOST_READY;
  19441. +
  19442. + /* Set the next address of the buffer */
  19443. + buff_addr += txsize + off;
  19444. + desc_last = desc;
  19445. + desc++;
  19446. + }
  19447. +
  19448. + /* Set the last, ioc and sp bits on the Last DMA Descriptor */
  19449. + desc_last->status.b.l = 1;
  19450. + desc_last->status.b.ioc = 1;
  19451. + desc_last->status.b.sp = ep->dwc_ep.sent_zlp;
  19452. + /* Save the last DMA descriptor pointer */
  19453. + cfiep->dma_desc_last = desc_last;
  19454. + cfiep->desc_count = sgval->bCount;
  19455. +}
  19456. +
  19457. +/**
  19458. + * This function builds the DMA descriptors for the Concatenation buffer mode.
  19459. + */
  19460. +static void cfi_build_concat_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
  19461. + dwc_otg_pcd_request_t * req)
  19462. +{
  19463. + struct dwc_otg_pcd_ep *ep = cfiep->ep;
  19464. + ddma_concat_buffer_setup_t *concatval = cfiep->bm_concat;
  19465. + struct dwc_otg_dma_desc *desc = cfiep->ep->dwc_ep.descs;
  19466. + struct dwc_otg_dma_desc *desc_last = cfiep->ep->dwc_ep.descs;
  19467. + dma_addr_t buff_addr = req->dma;
  19468. + int i;
  19469. + uint16_t *txsize;
  19470. +
  19471. + txsize = concatval->wTxBytes;
  19472. +
  19473. + for (i = 0; i < concatval->hdr.bDescCount; i++) {
  19474. + desc->buf = buff_addr;
  19475. + desc->status.b.bs = BS_HOST_BUSY;
  19476. + desc->status.b.l = 0;
  19477. + desc->status.b.ioc = 0;
  19478. + desc->status.b.sp = 0;
  19479. + desc->status.b.bytes = *txsize;
  19480. + desc->status.b.bs = BS_HOST_READY;
  19481. +
  19482. + txsize++;
  19483. + /* Set the next address of the buffer */
  19484. + buff_addr += UGETW(ep->desc->wMaxPacketSize);
  19485. + desc_last = desc;
  19486. + desc++;
  19487. + }
  19488. +
  19489. + /* Set the last, ioc and sp bits on the Last DMA Descriptor */
  19490. + desc_last->status.b.l = 1;
  19491. + desc_last->status.b.ioc = 1;
  19492. + desc_last->status.b.sp = ep->dwc_ep.sent_zlp;
  19493. + cfiep->dma_desc_last = desc_last;
  19494. + cfiep->desc_count = concatval->hdr.bDescCount;
  19495. +}
  19496. +
  19497. +/**
  19498. + * This function builds the DMA descriptors for the Circular buffer mode
  19499. + */
  19500. +static void cfi_build_circ_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
  19501. + dwc_otg_pcd_request_t * req)
  19502. +{
  19503. + /* @todo: MAS - add implementation when this feature needs to be tested */
  19504. +}
  19505. +
  19506. +/**
  19507. + * This function builds the DMA descriptors for the Alignment buffer mode
  19508. + */
  19509. +static void cfi_build_align_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
  19510. + dwc_otg_pcd_request_t * req)
  19511. +{
  19512. + struct dwc_otg_pcd_ep *ep = cfiep->ep;
  19513. + ddma_align_buffer_setup_t *alignval = cfiep->bm_align;
  19514. + struct dwc_otg_dma_desc *desc = cfiep->ep->dwc_ep.descs;
  19515. + dma_addr_t buff_addr = req->dma;
  19516. +
  19517. + desc->status.b.bs = BS_HOST_BUSY;
  19518. + desc->status.b.l = 1;
  19519. + desc->status.b.ioc = 1;
  19520. + desc->status.b.sp = ep->dwc_ep.sent_zlp;
  19521. + desc->status.b.bytes = req->length;
  19522. + /* Adjust the buffer alignment */
  19523. + desc->buf = (buff_addr + alignval->bAlign);
  19524. + desc->status.b.bs = BS_HOST_READY;
  19525. + cfiep->dma_desc_last = desc;
  19526. + cfiep->desc_count = 1;
  19527. +}
  19528. +
  19529. +/**
  19530. + * This function builds the DMA descriptors chain for different modes of the
  19531. + * buffer setup of an endpoint.
  19532. + */
  19533. +static void cfi_build_descriptors(struct cfiobject *cfi,
  19534. + struct dwc_otg_pcd *pcd,
  19535. + struct dwc_otg_pcd_ep *ep,
  19536. + dwc_otg_pcd_request_t * req)
  19537. +{
  19538. + cfi_ep_t *cfiep;
  19539. +
  19540. + /* Get the cfiep by the dwc_otg_pcd_ep */
  19541. + cfiep = get_cfi_ep_by_pcd_ep(cfi, ep);
  19542. + if (NULL == cfiep) {
  19543. + CFI_INFO("%s: Unable to find a matching active endpoint\n",
  19544. + __func__);
  19545. + return;
  19546. + }
  19547. +
  19548. + cfiep->xfer_len = req->length;
  19549. +
  19550. + /* Iterate through all the DMA descriptors */
  19551. + switch (cfiep->ep->dwc_ep.buff_mode) {
  19552. + case BM_SG:
  19553. + cfi_build_sg_descs(cfi, cfiep, req);
  19554. + break;
  19555. +
  19556. + case BM_CONCAT:
  19557. + cfi_build_concat_descs(cfi, cfiep, req);
  19558. + break;
  19559. +
  19560. + case BM_CIRCULAR:
  19561. + cfi_build_circ_descs(cfi, cfiep, req);
  19562. + break;
  19563. +
  19564. + case BM_ALIGN:
  19565. + cfi_build_align_descs(cfi, cfiep, req);
  19566. + break;
  19567. +
  19568. + default:
  19569. + break;
  19570. + }
  19571. +}
  19572. +
  19573. +/**
  19574. + * Allocate DMA buffer for different Buffer modes.
  19575. + */
  19576. +static void *cfi_ep_alloc_buf(struct cfiobject *cfi, struct dwc_otg_pcd *pcd,
  19577. + struct dwc_otg_pcd_ep *ep, dma_addr_t * dma,
  19578. + unsigned size, gfp_t flags)
  19579. +{
  19580. + return DWC_DMA_ALLOC(size, dma);
  19581. +}
  19582. +
  19583. +/**
  19584. + * This function initializes the CFI object.
  19585. + */
  19586. +int init_cfi(cfiobject_t * cfiobj)
  19587. +{
  19588. + CFI_INFO("%s\n", __func__);
  19589. +
  19590. + /* Allocate a buffer for IN XFERs */
  19591. + cfiobj->buf_in.buf =
  19592. + DWC_DMA_ALLOC(CFI_IN_BUF_LEN, &cfiobj->buf_in.addr);
  19593. + if (NULL == cfiobj->buf_in.buf) {
  19594. + CFI_INFO("Unable to allocate buffer for INs\n");
  19595. + return -DWC_E_NO_MEMORY;
  19596. + }
  19597. +
  19598. + /* Allocate a buffer for OUT XFERs */
  19599. + cfiobj->buf_out.buf =
  19600. + DWC_DMA_ALLOC(CFI_OUT_BUF_LEN, &cfiobj->buf_out.addr);
  19601. + if (NULL == cfiobj->buf_out.buf) {
  19602. + CFI_INFO("Unable to allocate buffer for OUT\n");
  19603. + return -DWC_E_NO_MEMORY;
  19604. + }
  19605. +
  19606. + /* Initialize the callback function pointers */
  19607. + cfiobj->ops.release = cfi_release;
  19608. + cfiobj->ops.ep_enable = cfi_ep_enable;
  19609. + cfiobj->ops.ctrl_write_complete = cfi_ctrl_write_complete;
  19610. + cfiobj->ops.build_descriptors = cfi_build_descriptors;
  19611. + cfiobj->ops.ep_alloc_buf = cfi_ep_alloc_buf;
  19612. +
  19613. + /* Initialize the list of active endpoints in the CFI object */
  19614. + DWC_LIST_INIT(&cfiobj->active_eps);
  19615. +
  19616. + return 0;
  19617. +}
  19618. +
  19619. +/**
  19620. + * This function reads the required feature's current value into the buffer
  19621. + *
  19622. + * @retval: Returns negative as error, or the data length of the feature
  19623. + */
  19624. +static int cfi_get_feature_value(uint8_t * buf, uint16_t buflen,
  19625. + struct dwc_otg_pcd *pcd,
  19626. + struct cfi_usb_ctrlrequest *ctrl_req)
  19627. +{
  19628. + int retval = -DWC_E_NOT_SUPPORTED;
  19629. + struct dwc_otg_core_if *coreif = GET_CORE_IF(pcd);
  19630. + uint16_t dfifo, rxfifo, txfifo;
  19631. +
  19632. + switch (ctrl_req->wIndex) {
  19633. + /* Whether the DDMA is enabled or not */
  19634. + case FT_ID_DMA_MODE:
  19635. + *buf = (coreif->dma_enable && coreif->dma_desc_enable) ? 1 : 0;
  19636. + retval = 1;
  19637. + break;
  19638. +
  19639. + case FT_ID_DMA_BUFFER_SETUP:
  19640. + retval = cfi_ep_get_sg_val(buf, pcd, ctrl_req);
  19641. + break;
  19642. +
  19643. + case FT_ID_DMA_BUFF_ALIGN:
  19644. + retval = cfi_ep_get_align_val(buf, pcd, ctrl_req);
  19645. + break;
  19646. +
  19647. + case FT_ID_DMA_CONCAT_SETUP:
  19648. + retval = cfi_ep_get_concat_val(buf, pcd, ctrl_req);
  19649. + break;
  19650. +
  19651. + case FT_ID_DMA_CIRCULAR:
  19652. + CFI_INFO("GetFeature value (FT_ID_DMA_CIRCULAR)\n");
  19653. + break;
  19654. +
  19655. + case FT_ID_THRESHOLD_SETUP:
  19656. + CFI_INFO("GetFeature value (FT_ID_THRESHOLD_SETUP)\n");
  19657. + break;
  19658. +
  19659. + case FT_ID_DFIFO_DEPTH:
  19660. + dfifo = get_dfifo_size(coreif);
  19661. + *((uint16_t *) buf) = dfifo;
  19662. + retval = sizeof(uint16_t);
  19663. + break;
  19664. +
  19665. + case FT_ID_TX_FIFO_DEPTH:
  19666. + retval = get_txfifo_size(pcd, ctrl_req->wValue);
  19667. + if (retval >= 0) {
  19668. + txfifo = retval;
  19669. + *((uint16_t *) buf) = txfifo;
  19670. + retval = sizeof(uint16_t);
  19671. + }
  19672. + break;
  19673. +
  19674. + case FT_ID_RX_FIFO_DEPTH:
  19675. + retval = get_rxfifo_size(coreif, ctrl_req->wValue);
  19676. + if (retval >= 0) {
  19677. + rxfifo = retval;
  19678. + *((uint16_t *) buf) = rxfifo;
  19679. + retval = sizeof(uint16_t);
  19680. + }
  19681. + break;
  19682. + }
  19683. +
  19684. + return retval;
  19685. +}
  19686. +
  19687. +/**
  19688. + * This function resets the SG for the specified EP to its default value
  19689. + */
  19690. +static int cfi_reset_sg_val(cfi_ep_t * cfiep)
  19691. +{
  19692. + dwc_memset(cfiep->bm_sg, 0, sizeof(ddma_sg_buffer_setup_t));
  19693. + return 0;
  19694. +}
  19695. +
  19696. +/**
  19697. + * This function resets the Alignment for the specified EP to its default value
  19698. + */
  19699. +static int cfi_reset_align_val(cfi_ep_t * cfiep)
  19700. +{
  19701. + dwc_memset(cfiep->bm_sg, 0, sizeof(ddma_sg_buffer_setup_t));
  19702. + return 0;
  19703. +}
  19704. +
  19705. +/**
  19706. + * This function resets the Concatenation for the specified EP to its default value
  19707. + * This function will also set the value of the wTxBytes field to NULL after
  19708. + * freeing the memory previously allocated for this field.
  19709. + */
  19710. +static int cfi_reset_concat_val(cfi_ep_t * cfiep)
  19711. +{
  19712. + /* First we need to free the wTxBytes field */
  19713. + if (cfiep->bm_concat->wTxBytes) {
  19714. + DWC_FREE(cfiep->bm_concat->wTxBytes);
  19715. + cfiep->bm_concat->wTxBytes = NULL;
  19716. + }
  19717. +
  19718. + dwc_memset(cfiep->bm_concat, 0, sizeof(ddma_concat_buffer_setup_t));
  19719. + return 0;
  19720. +}
  19721. +
  19722. +/**
  19723. + * This function resets all the buffer setups of the specified endpoint
  19724. + */
  19725. +static int cfi_ep_reset_all_setup_vals(cfi_ep_t * cfiep)
  19726. +{
  19727. + cfi_reset_sg_val(cfiep);
  19728. + cfi_reset_align_val(cfiep);
  19729. + cfi_reset_concat_val(cfiep);
  19730. + return 0;
  19731. +}
  19732. +
  19733. +static int cfi_handle_reset_fifo_val(struct dwc_otg_pcd *pcd, uint8_t ep_addr,
  19734. + uint8_t rx_rst, uint8_t tx_rst)
  19735. +{
  19736. + int retval = -DWC_E_INVALID;
  19737. + uint16_t tx_siz[15];
  19738. + uint16_t rx_siz = 0;
  19739. + dwc_otg_pcd_ep_t *ep = NULL;
  19740. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  19741. + dwc_otg_core_params_t *params = GET_CORE_IF(pcd)->core_params;
  19742. +
  19743. + if (rx_rst) {
  19744. + rx_siz = params->dev_rx_fifo_size;
  19745. + params->dev_rx_fifo_size = GET_CORE_IF(pcd)->init_rxfsiz;
  19746. + }
  19747. +
  19748. + if (tx_rst) {
  19749. + if (ep_addr == 0) {
  19750. + int i;
  19751. +
  19752. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  19753. + tx_siz[i] =
  19754. + core_if->core_params->dev_tx_fifo_size[i];
  19755. + core_if->core_params->dev_tx_fifo_size[i] =
  19756. + core_if->init_txfsiz[i];
  19757. + }
  19758. + } else {
  19759. +
  19760. + ep = get_ep_by_addr(pcd, ep_addr);
  19761. +
  19762. + if (NULL == ep) {
  19763. + CFI_INFO
  19764. + ("%s: Unable to get the endpoint addr=0x%02x\n",
  19765. + __func__, ep_addr);
  19766. + return -DWC_E_INVALID;
  19767. + }
  19768. +
  19769. + tx_siz[0] =
  19770. + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num -
  19771. + 1];
  19772. + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1] =
  19773. + GET_CORE_IF(pcd)->init_txfsiz[ep->
  19774. + dwc_ep.tx_fifo_num -
  19775. + 1];
  19776. + }
  19777. + }
  19778. +
  19779. + if (resize_fifos(GET_CORE_IF(pcd))) {
  19780. + retval = 0;
  19781. + } else {
  19782. + CFI_INFO
  19783. + ("%s: Error resetting the feature Reset All(FIFO size)\n",
  19784. + __func__);
  19785. + if (rx_rst) {
  19786. + params->dev_rx_fifo_size = rx_siz;
  19787. + }
  19788. +
  19789. + if (tx_rst) {
  19790. + if (ep_addr == 0) {
  19791. + int i;
  19792. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps;
  19793. + i++) {
  19794. + core_if->
  19795. + core_params->dev_tx_fifo_size[i] =
  19796. + tx_siz[i];
  19797. + }
  19798. + } else {
  19799. + params->dev_tx_fifo_size[ep->
  19800. + dwc_ep.tx_fifo_num -
  19801. + 1] = tx_siz[0];
  19802. + }
  19803. + }
  19804. + retval = -DWC_E_INVALID;
  19805. + }
  19806. + return retval;
  19807. +}
  19808. +
  19809. +static int cfi_handle_reset_all(struct dwc_otg_pcd *pcd, uint8_t addr)
  19810. +{
  19811. + int retval = 0;
  19812. + cfi_ep_t *cfiep;
  19813. + cfiobject_t *cfi = pcd->cfi;
  19814. + dwc_list_link_t *tmp;
  19815. +
  19816. + retval = cfi_handle_reset_fifo_val(pcd, addr, 1, 1);
  19817. + if (retval < 0) {
  19818. + return retval;
  19819. + }
  19820. +
  19821. + /* If the EP address is known then reset the features for only that EP */
  19822. + if (addr) {
  19823. + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
  19824. + if (NULL == cfiep) {
  19825. + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
  19826. + __func__, addr);
  19827. + return -DWC_E_INVALID;
  19828. + }
  19829. + retval = cfi_ep_reset_all_setup_vals(cfiep);
  19830. + cfiep->ep->dwc_ep.buff_mode = BM_STANDARD;
  19831. + }
  19832. + /* Otherwise (wValue == 0), reset all features of all EP's */
  19833. + else {
  19834. + /* Traverse all the active EP's and reset the feature(s) value(s) */
  19835. + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
  19836. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  19837. + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  19838. + retval = cfi_ep_reset_all_setup_vals(cfiep);
  19839. + cfiep->ep->dwc_ep.buff_mode = BM_STANDARD;
  19840. + if (retval < 0) {
  19841. + CFI_INFO
  19842. + ("%s: Error resetting the feature Reset All\n",
  19843. + __func__);
  19844. + return retval;
  19845. + }
  19846. + }
  19847. + }
  19848. + return retval;
  19849. +}
  19850. +
  19851. +static int cfi_handle_reset_dma_buff_setup(struct dwc_otg_pcd *pcd,
  19852. + uint8_t addr)
  19853. +{
  19854. + int retval = 0;
  19855. + cfi_ep_t *cfiep;
  19856. + cfiobject_t *cfi = pcd->cfi;
  19857. + dwc_list_link_t *tmp;
  19858. +
  19859. + /* If the EP address is known then reset the features for only that EP */
  19860. + if (addr) {
  19861. + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
  19862. + if (NULL == cfiep) {
  19863. + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
  19864. + __func__, addr);
  19865. + return -DWC_E_INVALID;
  19866. + }
  19867. + retval = cfi_reset_sg_val(cfiep);
  19868. + }
  19869. + /* Otherwise (wValue == 0), reset all features of all EP's */
  19870. + else {
  19871. + /* Traverse all the active EP's and reset the feature(s) value(s) */
  19872. + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
  19873. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  19874. + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  19875. + retval = cfi_reset_sg_val(cfiep);
  19876. + if (retval < 0) {
  19877. + CFI_INFO
  19878. + ("%s: Error resetting the feature Buffer Setup\n",
  19879. + __func__);
  19880. + return retval;
  19881. + }
  19882. + }
  19883. + }
  19884. + return retval;
  19885. +}
  19886. +
  19887. +static int cfi_handle_reset_concat_val(struct dwc_otg_pcd *pcd, uint8_t addr)
  19888. +{
  19889. + int retval = 0;
  19890. + cfi_ep_t *cfiep;
  19891. + cfiobject_t *cfi = pcd->cfi;
  19892. + dwc_list_link_t *tmp;
  19893. +
  19894. + /* If the EP address is known then reset the features for only that EP */
  19895. + if (addr) {
  19896. + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
  19897. + if (NULL == cfiep) {
  19898. + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
  19899. + __func__, addr);
  19900. + return -DWC_E_INVALID;
  19901. + }
  19902. + retval = cfi_reset_concat_val(cfiep);
  19903. + }
  19904. + /* Otherwise (wValue == 0), reset all features of all EP's */
  19905. + else {
  19906. + /* Traverse all the active EP's and reset the feature(s) value(s) */
  19907. + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
  19908. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  19909. + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  19910. + retval = cfi_reset_concat_val(cfiep);
  19911. + if (retval < 0) {
  19912. + CFI_INFO
  19913. + ("%s: Error resetting the feature Concatenation Value\n",
  19914. + __func__);
  19915. + return retval;
  19916. + }
  19917. + }
  19918. + }
  19919. + return retval;
  19920. +}
  19921. +
  19922. +static int cfi_handle_reset_align_val(struct dwc_otg_pcd *pcd, uint8_t addr)
  19923. +{
  19924. + int retval = 0;
  19925. + cfi_ep_t *cfiep;
  19926. + cfiobject_t *cfi = pcd->cfi;
  19927. + dwc_list_link_t *tmp;
  19928. +
  19929. + /* If the EP address is known then reset the features for only that EP */
  19930. + if (addr) {
  19931. + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
  19932. + if (NULL == cfiep) {
  19933. + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
  19934. + __func__, addr);
  19935. + return -DWC_E_INVALID;
  19936. + }
  19937. + retval = cfi_reset_align_val(cfiep);
  19938. + }
  19939. + /* Otherwise (wValue == 0), reset all features of all EP's */
  19940. + else {
  19941. + /* Traverse all the active EP's and reset the feature(s) value(s) */
  19942. + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
  19943. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  19944. + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  19945. + retval = cfi_reset_align_val(cfiep);
  19946. + if (retval < 0) {
  19947. + CFI_INFO
  19948. + ("%s: Error resetting the feature Aliignment Value\n",
  19949. + __func__);
  19950. + return retval;
  19951. + }
  19952. + }
  19953. + }
  19954. + return retval;
  19955. +
  19956. +}
  19957. +
  19958. +static int cfi_preproc_reset(struct dwc_otg_pcd *pcd,
  19959. + struct cfi_usb_ctrlrequest *req)
  19960. +{
  19961. + int retval = 0;
  19962. +
  19963. + switch (req->wIndex) {
  19964. + case 0:
  19965. + /* Reset all features */
  19966. + retval = cfi_handle_reset_all(pcd, req->wValue & 0xff);
  19967. + break;
  19968. +
  19969. + case FT_ID_DMA_BUFFER_SETUP:
  19970. + /* Reset the SG buffer setup */
  19971. + retval =
  19972. + cfi_handle_reset_dma_buff_setup(pcd, req->wValue & 0xff);
  19973. + break;
  19974. +
  19975. + case FT_ID_DMA_CONCAT_SETUP:
  19976. + /* Reset the Concatenation buffer setup */
  19977. + retval = cfi_handle_reset_concat_val(pcd, req->wValue & 0xff);
  19978. + break;
  19979. +
  19980. + case FT_ID_DMA_BUFF_ALIGN:
  19981. + /* Reset the Alignment buffer setup */
  19982. + retval = cfi_handle_reset_align_val(pcd, req->wValue & 0xff);
  19983. + break;
  19984. +
  19985. + case FT_ID_TX_FIFO_DEPTH:
  19986. + retval =
  19987. + cfi_handle_reset_fifo_val(pcd, req->wValue & 0xff, 0, 1);
  19988. + pcd->cfi->need_gadget_att = 0;
  19989. + break;
  19990. +
  19991. + case FT_ID_RX_FIFO_DEPTH:
  19992. + retval = cfi_handle_reset_fifo_val(pcd, 0, 1, 0);
  19993. + pcd->cfi->need_gadget_att = 0;
  19994. + break;
  19995. + default:
  19996. + break;
  19997. + }
  19998. + return retval;
  19999. +}
  20000. +
  20001. +/**
  20002. + * This function sets a new value for the SG buffer setup.
  20003. + */
  20004. +static int cfi_ep_set_sg_val(uint8_t * buf, struct dwc_otg_pcd *pcd)
  20005. +{
  20006. + uint8_t inaddr, outaddr;
  20007. + cfi_ep_t *epin, *epout;
  20008. + ddma_sg_buffer_setup_t *psgval;
  20009. + uint32_t desccount, size;
  20010. +
  20011. + CFI_INFO("%s\n", __func__);
  20012. +
  20013. + psgval = (ddma_sg_buffer_setup_t *) buf;
  20014. + desccount = (uint32_t) psgval->bCount;
  20015. + size = (uint32_t) psgval->wSize;
  20016. +
  20017. + /* Check the DMA descriptor count */
  20018. + if ((desccount > MAX_DMA_DESCS_PER_EP) || (desccount == 0)) {
  20019. + CFI_INFO
  20020. + ("%s: The count of DMA Descriptors should be between 1 and %d\n",
  20021. + __func__, MAX_DMA_DESCS_PER_EP);
  20022. + return -DWC_E_INVALID;
  20023. + }
  20024. +
  20025. + /* Check the DMA descriptor count */
  20026. +
  20027. + if (size == 0) {
  20028. +
  20029. + CFI_INFO("%s: The transfer size should be at least 1 byte\n",
  20030. + __func__);
  20031. +
  20032. + return -DWC_E_INVALID;
  20033. +
  20034. + }
  20035. +
  20036. + inaddr = psgval->bInEndpointAddress;
  20037. + outaddr = psgval->bOutEndpointAddress;
  20038. +
  20039. + epin = get_cfi_ep_by_addr(pcd->cfi, inaddr);
  20040. + epout = get_cfi_ep_by_addr(pcd->cfi, outaddr);
  20041. +
  20042. + if (NULL == epin || NULL == epout) {
  20043. + CFI_INFO
  20044. + ("%s: Unable to get the endpoints inaddr=0x%02x outaddr=0x%02x\n",
  20045. + __func__, inaddr, outaddr);
  20046. + return -DWC_E_INVALID;
  20047. + }
  20048. +
  20049. + epin->ep->dwc_ep.buff_mode = BM_SG;
  20050. + dwc_memcpy(epin->bm_sg, psgval, sizeof(ddma_sg_buffer_setup_t));
  20051. +
  20052. + epout->ep->dwc_ep.buff_mode = BM_SG;
  20053. + dwc_memcpy(epout->bm_sg, psgval, sizeof(ddma_sg_buffer_setup_t));
  20054. +
  20055. + return 0;
  20056. +}
  20057. +
  20058. +/**
  20059. + * This function sets a new value for the buffer Alignment setup.
  20060. + */
  20061. +static int cfi_ep_set_alignment_val(uint8_t * buf, struct dwc_otg_pcd *pcd)
  20062. +{
  20063. + cfi_ep_t *ep;
  20064. + uint8_t addr;
  20065. + ddma_align_buffer_setup_t *palignval;
  20066. +
  20067. + palignval = (ddma_align_buffer_setup_t *) buf;
  20068. + addr = palignval->bEndpointAddress;
  20069. +
  20070. + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
  20071. +
  20072. + if (NULL == ep) {
  20073. + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
  20074. + __func__, addr);
  20075. + return -DWC_E_INVALID;
  20076. + }
  20077. +
  20078. + ep->ep->dwc_ep.buff_mode = BM_ALIGN;
  20079. + dwc_memcpy(ep->bm_align, palignval, sizeof(ddma_align_buffer_setup_t));
  20080. +
  20081. + return 0;
  20082. +}
  20083. +
  20084. +/**
  20085. + * This function sets a new value for the Concatenation buffer setup.
  20086. + */
  20087. +static int cfi_ep_set_concat_val(uint8_t * buf, struct dwc_otg_pcd *pcd)
  20088. +{
  20089. + uint8_t addr;
  20090. + cfi_ep_t *ep;
  20091. + struct _ddma_concat_buffer_setup_hdr *pConcatValHdr;
  20092. + uint16_t *pVals;
  20093. + uint32_t desccount;
  20094. + int i;
  20095. + uint16_t mps;
  20096. +
  20097. + pConcatValHdr = (struct _ddma_concat_buffer_setup_hdr *)buf;
  20098. + desccount = (uint32_t) pConcatValHdr->bDescCount;
  20099. + pVals = (uint16_t *) (buf + BS_CONCAT_VAL_HDR_LEN);
  20100. +
  20101. + /* Check the DMA descriptor count */
  20102. + if (desccount > MAX_DMA_DESCS_PER_EP) {
  20103. + CFI_INFO("%s: Maximum DMA Descriptor count should be %d\n",
  20104. + __func__, MAX_DMA_DESCS_PER_EP);
  20105. + return -DWC_E_INVALID;
  20106. + }
  20107. +
  20108. + addr = pConcatValHdr->bEndpointAddress;
  20109. + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
  20110. + if (NULL == ep) {
  20111. + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
  20112. + __func__, addr);
  20113. + return -DWC_E_INVALID;
  20114. + }
  20115. +
  20116. + mps = UGETW(ep->ep->desc->wMaxPacketSize);
  20117. +
  20118. +#if 0
  20119. + for (i = 0; i < desccount; i++) {
  20120. + CFI_INFO("%s: wTxSize[%d]=0x%04x\n", __func__, i, pVals[i]);
  20121. + }
  20122. + CFI_INFO("%s: epname=%s; mps=%d\n", __func__, ep->ep->ep.name, mps);
  20123. +#endif
  20124. +
  20125. + /* Check the wTxSizes to be less than or equal to the mps */
  20126. + for (i = 0; i < desccount; i++) {
  20127. + if (pVals[i] > mps) {
  20128. + CFI_INFO
  20129. + ("%s: ERROR - the wTxSize[%d] should be <= MPS (wTxSize=%d)\n",
  20130. + __func__, i, pVals[i]);
  20131. + return -DWC_E_INVALID;
  20132. + }
  20133. + }
  20134. +
  20135. + ep->ep->dwc_ep.buff_mode = BM_CONCAT;
  20136. + dwc_memcpy(ep->bm_concat, pConcatValHdr, BS_CONCAT_VAL_HDR_LEN);
  20137. +
  20138. + /* Free the previously allocated storage for the wTxBytes */
  20139. + if (ep->bm_concat->wTxBytes) {
  20140. + DWC_FREE(ep->bm_concat->wTxBytes);
  20141. + }
  20142. +
  20143. + /* Allocate a new storage for the wTxBytes field */
  20144. + ep->bm_concat->wTxBytes =
  20145. + DWC_ALLOC(sizeof(uint16_t) * pConcatValHdr->bDescCount);
  20146. + if (NULL == ep->bm_concat->wTxBytes) {
  20147. + CFI_INFO("%s: Unable to allocate memory\n", __func__);
  20148. + return -DWC_E_NO_MEMORY;
  20149. + }
  20150. +
  20151. + /* Copy the new values into the wTxBytes filed */
  20152. + dwc_memcpy(ep->bm_concat->wTxBytes, buf + BS_CONCAT_VAL_HDR_LEN,
  20153. + sizeof(uint16_t) * pConcatValHdr->bDescCount);
  20154. +
  20155. + return 0;
  20156. +}
  20157. +
  20158. +/**
  20159. + * This function calculates the total of all FIFO sizes
  20160. + *
  20161. + * @param core_if Programming view of DWC_otg controller
  20162. + *
  20163. + * @return The total of data FIFO sizes.
  20164. + *
  20165. + */
  20166. +static uint16_t get_dfifo_size(dwc_otg_core_if_t * core_if)
  20167. +{
  20168. + dwc_otg_core_params_t *params = core_if->core_params;
  20169. + uint16_t dfifo_total = 0;
  20170. + int i;
  20171. +
  20172. + /* The shared RxFIFO size */
  20173. + dfifo_total =
  20174. + params->dev_rx_fifo_size + params->dev_nperio_tx_fifo_size;
  20175. +
  20176. + /* Add up each TxFIFO size to the total */
  20177. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  20178. + dfifo_total += params->dev_tx_fifo_size[i];
  20179. + }
  20180. +
  20181. + return dfifo_total;
  20182. +}
  20183. +
  20184. +/**
  20185. + * This function returns Rx FIFO size
  20186. + *
  20187. + * @param core_if Programming view of DWC_otg controller
  20188. + *
  20189. + * @return The total of data FIFO sizes.
  20190. + *
  20191. + */
  20192. +static int32_t get_rxfifo_size(dwc_otg_core_if_t * core_if, uint16_t wValue)
  20193. +{
  20194. + switch (wValue >> 8) {
  20195. + case 0:
  20196. + return (core_if->pwron_rxfsiz <
  20197. + 32768) ? core_if->pwron_rxfsiz : 32768;
  20198. + break;
  20199. + case 1:
  20200. + return core_if->core_params->dev_rx_fifo_size;
  20201. + break;
  20202. + default:
  20203. + return -DWC_E_INVALID;
  20204. + break;
  20205. + }
  20206. +}
  20207. +
  20208. +/**
  20209. + * This function returns Tx FIFO size for IN EP
  20210. + *
  20211. + * @param core_if Programming view of DWC_otg controller
  20212. + *
  20213. + * @return The total of data FIFO sizes.
  20214. + *
  20215. + */
  20216. +static int32_t get_txfifo_size(struct dwc_otg_pcd *pcd, uint16_t wValue)
  20217. +{
  20218. + dwc_otg_pcd_ep_t *ep;
  20219. +
  20220. + ep = get_ep_by_addr(pcd, wValue & 0xff);
  20221. +
  20222. + if (NULL == ep) {
  20223. + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
  20224. + __func__, wValue & 0xff);
  20225. + return -DWC_E_INVALID;
  20226. + }
  20227. +
  20228. + if (!ep->dwc_ep.is_in) {
  20229. + CFI_INFO
  20230. + ("%s: No Tx FIFO assingned to the Out endpoint addr=0x%02x\n",
  20231. + __func__, wValue & 0xff);
  20232. + return -DWC_E_INVALID;
  20233. + }
  20234. +
  20235. + switch (wValue >> 8) {
  20236. + case 0:
  20237. + return (GET_CORE_IF(pcd)->pwron_txfsiz
  20238. + [ep->dwc_ep.tx_fifo_num - 1] <
  20239. + 768) ? GET_CORE_IF(pcd)->pwron_txfsiz[ep->
  20240. + dwc_ep.tx_fifo_num
  20241. + - 1] : 32768;
  20242. + break;
  20243. + case 1:
  20244. + return GET_CORE_IF(pcd)->core_params->
  20245. + dev_tx_fifo_size[ep->dwc_ep.num - 1];
  20246. + break;
  20247. + default:
  20248. + return -DWC_E_INVALID;
  20249. + break;
  20250. + }
  20251. +}
  20252. +
  20253. +/**
  20254. + * This function checks if the submitted combination of
  20255. + * device mode FIFO sizes is possible or not.
  20256. + *
  20257. + * @param core_if Programming view of DWC_otg controller
  20258. + *
  20259. + * @return 1 if possible, 0 otherwise.
  20260. + *
  20261. + */
  20262. +static uint8_t check_fifo_sizes(dwc_otg_core_if_t * core_if)
  20263. +{
  20264. + uint16_t dfifo_actual = 0;
  20265. + dwc_otg_core_params_t *params = core_if->core_params;
  20266. + uint16_t start_addr = 0;
  20267. + int i;
  20268. +
  20269. + dfifo_actual =
  20270. + params->dev_rx_fifo_size + params->dev_nperio_tx_fifo_size;
  20271. +
  20272. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  20273. + dfifo_actual += params->dev_tx_fifo_size[i];
  20274. + }
  20275. +
  20276. + if (dfifo_actual > core_if->total_fifo_size) {
  20277. + return 0;
  20278. + }
  20279. +
  20280. + if (params->dev_rx_fifo_size > 32768 || params->dev_rx_fifo_size < 16)
  20281. + return 0;
  20282. +
  20283. + if (params->dev_nperio_tx_fifo_size > 32768
  20284. + || params->dev_nperio_tx_fifo_size < 16)
  20285. + return 0;
  20286. +
  20287. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  20288. +
  20289. + if (params->dev_tx_fifo_size[i] > 768
  20290. + || params->dev_tx_fifo_size[i] < 4)
  20291. + return 0;
  20292. + }
  20293. +
  20294. + if (params->dev_rx_fifo_size > core_if->pwron_rxfsiz)
  20295. + return 0;
  20296. + start_addr = params->dev_rx_fifo_size;
  20297. +
  20298. + if (params->dev_nperio_tx_fifo_size > core_if->pwron_gnptxfsiz)
  20299. + return 0;
  20300. + start_addr += params->dev_nperio_tx_fifo_size;
  20301. +
  20302. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  20303. +
  20304. + if (params->dev_tx_fifo_size[i] > core_if->pwron_txfsiz[i])
  20305. + return 0;
  20306. + start_addr += params->dev_tx_fifo_size[i];
  20307. + }
  20308. +
  20309. + return 1;
  20310. +}
  20311. +
  20312. +/**
  20313. + * This function resizes Device mode FIFOs
  20314. + *
  20315. + * @param core_if Programming view of DWC_otg controller
  20316. + *
  20317. + * @return 1 if successful, 0 otherwise
  20318. + *
  20319. + */
  20320. +static uint8_t resize_fifos(dwc_otg_core_if_t * core_if)
  20321. +{
  20322. + int i = 0;
  20323. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  20324. + dwc_otg_core_params_t *params = core_if->core_params;
  20325. + uint32_t rx_fifo_size;
  20326. + fifosize_data_t nptxfifosize;
  20327. + fifosize_data_t txfifosize[15];
  20328. +
  20329. + uint32_t rx_fsz_bak;
  20330. + uint32_t nptxfsz_bak;
  20331. + uint32_t txfsz_bak[15];
  20332. +
  20333. + uint16_t start_address;
  20334. + uint8_t retval = 1;
  20335. +
  20336. + if (!check_fifo_sizes(core_if)) {
  20337. + return 0;
  20338. + }
  20339. +
  20340. + /* Configure data FIFO sizes */
  20341. + if (core_if->hwcfg2.b.dynamic_fifo && params->enable_dynamic_fifo) {
  20342. + rx_fsz_bak = DWC_READ_REG32(&global_regs->grxfsiz);
  20343. + rx_fifo_size = params->dev_rx_fifo_size;
  20344. + DWC_WRITE_REG32(&global_regs->grxfsiz, rx_fifo_size);
  20345. +
  20346. + /*
  20347. + * Tx FIFOs These FIFOs are numbered from 1 to 15.
  20348. + * Indexes of the FIFO size module parameters in the
  20349. + * dev_tx_fifo_size array and the FIFO size registers in
  20350. + * the dtxfsiz array run from 0 to 14.
  20351. + */
  20352. +
  20353. + /* Non-periodic Tx FIFO */
  20354. + nptxfsz_bak = DWC_READ_REG32(&global_regs->gnptxfsiz);
  20355. + nptxfifosize.b.depth = params->dev_nperio_tx_fifo_size;
  20356. + start_address = params->dev_rx_fifo_size;
  20357. + nptxfifosize.b.startaddr = start_address;
  20358. +
  20359. + DWC_WRITE_REG32(&global_regs->gnptxfsiz, nptxfifosize.d32);
  20360. +
  20361. + start_address += nptxfifosize.b.depth;
  20362. +
  20363. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  20364. + txfsz_bak[i] = DWC_READ_REG32(&global_regs->dtxfsiz[i]);
  20365. +
  20366. + txfifosize[i].b.depth = params->dev_tx_fifo_size[i];
  20367. + txfifosize[i].b.startaddr = start_address;
  20368. + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
  20369. + txfifosize[i].d32);
  20370. +
  20371. + start_address += txfifosize[i].b.depth;
  20372. + }
  20373. +
  20374. + /** Check if register values are set correctly */
  20375. + if (rx_fifo_size != DWC_READ_REG32(&global_regs->grxfsiz)) {
  20376. + retval = 0;
  20377. + }
  20378. +
  20379. + if (nptxfifosize.d32 != DWC_READ_REG32(&global_regs->gnptxfsiz)) {
  20380. + retval = 0;
  20381. + }
  20382. +
  20383. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  20384. + if (txfifosize[i].d32 !=
  20385. + DWC_READ_REG32(&global_regs->dtxfsiz[i])) {
  20386. + retval = 0;
  20387. + }
  20388. + }
  20389. +
  20390. + /** If register values are not set correctly, reset old values */
  20391. + if (retval == 0) {
  20392. + DWC_WRITE_REG32(&global_regs->grxfsiz, rx_fsz_bak);
  20393. +
  20394. + /* Non-periodic Tx FIFO */
  20395. + DWC_WRITE_REG32(&global_regs->gnptxfsiz, nptxfsz_bak);
  20396. +
  20397. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  20398. + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
  20399. + txfsz_bak[i]);
  20400. + }
  20401. + }
  20402. + } else {
  20403. + return 0;
  20404. + }
  20405. +
  20406. + /* Flush the FIFOs */
  20407. + dwc_otg_flush_tx_fifo(core_if, 0x10); /* all Tx FIFOs */
  20408. + dwc_otg_flush_rx_fifo(core_if);
  20409. +
  20410. + return retval;
  20411. +}
  20412. +
  20413. +/**
  20414. + * This function sets a new value for the buffer Alignment setup.
  20415. + */
  20416. +static int cfi_ep_set_tx_fifo_val(uint8_t * buf, dwc_otg_pcd_t * pcd)
  20417. +{
  20418. + int retval;
  20419. + uint32_t fsiz;
  20420. + uint16_t size;
  20421. + uint16_t ep_addr;
  20422. + dwc_otg_pcd_ep_t *ep;
  20423. + dwc_otg_core_params_t *params = GET_CORE_IF(pcd)->core_params;
  20424. + tx_fifo_size_setup_t *ptxfifoval;
  20425. +
  20426. + ptxfifoval = (tx_fifo_size_setup_t *) buf;
  20427. + ep_addr = ptxfifoval->bEndpointAddress;
  20428. + size = ptxfifoval->wDepth;
  20429. +
  20430. + ep = get_ep_by_addr(pcd, ep_addr);
  20431. +
  20432. + CFI_INFO
  20433. + ("%s: Set Tx FIFO size: endpoint addr=0x%02x, depth=%d, FIFO Num=%d\n",
  20434. + __func__, ep_addr, size, ep->dwc_ep.tx_fifo_num);
  20435. +
  20436. + if (NULL == ep) {
  20437. + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
  20438. + __func__, ep_addr);
  20439. + return -DWC_E_INVALID;
  20440. + }
  20441. +
  20442. + fsiz = params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1];
  20443. + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1] = size;
  20444. +
  20445. + if (resize_fifos(GET_CORE_IF(pcd))) {
  20446. + retval = 0;
  20447. + } else {
  20448. + CFI_INFO
  20449. + ("%s: Error setting the feature Tx FIFO Size for EP%d\n",
  20450. + __func__, ep_addr);
  20451. + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1] = fsiz;
  20452. + retval = -DWC_E_INVALID;
  20453. + }
  20454. +
  20455. + return retval;
  20456. +}
  20457. +
  20458. +/**
  20459. + * This function sets a new value for the buffer Alignment setup.
  20460. + */
  20461. +static int cfi_set_rx_fifo_val(uint8_t * buf, dwc_otg_pcd_t * pcd)
  20462. +{
  20463. + int retval;
  20464. + uint32_t fsiz;
  20465. + uint16_t size;
  20466. + dwc_otg_core_params_t *params = GET_CORE_IF(pcd)->core_params;
  20467. + rx_fifo_size_setup_t *prxfifoval;
  20468. +
  20469. + prxfifoval = (rx_fifo_size_setup_t *) buf;
  20470. + size = prxfifoval->wDepth;
  20471. +
  20472. + fsiz = params->dev_rx_fifo_size;
  20473. + params->dev_rx_fifo_size = size;
  20474. +
  20475. + if (resize_fifos(GET_CORE_IF(pcd))) {
  20476. + retval = 0;
  20477. + } else {
  20478. + CFI_INFO("%s: Error setting the feature Rx FIFO Size\n",
  20479. + __func__);
  20480. + params->dev_rx_fifo_size = fsiz;
  20481. + retval = -DWC_E_INVALID;
  20482. + }
  20483. +
  20484. + return retval;
  20485. +}
  20486. +
  20487. +/**
  20488. + * This function reads the SG of an EP's buffer setup into the buffer buf
  20489. + */
  20490. +static int cfi_ep_get_sg_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  20491. + struct cfi_usb_ctrlrequest *req)
  20492. +{
  20493. + int retval = -DWC_E_INVALID;
  20494. + uint8_t addr;
  20495. + cfi_ep_t *ep;
  20496. +
  20497. + /* The Low Byte of the wValue contains a non-zero address of the endpoint */
  20498. + addr = req->wValue & 0xFF;
  20499. + if (addr == 0) /* The address should be non-zero */
  20500. + return retval;
  20501. +
  20502. + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
  20503. + if (NULL == ep) {
  20504. + CFI_INFO("%s: Unable to get the endpoint address(0x%02x)\n",
  20505. + __func__, addr);
  20506. + return retval;
  20507. + }
  20508. +
  20509. + dwc_memcpy(buf, ep->bm_sg, BS_SG_VAL_DESC_LEN);
  20510. + retval = BS_SG_VAL_DESC_LEN;
  20511. + return retval;
  20512. +}
  20513. +
  20514. +/**
  20515. + * This function reads the Concatenation value of an EP's buffer mode into
  20516. + * the buffer buf
  20517. + */
  20518. +static int cfi_ep_get_concat_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  20519. + struct cfi_usb_ctrlrequest *req)
  20520. +{
  20521. + int retval = -DWC_E_INVALID;
  20522. + uint8_t addr;
  20523. + cfi_ep_t *ep;
  20524. + uint8_t desc_count;
  20525. +
  20526. + /* The Low Byte of the wValue contains a non-zero address of the endpoint */
  20527. + addr = req->wValue & 0xFF;
  20528. + if (addr == 0) /* The address should be non-zero */
  20529. + return retval;
  20530. +
  20531. + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
  20532. + if (NULL == ep) {
  20533. + CFI_INFO("%s: Unable to get the endpoint address(0x%02x)\n",
  20534. + __func__, addr);
  20535. + return retval;
  20536. + }
  20537. +
  20538. + /* Copy the header to the buffer */
  20539. + dwc_memcpy(buf, ep->bm_concat, BS_CONCAT_VAL_HDR_LEN);
  20540. + /* Advance the buffer pointer by the header size */
  20541. + buf += BS_CONCAT_VAL_HDR_LEN;
  20542. +
  20543. + desc_count = ep->bm_concat->hdr.bDescCount;
  20544. + /* Copy alll the wTxBytes to the buffer */
  20545. + dwc_memcpy(buf, ep->bm_concat->wTxBytes, sizeof(uid16_t) * desc_count);
  20546. +
  20547. + retval = BS_CONCAT_VAL_HDR_LEN + sizeof(uid16_t) * desc_count;
  20548. + return retval;
  20549. +}
  20550. +
  20551. +/**
  20552. + * This function reads the buffer Alignment value of an EP's buffer mode into
  20553. + * the buffer buf
  20554. + *
  20555. + * @return The total number of bytes copied to the buffer or negative error code.
  20556. + */
  20557. +static int cfi_ep_get_align_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  20558. + struct cfi_usb_ctrlrequest *req)
  20559. +{
  20560. + int retval = -DWC_E_INVALID;
  20561. + uint8_t addr;
  20562. + cfi_ep_t *ep;
  20563. +
  20564. + /* The Low Byte of the wValue contains a non-zero address of the endpoint */
  20565. + addr = req->wValue & 0xFF;
  20566. + if (addr == 0) /* The address should be non-zero */
  20567. + return retval;
  20568. +
  20569. + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
  20570. + if (NULL == ep) {
  20571. + CFI_INFO("%s: Unable to get the endpoint address(0x%02x)\n",
  20572. + __func__, addr);
  20573. + return retval;
  20574. + }
  20575. +
  20576. + dwc_memcpy(buf, ep->bm_align, BS_ALIGN_VAL_HDR_LEN);
  20577. + retval = BS_ALIGN_VAL_HDR_LEN;
  20578. +
  20579. + return retval;
  20580. +}
  20581. +
  20582. +/**
  20583. + * This function sets a new value for the specified feature
  20584. + *
  20585. + * @param pcd A pointer to the PCD object
  20586. + *
  20587. + * @return 0 if successful, negative error code otherwise to stall the DCE.
  20588. + */
  20589. +static int cfi_set_feature_value(struct dwc_otg_pcd *pcd)
  20590. +{
  20591. + int retval = -DWC_E_NOT_SUPPORTED;
  20592. + uint16_t wIndex, wValue;
  20593. + uint8_t bRequest;
  20594. + struct dwc_otg_core_if *coreif;
  20595. + cfiobject_t *cfi = pcd->cfi;
  20596. + struct cfi_usb_ctrlrequest *ctrl_req;
  20597. + uint8_t *buf;
  20598. + ctrl_req = &cfi->ctrl_req;
  20599. +
  20600. + buf = pcd->cfi->ctrl_req.data;
  20601. +
  20602. + coreif = GET_CORE_IF(pcd);
  20603. + bRequest = ctrl_req->bRequest;
  20604. + wIndex = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wIndex);
  20605. + wValue = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wValue);
  20606. +
  20607. + /* See which feature is to be modified */
  20608. + switch (wIndex) {
  20609. + case FT_ID_DMA_BUFFER_SETUP:
  20610. + /* Modify the feature */
  20611. + if ((retval = cfi_ep_set_sg_val(buf, pcd)) < 0)
  20612. + return retval;
  20613. +
  20614. + /* And send this request to the gadget */
  20615. + cfi->need_gadget_att = 1;
  20616. + break;
  20617. +
  20618. + case FT_ID_DMA_BUFF_ALIGN:
  20619. + if ((retval = cfi_ep_set_alignment_val(buf, pcd)) < 0)
  20620. + return retval;
  20621. + cfi->need_gadget_att = 1;
  20622. + break;
  20623. +
  20624. + case FT_ID_DMA_CONCAT_SETUP:
  20625. + /* Modify the feature */
  20626. + if ((retval = cfi_ep_set_concat_val(buf, pcd)) < 0)
  20627. + return retval;
  20628. + cfi->need_gadget_att = 1;
  20629. + break;
  20630. +
  20631. + case FT_ID_DMA_CIRCULAR:
  20632. + CFI_INFO("FT_ID_DMA_CIRCULAR\n");
  20633. + break;
  20634. +
  20635. + case FT_ID_THRESHOLD_SETUP:
  20636. + CFI_INFO("FT_ID_THRESHOLD_SETUP\n");
  20637. + break;
  20638. +
  20639. + case FT_ID_DFIFO_DEPTH:
  20640. + CFI_INFO("FT_ID_DFIFO_DEPTH\n");
  20641. + break;
  20642. +
  20643. + case FT_ID_TX_FIFO_DEPTH:
  20644. + CFI_INFO("FT_ID_TX_FIFO_DEPTH\n");
  20645. + if ((retval = cfi_ep_set_tx_fifo_val(buf, pcd)) < 0)
  20646. + return retval;
  20647. + cfi->need_gadget_att = 0;
  20648. + break;
  20649. +
  20650. + case FT_ID_RX_FIFO_DEPTH:
  20651. + CFI_INFO("FT_ID_RX_FIFO_DEPTH\n");
  20652. + if ((retval = cfi_set_rx_fifo_val(buf, pcd)) < 0)
  20653. + return retval;
  20654. + cfi->need_gadget_att = 0;
  20655. + break;
  20656. + }
  20657. +
  20658. + return retval;
  20659. +}
  20660. +
  20661. +#endif //DWC_UTE_CFI
  20662. --- /dev/null
  20663. +++ b/drivers/usb/host/dwc_otg/dwc_otg_cfi.h
  20664. @@ -0,0 +1,320 @@
  20665. +/* ==========================================================================
  20666. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  20667. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  20668. + * otherwise expressly agreed to in writing between Synopsys and you.
  20669. + *
  20670. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  20671. + * any End User Software License Agreement or Agreement for Licensed Product
  20672. + * with Synopsys or any supplement thereto. You are permitted to use and
  20673. + * redistribute this Software in source and binary forms, with or without
  20674. + * modification, provided that redistributions of source code must retain this
  20675. + * notice. You may not view, use, disclose, copy or distribute this file or
  20676. + * any information contained herein except pursuant to this license grant from
  20677. + * Synopsys. If you do not agree with this notice, including the disclaimer
  20678. + * below, then you are not authorized to use the Software.
  20679. + *
  20680. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  20681. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20682. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20683. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  20684. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20685. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  20686. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20687. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  20688. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  20689. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  20690. + * DAMAGE.
  20691. + * ========================================================================== */
  20692. +
  20693. +#if !defined(__DWC_OTG_CFI_H__)
  20694. +#define __DWC_OTG_CFI_H__
  20695. +
  20696. +#include "dwc_otg_pcd.h"
  20697. +#include "dwc_cfi_common.h"
  20698. +
  20699. +/**
  20700. + * @file
  20701. + * This file contains the CFI related OTG PCD specific common constants,
  20702. + * interfaces(functions and macros) and data structures.The CFI Protocol is an
  20703. + * optional interface for internal testing purposes that a DUT may implement to
  20704. + * support testing of configurable features.
  20705. + *
  20706. + */
  20707. +
  20708. +struct dwc_otg_pcd;
  20709. +struct dwc_otg_pcd_ep;
  20710. +
  20711. +/** OTG CFI Features (properties) ID constants */
  20712. +/** This is a request for all Core Features */
  20713. +#define FT_ID_DMA_MODE 0x0001
  20714. +#define FT_ID_DMA_BUFFER_SETUP 0x0002
  20715. +#define FT_ID_DMA_BUFF_ALIGN 0x0003
  20716. +#define FT_ID_DMA_CONCAT_SETUP 0x0004
  20717. +#define FT_ID_DMA_CIRCULAR 0x0005
  20718. +#define FT_ID_THRESHOLD_SETUP 0x0006
  20719. +#define FT_ID_DFIFO_DEPTH 0x0007
  20720. +#define FT_ID_TX_FIFO_DEPTH 0x0008
  20721. +#define FT_ID_RX_FIFO_DEPTH 0x0009
  20722. +
  20723. +/**********************************************************/
  20724. +#define CFI_INFO_DEF
  20725. +
  20726. +#ifdef CFI_INFO_DEF
  20727. +#define CFI_INFO(fmt...) DWC_PRINTF("CFI: " fmt);
  20728. +#else
  20729. +#define CFI_INFO(fmt...)
  20730. +#endif
  20731. +
  20732. +#define min(x,y) ({ \
  20733. + x < y ? x : y; })
  20734. +
  20735. +#define max(x,y) ({ \
  20736. + x > y ? x : y; })
  20737. +
  20738. +/**
  20739. + * Descriptor DMA SG Buffer setup structure (SG buffer). This structure is
  20740. + * also used for setting up a buffer for Circular DDMA.
  20741. + */
  20742. +struct _ddma_sg_buffer_setup {
  20743. +#define BS_SG_VAL_DESC_LEN 6
  20744. + /* The OUT EP address */
  20745. + uint8_t bOutEndpointAddress;
  20746. + /* The IN EP address */
  20747. + uint8_t bInEndpointAddress;
  20748. + /* Number of bytes to put between transfer segments (must be DWORD boundaries) */
  20749. + uint8_t bOffset;
  20750. + /* The number of transfer segments (a DMA descriptors per each segment) */
  20751. + uint8_t bCount;
  20752. + /* Size (in byte) of each transfer segment */
  20753. + uint16_t wSize;
  20754. +} __attribute__ ((packed));
  20755. +typedef struct _ddma_sg_buffer_setup ddma_sg_buffer_setup_t;
  20756. +
  20757. +/** Descriptor DMA Concatenation Buffer setup structure */
  20758. +struct _ddma_concat_buffer_setup_hdr {
  20759. +#define BS_CONCAT_VAL_HDR_LEN 4
  20760. + /* The endpoint for which the buffer is to be set up */
  20761. + uint8_t bEndpointAddress;
  20762. + /* The count of descriptors to be used */
  20763. + uint8_t bDescCount;
  20764. + /* The total size of the transfer */
  20765. + uint16_t wSize;
  20766. +} __attribute__ ((packed));
  20767. +typedef struct _ddma_concat_buffer_setup_hdr ddma_concat_buffer_setup_hdr_t;
  20768. +
  20769. +/** Descriptor DMA Concatenation Buffer setup structure */
  20770. +struct _ddma_concat_buffer_setup {
  20771. + /* The SG header */
  20772. + ddma_concat_buffer_setup_hdr_t hdr;
  20773. +
  20774. + /* The XFER sizes pointer (allocated dynamically) */
  20775. + uint16_t *wTxBytes;
  20776. +} __attribute__ ((packed));
  20777. +typedef struct _ddma_concat_buffer_setup ddma_concat_buffer_setup_t;
  20778. +
  20779. +/** Descriptor DMA Alignment Buffer setup structure */
  20780. +struct _ddma_align_buffer_setup {
  20781. +#define BS_ALIGN_VAL_HDR_LEN 2
  20782. + uint8_t bEndpointAddress;
  20783. + uint8_t bAlign;
  20784. +} __attribute__ ((packed));
  20785. +typedef struct _ddma_align_buffer_setup ddma_align_buffer_setup_t;
  20786. +
  20787. +/** Transmit FIFO Size setup structure */
  20788. +struct _tx_fifo_size_setup {
  20789. + uint8_t bEndpointAddress;
  20790. + uint16_t wDepth;
  20791. +} __attribute__ ((packed));
  20792. +typedef struct _tx_fifo_size_setup tx_fifo_size_setup_t;
  20793. +
  20794. +/** Transmit FIFO Size setup structure */
  20795. +struct _rx_fifo_size_setup {
  20796. + uint16_t wDepth;
  20797. +} __attribute__ ((packed));
  20798. +typedef struct _rx_fifo_size_setup rx_fifo_size_setup_t;
  20799. +
  20800. +/**
  20801. + * struct cfi_usb_ctrlrequest - the CFI implementation of the struct usb_ctrlrequest
  20802. + * This structure encapsulates the standard usb_ctrlrequest and adds a pointer
  20803. + * to the data returned in the data stage of a 3-stage Control Write requests.
  20804. + */
  20805. +struct cfi_usb_ctrlrequest {
  20806. + uint8_t bRequestType;
  20807. + uint8_t bRequest;
  20808. + uint16_t wValue;
  20809. + uint16_t wIndex;
  20810. + uint16_t wLength;
  20811. + uint8_t *data;
  20812. +} UPACKED;
  20813. +
  20814. +/*---------------------------------------------------------------------------*/
  20815. +
  20816. +/**
  20817. + * The CFI wrapper of the enabled and activated dwc_otg_pcd_ep structures.
  20818. + * This structure is used to store the buffer setup data for any
  20819. + * enabled endpoint in the PCD.
  20820. + */
  20821. +struct cfi_ep {
  20822. + /* Entry for the list container */
  20823. + dwc_list_link_t lh;
  20824. + /* Pointer to the active PCD endpoint structure */
  20825. + struct dwc_otg_pcd_ep *ep;
  20826. + /* The last descriptor in the chain of DMA descriptors of the endpoint */
  20827. + struct dwc_otg_dma_desc *dma_desc_last;
  20828. + /* The SG feature value */
  20829. + ddma_sg_buffer_setup_t *bm_sg;
  20830. + /* The Circular feature value */
  20831. + ddma_sg_buffer_setup_t *bm_circ;
  20832. + /* The Concatenation feature value */
  20833. + ddma_concat_buffer_setup_t *bm_concat;
  20834. + /* The Alignment feature value */
  20835. + ddma_align_buffer_setup_t *bm_align;
  20836. + /* XFER length */
  20837. + uint32_t xfer_len;
  20838. + /*
  20839. + * Count of DMA descriptors currently used.
  20840. + * The total should not exceed the MAX_DMA_DESCS_PER_EP value
  20841. + * defined in the dwc_otg_cil.h
  20842. + */
  20843. + uint32_t desc_count;
  20844. +};
  20845. +typedef struct cfi_ep cfi_ep_t;
  20846. +
  20847. +typedef struct cfi_dma_buff {
  20848. +#define CFI_IN_BUF_LEN 1024
  20849. +#define CFI_OUT_BUF_LEN 1024
  20850. + dma_addr_t addr;
  20851. + uint8_t *buf;
  20852. +} cfi_dma_buff_t;
  20853. +
  20854. +struct cfiobject;
  20855. +
  20856. +/**
  20857. + * This is the interface for the CFI operations.
  20858. + *
  20859. + * @param ep_enable Called when any endpoint is enabled and activated.
  20860. + * @param release Called when the CFI object is released and it needs to correctly
  20861. + * deallocate the dynamic memory
  20862. + * @param ctrl_write_complete Called when the data stage of the request is complete
  20863. + */
  20864. +typedef struct cfi_ops {
  20865. + int (*ep_enable) (struct cfiobject * cfi, struct dwc_otg_pcd * pcd,
  20866. + struct dwc_otg_pcd_ep * ep);
  20867. + void *(*ep_alloc_buf) (struct cfiobject * cfi, struct dwc_otg_pcd * pcd,
  20868. + struct dwc_otg_pcd_ep * ep, dma_addr_t * dma,
  20869. + unsigned size, gfp_t flags);
  20870. + void (*release) (struct cfiobject * cfi);
  20871. + int (*ctrl_write_complete) (struct cfiobject * cfi,
  20872. + struct dwc_otg_pcd * pcd);
  20873. + void (*build_descriptors) (struct cfiobject * cfi,
  20874. + struct dwc_otg_pcd * pcd,
  20875. + struct dwc_otg_pcd_ep * ep,
  20876. + dwc_otg_pcd_request_t * req);
  20877. +} cfi_ops_t;
  20878. +
  20879. +struct cfiobject {
  20880. + cfi_ops_t ops;
  20881. + struct dwc_otg_pcd *pcd;
  20882. + struct usb_gadget *gadget;
  20883. +
  20884. + /* Buffers used to send/receive CFI-related request data */
  20885. + cfi_dma_buff_t buf_in;
  20886. + cfi_dma_buff_t buf_out;
  20887. +
  20888. + /* CFI specific Control request wrapper */
  20889. + struct cfi_usb_ctrlrequest ctrl_req;
  20890. +
  20891. + /* The list of active EP's in the PCD of type cfi_ep_t */
  20892. + dwc_list_link_t active_eps;
  20893. +
  20894. + /* This flag shall control the propagation of a specific request
  20895. + * to the gadget's processing routines.
  20896. + * 0 - no gadget handling
  20897. + * 1 - the gadget needs to know about this request (w/o completing a status
  20898. + * phase - just return a 0 to the _setup callback)
  20899. + */
  20900. + uint8_t need_gadget_att;
  20901. +
  20902. + /* Flag indicating whether the status IN phase needs to be
  20903. + * completed by the PCD
  20904. + */
  20905. + uint8_t need_status_in_complete;
  20906. +};
  20907. +typedef struct cfiobject cfiobject_t;
  20908. +
  20909. +#define DUMP_MSG
  20910. +
  20911. +#if defined(DUMP_MSG)
  20912. +static inline void dump_msg(const u8 * buf, unsigned int length)
  20913. +{
  20914. + unsigned int start, num, i;
  20915. + char line[52], *p;
  20916. +
  20917. + if (length >= 512)
  20918. + return;
  20919. +
  20920. + start = 0;
  20921. + while (length > 0) {
  20922. + num = min(length, 16u);
  20923. + p = line;
  20924. + for (i = 0; i < num; ++i) {
  20925. + if (i == 8)
  20926. + *p++ = ' ';
  20927. + DWC_SPRINTF(p, " %02x", buf[i]);
  20928. + p += 3;
  20929. + }
  20930. + *p = 0;
  20931. + DWC_DEBUG("%6x: %s\n", start, line);
  20932. + buf += num;
  20933. + start += num;
  20934. + length -= num;
  20935. + }
  20936. +}
  20937. +#else
  20938. +static inline void dump_msg(const u8 * buf, unsigned int length)
  20939. +{
  20940. +}
  20941. +#endif
  20942. +
  20943. +/**
  20944. + * This function returns a pointer to cfi_ep_t object with the addr address.
  20945. + */
  20946. +static inline struct cfi_ep *get_cfi_ep_by_addr(struct cfiobject *cfi,
  20947. + uint8_t addr)
  20948. +{
  20949. + struct cfi_ep *pcfiep;
  20950. + dwc_list_link_t *tmp;
  20951. +
  20952. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  20953. + pcfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  20954. +
  20955. + if (pcfiep->ep->desc->bEndpointAddress == addr) {
  20956. + return pcfiep;
  20957. + }
  20958. + }
  20959. +
  20960. + return NULL;
  20961. +}
  20962. +
  20963. +/**
  20964. + * This function returns a pointer to cfi_ep_t object that matches
  20965. + * the dwc_otg_pcd_ep object.
  20966. + */
  20967. +static inline struct cfi_ep *get_cfi_ep_by_pcd_ep(struct cfiobject *cfi,
  20968. + struct dwc_otg_pcd_ep *ep)
  20969. +{
  20970. + struct cfi_ep *pcfiep = NULL;
  20971. + dwc_list_link_t *tmp;
  20972. +
  20973. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  20974. + pcfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  20975. + if (pcfiep->ep == ep) {
  20976. + return pcfiep;
  20977. + }
  20978. + }
  20979. + return NULL;
  20980. +}
  20981. +
  20982. +int cfi_setup(struct dwc_otg_pcd *pcd, struct cfi_usb_ctrlrequest *ctrl);
  20983. +
  20984. +#endif /* (__DWC_OTG_CFI_H__) */
  20985. --- /dev/null
  20986. +++ b/drivers/usb/host/dwc_otg/dwc_otg_cil.c
  20987. @@ -0,0 +1,7146 @@
  20988. +/* ==========================================================================
  20989. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_cil.c $
  20990. + * $Revision: #191 $
  20991. + * $Date: 2012/08/10 $
  20992. + * $Change: 2047372 $
  20993. + *
  20994. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  20995. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  20996. + * otherwise expressly agreed to in writing between Synopsys and you.
  20997. + *
  20998. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  20999. + * any End User Software License Agreement or Agreement for Licensed Product
  21000. + * with Synopsys or any supplement thereto. You are permitted to use and
  21001. + * redistribute this Software in source and binary forms, with or without
  21002. + * modification, provided that redistributions of source code must retain this
  21003. + * notice. You may not view, use, disclose, copy or distribute this file or
  21004. + * any information contained herein except pursuant to this license grant from
  21005. + * Synopsys. If you do not agree with this notice, including the disclaimer
  21006. + * below, then you are not authorized to use the Software.
  21007. + *
  21008. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  21009. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21010. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21011. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  21012. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21013. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21014. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  21015. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21016. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  21017. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  21018. + * DAMAGE.
  21019. + * ========================================================================== */
  21020. +
  21021. +/** @file
  21022. + *
  21023. + * The Core Interface Layer provides basic services for accessing and
  21024. + * managing the DWC_otg hardware. These services are used by both the
  21025. + * Host Controller Driver and the Peripheral Controller Driver.
  21026. + *
  21027. + * The CIL manages the memory map for the core so that the HCD and PCD
  21028. + * don't have to do this separately. It also handles basic tasks like
  21029. + * reading/writing the registers and data FIFOs in the controller.
  21030. + * Some of the data access functions provide encapsulation of several
  21031. + * operations required to perform a task, such as writing multiple
  21032. + * registers to start a transfer. Finally, the CIL performs basic
  21033. + * services that are not specific to either the host or device modes
  21034. + * of operation. These services include management of the OTG Host
  21035. + * Negotiation Protocol (HNP) and Session Request Protocol (SRP). A
  21036. + * Diagnostic API is also provided to allow testing of the controller
  21037. + * hardware.
  21038. + *
  21039. + * The Core Interface Layer has the following requirements:
  21040. + * - Provides basic controller operations.
  21041. + * - Minimal use of OS services.
  21042. + * - The OS services used will be abstracted by using inline functions
  21043. + * or macros.
  21044. + *
  21045. + */
  21046. +
  21047. +#include "dwc_os.h"
  21048. +#include "dwc_otg_regs.h"
  21049. +#include "dwc_otg_cil.h"
  21050. +
  21051. +extern bool cil_force_host;
  21052. +
  21053. +static int dwc_otg_setup_params(dwc_otg_core_if_t * core_if);
  21054. +
  21055. +/**
  21056. + * This function is called to initialize the DWC_otg CSR data
  21057. + * structures. The register addresses in the device and host
  21058. + * structures are initialized from the base address supplied by the
  21059. + * caller. The calling function must make the OS calls to get the
  21060. + * base address of the DWC_otg controller registers. The core_params
  21061. + * argument holds the parameters that specify how the core should be
  21062. + * configured.
  21063. + *
  21064. + * @param reg_base_addr Base address of DWC_otg core registers
  21065. + *
  21066. + */
  21067. +dwc_otg_core_if_t *dwc_otg_cil_init(const uint32_t * reg_base_addr)
  21068. +{
  21069. + dwc_otg_core_if_t *core_if = 0;
  21070. + dwc_otg_dev_if_t *dev_if = 0;
  21071. + dwc_otg_host_if_t *host_if = 0;
  21072. + uint8_t *reg_base = (uint8_t *) reg_base_addr;
  21073. + int i = 0;
  21074. +
  21075. + DWC_DEBUGPL(DBG_CILV, "%s(%p)\n", __func__, reg_base_addr);
  21076. +
  21077. + core_if = DWC_ALLOC(sizeof(dwc_otg_core_if_t));
  21078. +
  21079. + if (core_if == NULL) {
  21080. + DWC_DEBUGPL(DBG_CIL,
  21081. + "Allocation of dwc_otg_core_if_t failed\n");
  21082. + return 0;
  21083. + }
  21084. + core_if->core_global_regs = (dwc_otg_core_global_regs_t *) reg_base;
  21085. +
  21086. + /*
  21087. + * Allocate the Device Mode structures.
  21088. + */
  21089. + dev_if = DWC_ALLOC(sizeof(dwc_otg_dev_if_t));
  21090. +
  21091. + if (dev_if == NULL) {
  21092. + DWC_DEBUGPL(DBG_CIL, "Allocation of dwc_otg_dev_if_t failed\n");
  21093. + DWC_FREE(core_if);
  21094. + return 0;
  21095. + }
  21096. +
  21097. + dev_if->dev_global_regs =
  21098. + (dwc_otg_device_global_regs_t *) (reg_base +
  21099. + DWC_DEV_GLOBAL_REG_OFFSET);
  21100. +
  21101. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  21102. + dev_if->in_ep_regs[i] = (dwc_otg_dev_in_ep_regs_t *)
  21103. + (reg_base + DWC_DEV_IN_EP_REG_OFFSET +
  21104. + (i * DWC_EP_REG_OFFSET));
  21105. +
  21106. + dev_if->out_ep_regs[i] = (dwc_otg_dev_out_ep_regs_t *)
  21107. + (reg_base + DWC_DEV_OUT_EP_REG_OFFSET +
  21108. + (i * DWC_EP_REG_OFFSET));
  21109. + DWC_DEBUGPL(DBG_CILV, "in_ep_regs[%d]->diepctl=%p\n",
  21110. + i, &dev_if->in_ep_regs[i]->diepctl);
  21111. + DWC_DEBUGPL(DBG_CILV, "out_ep_regs[%d]->doepctl=%p\n",
  21112. + i, &dev_if->out_ep_regs[i]->doepctl);
  21113. + }
  21114. +
  21115. + dev_if->speed = 0; // unknown
  21116. +
  21117. + core_if->dev_if = dev_if;
  21118. +
  21119. + /*
  21120. + * Allocate the Host Mode structures.
  21121. + */
  21122. + host_if = DWC_ALLOC(sizeof(dwc_otg_host_if_t));
  21123. +
  21124. + if (host_if == NULL) {
  21125. + DWC_DEBUGPL(DBG_CIL,
  21126. + "Allocation of dwc_otg_host_if_t failed\n");
  21127. + DWC_FREE(dev_if);
  21128. + DWC_FREE(core_if);
  21129. + return 0;
  21130. + }
  21131. +
  21132. + host_if->host_global_regs = (dwc_otg_host_global_regs_t *)
  21133. + (reg_base + DWC_OTG_HOST_GLOBAL_REG_OFFSET);
  21134. +
  21135. + host_if->hprt0 =
  21136. + (uint32_t *) (reg_base + DWC_OTG_HOST_PORT_REGS_OFFSET);
  21137. +
  21138. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  21139. + host_if->hc_regs[i] = (dwc_otg_hc_regs_t *)
  21140. + (reg_base + DWC_OTG_HOST_CHAN_REGS_OFFSET +
  21141. + (i * DWC_OTG_CHAN_REGS_OFFSET));
  21142. + DWC_DEBUGPL(DBG_CILV, "hc_reg[%d]->hcchar=%p\n",
  21143. + i, &host_if->hc_regs[i]->hcchar);
  21144. + }
  21145. +
  21146. + host_if->num_host_channels = MAX_EPS_CHANNELS;
  21147. + core_if->host_if = host_if;
  21148. +
  21149. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  21150. + core_if->data_fifo[i] =
  21151. + (uint32_t *) (reg_base + DWC_OTG_DATA_FIFO_OFFSET +
  21152. + (i * DWC_OTG_DATA_FIFO_SIZE));
  21153. + DWC_DEBUGPL(DBG_CILV, "data_fifo[%d]=0x%08lx\n",
  21154. + i, (unsigned long)core_if->data_fifo[i]);
  21155. + }
  21156. +
  21157. + core_if->pcgcctl = (uint32_t *) (reg_base + DWC_OTG_PCGCCTL_OFFSET);
  21158. +
  21159. + /* Initiate lx_state to L3 disconnected state */
  21160. + core_if->lx_state = DWC_OTG_L3;
  21161. + /*
  21162. + * Store the contents of the hardware configuration registers here for
  21163. + * easy access later.
  21164. + */
  21165. + core_if->hwcfg1.d32 =
  21166. + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg1);
  21167. + core_if->hwcfg2.d32 =
  21168. + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg2);
  21169. + core_if->hwcfg3.d32 =
  21170. + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg3);
  21171. + core_if->hwcfg4.d32 =
  21172. + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg4);
  21173. +
  21174. + /* Force host mode to get HPTXFSIZ exact power on value */
  21175. + {
  21176. + gusbcfg_data_t gusbcfg = {.d32 = 0 };
  21177. + gusbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  21178. + gusbcfg.b.force_host_mode = 1;
  21179. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gusbcfg.d32);
  21180. + dwc_mdelay(100);
  21181. + core_if->hptxfsiz.d32 =
  21182. + DWC_READ_REG32(&core_if->core_global_regs->hptxfsiz);
  21183. + gusbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  21184. + if (cil_force_host)
  21185. + gusbcfg.b.force_host_mode = 1;
  21186. + else
  21187. + gusbcfg.b.force_host_mode = 0;
  21188. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gusbcfg.d32);
  21189. + dwc_mdelay(100);
  21190. + }
  21191. +
  21192. + DWC_DEBUGPL(DBG_CILV, "hwcfg1=%08x\n", core_if->hwcfg1.d32);
  21193. + DWC_DEBUGPL(DBG_CILV, "hwcfg2=%08x\n", core_if->hwcfg2.d32);
  21194. + DWC_DEBUGPL(DBG_CILV, "hwcfg3=%08x\n", core_if->hwcfg3.d32);
  21195. + DWC_DEBUGPL(DBG_CILV, "hwcfg4=%08x\n", core_if->hwcfg4.d32);
  21196. +
  21197. + core_if->hcfg.d32 =
  21198. + DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
  21199. + core_if->dcfg.d32 =
  21200. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  21201. +
  21202. + DWC_DEBUGPL(DBG_CILV, "hcfg=%08x\n", core_if->hcfg.d32);
  21203. + DWC_DEBUGPL(DBG_CILV, "dcfg=%08x\n", core_if->dcfg.d32);
  21204. +
  21205. + DWC_DEBUGPL(DBG_CILV, "op_mode=%0x\n", core_if->hwcfg2.b.op_mode);
  21206. + DWC_DEBUGPL(DBG_CILV, "arch=%0x\n", core_if->hwcfg2.b.architecture);
  21207. + DWC_DEBUGPL(DBG_CILV, "num_dev_ep=%d\n", core_if->hwcfg2.b.num_dev_ep);
  21208. + DWC_DEBUGPL(DBG_CILV, "num_host_chan=%d\n",
  21209. + core_if->hwcfg2.b.num_host_chan);
  21210. + DWC_DEBUGPL(DBG_CILV, "nonperio_tx_q_depth=0x%0x\n",
  21211. + core_if->hwcfg2.b.nonperio_tx_q_depth);
  21212. + DWC_DEBUGPL(DBG_CILV, "host_perio_tx_q_depth=0x%0x\n",
  21213. + core_if->hwcfg2.b.host_perio_tx_q_depth);
  21214. + DWC_DEBUGPL(DBG_CILV, "dev_token_q_depth=0x%0x\n",
  21215. + core_if->hwcfg2.b.dev_token_q_depth);
  21216. +
  21217. + DWC_DEBUGPL(DBG_CILV, "Total FIFO SZ=%d\n",
  21218. + core_if->hwcfg3.b.dfifo_depth);
  21219. + DWC_DEBUGPL(DBG_CILV, "xfer_size_cntr_width=%0x\n",
  21220. + core_if->hwcfg3.b.xfer_size_cntr_width);
  21221. +
  21222. + /*
  21223. + * Set the SRP sucess bit for FS-I2c
  21224. + */
  21225. + core_if->srp_success = 0;
  21226. + core_if->srp_timer_started = 0;
  21227. +
  21228. + /*
  21229. + * Create new workqueue and init works
  21230. + */
  21231. + core_if->wq_otg = DWC_WORKQ_ALLOC("dwc_otg");
  21232. + if (core_if->wq_otg == 0) {
  21233. + DWC_WARN("DWC_WORKQ_ALLOC failed\n");
  21234. + DWC_FREE(host_if);
  21235. + DWC_FREE(dev_if);
  21236. + DWC_FREE(core_if);
  21237. + return 0;
  21238. + }
  21239. +
  21240. + core_if->snpsid = DWC_READ_REG32(&core_if->core_global_regs->gsnpsid);
  21241. +
  21242. + DWC_PRINTF("Core Release: %x.%x%x%x\n",
  21243. + (core_if->snpsid >> 12 & 0xF),
  21244. + (core_if->snpsid >> 8 & 0xF),
  21245. + (core_if->snpsid >> 4 & 0xF), (core_if->snpsid & 0xF));
  21246. +
  21247. + core_if->wkp_timer = DWC_TIMER_ALLOC("Wake Up Timer",
  21248. + w_wakeup_detected, core_if);
  21249. + if (core_if->wkp_timer == 0) {
  21250. + DWC_WARN("DWC_TIMER_ALLOC failed\n");
  21251. + DWC_FREE(host_if);
  21252. + DWC_FREE(dev_if);
  21253. + DWC_WORKQ_FREE(core_if->wq_otg);
  21254. + DWC_FREE(core_if);
  21255. + return 0;
  21256. + }
  21257. +
  21258. + if (dwc_otg_setup_params(core_if)) {
  21259. + DWC_WARN("Error while setting core params\n");
  21260. + }
  21261. +
  21262. + core_if->hibernation_suspend = 0;
  21263. +
  21264. + /** ADP initialization */
  21265. + dwc_otg_adp_init(core_if);
  21266. +
  21267. + return core_if;
  21268. +}
  21269. +
  21270. +/**
  21271. + * This function frees the structures allocated by dwc_otg_cil_init().
  21272. + *
  21273. + * @param core_if The core interface pointer returned from
  21274. + * dwc_otg_cil_init().
  21275. + *
  21276. + */
  21277. +void dwc_otg_cil_remove(dwc_otg_core_if_t * core_if)
  21278. +{
  21279. + dctl_data_t dctl = {.d32 = 0 };
  21280. + DWC_DEBUGPL(DBG_CILV, "%s(%p)\n", __func__, core_if);
  21281. +
  21282. + /* Disable all interrupts */
  21283. + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, 1, 0);
  21284. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, 0);
  21285. +
  21286. + dctl.b.sftdiscon = 1;
  21287. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  21288. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0,
  21289. + dctl.d32);
  21290. + }
  21291. +
  21292. + if (core_if->wq_otg) {
  21293. + DWC_WORKQ_WAIT_WORK_DONE(core_if->wq_otg, 500);
  21294. + DWC_WORKQ_FREE(core_if->wq_otg);
  21295. + }
  21296. + if (core_if->dev_if) {
  21297. + DWC_FREE(core_if->dev_if);
  21298. + }
  21299. + if (core_if->host_if) {
  21300. + DWC_FREE(core_if->host_if);
  21301. + }
  21302. +
  21303. + /** Remove ADP Stuff */
  21304. + dwc_otg_adp_remove(core_if);
  21305. + if (core_if->core_params) {
  21306. + DWC_FREE(core_if->core_params);
  21307. + }
  21308. + if (core_if->wkp_timer) {
  21309. + DWC_TIMER_FREE(core_if->wkp_timer);
  21310. + }
  21311. + if (core_if->srp_timer) {
  21312. + DWC_TIMER_FREE(core_if->srp_timer);
  21313. + }
  21314. + DWC_FREE(core_if);
  21315. +}
  21316. +
  21317. +/**
  21318. + * This function enables the controller's Global Interrupt in the AHB Config
  21319. + * register.
  21320. + *
  21321. + * @param core_if Programming view of DWC_otg controller.
  21322. + */
  21323. +void dwc_otg_enable_global_interrupts(dwc_otg_core_if_t * core_if)
  21324. +{
  21325. + gahbcfg_data_t ahbcfg = {.d32 = 0 };
  21326. + ahbcfg.b.glblintrmsk = 1; /* Enable interrupts */
  21327. + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, 0, ahbcfg.d32);
  21328. +}
  21329. +
  21330. +/**
  21331. + * This function disables the controller's Global Interrupt in the AHB Config
  21332. + * register.
  21333. + *
  21334. + * @param core_if Programming view of DWC_otg controller.
  21335. + */
  21336. +void dwc_otg_disable_global_interrupts(dwc_otg_core_if_t * core_if)
  21337. +{
  21338. + gahbcfg_data_t ahbcfg = {.d32 = 0 };
  21339. + ahbcfg.b.glblintrmsk = 1; /* Disable interrupts */
  21340. + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, ahbcfg.d32, 0);
  21341. +}
  21342. +
  21343. +/**
  21344. + * This function initializes the commmon interrupts, used in both
  21345. + * device and host modes.
  21346. + *
  21347. + * @param core_if Programming view of the DWC_otg controller
  21348. + *
  21349. + */
  21350. +static void dwc_otg_enable_common_interrupts(dwc_otg_core_if_t * core_if)
  21351. +{
  21352. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  21353. + gintmsk_data_t intr_mask = {.d32 = 0 };
  21354. +
  21355. + /* Clear any pending OTG Interrupts */
  21356. + DWC_WRITE_REG32(&global_regs->gotgint, 0xFFFFFFFF);
  21357. +
  21358. + /* Clear any pending interrupts */
  21359. + DWC_WRITE_REG32(&global_regs->gintsts, 0xFFFFFFFF);
  21360. +
  21361. + /*
  21362. + * Enable the interrupts in the GINTMSK.
  21363. + */
  21364. + intr_mask.b.modemismatch = 1;
  21365. + intr_mask.b.otgintr = 1;
  21366. +
  21367. + if (!core_if->dma_enable) {
  21368. + intr_mask.b.rxstsqlvl = 1;
  21369. + }
  21370. +
  21371. + intr_mask.b.conidstschng = 1;
  21372. + intr_mask.b.wkupintr = 1;
  21373. + intr_mask.b.disconnect = 0;
  21374. + intr_mask.b.usbsuspend = 1;
  21375. + intr_mask.b.sessreqintr = 1;
  21376. +#ifdef CONFIG_USB_DWC_OTG_LPM
  21377. + if (core_if->core_params->lpm_enable) {
  21378. + intr_mask.b.lpmtranrcvd = 1;
  21379. + }
  21380. +#endif
  21381. + DWC_WRITE_REG32(&global_regs->gintmsk, intr_mask.d32);
  21382. +}
  21383. +
  21384. +/*
  21385. + * The restore operation is modified to support Synopsys Emulated Powerdown and
  21386. + * Hibernation. This function is for exiting from Device mode hibernation by
  21387. + * Host Initiated Resume/Reset and Device Initiated Remote-Wakeup.
  21388. + * @param core_if Programming view of DWC_otg controller.
  21389. + * @param rem_wakeup - indicates whether resume is initiated by Device or Host.
  21390. + * @param reset - indicates whether resume is initiated by Reset.
  21391. + */
  21392. +int dwc_otg_device_hibernation_restore(dwc_otg_core_if_t * core_if,
  21393. + int rem_wakeup, int reset)
  21394. +{
  21395. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  21396. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  21397. + dctl_data_t dctl = {.d32 = 0 };
  21398. +
  21399. + int timeout = 2000;
  21400. +
  21401. + if (!core_if->hibernation_suspend) {
  21402. + DWC_PRINTF("Already exited from Hibernation\n");
  21403. + return 1;
  21404. + }
  21405. +
  21406. + DWC_DEBUGPL(DBG_PCD, "%s called\n", __FUNCTION__);
  21407. + /* Switch-on voltage to the core */
  21408. + gpwrdn.b.pwrdnswtch = 1;
  21409. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  21410. + dwc_udelay(10);
  21411. +
  21412. + /* Reset core */
  21413. + gpwrdn.d32 = 0;
  21414. + gpwrdn.b.pwrdnrstn = 1;
  21415. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  21416. + dwc_udelay(10);
  21417. +
  21418. + /* Assert Restore signal */
  21419. + gpwrdn.d32 = 0;
  21420. + gpwrdn.b.restore = 1;
  21421. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  21422. + dwc_udelay(10);
  21423. +
  21424. + /* Disable power clamps */
  21425. + gpwrdn.d32 = 0;
  21426. + gpwrdn.b.pwrdnclmp = 1;
  21427. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  21428. +
  21429. + if (rem_wakeup) {
  21430. + dwc_udelay(70);
  21431. + }
  21432. +
  21433. + /* Deassert Reset core */
  21434. + gpwrdn.d32 = 0;
  21435. + gpwrdn.b.pwrdnrstn = 1;
  21436. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  21437. + dwc_udelay(10);
  21438. +
  21439. + /* Disable PMU interrupt */
  21440. + gpwrdn.d32 = 0;
  21441. + gpwrdn.b.pmuintsel = 1;
  21442. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  21443. +
  21444. + /* Mask interrupts from gpwrdn */
  21445. + gpwrdn.d32 = 0;
  21446. + gpwrdn.b.connect_det_msk = 1;
  21447. + gpwrdn.b.srp_det_msk = 1;
  21448. + gpwrdn.b.disconn_det_msk = 1;
  21449. + gpwrdn.b.rst_det_msk = 1;
  21450. + gpwrdn.b.lnstchng_msk = 1;
  21451. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  21452. +
  21453. + /* Indicates that we are going out from hibernation */
  21454. + core_if->hibernation_suspend = 0;
  21455. +
  21456. + /*
  21457. + * Set Restore Essential Regs bit in PCGCCTL register, restore_mode = 1
  21458. + * indicates restore from remote_wakeup
  21459. + */
  21460. + restore_essential_regs(core_if, rem_wakeup, 0);
  21461. +
  21462. + /*
  21463. + * Wait a little for seeing new value of variable hibernation_suspend if
  21464. + * Restore done interrupt received before polling
  21465. + */
  21466. + dwc_udelay(10);
  21467. +
  21468. + if (core_if->hibernation_suspend == 0) {
  21469. + /*
  21470. + * Wait For Restore_done Interrupt. This mechanism of polling the
  21471. + * interrupt is introduced to avoid any possible race conditions
  21472. + */
  21473. + do {
  21474. + gintsts_data_t gintsts;
  21475. + gintsts.d32 =
  21476. + DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  21477. + if (gintsts.b.restoredone) {
  21478. + gintsts.d32 = 0;
  21479. + gintsts.b.restoredone = 1;
  21480. + DWC_WRITE_REG32(&core_if->core_global_regs->
  21481. + gintsts, gintsts.d32);
  21482. + DWC_PRINTF("Restore Done Interrupt seen\n");
  21483. + break;
  21484. + }
  21485. + dwc_udelay(10);
  21486. + } while (--timeout);
  21487. + if (!timeout) {
  21488. + DWC_PRINTF("Restore Done interrupt wasn't generated here\n");
  21489. + }
  21490. + }
  21491. + /* Clear all pending interupts */
  21492. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  21493. +
  21494. + /* De-assert Restore */
  21495. + gpwrdn.d32 = 0;
  21496. + gpwrdn.b.restore = 1;
  21497. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  21498. + dwc_udelay(10);
  21499. +
  21500. + if (!rem_wakeup) {
  21501. + pcgcctl.d32 = 0;
  21502. + pcgcctl.b.rstpdwnmodule = 1;
  21503. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  21504. + }
  21505. +
  21506. + /* Restore GUSBCFG and DCFG */
  21507. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg,
  21508. + core_if->gr_backup->gusbcfg_local);
  21509. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg,
  21510. + core_if->dr_backup->dcfg);
  21511. +
  21512. + /* De-assert Wakeup Logic */
  21513. + gpwrdn.d32 = 0;
  21514. + gpwrdn.b.pmuactv = 1;
  21515. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  21516. + dwc_udelay(10);
  21517. +
  21518. + if (!rem_wakeup) {
  21519. + /* Set Device programming done bit */
  21520. + dctl.b.pwronprgdone = 1;
  21521. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  21522. + } else {
  21523. + /* Start Remote Wakeup Signaling */
  21524. + dctl.d32 = core_if->dr_backup->dctl;
  21525. + dctl.b.rmtwkupsig = 1;
  21526. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
  21527. + }
  21528. +
  21529. + dwc_mdelay(2);
  21530. + /* Clear all pending interupts */
  21531. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  21532. +
  21533. + /* Restore global registers */
  21534. + dwc_otg_restore_global_regs(core_if);
  21535. + /* Restore device global registers */
  21536. + dwc_otg_restore_dev_regs(core_if, rem_wakeup);
  21537. +
  21538. + if (rem_wakeup) {
  21539. + dwc_mdelay(7);
  21540. + dctl.d32 = 0;
  21541. + dctl.b.rmtwkupsig = 1;
  21542. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, 0);
  21543. + }
  21544. +
  21545. + core_if->hibernation_suspend = 0;
  21546. + /* The core will be in ON STATE */
  21547. + core_if->lx_state = DWC_OTG_L0;
  21548. + DWC_PRINTF("Hibernation recovery completes here\n");
  21549. +
  21550. + return 1;
  21551. +}
  21552. +
  21553. +/*
  21554. + * The restore operation is modified to support Synopsys Emulated Powerdown and
  21555. + * Hibernation. This function is for exiting from Host mode hibernation by
  21556. + * Host Initiated Resume/Reset and Device Initiated Remote-Wakeup.
  21557. + * @param core_if Programming view of DWC_otg controller.
  21558. + * @param rem_wakeup - indicates whether resume is initiated by Device or Host.
  21559. + * @param reset - indicates whether resume is initiated by Reset.
  21560. + */
  21561. +int dwc_otg_host_hibernation_restore(dwc_otg_core_if_t * core_if,
  21562. + int rem_wakeup, int reset)
  21563. +{
  21564. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  21565. + hprt0_data_t hprt0 = {.d32 = 0 };
  21566. +
  21567. + int timeout = 2000;
  21568. +
  21569. + DWC_DEBUGPL(DBG_HCD, "%s called\n", __FUNCTION__);
  21570. + /* Switch-on voltage to the core */
  21571. + gpwrdn.b.pwrdnswtch = 1;
  21572. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  21573. + dwc_udelay(10);
  21574. +
  21575. + /* Reset core */
  21576. + gpwrdn.d32 = 0;
  21577. + gpwrdn.b.pwrdnrstn = 1;
  21578. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  21579. + dwc_udelay(10);
  21580. +
  21581. + /* Assert Restore signal */
  21582. + gpwrdn.d32 = 0;
  21583. + gpwrdn.b.restore = 1;
  21584. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  21585. + dwc_udelay(10);
  21586. +
  21587. + /* Disable power clamps */
  21588. + gpwrdn.d32 = 0;
  21589. + gpwrdn.b.pwrdnclmp = 1;
  21590. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  21591. +
  21592. + if (!rem_wakeup) {
  21593. + dwc_udelay(50);
  21594. + }
  21595. +
  21596. + /* Deassert Reset core */
  21597. + gpwrdn.d32 = 0;
  21598. + gpwrdn.b.pwrdnrstn = 1;
  21599. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  21600. + dwc_udelay(10);
  21601. +
  21602. + /* Disable PMU interrupt */
  21603. + gpwrdn.d32 = 0;
  21604. + gpwrdn.b.pmuintsel = 1;
  21605. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  21606. +
  21607. + gpwrdn.d32 = 0;
  21608. + gpwrdn.b.connect_det_msk = 1;
  21609. + gpwrdn.b.srp_det_msk = 1;
  21610. + gpwrdn.b.disconn_det_msk = 1;
  21611. + gpwrdn.b.rst_det_msk = 1;
  21612. + gpwrdn.b.lnstchng_msk = 1;
  21613. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  21614. +
  21615. + /* Indicates that we are going out from hibernation */
  21616. + core_if->hibernation_suspend = 0;
  21617. +
  21618. + /* Set Restore Essential Regs bit in PCGCCTL register */
  21619. + restore_essential_regs(core_if, rem_wakeup, 1);
  21620. +
  21621. + /* Wait a little for seeing new value of variable hibernation_suspend if
  21622. + * Restore done interrupt received before polling */
  21623. + dwc_udelay(10);
  21624. +
  21625. + if (core_if->hibernation_suspend == 0) {
  21626. + /* Wait For Restore_done Interrupt. This mechanism of polling the
  21627. + * interrupt is introduced to avoid any possible race conditions
  21628. + */
  21629. + do {
  21630. + gintsts_data_t gintsts;
  21631. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  21632. + if (gintsts.b.restoredone) {
  21633. + gintsts.d32 = 0;
  21634. + gintsts.b.restoredone = 1;
  21635. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  21636. + DWC_DEBUGPL(DBG_HCD,"Restore Done Interrupt seen\n");
  21637. + break;
  21638. + }
  21639. + dwc_udelay(10);
  21640. + } while (--timeout);
  21641. + if (!timeout) {
  21642. + DWC_WARN("Restore Done interrupt wasn't generated\n");
  21643. + }
  21644. + }
  21645. +
  21646. + /* Set the flag's value to 0 again after receiving restore done interrupt */
  21647. + core_if->hibernation_suspend = 0;
  21648. +
  21649. + /* This step is not described in functional spec but if not wait for this
  21650. + * delay, mismatch interrupts occurred because just after restore core is
  21651. + * in Device mode(gintsts.curmode == 0) */
  21652. + dwc_mdelay(100);
  21653. +
  21654. + /* Clear all pending interrupts */
  21655. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  21656. +
  21657. + /* De-assert Restore */
  21658. + gpwrdn.d32 = 0;
  21659. + gpwrdn.b.restore = 1;
  21660. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  21661. + dwc_udelay(10);
  21662. +
  21663. + /* Restore GUSBCFG and HCFG */
  21664. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg,
  21665. + core_if->gr_backup->gusbcfg_local);
  21666. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg,
  21667. + core_if->hr_backup->hcfg_local);
  21668. +
  21669. + /* De-assert Wakeup Logic */
  21670. + gpwrdn.d32 = 0;
  21671. + gpwrdn.b.pmuactv = 1;
  21672. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  21673. + dwc_udelay(10);
  21674. +
  21675. + /* Start the Resume operation by programming HPRT0 */
  21676. + hprt0.d32 = core_if->hr_backup->hprt0_local;
  21677. + hprt0.b.prtpwr = 1;
  21678. + hprt0.b.prtena = 0;
  21679. + hprt0.b.prtsusp = 0;
  21680. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  21681. +
  21682. + DWC_PRINTF("Resume Starts Now\n");
  21683. + if (!reset) { // Indicates it is Resume Operation
  21684. + hprt0.d32 = core_if->hr_backup->hprt0_local;
  21685. + hprt0.b.prtres = 1;
  21686. + hprt0.b.prtpwr = 1;
  21687. + hprt0.b.prtena = 0;
  21688. + hprt0.b.prtsusp = 0;
  21689. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  21690. +
  21691. + if (!rem_wakeup)
  21692. + hprt0.b.prtres = 0;
  21693. + /* Wait for Resume time and then program HPRT again */
  21694. + dwc_mdelay(100);
  21695. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  21696. +
  21697. + } else { // Indicates it is Reset Operation
  21698. + hprt0.d32 = core_if->hr_backup->hprt0_local;
  21699. + hprt0.b.prtrst = 1;
  21700. + hprt0.b.prtpwr = 1;
  21701. + hprt0.b.prtena = 0;
  21702. + hprt0.b.prtsusp = 0;
  21703. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  21704. + /* Wait for Reset time and then program HPRT again */
  21705. + dwc_mdelay(60);
  21706. + hprt0.b.prtrst = 0;
  21707. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  21708. + }
  21709. + /* Clear all interrupt status */
  21710. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  21711. + hprt0.b.prtconndet = 1;
  21712. + hprt0.b.prtenchng = 1;
  21713. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  21714. +
  21715. + /* Clear all pending interupts */
  21716. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  21717. +
  21718. + /* Restore global registers */
  21719. + dwc_otg_restore_global_regs(core_if);
  21720. + /* Restore host global registers */
  21721. + dwc_otg_restore_host_regs(core_if, reset);
  21722. +
  21723. + /* The core will be in ON STATE */
  21724. + core_if->lx_state = DWC_OTG_L0;
  21725. + DWC_PRINTF("Hibernation recovery is complete here\n");
  21726. + return 0;
  21727. +}
  21728. +
  21729. +/** Saves some register values into system memory. */
  21730. +int dwc_otg_save_global_regs(dwc_otg_core_if_t * core_if)
  21731. +{
  21732. + struct dwc_otg_global_regs_backup *gr;
  21733. + int i;
  21734. +
  21735. + gr = core_if->gr_backup;
  21736. + if (!gr) {
  21737. + gr = DWC_ALLOC(sizeof(*gr));
  21738. + if (!gr) {
  21739. + return -DWC_E_NO_MEMORY;
  21740. + }
  21741. + core_if->gr_backup = gr;
  21742. + }
  21743. +
  21744. + gr->gotgctl_local = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  21745. + gr->gintmsk_local = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  21746. + gr->gahbcfg_local = DWC_READ_REG32(&core_if->core_global_regs->gahbcfg);
  21747. + gr->gusbcfg_local = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  21748. + gr->grxfsiz_local = DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
  21749. + gr->gnptxfsiz_local = DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz);
  21750. + gr->hptxfsiz_local = DWC_READ_REG32(&core_if->core_global_regs->hptxfsiz);
  21751. +#ifdef CONFIG_USB_DWC_OTG_LPM
  21752. + gr->glpmcfg_local = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  21753. +#endif
  21754. + gr->gi2cctl_local = DWC_READ_REG32(&core_if->core_global_regs->gi2cctl);
  21755. + gr->pcgcctl_local = DWC_READ_REG32(core_if->pcgcctl);
  21756. + gr->gdfifocfg_local =
  21757. + DWC_READ_REG32(&core_if->core_global_regs->gdfifocfg);
  21758. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  21759. + gr->dtxfsiz_local[i] =
  21760. + DWC_READ_REG32(&(core_if->core_global_regs->dtxfsiz[i]));
  21761. + }
  21762. +
  21763. + DWC_DEBUGPL(DBG_ANY, "===========Backing Global registers==========\n");
  21764. + DWC_DEBUGPL(DBG_ANY, "Backed up gotgctl = %08x\n", gr->gotgctl_local);
  21765. + DWC_DEBUGPL(DBG_ANY, "Backed up gintmsk = %08x\n", gr->gintmsk_local);
  21766. + DWC_DEBUGPL(DBG_ANY, "Backed up gahbcfg = %08x\n", gr->gahbcfg_local);
  21767. + DWC_DEBUGPL(DBG_ANY, "Backed up gusbcfg = %08x\n", gr->gusbcfg_local);
  21768. + DWC_DEBUGPL(DBG_ANY, "Backed up grxfsiz = %08x\n", gr->grxfsiz_local);
  21769. + DWC_DEBUGPL(DBG_ANY, "Backed up gnptxfsiz = %08x\n",
  21770. + gr->gnptxfsiz_local);
  21771. + DWC_DEBUGPL(DBG_ANY, "Backed up hptxfsiz = %08x\n",
  21772. + gr->hptxfsiz_local);
  21773. +#ifdef CONFIG_USB_DWC_OTG_LPM
  21774. + DWC_DEBUGPL(DBG_ANY, "Backed up glpmcfg = %08x\n", gr->glpmcfg_local);
  21775. +#endif
  21776. + DWC_DEBUGPL(DBG_ANY, "Backed up gi2cctl = %08x\n", gr->gi2cctl_local);
  21777. + DWC_DEBUGPL(DBG_ANY, "Backed up pcgcctl = %08x\n", gr->pcgcctl_local);
  21778. + DWC_DEBUGPL(DBG_ANY,"Backed up gdfifocfg = %08x\n",gr->gdfifocfg_local);
  21779. +
  21780. + return 0;
  21781. +}
  21782. +
  21783. +/** Saves GINTMSK register before setting the msk bits. */
  21784. +int dwc_otg_save_gintmsk_reg(dwc_otg_core_if_t * core_if)
  21785. +{
  21786. + struct dwc_otg_global_regs_backup *gr;
  21787. +
  21788. + gr = core_if->gr_backup;
  21789. + if (!gr) {
  21790. + gr = DWC_ALLOC(sizeof(*gr));
  21791. + if (!gr) {
  21792. + return -DWC_E_NO_MEMORY;
  21793. + }
  21794. + core_if->gr_backup = gr;
  21795. + }
  21796. +
  21797. + gr->gintmsk_local = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  21798. +
  21799. + DWC_DEBUGPL(DBG_ANY,"=============Backing GINTMSK registers============\n");
  21800. + DWC_DEBUGPL(DBG_ANY, "Backed up gintmsk = %08x\n", gr->gintmsk_local);
  21801. +
  21802. + return 0;
  21803. +}
  21804. +
  21805. +int dwc_otg_save_dev_regs(dwc_otg_core_if_t * core_if)
  21806. +{
  21807. + struct dwc_otg_dev_regs_backup *dr;
  21808. + int i;
  21809. +
  21810. + dr = core_if->dr_backup;
  21811. + if (!dr) {
  21812. + dr = DWC_ALLOC(sizeof(*dr));
  21813. + if (!dr) {
  21814. + return -DWC_E_NO_MEMORY;
  21815. + }
  21816. + core_if->dr_backup = dr;
  21817. + }
  21818. +
  21819. + dr->dcfg = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  21820. + dr->dctl = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
  21821. + dr->daintmsk =
  21822. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daintmsk);
  21823. + dr->diepmsk =
  21824. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->diepmsk);
  21825. + dr->doepmsk =
  21826. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->doepmsk);
  21827. +
  21828. + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
  21829. + dr->diepctl[i] =
  21830. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->diepctl);
  21831. + dr->dieptsiz[i] =
  21832. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->dieptsiz);
  21833. + dr->diepdma[i] =
  21834. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->diepdma);
  21835. + }
  21836. +
  21837. + DWC_DEBUGPL(DBG_ANY,
  21838. + "=============Backing Host registers==============\n");
  21839. + DWC_DEBUGPL(DBG_ANY, "Backed up dcfg = %08x\n", dr->dcfg);
  21840. + DWC_DEBUGPL(DBG_ANY, "Backed up dctl = %08x\n", dr->dctl);
  21841. + DWC_DEBUGPL(DBG_ANY, "Backed up daintmsk = %08x\n",
  21842. + dr->daintmsk);
  21843. + DWC_DEBUGPL(DBG_ANY, "Backed up diepmsk = %08x\n", dr->diepmsk);
  21844. + DWC_DEBUGPL(DBG_ANY, "Backed up doepmsk = %08x\n", dr->doepmsk);
  21845. + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
  21846. + DWC_DEBUGPL(DBG_ANY, "Backed up diepctl[%d] = %08x\n", i,
  21847. + dr->diepctl[i]);
  21848. + DWC_DEBUGPL(DBG_ANY, "Backed up dieptsiz[%d] = %08x\n",
  21849. + i, dr->dieptsiz[i]);
  21850. + DWC_DEBUGPL(DBG_ANY, "Backed up diepdma[%d] = %08x\n", i,
  21851. + dr->diepdma[i]);
  21852. + }
  21853. +
  21854. + return 0;
  21855. +}
  21856. +
  21857. +int dwc_otg_save_host_regs(dwc_otg_core_if_t * core_if)
  21858. +{
  21859. + struct dwc_otg_host_regs_backup *hr;
  21860. + int i;
  21861. +
  21862. + hr = core_if->hr_backup;
  21863. + if (!hr) {
  21864. + hr = DWC_ALLOC(sizeof(*hr));
  21865. + if (!hr) {
  21866. + return -DWC_E_NO_MEMORY;
  21867. + }
  21868. + core_if->hr_backup = hr;
  21869. + }
  21870. +
  21871. + hr->hcfg_local =
  21872. + DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
  21873. + hr->haintmsk_local =
  21874. + DWC_READ_REG32(&core_if->host_if->host_global_regs->haintmsk);
  21875. + for (i = 0; i < dwc_otg_get_param_host_channels(core_if); ++i) {
  21876. + hr->hcintmsk_local[i] =
  21877. + DWC_READ_REG32(&core_if->host_if->hc_regs[i]->hcintmsk);
  21878. + }
  21879. + hr->hprt0_local = DWC_READ_REG32(core_if->host_if->hprt0);
  21880. + hr->hfir_local =
  21881. + DWC_READ_REG32(&core_if->host_if->host_global_regs->hfir);
  21882. +
  21883. + DWC_DEBUGPL(DBG_ANY,
  21884. + "=============Backing Host registers===============\n");
  21885. + DWC_DEBUGPL(DBG_ANY, "Backed up hcfg = %08x\n",
  21886. + hr->hcfg_local);
  21887. + DWC_DEBUGPL(DBG_ANY, "Backed up haintmsk = %08x\n", hr->haintmsk_local);
  21888. + for (i = 0; i < dwc_otg_get_param_host_channels(core_if); ++i) {
  21889. + DWC_DEBUGPL(DBG_ANY, "Backed up hcintmsk[%02d]=%08x\n", i,
  21890. + hr->hcintmsk_local[i]);
  21891. + }
  21892. + DWC_DEBUGPL(DBG_ANY, "Backed up hprt0 = %08x\n",
  21893. + hr->hprt0_local);
  21894. + DWC_DEBUGPL(DBG_ANY, "Backed up hfir = %08x\n",
  21895. + hr->hfir_local);
  21896. +
  21897. + return 0;
  21898. +}
  21899. +
  21900. +int dwc_otg_restore_global_regs(dwc_otg_core_if_t *core_if)
  21901. +{
  21902. + struct dwc_otg_global_regs_backup *gr;
  21903. + int i;
  21904. +
  21905. + gr = core_if->gr_backup;
  21906. + if (!gr) {
  21907. + return -DWC_E_INVALID;
  21908. + }
  21909. +
  21910. + DWC_WRITE_REG32(&core_if->core_global_regs->gotgctl, gr->gotgctl_local);
  21911. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gr->gintmsk_local);
  21912. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gr->gusbcfg_local);
  21913. + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg, gr->gahbcfg_local);
  21914. + DWC_WRITE_REG32(&core_if->core_global_regs->grxfsiz, gr->grxfsiz_local);
  21915. + DWC_WRITE_REG32(&core_if->core_global_regs->gnptxfsiz,
  21916. + gr->gnptxfsiz_local);
  21917. + DWC_WRITE_REG32(&core_if->core_global_regs->hptxfsiz,
  21918. + gr->hptxfsiz_local);
  21919. + DWC_WRITE_REG32(&core_if->core_global_regs->gdfifocfg,
  21920. + gr->gdfifocfg_local);
  21921. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  21922. + DWC_WRITE_REG32(&core_if->core_global_regs->dtxfsiz[i],
  21923. + gr->dtxfsiz_local[i]);
  21924. + }
  21925. +
  21926. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  21927. + DWC_WRITE_REG32(core_if->host_if->hprt0, 0x0000100A);
  21928. + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg,
  21929. + (gr->gahbcfg_local));
  21930. + return 0;
  21931. +}
  21932. +
  21933. +int dwc_otg_restore_dev_regs(dwc_otg_core_if_t * core_if, int rem_wakeup)
  21934. +{
  21935. + struct dwc_otg_dev_regs_backup *dr;
  21936. + int i;
  21937. +
  21938. + dr = core_if->dr_backup;
  21939. +
  21940. + if (!dr) {
  21941. + return -DWC_E_INVALID;
  21942. + }
  21943. +
  21944. + if (!rem_wakeup) {
  21945. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl,
  21946. + dr->dctl);
  21947. + }
  21948. +
  21949. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->daintmsk, dr->daintmsk);
  21950. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->diepmsk, dr->diepmsk);
  21951. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->doepmsk, dr->doepmsk);
  21952. +
  21953. + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
  21954. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->dieptsiz, dr->dieptsiz[i]);
  21955. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->diepdma, dr->diepdma[i]);
  21956. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->diepctl, dr->diepctl[i]);
  21957. + }
  21958. +
  21959. + return 0;
  21960. +}
  21961. +
  21962. +int dwc_otg_restore_host_regs(dwc_otg_core_if_t * core_if, int reset)
  21963. +{
  21964. + struct dwc_otg_host_regs_backup *hr;
  21965. + int i;
  21966. + hr = core_if->hr_backup;
  21967. +
  21968. + if (!hr) {
  21969. + return -DWC_E_INVALID;
  21970. + }
  21971. +
  21972. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg, hr->hcfg_local);
  21973. + //if (!reset)
  21974. + //{
  21975. + // DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hfir, hr->hfir_local);
  21976. + //}
  21977. +
  21978. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->haintmsk,
  21979. + hr->haintmsk_local);
  21980. + for (i = 0; i < dwc_otg_get_param_host_channels(core_if); ++i) {
  21981. + DWC_WRITE_REG32(&core_if->host_if->hc_regs[i]->hcintmsk,
  21982. + hr->hcintmsk_local[i]);
  21983. + }
  21984. +
  21985. + return 0;
  21986. +}
  21987. +
  21988. +int restore_lpm_i2c_regs(dwc_otg_core_if_t * core_if)
  21989. +{
  21990. + struct dwc_otg_global_regs_backup *gr;
  21991. +
  21992. + gr = core_if->gr_backup;
  21993. +
  21994. + /* Restore values for LPM and I2C */
  21995. +#ifdef CONFIG_USB_DWC_OTG_LPM
  21996. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, gr->glpmcfg_local);
  21997. +#endif
  21998. + DWC_WRITE_REG32(&core_if->core_global_regs->gi2cctl, gr->gi2cctl_local);
  21999. +
  22000. + return 0;
  22001. +}
  22002. +
  22003. +int restore_essential_regs(dwc_otg_core_if_t * core_if, int rmode, int is_host)
  22004. +{
  22005. + struct dwc_otg_global_regs_backup *gr;
  22006. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  22007. + gahbcfg_data_t gahbcfg = {.d32 = 0 };
  22008. + gusbcfg_data_t gusbcfg = {.d32 = 0 };
  22009. + gintmsk_data_t gintmsk = {.d32 = 0 };
  22010. +
  22011. + /* Restore LPM and I2C registers */
  22012. + restore_lpm_i2c_regs(core_if);
  22013. +
  22014. + /* Set PCGCCTL to 0 */
  22015. + DWC_WRITE_REG32(core_if->pcgcctl, 0x00000000);
  22016. +
  22017. + gr = core_if->gr_backup;
  22018. + /* Load restore values for [31:14] bits */
  22019. + DWC_WRITE_REG32(core_if->pcgcctl,
  22020. + ((gr->pcgcctl_local & 0xffffc000) | 0x00020000));
  22021. +
  22022. + /* Umnask global Interrupt in GAHBCFG and restore it */
  22023. + gahbcfg.d32 = gr->gahbcfg_local;
  22024. + gahbcfg.b.glblintrmsk = 1;
  22025. + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg, gahbcfg.d32);
  22026. +
  22027. + /* Clear all pending interupts */
  22028. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  22029. +
  22030. + /* Unmask restore done interrupt */
  22031. + gintmsk.b.restoredone = 1;
  22032. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32);
  22033. +
  22034. + /* Restore GUSBCFG and HCFG/DCFG */
  22035. + gusbcfg.d32 = core_if->gr_backup->gusbcfg_local;
  22036. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gusbcfg.d32);
  22037. +
  22038. + if (is_host) {
  22039. + hcfg_data_t hcfg = {.d32 = 0 };
  22040. + hcfg.d32 = core_if->hr_backup->hcfg_local;
  22041. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg,
  22042. + hcfg.d32);
  22043. +
  22044. + /* Load restore values for [31:14] bits */
  22045. + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
  22046. + pcgcctl.d32 = gr->pcgcctl_local | 0x00020000;
  22047. +
  22048. + if (rmode)
  22049. + pcgcctl.b.restoremode = 1;
  22050. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  22051. + dwc_udelay(10);
  22052. +
  22053. + /* Load restore values for [31:14] bits and set EssRegRestored bit */
  22054. + pcgcctl.d32 = gr->pcgcctl_local | 0xffffc000;
  22055. + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
  22056. + pcgcctl.b.ess_reg_restored = 1;
  22057. + if (rmode)
  22058. + pcgcctl.b.restoremode = 1;
  22059. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  22060. + } else {
  22061. + dcfg_data_t dcfg = {.d32 = 0 };
  22062. + dcfg.d32 = core_if->dr_backup->dcfg;
  22063. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, dcfg.d32);
  22064. +
  22065. + /* Load restore values for [31:14] bits */
  22066. + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
  22067. + pcgcctl.d32 = gr->pcgcctl_local | 0x00020000;
  22068. + if (!rmode) {
  22069. + pcgcctl.d32 |= 0x208;
  22070. + }
  22071. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  22072. + dwc_udelay(10);
  22073. +
  22074. + /* Load restore values for [31:14] bits */
  22075. + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
  22076. + pcgcctl.d32 = gr->pcgcctl_local | 0x00020000;
  22077. + pcgcctl.b.ess_reg_restored = 1;
  22078. + if (!rmode)
  22079. + pcgcctl.d32 |= 0x208;
  22080. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  22081. + }
  22082. +
  22083. + return 0;
  22084. +}
  22085. +
  22086. +/**
  22087. + * Initializes the FSLSPClkSel field of the HCFG register depending on the PHY
  22088. + * type.
  22089. + */
  22090. +static void init_fslspclksel(dwc_otg_core_if_t * core_if)
  22091. +{
  22092. + uint32_t val;
  22093. + hcfg_data_t hcfg;
  22094. +
  22095. + if (((core_if->hwcfg2.b.hs_phy_type == 2) &&
  22096. + (core_if->hwcfg2.b.fs_phy_type == 1) &&
  22097. + (core_if->core_params->ulpi_fs_ls)) ||
  22098. + (core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS)) {
  22099. + /* Full speed PHY */
  22100. + val = DWC_HCFG_48_MHZ;
  22101. + } else {
  22102. + /* High speed PHY running at full speed or high speed */
  22103. + val = DWC_HCFG_30_60_MHZ;
  22104. + }
  22105. +
  22106. + DWC_DEBUGPL(DBG_CIL, "Initializing HCFG.FSLSPClkSel to 0x%1x\n", val);
  22107. + hcfg.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
  22108. + hcfg.b.fslspclksel = val;
  22109. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg, hcfg.d32);
  22110. +}
  22111. +
  22112. +/**
  22113. + * Initializes the DevSpd field of the DCFG register depending on the PHY type
  22114. + * and the enumeration speed of the device.
  22115. + */
  22116. +static void init_devspd(dwc_otg_core_if_t * core_if)
  22117. +{
  22118. + uint32_t val;
  22119. + dcfg_data_t dcfg;
  22120. +
  22121. + if (((core_if->hwcfg2.b.hs_phy_type == 2) &&
  22122. + (core_if->hwcfg2.b.fs_phy_type == 1) &&
  22123. + (core_if->core_params->ulpi_fs_ls)) ||
  22124. + (core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS)) {
  22125. + /* Full speed PHY */
  22126. + val = 0x3;
  22127. + } else if (core_if->core_params->speed == DWC_SPEED_PARAM_FULL) {
  22128. + /* High speed PHY running at full speed */
  22129. + val = 0x1;
  22130. + } else {
  22131. + /* High speed PHY running at high speed */
  22132. + val = 0x0;
  22133. + }
  22134. +
  22135. + DWC_DEBUGPL(DBG_CIL, "Initializing DCFG.DevSpd to 0x%1x\n", val);
  22136. +
  22137. + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  22138. + dcfg.b.devspd = val;
  22139. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, dcfg.d32);
  22140. +}
  22141. +
  22142. +/**
  22143. + * This function calculates the number of IN EPS
  22144. + * using GHWCFG1 and GHWCFG2 registers values
  22145. + *
  22146. + * @param core_if Programming view of the DWC_otg controller
  22147. + */
  22148. +static uint32_t calc_num_in_eps(dwc_otg_core_if_t * core_if)
  22149. +{
  22150. + uint32_t num_in_eps = 0;
  22151. + uint32_t num_eps = core_if->hwcfg2.b.num_dev_ep;
  22152. + uint32_t hwcfg1 = core_if->hwcfg1.d32 >> 3;
  22153. + uint32_t num_tx_fifos = core_if->hwcfg4.b.num_in_eps;
  22154. + int i;
  22155. +
  22156. + for (i = 0; i < num_eps; ++i) {
  22157. + if (!(hwcfg1 & 0x1))
  22158. + num_in_eps++;
  22159. +
  22160. + hwcfg1 >>= 2;
  22161. + }
  22162. +
  22163. + if (core_if->hwcfg4.b.ded_fifo_en) {
  22164. + num_in_eps =
  22165. + (num_in_eps > num_tx_fifos) ? num_tx_fifos : num_in_eps;
  22166. + }
  22167. +
  22168. + return num_in_eps;
  22169. +}
  22170. +
  22171. +/**
  22172. + * This function calculates the number of OUT EPS
  22173. + * using GHWCFG1 and GHWCFG2 registers values
  22174. + *
  22175. + * @param core_if Programming view of the DWC_otg controller
  22176. + */
  22177. +static uint32_t calc_num_out_eps(dwc_otg_core_if_t * core_if)
  22178. +{
  22179. + uint32_t num_out_eps = 0;
  22180. + uint32_t num_eps = core_if->hwcfg2.b.num_dev_ep;
  22181. + uint32_t hwcfg1 = core_if->hwcfg1.d32 >> 2;
  22182. + int i;
  22183. +
  22184. + for (i = 0; i < num_eps; ++i) {
  22185. + if (!(hwcfg1 & 0x1))
  22186. + num_out_eps++;
  22187. +
  22188. + hwcfg1 >>= 2;
  22189. + }
  22190. + return num_out_eps;
  22191. +}
  22192. +
  22193. +/**
  22194. + * This function initializes the DWC_otg controller registers and
  22195. + * prepares the core for device mode or host mode operation.
  22196. + *
  22197. + * @param core_if Programming view of the DWC_otg controller
  22198. + *
  22199. + */
  22200. +void dwc_otg_core_init(dwc_otg_core_if_t * core_if)
  22201. +{
  22202. + int i = 0;
  22203. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  22204. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  22205. + gahbcfg_data_t ahbcfg = {.d32 = 0 };
  22206. + gusbcfg_data_t usbcfg = {.d32 = 0 };
  22207. + gi2cctl_data_t i2cctl = {.d32 = 0 };
  22208. +
  22209. + DWC_DEBUGPL(DBG_CILV, "dwc_otg_core_init(%p) regs at %p\n",
  22210. + core_if, global_regs);
  22211. +
  22212. + /* Common Initialization */
  22213. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  22214. +
  22215. + /* Program the ULPI External VBUS bit if needed */
  22216. + usbcfg.b.ulpi_ext_vbus_drv =
  22217. + (core_if->core_params->phy_ulpi_ext_vbus ==
  22218. + DWC_PHY_ULPI_EXTERNAL_VBUS) ? 1 : 0;
  22219. +
  22220. + /* Set external TS Dline pulsing */
  22221. + usbcfg.b.term_sel_dl_pulse =
  22222. + (core_if->core_params->ts_dline == 1) ? 1 : 0;
  22223. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  22224. +
  22225. + /* Reset the Controller */
  22226. + dwc_otg_core_reset(core_if);
  22227. +
  22228. + core_if->adp_enable = core_if->core_params->adp_supp_enable;
  22229. + core_if->power_down = core_if->core_params->power_down;
  22230. + core_if->otg_sts = 0;
  22231. +
  22232. + /* Initialize parameters from Hardware configuration registers. */
  22233. + dev_if->num_in_eps = calc_num_in_eps(core_if);
  22234. + dev_if->num_out_eps = calc_num_out_eps(core_if);
  22235. +
  22236. + DWC_DEBUGPL(DBG_CIL, "num_dev_perio_in_ep=%d\n",
  22237. + core_if->hwcfg4.b.num_dev_perio_in_ep);
  22238. +
  22239. + for (i = 0; i < core_if->hwcfg4.b.num_dev_perio_in_ep; i++) {
  22240. + dev_if->perio_tx_fifo_size[i] =
  22241. + DWC_READ_REG32(&global_regs->dtxfsiz[i]) >> 16;
  22242. + DWC_DEBUGPL(DBG_CIL, "Periodic Tx FIFO SZ #%d=0x%0x\n",
  22243. + i, dev_if->perio_tx_fifo_size[i]);
  22244. + }
  22245. +
  22246. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  22247. + dev_if->tx_fifo_size[i] =
  22248. + DWC_READ_REG32(&global_regs->dtxfsiz[i]) >> 16;
  22249. + DWC_DEBUGPL(DBG_CIL, "Tx FIFO SZ #%d=0x%0x\n",
  22250. + i, dev_if->tx_fifo_size[i]);
  22251. + }
  22252. +
  22253. + core_if->total_fifo_size = core_if->hwcfg3.b.dfifo_depth;
  22254. + core_if->rx_fifo_size = DWC_READ_REG32(&global_regs->grxfsiz);
  22255. + core_if->nperio_tx_fifo_size =
  22256. + DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16;
  22257. +
  22258. + DWC_DEBUGPL(DBG_CIL, "Total FIFO SZ=%d\n", core_if->total_fifo_size);
  22259. + DWC_DEBUGPL(DBG_CIL, "Rx FIFO SZ=%d\n", core_if->rx_fifo_size);
  22260. + DWC_DEBUGPL(DBG_CIL, "NP Tx FIFO SZ=%d\n",
  22261. + core_if->nperio_tx_fifo_size);
  22262. +
  22263. + /* This programming sequence needs to happen in FS mode before any other
  22264. + * programming occurs */
  22265. + if ((core_if->core_params->speed == DWC_SPEED_PARAM_FULL) &&
  22266. + (core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS)) {
  22267. + /* If FS mode with FS PHY */
  22268. +
  22269. + /* core_init() is now called on every switch so only call the
  22270. + * following for the first time through. */
  22271. + if (!core_if->phy_init_done) {
  22272. + core_if->phy_init_done = 1;
  22273. + DWC_DEBUGPL(DBG_CIL, "FS_PHY detected\n");
  22274. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  22275. + usbcfg.b.physel = 1;
  22276. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  22277. +
  22278. + /* Reset after a PHY select */
  22279. + dwc_otg_core_reset(core_if);
  22280. + }
  22281. +
  22282. + /* Program DCFG.DevSpd or HCFG.FSLSPclkSel to 48Mhz in FS. Also
  22283. + * do this on HNP Dev/Host mode switches (done in dev_init and
  22284. + * host_init). */
  22285. + if (dwc_otg_is_host_mode(core_if)) {
  22286. + init_fslspclksel(core_if);
  22287. + } else {
  22288. + init_devspd(core_if);
  22289. + }
  22290. +
  22291. + if (core_if->core_params->i2c_enable) {
  22292. + DWC_DEBUGPL(DBG_CIL, "FS_PHY Enabling I2c\n");
  22293. + /* Program GUSBCFG.OtgUtmifsSel to I2C */
  22294. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  22295. + usbcfg.b.otgutmifssel = 1;
  22296. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  22297. +
  22298. + /* Program GI2CCTL.I2CEn */
  22299. + i2cctl.d32 = DWC_READ_REG32(&global_regs->gi2cctl);
  22300. + i2cctl.b.i2cdevaddr = 1;
  22301. + i2cctl.b.i2cen = 0;
  22302. + DWC_WRITE_REG32(&global_regs->gi2cctl, i2cctl.d32);
  22303. + i2cctl.b.i2cen = 1;
  22304. + DWC_WRITE_REG32(&global_regs->gi2cctl, i2cctl.d32);
  22305. + }
  22306. +
  22307. + } /* endif speed == DWC_SPEED_PARAM_FULL */
  22308. + else {
  22309. + /* High speed PHY. */
  22310. + if (!core_if->phy_init_done) {
  22311. + core_if->phy_init_done = 1;
  22312. + /* HS PHY parameters. These parameters are preserved
  22313. + * during soft reset so only program the first time. Do
  22314. + * a soft reset immediately after setting phyif. */
  22315. +
  22316. + if (core_if->core_params->phy_type == 2) {
  22317. + /* ULPI interface */
  22318. + usbcfg.b.ulpi_utmi_sel = 1;
  22319. + usbcfg.b.phyif = 0;
  22320. + usbcfg.b.ddrsel =
  22321. + core_if->core_params->phy_ulpi_ddr;
  22322. + } else if (core_if->core_params->phy_type == 1) {
  22323. + /* UTMI+ interface */
  22324. + usbcfg.b.ulpi_utmi_sel = 0;
  22325. + if (core_if->core_params->phy_utmi_width == 16) {
  22326. + usbcfg.b.phyif = 1;
  22327. +
  22328. + } else {
  22329. + usbcfg.b.phyif = 0;
  22330. + }
  22331. + } else {
  22332. + DWC_ERROR("FS PHY TYPE\n");
  22333. + }
  22334. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  22335. + /* Reset after setting the PHY parameters */
  22336. + dwc_otg_core_reset(core_if);
  22337. + }
  22338. + }
  22339. +
  22340. + if ((core_if->hwcfg2.b.hs_phy_type == 2) &&
  22341. + (core_if->hwcfg2.b.fs_phy_type == 1) &&
  22342. + (core_if->core_params->ulpi_fs_ls)) {
  22343. + DWC_DEBUGPL(DBG_CIL, "Setting ULPI FSLS\n");
  22344. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  22345. + usbcfg.b.ulpi_fsls = 1;
  22346. + usbcfg.b.ulpi_clk_sus_m = 1;
  22347. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  22348. + } else {
  22349. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  22350. + usbcfg.b.ulpi_fsls = 0;
  22351. + usbcfg.b.ulpi_clk_sus_m = 0;
  22352. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  22353. + }
  22354. +
  22355. + /* Program the GAHBCFG Register. */
  22356. + switch (core_if->hwcfg2.b.architecture) {
  22357. +
  22358. + case DWC_SLAVE_ONLY_ARCH:
  22359. + DWC_DEBUGPL(DBG_CIL, "Slave Only Mode\n");
  22360. + ahbcfg.b.nptxfemplvl_txfemplvl =
  22361. + DWC_GAHBCFG_TXFEMPTYLVL_HALFEMPTY;
  22362. + ahbcfg.b.ptxfemplvl = DWC_GAHBCFG_TXFEMPTYLVL_HALFEMPTY;
  22363. + core_if->dma_enable = 0;
  22364. + core_if->dma_desc_enable = 0;
  22365. + break;
  22366. +
  22367. + case DWC_EXT_DMA_ARCH:
  22368. + DWC_DEBUGPL(DBG_CIL, "External DMA Mode\n");
  22369. + {
  22370. + uint8_t brst_sz = core_if->core_params->dma_burst_size;
  22371. + ahbcfg.b.hburstlen = 0;
  22372. + while (brst_sz > 1) {
  22373. + ahbcfg.b.hburstlen++;
  22374. + brst_sz >>= 1;
  22375. + }
  22376. + }
  22377. + core_if->dma_enable = (core_if->core_params->dma_enable != 0);
  22378. + core_if->dma_desc_enable =
  22379. + (core_if->core_params->dma_desc_enable != 0);
  22380. + break;
  22381. +
  22382. + case DWC_INT_DMA_ARCH:
  22383. + DWC_DEBUGPL(DBG_CIL, "Internal DMA Mode\n");
  22384. + /* Old value was DWC_GAHBCFG_INT_DMA_BURST_INCR - done for
  22385. + Host mode ISOC in issue fix - vahrama */
  22386. + /* Broadcom had altered to (1<<3)|(0<<0) - WRESP=1, max 4 beats */
  22387. + ahbcfg.b.hburstlen = (1<<3)|(0<<0);//DWC_GAHBCFG_INT_DMA_BURST_INCR4;
  22388. + core_if->dma_enable = (core_if->core_params->dma_enable != 0);
  22389. + core_if->dma_desc_enable =
  22390. + (core_if->core_params->dma_desc_enable != 0);
  22391. + break;
  22392. +
  22393. + }
  22394. + if (core_if->dma_enable) {
  22395. + if (core_if->dma_desc_enable) {
  22396. + DWC_PRINTF("Using Descriptor DMA mode\n");
  22397. + } else {
  22398. + DWC_PRINTF("Using Buffer DMA mode\n");
  22399. +
  22400. + }
  22401. + } else {
  22402. + DWC_PRINTF("Using Slave mode\n");
  22403. + core_if->dma_desc_enable = 0;
  22404. + }
  22405. +
  22406. + if (core_if->core_params->ahb_single) {
  22407. + ahbcfg.b.ahbsingle = 1;
  22408. + }
  22409. +
  22410. + ahbcfg.b.dmaenable = core_if->dma_enable;
  22411. + DWC_WRITE_REG32(&global_regs->gahbcfg, ahbcfg.d32);
  22412. +
  22413. + core_if->en_multiple_tx_fifo = core_if->hwcfg4.b.ded_fifo_en;
  22414. +
  22415. + core_if->pti_enh_enable = core_if->core_params->pti_enable != 0;
  22416. + core_if->multiproc_int_enable = core_if->core_params->mpi_enable;
  22417. + DWC_PRINTF("Periodic Transfer Interrupt Enhancement - %s\n",
  22418. + ((core_if->pti_enh_enable) ? "enabled" : "disabled"));
  22419. + DWC_PRINTF("Multiprocessor Interrupt Enhancement - %s\n",
  22420. + ((core_if->multiproc_int_enable) ? "enabled" : "disabled"));
  22421. +
  22422. + /*
  22423. + * Program the GUSBCFG register.
  22424. + */
  22425. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  22426. +
  22427. + switch (core_if->hwcfg2.b.op_mode) {
  22428. + case DWC_MODE_HNP_SRP_CAPABLE:
  22429. + usbcfg.b.hnpcap = (core_if->core_params->otg_cap ==
  22430. + DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE);
  22431. + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
  22432. + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
  22433. + break;
  22434. +
  22435. + case DWC_MODE_SRP_ONLY_CAPABLE:
  22436. + usbcfg.b.hnpcap = 0;
  22437. + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
  22438. + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
  22439. + break;
  22440. +
  22441. + case DWC_MODE_NO_HNP_SRP_CAPABLE:
  22442. + usbcfg.b.hnpcap = 0;
  22443. + usbcfg.b.srpcap = 0;
  22444. + break;
  22445. +
  22446. + case DWC_MODE_SRP_CAPABLE_DEVICE:
  22447. + usbcfg.b.hnpcap = 0;
  22448. + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
  22449. + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
  22450. + break;
  22451. +
  22452. + case DWC_MODE_NO_SRP_CAPABLE_DEVICE:
  22453. + usbcfg.b.hnpcap = 0;
  22454. + usbcfg.b.srpcap = 0;
  22455. + break;
  22456. +
  22457. + case DWC_MODE_SRP_CAPABLE_HOST:
  22458. + usbcfg.b.hnpcap = 0;
  22459. + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
  22460. + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
  22461. + break;
  22462. +
  22463. + case DWC_MODE_NO_SRP_CAPABLE_HOST:
  22464. + usbcfg.b.hnpcap = 0;
  22465. + usbcfg.b.srpcap = 0;
  22466. + break;
  22467. + }
  22468. +
  22469. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  22470. +
  22471. +#ifdef CONFIG_USB_DWC_OTG_LPM
  22472. + if (core_if->core_params->lpm_enable) {
  22473. + glpmcfg_data_t lpmcfg = {.d32 = 0 };
  22474. +
  22475. + /* To enable LPM support set lpm_cap_en bit */
  22476. + lpmcfg.b.lpm_cap_en = 1;
  22477. +
  22478. + /* Make AppL1Res ACK */
  22479. + lpmcfg.b.appl_resp = 1;
  22480. +
  22481. + /* Retry 3 times */
  22482. + lpmcfg.b.retry_count = 3;
  22483. +
  22484. + DWC_MODIFY_REG32(&core_if->core_global_regs->glpmcfg,
  22485. + 0, lpmcfg.d32);
  22486. +
  22487. + }
  22488. +#endif
  22489. + if (core_if->core_params->ic_usb_cap) {
  22490. + gusbcfg_data_t gusbcfg = {.d32 = 0 };
  22491. + gusbcfg.b.ic_usb_cap = 1;
  22492. + DWC_MODIFY_REG32(&core_if->core_global_regs->gusbcfg,
  22493. + 0, gusbcfg.d32);
  22494. + }
  22495. + {
  22496. + gotgctl_data_t gotgctl = {.d32 = 0 };
  22497. + gotgctl.b.otgver = core_if->core_params->otg_ver;
  22498. + DWC_MODIFY_REG32(&core_if->core_global_regs->gotgctl, 0,
  22499. + gotgctl.d32);
  22500. + /* Set OTG version supported */
  22501. + core_if->otg_ver = core_if->core_params->otg_ver;
  22502. + DWC_PRINTF("OTG VER PARAM: %d, OTG VER FLAG: %d\n",
  22503. + core_if->core_params->otg_ver, core_if->otg_ver);
  22504. + }
  22505. +
  22506. +
  22507. + /* Enable common interrupts */
  22508. + dwc_otg_enable_common_interrupts(core_if);
  22509. +
  22510. + /* Do device or host intialization based on mode during PCD
  22511. + * and HCD initialization */
  22512. + if (dwc_otg_is_host_mode(core_if)) {
  22513. + DWC_DEBUGPL(DBG_ANY, "Host Mode\n");
  22514. + core_if->op_state = A_HOST;
  22515. + } else {
  22516. + DWC_DEBUGPL(DBG_ANY, "Device Mode\n");
  22517. + core_if->op_state = B_PERIPHERAL;
  22518. +#ifdef DWC_DEVICE_ONLY
  22519. + dwc_otg_core_dev_init(core_if);
  22520. +#endif
  22521. + }
  22522. +}
  22523. +
  22524. +/**
  22525. + * This function enables the Device mode interrupts.
  22526. + *
  22527. + * @param core_if Programming view of DWC_otg controller
  22528. + */
  22529. +void dwc_otg_enable_device_interrupts(dwc_otg_core_if_t * core_if)
  22530. +{
  22531. + gintmsk_data_t intr_mask = {.d32 = 0 };
  22532. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  22533. +
  22534. + DWC_DEBUGPL(DBG_CIL, "%s()\n", __func__);
  22535. +
  22536. + /* Disable all interrupts. */
  22537. + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
  22538. +
  22539. + /* Clear any pending interrupts */
  22540. + DWC_WRITE_REG32(&global_regs->gintsts, 0xFFFFFFFF);
  22541. +
  22542. + /* Enable the common interrupts */
  22543. + dwc_otg_enable_common_interrupts(core_if);
  22544. +
  22545. + /* Enable interrupts */
  22546. + intr_mask.b.usbreset = 1;
  22547. + intr_mask.b.enumdone = 1;
  22548. + /* Disable Disconnect interrupt in Device mode */
  22549. + intr_mask.b.disconnect = 0;
  22550. +
  22551. + if (!core_if->multiproc_int_enable) {
  22552. + intr_mask.b.inepintr = 1;
  22553. + intr_mask.b.outepintr = 1;
  22554. + }
  22555. +
  22556. + intr_mask.b.erlysuspend = 1;
  22557. +
  22558. + if (core_if->en_multiple_tx_fifo == 0) {
  22559. + intr_mask.b.epmismatch = 1;
  22560. + }
  22561. +
  22562. + //intr_mask.b.incomplisoout = 1;
  22563. + intr_mask.b.incomplisoin = 1;
  22564. +
  22565. +/* Enable the ignore frame number for ISOC xfers - MAS */
  22566. +/* Disable to support high bandwith ISOC transfers - manukz */
  22567. +#if 0
  22568. +#ifdef DWC_UTE_PER_IO
  22569. + if (core_if->dma_enable) {
  22570. + if (core_if->dma_desc_enable) {
  22571. + dctl_data_t dctl1 = {.d32 = 0 };
  22572. + dctl1.b.ifrmnum = 1;
  22573. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  22574. + dctl, 0, dctl1.d32);
  22575. + DWC_DEBUG("----Enabled Ignore frame number (0x%08x)",
  22576. + DWC_READ_REG32(&core_if->dev_if->
  22577. + dev_global_regs->dctl));
  22578. + }
  22579. + }
  22580. +#endif
  22581. +#endif
  22582. +#ifdef DWC_EN_ISOC
  22583. + if (core_if->dma_enable) {
  22584. + if (core_if->dma_desc_enable == 0) {
  22585. + if (core_if->pti_enh_enable) {
  22586. + dctl_data_t dctl = {.d32 = 0 };
  22587. + dctl.b.ifrmnum = 1;
  22588. + DWC_MODIFY_REG32(&core_if->
  22589. + dev_if->dev_global_regs->dctl,
  22590. + 0, dctl.d32);
  22591. + } else {
  22592. + intr_mask.b.incomplisoin = 1;
  22593. + intr_mask.b.incomplisoout = 1;
  22594. + }
  22595. + }
  22596. + } else {
  22597. + intr_mask.b.incomplisoin = 1;
  22598. + intr_mask.b.incomplisoout = 1;
  22599. + }
  22600. +#endif /* DWC_EN_ISOC */
  22601. +
  22602. + /** @todo NGS: Should this be a module parameter? */
  22603. +#ifdef USE_PERIODIC_EP
  22604. + intr_mask.b.isooutdrop = 1;
  22605. + intr_mask.b.eopframe = 1;
  22606. + intr_mask.b.incomplisoin = 1;
  22607. + intr_mask.b.incomplisoout = 1;
  22608. +#endif
  22609. +
  22610. + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32, intr_mask.d32);
  22611. +
  22612. + DWC_DEBUGPL(DBG_CIL, "%s() gintmsk=%0x\n", __func__,
  22613. + DWC_READ_REG32(&global_regs->gintmsk));
  22614. +}
  22615. +
  22616. +/**
  22617. + * This function initializes the DWC_otg controller registers for
  22618. + * device mode.
  22619. + *
  22620. + * @param core_if Programming view of DWC_otg controller
  22621. + *
  22622. + */
  22623. +void dwc_otg_core_dev_init(dwc_otg_core_if_t * core_if)
  22624. +{
  22625. + int i;
  22626. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  22627. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  22628. + dwc_otg_core_params_t *params = core_if->core_params;
  22629. + dcfg_data_t dcfg = {.d32 = 0 };
  22630. + depctl_data_t diepctl = {.d32 = 0 };
  22631. + grstctl_t resetctl = {.d32 = 0 };
  22632. + uint32_t rx_fifo_size;
  22633. + fifosize_data_t nptxfifosize;
  22634. + fifosize_data_t txfifosize;
  22635. + dthrctl_data_t dthrctl;
  22636. + fifosize_data_t ptxfifosize;
  22637. + uint16_t rxfsiz, nptxfsiz;
  22638. + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
  22639. + hwcfg3_data_t hwcfg3 = {.d32 = 0 };
  22640. +
  22641. + /* Restart the Phy Clock */
  22642. + DWC_WRITE_REG32(core_if->pcgcctl, 0);
  22643. +
  22644. + /* Device configuration register */
  22645. + init_devspd(core_if);
  22646. + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
  22647. + dcfg.b.descdma = (core_if->dma_desc_enable) ? 1 : 0;
  22648. + dcfg.b.perfrint = DWC_DCFG_FRAME_INTERVAL_80;
  22649. + /* Enable Device OUT NAK in case of DDMA mode*/
  22650. + if (core_if->core_params->dev_out_nak) {
  22651. + dcfg.b.endevoutnak = 1;
  22652. + }
  22653. +
  22654. + if (core_if->core_params->cont_on_bna) {
  22655. + dctl_data_t dctl = {.d32 = 0 };
  22656. + dctl.b.encontonbna = 1;
  22657. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, 0, dctl.d32);
  22658. + }
  22659. +
  22660. +
  22661. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
  22662. +
  22663. + /* Configure data FIFO sizes */
  22664. + if (core_if->hwcfg2.b.dynamic_fifo && params->enable_dynamic_fifo) {
  22665. + DWC_DEBUGPL(DBG_CIL, "Total FIFO Size=%d\n",
  22666. + core_if->total_fifo_size);
  22667. + DWC_DEBUGPL(DBG_CIL, "Rx FIFO Size=%d\n",
  22668. + params->dev_rx_fifo_size);
  22669. + DWC_DEBUGPL(DBG_CIL, "NP Tx FIFO Size=%d\n",
  22670. + params->dev_nperio_tx_fifo_size);
  22671. +
  22672. + /* Rx FIFO */
  22673. + DWC_DEBUGPL(DBG_CIL, "initial grxfsiz=%08x\n",
  22674. + DWC_READ_REG32(&global_regs->grxfsiz));
  22675. +
  22676. +#ifdef DWC_UTE_CFI
  22677. + core_if->pwron_rxfsiz = DWC_READ_REG32(&global_regs->grxfsiz);
  22678. + core_if->init_rxfsiz = params->dev_rx_fifo_size;
  22679. +#endif
  22680. + rx_fifo_size = params->dev_rx_fifo_size;
  22681. + DWC_WRITE_REG32(&global_regs->grxfsiz, rx_fifo_size);
  22682. +
  22683. + DWC_DEBUGPL(DBG_CIL, "new grxfsiz=%08x\n",
  22684. + DWC_READ_REG32(&global_regs->grxfsiz));
  22685. +
  22686. + /** Set Periodic Tx FIFO Mask all bits 0 */
  22687. + core_if->p_tx_msk = 0;
  22688. +
  22689. + /** Set Tx FIFO Mask all bits 0 */
  22690. + core_if->tx_msk = 0;
  22691. +
  22692. + if (core_if->en_multiple_tx_fifo == 0) {
  22693. + /* Non-periodic Tx FIFO */
  22694. + DWC_DEBUGPL(DBG_CIL, "initial gnptxfsiz=%08x\n",
  22695. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  22696. +
  22697. + nptxfifosize.b.depth = params->dev_nperio_tx_fifo_size;
  22698. + nptxfifosize.b.startaddr = params->dev_rx_fifo_size;
  22699. +
  22700. + DWC_WRITE_REG32(&global_regs->gnptxfsiz,
  22701. + nptxfifosize.d32);
  22702. +
  22703. + DWC_DEBUGPL(DBG_CIL, "new gnptxfsiz=%08x\n",
  22704. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  22705. +
  22706. + /**@todo NGS: Fix Periodic FIFO Sizing! */
  22707. + /*
  22708. + * Periodic Tx FIFOs These FIFOs are numbered from 1 to 15.
  22709. + * Indexes of the FIFO size module parameters in the
  22710. + * dev_perio_tx_fifo_size array and the FIFO size registers in
  22711. + * the dptxfsiz array run from 0 to 14.
  22712. + */
  22713. + /** @todo Finish debug of this */
  22714. + ptxfifosize.b.startaddr =
  22715. + nptxfifosize.b.startaddr + nptxfifosize.b.depth;
  22716. + for (i = 0; i < core_if->hwcfg4.b.num_dev_perio_in_ep; i++) {
  22717. + ptxfifosize.b.depth =
  22718. + params->dev_perio_tx_fifo_size[i];
  22719. + DWC_DEBUGPL(DBG_CIL,
  22720. + "initial dtxfsiz[%d]=%08x\n", i,
  22721. + DWC_READ_REG32(&global_regs->dtxfsiz
  22722. + [i]));
  22723. + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
  22724. + ptxfifosize.d32);
  22725. + DWC_DEBUGPL(DBG_CIL, "new dtxfsiz[%d]=%08x\n",
  22726. + i,
  22727. + DWC_READ_REG32(&global_regs->dtxfsiz
  22728. + [i]));
  22729. + ptxfifosize.b.startaddr += ptxfifosize.b.depth;
  22730. + }
  22731. + } else {
  22732. + /*
  22733. + * Tx FIFOs These FIFOs are numbered from 1 to 15.
  22734. + * Indexes of the FIFO size module parameters in the
  22735. + * dev_tx_fifo_size array and the FIFO size registers in
  22736. + * the dtxfsiz array run from 0 to 14.
  22737. + */
  22738. +
  22739. + /* Non-periodic Tx FIFO */
  22740. + DWC_DEBUGPL(DBG_CIL, "initial gnptxfsiz=%08x\n",
  22741. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  22742. +
  22743. +#ifdef DWC_UTE_CFI
  22744. + core_if->pwron_gnptxfsiz =
  22745. + (DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16);
  22746. + core_if->init_gnptxfsiz =
  22747. + params->dev_nperio_tx_fifo_size;
  22748. +#endif
  22749. + nptxfifosize.b.depth = params->dev_nperio_tx_fifo_size;
  22750. + nptxfifosize.b.startaddr = params->dev_rx_fifo_size;
  22751. +
  22752. + DWC_WRITE_REG32(&global_regs->gnptxfsiz,
  22753. + nptxfifosize.d32);
  22754. +
  22755. + DWC_DEBUGPL(DBG_CIL, "new gnptxfsiz=%08x\n",
  22756. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  22757. +
  22758. + txfifosize.b.startaddr =
  22759. + nptxfifosize.b.startaddr + nptxfifosize.b.depth;
  22760. +
  22761. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  22762. +
  22763. + txfifosize.b.depth =
  22764. + params->dev_tx_fifo_size[i];
  22765. +
  22766. + DWC_DEBUGPL(DBG_CIL,
  22767. + "initial dtxfsiz[%d]=%08x\n",
  22768. + i,
  22769. + DWC_READ_REG32(&global_regs->dtxfsiz
  22770. + [i]));
  22771. +
  22772. +#ifdef DWC_UTE_CFI
  22773. + core_if->pwron_txfsiz[i] =
  22774. + (DWC_READ_REG32
  22775. + (&global_regs->dtxfsiz[i]) >> 16);
  22776. + core_if->init_txfsiz[i] =
  22777. + params->dev_tx_fifo_size[i];
  22778. +#endif
  22779. + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
  22780. + txfifosize.d32);
  22781. +
  22782. + DWC_DEBUGPL(DBG_CIL,
  22783. + "new dtxfsiz[%d]=%08x\n",
  22784. + i,
  22785. + DWC_READ_REG32(&global_regs->dtxfsiz
  22786. + [i]));
  22787. +
  22788. + txfifosize.b.startaddr += txfifosize.b.depth;
  22789. + }
  22790. + if (core_if->snpsid <= OTG_CORE_REV_2_94a) {
  22791. + /* Calculating DFIFOCFG for Device mode to include RxFIFO and NPTXFIFO */
  22792. + gdfifocfg.d32 = DWC_READ_REG32(&global_regs->gdfifocfg);
  22793. + hwcfg3.d32 = DWC_READ_REG32(&global_regs->ghwcfg3);
  22794. + gdfifocfg.b.gdfifocfg = (DWC_READ_REG32(&global_regs->ghwcfg3) >> 16);
  22795. + DWC_WRITE_REG32(&global_regs->gdfifocfg, gdfifocfg.d32);
  22796. + rxfsiz = (DWC_READ_REG32(&global_regs->grxfsiz) & 0x0000ffff);
  22797. + nptxfsiz = (DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16);
  22798. + gdfifocfg.b.epinfobase = rxfsiz + nptxfsiz;
  22799. + DWC_WRITE_REG32(&global_regs->gdfifocfg, gdfifocfg.d32);
  22800. + }
  22801. + }
  22802. +
  22803. + /* Flush the FIFOs */
  22804. + dwc_otg_flush_tx_fifo(core_if, 0x10); /* all Tx FIFOs */
  22805. + dwc_otg_flush_rx_fifo(core_if);
  22806. +
  22807. + /* Flush the Learning Queue. */
  22808. + resetctl.b.intknqflsh = 1;
  22809. + DWC_WRITE_REG32(&core_if->core_global_regs->grstctl, resetctl.d32);
  22810. +
  22811. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable) {
  22812. + core_if->start_predict = 0;
  22813. + for (i = 0; i<= core_if->dev_if->num_in_eps; ++i) {
  22814. + core_if->nextep_seq[i] = 0xff; // 0xff - EP not active
  22815. + }
  22816. + core_if->nextep_seq[0] = 0;
  22817. + core_if->first_in_nextep_seq = 0;
  22818. + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl);
  22819. + diepctl.b.nextep = 0;
  22820. + DWC_WRITE_REG32(&dev_if->in_ep_regs[0]->diepctl, diepctl.d32);
  22821. +
  22822. + /* Update IN Endpoint Mismatch Count by active IN NP EP count + 1 */
  22823. + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
  22824. + dcfg.b.epmscnt = 2;
  22825. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
  22826. +
  22827. + DWC_DEBUGPL(DBG_CILV,"%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
  22828. + __func__, core_if->first_in_nextep_seq);
  22829. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  22830. + DWC_DEBUGPL(DBG_CILV, "%2d ", core_if->nextep_seq[i]);
  22831. + }
  22832. + DWC_DEBUGPL(DBG_CILV,"\n");
  22833. + }
  22834. +
  22835. + /* Clear all pending Device Interrupts */
  22836. + /** @todo - if the condition needed to be checked
  22837. + * or in any case all pending interrutps should be cleared?
  22838. + */
  22839. + if (core_if->multiproc_int_enable) {
  22840. + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
  22841. + DWC_WRITE_REG32(&dev_if->
  22842. + dev_global_regs->diepeachintmsk[i], 0);
  22843. + }
  22844. + }
  22845. +
  22846. + for (i = 0; i < core_if->dev_if->num_out_eps; ++i) {
  22847. + DWC_WRITE_REG32(&dev_if->
  22848. + dev_global_regs->doepeachintmsk[i], 0);
  22849. + }
  22850. +
  22851. + DWC_WRITE_REG32(&dev_if->dev_global_regs->deachint, 0xFFFFFFFF);
  22852. + DWC_WRITE_REG32(&dev_if->dev_global_regs->deachintmsk, 0);
  22853. + } else {
  22854. + DWC_WRITE_REG32(&dev_if->dev_global_regs->diepmsk, 0);
  22855. + DWC_WRITE_REG32(&dev_if->dev_global_regs->doepmsk, 0);
  22856. + DWC_WRITE_REG32(&dev_if->dev_global_regs->daint, 0xFFFFFFFF);
  22857. + DWC_WRITE_REG32(&dev_if->dev_global_regs->daintmsk, 0);
  22858. + }
  22859. +
  22860. + for (i = 0; i <= dev_if->num_in_eps; i++) {
  22861. + depctl_data_t depctl;
  22862. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  22863. + if (depctl.b.epena) {
  22864. + depctl.d32 = 0;
  22865. + depctl.b.epdis = 1;
  22866. + depctl.b.snak = 1;
  22867. + } else {
  22868. + depctl.d32 = 0;
  22869. + }
  22870. +
  22871. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32);
  22872. +
  22873. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->dieptsiz, 0);
  22874. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepdma, 0);
  22875. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepint, 0xFF);
  22876. + }
  22877. +
  22878. + for (i = 0; i <= dev_if->num_out_eps; i++) {
  22879. + depctl_data_t depctl;
  22880. + depctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[i]->doepctl);
  22881. + if (depctl.b.epena) {
  22882. + dctl_data_t dctl = {.d32 = 0 };
  22883. + gintmsk_data_t gintsts = {.d32 = 0 };
  22884. + doepint_data_t doepint = {.d32 = 0 };
  22885. + dctl.b.sgoutnak = 1;
  22886. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  22887. + do {
  22888. + dwc_udelay(10);
  22889. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  22890. + } while (!gintsts.b.goutnakeff);
  22891. + gintsts.d32 = 0;
  22892. + gintsts.b.goutnakeff = 1;
  22893. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  22894. +
  22895. + depctl.d32 = 0;
  22896. + depctl.b.epdis = 1;
  22897. + depctl.b.snak = 1;
  22898. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->doepctl, depctl.d32);
  22899. + do {
  22900. + dwc_udelay(10);
  22901. + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  22902. + out_ep_regs[i]->doepint);
  22903. + } while (!doepint.b.epdisabled);
  22904. +
  22905. + doepint.b.epdisabled = 1;
  22906. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->doepint, doepint.d32);
  22907. +
  22908. + dctl.d32 = 0;
  22909. + dctl.b.cgoutnak = 1;
  22910. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  22911. + } else {
  22912. + depctl.d32 = 0;
  22913. + }
  22914. +
  22915. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepctl, depctl.d32);
  22916. +
  22917. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doeptsiz, 0);
  22918. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepdma, 0);
  22919. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepint, 0xFF);
  22920. + }
  22921. +
  22922. + if (core_if->en_multiple_tx_fifo && core_if->dma_enable) {
  22923. + dev_if->non_iso_tx_thr_en = params->thr_ctl & 0x1;
  22924. + dev_if->iso_tx_thr_en = (params->thr_ctl >> 1) & 0x1;
  22925. + dev_if->rx_thr_en = (params->thr_ctl >> 2) & 0x1;
  22926. +
  22927. + dev_if->rx_thr_length = params->rx_thr_length;
  22928. + dev_if->tx_thr_length = params->tx_thr_length;
  22929. +
  22930. + dev_if->setup_desc_index = 0;
  22931. +
  22932. + dthrctl.d32 = 0;
  22933. + dthrctl.b.non_iso_thr_en = dev_if->non_iso_tx_thr_en;
  22934. + dthrctl.b.iso_thr_en = dev_if->iso_tx_thr_en;
  22935. + dthrctl.b.tx_thr_len = dev_if->tx_thr_length;
  22936. + dthrctl.b.rx_thr_en = dev_if->rx_thr_en;
  22937. + dthrctl.b.rx_thr_len = dev_if->rx_thr_length;
  22938. + dthrctl.b.ahb_thr_ratio = params->ahb_thr_ratio;
  22939. +
  22940. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dtknqr3_dthrctl,
  22941. + dthrctl.d32);
  22942. +
  22943. + DWC_DEBUGPL(DBG_CIL,
  22944. + "Non ISO Tx Thr - %d\nISO Tx Thr - %d\nRx Thr - %d\nTx Thr Len - %d\nRx Thr Len - %d\n",
  22945. + dthrctl.b.non_iso_thr_en, dthrctl.b.iso_thr_en,
  22946. + dthrctl.b.rx_thr_en, dthrctl.b.tx_thr_len,
  22947. + dthrctl.b.rx_thr_len);
  22948. +
  22949. + }
  22950. +
  22951. + dwc_otg_enable_device_interrupts(core_if);
  22952. +
  22953. + {
  22954. + diepmsk_data_t msk = {.d32 = 0 };
  22955. + msk.b.txfifoundrn = 1;
  22956. + if (core_if->multiproc_int_enable) {
  22957. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->
  22958. + diepeachintmsk[0], msk.d32, msk.d32);
  22959. + } else {
  22960. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->diepmsk,
  22961. + msk.d32, msk.d32);
  22962. + }
  22963. + }
  22964. +
  22965. + if (core_if->multiproc_int_enable) {
  22966. + /* Set NAK on Babble */
  22967. + dctl_data_t dctl = {.d32 = 0 };
  22968. + dctl.b.nakonbble = 1;
  22969. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, 0, dctl.d32);
  22970. + }
  22971. +
  22972. + if (core_if->snpsid >= OTG_CORE_REV_2_94a) {
  22973. + dctl_data_t dctl = {.d32 = 0 };
  22974. + dctl.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dctl);
  22975. + dctl.b.sftdiscon = 0;
  22976. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dctl, dctl.d32);
  22977. + }
  22978. +}
  22979. +
  22980. +/**
  22981. + * This function enables the Host mode interrupts.
  22982. + *
  22983. + * @param core_if Programming view of DWC_otg controller
  22984. + */
  22985. +void dwc_otg_enable_host_interrupts(dwc_otg_core_if_t * core_if)
  22986. +{
  22987. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  22988. + gintmsk_data_t intr_mask = {.d32 = 0 };
  22989. +
  22990. + DWC_DEBUGPL(DBG_CIL, "%s(%p)\n", __func__, core_if);
  22991. +
  22992. + /* Disable all interrupts. */
  22993. + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
  22994. +
  22995. + /* Clear any pending interrupts. */
  22996. + DWC_WRITE_REG32(&global_regs->gintsts, 0xFFFFFFFF);
  22997. +
  22998. + /* Enable the common interrupts */
  22999. + dwc_otg_enable_common_interrupts(core_if);
  23000. +
  23001. + /*
  23002. + * Enable host mode interrupts without disturbing common
  23003. + * interrupts.
  23004. + */
  23005. +
  23006. + intr_mask.b.disconnect = 1;
  23007. + intr_mask.b.portintr = 1;
  23008. + intr_mask.b.hcintr = 1;
  23009. +
  23010. + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32, intr_mask.d32);
  23011. +}
  23012. +
  23013. +/**
  23014. + * This function disables the Host Mode interrupts.
  23015. + *
  23016. + * @param core_if Programming view of DWC_otg controller
  23017. + */
  23018. +void dwc_otg_disable_host_interrupts(dwc_otg_core_if_t * core_if)
  23019. +{
  23020. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  23021. + gintmsk_data_t intr_mask = {.d32 = 0 };
  23022. +
  23023. + DWC_DEBUGPL(DBG_CILV, "%s()\n", __func__);
  23024. +
  23025. + /*
  23026. + * Disable host mode interrupts without disturbing common
  23027. + * interrupts.
  23028. + */
  23029. + intr_mask.b.sofintr = 1;
  23030. + intr_mask.b.portintr = 1;
  23031. + intr_mask.b.hcintr = 1;
  23032. + intr_mask.b.ptxfempty = 1;
  23033. + intr_mask.b.nptxfempty = 1;
  23034. +
  23035. + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32, 0);
  23036. +}
  23037. +
  23038. +/**
  23039. + * This function initializes the DWC_otg controller registers for
  23040. + * host mode.
  23041. + *
  23042. + * This function flushes the Tx and Rx FIFOs and it flushes any entries in the
  23043. + * request queues. Host channels are reset to ensure that they are ready for
  23044. + * performing transfers.
  23045. + *
  23046. + * @param core_if Programming view of DWC_otg controller
  23047. + *
  23048. + */
  23049. +void dwc_otg_core_host_init(dwc_otg_core_if_t * core_if)
  23050. +{
  23051. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  23052. + dwc_otg_host_if_t *host_if = core_if->host_if;
  23053. + dwc_otg_core_params_t *params = core_if->core_params;
  23054. + hprt0_data_t hprt0 = {.d32 = 0 };
  23055. + fifosize_data_t nptxfifosize;
  23056. + fifosize_data_t ptxfifosize;
  23057. + uint16_t rxfsiz, nptxfsiz, hptxfsiz;
  23058. + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
  23059. + int i;
  23060. + hcchar_data_t hcchar;
  23061. + hcfg_data_t hcfg;
  23062. + hfir_data_t hfir;
  23063. + dwc_otg_hc_regs_t *hc_regs;
  23064. + int num_channels;
  23065. + gotgctl_data_t gotgctl = {.d32 = 0 };
  23066. +
  23067. + DWC_DEBUGPL(DBG_CILV, "%s(%p)\n", __func__, core_if);
  23068. +
  23069. + /* Restart the Phy Clock */
  23070. + DWC_WRITE_REG32(core_if->pcgcctl, 0);
  23071. +
  23072. + /* Initialize Host Configuration Register */
  23073. + init_fslspclksel(core_if);
  23074. + if (core_if->core_params->speed == DWC_SPEED_PARAM_FULL) {
  23075. + hcfg.d32 = DWC_READ_REG32(&host_if->host_global_regs->hcfg);
  23076. + hcfg.b.fslssupp = 1;
  23077. + DWC_WRITE_REG32(&host_if->host_global_regs->hcfg, hcfg.d32);
  23078. +
  23079. + }
  23080. +
  23081. + /* This bit allows dynamic reloading of the HFIR register
  23082. + * during runtime. This bit needs to be programmed during
  23083. + * initial configuration and its value must not be changed
  23084. + * during runtime.*/
  23085. + if (core_if->core_params->reload_ctl == 1) {
  23086. + hfir.d32 = DWC_READ_REG32(&host_if->host_global_regs->hfir);
  23087. + hfir.b.hfirrldctrl = 1;
  23088. + DWC_WRITE_REG32(&host_if->host_global_regs->hfir, hfir.d32);
  23089. + }
  23090. +
  23091. + if (core_if->core_params->dma_desc_enable) {
  23092. + uint8_t op_mode = core_if->hwcfg2.b.op_mode;
  23093. + if (!
  23094. + (core_if->hwcfg4.b.desc_dma
  23095. + && (core_if->snpsid >= OTG_CORE_REV_2_90a)
  23096. + && ((op_mode == DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
  23097. + || (op_mode == DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG)
  23098. + || (op_mode ==
  23099. + DWC_HWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE_OTG)
  23100. + || (op_mode == DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST)
  23101. + || (op_mode ==
  23102. + DWC_HWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST)))) {
  23103. +
  23104. + DWC_ERROR("Host can't operate in Descriptor DMA mode.\n"
  23105. + "Either core version is below 2.90a or "
  23106. + "GHWCFG2, GHWCFG4 registers' values do not allow Descriptor DMA in host mode.\n"
  23107. + "To run the driver in Buffer DMA host mode set dma_desc_enable "
  23108. + "module parameter to 0.\n");
  23109. + return;
  23110. + }
  23111. + hcfg.d32 = DWC_READ_REG32(&host_if->host_global_regs->hcfg);
  23112. + hcfg.b.descdma = 1;
  23113. + DWC_WRITE_REG32(&host_if->host_global_regs->hcfg, hcfg.d32);
  23114. + }
  23115. +
  23116. + /* Configure data FIFO sizes */
  23117. + if (core_if->hwcfg2.b.dynamic_fifo && params->enable_dynamic_fifo) {
  23118. + DWC_DEBUGPL(DBG_CIL, "Total FIFO Size=%d\n",
  23119. + core_if->total_fifo_size);
  23120. + DWC_DEBUGPL(DBG_CIL, "Rx FIFO Size=%d\n",
  23121. + params->host_rx_fifo_size);
  23122. + DWC_DEBUGPL(DBG_CIL, "NP Tx FIFO Size=%d\n",
  23123. + params->host_nperio_tx_fifo_size);
  23124. + DWC_DEBUGPL(DBG_CIL, "P Tx FIFO Size=%d\n",
  23125. + params->host_perio_tx_fifo_size);
  23126. +
  23127. + /* Rx FIFO */
  23128. + DWC_DEBUGPL(DBG_CIL, "initial grxfsiz=%08x\n",
  23129. + DWC_READ_REG32(&global_regs->grxfsiz));
  23130. + DWC_WRITE_REG32(&global_regs->grxfsiz,
  23131. + params->host_rx_fifo_size);
  23132. + DWC_DEBUGPL(DBG_CIL, "new grxfsiz=%08x\n",
  23133. + DWC_READ_REG32(&global_regs->grxfsiz));
  23134. +
  23135. + /* Non-periodic Tx FIFO */
  23136. + DWC_DEBUGPL(DBG_CIL, "initial gnptxfsiz=%08x\n",
  23137. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  23138. + nptxfifosize.b.depth = params->host_nperio_tx_fifo_size;
  23139. + nptxfifosize.b.startaddr = params->host_rx_fifo_size;
  23140. + DWC_WRITE_REG32(&global_regs->gnptxfsiz, nptxfifosize.d32);
  23141. + DWC_DEBUGPL(DBG_CIL, "new gnptxfsiz=%08x\n",
  23142. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  23143. +
  23144. + /* Periodic Tx FIFO */
  23145. + DWC_DEBUGPL(DBG_CIL, "initial hptxfsiz=%08x\n",
  23146. + DWC_READ_REG32(&global_regs->hptxfsiz));
  23147. + ptxfifosize.b.depth = params->host_perio_tx_fifo_size;
  23148. + ptxfifosize.b.startaddr =
  23149. + nptxfifosize.b.startaddr + nptxfifosize.b.depth;
  23150. + DWC_WRITE_REG32(&global_regs->hptxfsiz, ptxfifosize.d32);
  23151. + DWC_DEBUGPL(DBG_CIL, "new hptxfsiz=%08x\n",
  23152. + DWC_READ_REG32(&global_regs->hptxfsiz));
  23153. +
  23154. + if (core_if->en_multiple_tx_fifo
  23155. + && core_if->snpsid <= OTG_CORE_REV_2_94a) {
  23156. + /* Global DFIFOCFG calculation for Host mode - include RxFIFO, NPTXFIFO and HPTXFIFO */
  23157. + gdfifocfg.d32 = DWC_READ_REG32(&global_regs->gdfifocfg);
  23158. + rxfsiz = (DWC_READ_REG32(&global_regs->grxfsiz) & 0x0000ffff);
  23159. + nptxfsiz = (DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16);
  23160. + hptxfsiz = (DWC_READ_REG32(&global_regs->hptxfsiz) >> 16);
  23161. + gdfifocfg.b.epinfobase = rxfsiz + nptxfsiz + hptxfsiz;
  23162. + DWC_WRITE_REG32(&global_regs->gdfifocfg, gdfifocfg.d32);
  23163. + }
  23164. + }
  23165. +
  23166. + /* TODO - check this */
  23167. + /* Clear Host Set HNP Enable in the OTG Control Register */
  23168. + gotgctl.b.hstsethnpen = 1;
  23169. + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
  23170. + /* Make sure the FIFOs are flushed. */
  23171. + dwc_otg_flush_tx_fifo(core_if, 0x10 /* all TX FIFOs */ );
  23172. + dwc_otg_flush_rx_fifo(core_if);
  23173. +
  23174. + /* Clear Host Set HNP Enable in the OTG Control Register */
  23175. + gotgctl.b.hstsethnpen = 1;
  23176. + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
  23177. +
  23178. + if (!core_if->core_params->dma_desc_enable) {
  23179. + /* Flush out any leftover queued requests. */
  23180. + num_channels = core_if->core_params->host_channels;
  23181. +
  23182. + for (i = 0; i < num_channels; i++) {
  23183. + hc_regs = core_if->host_if->hc_regs[i];
  23184. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  23185. + hcchar.b.chen = 0;
  23186. + hcchar.b.chdis = 1;
  23187. + hcchar.b.epdir = 0;
  23188. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  23189. + }
  23190. +
  23191. + /* Halt all channels to put them into a known state. */
  23192. + for (i = 0; i < num_channels; i++) {
  23193. + int count = 0;
  23194. + hc_regs = core_if->host_if->hc_regs[i];
  23195. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  23196. + hcchar.b.chen = 1;
  23197. + hcchar.b.chdis = 1;
  23198. + hcchar.b.epdir = 0;
  23199. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  23200. + DWC_DEBUGPL(DBG_HCDV, "%s: Halt channel %d regs %p\n", __func__, i, hc_regs);
  23201. + do {
  23202. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  23203. + if (++count > 1000) {
  23204. + DWC_ERROR
  23205. + ("%s: Unable to clear halt on channel %d (timeout HCCHAR 0x%X @%p)\n",
  23206. + __func__, i, hcchar.d32, &hc_regs->hcchar);
  23207. + break;
  23208. + }
  23209. + dwc_udelay(1);
  23210. + } while (hcchar.b.chen);
  23211. + }
  23212. + }
  23213. +
  23214. + /* Turn on the vbus power. */
  23215. + DWC_PRINTF("Init: Port Power? op_state=%d\n", core_if->op_state);
  23216. + if (core_if->op_state == A_HOST) {
  23217. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  23218. + DWC_PRINTF("Init: Power Port (%d)\n", hprt0.b.prtpwr);
  23219. + if (hprt0.b.prtpwr == 0) {
  23220. + hprt0.b.prtpwr = 1;
  23221. + DWC_WRITE_REG32(host_if->hprt0, hprt0.d32);
  23222. + }
  23223. + }
  23224. +
  23225. + dwc_otg_enable_host_interrupts(core_if);
  23226. +}
  23227. +
  23228. +/**
  23229. + * Prepares a host channel for transferring packets to/from a specific
  23230. + * endpoint. The HCCHARn register is set up with the characteristics specified
  23231. + * in _hc. Host channel interrupts that may need to be serviced while this
  23232. + * transfer is in progress are enabled.
  23233. + *
  23234. + * @param core_if Programming view of DWC_otg controller
  23235. + * @param hc Information needed to initialize the host channel
  23236. + */
  23237. +void dwc_otg_hc_init(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  23238. +{
  23239. + hcintmsk_data_t hc_intr_mask;
  23240. + hcchar_data_t hcchar;
  23241. + hcsplt_data_t hcsplt;
  23242. +
  23243. + uint8_t hc_num = hc->hc_num;
  23244. + dwc_otg_host_if_t *host_if = core_if->host_if;
  23245. + dwc_otg_hc_regs_t *hc_regs = host_if->hc_regs[hc_num];
  23246. +
  23247. + /* Clear old interrupt conditions for this host channel. */
  23248. + hc_intr_mask.d32 = 0xFFFFFFFF;
  23249. + hc_intr_mask.b.reserved14_31 = 0;
  23250. + DWC_WRITE_REG32(&hc_regs->hcint, hc_intr_mask.d32);
  23251. +
  23252. + /* Enable channel interrupts required for this transfer. */
  23253. + hc_intr_mask.d32 = 0;
  23254. + hc_intr_mask.b.chhltd = 1;
  23255. + if (core_if->dma_enable) {
  23256. + /* For Descriptor DMA mode core halts the channel on AHB error. Interrupt is not required */
  23257. + if (!core_if->dma_desc_enable)
  23258. + hc_intr_mask.b.ahberr = 1;
  23259. + else {
  23260. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
  23261. + hc_intr_mask.b.xfercompl = 1;
  23262. + }
  23263. +
  23264. + if (hc->error_state && !hc->do_split &&
  23265. + hc->ep_type != DWC_OTG_EP_TYPE_ISOC) {
  23266. + hc_intr_mask.b.ack = 1;
  23267. + if (hc->ep_is_in) {
  23268. + hc_intr_mask.b.datatglerr = 1;
  23269. + if (hc->ep_type != DWC_OTG_EP_TYPE_INTR) {
  23270. + hc_intr_mask.b.nak = 1;
  23271. + }
  23272. + }
  23273. + }
  23274. + } else {
  23275. + switch (hc->ep_type) {
  23276. + case DWC_OTG_EP_TYPE_CONTROL:
  23277. + case DWC_OTG_EP_TYPE_BULK:
  23278. + hc_intr_mask.b.xfercompl = 1;
  23279. + hc_intr_mask.b.stall = 1;
  23280. + hc_intr_mask.b.xacterr = 1;
  23281. + hc_intr_mask.b.datatglerr = 1;
  23282. + if (hc->ep_is_in) {
  23283. + hc_intr_mask.b.bblerr = 1;
  23284. + } else {
  23285. + hc_intr_mask.b.nak = 1;
  23286. + hc_intr_mask.b.nyet = 1;
  23287. + if (hc->do_ping) {
  23288. + hc_intr_mask.b.ack = 1;
  23289. + }
  23290. + }
  23291. +
  23292. + if (hc->do_split) {
  23293. + hc_intr_mask.b.nak = 1;
  23294. + if (hc->complete_split) {
  23295. + hc_intr_mask.b.nyet = 1;
  23296. + } else {
  23297. + hc_intr_mask.b.ack = 1;
  23298. + }
  23299. + }
  23300. +
  23301. + if (hc->error_state) {
  23302. + hc_intr_mask.b.ack = 1;
  23303. + }
  23304. + break;
  23305. + case DWC_OTG_EP_TYPE_INTR:
  23306. + hc_intr_mask.b.xfercompl = 1;
  23307. + hc_intr_mask.b.nak = 1;
  23308. + hc_intr_mask.b.stall = 1;
  23309. + hc_intr_mask.b.xacterr = 1;
  23310. + hc_intr_mask.b.datatglerr = 1;
  23311. + hc_intr_mask.b.frmovrun = 1;
  23312. +
  23313. + if (hc->ep_is_in) {
  23314. + hc_intr_mask.b.bblerr = 1;
  23315. + }
  23316. + if (hc->error_state) {
  23317. + hc_intr_mask.b.ack = 1;
  23318. + }
  23319. + if (hc->do_split) {
  23320. + if (hc->complete_split) {
  23321. + hc_intr_mask.b.nyet = 1;
  23322. + } else {
  23323. + hc_intr_mask.b.ack = 1;
  23324. + }
  23325. + }
  23326. + break;
  23327. + case DWC_OTG_EP_TYPE_ISOC:
  23328. + hc_intr_mask.b.xfercompl = 1;
  23329. + hc_intr_mask.b.frmovrun = 1;
  23330. + hc_intr_mask.b.ack = 1;
  23331. +
  23332. + if (hc->ep_is_in) {
  23333. + hc_intr_mask.b.xacterr = 1;
  23334. + hc_intr_mask.b.bblerr = 1;
  23335. + }
  23336. + break;
  23337. + }
  23338. + }
  23339. + DWC_WRITE_REG32(&hc_regs->hcintmsk, hc_intr_mask.d32);
  23340. +
  23341. + /*
  23342. + * Program the HCCHARn register with the endpoint characteristics for
  23343. + * the current transfer.
  23344. + */
  23345. + hcchar.d32 = 0;
  23346. + hcchar.b.devaddr = hc->dev_addr;
  23347. + hcchar.b.epnum = hc->ep_num;
  23348. + hcchar.b.epdir = hc->ep_is_in;
  23349. + hcchar.b.lspddev = (hc->speed == DWC_OTG_EP_SPEED_LOW);
  23350. + hcchar.b.eptype = hc->ep_type;
  23351. + hcchar.b.mps = hc->max_packet;
  23352. +
  23353. + DWC_WRITE_REG32(&host_if->hc_regs[hc_num]->hcchar, hcchar.d32);
  23354. +
  23355. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d, Dev Addr %d, EP #%d\n",
  23356. + __func__, hc->hc_num, hcchar.b.devaddr, hcchar.b.epnum);
  23357. + DWC_DEBUGPL(DBG_HCDV, " Is In %d, Is Low Speed %d, EP Type %d, "
  23358. + "Max Pkt %d, Multi Cnt %d\n",
  23359. + hcchar.b.epdir, hcchar.b.lspddev, hcchar.b.eptype,
  23360. + hcchar.b.mps, hcchar.b.multicnt);
  23361. +
  23362. + /*
  23363. + * Program the HCSPLIT register for SPLITs
  23364. + */
  23365. + hcsplt.d32 = 0;
  23366. + if (hc->do_split) {
  23367. + DWC_DEBUGPL(DBG_HCDV, "Programming HC %d with split --> %s\n",
  23368. + hc->hc_num,
  23369. + hc->complete_split ? "CSPLIT" : "SSPLIT");
  23370. + hcsplt.b.compsplt = hc->complete_split;
  23371. + hcsplt.b.xactpos = hc->xact_pos;
  23372. + hcsplt.b.hubaddr = hc->hub_addr;
  23373. + hcsplt.b.prtaddr = hc->port_addr;
  23374. + DWC_DEBUGPL(DBG_HCDV, "\t comp split %d\n", hc->complete_split);
  23375. + DWC_DEBUGPL(DBG_HCDV, "\t xact pos %d\n", hc->xact_pos);
  23376. + DWC_DEBUGPL(DBG_HCDV, "\t hub addr %d\n", hc->hub_addr);
  23377. + DWC_DEBUGPL(DBG_HCDV, "\t port addr %d\n", hc->port_addr);
  23378. + DWC_DEBUGPL(DBG_HCDV, "\t is_in %d\n", hc->ep_is_in);
  23379. + DWC_DEBUGPL(DBG_HCDV, "\t Max Pkt: %d\n", hcchar.b.mps);
  23380. + DWC_DEBUGPL(DBG_HCDV, "\t xferlen: %d\n", hc->xfer_len);
  23381. + }
  23382. + DWC_WRITE_REG32(&host_if->hc_regs[hc_num]->hcsplt, hcsplt.d32);
  23383. +
  23384. +}
  23385. +
  23386. +/**
  23387. + * Attempts to halt a host channel. This function should only be called in
  23388. + * Slave mode or to abort a transfer in either Slave mode or DMA mode. Under
  23389. + * normal circumstances in DMA mode, the controller halts the channel when the
  23390. + * transfer is complete or a condition occurs that requires application
  23391. + * intervention.
  23392. + *
  23393. + * In slave mode, checks for a free request queue entry, then sets the Channel
  23394. + * Enable and Channel Disable bits of the Host Channel Characteristics
  23395. + * register of the specified channel to intiate the halt. If there is no free
  23396. + * request queue entry, sets only the Channel Disable bit of the HCCHARn
  23397. + * register to flush requests for this channel. In the latter case, sets a
  23398. + * flag to indicate that the host channel needs to be halted when a request
  23399. + * queue slot is open.
  23400. + *
  23401. + * In DMA mode, always sets the Channel Enable and Channel Disable bits of the
  23402. + * HCCHARn register. The controller ensures there is space in the request
  23403. + * queue before submitting the halt request.
  23404. + *
  23405. + * Some time may elapse before the core flushes any posted requests for this
  23406. + * host channel and halts. The Channel Halted interrupt handler completes the
  23407. + * deactivation of the host channel.
  23408. + *
  23409. + * @param core_if Controller register interface.
  23410. + * @param hc Host channel to halt.
  23411. + * @param halt_status Reason for halting the channel.
  23412. + */
  23413. +void dwc_otg_hc_halt(dwc_otg_core_if_t * core_if,
  23414. + dwc_hc_t * hc, dwc_otg_halt_status_e halt_status)
  23415. +{
  23416. + gnptxsts_data_t nptxsts;
  23417. + hptxsts_data_t hptxsts;
  23418. + hcchar_data_t hcchar;
  23419. + dwc_otg_hc_regs_t *hc_regs;
  23420. + dwc_otg_core_global_regs_t *global_regs;
  23421. + dwc_otg_host_global_regs_t *host_global_regs;
  23422. +
  23423. + hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  23424. + global_regs = core_if->core_global_regs;
  23425. + host_global_regs = core_if->host_if->host_global_regs;
  23426. +
  23427. + DWC_ASSERT(!(halt_status == DWC_OTG_HC_XFER_NO_HALT_STATUS),
  23428. + "halt_status = %d\n", halt_status);
  23429. +
  23430. + if (halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE ||
  23431. + halt_status == DWC_OTG_HC_XFER_AHB_ERR) {
  23432. + /*
  23433. + * Disable all channel interrupts except Ch Halted. The QTD
  23434. + * and QH state associated with this transfer has been cleared
  23435. + * (in the case of URB_DEQUEUE), so the channel needs to be
  23436. + * shut down carefully to prevent crashes.
  23437. + */
  23438. + hcintmsk_data_t hcintmsk;
  23439. + hcintmsk.d32 = 0;
  23440. + hcintmsk.b.chhltd = 1;
  23441. + DWC_WRITE_REG32(&hc_regs->hcintmsk, hcintmsk.d32);
  23442. +
  23443. + /*
  23444. + * Make sure no other interrupts besides halt are currently
  23445. + * pending. Handling another interrupt could cause a crash due
  23446. + * to the QTD and QH state.
  23447. + */
  23448. + DWC_WRITE_REG32(&hc_regs->hcint, ~hcintmsk.d32);
  23449. +
  23450. + /*
  23451. + * Make sure the halt status is set to URB_DEQUEUE or AHB_ERR
  23452. + * even if the channel was already halted for some other
  23453. + * reason.
  23454. + */
  23455. + hc->halt_status = halt_status;
  23456. +
  23457. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  23458. + if (hcchar.b.chen == 0) {
  23459. + /*
  23460. + * The channel is either already halted or it hasn't
  23461. + * started yet. In DMA mode, the transfer may halt if
  23462. + * it finishes normally or a condition occurs that
  23463. + * requires driver intervention. Don't want to halt
  23464. + * the channel again. In either Slave or DMA mode,
  23465. + * it's possible that the transfer has been assigned
  23466. + * to a channel, but not started yet when an URB is
  23467. + * dequeued. Don't want to halt a channel that hasn't
  23468. + * started yet.
  23469. + */
  23470. + return;
  23471. + }
  23472. + }
  23473. + if (hc->halt_pending) {
  23474. + /*
  23475. + * A halt has already been issued for this channel. This might
  23476. + * happen when a transfer is aborted by a higher level in
  23477. + * the stack.
  23478. + */
  23479. +#ifdef DEBUG
  23480. + DWC_PRINTF
  23481. + ("*** %s: Channel %d, _hc->halt_pending already set ***\n",
  23482. + __func__, hc->hc_num);
  23483. +
  23484. +#endif
  23485. + return;
  23486. + }
  23487. +
  23488. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  23489. +
  23490. + /* No need to set the bit in DDMA for disabling the channel */
  23491. + //TODO check it everywhere channel is disabled
  23492. + if (!core_if->core_params->dma_desc_enable)
  23493. + hcchar.b.chen = 1;
  23494. + hcchar.b.chdis = 1;
  23495. +
  23496. + if (!core_if->dma_enable) {
  23497. + /* Check for space in the request queue to issue the halt. */
  23498. + if (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL ||
  23499. + hc->ep_type == DWC_OTG_EP_TYPE_BULK) {
  23500. + nptxsts.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  23501. + if (nptxsts.b.nptxqspcavail == 0) {
  23502. + hcchar.b.chen = 0;
  23503. + }
  23504. + } else {
  23505. + hptxsts.d32 =
  23506. + DWC_READ_REG32(&host_global_regs->hptxsts);
  23507. + if ((hptxsts.b.ptxqspcavail == 0)
  23508. + || (core_if->queuing_high_bandwidth)) {
  23509. + hcchar.b.chen = 0;
  23510. + }
  23511. + }
  23512. + }
  23513. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  23514. +
  23515. + hc->halt_status = halt_status;
  23516. +
  23517. + if (hcchar.b.chen) {
  23518. + hc->halt_pending = 1;
  23519. + hc->halt_on_queue = 0;
  23520. + } else {
  23521. + hc->halt_on_queue = 1;
  23522. + }
  23523. +
  23524. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
  23525. + DWC_DEBUGPL(DBG_HCDV, " hcchar: 0x%08x\n", hcchar.d32);
  23526. + DWC_DEBUGPL(DBG_HCDV, " halt_pending: %d\n", hc->halt_pending);
  23527. + DWC_DEBUGPL(DBG_HCDV, " halt_on_queue: %d\n", hc->halt_on_queue);
  23528. + DWC_DEBUGPL(DBG_HCDV, " halt_status: %d\n", hc->halt_status);
  23529. +
  23530. + return;
  23531. +}
  23532. +
  23533. +/**
  23534. + * Clears the transfer state for a host channel. This function is normally
  23535. + * called after a transfer is done and the host channel is being released.
  23536. + *
  23537. + * @param core_if Programming view of DWC_otg controller.
  23538. + * @param hc Identifies the host channel to clean up.
  23539. + */
  23540. +void dwc_otg_hc_cleanup(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  23541. +{
  23542. + dwc_otg_hc_regs_t *hc_regs;
  23543. +
  23544. + hc->xfer_started = 0;
  23545. +
  23546. + /*
  23547. + * Clear channel interrupt enables and any unhandled channel interrupt
  23548. + * conditions.
  23549. + */
  23550. + hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  23551. + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0);
  23552. + DWC_WRITE_REG32(&hc_regs->hcint, 0xFFFFFFFF);
  23553. +#ifdef DEBUG
  23554. + DWC_TIMER_CANCEL(core_if->hc_xfer_timer[hc->hc_num]);
  23555. +#endif
  23556. +}
  23557. +
  23558. +/**
  23559. + * Sets the channel property that indicates in which frame a periodic transfer
  23560. + * should occur. This is always set to the _next_ frame. This function has no
  23561. + * effect on non-periodic transfers.
  23562. + *
  23563. + * @param core_if Programming view of DWC_otg controller.
  23564. + * @param hc Identifies the host channel to set up and its properties.
  23565. + * @param hcchar Current value of the HCCHAR register for the specified host
  23566. + * channel.
  23567. + */
  23568. +static inline void hc_set_even_odd_frame(dwc_otg_core_if_t * core_if,
  23569. + dwc_hc_t * hc, hcchar_data_t * hcchar)
  23570. +{
  23571. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  23572. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  23573. + hfnum_data_t hfnum;
  23574. + hfnum.d32 =
  23575. + DWC_READ_REG32(&core_if->host_if->host_global_regs->hfnum);
  23576. +
  23577. + /* 1 if _next_ frame is odd, 0 if it's even */
  23578. + hcchar->b.oddfrm = (hfnum.b.frnum & 0x1) ? 0 : 1;
  23579. +#ifdef DEBUG
  23580. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR && hc->do_split
  23581. + && !hc->complete_split) {
  23582. + switch (hfnum.b.frnum & 0x7) {
  23583. + case 7:
  23584. + core_if->hfnum_7_samples++;
  23585. + core_if->hfnum_7_frrem_accum += hfnum.b.frrem;
  23586. + break;
  23587. + case 0:
  23588. + core_if->hfnum_0_samples++;
  23589. + core_if->hfnum_0_frrem_accum += hfnum.b.frrem;
  23590. + break;
  23591. + default:
  23592. + core_if->hfnum_other_samples++;
  23593. + core_if->hfnum_other_frrem_accum +=
  23594. + hfnum.b.frrem;
  23595. + break;
  23596. + }
  23597. + }
  23598. +#endif
  23599. + }
  23600. +}
  23601. +
  23602. +#ifdef DEBUG
  23603. +void hc_xfer_timeout(void *ptr)
  23604. +{
  23605. + hc_xfer_info_t *xfer_info = NULL;
  23606. + int hc_num = 0;
  23607. +
  23608. + if (ptr)
  23609. + xfer_info = (hc_xfer_info_t *) ptr;
  23610. +
  23611. + if (!xfer_info->hc) {
  23612. + DWC_ERROR("xfer_info->hc = %p\n", xfer_info->hc);
  23613. + return;
  23614. + }
  23615. +
  23616. + hc_num = xfer_info->hc->hc_num;
  23617. + DWC_WARN("%s: timeout on channel %d\n", __func__, hc_num);
  23618. + DWC_WARN(" start_hcchar_val 0x%08x\n",
  23619. + xfer_info->core_if->start_hcchar_val[hc_num]);
  23620. +}
  23621. +#endif
  23622. +
  23623. +void ep_xfer_timeout(void *ptr)
  23624. +{
  23625. + ep_xfer_info_t *xfer_info = NULL;
  23626. + int ep_num = 0;
  23627. + dctl_data_t dctl = {.d32 = 0 };
  23628. + gintsts_data_t gintsts = {.d32 = 0 };
  23629. + gintmsk_data_t gintmsk = {.d32 = 0 };
  23630. +
  23631. + if (ptr)
  23632. + xfer_info = (ep_xfer_info_t *) ptr;
  23633. +
  23634. + if (!xfer_info->ep) {
  23635. + DWC_ERROR("xfer_info->ep = %p\n", xfer_info->ep);
  23636. + return;
  23637. + }
  23638. +
  23639. + ep_num = xfer_info->ep->num;
  23640. + DWC_WARN("%s: timeout on endpoit %d\n", __func__, ep_num);
  23641. + /* Put the sate to 2 as it was time outed */
  23642. + xfer_info->state = 2;
  23643. +
  23644. + dctl.d32 =
  23645. + DWC_READ_REG32(&xfer_info->core_if->dev_if->dev_global_regs->dctl);
  23646. + gintsts.d32 =
  23647. + DWC_READ_REG32(&xfer_info->core_if->core_global_regs->gintsts);
  23648. + gintmsk.d32 =
  23649. + DWC_READ_REG32(&xfer_info->core_if->core_global_regs->gintmsk);
  23650. +
  23651. + if (!gintmsk.b.goutnakeff) {
  23652. + /* Unmask it */
  23653. + gintmsk.b.goutnakeff = 1;
  23654. + DWC_WRITE_REG32(&xfer_info->core_if->core_global_regs->gintmsk,
  23655. + gintmsk.d32);
  23656. +
  23657. + }
  23658. +
  23659. + if (!gintsts.b.goutnakeff) {
  23660. + dctl.b.sgoutnak = 1;
  23661. + }
  23662. + DWC_WRITE_REG32(&xfer_info->core_if->dev_if->dev_global_regs->dctl,
  23663. + dctl.d32);
  23664. +
  23665. +}
  23666. +
  23667. +void set_pid_isoc(dwc_hc_t * hc)
  23668. +{
  23669. + /* Set up the initial PID for the transfer. */
  23670. + if (hc->speed == DWC_OTG_EP_SPEED_HIGH) {
  23671. + if (hc->ep_is_in) {
  23672. + if (hc->multi_count == 1) {
  23673. + hc->data_pid_start = DWC_OTG_HC_PID_DATA0;
  23674. + } else if (hc->multi_count == 2) {
  23675. + hc->data_pid_start = DWC_OTG_HC_PID_DATA1;
  23676. + } else {
  23677. + hc->data_pid_start = DWC_OTG_HC_PID_DATA2;
  23678. + }
  23679. + } else {
  23680. + if (hc->multi_count == 1) {
  23681. + hc->data_pid_start = DWC_OTG_HC_PID_DATA0;
  23682. + } else {
  23683. + hc->data_pid_start = DWC_OTG_HC_PID_MDATA;
  23684. + }
  23685. + }
  23686. + } else {
  23687. + hc->data_pid_start = DWC_OTG_HC_PID_DATA0;
  23688. + }
  23689. +}
  23690. +
  23691. +/**
  23692. + * This function does the setup for a data transfer for a host channel and
  23693. + * starts the transfer. May be called in either Slave mode or DMA mode. In
  23694. + * Slave mode, the caller must ensure that there is sufficient space in the
  23695. + * request queue and Tx Data FIFO.
  23696. + *
  23697. + * For an OUT transfer in Slave mode, it loads a data packet into the
  23698. + * appropriate FIFO. If necessary, additional data packets will be loaded in
  23699. + * the Host ISR.
  23700. + *
  23701. + * For an IN transfer in Slave mode, a data packet is requested. The data
  23702. + * packets are unloaded from the Rx FIFO in the Host ISR. If necessary,
  23703. + * additional data packets are requested in the Host ISR.
  23704. + *
  23705. + * For a PING transfer in Slave mode, the Do Ping bit is set in the HCTSIZ
  23706. + * register along with a packet count of 1 and the channel is enabled. This
  23707. + * causes a single PING transaction to occur. Other fields in HCTSIZ are
  23708. + * simply set to 0 since no data transfer occurs in this case.
  23709. + *
  23710. + * For a PING transfer in DMA mode, the HCTSIZ register is initialized with
  23711. + * all the information required to perform the subsequent data transfer. In
  23712. + * addition, the Do Ping bit is set in the HCTSIZ register. In this case, the
  23713. + * controller performs the entire PING protocol, then starts the data
  23714. + * transfer.
  23715. + *
  23716. + * @param core_if Programming view of DWC_otg controller.
  23717. + * @param hc Information needed to initialize the host channel. The xfer_len
  23718. + * value may be reduced to accommodate the max widths of the XferSize and
  23719. + * PktCnt fields in the HCTSIZn register. The multi_count value may be changed
  23720. + * to reflect the final xfer_len value.
  23721. + */
  23722. +void dwc_otg_hc_start_transfer(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  23723. +{
  23724. + hcchar_data_t hcchar;
  23725. + hctsiz_data_t hctsiz;
  23726. + uint16_t num_packets;
  23727. + uint32_t max_hc_xfer_size = core_if->core_params->max_transfer_size;
  23728. + uint16_t max_hc_pkt_count = core_if->core_params->max_packet_count;
  23729. + dwc_otg_hc_regs_t *hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  23730. +
  23731. + hctsiz.d32 = 0;
  23732. +
  23733. + if (hc->do_ping) {
  23734. + if (!core_if->dma_enable) {
  23735. + dwc_otg_hc_do_ping(core_if, hc);
  23736. + hc->xfer_started = 1;
  23737. + return;
  23738. + } else {
  23739. + hctsiz.b.dopng = 1;
  23740. + }
  23741. + }
  23742. +
  23743. + if (hc->do_split) {
  23744. + num_packets = 1;
  23745. +
  23746. + if (hc->complete_split && !hc->ep_is_in) {
  23747. + /* For CSPLIT OUT Transfer, set the size to 0 so the
  23748. + * core doesn't expect any data written to the FIFO */
  23749. + hc->xfer_len = 0;
  23750. + } else if (hc->ep_is_in || (hc->xfer_len > hc->max_packet)) {
  23751. + hc->xfer_len = hc->max_packet;
  23752. + } else if (!hc->ep_is_in && (hc->xfer_len > 188)) {
  23753. + hc->xfer_len = 188;
  23754. + }
  23755. +
  23756. + hctsiz.b.xfersize = hc->xfer_len;
  23757. + } else {
  23758. + /*
  23759. + * Ensure that the transfer length and packet count will fit
  23760. + * in the widths allocated for them in the HCTSIZn register.
  23761. + */
  23762. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  23763. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  23764. + /*
  23765. + * Make sure the transfer size is no larger than one
  23766. + * (micro)frame's worth of data. (A check was done
  23767. + * when the periodic transfer was accepted to ensure
  23768. + * that a (micro)frame's worth of data can be
  23769. + * programmed into a channel.)
  23770. + */
  23771. + uint32_t max_periodic_len =
  23772. + hc->multi_count * hc->max_packet;
  23773. + if (hc->xfer_len > max_periodic_len) {
  23774. + hc->xfer_len = max_periodic_len;
  23775. + } else {
  23776. + }
  23777. + } else if (hc->xfer_len > max_hc_xfer_size) {
  23778. + /* Make sure that xfer_len is a multiple of max packet size. */
  23779. + hc->xfer_len = max_hc_xfer_size - hc->max_packet + 1;
  23780. + }
  23781. +
  23782. + if (hc->xfer_len > 0) {
  23783. + num_packets =
  23784. + (hc->xfer_len + hc->max_packet -
  23785. + 1) / hc->max_packet;
  23786. + if (num_packets > max_hc_pkt_count) {
  23787. + num_packets = max_hc_pkt_count;
  23788. + hc->xfer_len = num_packets * hc->max_packet;
  23789. + }
  23790. + } else {
  23791. + /* Need 1 packet for transfer length of 0. */
  23792. + num_packets = 1;
  23793. + }
  23794. +
  23795. + if (hc->ep_is_in) {
  23796. + /* Always program an integral # of max packets for IN transfers. */
  23797. + hc->xfer_len = num_packets * hc->max_packet;
  23798. + }
  23799. +
  23800. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  23801. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  23802. + /*
  23803. + * Make sure that the multi_count field matches the
  23804. + * actual transfer length.
  23805. + */
  23806. + hc->multi_count = num_packets;
  23807. + }
  23808. +
  23809. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
  23810. + set_pid_isoc(hc);
  23811. +
  23812. + hctsiz.b.xfersize = hc->xfer_len;
  23813. + }
  23814. +
  23815. + hc->start_pkt_count = num_packets;
  23816. + hctsiz.b.pktcnt = num_packets;
  23817. + hctsiz.b.pid = hc->data_pid_start;
  23818. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  23819. +
  23820. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
  23821. + DWC_DEBUGPL(DBG_HCDV, " Xfer Size: %d\n", hctsiz.b.xfersize);
  23822. + DWC_DEBUGPL(DBG_HCDV, " Num Pkts: %d\n", hctsiz.b.pktcnt);
  23823. + DWC_DEBUGPL(DBG_HCDV, " Start PID: %d\n", hctsiz.b.pid);
  23824. +
  23825. + if (core_if->dma_enable) {
  23826. + dwc_dma_t dma_addr;
  23827. + if (hc->align_buff) {
  23828. + dma_addr = hc->align_buff;
  23829. + } else {
  23830. + dma_addr = ((unsigned long)hc->xfer_buff & 0xffffffff);
  23831. + }
  23832. + DWC_WRITE_REG32(&hc_regs->hcdma, dma_addr);
  23833. + }
  23834. +
  23835. + /* Start the split */
  23836. + if (hc->do_split) {
  23837. + hcsplt_data_t hcsplt;
  23838. + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
  23839. + hcsplt.b.spltena = 1;
  23840. + DWC_WRITE_REG32(&hc_regs->hcsplt, hcsplt.d32);
  23841. + }
  23842. +
  23843. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  23844. + hcchar.b.multicnt = hc->multi_count;
  23845. + hc_set_even_odd_frame(core_if, hc, &hcchar);
  23846. +#ifdef DEBUG
  23847. + core_if->start_hcchar_val[hc->hc_num] = hcchar.d32;
  23848. + if (hcchar.b.chdis) {
  23849. + DWC_WARN("%s: chdis set, channel %d, hcchar 0x%08x\n",
  23850. + __func__, hc->hc_num, hcchar.d32);
  23851. + }
  23852. +#endif
  23853. +
  23854. + /* Set host channel enable after all other setup is complete. */
  23855. + hcchar.b.chen = 1;
  23856. + hcchar.b.chdis = 0;
  23857. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  23858. +
  23859. + hc->xfer_started = 1;
  23860. + hc->requests++;
  23861. +
  23862. + if (!core_if->dma_enable && !hc->ep_is_in && hc->xfer_len > 0) {
  23863. + /* Load OUT packet into the appropriate Tx FIFO. */
  23864. + dwc_otg_hc_write_packet(core_if, hc);
  23865. + }
  23866. +#ifdef DEBUG
  23867. + if (hc->ep_type != DWC_OTG_EP_TYPE_INTR) {
  23868. + DWC_DEBUGPL(DBG_HCDV, "transfer %d from core_if %p\n",
  23869. + hc->hc_num, core_if);//GRAYG
  23870. + core_if->hc_xfer_info[hc->hc_num].core_if = core_if;
  23871. + core_if->hc_xfer_info[hc->hc_num].hc = hc;
  23872. +
  23873. + /* Start a timer for this transfer. */
  23874. + DWC_TIMER_SCHEDULE(core_if->hc_xfer_timer[hc->hc_num], 10000);
  23875. + }
  23876. +#endif
  23877. +}
  23878. +
  23879. +/**
  23880. + * This function does the setup for a data transfer for a host channel
  23881. + * and starts the transfer in Descriptor DMA mode.
  23882. + *
  23883. + * Initializes HCTSIZ register. For a PING transfer the Do Ping bit is set.
  23884. + * Sets PID and NTD values. For periodic transfers
  23885. + * initializes SCHED_INFO field with micro-frame bitmap.
  23886. + *
  23887. + * Initializes HCDMA register with descriptor list address and CTD value
  23888. + * then starts the transfer via enabling the channel.
  23889. + *
  23890. + * @param core_if Programming view of DWC_otg controller.
  23891. + * @param hc Information needed to initialize the host channel.
  23892. + */
  23893. +void dwc_otg_hc_start_transfer_ddma(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  23894. +{
  23895. + dwc_otg_hc_regs_t *hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  23896. + hcchar_data_t hcchar;
  23897. + hctsiz_data_t hctsiz;
  23898. + hcdma_data_t hcdma;
  23899. +
  23900. + hctsiz.d32 = 0;
  23901. +
  23902. + if (hc->do_ping)
  23903. + hctsiz.b_ddma.dopng = 1;
  23904. +
  23905. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
  23906. + set_pid_isoc(hc);
  23907. +
  23908. + /* Packet Count and Xfer Size are not used in Descriptor DMA mode */
  23909. + hctsiz.b_ddma.pid = hc->data_pid_start;
  23910. + hctsiz.b_ddma.ntd = hc->ntd - 1; /* 0 - 1 descriptor, 1 - 2 descriptors, etc. */
  23911. + hctsiz.b_ddma.schinfo = hc->schinfo; /* Non-zero only for high-speed interrupt endpoints */
  23912. +
  23913. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
  23914. + DWC_DEBUGPL(DBG_HCDV, " Start PID: %d\n", hctsiz.b.pid);
  23915. + DWC_DEBUGPL(DBG_HCDV, " NTD: %d\n", hctsiz.b_ddma.ntd);
  23916. +
  23917. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  23918. +
  23919. + hcdma.d32 = 0;
  23920. + hcdma.b.dma_addr = ((uint32_t) hc->desc_list_addr) >> 11;
  23921. +
  23922. + /* Always start from first descriptor. */
  23923. + hcdma.b.ctd = 0;
  23924. + DWC_WRITE_REG32(&hc_regs->hcdma, hcdma.d32);
  23925. +
  23926. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  23927. + hcchar.b.multicnt = hc->multi_count;
  23928. +
  23929. +#ifdef DEBUG
  23930. + core_if->start_hcchar_val[hc->hc_num] = hcchar.d32;
  23931. + if (hcchar.b.chdis) {
  23932. + DWC_WARN("%s: chdis set, channel %d, hcchar 0x%08x\n",
  23933. + __func__, hc->hc_num, hcchar.d32);
  23934. + }
  23935. +#endif
  23936. +
  23937. + /* Set host channel enable after all other setup is complete. */
  23938. + hcchar.b.chen = 1;
  23939. + hcchar.b.chdis = 0;
  23940. +
  23941. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  23942. +
  23943. + hc->xfer_started = 1;
  23944. + hc->requests++;
  23945. +
  23946. +#ifdef DEBUG
  23947. + if ((hc->ep_type != DWC_OTG_EP_TYPE_INTR)
  23948. + && (hc->ep_type != DWC_OTG_EP_TYPE_ISOC)) {
  23949. + DWC_DEBUGPL(DBG_HCDV, "DMA transfer %d from core_if %p\n",
  23950. + hc->hc_num, core_if);//GRAYG
  23951. + core_if->hc_xfer_info[hc->hc_num].core_if = core_if;
  23952. + core_if->hc_xfer_info[hc->hc_num].hc = hc;
  23953. + /* Start a timer for this transfer. */
  23954. + DWC_TIMER_SCHEDULE(core_if->hc_xfer_timer[hc->hc_num], 10000);
  23955. + }
  23956. +#endif
  23957. +
  23958. +}
  23959. +
  23960. +/**
  23961. + * This function continues a data transfer that was started by previous call
  23962. + * to <code>dwc_otg_hc_start_transfer</code>. The caller must ensure there is
  23963. + * sufficient space in the request queue and Tx Data FIFO. This function
  23964. + * should only be called in Slave mode. In DMA mode, the controller acts
  23965. + * autonomously to complete transfers programmed to a host channel.
  23966. + *
  23967. + * For an OUT transfer, a new data packet is loaded into the appropriate FIFO
  23968. + * if there is any data remaining to be queued. For an IN transfer, another
  23969. + * data packet is always requested. For the SETUP phase of a control transfer,
  23970. + * this function does nothing.
  23971. + *
  23972. + * @return 1 if a new request is queued, 0 if no more requests are required
  23973. + * for this transfer.
  23974. + */
  23975. +int dwc_otg_hc_continue_transfer(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  23976. +{
  23977. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
  23978. +
  23979. + if (hc->do_split) {
  23980. + /* SPLITs always queue just once per channel */
  23981. + return 0;
  23982. + } else if (hc->data_pid_start == DWC_OTG_HC_PID_SETUP) {
  23983. + /* SETUPs are queued only once since they can't be NAKed. */
  23984. + return 0;
  23985. + } else if (hc->ep_is_in) {
  23986. + /*
  23987. + * Always queue another request for other IN transfers. If
  23988. + * back-to-back INs are issued and NAKs are received for both,
  23989. + * the driver may still be processing the first NAK when the
  23990. + * second NAK is received. When the interrupt handler clears
  23991. + * the NAK interrupt for the first NAK, the second NAK will
  23992. + * not be seen. So we can't depend on the NAK interrupt
  23993. + * handler to requeue a NAKed request. Instead, IN requests
  23994. + * are issued each time this function is called. When the
  23995. + * transfer completes, the extra requests for the channel will
  23996. + * be flushed.
  23997. + */
  23998. + hcchar_data_t hcchar;
  23999. + dwc_otg_hc_regs_t *hc_regs =
  24000. + core_if->host_if->hc_regs[hc->hc_num];
  24001. +
  24002. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  24003. + hc_set_even_odd_frame(core_if, hc, &hcchar);
  24004. + hcchar.b.chen = 1;
  24005. + hcchar.b.chdis = 0;
  24006. + DWC_DEBUGPL(DBG_HCDV, " IN xfer: hcchar = 0x%08x\n",
  24007. + hcchar.d32);
  24008. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  24009. + hc->requests++;
  24010. + return 1;
  24011. + } else {
  24012. + /* OUT transfers. */
  24013. + if (hc->xfer_count < hc->xfer_len) {
  24014. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  24015. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  24016. + hcchar_data_t hcchar;
  24017. + dwc_otg_hc_regs_t *hc_regs;
  24018. + hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  24019. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  24020. + hc_set_even_odd_frame(core_if, hc, &hcchar);
  24021. + }
  24022. +
  24023. + /* Load OUT packet into the appropriate Tx FIFO. */
  24024. + dwc_otg_hc_write_packet(core_if, hc);
  24025. + hc->requests++;
  24026. + return 1;
  24027. + } else {
  24028. + return 0;
  24029. + }
  24030. + }
  24031. +}
  24032. +
  24033. +/**
  24034. + * Starts a PING transfer. This function should only be called in Slave mode.
  24035. + * The Do Ping bit is set in the HCTSIZ register, then the channel is enabled.
  24036. + */
  24037. +void dwc_otg_hc_do_ping(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  24038. +{
  24039. + hcchar_data_t hcchar;
  24040. + hctsiz_data_t hctsiz;
  24041. + dwc_otg_hc_regs_t *hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  24042. +
  24043. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
  24044. +
  24045. + hctsiz.d32 = 0;
  24046. + hctsiz.b.dopng = 1;
  24047. + hctsiz.b.pktcnt = 1;
  24048. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  24049. +
  24050. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  24051. + hcchar.b.chen = 1;
  24052. + hcchar.b.chdis = 0;
  24053. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  24054. +}
  24055. +
  24056. +/*
  24057. + * This function writes a packet into the Tx FIFO associated with the Host
  24058. + * Channel. For a channel associated with a non-periodic EP, the non-periodic
  24059. + * Tx FIFO is written. For a channel associated with a periodic EP, the
  24060. + * periodic Tx FIFO is written. This function should only be called in Slave
  24061. + * mode.
  24062. + *
  24063. + * Upon return the xfer_buff and xfer_count fields in _hc are incremented by
  24064. + * then number of bytes written to the Tx FIFO.
  24065. + */
  24066. +void dwc_otg_hc_write_packet(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  24067. +{
  24068. + uint32_t i;
  24069. + uint32_t remaining_count;
  24070. + uint32_t byte_count;
  24071. + uint32_t dword_count;
  24072. +
  24073. + uint32_t *data_buff = (uint32_t *) (hc->xfer_buff);
  24074. + uint32_t *data_fifo = core_if->data_fifo[hc->hc_num];
  24075. +
  24076. + remaining_count = hc->xfer_len - hc->xfer_count;
  24077. + if (remaining_count > hc->max_packet) {
  24078. + byte_count = hc->max_packet;
  24079. + } else {
  24080. + byte_count = remaining_count;
  24081. + }
  24082. +
  24083. + dword_count = (byte_count + 3) / 4;
  24084. +
  24085. + if ((((unsigned long)data_buff) & 0x3) == 0) {
  24086. + /* xfer_buff is DWORD aligned. */
  24087. + for (i = 0; i < dword_count; i++, data_buff++) {
  24088. + DWC_WRITE_REG32(data_fifo, *data_buff);
  24089. + }
  24090. + } else {
  24091. + /* xfer_buff is not DWORD aligned. */
  24092. + for (i = 0; i < dword_count; i++, data_buff++) {
  24093. + uint32_t data;
  24094. + data =
  24095. + (data_buff[0] | data_buff[1] << 8 | data_buff[2] <<
  24096. + 16 | data_buff[3] << 24);
  24097. + DWC_WRITE_REG32(data_fifo, data);
  24098. + }
  24099. + }
  24100. +
  24101. + hc->xfer_count += byte_count;
  24102. + hc->xfer_buff += byte_count;
  24103. +}
  24104. +
  24105. +/**
  24106. + * Gets the current USB frame number. This is the frame number from the last
  24107. + * SOF packet.
  24108. + */
  24109. +uint32_t dwc_otg_get_frame_number(dwc_otg_core_if_t * core_if)
  24110. +{
  24111. + dsts_data_t dsts;
  24112. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  24113. +
  24114. + /* read current frame/microframe number from DSTS register */
  24115. + return dsts.b.soffn;
  24116. +}
  24117. +
  24118. +/**
  24119. + * Calculates and gets the frame Interval value of HFIR register according PHY
  24120. + * type and speed.The application can modify a value of HFIR register only after
  24121. + * the Port Enable bit of the Host Port Control and Status register
  24122. + * (HPRT.PrtEnaPort) has been set.
  24123. +*/
  24124. +
  24125. +uint32_t calc_frame_interval(dwc_otg_core_if_t * core_if)
  24126. +{
  24127. + gusbcfg_data_t usbcfg;
  24128. + hwcfg2_data_t hwcfg2;
  24129. + hprt0_data_t hprt0;
  24130. + int clock = 60; // default value
  24131. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  24132. + hwcfg2.d32 = DWC_READ_REG32(&core_if->core_global_regs->ghwcfg2);
  24133. + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
  24134. + if (!usbcfg.b.physel && usbcfg.b.ulpi_utmi_sel && !usbcfg.b.phyif)
  24135. + clock = 60;
  24136. + if (usbcfg.b.physel && hwcfg2.b.fs_phy_type == 3)
  24137. + clock = 48;
  24138. + if (!usbcfg.b.phylpwrclksel && !usbcfg.b.physel &&
  24139. + !usbcfg.b.ulpi_utmi_sel && usbcfg.b.phyif)
  24140. + clock = 30;
  24141. + if (!usbcfg.b.phylpwrclksel && !usbcfg.b.physel &&
  24142. + !usbcfg.b.ulpi_utmi_sel && !usbcfg.b.phyif)
  24143. + clock = 60;
  24144. + if (usbcfg.b.phylpwrclksel && !usbcfg.b.physel &&
  24145. + !usbcfg.b.ulpi_utmi_sel && usbcfg.b.phyif)
  24146. + clock = 48;
  24147. + if (usbcfg.b.physel && !usbcfg.b.phyif && hwcfg2.b.fs_phy_type == 2)
  24148. + clock = 48;
  24149. + if (usbcfg.b.physel && hwcfg2.b.fs_phy_type == 1)
  24150. + clock = 48;
  24151. + if (hprt0.b.prtspd == 0)
  24152. + /* High speed case */
  24153. + return 125 * clock - 1;
  24154. + else
  24155. + /* FS/LS case */
  24156. + return 1000 * clock - 1;
  24157. +}
  24158. +
  24159. +/**
  24160. + * This function reads a setup packet from the Rx FIFO into the destination
  24161. + * buffer. This function is called from the Rx Status Queue Level (RxStsQLvl)
  24162. + * Interrupt routine when a SETUP packet has been received in Slave mode.
  24163. + *
  24164. + * @param core_if Programming view of DWC_otg controller.
  24165. + * @param dest Destination buffer for packet data.
  24166. + */
  24167. +void dwc_otg_read_setup_packet(dwc_otg_core_if_t * core_if, uint32_t * dest)
  24168. +{
  24169. + device_grxsts_data_t status;
  24170. + /* Get the 8 bytes of a setup transaction data */
  24171. +
  24172. + /* Pop 2 DWORDS off the receive data FIFO into memory */
  24173. + dest[0] = DWC_READ_REG32(core_if->data_fifo[0]);
  24174. + dest[1] = DWC_READ_REG32(core_if->data_fifo[0]);
  24175. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  24176. + status.d32 =
  24177. + DWC_READ_REG32(&core_if->core_global_regs->grxstsp);
  24178. + DWC_DEBUGPL(DBG_ANY,
  24179. + "EP:%d BCnt:%d " "pktsts:%x Frame:%d(0x%0x)\n",
  24180. + status.b.epnum, status.b.bcnt, status.b.pktsts,
  24181. + status.b.fn, status.b.fn);
  24182. + }
  24183. +}
  24184. +
  24185. +/**
  24186. + * This function enables EP0 OUT to receive SETUP packets and configures EP0
  24187. + * IN for transmitting packets. It is normally called when the
  24188. + * "Enumeration Done" interrupt occurs.
  24189. + *
  24190. + * @param core_if Programming view of DWC_otg controller.
  24191. + * @param ep The EP0 data.
  24192. + */
  24193. +void dwc_otg_ep0_activate(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  24194. +{
  24195. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  24196. + dsts_data_t dsts;
  24197. + depctl_data_t diepctl;
  24198. + depctl_data_t doepctl;
  24199. + dctl_data_t dctl = {.d32 = 0 };
  24200. +
  24201. + ep->stp_rollover = 0;
  24202. + /* Read the Device Status and Endpoint 0 Control registers */
  24203. + dsts.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dsts);
  24204. + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl);
  24205. + doepctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl);
  24206. +
  24207. + /* Set the MPS of the IN EP based on the enumeration speed */
  24208. + switch (dsts.b.enumspd) {
  24209. + case DWC_DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ:
  24210. + case DWC_DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ:
  24211. + case DWC_DSTS_ENUMSPD_FS_PHY_48MHZ:
  24212. + diepctl.b.mps = DWC_DEP0CTL_MPS_64;
  24213. + break;
  24214. + case DWC_DSTS_ENUMSPD_LS_PHY_6MHZ:
  24215. + diepctl.b.mps = DWC_DEP0CTL_MPS_8;
  24216. + break;
  24217. + }
  24218. +
  24219. + DWC_WRITE_REG32(&dev_if->in_ep_regs[0]->diepctl, diepctl.d32);
  24220. +
  24221. + /* Enable OUT EP for receive */
  24222. + if (core_if->snpsid <= OTG_CORE_REV_2_94a) {
  24223. + doepctl.b.epena = 1;
  24224. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepctl, doepctl.d32);
  24225. + }
  24226. +#ifdef VERBOSE
  24227. + DWC_DEBUGPL(DBG_PCDV, "doepctl0=%0x\n",
  24228. + DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl));
  24229. + DWC_DEBUGPL(DBG_PCDV, "diepctl0=%0x\n",
  24230. + DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl));
  24231. +#endif
  24232. + dctl.b.cgnpinnak = 1;
  24233. +
  24234. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
  24235. + DWC_DEBUGPL(DBG_PCDV, "dctl=%0x\n",
  24236. + DWC_READ_REG32(&dev_if->dev_global_regs->dctl));
  24237. +
  24238. +}
  24239. +
  24240. +/**
  24241. + * This function activates an EP. The Device EP control register for
  24242. + * the EP is configured as defined in the ep structure. Note: This
  24243. + * function is not used for EP0.
  24244. + *
  24245. + * @param core_if Programming view of DWC_otg controller.
  24246. + * @param ep The EP to activate.
  24247. + */
  24248. +void dwc_otg_ep_activate(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  24249. +{
  24250. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  24251. + depctl_data_t depctl;
  24252. + volatile uint32_t *addr;
  24253. + daint_data_t daintmsk = {.d32 = 0 };
  24254. + dcfg_data_t dcfg;
  24255. + uint8_t i;
  24256. +
  24257. + DWC_DEBUGPL(DBG_PCDV, "%s() EP%d-%s\n", __func__, ep->num,
  24258. + (ep->is_in ? "IN" : "OUT"));
  24259. +
  24260. +#ifdef DWC_UTE_PER_IO
  24261. + ep->xiso_frame_num = 0xFFFFFFFF;
  24262. + ep->xiso_active_xfers = 0;
  24263. + ep->xiso_queued_xfers = 0;
  24264. +#endif
  24265. + /* Read DEPCTLn register */
  24266. + if (ep->is_in == 1) {
  24267. + addr = &dev_if->in_ep_regs[ep->num]->diepctl;
  24268. + daintmsk.ep.in = 1 << ep->num;
  24269. + } else {
  24270. + addr = &dev_if->out_ep_regs[ep->num]->doepctl;
  24271. + daintmsk.ep.out = 1 << ep->num;
  24272. + }
  24273. +
  24274. + /* If the EP is already active don't change the EP Control
  24275. + * register. */
  24276. + depctl.d32 = DWC_READ_REG32(addr);
  24277. + if (!depctl.b.usbactep) {
  24278. + depctl.b.mps = ep->maxpacket;
  24279. + depctl.b.eptype = ep->type;
  24280. + depctl.b.txfnum = ep->tx_fifo_num;
  24281. +
  24282. + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
  24283. + depctl.b.setd0pid = 1; // ???
  24284. + } else {
  24285. + depctl.b.setd0pid = 1;
  24286. + }
  24287. + depctl.b.usbactep = 1;
  24288. +
  24289. + /* Update nextep_seq array and EPMSCNT in DCFG*/
  24290. + if (!(depctl.b.eptype & 1) && (ep->is_in == 1)) { // NP IN EP
  24291. + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  24292. + if (core_if->nextep_seq[i] == core_if->first_in_nextep_seq)
  24293. + break;
  24294. + }
  24295. + core_if->nextep_seq[i] = ep->num;
  24296. + core_if->nextep_seq[ep->num] = core_if->first_in_nextep_seq;
  24297. + depctl.b.nextep = core_if->nextep_seq[ep->num];
  24298. + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
  24299. + dcfg.b.epmscnt++;
  24300. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
  24301. +
  24302. + DWC_DEBUGPL(DBG_PCDV,
  24303. + "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
  24304. + __func__, core_if->first_in_nextep_seq);
  24305. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  24306. + DWC_DEBUGPL(DBG_PCDV, "%2d\n",
  24307. + core_if->nextep_seq[i]);
  24308. + }
  24309. +
  24310. + }
  24311. +
  24312. +
  24313. + DWC_WRITE_REG32(addr, depctl.d32);
  24314. + DWC_DEBUGPL(DBG_PCDV, "DEPCTL=%08x\n", DWC_READ_REG32(addr));
  24315. + }
  24316. +
  24317. + /* Enable the Interrupt for this EP */
  24318. + if (core_if->multiproc_int_enable) {
  24319. + if (ep->is_in == 1) {
  24320. + diepmsk_data_t diepmsk = {.d32 = 0 };
  24321. + diepmsk.b.xfercompl = 1;
  24322. + diepmsk.b.timeout = 1;
  24323. + diepmsk.b.epdisabled = 1;
  24324. + diepmsk.b.ahberr = 1;
  24325. + diepmsk.b.intknepmis = 1;
  24326. + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable)
  24327. + diepmsk.b.intknepmis = 0;
  24328. + diepmsk.b.txfifoundrn = 1; //?????
  24329. + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
  24330. + diepmsk.b.nak = 1;
  24331. + }
  24332. +
  24333. +
  24334. +
  24335. +/*
  24336. + if (core_if->dma_desc_enable) {
  24337. + diepmsk.b.bna = 1;
  24338. + }
  24339. +*/
  24340. +/*
  24341. + if (core_if->dma_enable) {
  24342. + doepmsk.b.nak = 1;
  24343. + }
  24344. +*/
  24345. + DWC_WRITE_REG32(&dev_if->dev_global_regs->
  24346. + diepeachintmsk[ep->num], diepmsk.d32);
  24347. +
  24348. + } else {
  24349. + doepmsk_data_t doepmsk = {.d32 = 0 };
  24350. + doepmsk.b.xfercompl = 1;
  24351. + doepmsk.b.ahberr = 1;
  24352. + doepmsk.b.epdisabled = 1;
  24353. + if (ep->type == DWC_OTG_EP_TYPE_ISOC)
  24354. + doepmsk.b.outtknepdis = 1;
  24355. +
  24356. +/*
  24357. +
  24358. + if (core_if->dma_desc_enable) {
  24359. + doepmsk.b.bna = 1;
  24360. + }
  24361. +*/
  24362. +/*
  24363. + doepmsk.b.babble = 1;
  24364. + doepmsk.b.nyet = 1;
  24365. + doepmsk.b.nak = 1;
  24366. +*/
  24367. + DWC_WRITE_REG32(&dev_if->dev_global_regs->
  24368. + doepeachintmsk[ep->num], doepmsk.d32);
  24369. + }
  24370. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->deachintmsk,
  24371. + 0, daintmsk.d32);
  24372. + } else {
  24373. + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
  24374. + if (ep->is_in) {
  24375. + diepmsk_data_t diepmsk = {.d32 = 0 };
  24376. + diepmsk.b.nak = 1;
  24377. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->diepmsk, 0, diepmsk.d32);
  24378. + } else {
  24379. + doepmsk_data_t doepmsk = {.d32 = 0 };
  24380. + doepmsk.b.outtknepdis = 1;
  24381. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->doepmsk, 0, doepmsk.d32);
  24382. + }
  24383. + }
  24384. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->daintmsk,
  24385. + 0, daintmsk.d32);
  24386. + }
  24387. +
  24388. + DWC_DEBUGPL(DBG_PCDV, "DAINTMSK=%0x\n",
  24389. + DWC_READ_REG32(&dev_if->dev_global_regs->daintmsk));
  24390. +
  24391. + ep->stall_clear_flag = 0;
  24392. +
  24393. + return;
  24394. +}
  24395. +
  24396. +/**
  24397. + * This function deactivates an EP. This is done by clearing the USB Active
  24398. + * EP bit in the Device EP control register. Note: This function is not used
  24399. + * for EP0. EP0 cannot be deactivated.
  24400. + *
  24401. + * @param core_if Programming view of DWC_otg controller.
  24402. + * @param ep The EP to deactivate.
  24403. + */
  24404. +void dwc_otg_ep_deactivate(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  24405. +{
  24406. + depctl_data_t depctl = {.d32 = 0 };
  24407. + volatile uint32_t *addr;
  24408. + daint_data_t daintmsk = {.d32 = 0 };
  24409. + dcfg_data_t dcfg;
  24410. + uint8_t i = 0;
  24411. +
  24412. +#ifdef DWC_UTE_PER_IO
  24413. + ep->xiso_frame_num = 0xFFFFFFFF;
  24414. + ep->xiso_active_xfers = 0;
  24415. + ep->xiso_queued_xfers = 0;
  24416. +#endif
  24417. +
  24418. + /* Read DEPCTLn register */
  24419. + if (ep->is_in == 1) {
  24420. + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
  24421. + daintmsk.ep.in = 1 << ep->num;
  24422. + } else {
  24423. + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
  24424. + daintmsk.ep.out = 1 << ep->num;
  24425. + }
  24426. +
  24427. + depctl.d32 = DWC_READ_REG32(addr);
  24428. +
  24429. + depctl.b.usbactep = 0;
  24430. +
  24431. + /* Update nextep_seq array and EPMSCNT in DCFG*/
  24432. + if (!(depctl.b.eptype & 1) && ep->is_in == 1) { // NP EP IN
  24433. + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  24434. + if (core_if->nextep_seq[i] == ep->num)
  24435. + break;
  24436. + }
  24437. + core_if->nextep_seq[i] = core_if->nextep_seq[ep->num];
  24438. + if (core_if->first_in_nextep_seq == ep->num)
  24439. + core_if->first_in_nextep_seq = i;
  24440. + core_if->nextep_seq[ep->num] = 0xff;
  24441. + depctl.b.nextep = 0;
  24442. + dcfg.d32 =
  24443. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  24444. + dcfg.b.epmscnt--;
  24445. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg,
  24446. + dcfg.d32);
  24447. +
  24448. + DWC_DEBUGPL(DBG_PCDV,
  24449. + "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
  24450. + __func__, core_if->first_in_nextep_seq);
  24451. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  24452. + DWC_DEBUGPL(DBG_PCDV, "%2d\n", core_if->nextep_seq[i]);
  24453. + }
  24454. + }
  24455. +
  24456. + if (ep->is_in == 1)
  24457. + depctl.b.txfnum = 0;
  24458. +
  24459. + if (core_if->dma_desc_enable)
  24460. + depctl.b.epdis = 1;
  24461. +
  24462. + DWC_WRITE_REG32(addr, depctl.d32);
  24463. + depctl.d32 = DWC_READ_REG32(addr);
  24464. + if (core_if->dma_enable && ep->type == DWC_OTG_EP_TYPE_ISOC
  24465. + && depctl.b.epena) {
  24466. + depctl_data_t depctl = {.d32 = 0};
  24467. + if (ep->is_in) {
  24468. + diepint_data_t diepint = {.d32 = 0};
  24469. +
  24470. + depctl.b.snak = 1;
  24471. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  24472. + diepctl, depctl.d32);
  24473. + do {
  24474. + dwc_udelay(10);
  24475. + diepint.d32 =
  24476. + DWC_READ_REG32(&core_if->
  24477. + dev_if->in_ep_regs[ep->num]->
  24478. + diepint);
  24479. + } while (!diepint.b.inepnakeff);
  24480. + diepint.b.inepnakeff = 1;
  24481. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  24482. + diepint, diepint.d32);
  24483. + depctl.d32 = 0;
  24484. + depctl.b.epdis = 1;
  24485. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  24486. + diepctl, depctl.d32);
  24487. + do {
  24488. + dwc_udelay(10);
  24489. + diepint.d32 =
  24490. + DWC_READ_REG32(&core_if->
  24491. + dev_if->in_ep_regs[ep->num]->
  24492. + diepint);
  24493. + } while (!diepint.b.epdisabled);
  24494. + diepint.b.epdisabled = 1;
  24495. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  24496. + diepint, diepint.d32);
  24497. + } else {
  24498. + dctl_data_t dctl = {.d32 = 0};
  24499. + gintmsk_data_t gintsts = {.d32 = 0};
  24500. + doepint_data_t doepint = {.d32 = 0};
  24501. + dctl.b.sgoutnak = 1;
  24502. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  24503. + dctl, 0, dctl.d32);
  24504. + do {
  24505. + dwc_udelay(10);
  24506. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  24507. + } while (!gintsts.b.goutnakeff);
  24508. + gintsts.d32 = 0;
  24509. + gintsts.b.goutnakeff = 1;
  24510. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  24511. +
  24512. + depctl.d32 = 0;
  24513. + depctl.b.epdis = 1;
  24514. + depctl.b.snak = 1;
  24515. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[ep->num]->doepctl, depctl.d32);
  24516. + do
  24517. + {
  24518. + dwc_udelay(10);
  24519. + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  24520. + out_ep_regs[ep->num]->doepint);
  24521. + } while (!doepint.b.epdisabled);
  24522. +
  24523. + doepint.b.epdisabled = 1;
  24524. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[ep->num]->doepint, doepint.d32);
  24525. +
  24526. + dctl.d32 = 0;
  24527. + dctl.b.cgoutnak = 1;
  24528. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  24529. + }
  24530. + }
  24531. +
  24532. + /* Disable the Interrupt for this EP */
  24533. + if (core_if->multiproc_int_enable) {
  24534. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->deachintmsk,
  24535. + daintmsk.d32, 0);
  24536. +
  24537. + if (ep->is_in == 1) {
  24538. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
  24539. + diepeachintmsk[ep->num], 0);
  24540. + } else {
  24541. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
  24542. + doepeachintmsk[ep->num], 0);
  24543. + }
  24544. + } else {
  24545. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->daintmsk,
  24546. + daintmsk.d32, 0);
  24547. + }
  24548. +
  24549. +}
  24550. +
  24551. +/**
  24552. + * This function initializes dma descriptor chain.
  24553. + *
  24554. + * @param core_if Programming view of DWC_otg controller.
  24555. + * @param ep The EP to start the transfer on.
  24556. + */
  24557. +static void init_dma_desc_chain(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  24558. +{
  24559. + dwc_otg_dev_dma_desc_t *dma_desc;
  24560. + uint32_t offset;
  24561. + uint32_t xfer_est;
  24562. + int i;
  24563. + unsigned maxxfer_local, total_len;
  24564. +
  24565. + if (!ep->is_in && ep->type == DWC_OTG_EP_TYPE_INTR &&
  24566. + (ep->maxpacket%4)) {
  24567. + maxxfer_local = ep->maxpacket;
  24568. + total_len = ep->xfer_len;
  24569. + } else {
  24570. + maxxfer_local = ep->maxxfer;
  24571. + total_len = ep->total_len;
  24572. + }
  24573. +
  24574. + ep->desc_cnt = (total_len / maxxfer_local) +
  24575. + ((total_len % maxxfer_local) ? 1 : 0);
  24576. +
  24577. + if (!ep->desc_cnt)
  24578. + ep->desc_cnt = 1;
  24579. +
  24580. + if (ep->desc_cnt > MAX_DMA_DESC_CNT)
  24581. + ep->desc_cnt = MAX_DMA_DESC_CNT;
  24582. +
  24583. + dma_desc = ep->desc_addr;
  24584. + if (maxxfer_local == ep->maxpacket) {
  24585. + if ((total_len % maxxfer_local) &&
  24586. + (total_len/maxxfer_local < MAX_DMA_DESC_CNT)) {
  24587. + xfer_est = (ep->desc_cnt - 1) * maxxfer_local +
  24588. + (total_len % maxxfer_local);
  24589. + } else
  24590. + xfer_est = ep->desc_cnt * maxxfer_local;
  24591. + } else
  24592. + xfer_est = total_len;
  24593. + offset = 0;
  24594. + for (i = 0; i < ep->desc_cnt; ++i) {
  24595. + /** DMA Descriptor Setup */
  24596. + if (xfer_est > maxxfer_local) {
  24597. + dma_desc->status.b.bs = BS_HOST_BUSY;
  24598. + dma_desc->status.b.l = 0;
  24599. + dma_desc->status.b.ioc = 0;
  24600. + dma_desc->status.b.sp = 0;
  24601. + dma_desc->status.b.bytes = maxxfer_local;
  24602. + dma_desc->buf = ep->dma_addr + offset;
  24603. + dma_desc->status.b.sts = 0;
  24604. + dma_desc->status.b.bs = BS_HOST_READY;
  24605. +
  24606. + xfer_est -= maxxfer_local;
  24607. + offset += maxxfer_local;
  24608. + } else {
  24609. + dma_desc->status.b.bs = BS_HOST_BUSY;
  24610. + dma_desc->status.b.l = 1;
  24611. + dma_desc->status.b.ioc = 1;
  24612. + if (ep->is_in) {
  24613. + dma_desc->status.b.sp =
  24614. + (xfer_est %
  24615. + ep->maxpacket) ? 1 : ((ep->
  24616. + sent_zlp) ? 1 : 0);
  24617. + dma_desc->status.b.bytes = xfer_est;
  24618. + } else {
  24619. + if (maxxfer_local == ep->maxpacket)
  24620. + dma_desc->status.b.bytes = xfer_est;
  24621. + else
  24622. + dma_desc->status.b.bytes =
  24623. + xfer_est + ((4 - (xfer_est & 0x3)) & 0x3);
  24624. + }
  24625. +
  24626. + dma_desc->buf = ep->dma_addr + offset;
  24627. + dma_desc->status.b.sts = 0;
  24628. + dma_desc->status.b.bs = BS_HOST_READY;
  24629. + }
  24630. + dma_desc++;
  24631. + }
  24632. +}
  24633. +/**
  24634. + * This function is called when to write ISOC data into appropriate dedicated
  24635. + * periodic FIFO.
  24636. + */
  24637. +static int32_t write_isoc_tx_fifo(dwc_otg_core_if_t * core_if, dwc_ep_t * dwc_ep)
  24638. +{
  24639. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  24640. + dwc_otg_dev_in_ep_regs_t *ep_regs;
  24641. + dtxfsts_data_t txstatus = {.d32 = 0 };
  24642. + uint32_t len = 0;
  24643. + int epnum = dwc_ep->num;
  24644. + int dwords;
  24645. +
  24646. + DWC_DEBUGPL(DBG_PCD, "Dedicated TxFifo Empty: %d \n", epnum);
  24647. +
  24648. + ep_regs = core_if->dev_if->in_ep_regs[epnum];
  24649. +
  24650. + len = dwc_ep->xfer_len - dwc_ep->xfer_count;
  24651. +
  24652. + if (len > dwc_ep->maxpacket) {
  24653. + len = dwc_ep->maxpacket;
  24654. + }
  24655. +
  24656. + dwords = (len + 3) / 4;
  24657. +
  24658. + /* While there is space in the queue and space in the FIFO and
  24659. + * More data to tranfer, Write packets to the Tx FIFO */
  24660. + txstatus.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
  24661. + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum, txstatus.d32);
  24662. +
  24663. + while (txstatus.b.txfspcavail > dwords &&
  24664. + dwc_ep->xfer_count < dwc_ep->xfer_len && dwc_ep->xfer_len != 0) {
  24665. + /* Write the FIFO */
  24666. + dwc_otg_ep_write_packet(core_if, dwc_ep, 0);
  24667. +
  24668. + len = dwc_ep->xfer_len - dwc_ep->xfer_count;
  24669. + if (len > dwc_ep->maxpacket) {
  24670. + len = dwc_ep->maxpacket;
  24671. + }
  24672. +
  24673. + dwords = (len + 3) / 4;
  24674. + txstatus.d32 =
  24675. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
  24676. + DWC_DEBUGPL(DBG_PCDV, "dtxfsts[%d]=0x%08x\n", epnum,
  24677. + txstatus.d32);
  24678. + }
  24679. +
  24680. + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum,
  24681. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts));
  24682. +
  24683. + return 1;
  24684. +}
  24685. +/**
  24686. + * This function does the setup for a data transfer for an EP and
  24687. + * starts the transfer. For an IN transfer, the packets will be
  24688. + * loaded into the appropriate Tx FIFO in the ISR. For OUT transfers,
  24689. + * the packets are unloaded from the Rx FIFO in the ISR. the ISR.
  24690. + *
  24691. + * @param core_if Programming view of DWC_otg controller.
  24692. + * @param ep The EP to start the transfer on.
  24693. + */
  24694. +
  24695. +void dwc_otg_ep_start_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  24696. +{
  24697. + depctl_data_t depctl;
  24698. + deptsiz_data_t deptsiz;
  24699. + gintmsk_data_t intr_mask = {.d32 = 0 };
  24700. +
  24701. + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s()\n", __func__);
  24702. + DWC_DEBUGPL(DBG_PCD, "ep%d-%s xfer_len=%d xfer_cnt=%d "
  24703. + "xfer_buff=%p start_xfer_buff=%p, total_len = %d\n",
  24704. + ep->num, (ep->is_in ? "IN" : "OUT"), ep->xfer_len,
  24705. + ep->xfer_count, ep->xfer_buff, ep->start_xfer_buff,
  24706. + ep->total_len);
  24707. + /* IN endpoint */
  24708. + if (ep->is_in == 1) {
  24709. + dwc_otg_dev_in_ep_regs_t *in_regs =
  24710. + core_if->dev_if->in_ep_regs[ep->num];
  24711. +
  24712. + gnptxsts_data_t gtxstatus;
  24713. +
  24714. + gtxstatus.d32 =
  24715. + DWC_READ_REG32(&core_if->core_global_regs->gnptxsts);
  24716. +
  24717. + if (core_if->en_multiple_tx_fifo == 0
  24718. + && gtxstatus.b.nptxqspcavail == 0 && !core_if->dma_enable) {
  24719. +#ifdef DEBUG
  24720. + DWC_PRINTF("TX Queue Full (0x%0x)\n", gtxstatus.d32);
  24721. +#endif
  24722. + return;
  24723. + }
  24724. +
  24725. + depctl.d32 = DWC_READ_REG32(&(in_regs->diepctl));
  24726. + deptsiz.d32 = DWC_READ_REG32(&(in_regs->dieptsiz));
  24727. +
  24728. + if (ep->maxpacket > ep->maxxfer / MAX_PKT_CNT)
  24729. + ep->xfer_len += (ep->maxxfer < (ep->total_len - ep->xfer_len)) ?
  24730. + ep->maxxfer : (ep->total_len - ep->xfer_len);
  24731. + else
  24732. + ep->xfer_len += (MAX_PKT_CNT * ep->maxpacket < (ep->total_len - ep->xfer_len)) ?
  24733. + MAX_PKT_CNT * ep->maxpacket : (ep->total_len - ep->xfer_len);
  24734. +
  24735. +
  24736. + /* Zero Length Packet? */
  24737. + if ((ep->xfer_len - ep->xfer_count) == 0) {
  24738. + deptsiz.b.xfersize = 0;
  24739. + deptsiz.b.pktcnt = 1;
  24740. + } else {
  24741. + /* Program the transfer size and packet count
  24742. + * as follows: xfersize = N * maxpacket +
  24743. + * short_packet pktcnt = N + (short_packet
  24744. + * exist ? 1 : 0)
  24745. + */
  24746. + deptsiz.b.xfersize = ep->xfer_len - ep->xfer_count;
  24747. + deptsiz.b.pktcnt =
  24748. + (ep->xfer_len - ep->xfer_count - 1 +
  24749. + ep->maxpacket) / ep->maxpacket;
  24750. + if (deptsiz.b.pktcnt > MAX_PKT_CNT) {
  24751. + deptsiz.b.pktcnt = MAX_PKT_CNT;
  24752. + deptsiz.b.xfersize = deptsiz.b.pktcnt * ep->maxpacket;
  24753. + }
  24754. + if (ep->type == DWC_OTG_EP_TYPE_ISOC)
  24755. + deptsiz.b.mc = deptsiz.b.pktcnt;
  24756. + }
  24757. +
  24758. + /* Write the DMA register */
  24759. + if (core_if->dma_enable) {
  24760. + if (core_if->dma_desc_enable == 0) {
  24761. + if (ep->type != DWC_OTG_EP_TYPE_ISOC)
  24762. + deptsiz.b.mc = 1;
  24763. + DWC_WRITE_REG32(&in_regs->dieptsiz,
  24764. + deptsiz.d32);
  24765. + DWC_WRITE_REG32(&(in_regs->diepdma),
  24766. + (uint32_t) ep->dma_addr);
  24767. + } else {
  24768. +#ifdef DWC_UTE_CFI
  24769. + /* The descriptor chain should be already initialized by now */
  24770. + if (ep->buff_mode != BM_STANDARD) {
  24771. + DWC_WRITE_REG32(&in_regs->diepdma,
  24772. + ep->descs_dma_addr);
  24773. + } else {
  24774. +#endif
  24775. + init_dma_desc_chain(core_if, ep);
  24776. + /** DIEPDMAn Register write */
  24777. + DWC_WRITE_REG32(&in_regs->diepdma,
  24778. + ep->dma_desc_addr);
  24779. +#ifdef DWC_UTE_CFI
  24780. + }
  24781. +#endif
  24782. + }
  24783. + } else {
  24784. + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
  24785. + if (ep->type != DWC_OTG_EP_TYPE_ISOC) {
  24786. + /**
  24787. + * Enable the Non-Periodic Tx FIFO empty interrupt,
  24788. + * or the Tx FIFO epmty interrupt in dedicated Tx FIFO mode,
  24789. + * the data will be written into the fifo by the ISR.
  24790. + */
  24791. + if (core_if->en_multiple_tx_fifo == 0) {
  24792. + intr_mask.b.nptxfempty = 1;
  24793. + DWC_MODIFY_REG32
  24794. + (&core_if->core_global_regs->gintmsk,
  24795. + intr_mask.d32, intr_mask.d32);
  24796. + } else {
  24797. + /* Enable the Tx FIFO Empty Interrupt for this EP */
  24798. + if (ep->xfer_len > 0) {
  24799. + uint32_t fifoemptymsk = 0;
  24800. + fifoemptymsk = 1 << ep->num;
  24801. + DWC_MODIFY_REG32
  24802. + (&core_if->dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
  24803. + 0, fifoemptymsk);
  24804. +
  24805. + }
  24806. + }
  24807. + } else {
  24808. + write_isoc_tx_fifo(core_if, ep);
  24809. + }
  24810. + }
  24811. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
  24812. + depctl.b.nextep = core_if->nextep_seq[ep->num];
  24813. +
  24814. + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
  24815. + dsts_data_t dsts = {.d32 = 0};
  24816. + if (ep->bInterval == 1) {
  24817. + dsts.d32 =
  24818. + DWC_READ_REG32(&core_if->dev_if->
  24819. + dev_global_regs->dsts);
  24820. + ep->frame_num = dsts.b.soffn + ep->bInterval;
  24821. + if (ep->frame_num > 0x3FFF) {
  24822. + ep->frm_overrun = 1;
  24823. + ep->frame_num &= 0x3FFF;
  24824. + } else
  24825. + ep->frm_overrun = 0;
  24826. + if (ep->frame_num & 0x1) {
  24827. + depctl.b.setd1pid = 1;
  24828. + } else {
  24829. + depctl.b.setd0pid = 1;
  24830. + }
  24831. + }
  24832. + }
  24833. + /* EP enable, IN data in FIFO */
  24834. + depctl.b.cnak = 1;
  24835. + depctl.b.epena = 1;
  24836. + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
  24837. +
  24838. + } else {
  24839. + /* OUT endpoint */
  24840. + dwc_otg_dev_out_ep_regs_t *out_regs =
  24841. + core_if->dev_if->out_ep_regs[ep->num];
  24842. +
  24843. + depctl.d32 = DWC_READ_REG32(&(out_regs->doepctl));
  24844. + deptsiz.d32 = DWC_READ_REG32(&(out_regs->doeptsiz));
  24845. +
  24846. + if (!core_if->dma_desc_enable) {
  24847. + if (ep->maxpacket > ep->maxxfer / MAX_PKT_CNT)
  24848. + ep->xfer_len += (ep->maxxfer < (ep->total_len - ep->xfer_len)) ?
  24849. + ep->maxxfer : (ep->total_len - ep->xfer_len);
  24850. + else
  24851. + ep->xfer_len += (MAX_PKT_CNT * ep->maxpacket < (ep->total_len
  24852. + - ep->xfer_len)) ? MAX_PKT_CNT * ep->maxpacket : (ep->total_len - ep->xfer_len);
  24853. + }
  24854. +
  24855. + /* Program the transfer size and packet count as follows:
  24856. + *
  24857. + * pktcnt = N
  24858. + * xfersize = N * maxpacket
  24859. + */
  24860. + if ((ep->xfer_len - ep->xfer_count) == 0) {
  24861. + /* Zero Length Packet */
  24862. + deptsiz.b.xfersize = ep->maxpacket;
  24863. + deptsiz.b.pktcnt = 1;
  24864. + } else {
  24865. + deptsiz.b.pktcnt =
  24866. + (ep->xfer_len - ep->xfer_count +
  24867. + (ep->maxpacket - 1)) / ep->maxpacket;
  24868. + if (deptsiz.b.pktcnt > MAX_PKT_CNT) {
  24869. + deptsiz.b.pktcnt = MAX_PKT_CNT;
  24870. + }
  24871. + if (!core_if->dma_desc_enable) {
  24872. + ep->xfer_len =
  24873. + deptsiz.b.pktcnt * ep->maxpacket + ep->xfer_count;
  24874. + }
  24875. + deptsiz.b.xfersize = ep->xfer_len - ep->xfer_count;
  24876. + }
  24877. +
  24878. + DWC_DEBUGPL(DBG_PCDV, "ep%d xfersize=%d pktcnt=%d\n",
  24879. + ep->num, deptsiz.b.xfersize, deptsiz.b.pktcnt);
  24880. +
  24881. + if (core_if->dma_enable) {
  24882. + if (!core_if->dma_desc_enable) {
  24883. + DWC_WRITE_REG32(&out_regs->doeptsiz,
  24884. + deptsiz.d32);
  24885. +
  24886. + DWC_WRITE_REG32(&(out_regs->doepdma),
  24887. + (uint32_t) ep->dma_addr);
  24888. + } else {
  24889. +#ifdef DWC_UTE_CFI
  24890. + /* The descriptor chain should be already initialized by now */
  24891. + if (ep->buff_mode != BM_STANDARD) {
  24892. + DWC_WRITE_REG32(&out_regs->doepdma,
  24893. + ep->descs_dma_addr);
  24894. + } else {
  24895. +#endif
  24896. + /** This is used for interrupt out transfers*/
  24897. + if (!ep->xfer_len)
  24898. + ep->xfer_len = ep->total_len;
  24899. + init_dma_desc_chain(core_if, ep);
  24900. +
  24901. + if (core_if->core_params->dev_out_nak) {
  24902. + if (ep->type == DWC_OTG_EP_TYPE_BULK) {
  24903. + deptsiz.b.pktcnt = (ep->total_len +
  24904. + (ep->maxpacket - 1)) / ep->maxpacket;
  24905. + deptsiz.b.xfersize = ep->total_len;
  24906. + /* Remember initial value of doeptsiz */
  24907. + core_if->start_doeptsiz_val[ep->num] = deptsiz.d32;
  24908. + DWC_WRITE_REG32(&out_regs->doeptsiz,
  24909. + deptsiz.d32);
  24910. + }
  24911. + }
  24912. + /** DOEPDMAn Register write */
  24913. + DWC_WRITE_REG32(&out_regs->doepdma,
  24914. + ep->dma_desc_addr);
  24915. +#ifdef DWC_UTE_CFI
  24916. + }
  24917. +#endif
  24918. + }
  24919. + } else {
  24920. + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
  24921. + }
  24922. +
  24923. + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
  24924. + dsts_data_t dsts = {.d32 = 0};
  24925. + if (ep->bInterval == 1) {
  24926. + dsts.d32 =
  24927. + DWC_READ_REG32(&core_if->dev_if->
  24928. + dev_global_regs->dsts);
  24929. + ep->frame_num = dsts.b.soffn + ep->bInterval;
  24930. + if (ep->frame_num > 0x3FFF) {
  24931. + ep->frm_overrun = 1;
  24932. + ep->frame_num &= 0x3FFF;
  24933. + } else
  24934. + ep->frm_overrun = 0;
  24935. +
  24936. + if (ep->frame_num & 0x1) {
  24937. + depctl.b.setd1pid = 1;
  24938. + } else {
  24939. + depctl.b.setd0pid = 1;
  24940. + }
  24941. + }
  24942. + }
  24943. +
  24944. + /* EP enable */
  24945. + depctl.b.cnak = 1;
  24946. + depctl.b.epena = 1;
  24947. +
  24948. + DWC_WRITE_REG32(&out_regs->doepctl, depctl.d32);
  24949. +
  24950. + DWC_DEBUGPL(DBG_PCD, "DOEPCTL=%08x DOEPTSIZ=%08x\n",
  24951. + DWC_READ_REG32(&out_regs->doepctl),
  24952. + DWC_READ_REG32(&out_regs->doeptsiz));
  24953. + DWC_DEBUGPL(DBG_PCD, "DAINTMSK=%08x GINTMSK=%08x\n",
  24954. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->
  24955. + daintmsk),
  24956. + DWC_READ_REG32(&core_if->core_global_regs->
  24957. + gintmsk));
  24958. +
  24959. + /* Timer is scheduling only for out bulk transfers for
  24960. + * "Device DDMA OUT NAK Enhancement" feature to inform user
  24961. + * about received data payload in case of timeout
  24962. + */
  24963. + if (core_if->core_params->dev_out_nak) {
  24964. + if (ep->type == DWC_OTG_EP_TYPE_BULK) {
  24965. + core_if->ep_xfer_info[ep->num].core_if = core_if;
  24966. + core_if->ep_xfer_info[ep->num].ep = ep;
  24967. + core_if->ep_xfer_info[ep->num].state = 1;
  24968. +
  24969. + /* Start a timer for this transfer. */
  24970. + DWC_TIMER_SCHEDULE(core_if->ep_xfer_timer[ep->num], 10000);
  24971. + }
  24972. + }
  24973. + }
  24974. +}
  24975. +
  24976. +/**
  24977. + * This function setup a zero length transfer in Buffer DMA and
  24978. + * Slave modes for usb requests with zero field set
  24979. + *
  24980. + * @param core_if Programming view of DWC_otg controller.
  24981. + * @param ep The EP to start the transfer on.
  24982. + *
  24983. + */
  24984. +void dwc_otg_ep_start_zl_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  24985. +{
  24986. +
  24987. + depctl_data_t depctl;
  24988. + deptsiz_data_t deptsiz;
  24989. + gintmsk_data_t intr_mask = {.d32 = 0 };
  24990. +
  24991. + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s()\n", __func__);
  24992. + DWC_PRINTF("zero length transfer is called\n");
  24993. +
  24994. + /* IN endpoint */
  24995. + if (ep->is_in == 1) {
  24996. + dwc_otg_dev_in_ep_regs_t *in_regs =
  24997. + core_if->dev_if->in_ep_regs[ep->num];
  24998. +
  24999. + depctl.d32 = DWC_READ_REG32(&(in_regs->diepctl));
  25000. + deptsiz.d32 = DWC_READ_REG32(&(in_regs->dieptsiz));
  25001. +
  25002. + deptsiz.b.xfersize = 0;
  25003. + deptsiz.b.pktcnt = 1;
  25004. +
  25005. + /* Write the DMA register */
  25006. + if (core_if->dma_enable) {
  25007. + if (core_if->dma_desc_enable == 0) {
  25008. + deptsiz.b.mc = 1;
  25009. + DWC_WRITE_REG32(&in_regs->dieptsiz,
  25010. + deptsiz.d32);
  25011. + DWC_WRITE_REG32(&(in_regs->diepdma),
  25012. + (uint32_t) ep->dma_addr);
  25013. + }
  25014. + } else {
  25015. + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
  25016. + /**
  25017. + * Enable the Non-Periodic Tx FIFO empty interrupt,
  25018. + * or the Tx FIFO epmty interrupt in dedicated Tx FIFO mode,
  25019. + * the data will be written into the fifo by the ISR.
  25020. + */
  25021. + if (core_if->en_multiple_tx_fifo == 0) {
  25022. + intr_mask.b.nptxfempty = 1;
  25023. + DWC_MODIFY_REG32(&core_if->
  25024. + core_global_regs->gintmsk,
  25025. + intr_mask.d32, intr_mask.d32);
  25026. + } else {
  25027. + /* Enable the Tx FIFO Empty Interrupt for this EP */
  25028. + if (ep->xfer_len > 0) {
  25029. + uint32_t fifoemptymsk = 0;
  25030. + fifoemptymsk = 1 << ep->num;
  25031. + DWC_MODIFY_REG32(&core_if->
  25032. + dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
  25033. + 0, fifoemptymsk);
  25034. + }
  25035. + }
  25036. + }
  25037. +
  25038. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
  25039. + depctl.b.nextep = core_if->nextep_seq[ep->num];
  25040. + /* EP enable, IN data in FIFO */
  25041. + depctl.b.cnak = 1;
  25042. + depctl.b.epena = 1;
  25043. + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
  25044. +
  25045. + } else {
  25046. + /* OUT endpoint */
  25047. + dwc_otg_dev_out_ep_regs_t *out_regs =
  25048. + core_if->dev_if->out_ep_regs[ep->num];
  25049. +
  25050. + depctl.d32 = DWC_READ_REG32(&(out_regs->doepctl));
  25051. + deptsiz.d32 = DWC_READ_REG32(&(out_regs->doeptsiz));
  25052. +
  25053. + /* Zero Length Packet */
  25054. + deptsiz.b.xfersize = ep->maxpacket;
  25055. + deptsiz.b.pktcnt = 1;
  25056. +
  25057. + if (core_if->dma_enable) {
  25058. + if (!core_if->dma_desc_enable) {
  25059. + DWC_WRITE_REG32(&out_regs->doeptsiz,
  25060. + deptsiz.d32);
  25061. +
  25062. + DWC_WRITE_REG32(&(out_regs->doepdma),
  25063. + (uint32_t) ep->dma_addr);
  25064. + }
  25065. + } else {
  25066. + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
  25067. + }
  25068. +
  25069. + /* EP enable */
  25070. + depctl.b.cnak = 1;
  25071. + depctl.b.epena = 1;
  25072. +
  25073. + DWC_WRITE_REG32(&out_regs->doepctl, depctl.d32);
  25074. +
  25075. + }
  25076. +}
  25077. +
  25078. +/**
  25079. + * This function does the setup for a data transfer for EP0 and starts
  25080. + * the transfer. For an IN transfer, the packets will be loaded into
  25081. + * the appropriate Tx FIFO in the ISR. For OUT transfers, the packets are
  25082. + * unloaded from the Rx FIFO in the ISR.
  25083. + *
  25084. + * @param core_if Programming view of DWC_otg controller.
  25085. + * @param ep The EP0 data.
  25086. + */
  25087. +void dwc_otg_ep0_start_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  25088. +{
  25089. + depctl_data_t depctl;
  25090. + deptsiz0_data_t deptsiz;
  25091. + gintmsk_data_t intr_mask = {.d32 = 0 };
  25092. + dwc_otg_dev_dma_desc_t *dma_desc;
  25093. +
  25094. + DWC_DEBUGPL(DBG_PCD, "ep%d-%s xfer_len=%d xfer_cnt=%d "
  25095. + "xfer_buff=%p start_xfer_buff=%p \n",
  25096. + ep->num, (ep->is_in ? "IN" : "OUT"), ep->xfer_len,
  25097. + ep->xfer_count, ep->xfer_buff, ep->start_xfer_buff);
  25098. +
  25099. + ep->total_len = ep->xfer_len;
  25100. +
  25101. + /* IN endpoint */
  25102. + if (ep->is_in == 1) {
  25103. + dwc_otg_dev_in_ep_regs_t *in_regs =
  25104. + core_if->dev_if->in_ep_regs[0];
  25105. +
  25106. + gnptxsts_data_t gtxstatus;
  25107. +
  25108. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  25109. + depctl.d32 = DWC_READ_REG32(&in_regs->diepctl);
  25110. + if (depctl.b.epena)
  25111. + return;
  25112. + }
  25113. +
  25114. + gtxstatus.d32 =
  25115. + DWC_READ_REG32(&core_if->core_global_regs->gnptxsts);
  25116. +
  25117. + /* If dedicated FIFO every time flush fifo before enable ep*/
  25118. + if (core_if->en_multiple_tx_fifo && core_if->snpsid >= OTG_CORE_REV_3_00a)
  25119. + dwc_otg_flush_tx_fifo(core_if, ep->tx_fifo_num);
  25120. +
  25121. + if (core_if->en_multiple_tx_fifo == 0
  25122. + && gtxstatus.b.nptxqspcavail == 0
  25123. + && !core_if->dma_enable) {
  25124. +#ifdef DEBUG
  25125. + deptsiz.d32 = DWC_READ_REG32(&in_regs->dieptsiz);
  25126. + DWC_DEBUGPL(DBG_PCD, "DIEPCTL0=%0x\n",
  25127. + DWC_READ_REG32(&in_regs->diepctl));
  25128. + DWC_DEBUGPL(DBG_PCD, "DIEPTSIZ0=%0x (sz=%d, pcnt=%d)\n",
  25129. + deptsiz.d32,
  25130. + deptsiz.b.xfersize, deptsiz.b.pktcnt);
  25131. + DWC_PRINTF("TX Queue or FIFO Full (0x%0x)\n",
  25132. + gtxstatus.d32);
  25133. +#endif
  25134. + return;
  25135. + }
  25136. +
  25137. + depctl.d32 = DWC_READ_REG32(&in_regs->diepctl);
  25138. + deptsiz.d32 = DWC_READ_REG32(&in_regs->dieptsiz);
  25139. +
  25140. + /* Zero Length Packet? */
  25141. + if (ep->xfer_len == 0) {
  25142. + deptsiz.b.xfersize = 0;
  25143. + deptsiz.b.pktcnt = 1;
  25144. + } else {
  25145. + /* Program the transfer size and packet count
  25146. + * as follows: xfersize = N * maxpacket +
  25147. + * short_packet pktcnt = N + (short_packet
  25148. + * exist ? 1 : 0)
  25149. + */
  25150. + if (ep->xfer_len > ep->maxpacket) {
  25151. + ep->xfer_len = ep->maxpacket;
  25152. + deptsiz.b.xfersize = ep->maxpacket;
  25153. + } else {
  25154. + deptsiz.b.xfersize = ep->xfer_len;
  25155. + }
  25156. + deptsiz.b.pktcnt = 1;
  25157. +
  25158. + }
  25159. + DWC_DEBUGPL(DBG_PCDV,
  25160. + "IN len=%d xfersize=%d pktcnt=%d [%08x]\n",
  25161. + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt,
  25162. + deptsiz.d32);
  25163. +
  25164. + /* Write the DMA register */
  25165. + if (core_if->dma_enable) {
  25166. + if (core_if->dma_desc_enable == 0) {
  25167. + DWC_WRITE_REG32(&in_regs->dieptsiz,
  25168. + deptsiz.d32);
  25169. +
  25170. + DWC_WRITE_REG32(&(in_regs->diepdma),
  25171. + (uint32_t) ep->dma_addr);
  25172. + } else {
  25173. + dma_desc = core_if->dev_if->in_desc_addr;
  25174. +
  25175. + /** DMA Descriptor Setup */
  25176. + dma_desc->status.b.bs = BS_HOST_BUSY;
  25177. + dma_desc->status.b.l = 1;
  25178. + dma_desc->status.b.ioc = 1;
  25179. + dma_desc->status.b.sp =
  25180. + (ep->xfer_len == ep->maxpacket) ? 0 : 1;
  25181. + dma_desc->status.b.bytes = ep->xfer_len;
  25182. + dma_desc->buf = ep->dma_addr;
  25183. + dma_desc->status.b.sts = 0;
  25184. + dma_desc->status.b.bs = BS_HOST_READY;
  25185. +
  25186. + /** DIEPDMA0 Register write */
  25187. + DWC_WRITE_REG32(&in_regs->diepdma,
  25188. + core_if->
  25189. + dev_if->dma_in_desc_addr);
  25190. + }
  25191. + } else {
  25192. + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
  25193. + }
  25194. +
  25195. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
  25196. + depctl.b.nextep = core_if->nextep_seq[ep->num];
  25197. + /* EP enable, IN data in FIFO */
  25198. + depctl.b.cnak = 1;
  25199. + depctl.b.epena = 1;
  25200. + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
  25201. +
  25202. + /**
  25203. + * Enable the Non-Periodic Tx FIFO empty interrupt, the
  25204. + * data will be written into the fifo by the ISR.
  25205. + */
  25206. + if (!core_if->dma_enable) {
  25207. + if (core_if->en_multiple_tx_fifo == 0) {
  25208. + intr_mask.b.nptxfempty = 1;
  25209. + DWC_MODIFY_REG32(&core_if->
  25210. + core_global_regs->gintmsk,
  25211. + intr_mask.d32, intr_mask.d32);
  25212. + } else {
  25213. + /* Enable the Tx FIFO Empty Interrupt for this EP */
  25214. + if (ep->xfer_len > 0) {
  25215. + uint32_t fifoemptymsk = 0;
  25216. + fifoemptymsk |= 1 << ep->num;
  25217. + DWC_MODIFY_REG32(&core_if->
  25218. + dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
  25219. + 0, fifoemptymsk);
  25220. + }
  25221. + }
  25222. + }
  25223. + } else {
  25224. + /* OUT endpoint */
  25225. + dwc_otg_dev_out_ep_regs_t *out_regs =
  25226. + core_if->dev_if->out_ep_regs[0];
  25227. +
  25228. + depctl.d32 = DWC_READ_REG32(&out_regs->doepctl);
  25229. + deptsiz.d32 = DWC_READ_REG32(&out_regs->doeptsiz);
  25230. +
  25231. + /* Program the transfer size and packet count as follows:
  25232. + * xfersize = N * (maxpacket + 4 - (maxpacket % 4))
  25233. + * pktcnt = N */
  25234. + /* Zero Length Packet */
  25235. + deptsiz.b.xfersize = ep->maxpacket;
  25236. + deptsiz.b.pktcnt = 1;
  25237. + if (core_if->snpsid >= OTG_CORE_REV_3_00a)
  25238. + deptsiz.b.supcnt = 3;
  25239. +
  25240. + DWC_DEBUGPL(DBG_PCDV, "len=%d xfersize=%d pktcnt=%d\n",
  25241. + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt);
  25242. +
  25243. + if (core_if->dma_enable) {
  25244. + if (!core_if->dma_desc_enable) {
  25245. + DWC_WRITE_REG32(&out_regs->doeptsiz,
  25246. + deptsiz.d32);
  25247. +
  25248. + DWC_WRITE_REG32(&(out_regs->doepdma),
  25249. + (uint32_t) ep->dma_addr);
  25250. + } else {
  25251. + dma_desc = core_if->dev_if->out_desc_addr;
  25252. +
  25253. + /** DMA Descriptor Setup */
  25254. + dma_desc->status.b.bs = BS_HOST_BUSY;
  25255. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  25256. + dma_desc->status.b.mtrf = 0;
  25257. + dma_desc->status.b.sr = 0;
  25258. + }
  25259. + dma_desc->status.b.l = 1;
  25260. + dma_desc->status.b.ioc = 1;
  25261. + dma_desc->status.b.bytes = ep->maxpacket;
  25262. + dma_desc->buf = ep->dma_addr;
  25263. + dma_desc->status.b.sts = 0;
  25264. + dma_desc->status.b.bs = BS_HOST_READY;
  25265. +
  25266. + /** DOEPDMA0 Register write */
  25267. + DWC_WRITE_REG32(&out_regs->doepdma,
  25268. + core_if->dev_if->
  25269. + dma_out_desc_addr);
  25270. + }
  25271. + } else {
  25272. + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
  25273. + }
  25274. +
  25275. + /* EP enable */
  25276. + depctl.b.cnak = 1;
  25277. + depctl.b.epena = 1;
  25278. + DWC_WRITE_REG32(&(out_regs->doepctl), depctl.d32);
  25279. + }
  25280. +}
  25281. +
  25282. +/**
  25283. + * This function continues control IN transfers started by
  25284. + * dwc_otg_ep0_start_transfer, when the transfer does not fit in a
  25285. + * single packet. NOTE: The DIEPCTL0/DOEPCTL0 registers only have one
  25286. + * bit for the packet count.
  25287. + *
  25288. + * @param core_if Programming view of DWC_otg controller.
  25289. + * @param ep The EP0 data.
  25290. + */
  25291. +void dwc_otg_ep0_continue_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  25292. +{
  25293. + depctl_data_t depctl;
  25294. + deptsiz0_data_t deptsiz;
  25295. + gintmsk_data_t intr_mask = {.d32 = 0 };
  25296. + dwc_otg_dev_dma_desc_t *dma_desc;
  25297. +
  25298. + if (ep->is_in == 1) {
  25299. + dwc_otg_dev_in_ep_regs_t *in_regs =
  25300. + core_if->dev_if->in_ep_regs[0];
  25301. + gnptxsts_data_t tx_status = {.d32 = 0 };
  25302. +
  25303. + tx_status.d32 =
  25304. + DWC_READ_REG32(&core_if->core_global_regs->gnptxsts);
  25305. + /** @todo Should there be check for room in the Tx
  25306. + * Status Queue. If not remove the code above this comment. */
  25307. +
  25308. + depctl.d32 = DWC_READ_REG32(&in_regs->diepctl);
  25309. + deptsiz.d32 = DWC_READ_REG32(&in_regs->dieptsiz);
  25310. +
  25311. + /* Program the transfer size and packet count
  25312. + * as follows: xfersize = N * maxpacket +
  25313. + * short_packet pktcnt = N + (short_packet
  25314. + * exist ? 1 : 0)
  25315. + */
  25316. +
  25317. + if (core_if->dma_desc_enable == 0) {
  25318. + deptsiz.b.xfersize =
  25319. + (ep->total_len - ep->xfer_count) >
  25320. + ep->maxpacket ? ep->maxpacket : (ep->total_len -
  25321. + ep->xfer_count);
  25322. + deptsiz.b.pktcnt = 1;
  25323. + if (core_if->dma_enable == 0) {
  25324. + ep->xfer_len += deptsiz.b.xfersize;
  25325. + } else {
  25326. + ep->xfer_len = deptsiz.b.xfersize;
  25327. + }
  25328. + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
  25329. + } else {
  25330. + ep->xfer_len =
  25331. + (ep->total_len - ep->xfer_count) >
  25332. + ep->maxpacket ? ep->maxpacket : (ep->total_len -
  25333. + ep->xfer_count);
  25334. +
  25335. + dma_desc = core_if->dev_if->in_desc_addr;
  25336. +
  25337. + /** DMA Descriptor Setup */
  25338. + dma_desc->status.b.bs = BS_HOST_BUSY;
  25339. + dma_desc->status.b.l = 1;
  25340. + dma_desc->status.b.ioc = 1;
  25341. + dma_desc->status.b.sp =
  25342. + (ep->xfer_len == ep->maxpacket) ? 0 : 1;
  25343. + dma_desc->status.b.bytes = ep->xfer_len;
  25344. + dma_desc->buf = ep->dma_addr;
  25345. + dma_desc->status.b.sts = 0;
  25346. + dma_desc->status.b.bs = BS_HOST_READY;
  25347. +
  25348. + /** DIEPDMA0 Register write */
  25349. + DWC_WRITE_REG32(&in_regs->diepdma,
  25350. + core_if->dev_if->dma_in_desc_addr);
  25351. + }
  25352. +
  25353. + DWC_DEBUGPL(DBG_PCDV,
  25354. + "IN len=%d xfersize=%d pktcnt=%d [%08x]\n",
  25355. + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt,
  25356. + deptsiz.d32);
  25357. +
  25358. + /* Write the DMA register */
  25359. + if (core_if->hwcfg2.b.architecture == DWC_INT_DMA_ARCH) {
  25360. + if (core_if->dma_desc_enable == 0)
  25361. + DWC_WRITE_REG32(&(in_regs->diepdma),
  25362. + (uint32_t) ep->dma_addr);
  25363. + }
  25364. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
  25365. + depctl.b.nextep = core_if->nextep_seq[ep->num];
  25366. + /* EP enable, IN data in FIFO */
  25367. + depctl.b.cnak = 1;
  25368. + depctl.b.epena = 1;
  25369. + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
  25370. +
  25371. + /**
  25372. + * Enable the Non-Periodic Tx FIFO empty interrupt, the
  25373. + * data will be written into the fifo by the ISR.
  25374. + */
  25375. + if (!core_if->dma_enable) {
  25376. + if (core_if->en_multiple_tx_fifo == 0) {
  25377. + /* First clear it from GINTSTS */
  25378. + intr_mask.b.nptxfempty = 1;
  25379. + DWC_MODIFY_REG32(&core_if->
  25380. + core_global_regs->gintmsk,
  25381. + intr_mask.d32, intr_mask.d32);
  25382. +
  25383. + } else {
  25384. + /* Enable the Tx FIFO Empty Interrupt for this EP */
  25385. + if (ep->xfer_len > 0) {
  25386. + uint32_t fifoemptymsk = 0;
  25387. + fifoemptymsk |= 1 << ep->num;
  25388. + DWC_MODIFY_REG32(&core_if->
  25389. + dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
  25390. + 0, fifoemptymsk);
  25391. + }
  25392. + }
  25393. + }
  25394. + } else {
  25395. + dwc_otg_dev_out_ep_regs_t *out_regs =
  25396. + core_if->dev_if->out_ep_regs[0];
  25397. +
  25398. + depctl.d32 = DWC_READ_REG32(&out_regs->doepctl);
  25399. + deptsiz.d32 = DWC_READ_REG32(&out_regs->doeptsiz);
  25400. +
  25401. + /* Program the transfer size and packet count
  25402. + * as follows: xfersize = N * maxpacket +
  25403. + * short_packet pktcnt = N + (short_packet
  25404. + * exist ? 1 : 0)
  25405. + */
  25406. + deptsiz.b.xfersize = ep->maxpacket;
  25407. + deptsiz.b.pktcnt = 1;
  25408. +
  25409. + if (core_if->dma_desc_enable == 0) {
  25410. + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
  25411. + } else {
  25412. + dma_desc = core_if->dev_if->out_desc_addr;
  25413. +
  25414. + /** DMA Descriptor Setup */
  25415. + dma_desc->status.b.bs = BS_HOST_BUSY;
  25416. + dma_desc->status.b.l = 1;
  25417. + dma_desc->status.b.ioc = 1;
  25418. + dma_desc->status.b.bytes = ep->maxpacket;
  25419. + dma_desc->buf = ep->dma_addr;
  25420. + dma_desc->status.b.sts = 0;
  25421. + dma_desc->status.b.bs = BS_HOST_READY;
  25422. +
  25423. + /** DOEPDMA0 Register write */
  25424. + DWC_WRITE_REG32(&out_regs->doepdma,
  25425. + core_if->dev_if->dma_out_desc_addr);
  25426. + }
  25427. +
  25428. + DWC_DEBUGPL(DBG_PCDV,
  25429. + "IN len=%d xfersize=%d pktcnt=%d [%08x]\n",
  25430. + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt,
  25431. + deptsiz.d32);
  25432. +
  25433. + /* Write the DMA register */
  25434. + if (core_if->hwcfg2.b.architecture == DWC_INT_DMA_ARCH) {
  25435. + if (core_if->dma_desc_enable == 0)
  25436. + DWC_WRITE_REG32(&(out_regs->doepdma),
  25437. + (uint32_t) ep->dma_addr);
  25438. +
  25439. + }
  25440. +
  25441. + /* EP enable, IN data in FIFO */
  25442. + depctl.b.cnak = 1;
  25443. + depctl.b.epena = 1;
  25444. + DWC_WRITE_REG32(&out_regs->doepctl, depctl.d32);
  25445. +
  25446. + }
  25447. +}
  25448. +
  25449. +#ifdef DEBUG
  25450. +void dump_msg(const u8 * buf, unsigned int length)
  25451. +{
  25452. + unsigned int start, num, i;
  25453. + char line[52], *p;
  25454. +
  25455. + if (length >= 512)
  25456. + return;
  25457. + start = 0;
  25458. + while (length > 0) {
  25459. + num = length < 16u ? length : 16u;
  25460. + p = line;
  25461. + for (i = 0; i < num; ++i) {
  25462. + if (i == 8)
  25463. + *p++ = ' ';
  25464. + DWC_SPRINTF(p, " %02x", buf[i]);
  25465. + p += 3;
  25466. + }
  25467. + *p = 0;
  25468. + DWC_PRINTF("%6x: %s\n", start, line);
  25469. + buf += num;
  25470. + start += num;
  25471. + length -= num;
  25472. + }
  25473. +}
  25474. +#else
  25475. +static inline void dump_msg(const u8 * buf, unsigned int length)
  25476. +{
  25477. +}
  25478. +#endif
  25479. +
  25480. +/**
  25481. + * This function writes a packet into the Tx FIFO associated with the
  25482. + * EP. For non-periodic EPs the non-periodic Tx FIFO is written. For
  25483. + * periodic EPs the periodic Tx FIFO associated with the EP is written
  25484. + * with all packets for the next micro-frame.
  25485. + *
  25486. + * @param core_if Programming view of DWC_otg controller.
  25487. + * @param ep The EP to write packet for.
  25488. + * @param dma Indicates if DMA is being used.
  25489. + */
  25490. +void dwc_otg_ep_write_packet(dwc_otg_core_if_t * core_if, dwc_ep_t * ep,
  25491. + int dma)
  25492. +{
  25493. + /**
  25494. + * The buffer is padded to DWORD on a per packet basis in
  25495. + * slave/dma mode if the MPS is not DWORD aligned. The last
  25496. + * packet, if short, is also padded to a multiple of DWORD.
  25497. + *
  25498. + * ep->xfer_buff always starts DWORD aligned in memory and is a
  25499. + * multiple of DWORD in length
  25500. + *
  25501. + * ep->xfer_len can be any number of bytes
  25502. + *
  25503. + * ep->xfer_count is a multiple of ep->maxpacket until the last
  25504. + * packet
  25505. + *
  25506. + * FIFO access is DWORD */
  25507. +
  25508. + uint32_t i;
  25509. + uint32_t byte_count;
  25510. + uint32_t dword_count;
  25511. + uint32_t *fifo;
  25512. + uint32_t *data_buff = (uint32_t *) ep->xfer_buff;
  25513. +
  25514. + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s(%p,%p)\n", __func__, core_if,
  25515. + ep);
  25516. + if (ep->xfer_count >= ep->xfer_len) {
  25517. + DWC_WARN("%s() No data for EP%d!!!\n", __func__, ep->num);
  25518. + return;
  25519. + }
  25520. +
  25521. + /* Find the byte length of the packet either short packet or MPS */
  25522. + if ((ep->xfer_len - ep->xfer_count) < ep->maxpacket) {
  25523. + byte_count = ep->xfer_len - ep->xfer_count;
  25524. + } else {
  25525. + byte_count = ep->maxpacket;
  25526. + }
  25527. +
  25528. + /* Find the DWORD length, padded by extra bytes as neccessary if MPS
  25529. + * is not a multiple of DWORD */
  25530. + dword_count = (byte_count + 3) / 4;
  25531. +
  25532. +#ifdef VERBOSE
  25533. + dump_msg(ep->xfer_buff, byte_count);
  25534. +#endif
  25535. +
  25536. + /**@todo NGS Where are the Periodic Tx FIFO addresses
  25537. + * intialized? What should this be? */
  25538. +
  25539. + fifo = core_if->data_fifo[ep->num];
  25540. +
  25541. + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "fifo=%p buff=%p *p=%08x bc=%d\n",
  25542. + fifo, data_buff, *data_buff, byte_count);
  25543. +
  25544. + if (!dma) {
  25545. + for (i = 0; i < dword_count; i++, data_buff++) {
  25546. + DWC_WRITE_REG32(fifo, *data_buff);
  25547. + }
  25548. + }
  25549. +
  25550. + ep->xfer_count += byte_count;
  25551. + ep->xfer_buff += byte_count;
  25552. + ep->dma_addr += byte_count;
  25553. +}
  25554. +
  25555. +/**
  25556. + * Set the EP STALL.
  25557. + *
  25558. + * @param core_if Programming view of DWC_otg controller.
  25559. + * @param ep The EP to set the stall on.
  25560. + */
  25561. +void dwc_otg_ep_set_stall(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  25562. +{
  25563. + depctl_data_t depctl;
  25564. + volatile uint32_t *depctl_addr;
  25565. +
  25566. + DWC_DEBUGPL(DBG_PCD, "%s ep%d-%s\n", __func__, ep->num,
  25567. + (ep->is_in ? "IN" : "OUT"));
  25568. +
  25569. + if (ep->is_in == 1) {
  25570. + depctl_addr = &(core_if->dev_if->in_ep_regs[ep->num]->diepctl);
  25571. + depctl.d32 = DWC_READ_REG32(depctl_addr);
  25572. +
  25573. + /* set the disable and stall bits */
  25574. + if (depctl.b.epena) {
  25575. + depctl.b.epdis = 1;
  25576. + }
  25577. + depctl.b.stall = 1;
  25578. + DWC_WRITE_REG32(depctl_addr, depctl.d32);
  25579. + } else {
  25580. + depctl_addr = &(core_if->dev_if->out_ep_regs[ep->num]->doepctl);
  25581. + depctl.d32 = DWC_READ_REG32(depctl_addr);
  25582. +
  25583. + /* set the stall bit */
  25584. + depctl.b.stall = 1;
  25585. + DWC_WRITE_REG32(depctl_addr, depctl.d32);
  25586. + }
  25587. +
  25588. + DWC_DEBUGPL(DBG_PCD, "DEPCTL=%0x\n", DWC_READ_REG32(depctl_addr));
  25589. +
  25590. + return;
  25591. +}
  25592. +
  25593. +/**
  25594. + * Clear the EP STALL.
  25595. + *
  25596. + * @param core_if Programming view of DWC_otg controller.
  25597. + * @param ep The EP to clear stall from.
  25598. + */
  25599. +void dwc_otg_ep_clear_stall(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  25600. +{
  25601. + depctl_data_t depctl;
  25602. + volatile uint32_t *depctl_addr;
  25603. +
  25604. + DWC_DEBUGPL(DBG_PCD, "%s ep%d-%s\n", __func__, ep->num,
  25605. + (ep->is_in ? "IN" : "OUT"));
  25606. +
  25607. + if (ep->is_in == 1) {
  25608. + depctl_addr = &(core_if->dev_if->in_ep_regs[ep->num]->diepctl);
  25609. + } else {
  25610. + depctl_addr = &(core_if->dev_if->out_ep_regs[ep->num]->doepctl);
  25611. + }
  25612. +
  25613. + depctl.d32 = DWC_READ_REG32(depctl_addr);
  25614. +
  25615. + /* clear the stall bits */
  25616. + depctl.b.stall = 0;
  25617. +
  25618. + /*
  25619. + * USB Spec 9.4.5: For endpoints using data toggle, regardless
  25620. + * of whether an endpoint has the Halt feature set, a
  25621. + * ClearFeature(ENDPOINT_HALT) request always results in the
  25622. + * data toggle being reinitialized to DATA0.
  25623. + */
  25624. + if (ep->type == DWC_OTG_EP_TYPE_INTR ||
  25625. + ep->type == DWC_OTG_EP_TYPE_BULK) {
  25626. + depctl.b.setd0pid = 1; /* DATA0 */
  25627. + }
  25628. +
  25629. + DWC_WRITE_REG32(depctl_addr, depctl.d32);
  25630. + DWC_DEBUGPL(DBG_PCD, "DEPCTL=%0x\n", DWC_READ_REG32(depctl_addr));
  25631. + return;
  25632. +}
  25633. +
  25634. +/**
  25635. + * This function reads a packet from the Rx FIFO into the destination
  25636. + * buffer. To read SETUP data use dwc_otg_read_setup_packet.
  25637. + *
  25638. + * @param core_if Programming view of DWC_otg controller.
  25639. + * @param dest Destination buffer for the packet.
  25640. + * @param bytes Number of bytes to copy to the destination.
  25641. + */
  25642. +void dwc_otg_read_packet(dwc_otg_core_if_t * core_if,
  25643. + uint8_t * dest, uint16_t bytes)
  25644. +{
  25645. + int i;
  25646. + int word_count = (bytes + 3) / 4;
  25647. +
  25648. + volatile uint32_t *fifo = core_if->data_fifo[0];
  25649. + uint32_t *data_buff = (uint32_t *) dest;
  25650. +
  25651. + /**
  25652. + * @todo Account for the case where _dest is not dword aligned. This
  25653. + * requires reading data from the FIFO into a uint32_t temp buffer,
  25654. + * then moving it into the data buffer.
  25655. + */
  25656. +
  25657. + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s(%p,%p,%d)\n", __func__,
  25658. + core_if, dest, bytes);
  25659. +
  25660. + for (i = 0; i < word_count; i++, data_buff++) {
  25661. + *data_buff = DWC_READ_REG32(fifo);
  25662. + }
  25663. +
  25664. + return;
  25665. +}
  25666. +
  25667. +/**
  25668. + * This functions reads the device registers and prints them
  25669. + *
  25670. + * @param core_if Programming view of DWC_otg controller.
  25671. + */
  25672. +void dwc_otg_dump_dev_registers(dwc_otg_core_if_t * core_if)
  25673. +{
  25674. + int i;
  25675. + volatile uint32_t *addr;
  25676. +
  25677. + DWC_PRINTF("Device Global Registers\n");
  25678. + addr = &core_if->dev_if->dev_global_regs->dcfg;
  25679. + DWC_PRINTF("DCFG @0x%08lX : 0x%08X\n",
  25680. + (unsigned long)addr, DWC_READ_REG32(addr));
  25681. + addr = &core_if->dev_if->dev_global_regs->dctl;
  25682. + DWC_PRINTF("DCTL @0x%08lX : 0x%08X\n",
  25683. + (unsigned long)addr, DWC_READ_REG32(addr));
  25684. + addr = &core_if->dev_if->dev_global_regs->dsts;
  25685. + DWC_PRINTF("DSTS @0x%08lX : 0x%08X\n",
  25686. + (unsigned long)addr, DWC_READ_REG32(addr));
  25687. + addr = &core_if->dev_if->dev_global_regs->diepmsk;
  25688. + DWC_PRINTF("DIEPMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  25689. + DWC_READ_REG32(addr));
  25690. + addr = &core_if->dev_if->dev_global_regs->doepmsk;
  25691. + DWC_PRINTF("DOEPMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  25692. + DWC_READ_REG32(addr));
  25693. + addr = &core_if->dev_if->dev_global_regs->daint;
  25694. + DWC_PRINTF("DAINT @0x%08lX : 0x%08X\n", (unsigned long)addr,
  25695. + DWC_READ_REG32(addr));
  25696. + addr = &core_if->dev_if->dev_global_regs->daintmsk;
  25697. + DWC_PRINTF("DAINTMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  25698. + DWC_READ_REG32(addr));
  25699. + addr = &core_if->dev_if->dev_global_regs->dtknqr1;
  25700. + DWC_PRINTF("DTKNQR1 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  25701. + DWC_READ_REG32(addr));
  25702. + if (core_if->hwcfg2.b.dev_token_q_depth > 6) {
  25703. + addr = &core_if->dev_if->dev_global_regs->dtknqr2;
  25704. + DWC_PRINTF("DTKNQR2 @0x%08lX : 0x%08X\n",
  25705. + (unsigned long)addr, DWC_READ_REG32(addr));
  25706. + }
  25707. +
  25708. + addr = &core_if->dev_if->dev_global_regs->dvbusdis;
  25709. + DWC_PRINTF("DVBUSID @0x%08lX : 0x%08X\n", (unsigned long)addr,
  25710. + DWC_READ_REG32(addr));
  25711. +
  25712. + addr = &core_if->dev_if->dev_global_regs->dvbuspulse;
  25713. + DWC_PRINTF("DVBUSPULSE @0x%08lX : 0x%08X\n",
  25714. + (unsigned long)addr, DWC_READ_REG32(addr));
  25715. +
  25716. + addr = &core_if->dev_if->dev_global_regs->dtknqr3_dthrctl;
  25717. + DWC_PRINTF("DTKNQR3_DTHRCTL @0x%08lX : 0x%08X\n",
  25718. + (unsigned long)addr, DWC_READ_REG32(addr));
  25719. +
  25720. + if (core_if->hwcfg2.b.dev_token_q_depth > 22) {
  25721. + addr = &core_if->dev_if->dev_global_regs->dtknqr4_fifoemptymsk;
  25722. + DWC_PRINTF("DTKNQR4 @0x%08lX : 0x%08X\n",
  25723. + (unsigned long)addr, DWC_READ_REG32(addr));
  25724. + }
  25725. +
  25726. + addr = &core_if->dev_if->dev_global_regs->dtknqr4_fifoemptymsk;
  25727. + DWC_PRINTF("FIFOEMPMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  25728. + DWC_READ_REG32(addr));
  25729. +
  25730. + if (core_if->hwcfg2.b.multi_proc_int) {
  25731. +
  25732. + addr = &core_if->dev_if->dev_global_regs->deachint;
  25733. + DWC_PRINTF("DEACHINT @0x%08lX : 0x%08X\n",
  25734. + (unsigned long)addr, DWC_READ_REG32(addr));
  25735. + addr = &core_if->dev_if->dev_global_regs->deachintmsk;
  25736. + DWC_PRINTF("DEACHINTMSK @0x%08lX : 0x%08X\n",
  25737. + (unsigned long)addr, DWC_READ_REG32(addr));
  25738. +
  25739. + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  25740. + addr =
  25741. + &core_if->dev_if->
  25742. + dev_global_regs->diepeachintmsk[i];
  25743. + DWC_PRINTF("DIEPEACHINTMSK[%d] @0x%08lX : 0x%08X\n",
  25744. + i, (unsigned long)addr,
  25745. + DWC_READ_REG32(addr));
  25746. + }
  25747. +
  25748. + for (i = 0; i <= core_if->dev_if->num_out_eps; i++) {
  25749. + addr =
  25750. + &core_if->dev_if->
  25751. + dev_global_regs->doepeachintmsk[i];
  25752. + DWC_PRINTF("DOEPEACHINTMSK[%d] @0x%08lX : 0x%08X\n",
  25753. + i, (unsigned long)addr,
  25754. + DWC_READ_REG32(addr));
  25755. + }
  25756. + }
  25757. +
  25758. + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  25759. + DWC_PRINTF("Device IN EP %d Registers\n", i);
  25760. + addr = &core_if->dev_if->in_ep_regs[i]->diepctl;
  25761. + DWC_PRINTF("DIEPCTL @0x%08lX : 0x%08X\n",
  25762. + (unsigned long)addr, DWC_READ_REG32(addr));
  25763. + addr = &core_if->dev_if->in_ep_regs[i]->diepint;
  25764. + DWC_PRINTF("DIEPINT @0x%08lX : 0x%08X\n",
  25765. + (unsigned long)addr, DWC_READ_REG32(addr));
  25766. + addr = &core_if->dev_if->in_ep_regs[i]->dieptsiz;
  25767. + DWC_PRINTF("DIETSIZ @0x%08lX : 0x%08X\n",
  25768. + (unsigned long)addr, DWC_READ_REG32(addr));
  25769. + addr = &core_if->dev_if->in_ep_regs[i]->diepdma;
  25770. + DWC_PRINTF("DIEPDMA @0x%08lX : 0x%08X\n",
  25771. + (unsigned long)addr, DWC_READ_REG32(addr));
  25772. + addr = &core_if->dev_if->in_ep_regs[i]->dtxfsts;
  25773. + DWC_PRINTF("DTXFSTS @0x%08lX : 0x%08X\n",
  25774. + (unsigned long)addr, DWC_READ_REG32(addr));
  25775. + addr = &core_if->dev_if->in_ep_regs[i]->diepdmab;
  25776. + DWC_PRINTF("DIEPDMAB @0x%08lX : 0x%08X\n",
  25777. + (unsigned long)addr, 0 /*DWC_READ_REG32(addr) */ );
  25778. + }
  25779. +
  25780. + for (i = 0; i <= core_if->dev_if->num_out_eps; i++) {
  25781. + DWC_PRINTF("Device OUT EP %d Registers\n", i);
  25782. + addr = &core_if->dev_if->out_ep_regs[i]->doepctl;
  25783. + DWC_PRINTF("DOEPCTL @0x%08lX : 0x%08X\n",
  25784. + (unsigned long)addr, DWC_READ_REG32(addr));
  25785. + addr = &core_if->dev_if->out_ep_regs[i]->doepint;
  25786. + DWC_PRINTF("DOEPINT @0x%08lX : 0x%08X\n",
  25787. + (unsigned long)addr, DWC_READ_REG32(addr));
  25788. + addr = &core_if->dev_if->out_ep_regs[i]->doeptsiz;
  25789. + DWC_PRINTF("DOETSIZ @0x%08lX : 0x%08X\n",
  25790. + (unsigned long)addr, DWC_READ_REG32(addr));
  25791. + addr = &core_if->dev_if->out_ep_regs[i]->doepdma;
  25792. + DWC_PRINTF("DOEPDMA @0x%08lX : 0x%08X\n",
  25793. + (unsigned long)addr, DWC_READ_REG32(addr));
  25794. + if (core_if->dma_enable) { /* Don't access this register in SLAVE mode */
  25795. + addr = &core_if->dev_if->out_ep_regs[i]->doepdmab;
  25796. + DWC_PRINTF("DOEPDMAB @0x%08lX : 0x%08X\n",
  25797. + (unsigned long)addr, DWC_READ_REG32(addr));
  25798. + }
  25799. +
  25800. + }
  25801. +}
  25802. +
  25803. +/**
  25804. + * This functions reads the SPRAM and prints its content
  25805. + *
  25806. + * @param core_if Programming view of DWC_otg controller.
  25807. + */
  25808. +void dwc_otg_dump_spram(dwc_otg_core_if_t * core_if)
  25809. +{
  25810. + volatile uint8_t *addr, *start_addr, *end_addr;
  25811. +
  25812. + DWC_PRINTF("SPRAM Data:\n");
  25813. + start_addr = (void *)core_if->core_global_regs;
  25814. + DWC_PRINTF("Base Address: 0x%8lX\n", (unsigned long)start_addr);
  25815. + start_addr += 0x00028000;
  25816. + end_addr = (void *)core_if->core_global_regs;
  25817. + end_addr += 0x000280e0;
  25818. +
  25819. + for (addr = start_addr; addr < end_addr; addr += 16) {
  25820. + DWC_PRINTF
  25821. + ("0x%8lX:\t%2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X\n",
  25822. + (unsigned long)addr, addr[0], addr[1], addr[2], addr[3],
  25823. + addr[4], addr[5], addr[6], addr[7], addr[8], addr[9],
  25824. + addr[10], addr[11], addr[12], addr[13], addr[14], addr[15]
  25825. + );
  25826. + }
  25827. +
  25828. + return;
  25829. +}
  25830. +
  25831. +/**
  25832. + * This function reads the host registers and prints them
  25833. + *
  25834. + * @param core_if Programming view of DWC_otg controller.
  25835. + */
  25836. +void dwc_otg_dump_host_registers(dwc_otg_core_if_t * core_if)
  25837. +{
  25838. + int i;
  25839. + volatile uint32_t *addr;
  25840. +
  25841. + DWC_PRINTF("Host Global Registers\n");
  25842. + addr = &core_if->host_if->host_global_regs->hcfg;
  25843. + DWC_PRINTF("HCFG @0x%08lX : 0x%08X\n",
  25844. + (unsigned long)addr, DWC_READ_REG32(addr));
  25845. + addr = &core_if->host_if->host_global_regs->hfir;
  25846. + DWC_PRINTF("HFIR @0x%08lX : 0x%08X\n",
  25847. + (unsigned long)addr, DWC_READ_REG32(addr));
  25848. + addr = &core_if->host_if->host_global_regs->hfnum;
  25849. + DWC_PRINTF("HFNUM @0x%08lX : 0x%08X\n", (unsigned long)addr,
  25850. + DWC_READ_REG32(addr));
  25851. + addr = &core_if->host_if->host_global_regs->hptxsts;
  25852. + DWC_PRINTF("HPTXSTS @0x%08lX : 0x%08X\n", (unsigned long)addr,
  25853. + DWC_READ_REG32(addr));
  25854. + addr = &core_if->host_if->host_global_regs->haint;
  25855. + DWC_PRINTF("HAINT @0x%08lX : 0x%08X\n", (unsigned long)addr,
  25856. + DWC_READ_REG32(addr));
  25857. + addr = &core_if->host_if->host_global_regs->haintmsk;
  25858. + DWC_PRINTF("HAINTMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  25859. + DWC_READ_REG32(addr));
  25860. + if (core_if->dma_desc_enable) {
  25861. + addr = &core_if->host_if->host_global_regs->hflbaddr;
  25862. + DWC_PRINTF("HFLBADDR @0x%08lX : 0x%08X\n",
  25863. + (unsigned long)addr, DWC_READ_REG32(addr));
  25864. + }
  25865. +
  25866. + addr = core_if->host_if->hprt0;
  25867. + DWC_PRINTF("HPRT0 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  25868. + DWC_READ_REG32(addr));
  25869. +
  25870. + for (i = 0; i < core_if->core_params->host_channels; i++) {
  25871. + DWC_PRINTF("Host Channel %d Specific Registers\n", i);
  25872. + addr = &core_if->host_if->hc_regs[i]->hcchar;
  25873. + DWC_PRINTF("HCCHAR @0x%08lX : 0x%08X\n",
  25874. + (unsigned long)addr, DWC_READ_REG32(addr));
  25875. + addr = &core_if->host_if->hc_regs[i]->hcsplt;
  25876. + DWC_PRINTF("HCSPLT @0x%08lX : 0x%08X\n",
  25877. + (unsigned long)addr, DWC_READ_REG32(addr));
  25878. + addr = &core_if->host_if->hc_regs[i]->hcint;
  25879. + DWC_PRINTF("HCINT @0x%08lX : 0x%08X\n",
  25880. + (unsigned long)addr, DWC_READ_REG32(addr));
  25881. + addr = &core_if->host_if->hc_regs[i]->hcintmsk;
  25882. + DWC_PRINTF("HCINTMSK @0x%08lX : 0x%08X\n",
  25883. + (unsigned long)addr, DWC_READ_REG32(addr));
  25884. + addr = &core_if->host_if->hc_regs[i]->hctsiz;
  25885. + DWC_PRINTF("HCTSIZ @0x%08lX : 0x%08X\n",
  25886. + (unsigned long)addr, DWC_READ_REG32(addr));
  25887. + addr = &core_if->host_if->hc_regs[i]->hcdma;
  25888. + DWC_PRINTF("HCDMA @0x%08lX : 0x%08X\n",
  25889. + (unsigned long)addr, DWC_READ_REG32(addr));
  25890. + if (core_if->dma_desc_enable) {
  25891. + addr = &core_if->host_if->hc_regs[i]->hcdmab;
  25892. + DWC_PRINTF("HCDMAB @0x%08lX : 0x%08X\n",
  25893. + (unsigned long)addr, DWC_READ_REG32(addr));
  25894. + }
  25895. +
  25896. + }
  25897. + return;
  25898. +}
  25899. +
  25900. +/**
  25901. + * This function reads the core global registers and prints them
  25902. + *
  25903. + * @param core_if Programming view of DWC_otg controller.
  25904. + */
  25905. +void dwc_otg_dump_global_registers(dwc_otg_core_if_t * core_if)
  25906. +{
  25907. + int i, ep_num;
  25908. + volatile uint32_t *addr;
  25909. + char *txfsiz;
  25910. +
  25911. + DWC_PRINTF("Core Global Registers\n");
  25912. + addr = &core_if->core_global_regs->gotgctl;
  25913. + DWC_PRINTF("GOTGCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  25914. + DWC_READ_REG32(addr));
  25915. + addr = &core_if->core_global_regs->gotgint;
  25916. + DWC_PRINTF("GOTGINT @0x%08lX : 0x%08X\n", (unsigned long)addr,
  25917. + DWC_READ_REG32(addr));
  25918. + addr = &core_if->core_global_regs->gahbcfg;
  25919. + DWC_PRINTF("GAHBCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
  25920. + DWC_READ_REG32(addr));
  25921. + addr = &core_if->core_global_regs->gusbcfg;
  25922. + DWC_PRINTF("GUSBCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
  25923. + DWC_READ_REG32(addr));
  25924. + addr = &core_if->core_global_regs->grstctl;
  25925. + DWC_PRINTF("GRSTCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  25926. + DWC_READ_REG32(addr));
  25927. + addr = &core_if->core_global_regs->gintsts;
  25928. + DWC_PRINTF("GINTSTS @0x%08lX : 0x%08X\n", (unsigned long)addr,
  25929. + DWC_READ_REG32(addr));
  25930. + addr = &core_if->core_global_regs->gintmsk;
  25931. + DWC_PRINTF("GINTMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  25932. + DWC_READ_REG32(addr));
  25933. + addr = &core_if->core_global_regs->grxstsr;
  25934. + DWC_PRINTF("GRXSTSR @0x%08lX : 0x%08X\n", (unsigned long)addr,
  25935. + DWC_READ_REG32(addr));
  25936. + addr = &core_if->core_global_regs->grxfsiz;
  25937. + DWC_PRINTF("GRXFSIZ @0x%08lX : 0x%08X\n", (unsigned long)addr,
  25938. + DWC_READ_REG32(addr));
  25939. + addr = &core_if->core_global_regs->gnptxfsiz;
  25940. + DWC_PRINTF("GNPTXFSIZ @0x%08lX : 0x%08X\n", (unsigned long)addr,
  25941. + DWC_READ_REG32(addr));
  25942. + addr = &core_if->core_global_regs->gnptxsts;
  25943. + DWC_PRINTF("GNPTXSTS @0x%08lX : 0x%08X\n", (unsigned long)addr,
  25944. + DWC_READ_REG32(addr));
  25945. + addr = &core_if->core_global_regs->gi2cctl;
  25946. + DWC_PRINTF("GI2CCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  25947. + DWC_READ_REG32(addr));
  25948. + addr = &core_if->core_global_regs->gpvndctl;
  25949. + DWC_PRINTF("GPVNDCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  25950. + DWC_READ_REG32(addr));
  25951. + addr = &core_if->core_global_regs->ggpio;
  25952. + DWC_PRINTF("GGPIO @0x%08lX : 0x%08X\n", (unsigned long)addr,
  25953. + DWC_READ_REG32(addr));
  25954. + addr = &core_if->core_global_regs->guid;
  25955. + DWC_PRINTF("GUID @0x%08lX : 0x%08X\n",
  25956. + (unsigned long)addr, DWC_READ_REG32(addr));
  25957. + addr = &core_if->core_global_regs->gsnpsid;
  25958. + DWC_PRINTF("GSNPSID @0x%08lX : 0x%08X\n", (unsigned long)addr,
  25959. + DWC_READ_REG32(addr));
  25960. + addr = &core_if->core_global_regs->ghwcfg1;
  25961. + DWC_PRINTF("GHWCFG1 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  25962. + DWC_READ_REG32(addr));
  25963. + addr = &core_if->core_global_regs->ghwcfg2;
  25964. + DWC_PRINTF("GHWCFG2 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  25965. + DWC_READ_REG32(addr));
  25966. + addr = &core_if->core_global_regs->ghwcfg3;
  25967. + DWC_PRINTF("GHWCFG3 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  25968. + DWC_READ_REG32(addr));
  25969. + addr = &core_if->core_global_regs->ghwcfg4;
  25970. + DWC_PRINTF("GHWCFG4 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  25971. + DWC_READ_REG32(addr));
  25972. + addr = &core_if->core_global_regs->glpmcfg;
  25973. + DWC_PRINTF("GLPMCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
  25974. + DWC_READ_REG32(addr));
  25975. + addr = &core_if->core_global_regs->gpwrdn;
  25976. + DWC_PRINTF("GPWRDN @0x%08lX : 0x%08X\n", (unsigned long)addr,
  25977. + DWC_READ_REG32(addr));
  25978. + addr = &core_if->core_global_regs->gdfifocfg;
  25979. + DWC_PRINTF("GDFIFOCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
  25980. + DWC_READ_REG32(addr));
  25981. + addr = &core_if->core_global_regs->adpctl;
  25982. + DWC_PRINTF("ADPCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  25983. + dwc_otg_adp_read_reg(core_if));
  25984. + addr = &core_if->core_global_regs->hptxfsiz;
  25985. + DWC_PRINTF("HPTXFSIZ @0x%08lX : 0x%08X\n", (unsigned long)addr,
  25986. + DWC_READ_REG32(addr));
  25987. +
  25988. + if (core_if->en_multiple_tx_fifo == 0) {
  25989. + ep_num = core_if->hwcfg4.b.num_dev_perio_in_ep;
  25990. + txfsiz = "DPTXFSIZ";
  25991. + } else {
  25992. + ep_num = core_if->hwcfg4.b.num_in_eps;
  25993. + txfsiz = "DIENPTXF";
  25994. + }
  25995. + for (i = 0; i < ep_num; i++) {
  25996. + addr = &core_if->core_global_regs->dtxfsiz[i];
  25997. + DWC_PRINTF("%s[%d] @0x%08lX : 0x%08X\n", txfsiz, i + 1,
  25998. + (unsigned long)addr, DWC_READ_REG32(addr));
  25999. + }
  26000. + addr = core_if->pcgcctl;
  26001. + DWC_PRINTF("PCGCCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26002. + DWC_READ_REG32(addr));
  26003. +}
  26004. +
  26005. +/**
  26006. + * Flush a Tx FIFO.
  26007. + *
  26008. + * @param core_if Programming view of DWC_otg controller.
  26009. + * @param num Tx FIFO to flush.
  26010. + */
  26011. +void dwc_otg_flush_tx_fifo(dwc_otg_core_if_t * core_if, const int num)
  26012. +{
  26013. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  26014. + volatile grstctl_t greset = {.d32 = 0 };
  26015. + int count = 0;
  26016. +
  26017. + DWC_DEBUGPL((DBG_CIL | DBG_PCDV), "Flush Tx FIFO %d\n", num);
  26018. +
  26019. + greset.b.txfflsh = 1;
  26020. + greset.b.txfnum = num;
  26021. + DWC_WRITE_REG32(&global_regs->grstctl, greset.d32);
  26022. +
  26023. + do {
  26024. + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
  26025. + if (++count > 10000) {
  26026. + DWC_WARN("%s() HANG! GRSTCTL=%0x GNPTXSTS=0x%08x\n",
  26027. + __func__, greset.d32,
  26028. + DWC_READ_REG32(&global_regs->gnptxsts));
  26029. + break;
  26030. + }
  26031. + dwc_udelay(1);
  26032. + } while (greset.b.txfflsh == 1);
  26033. +
  26034. + /* Wait for 3 PHY Clocks */
  26035. + dwc_udelay(1);
  26036. +}
  26037. +
  26038. +/**
  26039. + * Flush Rx FIFO.
  26040. + *
  26041. + * @param core_if Programming view of DWC_otg controller.
  26042. + */
  26043. +void dwc_otg_flush_rx_fifo(dwc_otg_core_if_t * core_if)
  26044. +{
  26045. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  26046. + volatile grstctl_t greset = {.d32 = 0 };
  26047. + int count = 0;
  26048. +
  26049. + DWC_DEBUGPL((DBG_CIL | DBG_PCDV), "%s\n", __func__);
  26050. + /*
  26051. + *
  26052. + */
  26053. + greset.b.rxfflsh = 1;
  26054. + DWC_WRITE_REG32(&global_regs->grstctl, greset.d32);
  26055. +
  26056. + do {
  26057. + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
  26058. + if (++count > 10000) {
  26059. + DWC_WARN("%s() HANG! GRSTCTL=%0x\n", __func__,
  26060. + greset.d32);
  26061. + break;
  26062. + }
  26063. + dwc_udelay(1);
  26064. + } while (greset.b.rxfflsh == 1);
  26065. +
  26066. + /* Wait for 3 PHY Clocks */
  26067. + dwc_udelay(1);
  26068. +}
  26069. +
  26070. +/**
  26071. + * Do core a soft reset of the core. Be careful with this because it
  26072. + * resets all the internal state machines of the core.
  26073. + */
  26074. +void dwc_otg_core_reset(dwc_otg_core_if_t * core_if)
  26075. +{
  26076. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  26077. + volatile grstctl_t greset = {.d32 = 0 };
  26078. + int count = 0;
  26079. +
  26080. + DWC_DEBUGPL(DBG_CILV, "%s\n", __func__);
  26081. + /* Wait for AHB master IDLE state. */
  26082. + do {
  26083. + dwc_udelay(10);
  26084. + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
  26085. + if (++count > 100000) {
  26086. + DWC_WARN("%s() HANG! AHB Idle GRSTCTL=%0x\n", __func__,
  26087. + greset.d32);
  26088. + return;
  26089. + }
  26090. + }
  26091. + while (greset.b.ahbidle == 0);
  26092. +
  26093. + /* Core Soft Reset */
  26094. + count = 0;
  26095. + greset.b.csftrst = 1;
  26096. + DWC_WRITE_REG32(&global_regs->grstctl, greset.d32);
  26097. + do {
  26098. + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
  26099. + if (++count > 10000) {
  26100. + DWC_WARN("%s() HANG! Soft Reset GRSTCTL=%0x\n",
  26101. + __func__, greset.d32);
  26102. + break;
  26103. + }
  26104. + dwc_udelay(1);
  26105. + }
  26106. + while (greset.b.csftrst == 1);
  26107. +
  26108. + /* Wait for 3 PHY Clocks */
  26109. + dwc_mdelay(100);
  26110. +}
  26111. +
  26112. +uint8_t dwc_otg_is_device_mode(dwc_otg_core_if_t * _core_if)
  26113. +{
  26114. + return (dwc_otg_mode(_core_if) != DWC_HOST_MODE);
  26115. +}
  26116. +
  26117. +uint8_t dwc_otg_is_host_mode(dwc_otg_core_if_t * _core_if)
  26118. +{
  26119. + return (dwc_otg_mode(_core_if) == DWC_HOST_MODE);
  26120. +}
  26121. +
  26122. +/**
  26123. + * Register HCD callbacks. The callbacks are used to start and stop
  26124. + * the HCD for interrupt processing.
  26125. + *
  26126. + * @param core_if Programming view of DWC_otg controller.
  26127. + * @param cb the HCD callback structure.
  26128. + * @param p pointer to be passed to callback function (usb_hcd*).
  26129. + */
  26130. +void dwc_otg_cil_register_hcd_callbacks(dwc_otg_core_if_t * core_if,
  26131. + dwc_otg_cil_callbacks_t * cb, void *p)
  26132. +{
  26133. + core_if->hcd_cb = cb;
  26134. + cb->p = p;
  26135. +}
  26136. +
  26137. +/**
  26138. + * Register PCD callbacks. The callbacks are used to start and stop
  26139. + * the PCD for interrupt processing.
  26140. + *
  26141. + * @param core_if Programming view of DWC_otg controller.
  26142. + * @param cb the PCD callback structure.
  26143. + * @param p pointer to be passed to callback function (pcd*).
  26144. + */
  26145. +void dwc_otg_cil_register_pcd_callbacks(dwc_otg_core_if_t * core_if,
  26146. + dwc_otg_cil_callbacks_t * cb, void *p)
  26147. +{
  26148. + core_if->pcd_cb = cb;
  26149. + cb->p = p;
  26150. +}
  26151. +
  26152. +#ifdef DWC_EN_ISOC
  26153. +
  26154. +/**
  26155. + * This function writes isoc data per 1 (micro)frame into tx fifo
  26156. + *
  26157. + * @param core_if Programming view of DWC_otg controller.
  26158. + * @param ep The EP to start the transfer on.
  26159. + *
  26160. + */
  26161. +void write_isoc_frame_data(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  26162. +{
  26163. + dwc_otg_dev_in_ep_regs_t *ep_regs;
  26164. + dtxfsts_data_t txstatus = {.d32 = 0 };
  26165. + uint32_t len = 0;
  26166. + uint32_t dwords;
  26167. +
  26168. + ep->xfer_len = ep->data_per_frame;
  26169. + ep->xfer_count = 0;
  26170. +
  26171. + ep_regs = core_if->dev_if->in_ep_regs[ep->num];
  26172. +
  26173. + len = ep->xfer_len - ep->xfer_count;
  26174. +
  26175. + if (len > ep->maxpacket) {
  26176. + len = ep->maxpacket;
  26177. + }
  26178. +
  26179. + dwords = (len + 3) / 4;
  26180. +
  26181. + /* While there is space in the queue and space in the FIFO and
  26182. + * More data to tranfer, Write packets to the Tx FIFO */
  26183. + txstatus.d32 =
  26184. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[ep->num]->dtxfsts);
  26185. + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", ep->num, txstatus.d32);
  26186. +
  26187. + while (txstatus.b.txfspcavail > dwords &&
  26188. + ep->xfer_count < ep->xfer_len && ep->xfer_len != 0) {
  26189. + /* Write the FIFO */
  26190. + dwc_otg_ep_write_packet(core_if, ep, 0);
  26191. +
  26192. + len = ep->xfer_len - ep->xfer_count;
  26193. + if (len > ep->maxpacket) {
  26194. + len = ep->maxpacket;
  26195. + }
  26196. +
  26197. + dwords = (len + 3) / 4;
  26198. + txstatus.d32 =
  26199. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  26200. + dtxfsts);
  26201. + DWC_DEBUGPL(DBG_PCDV, "dtxfsts[%d]=0x%08x\n", ep->num,
  26202. + txstatus.d32);
  26203. + }
  26204. +}
  26205. +
  26206. +/**
  26207. + * This function initializes a descriptor chain for Isochronous transfer
  26208. + *
  26209. + * @param core_if Programming view of DWC_otg controller.
  26210. + * @param ep The EP to start the transfer on.
  26211. + *
  26212. + */
  26213. +void dwc_otg_iso_ep_start_frm_transfer(dwc_otg_core_if_t * core_if,
  26214. + dwc_ep_t * ep)
  26215. +{
  26216. + deptsiz_data_t deptsiz = {.d32 = 0 };
  26217. + depctl_data_t depctl = {.d32 = 0 };
  26218. + dsts_data_t dsts = {.d32 = 0 };
  26219. + volatile uint32_t *addr;
  26220. +
  26221. + if (ep->is_in) {
  26222. + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
  26223. + } else {
  26224. + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
  26225. + }
  26226. +
  26227. + ep->xfer_len = ep->data_per_frame;
  26228. + ep->xfer_count = 0;
  26229. + ep->xfer_buff = ep->cur_pkt_addr;
  26230. + ep->dma_addr = ep->cur_pkt_dma_addr;
  26231. +
  26232. + if (ep->is_in) {
  26233. + /* Program the transfer size and packet count
  26234. + * as follows: xfersize = N * maxpacket +
  26235. + * short_packet pktcnt = N + (short_packet
  26236. + * exist ? 1 : 0)
  26237. + */
  26238. + deptsiz.b.xfersize = ep->xfer_len;
  26239. + deptsiz.b.pktcnt =
  26240. + (ep->xfer_len - 1 + ep->maxpacket) / ep->maxpacket;
  26241. + deptsiz.b.mc = deptsiz.b.pktcnt;
  26242. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->dieptsiz,
  26243. + deptsiz.d32);
  26244. +
  26245. + /* Write the DMA register */
  26246. + if (core_if->dma_enable) {
  26247. + DWC_WRITE_REG32(&
  26248. + (core_if->dev_if->in_ep_regs[ep->num]->
  26249. + diepdma), (uint32_t) ep->dma_addr);
  26250. + }
  26251. + } else {
  26252. + deptsiz.b.pktcnt =
  26253. + (ep->xfer_len + (ep->maxpacket - 1)) / ep->maxpacket;
  26254. + deptsiz.b.xfersize = deptsiz.b.pktcnt * ep->maxpacket;
  26255. +
  26256. + DWC_WRITE_REG32(&core_if->dev_if->
  26257. + out_ep_regs[ep->num]->doeptsiz, deptsiz.d32);
  26258. +
  26259. + if (core_if->dma_enable) {
  26260. + DWC_WRITE_REG32(&
  26261. + (core_if->dev_if->
  26262. + out_ep_regs[ep->num]->doepdma),
  26263. + (uint32_t) ep->dma_addr);
  26264. + }
  26265. + }
  26266. +
  26267. + /** Enable endpoint, clear nak */
  26268. +
  26269. + depctl.d32 = 0;
  26270. + if (ep->bInterval == 1) {
  26271. + dsts.d32 =
  26272. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  26273. + ep->next_frame = dsts.b.soffn + ep->bInterval;
  26274. +
  26275. + if (ep->next_frame & 0x1) {
  26276. + depctl.b.setd1pid = 1;
  26277. + } else {
  26278. + depctl.b.setd0pid = 1;
  26279. + }
  26280. + } else {
  26281. + ep->next_frame += ep->bInterval;
  26282. +
  26283. + if (ep->next_frame & 0x1) {
  26284. + depctl.b.setd1pid = 1;
  26285. + } else {
  26286. + depctl.b.setd0pid = 1;
  26287. + }
  26288. + }
  26289. + depctl.b.epena = 1;
  26290. + depctl.b.cnak = 1;
  26291. +
  26292. + DWC_MODIFY_REG32(addr, 0, depctl.d32);
  26293. + depctl.d32 = DWC_READ_REG32(addr);
  26294. +
  26295. + if (ep->is_in && core_if->dma_enable == 0) {
  26296. + write_isoc_frame_data(core_if, ep);
  26297. + }
  26298. +
  26299. +}
  26300. +#endif /* DWC_EN_ISOC */
  26301. +
  26302. +static void dwc_otg_set_uninitialized(int32_t * p, int size)
  26303. +{
  26304. + int i;
  26305. + for (i = 0; i < size; i++) {
  26306. + p[i] = -1;
  26307. + }
  26308. +}
  26309. +
  26310. +static int dwc_otg_param_initialized(int32_t val)
  26311. +{
  26312. + return val != -1;
  26313. +}
  26314. +
  26315. +static int dwc_otg_setup_params(dwc_otg_core_if_t * core_if)
  26316. +{
  26317. + int i;
  26318. + core_if->core_params = DWC_ALLOC(sizeof(*core_if->core_params));
  26319. + if (!core_if->core_params) {
  26320. + return -DWC_E_NO_MEMORY;
  26321. + }
  26322. + dwc_otg_set_uninitialized((int32_t *) core_if->core_params,
  26323. + sizeof(*core_if->core_params) /
  26324. + sizeof(int32_t));
  26325. + DWC_PRINTF("Setting default values for core params\n");
  26326. + dwc_otg_set_param_otg_cap(core_if, dwc_param_otg_cap_default);
  26327. + dwc_otg_set_param_dma_enable(core_if, dwc_param_dma_enable_default);
  26328. + dwc_otg_set_param_dma_desc_enable(core_if,
  26329. + dwc_param_dma_desc_enable_default);
  26330. + dwc_otg_set_param_opt(core_if, dwc_param_opt_default);
  26331. + dwc_otg_set_param_dma_burst_size(core_if,
  26332. + dwc_param_dma_burst_size_default);
  26333. + dwc_otg_set_param_host_support_fs_ls_low_power(core_if,
  26334. + dwc_param_host_support_fs_ls_low_power_default);
  26335. + dwc_otg_set_param_enable_dynamic_fifo(core_if,
  26336. + dwc_param_enable_dynamic_fifo_default);
  26337. + dwc_otg_set_param_data_fifo_size(core_if,
  26338. + dwc_param_data_fifo_size_default);
  26339. + dwc_otg_set_param_dev_rx_fifo_size(core_if,
  26340. + dwc_param_dev_rx_fifo_size_default);
  26341. + dwc_otg_set_param_dev_nperio_tx_fifo_size(core_if,
  26342. + dwc_param_dev_nperio_tx_fifo_size_default);
  26343. + dwc_otg_set_param_host_rx_fifo_size(core_if,
  26344. + dwc_param_host_rx_fifo_size_default);
  26345. + dwc_otg_set_param_host_nperio_tx_fifo_size(core_if,
  26346. + dwc_param_host_nperio_tx_fifo_size_default);
  26347. + dwc_otg_set_param_host_perio_tx_fifo_size(core_if,
  26348. + dwc_param_host_perio_tx_fifo_size_default);
  26349. + dwc_otg_set_param_max_transfer_size(core_if,
  26350. + dwc_param_max_transfer_size_default);
  26351. + dwc_otg_set_param_max_packet_count(core_if,
  26352. + dwc_param_max_packet_count_default);
  26353. + dwc_otg_set_param_host_channels(core_if,
  26354. + dwc_param_host_channels_default);
  26355. + dwc_otg_set_param_dev_endpoints(core_if,
  26356. + dwc_param_dev_endpoints_default);
  26357. + dwc_otg_set_param_phy_type(core_if, dwc_param_phy_type_default);
  26358. + dwc_otg_set_param_speed(core_if, dwc_param_speed_default);
  26359. + dwc_otg_set_param_host_ls_low_power_phy_clk(core_if,
  26360. + dwc_param_host_ls_low_power_phy_clk_default);
  26361. + dwc_otg_set_param_phy_ulpi_ddr(core_if, dwc_param_phy_ulpi_ddr_default);
  26362. + dwc_otg_set_param_phy_ulpi_ext_vbus(core_if,
  26363. + dwc_param_phy_ulpi_ext_vbus_default);
  26364. + dwc_otg_set_param_phy_utmi_width(core_if,
  26365. + dwc_param_phy_utmi_width_default);
  26366. + dwc_otg_set_param_ts_dline(core_if, dwc_param_ts_dline_default);
  26367. + dwc_otg_set_param_i2c_enable(core_if, dwc_param_i2c_enable_default);
  26368. + dwc_otg_set_param_ulpi_fs_ls(core_if, dwc_param_ulpi_fs_ls_default);
  26369. + dwc_otg_set_param_en_multiple_tx_fifo(core_if,
  26370. + dwc_param_en_multiple_tx_fifo_default);
  26371. + for (i = 0; i < 15; i++) {
  26372. + dwc_otg_set_param_dev_perio_tx_fifo_size(core_if,
  26373. + dwc_param_dev_perio_tx_fifo_size_default,
  26374. + i);
  26375. + }
  26376. +
  26377. + for (i = 0; i < 15; i++) {
  26378. + dwc_otg_set_param_dev_tx_fifo_size(core_if,
  26379. + dwc_param_dev_tx_fifo_size_default,
  26380. + i);
  26381. + }
  26382. + dwc_otg_set_param_thr_ctl(core_if, dwc_param_thr_ctl_default);
  26383. + dwc_otg_set_param_mpi_enable(core_if, dwc_param_mpi_enable_default);
  26384. + dwc_otg_set_param_pti_enable(core_if, dwc_param_pti_enable_default);
  26385. + dwc_otg_set_param_lpm_enable(core_if, dwc_param_lpm_enable_default);
  26386. + dwc_otg_set_param_ic_usb_cap(core_if, dwc_param_ic_usb_cap_default);
  26387. + dwc_otg_set_param_tx_thr_length(core_if,
  26388. + dwc_param_tx_thr_length_default);
  26389. + dwc_otg_set_param_rx_thr_length(core_if,
  26390. + dwc_param_rx_thr_length_default);
  26391. + dwc_otg_set_param_ahb_thr_ratio(core_if,
  26392. + dwc_param_ahb_thr_ratio_default);
  26393. + dwc_otg_set_param_power_down(core_if, dwc_param_power_down_default);
  26394. + dwc_otg_set_param_reload_ctl(core_if, dwc_param_reload_ctl_default);
  26395. + dwc_otg_set_param_dev_out_nak(core_if, dwc_param_dev_out_nak_default);
  26396. + dwc_otg_set_param_cont_on_bna(core_if, dwc_param_cont_on_bna_default);
  26397. + dwc_otg_set_param_ahb_single(core_if, dwc_param_ahb_single_default);
  26398. + dwc_otg_set_param_otg_ver(core_if, dwc_param_otg_ver_default);
  26399. + dwc_otg_set_param_adp_enable(core_if, dwc_param_adp_enable_default);
  26400. + DWC_PRINTF("Finished setting default values for core params\n");
  26401. +
  26402. + return 0;
  26403. +}
  26404. +
  26405. +uint8_t dwc_otg_is_dma_enable(dwc_otg_core_if_t * core_if)
  26406. +{
  26407. + return core_if->dma_enable;
  26408. +}
  26409. +
  26410. +/* Checks if the parameter is outside of its valid range of values */
  26411. +#define DWC_OTG_PARAM_TEST(_param_, _low_, _high_) \
  26412. + (((_param_) < (_low_)) || \
  26413. + ((_param_) > (_high_)))
  26414. +
  26415. +/* Parameter access functions */
  26416. +int dwc_otg_set_param_otg_cap(dwc_otg_core_if_t * core_if, int32_t val)
  26417. +{
  26418. + int valid;
  26419. + int retval = 0;
  26420. + if (DWC_OTG_PARAM_TEST(val, 0, 2)) {
  26421. + DWC_WARN("Wrong value for otg_cap parameter\n");
  26422. + DWC_WARN("otg_cap parameter must be 0,1 or 2\n");
  26423. + retval = -DWC_E_INVALID;
  26424. + goto out;
  26425. + }
  26426. +
  26427. + valid = 1;
  26428. + switch (val) {
  26429. + case DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE:
  26430. + if (core_if->hwcfg2.b.op_mode !=
  26431. + DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
  26432. + valid = 0;
  26433. + break;
  26434. + case DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE:
  26435. + if ((core_if->hwcfg2.b.op_mode !=
  26436. + DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
  26437. + && (core_if->hwcfg2.b.op_mode !=
  26438. + DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG)
  26439. + && (core_if->hwcfg2.b.op_mode !=
  26440. + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_DEVICE)
  26441. + && (core_if->hwcfg2.b.op_mode !=
  26442. + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST)) {
  26443. + valid = 0;
  26444. + }
  26445. + break;
  26446. + case DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE:
  26447. + /* always valid */
  26448. + break;
  26449. + }
  26450. + if (!valid) {
  26451. + if (dwc_otg_param_initialized(core_if->core_params->otg_cap)) {
  26452. + DWC_ERROR
  26453. + ("%d invalid for otg_cap paremter. Check HW configuration.\n",
  26454. + val);
  26455. + }
  26456. + val =
  26457. + (((core_if->hwcfg2.b.op_mode ==
  26458. + DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
  26459. + || (core_if->hwcfg2.b.op_mode ==
  26460. + DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG)
  26461. + || (core_if->hwcfg2.b.op_mode ==
  26462. + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_DEVICE)
  26463. + || (core_if->hwcfg2.b.op_mode ==
  26464. + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST)) ?
  26465. + DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE :
  26466. + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
  26467. + retval = -DWC_E_INVALID;
  26468. + }
  26469. +
  26470. + core_if->core_params->otg_cap = val;
  26471. +out:
  26472. + return retval;
  26473. +}
  26474. +
  26475. +int32_t dwc_otg_get_param_otg_cap(dwc_otg_core_if_t * core_if)
  26476. +{
  26477. + return core_if->core_params->otg_cap;
  26478. +}
  26479. +
  26480. +int dwc_otg_set_param_opt(dwc_otg_core_if_t * core_if, int32_t val)
  26481. +{
  26482. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  26483. + DWC_WARN("Wrong value for opt parameter\n");
  26484. + return -DWC_E_INVALID;
  26485. + }
  26486. + core_if->core_params->opt = val;
  26487. + return 0;
  26488. +}
  26489. +
  26490. +int32_t dwc_otg_get_param_opt(dwc_otg_core_if_t * core_if)
  26491. +{
  26492. + return core_if->core_params->opt;
  26493. +}
  26494. +
  26495. +int dwc_otg_set_param_dma_enable(dwc_otg_core_if_t * core_if, int32_t val)
  26496. +{
  26497. + int retval = 0;
  26498. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  26499. + DWC_WARN("Wrong value for dma enable\n");
  26500. + return -DWC_E_INVALID;
  26501. + }
  26502. +
  26503. + if ((val == 1) && (core_if->hwcfg2.b.architecture == 0)) {
  26504. + if (dwc_otg_param_initialized(core_if->core_params->dma_enable)) {
  26505. + DWC_ERROR
  26506. + ("%d invalid for dma_enable paremter. Check HW configuration.\n",
  26507. + val);
  26508. + }
  26509. + val = 0;
  26510. + retval = -DWC_E_INVALID;
  26511. + }
  26512. +
  26513. + core_if->core_params->dma_enable = val;
  26514. + if (val == 0) {
  26515. + dwc_otg_set_param_dma_desc_enable(core_if, 0);
  26516. + }
  26517. + return retval;
  26518. +}
  26519. +
  26520. +int32_t dwc_otg_get_param_dma_enable(dwc_otg_core_if_t * core_if)
  26521. +{
  26522. + return core_if->core_params->dma_enable;
  26523. +}
  26524. +
  26525. +int dwc_otg_set_param_dma_desc_enable(dwc_otg_core_if_t * core_if, int32_t val)
  26526. +{
  26527. + int retval = 0;
  26528. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  26529. + DWC_WARN("Wrong value for dma_enable\n");
  26530. + DWC_WARN("dma_desc_enable must be 0 or 1\n");
  26531. + return -DWC_E_INVALID;
  26532. + }
  26533. +
  26534. + if ((val == 1)
  26535. + && ((dwc_otg_get_param_dma_enable(core_if) == 0)
  26536. + || (core_if->hwcfg4.b.desc_dma == 0))) {
  26537. + if (dwc_otg_param_initialized
  26538. + (core_if->core_params->dma_desc_enable)) {
  26539. + DWC_ERROR
  26540. + ("%d invalid for dma_desc_enable paremter. Check HW configuration.\n",
  26541. + val);
  26542. + }
  26543. + val = 0;
  26544. + retval = -DWC_E_INVALID;
  26545. + }
  26546. + core_if->core_params->dma_desc_enable = val;
  26547. + return retval;
  26548. +}
  26549. +
  26550. +int32_t dwc_otg_get_param_dma_desc_enable(dwc_otg_core_if_t * core_if)
  26551. +{
  26552. + return core_if->core_params->dma_desc_enable;
  26553. +}
  26554. +
  26555. +int dwc_otg_set_param_host_support_fs_ls_low_power(dwc_otg_core_if_t * core_if,
  26556. + int32_t val)
  26557. +{
  26558. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  26559. + DWC_WARN("Wrong value for host_support_fs_low_power\n");
  26560. + DWC_WARN("host_support_fs_low_power must be 0 or 1\n");
  26561. + return -DWC_E_INVALID;
  26562. + }
  26563. + core_if->core_params->host_support_fs_ls_low_power = val;
  26564. + return 0;
  26565. +}
  26566. +
  26567. +int32_t dwc_otg_get_param_host_support_fs_ls_low_power(dwc_otg_core_if_t *
  26568. + core_if)
  26569. +{
  26570. + return core_if->core_params->host_support_fs_ls_low_power;
  26571. +}
  26572. +
  26573. +int dwc_otg_set_param_enable_dynamic_fifo(dwc_otg_core_if_t * core_if,
  26574. + int32_t val)
  26575. +{
  26576. + int retval = 0;
  26577. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  26578. + DWC_WARN("Wrong value for enable_dynamic_fifo\n");
  26579. + DWC_WARN("enable_dynamic_fifo must be 0 or 1\n");
  26580. + return -DWC_E_INVALID;
  26581. + }
  26582. +
  26583. + if ((val == 1) && (core_if->hwcfg2.b.dynamic_fifo == 0)) {
  26584. + if (dwc_otg_param_initialized
  26585. + (core_if->core_params->enable_dynamic_fifo)) {
  26586. + DWC_ERROR
  26587. + ("%d invalid for enable_dynamic_fifo paremter. Check HW configuration.\n",
  26588. + val);
  26589. + }
  26590. + val = 0;
  26591. + retval = -DWC_E_INVALID;
  26592. + }
  26593. + core_if->core_params->enable_dynamic_fifo = val;
  26594. + return retval;
  26595. +}
  26596. +
  26597. +int32_t dwc_otg_get_param_enable_dynamic_fifo(dwc_otg_core_if_t * core_if)
  26598. +{
  26599. + return core_if->core_params->enable_dynamic_fifo;
  26600. +}
  26601. +
  26602. +int dwc_otg_set_param_data_fifo_size(dwc_otg_core_if_t * core_if, int32_t val)
  26603. +{
  26604. + int retval = 0;
  26605. + if (DWC_OTG_PARAM_TEST(val, 32, 32768)) {
  26606. + DWC_WARN("Wrong value for data_fifo_size\n");
  26607. + DWC_WARN("data_fifo_size must be 32-32768\n");
  26608. + return -DWC_E_INVALID;
  26609. + }
  26610. +
  26611. + if (val > core_if->hwcfg3.b.dfifo_depth) {
  26612. + if (dwc_otg_param_initialized
  26613. + (core_if->core_params->data_fifo_size)) {
  26614. + DWC_ERROR
  26615. + ("%d invalid for data_fifo_size parameter. Check HW configuration.\n",
  26616. + val);
  26617. + }
  26618. + val = core_if->hwcfg3.b.dfifo_depth;
  26619. + retval = -DWC_E_INVALID;
  26620. + }
  26621. +
  26622. + core_if->core_params->data_fifo_size = val;
  26623. + return retval;
  26624. +}
  26625. +
  26626. +int32_t dwc_otg_get_param_data_fifo_size(dwc_otg_core_if_t * core_if)
  26627. +{
  26628. + return core_if->core_params->data_fifo_size;
  26629. +}
  26630. +
  26631. +int dwc_otg_set_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if, int32_t val)
  26632. +{
  26633. + int retval = 0;
  26634. + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
  26635. + DWC_WARN("Wrong value for dev_rx_fifo_size\n");
  26636. + DWC_WARN("dev_rx_fifo_size must be 16-32768\n");
  26637. + return -DWC_E_INVALID;
  26638. + }
  26639. +
  26640. + if (val > DWC_READ_REG32(&core_if->core_global_regs->grxfsiz)) {
  26641. + if (dwc_otg_param_initialized(core_if->core_params->dev_rx_fifo_size)) {
  26642. + DWC_WARN("%d invalid for dev_rx_fifo_size parameter\n", val);
  26643. + }
  26644. + val = DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
  26645. + retval = -DWC_E_INVALID;
  26646. + }
  26647. +
  26648. + core_if->core_params->dev_rx_fifo_size = val;
  26649. + return retval;
  26650. +}
  26651. +
  26652. +int32_t dwc_otg_get_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if)
  26653. +{
  26654. + return core_if->core_params->dev_rx_fifo_size;
  26655. +}
  26656. +
  26657. +int dwc_otg_set_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  26658. + int32_t val)
  26659. +{
  26660. + int retval = 0;
  26661. +
  26662. + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
  26663. + DWC_WARN("Wrong value for dev_nperio_tx_fifo\n");
  26664. + DWC_WARN("dev_nperio_tx_fifo must be 16-32768\n");
  26665. + return -DWC_E_INVALID;
  26666. + }
  26667. +
  26668. + if (val > (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >> 16)) {
  26669. + if (dwc_otg_param_initialized
  26670. + (core_if->core_params->dev_nperio_tx_fifo_size)) {
  26671. + DWC_ERROR
  26672. + ("%d invalid for dev_nperio_tx_fifo_size. Check HW configuration.\n",
  26673. + val);
  26674. + }
  26675. + val =
  26676. + (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >>
  26677. + 16);
  26678. + retval = -DWC_E_INVALID;
  26679. + }
  26680. +
  26681. + core_if->core_params->dev_nperio_tx_fifo_size = val;
  26682. + return retval;
  26683. +}
  26684. +
  26685. +int32_t dwc_otg_get_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if)
  26686. +{
  26687. + return core_if->core_params->dev_nperio_tx_fifo_size;
  26688. +}
  26689. +
  26690. +int dwc_otg_set_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if,
  26691. + int32_t val)
  26692. +{
  26693. + int retval = 0;
  26694. +
  26695. + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
  26696. + DWC_WARN("Wrong value for host_rx_fifo_size\n");
  26697. + DWC_WARN("host_rx_fifo_size must be 16-32768\n");
  26698. + return -DWC_E_INVALID;
  26699. + }
  26700. +
  26701. + if (val > DWC_READ_REG32(&core_if->core_global_regs->grxfsiz)) {
  26702. + if (dwc_otg_param_initialized
  26703. + (core_if->core_params->host_rx_fifo_size)) {
  26704. + DWC_ERROR
  26705. + ("%d invalid for host_rx_fifo_size. Check HW configuration.\n",
  26706. + val);
  26707. + }
  26708. + val = DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
  26709. + retval = -DWC_E_INVALID;
  26710. + }
  26711. +
  26712. + core_if->core_params->host_rx_fifo_size = val;
  26713. + return retval;
  26714. +
  26715. +}
  26716. +
  26717. +int32_t dwc_otg_get_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if)
  26718. +{
  26719. + return core_if->core_params->host_rx_fifo_size;
  26720. +}
  26721. +
  26722. +int dwc_otg_set_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  26723. + int32_t val)
  26724. +{
  26725. + int retval = 0;
  26726. +
  26727. + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
  26728. + DWC_WARN("Wrong value for host_nperio_tx_fifo_size\n");
  26729. + DWC_WARN("host_nperio_tx_fifo_size must be 16-32768\n");
  26730. + return -DWC_E_INVALID;
  26731. + }
  26732. +
  26733. + if (val > (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >> 16)) {
  26734. + if (dwc_otg_param_initialized
  26735. + (core_if->core_params->host_nperio_tx_fifo_size)) {
  26736. + DWC_ERROR
  26737. + ("%d invalid for host_nperio_tx_fifo_size. Check HW configuration.\n",
  26738. + val);
  26739. + }
  26740. + val =
  26741. + (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >>
  26742. + 16);
  26743. + retval = -DWC_E_INVALID;
  26744. + }
  26745. +
  26746. + core_if->core_params->host_nperio_tx_fifo_size = val;
  26747. + return retval;
  26748. +}
  26749. +
  26750. +int32_t dwc_otg_get_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if)
  26751. +{
  26752. + return core_if->core_params->host_nperio_tx_fifo_size;
  26753. +}
  26754. +
  26755. +int dwc_otg_set_param_host_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  26756. + int32_t val)
  26757. +{
  26758. + int retval = 0;
  26759. + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
  26760. + DWC_WARN("Wrong value for host_perio_tx_fifo_size\n");
  26761. + DWC_WARN("host_perio_tx_fifo_size must be 16-32768\n");
  26762. + return -DWC_E_INVALID;
  26763. + }
  26764. +
  26765. + if (val > ((core_if->hptxfsiz.d32) >> 16)) {
  26766. + if (dwc_otg_param_initialized
  26767. + (core_if->core_params->host_perio_tx_fifo_size)) {
  26768. + DWC_ERROR
  26769. + ("%d invalid for host_perio_tx_fifo_size. Check HW configuration.\n",
  26770. + val);
  26771. + }
  26772. + val = (core_if->hptxfsiz.d32) >> 16;
  26773. + retval = -DWC_E_INVALID;
  26774. + }
  26775. +
  26776. + core_if->core_params->host_perio_tx_fifo_size = val;
  26777. + return retval;
  26778. +}
  26779. +
  26780. +int32_t dwc_otg_get_param_host_perio_tx_fifo_size(dwc_otg_core_if_t * core_if)
  26781. +{
  26782. + return core_if->core_params->host_perio_tx_fifo_size;
  26783. +}
  26784. +
  26785. +int dwc_otg_set_param_max_transfer_size(dwc_otg_core_if_t * core_if,
  26786. + int32_t val)
  26787. +{
  26788. + int retval = 0;
  26789. +
  26790. + if (DWC_OTG_PARAM_TEST(val, 2047, 524288)) {
  26791. + DWC_WARN("Wrong value for max_transfer_size\n");
  26792. + DWC_WARN("max_transfer_size must be 2047-524288\n");
  26793. + return -DWC_E_INVALID;
  26794. + }
  26795. +
  26796. + if (val >= (1 << (core_if->hwcfg3.b.xfer_size_cntr_width + 11))) {
  26797. + if (dwc_otg_param_initialized
  26798. + (core_if->core_params->max_transfer_size)) {
  26799. + DWC_ERROR
  26800. + ("%d invalid for max_transfer_size. Check HW configuration.\n",
  26801. + val);
  26802. + }
  26803. + val =
  26804. + ((1 << (core_if->hwcfg3.b.packet_size_cntr_width + 11)) -
  26805. + 1);
  26806. + retval = -DWC_E_INVALID;
  26807. + }
  26808. +
  26809. + core_if->core_params->max_transfer_size = val;
  26810. + return retval;
  26811. +}
  26812. +
  26813. +int32_t dwc_otg_get_param_max_transfer_size(dwc_otg_core_if_t * core_if)
  26814. +{
  26815. + return core_if->core_params->max_transfer_size;
  26816. +}
  26817. +
  26818. +int dwc_otg_set_param_max_packet_count(dwc_otg_core_if_t * core_if, int32_t val)
  26819. +{
  26820. + int retval = 0;
  26821. +
  26822. + if (DWC_OTG_PARAM_TEST(val, 15, 511)) {
  26823. + DWC_WARN("Wrong value for max_packet_count\n");
  26824. + DWC_WARN("max_packet_count must be 15-511\n");
  26825. + return -DWC_E_INVALID;
  26826. + }
  26827. +
  26828. + if (val > (1 << (core_if->hwcfg3.b.packet_size_cntr_width + 4))) {
  26829. + if (dwc_otg_param_initialized
  26830. + (core_if->core_params->max_packet_count)) {
  26831. + DWC_ERROR
  26832. + ("%d invalid for max_packet_count. Check HW configuration.\n",
  26833. + val);
  26834. + }
  26835. + val =
  26836. + ((1 << (core_if->hwcfg3.b.packet_size_cntr_width + 4)) - 1);
  26837. + retval = -DWC_E_INVALID;
  26838. + }
  26839. +
  26840. + core_if->core_params->max_packet_count = val;
  26841. + return retval;
  26842. +}
  26843. +
  26844. +int32_t dwc_otg_get_param_max_packet_count(dwc_otg_core_if_t * core_if)
  26845. +{
  26846. + return core_if->core_params->max_packet_count;
  26847. +}
  26848. +
  26849. +int dwc_otg_set_param_host_channels(dwc_otg_core_if_t * core_if, int32_t val)
  26850. +{
  26851. + int retval = 0;
  26852. +
  26853. + if (DWC_OTG_PARAM_TEST(val, 1, 16)) {
  26854. + DWC_WARN("Wrong value for host_channels\n");
  26855. + DWC_WARN("host_channels must be 1-16\n");
  26856. + return -DWC_E_INVALID;
  26857. + }
  26858. +
  26859. + if (val > (core_if->hwcfg2.b.num_host_chan + 1)) {
  26860. + if (dwc_otg_param_initialized
  26861. + (core_if->core_params->host_channels)) {
  26862. + DWC_ERROR
  26863. + ("%d invalid for host_channels. Check HW configurations.\n",
  26864. + val);
  26865. + }
  26866. + val = (core_if->hwcfg2.b.num_host_chan + 1);
  26867. + retval = -DWC_E_INVALID;
  26868. + }
  26869. +
  26870. + core_if->core_params->host_channels = val;
  26871. + return retval;
  26872. +}
  26873. +
  26874. +int32_t dwc_otg_get_param_host_channels(dwc_otg_core_if_t * core_if)
  26875. +{
  26876. + return core_if->core_params->host_channels;
  26877. +}
  26878. +
  26879. +int dwc_otg_set_param_dev_endpoints(dwc_otg_core_if_t * core_if, int32_t val)
  26880. +{
  26881. + int retval = 0;
  26882. +
  26883. + if (DWC_OTG_PARAM_TEST(val, 1, 15)) {
  26884. + DWC_WARN("Wrong value for dev_endpoints\n");
  26885. + DWC_WARN("dev_endpoints must be 1-15\n");
  26886. + return -DWC_E_INVALID;
  26887. + }
  26888. +
  26889. + if (val > (core_if->hwcfg2.b.num_dev_ep)) {
  26890. + if (dwc_otg_param_initialized
  26891. + (core_if->core_params->dev_endpoints)) {
  26892. + DWC_ERROR
  26893. + ("%d invalid for dev_endpoints. Check HW configurations.\n",
  26894. + val);
  26895. + }
  26896. + val = core_if->hwcfg2.b.num_dev_ep;
  26897. + retval = -DWC_E_INVALID;
  26898. + }
  26899. +
  26900. + core_if->core_params->dev_endpoints = val;
  26901. + return retval;
  26902. +}
  26903. +
  26904. +int32_t dwc_otg_get_param_dev_endpoints(dwc_otg_core_if_t * core_if)
  26905. +{
  26906. + return core_if->core_params->dev_endpoints;
  26907. +}
  26908. +
  26909. +int dwc_otg_set_param_phy_type(dwc_otg_core_if_t * core_if, int32_t val)
  26910. +{
  26911. + int retval = 0;
  26912. + int valid = 0;
  26913. +
  26914. + if (DWC_OTG_PARAM_TEST(val, 0, 2)) {
  26915. + DWC_WARN("Wrong value for phy_type\n");
  26916. + DWC_WARN("phy_type must be 0,1 or 2\n");
  26917. + return -DWC_E_INVALID;
  26918. + }
  26919. +#ifndef NO_FS_PHY_HW_CHECKS
  26920. + if ((val == DWC_PHY_TYPE_PARAM_UTMI) &&
  26921. + ((core_if->hwcfg2.b.hs_phy_type == 1) ||
  26922. + (core_if->hwcfg2.b.hs_phy_type == 3))) {
  26923. + valid = 1;
  26924. + } else if ((val == DWC_PHY_TYPE_PARAM_ULPI) &&
  26925. + ((core_if->hwcfg2.b.hs_phy_type == 2) ||
  26926. + (core_if->hwcfg2.b.hs_phy_type == 3))) {
  26927. + valid = 1;
  26928. + } else if ((val == DWC_PHY_TYPE_PARAM_FS) &&
  26929. + (core_if->hwcfg2.b.fs_phy_type == 1)) {
  26930. + valid = 1;
  26931. + }
  26932. + if (!valid) {
  26933. + if (dwc_otg_param_initialized(core_if->core_params->phy_type)) {
  26934. + DWC_ERROR
  26935. + ("%d invalid for phy_type. Check HW configurations.\n",
  26936. + val);
  26937. + }
  26938. + if (core_if->hwcfg2.b.hs_phy_type) {
  26939. + if ((core_if->hwcfg2.b.hs_phy_type == 3) ||
  26940. + (core_if->hwcfg2.b.hs_phy_type == 1)) {
  26941. + val = DWC_PHY_TYPE_PARAM_UTMI;
  26942. + } else {
  26943. + val = DWC_PHY_TYPE_PARAM_ULPI;
  26944. + }
  26945. + }
  26946. + retval = -DWC_E_INVALID;
  26947. + }
  26948. +#endif
  26949. + core_if->core_params->phy_type = val;
  26950. + return retval;
  26951. +}
  26952. +
  26953. +int32_t dwc_otg_get_param_phy_type(dwc_otg_core_if_t * core_if)
  26954. +{
  26955. + return core_if->core_params->phy_type;
  26956. +}
  26957. +
  26958. +int dwc_otg_set_param_speed(dwc_otg_core_if_t * core_if, int32_t val)
  26959. +{
  26960. + int retval = 0;
  26961. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  26962. + DWC_WARN("Wrong value for speed parameter\n");
  26963. + DWC_WARN("max_speed parameter must be 0 or 1\n");
  26964. + return -DWC_E_INVALID;
  26965. + }
  26966. + if ((val == 0)
  26967. + && dwc_otg_get_param_phy_type(core_if) == DWC_PHY_TYPE_PARAM_FS) {
  26968. + if (dwc_otg_param_initialized(core_if->core_params->speed)) {
  26969. + DWC_ERROR
  26970. + ("%d invalid for speed paremter. Check HW configuration.\n",
  26971. + val);
  26972. + }
  26973. + val =
  26974. + (dwc_otg_get_param_phy_type(core_if) ==
  26975. + DWC_PHY_TYPE_PARAM_FS ? 1 : 0);
  26976. + retval = -DWC_E_INVALID;
  26977. + }
  26978. + core_if->core_params->speed = val;
  26979. + return retval;
  26980. +}
  26981. +
  26982. +int32_t dwc_otg_get_param_speed(dwc_otg_core_if_t * core_if)
  26983. +{
  26984. + return core_if->core_params->speed;
  26985. +}
  26986. +
  26987. +int dwc_otg_set_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t * core_if,
  26988. + int32_t val)
  26989. +{
  26990. + int retval = 0;
  26991. +
  26992. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  26993. + DWC_WARN
  26994. + ("Wrong value for host_ls_low_power_phy_clk parameter\n");
  26995. + DWC_WARN("host_ls_low_power_phy_clk must be 0 or 1\n");
  26996. + return -DWC_E_INVALID;
  26997. + }
  26998. +
  26999. + if ((val == DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ)
  27000. + && (dwc_otg_get_param_phy_type(core_if) == DWC_PHY_TYPE_PARAM_FS)) {
  27001. + if (dwc_otg_param_initialized
  27002. + (core_if->core_params->host_ls_low_power_phy_clk)) {
  27003. + DWC_ERROR
  27004. + ("%d invalid for host_ls_low_power_phy_clk. Check HW configuration.\n",
  27005. + val);
  27006. + }
  27007. + val =
  27008. + (dwc_otg_get_param_phy_type(core_if) ==
  27009. + DWC_PHY_TYPE_PARAM_FS) ?
  27010. + DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ :
  27011. + DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ;
  27012. + retval = -DWC_E_INVALID;
  27013. + }
  27014. +
  27015. + core_if->core_params->host_ls_low_power_phy_clk = val;
  27016. + return retval;
  27017. +}
  27018. +
  27019. +int32_t dwc_otg_get_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t * core_if)
  27020. +{
  27021. + return core_if->core_params->host_ls_low_power_phy_clk;
  27022. +}
  27023. +
  27024. +int dwc_otg_set_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if, int32_t val)
  27025. +{
  27026. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27027. + DWC_WARN("Wrong value for phy_ulpi_ddr\n");
  27028. + DWC_WARN("phy_upli_ddr must be 0 or 1\n");
  27029. + return -DWC_E_INVALID;
  27030. + }
  27031. +
  27032. + core_if->core_params->phy_ulpi_ddr = val;
  27033. + return 0;
  27034. +}
  27035. +
  27036. +int32_t dwc_otg_get_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if)
  27037. +{
  27038. + return core_if->core_params->phy_ulpi_ddr;
  27039. +}
  27040. +
  27041. +int dwc_otg_set_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if,
  27042. + int32_t val)
  27043. +{
  27044. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27045. + DWC_WARN("Wrong valaue for phy_ulpi_ext_vbus\n");
  27046. + DWC_WARN("phy_ulpi_ext_vbus must be 0 or 1\n");
  27047. + return -DWC_E_INVALID;
  27048. + }
  27049. +
  27050. + core_if->core_params->phy_ulpi_ext_vbus = val;
  27051. + return 0;
  27052. +}
  27053. +
  27054. +int32_t dwc_otg_get_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if)
  27055. +{
  27056. + return core_if->core_params->phy_ulpi_ext_vbus;
  27057. +}
  27058. +
  27059. +int dwc_otg_set_param_phy_utmi_width(dwc_otg_core_if_t * core_if, int32_t val)
  27060. +{
  27061. + if (DWC_OTG_PARAM_TEST(val, 8, 8) && DWC_OTG_PARAM_TEST(val, 16, 16)) {
  27062. + DWC_WARN("Wrong valaue for phy_utmi_width\n");
  27063. + DWC_WARN("phy_utmi_width must be 8 or 16\n");
  27064. + return -DWC_E_INVALID;
  27065. + }
  27066. +
  27067. + core_if->core_params->phy_utmi_width = val;
  27068. + return 0;
  27069. +}
  27070. +
  27071. +int32_t dwc_otg_get_param_phy_utmi_width(dwc_otg_core_if_t * core_if)
  27072. +{
  27073. + return core_if->core_params->phy_utmi_width;
  27074. +}
  27075. +
  27076. +int dwc_otg_set_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if, int32_t val)
  27077. +{
  27078. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27079. + DWC_WARN("Wrong valaue for ulpi_fs_ls\n");
  27080. + DWC_WARN("ulpi_fs_ls must be 0 or 1\n");
  27081. + return -DWC_E_INVALID;
  27082. + }
  27083. +
  27084. + core_if->core_params->ulpi_fs_ls = val;
  27085. + return 0;
  27086. +}
  27087. +
  27088. +int32_t dwc_otg_get_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if)
  27089. +{
  27090. + return core_if->core_params->ulpi_fs_ls;
  27091. +}
  27092. +
  27093. +int dwc_otg_set_param_ts_dline(dwc_otg_core_if_t * core_if, int32_t val)
  27094. +{
  27095. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27096. + DWC_WARN("Wrong valaue for ts_dline\n");
  27097. + DWC_WARN("ts_dline must be 0 or 1\n");
  27098. + return -DWC_E_INVALID;
  27099. + }
  27100. +
  27101. + core_if->core_params->ts_dline = val;
  27102. + return 0;
  27103. +}
  27104. +
  27105. +int32_t dwc_otg_get_param_ts_dline(dwc_otg_core_if_t * core_if)
  27106. +{
  27107. + return core_if->core_params->ts_dline;
  27108. +}
  27109. +
  27110. +int dwc_otg_set_param_i2c_enable(dwc_otg_core_if_t * core_if, int32_t val)
  27111. +{
  27112. + int retval = 0;
  27113. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27114. + DWC_WARN("Wrong valaue for i2c_enable\n");
  27115. + DWC_WARN("i2c_enable must be 0 or 1\n");
  27116. + return -DWC_E_INVALID;
  27117. + }
  27118. +#ifndef NO_FS_PHY_HW_CHECK
  27119. + if (val == 1 && core_if->hwcfg3.b.i2c == 0) {
  27120. + if (dwc_otg_param_initialized(core_if->core_params->i2c_enable)) {
  27121. + DWC_ERROR
  27122. + ("%d invalid for i2c_enable. Check HW configuration.\n",
  27123. + val);
  27124. + }
  27125. + val = 0;
  27126. + retval = -DWC_E_INVALID;
  27127. + }
  27128. +#endif
  27129. +
  27130. + core_if->core_params->i2c_enable = val;
  27131. + return retval;
  27132. +}
  27133. +
  27134. +int32_t dwc_otg_get_param_i2c_enable(dwc_otg_core_if_t * core_if)
  27135. +{
  27136. + return core_if->core_params->i2c_enable;
  27137. +}
  27138. +
  27139. +int dwc_otg_set_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  27140. + int32_t val, int fifo_num)
  27141. +{
  27142. + int retval = 0;
  27143. +
  27144. + if (DWC_OTG_PARAM_TEST(val, 4, 768)) {
  27145. + DWC_WARN("Wrong value for dev_perio_tx_fifo_size\n");
  27146. + DWC_WARN("dev_perio_tx_fifo_size must be 4-768\n");
  27147. + return -DWC_E_INVALID;
  27148. + }
  27149. +
  27150. + if (val >
  27151. + (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]))) {
  27152. + if (dwc_otg_param_initialized
  27153. + (core_if->core_params->dev_perio_tx_fifo_size[fifo_num])) {
  27154. + DWC_ERROR
  27155. + ("`%d' invalid for parameter `dev_perio_fifo_size_%d'. Check HW configuration.\n",
  27156. + val, fifo_num);
  27157. + }
  27158. + val = (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]));
  27159. + retval = -DWC_E_INVALID;
  27160. + }
  27161. +
  27162. + core_if->core_params->dev_perio_tx_fifo_size[fifo_num] = val;
  27163. + return retval;
  27164. +}
  27165. +
  27166. +int32_t dwc_otg_get_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  27167. + int fifo_num)
  27168. +{
  27169. + return core_if->core_params->dev_perio_tx_fifo_size[fifo_num];
  27170. +}
  27171. +
  27172. +int dwc_otg_set_param_en_multiple_tx_fifo(dwc_otg_core_if_t * core_if,
  27173. + int32_t val)
  27174. +{
  27175. + int retval = 0;
  27176. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27177. + DWC_WARN("Wrong valaue for en_multiple_tx_fifo,\n");
  27178. + DWC_WARN("en_multiple_tx_fifo must be 0 or 1\n");
  27179. + return -DWC_E_INVALID;
  27180. + }
  27181. +
  27182. + if (val == 1 && core_if->hwcfg4.b.ded_fifo_en == 0) {
  27183. + if (dwc_otg_param_initialized
  27184. + (core_if->core_params->en_multiple_tx_fifo)) {
  27185. + DWC_ERROR
  27186. + ("%d invalid for parameter en_multiple_tx_fifo. Check HW configuration.\n",
  27187. + val);
  27188. + }
  27189. + val = 0;
  27190. + retval = -DWC_E_INVALID;
  27191. + }
  27192. +
  27193. + core_if->core_params->en_multiple_tx_fifo = val;
  27194. + return retval;
  27195. +}
  27196. +
  27197. +int32_t dwc_otg_get_param_en_multiple_tx_fifo(dwc_otg_core_if_t * core_if)
  27198. +{
  27199. + return core_if->core_params->en_multiple_tx_fifo;
  27200. +}
  27201. +
  27202. +int dwc_otg_set_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if, int32_t val,
  27203. + int fifo_num)
  27204. +{
  27205. + int retval = 0;
  27206. +
  27207. + if (DWC_OTG_PARAM_TEST(val, 4, 768)) {
  27208. + DWC_WARN("Wrong value for dev_tx_fifo_size\n");
  27209. + DWC_WARN("dev_tx_fifo_size must be 4-768\n");
  27210. + return -DWC_E_INVALID;
  27211. + }
  27212. +
  27213. + if (val >
  27214. + (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]))) {
  27215. + if (dwc_otg_param_initialized
  27216. + (core_if->core_params->dev_tx_fifo_size[fifo_num])) {
  27217. + DWC_ERROR
  27218. + ("`%d' invalid for parameter `dev_tx_fifo_size_%d'. Check HW configuration.\n",
  27219. + val, fifo_num);
  27220. + }
  27221. + val = (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]));
  27222. + retval = -DWC_E_INVALID;
  27223. + }
  27224. +
  27225. + core_if->core_params->dev_tx_fifo_size[fifo_num] = val;
  27226. + return retval;
  27227. +}
  27228. +
  27229. +int32_t dwc_otg_get_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if,
  27230. + int fifo_num)
  27231. +{
  27232. + return core_if->core_params->dev_tx_fifo_size[fifo_num];
  27233. +}
  27234. +
  27235. +int dwc_otg_set_param_thr_ctl(dwc_otg_core_if_t * core_if, int32_t val)
  27236. +{
  27237. + int retval = 0;
  27238. +
  27239. + if (DWC_OTG_PARAM_TEST(val, 0, 7)) {
  27240. + DWC_WARN("Wrong value for thr_ctl\n");
  27241. + DWC_WARN("thr_ctl must be 0-7\n");
  27242. + return -DWC_E_INVALID;
  27243. + }
  27244. +
  27245. + if ((val != 0) &&
  27246. + (!dwc_otg_get_param_dma_enable(core_if) ||
  27247. + !core_if->hwcfg4.b.ded_fifo_en)) {
  27248. + if (dwc_otg_param_initialized(core_if->core_params->thr_ctl)) {
  27249. + DWC_ERROR
  27250. + ("%d invalid for parameter thr_ctl. Check HW configuration.\n",
  27251. + val);
  27252. + }
  27253. + val = 0;
  27254. + retval = -DWC_E_INVALID;
  27255. + }
  27256. +
  27257. + core_if->core_params->thr_ctl = val;
  27258. + return retval;
  27259. +}
  27260. +
  27261. +int32_t dwc_otg_get_param_thr_ctl(dwc_otg_core_if_t * core_if)
  27262. +{
  27263. + return core_if->core_params->thr_ctl;
  27264. +}
  27265. +
  27266. +int dwc_otg_set_param_lpm_enable(dwc_otg_core_if_t * core_if, int32_t val)
  27267. +{
  27268. + int retval = 0;
  27269. +
  27270. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27271. + DWC_WARN("Wrong value for lpm_enable\n");
  27272. + DWC_WARN("lpm_enable must be 0 or 1\n");
  27273. + return -DWC_E_INVALID;
  27274. + }
  27275. +
  27276. + if (val && !core_if->hwcfg3.b.otg_lpm_en) {
  27277. + if (dwc_otg_param_initialized(core_if->core_params->lpm_enable)) {
  27278. + DWC_ERROR
  27279. + ("%d invalid for parameter lpm_enable. Check HW configuration.\n",
  27280. + val);
  27281. + }
  27282. + val = 0;
  27283. + retval = -DWC_E_INVALID;
  27284. + }
  27285. +
  27286. + core_if->core_params->lpm_enable = val;
  27287. + return retval;
  27288. +}
  27289. +
  27290. +int32_t dwc_otg_get_param_lpm_enable(dwc_otg_core_if_t * core_if)
  27291. +{
  27292. + return core_if->core_params->lpm_enable;
  27293. +}
  27294. +
  27295. +int dwc_otg_set_param_tx_thr_length(dwc_otg_core_if_t * core_if, int32_t val)
  27296. +{
  27297. + if (DWC_OTG_PARAM_TEST(val, 8, 128)) {
  27298. + DWC_WARN("Wrong valaue for tx_thr_length\n");
  27299. + DWC_WARN("tx_thr_length must be 8 - 128\n");
  27300. + return -DWC_E_INVALID;
  27301. + }
  27302. +
  27303. + core_if->core_params->tx_thr_length = val;
  27304. + return 0;
  27305. +}
  27306. +
  27307. +int32_t dwc_otg_get_param_tx_thr_length(dwc_otg_core_if_t * core_if)
  27308. +{
  27309. + return core_if->core_params->tx_thr_length;
  27310. +}
  27311. +
  27312. +int dwc_otg_set_param_rx_thr_length(dwc_otg_core_if_t * core_if, int32_t val)
  27313. +{
  27314. + if (DWC_OTG_PARAM_TEST(val, 8, 128)) {
  27315. + DWC_WARN("Wrong valaue for rx_thr_length\n");
  27316. + DWC_WARN("rx_thr_length must be 8 - 128\n");
  27317. + return -DWC_E_INVALID;
  27318. + }
  27319. +
  27320. + core_if->core_params->rx_thr_length = val;
  27321. + return 0;
  27322. +}
  27323. +
  27324. +int32_t dwc_otg_get_param_rx_thr_length(dwc_otg_core_if_t * core_if)
  27325. +{
  27326. + return core_if->core_params->rx_thr_length;
  27327. +}
  27328. +
  27329. +int dwc_otg_set_param_dma_burst_size(dwc_otg_core_if_t * core_if, int32_t val)
  27330. +{
  27331. + if (DWC_OTG_PARAM_TEST(val, 1, 1) &&
  27332. + DWC_OTG_PARAM_TEST(val, 4, 4) &&
  27333. + DWC_OTG_PARAM_TEST(val, 8, 8) &&
  27334. + DWC_OTG_PARAM_TEST(val, 16, 16) &&
  27335. + DWC_OTG_PARAM_TEST(val, 32, 32) &&
  27336. + DWC_OTG_PARAM_TEST(val, 64, 64) &&
  27337. + DWC_OTG_PARAM_TEST(val, 128, 128) &&
  27338. + DWC_OTG_PARAM_TEST(val, 256, 256)) {
  27339. + DWC_WARN("`%d' invalid for parameter `dma_burst_size'\n", val);
  27340. + return -DWC_E_INVALID;
  27341. + }
  27342. + core_if->core_params->dma_burst_size = val;
  27343. + return 0;
  27344. +}
  27345. +
  27346. +int32_t dwc_otg_get_param_dma_burst_size(dwc_otg_core_if_t * core_if)
  27347. +{
  27348. + return core_if->core_params->dma_burst_size;
  27349. +}
  27350. +
  27351. +int dwc_otg_set_param_pti_enable(dwc_otg_core_if_t * core_if, int32_t val)
  27352. +{
  27353. + int retval = 0;
  27354. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27355. + DWC_WARN("`%d' invalid for parameter `pti_enable'\n", val);
  27356. + return -DWC_E_INVALID;
  27357. + }
  27358. + if (val && (core_if->snpsid < OTG_CORE_REV_2_72a)) {
  27359. + if (dwc_otg_param_initialized(core_if->core_params->pti_enable)) {
  27360. + DWC_ERROR
  27361. + ("%d invalid for parameter pti_enable. Check HW configuration.\n",
  27362. + val);
  27363. + }
  27364. + retval = -DWC_E_INVALID;
  27365. + val = 0;
  27366. + }
  27367. + core_if->core_params->pti_enable = val;
  27368. + return retval;
  27369. +}
  27370. +
  27371. +int32_t dwc_otg_get_param_pti_enable(dwc_otg_core_if_t * core_if)
  27372. +{
  27373. + return core_if->core_params->pti_enable;
  27374. +}
  27375. +
  27376. +int dwc_otg_set_param_mpi_enable(dwc_otg_core_if_t * core_if, int32_t val)
  27377. +{
  27378. + int retval = 0;
  27379. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27380. + DWC_WARN("`%d' invalid for parameter `mpi_enable'\n", val);
  27381. + return -DWC_E_INVALID;
  27382. + }
  27383. + if (val && (core_if->hwcfg2.b.multi_proc_int == 0)) {
  27384. + if (dwc_otg_param_initialized(core_if->core_params->mpi_enable)) {
  27385. + DWC_ERROR
  27386. + ("%d invalid for parameter mpi_enable. Check HW configuration.\n",
  27387. + val);
  27388. + }
  27389. + retval = -DWC_E_INVALID;
  27390. + val = 0;
  27391. + }
  27392. + core_if->core_params->mpi_enable = val;
  27393. + return retval;
  27394. +}
  27395. +
  27396. +int32_t dwc_otg_get_param_mpi_enable(dwc_otg_core_if_t * core_if)
  27397. +{
  27398. + return core_if->core_params->mpi_enable;
  27399. +}
  27400. +
  27401. +int dwc_otg_set_param_adp_enable(dwc_otg_core_if_t * core_if, int32_t val)
  27402. +{
  27403. + int retval = 0;
  27404. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27405. + DWC_WARN("`%d' invalid for parameter `adp_enable'\n", val);
  27406. + return -DWC_E_INVALID;
  27407. + }
  27408. + if (val && (core_if->hwcfg3.b.adp_supp == 0)) {
  27409. + if (dwc_otg_param_initialized
  27410. + (core_if->core_params->adp_supp_enable)) {
  27411. + DWC_ERROR
  27412. + ("%d invalid for parameter adp_enable. Check HW configuration.\n",
  27413. + val);
  27414. + }
  27415. + retval = -DWC_E_INVALID;
  27416. + val = 0;
  27417. + }
  27418. + core_if->core_params->adp_supp_enable = val;
  27419. + /*Set OTG version 2.0 in case of enabling ADP*/
  27420. + if (val)
  27421. + dwc_otg_set_param_otg_ver(core_if, 1);
  27422. +
  27423. + return retval;
  27424. +}
  27425. +
  27426. +int32_t dwc_otg_get_param_adp_enable(dwc_otg_core_if_t * core_if)
  27427. +{
  27428. + return core_if->core_params->adp_supp_enable;
  27429. +}
  27430. +
  27431. +int dwc_otg_set_param_ic_usb_cap(dwc_otg_core_if_t * core_if, int32_t val)
  27432. +{
  27433. + int retval = 0;
  27434. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27435. + DWC_WARN("`%d' invalid for parameter `ic_usb_cap'\n", val);
  27436. + DWC_WARN("ic_usb_cap must be 0 or 1\n");
  27437. + return -DWC_E_INVALID;
  27438. + }
  27439. +
  27440. + if (val && (core_if->hwcfg2.b.otg_enable_ic_usb == 0)) {
  27441. + if (dwc_otg_param_initialized(core_if->core_params->ic_usb_cap)) {
  27442. + DWC_ERROR
  27443. + ("%d invalid for parameter ic_usb_cap. Check HW configuration.\n",
  27444. + val);
  27445. + }
  27446. + retval = -DWC_E_INVALID;
  27447. + val = 0;
  27448. + }
  27449. + core_if->core_params->ic_usb_cap = val;
  27450. + return retval;
  27451. +}
  27452. +
  27453. +int32_t dwc_otg_get_param_ic_usb_cap(dwc_otg_core_if_t * core_if)
  27454. +{
  27455. + return core_if->core_params->ic_usb_cap;
  27456. +}
  27457. +
  27458. +int dwc_otg_set_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if, int32_t val)
  27459. +{
  27460. + int retval = 0;
  27461. + int valid = 1;
  27462. +
  27463. + if (DWC_OTG_PARAM_TEST(val, 0, 3)) {
  27464. + DWC_WARN("`%d' invalid for parameter `ahb_thr_ratio'\n", val);
  27465. + DWC_WARN("ahb_thr_ratio must be 0 - 3\n");
  27466. + return -DWC_E_INVALID;
  27467. + }
  27468. +
  27469. + if (val
  27470. + && (core_if->snpsid < OTG_CORE_REV_2_81a
  27471. + || !dwc_otg_get_param_thr_ctl(core_if))) {
  27472. + valid = 0;
  27473. + } else if (val
  27474. + && ((dwc_otg_get_param_tx_thr_length(core_if) / (1 << val)) <
  27475. + 4)) {
  27476. + valid = 0;
  27477. + }
  27478. + if (valid == 0) {
  27479. + if (dwc_otg_param_initialized
  27480. + (core_if->core_params->ahb_thr_ratio)) {
  27481. + DWC_ERROR
  27482. + ("%d invalid for parameter ahb_thr_ratio. Check HW configuration.\n",
  27483. + val);
  27484. + }
  27485. + retval = -DWC_E_INVALID;
  27486. + val = 0;
  27487. + }
  27488. +
  27489. + core_if->core_params->ahb_thr_ratio = val;
  27490. + return retval;
  27491. +}
  27492. +
  27493. +int32_t dwc_otg_get_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if)
  27494. +{
  27495. + return core_if->core_params->ahb_thr_ratio;
  27496. +}
  27497. +
  27498. +int dwc_otg_set_param_power_down(dwc_otg_core_if_t * core_if, int32_t val)
  27499. +{
  27500. + int retval = 0;
  27501. + int valid = 1;
  27502. + hwcfg4_data_t hwcfg4 = {.d32 = 0 };
  27503. + hwcfg4.d32 = DWC_READ_REG32(&core_if->core_global_regs->ghwcfg4);
  27504. +
  27505. + if (DWC_OTG_PARAM_TEST(val, 0, 3)) {
  27506. + DWC_WARN("`%d' invalid for parameter `power_down'\n", val);
  27507. + DWC_WARN("power_down must be 0 - 2\n");
  27508. + return -DWC_E_INVALID;
  27509. + }
  27510. +
  27511. + if ((val == 2) && (core_if->snpsid < OTG_CORE_REV_2_91a)) {
  27512. + valid = 0;
  27513. + }
  27514. + if ((val == 3)
  27515. + && ((core_if->snpsid < OTG_CORE_REV_3_00a)
  27516. + || (hwcfg4.b.xhiber == 0))) {
  27517. + valid = 0;
  27518. + }
  27519. + if (valid == 0) {
  27520. + if (dwc_otg_param_initialized(core_if->core_params->power_down)) {
  27521. + DWC_ERROR
  27522. + ("%d invalid for parameter power_down. Check HW configuration.\n",
  27523. + val);
  27524. + }
  27525. + retval = -DWC_E_INVALID;
  27526. + val = 0;
  27527. + }
  27528. + core_if->core_params->power_down = val;
  27529. + return retval;
  27530. +}
  27531. +
  27532. +int32_t dwc_otg_get_param_power_down(dwc_otg_core_if_t * core_if)
  27533. +{
  27534. + return core_if->core_params->power_down;
  27535. +}
  27536. +
  27537. +int dwc_otg_set_param_reload_ctl(dwc_otg_core_if_t * core_if, int32_t val)
  27538. +{
  27539. + int retval = 0;
  27540. + int valid = 1;
  27541. +
  27542. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27543. + DWC_WARN("`%d' invalid for parameter `reload_ctl'\n", val);
  27544. + DWC_WARN("reload_ctl must be 0 or 1\n");
  27545. + return -DWC_E_INVALID;
  27546. + }
  27547. +
  27548. + if ((val == 1) && (core_if->snpsid < OTG_CORE_REV_2_92a)) {
  27549. + valid = 0;
  27550. + }
  27551. + if (valid == 0) {
  27552. + if (dwc_otg_param_initialized(core_if->core_params->reload_ctl)) {
  27553. + DWC_ERROR("%d invalid for parameter reload_ctl."
  27554. + "Check HW configuration.\n", val);
  27555. + }
  27556. + retval = -DWC_E_INVALID;
  27557. + val = 0;
  27558. + }
  27559. + core_if->core_params->reload_ctl = val;
  27560. + return retval;
  27561. +}
  27562. +
  27563. +int32_t dwc_otg_get_param_reload_ctl(dwc_otg_core_if_t * core_if)
  27564. +{
  27565. + return core_if->core_params->reload_ctl;
  27566. +}
  27567. +
  27568. +int dwc_otg_set_param_dev_out_nak(dwc_otg_core_if_t * core_if, int32_t val)
  27569. +{
  27570. + int retval = 0;
  27571. + int valid = 1;
  27572. +
  27573. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27574. + DWC_WARN("`%d' invalid for parameter `dev_out_nak'\n", val);
  27575. + DWC_WARN("dev_out_nak must be 0 or 1\n");
  27576. + return -DWC_E_INVALID;
  27577. + }
  27578. +
  27579. + if ((val == 1) && ((core_if->snpsid < OTG_CORE_REV_2_93a) ||
  27580. + !(core_if->core_params->dma_desc_enable))) {
  27581. + valid = 0;
  27582. + }
  27583. + if (valid == 0) {
  27584. + if (dwc_otg_param_initialized(core_if->core_params->dev_out_nak)) {
  27585. + DWC_ERROR("%d invalid for parameter dev_out_nak."
  27586. + "Check HW configuration.\n", val);
  27587. + }
  27588. + retval = -DWC_E_INVALID;
  27589. + val = 0;
  27590. + }
  27591. + core_if->core_params->dev_out_nak = val;
  27592. + return retval;
  27593. +}
  27594. +
  27595. +int32_t dwc_otg_get_param_dev_out_nak(dwc_otg_core_if_t * core_if)
  27596. +{
  27597. + return core_if->core_params->dev_out_nak;
  27598. +}
  27599. +
  27600. +int dwc_otg_set_param_cont_on_bna(dwc_otg_core_if_t * core_if, int32_t val)
  27601. +{
  27602. + int retval = 0;
  27603. + int valid = 1;
  27604. +
  27605. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27606. + DWC_WARN("`%d' invalid for parameter `cont_on_bna'\n", val);
  27607. + DWC_WARN("cont_on_bna must be 0 or 1\n");
  27608. + return -DWC_E_INVALID;
  27609. + }
  27610. +
  27611. + if ((val == 1) && ((core_if->snpsid < OTG_CORE_REV_2_94a) ||
  27612. + !(core_if->core_params->dma_desc_enable))) {
  27613. + valid = 0;
  27614. + }
  27615. + if (valid == 0) {
  27616. + if (dwc_otg_param_initialized(core_if->core_params->cont_on_bna)) {
  27617. + DWC_ERROR("%d invalid for parameter cont_on_bna."
  27618. + "Check HW configuration.\n", val);
  27619. + }
  27620. + retval = -DWC_E_INVALID;
  27621. + val = 0;
  27622. + }
  27623. + core_if->core_params->cont_on_bna = val;
  27624. + return retval;
  27625. +}
  27626. +
  27627. +int32_t dwc_otg_get_param_cont_on_bna(dwc_otg_core_if_t * core_if)
  27628. +{
  27629. + return core_if->core_params->cont_on_bna;
  27630. +}
  27631. +
  27632. +int dwc_otg_set_param_ahb_single(dwc_otg_core_if_t * core_if, int32_t val)
  27633. +{
  27634. + int retval = 0;
  27635. + int valid = 1;
  27636. +
  27637. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27638. + DWC_WARN("`%d' invalid for parameter `ahb_single'\n", val);
  27639. + DWC_WARN("ahb_single must be 0 or 1\n");
  27640. + return -DWC_E_INVALID;
  27641. + }
  27642. +
  27643. + if ((val == 1) && (core_if->snpsid < OTG_CORE_REV_2_94a)) {
  27644. + valid = 0;
  27645. + }
  27646. + if (valid == 0) {
  27647. + if (dwc_otg_param_initialized(core_if->core_params->ahb_single)) {
  27648. + DWC_ERROR("%d invalid for parameter ahb_single."
  27649. + "Check HW configuration.\n", val);
  27650. + }
  27651. + retval = -DWC_E_INVALID;
  27652. + val = 0;
  27653. + }
  27654. + core_if->core_params->ahb_single = val;
  27655. + return retval;
  27656. +}
  27657. +
  27658. +int32_t dwc_otg_get_param_ahb_single(dwc_otg_core_if_t * core_if)
  27659. +{
  27660. + return core_if->core_params->ahb_single;
  27661. +}
  27662. +
  27663. +int dwc_otg_set_param_otg_ver(dwc_otg_core_if_t * core_if, int32_t val)
  27664. +{
  27665. + int retval = 0;
  27666. +
  27667. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27668. + DWC_WARN("`%d' invalid for parameter `otg_ver'\n", val);
  27669. + DWC_WARN
  27670. + ("otg_ver must be 0(for OTG 1.3 support) or 1(for OTG 2.0 support)\n");
  27671. + return -DWC_E_INVALID;
  27672. + }
  27673. +
  27674. + core_if->core_params->otg_ver = val;
  27675. + return retval;
  27676. +}
  27677. +
  27678. +int32_t dwc_otg_get_param_otg_ver(dwc_otg_core_if_t * core_if)
  27679. +{
  27680. + return core_if->core_params->otg_ver;
  27681. +}
  27682. +
  27683. +uint32_t dwc_otg_get_hnpstatus(dwc_otg_core_if_t * core_if)
  27684. +{
  27685. + gotgctl_data_t otgctl;
  27686. + otgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  27687. + return otgctl.b.hstnegscs;
  27688. +}
  27689. +
  27690. +uint32_t dwc_otg_get_srpstatus(dwc_otg_core_if_t * core_if)
  27691. +{
  27692. + gotgctl_data_t otgctl;
  27693. + otgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  27694. + return otgctl.b.sesreqscs;
  27695. +}
  27696. +
  27697. +void dwc_otg_set_hnpreq(dwc_otg_core_if_t * core_if, uint32_t val)
  27698. +{
  27699. + if(core_if->otg_ver == 0) {
  27700. + gotgctl_data_t otgctl;
  27701. + otgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  27702. + otgctl.b.hnpreq = val;
  27703. + DWC_WRITE_REG32(&core_if->core_global_regs->gotgctl, otgctl.d32);
  27704. + } else {
  27705. + core_if->otg_sts = val;
  27706. + }
  27707. +}
  27708. +
  27709. +uint32_t dwc_otg_get_gsnpsid(dwc_otg_core_if_t * core_if)
  27710. +{
  27711. + return core_if->snpsid;
  27712. +}
  27713. +
  27714. +uint32_t dwc_otg_get_mode(dwc_otg_core_if_t * core_if)
  27715. +{
  27716. + gintsts_data_t gintsts;
  27717. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  27718. + return gintsts.b.curmode;
  27719. +}
  27720. +
  27721. +uint32_t dwc_otg_get_hnpcapable(dwc_otg_core_if_t * core_if)
  27722. +{
  27723. + gusbcfg_data_t usbcfg;
  27724. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  27725. + return usbcfg.b.hnpcap;
  27726. +}
  27727. +
  27728. +void dwc_otg_set_hnpcapable(dwc_otg_core_if_t * core_if, uint32_t val)
  27729. +{
  27730. + gusbcfg_data_t usbcfg;
  27731. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  27732. + usbcfg.b.hnpcap = val;
  27733. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, usbcfg.d32);
  27734. +}
  27735. +
  27736. +uint32_t dwc_otg_get_srpcapable(dwc_otg_core_if_t * core_if)
  27737. +{
  27738. + gusbcfg_data_t usbcfg;
  27739. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  27740. + return usbcfg.b.srpcap;
  27741. +}
  27742. +
  27743. +void dwc_otg_set_srpcapable(dwc_otg_core_if_t * core_if, uint32_t val)
  27744. +{
  27745. + gusbcfg_data_t usbcfg;
  27746. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  27747. + usbcfg.b.srpcap = val;
  27748. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, usbcfg.d32);
  27749. +}
  27750. +
  27751. +uint32_t dwc_otg_get_devspeed(dwc_otg_core_if_t * core_if)
  27752. +{
  27753. + dcfg_data_t dcfg;
  27754. + /* originally: dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg); */
  27755. +
  27756. + dcfg.d32 = -1; //GRAYG
  27757. + DWC_DEBUGPL(DBG_CILV, "%s - core_if(%p)\n", __func__, core_if);
  27758. + if (NULL == core_if)
  27759. + DWC_ERROR("reg request with NULL core_if\n");
  27760. + DWC_DEBUGPL(DBG_CILV, "%s - core_if(%p)->dev_if(%p)\n", __func__,
  27761. + core_if, core_if->dev_if);
  27762. + if (NULL == core_if->dev_if)
  27763. + DWC_ERROR("reg request with NULL dev_if\n");
  27764. + DWC_DEBUGPL(DBG_CILV, "%s - core_if(%p)->dev_if(%p)->"
  27765. + "dev_global_regs(%p)\n", __func__,
  27766. + core_if, core_if->dev_if,
  27767. + core_if->dev_if->dev_global_regs);
  27768. + if (NULL == core_if->dev_if->dev_global_regs)
  27769. + DWC_ERROR("reg request with NULL dev_global_regs\n");
  27770. + else {
  27771. + DWC_DEBUGPL(DBG_CILV, "%s - &core_if(%p)->dev_if(%p)->"
  27772. + "dev_global_regs(%p)->dcfg = %p\n", __func__,
  27773. + core_if, core_if->dev_if,
  27774. + core_if->dev_if->dev_global_regs,
  27775. + &core_if->dev_if->dev_global_regs->dcfg);
  27776. + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  27777. + }
  27778. + return dcfg.b.devspd;
  27779. +}
  27780. +
  27781. +void dwc_otg_set_devspeed(dwc_otg_core_if_t * core_if, uint32_t val)
  27782. +{
  27783. + dcfg_data_t dcfg;
  27784. + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  27785. + dcfg.b.devspd = val;
  27786. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, dcfg.d32);
  27787. +}
  27788. +
  27789. +uint32_t dwc_otg_get_busconnected(dwc_otg_core_if_t * core_if)
  27790. +{
  27791. + hprt0_data_t hprt0;
  27792. + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
  27793. + return hprt0.b.prtconnsts;
  27794. +}
  27795. +
  27796. +uint32_t dwc_otg_get_enumspeed(dwc_otg_core_if_t * core_if)
  27797. +{
  27798. + dsts_data_t dsts;
  27799. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  27800. + return dsts.b.enumspd;
  27801. +}
  27802. +
  27803. +uint32_t dwc_otg_get_prtpower(dwc_otg_core_if_t * core_if)
  27804. +{
  27805. + hprt0_data_t hprt0;
  27806. + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
  27807. + return hprt0.b.prtpwr;
  27808. +
  27809. +}
  27810. +
  27811. +uint32_t dwc_otg_get_core_state(dwc_otg_core_if_t * core_if)
  27812. +{
  27813. + return core_if->hibernation_suspend;
  27814. +}
  27815. +
  27816. +void dwc_otg_set_prtpower(dwc_otg_core_if_t * core_if, uint32_t val)
  27817. +{
  27818. + hprt0_data_t hprt0;
  27819. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  27820. + hprt0.b.prtpwr = val;
  27821. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  27822. +}
  27823. +
  27824. +uint32_t dwc_otg_get_prtsuspend(dwc_otg_core_if_t * core_if)
  27825. +{
  27826. + hprt0_data_t hprt0;
  27827. + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
  27828. + return hprt0.b.prtsusp;
  27829. +
  27830. +}
  27831. +
  27832. +void dwc_otg_set_prtsuspend(dwc_otg_core_if_t * core_if, uint32_t val)
  27833. +{
  27834. + hprt0_data_t hprt0;
  27835. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  27836. + hprt0.b.prtsusp = val;
  27837. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  27838. +}
  27839. +
  27840. +uint32_t dwc_otg_get_fr_interval(dwc_otg_core_if_t * core_if)
  27841. +{
  27842. + hfir_data_t hfir;
  27843. + hfir.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hfir);
  27844. + return hfir.b.frint;
  27845. +
  27846. +}
  27847. +
  27848. +void dwc_otg_set_fr_interval(dwc_otg_core_if_t * core_if, uint32_t val)
  27849. +{
  27850. + hfir_data_t hfir;
  27851. + uint32_t fram_int;
  27852. + fram_int = calc_frame_interval(core_if);
  27853. + hfir.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hfir);
  27854. + if (!core_if->core_params->reload_ctl) {
  27855. + DWC_WARN("\nCannot reload HFIR register.HFIR.HFIRRldCtrl bit is"
  27856. + "not set to 1.\nShould load driver with reload_ctl=1"
  27857. + " module parameter\n");
  27858. + return;
  27859. + }
  27860. + switch (fram_int) {
  27861. + case 3750:
  27862. + if ((val < 3350) || (val > 4150)) {
  27863. + DWC_WARN("HFIR interval for HS core and 30 MHz"
  27864. + "clock freq should be from 3350 to 4150\n");
  27865. + return;
  27866. + }
  27867. + break;
  27868. + case 30000:
  27869. + if ((val < 26820) || (val > 33180)) {
  27870. + DWC_WARN("HFIR interval for FS/LS core and 30 MHz"
  27871. + "clock freq should be from 26820 to 33180\n");
  27872. + return;
  27873. + }
  27874. + break;
  27875. + case 6000:
  27876. + if ((val < 5360) || (val > 6640)) {
  27877. + DWC_WARN("HFIR interval for HS core and 48 MHz"
  27878. + "clock freq should be from 5360 to 6640\n");
  27879. + return;
  27880. + }
  27881. + break;
  27882. + case 48000:
  27883. + if ((val < 42912) || (val > 53088)) {
  27884. + DWC_WARN("HFIR interval for FS/LS core and 48 MHz"
  27885. + "clock freq should be from 42912 to 53088\n");
  27886. + return;
  27887. + }
  27888. + break;
  27889. + case 7500:
  27890. + if ((val < 6700) || (val > 8300)) {
  27891. + DWC_WARN("HFIR interval for HS core and 60 MHz"
  27892. + "clock freq should be from 6700 to 8300\n");
  27893. + return;
  27894. + }
  27895. + break;
  27896. + case 60000:
  27897. + if ((val < 53640) || (val > 65536)) {
  27898. + DWC_WARN("HFIR interval for FS/LS core and 60 MHz"
  27899. + "clock freq should be from 53640 to 65536\n");
  27900. + return;
  27901. + }
  27902. + break;
  27903. + default:
  27904. + DWC_WARN("Unknown frame interval\n");
  27905. + return;
  27906. + break;
  27907. +
  27908. + }
  27909. + hfir.b.frint = val;
  27910. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hfir, hfir.d32);
  27911. +}
  27912. +
  27913. +uint32_t dwc_otg_get_mode_ch_tim(dwc_otg_core_if_t * core_if)
  27914. +{
  27915. + hcfg_data_t hcfg;
  27916. + hcfg.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
  27917. + return hcfg.b.modechtimen;
  27918. +
  27919. +}
  27920. +
  27921. +void dwc_otg_set_mode_ch_tim(dwc_otg_core_if_t * core_if, uint32_t val)
  27922. +{
  27923. + hcfg_data_t hcfg;
  27924. + hcfg.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
  27925. + hcfg.b.modechtimen = val;
  27926. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg, hcfg.d32);
  27927. +}
  27928. +
  27929. +void dwc_otg_set_prtresume(dwc_otg_core_if_t * core_if, uint32_t val)
  27930. +{
  27931. + hprt0_data_t hprt0;
  27932. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  27933. + hprt0.b.prtres = val;
  27934. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  27935. +}
  27936. +
  27937. +uint32_t dwc_otg_get_remotewakesig(dwc_otg_core_if_t * core_if)
  27938. +{
  27939. + dctl_data_t dctl;
  27940. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
  27941. + return dctl.b.rmtwkupsig;
  27942. +}
  27943. +
  27944. +uint32_t dwc_otg_get_lpm_portsleepstatus(dwc_otg_core_if_t * core_if)
  27945. +{
  27946. + glpmcfg_data_t lpmcfg;
  27947. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  27948. +
  27949. + DWC_ASSERT(!
  27950. + ((core_if->lx_state == DWC_OTG_L1) ^ lpmcfg.b.prt_sleep_sts),
  27951. + "lx_state = %d, lmpcfg.prt_sleep_sts = %d\n",
  27952. + core_if->lx_state, lpmcfg.b.prt_sleep_sts);
  27953. +
  27954. + return lpmcfg.b.prt_sleep_sts;
  27955. +}
  27956. +
  27957. +uint32_t dwc_otg_get_lpm_remotewakeenabled(dwc_otg_core_if_t * core_if)
  27958. +{
  27959. + glpmcfg_data_t lpmcfg;
  27960. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  27961. + return lpmcfg.b.rem_wkup_en;
  27962. +}
  27963. +
  27964. +uint32_t dwc_otg_get_lpmresponse(dwc_otg_core_if_t * core_if)
  27965. +{
  27966. + glpmcfg_data_t lpmcfg;
  27967. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  27968. + return lpmcfg.b.appl_resp;
  27969. +}
  27970. +
  27971. +void dwc_otg_set_lpmresponse(dwc_otg_core_if_t * core_if, uint32_t val)
  27972. +{
  27973. + glpmcfg_data_t lpmcfg;
  27974. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  27975. + lpmcfg.b.appl_resp = val;
  27976. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  27977. +}
  27978. +
  27979. +uint32_t dwc_otg_get_hsic_connect(dwc_otg_core_if_t * core_if)
  27980. +{
  27981. + glpmcfg_data_t lpmcfg;
  27982. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  27983. + return lpmcfg.b.hsic_connect;
  27984. +}
  27985. +
  27986. +void dwc_otg_set_hsic_connect(dwc_otg_core_if_t * core_if, uint32_t val)
  27987. +{
  27988. + glpmcfg_data_t lpmcfg;
  27989. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  27990. + lpmcfg.b.hsic_connect = val;
  27991. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  27992. +}
  27993. +
  27994. +uint32_t dwc_otg_get_inv_sel_hsic(dwc_otg_core_if_t * core_if)
  27995. +{
  27996. + glpmcfg_data_t lpmcfg;
  27997. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  27998. + return lpmcfg.b.inv_sel_hsic;
  27999. +
  28000. +}
  28001. +
  28002. +void dwc_otg_set_inv_sel_hsic(dwc_otg_core_if_t * core_if, uint32_t val)
  28003. +{
  28004. + glpmcfg_data_t lpmcfg;
  28005. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  28006. + lpmcfg.b.inv_sel_hsic = val;
  28007. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  28008. +}
  28009. +
  28010. +uint32_t dwc_otg_get_gotgctl(dwc_otg_core_if_t * core_if)
  28011. +{
  28012. + return DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  28013. +}
  28014. +
  28015. +void dwc_otg_set_gotgctl(dwc_otg_core_if_t * core_if, uint32_t val)
  28016. +{
  28017. + DWC_WRITE_REG32(&core_if->core_global_regs->gotgctl, val);
  28018. +}
  28019. +
  28020. +uint32_t dwc_otg_get_gusbcfg(dwc_otg_core_if_t * core_if)
  28021. +{
  28022. + return DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  28023. +}
  28024. +
  28025. +void dwc_otg_set_gusbcfg(dwc_otg_core_if_t * core_if, uint32_t val)
  28026. +{
  28027. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, val);
  28028. +}
  28029. +
  28030. +uint32_t dwc_otg_get_grxfsiz(dwc_otg_core_if_t * core_if)
  28031. +{
  28032. + return DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
  28033. +}
  28034. +
  28035. +void dwc_otg_set_grxfsiz(dwc_otg_core_if_t * core_if, uint32_t val)
  28036. +{
  28037. + DWC_WRITE_REG32(&core_if->core_global_regs->grxfsiz, val);
  28038. +}
  28039. +
  28040. +uint32_t dwc_otg_get_gnptxfsiz(dwc_otg_core_if_t * core_if)
  28041. +{
  28042. + return DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz);
  28043. +}
  28044. +
  28045. +void dwc_otg_set_gnptxfsiz(dwc_otg_core_if_t * core_if, uint32_t val)
  28046. +{
  28047. + DWC_WRITE_REG32(&core_if->core_global_regs->gnptxfsiz, val);
  28048. +}
  28049. +
  28050. +uint32_t dwc_otg_get_gpvndctl(dwc_otg_core_if_t * core_if)
  28051. +{
  28052. + return DWC_READ_REG32(&core_if->core_global_regs->gpvndctl);
  28053. +}
  28054. +
  28055. +void dwc_otg_set_gpvndctl(dwc_otg_core_if_t * core_if, uint32_t val)
  28056. +{
  28057. + DWC_WRITE_REG32(&core_if->core_global_regs->gpvndctl, val);
  28058. +}
  28059. +
  28060. +uint32_t dwc_otg_get_ggpio(dwc_otg_core_if_t * core_if)
  28061. +{
  28062. + return DWC_READ_REG32(&core_if->core_global_regs->ggpio);
  28063. +}
  28064. +
  28065. +void dwc_otg_set_ggpio(dwc_otg_core_if_t * core_if, uint32_t val)
  28066. +{
  28067. + DWC_WRITE_REG32(&core_if->core_global_regs->ggpio, val);
  28068. +}
  28069. +
  28070. +uint32_t dwc_otg_get_hprt0(dwc_otg_core_if_t * core_if)
  28071. +{
  28072. + return DWC_READ_REG32(core_if->host_if->hprt0);
  28073. +
  28074. +}
  28075. +
  28076. +void dwc_otg_set_hprt0(dwc_otg_core_if_t * core_if, uint32_t val)
  28077. +{
  28078. + DWC_WRITE_REG32(core_if->host_if->hprt0, val);
  28079. +}
  28080. +
  28081. +uint32_t dwc_otg_get_guid(dwc_otg_core_if_t * core_if)
  28082. +{
  28083. + return DWC_READ_REG32(&core_if->core_global_regs->guid);
  28084. +}
  28085. +
  28086. +void dwc_otg_set_guid(dwc_otg_core_if_t * core_if, uint32_t val)
  28087. +{
  28088. + DWC_WRITE_REG32(&core_if->core_global_regs->guid, val);
  28089. +}
  28090. +
  28091. +uint32_t dwc_otg_get_hptxfsiz(dwc_otg_core_if_t * core_if)
  28092. +{
  28093. + return DWC_READ_REG32(&core_if->core_global_regs->hptxfsiz);
  28094. +}
  28095. +
  28096. +uint16_t dwc_otg_get_otg_version(dwc_otg_core_if_t * core_if)
  28097. +{
  28098. + return ((core_if->otg_ver == 1) ? (uint16_t)0x0200 : (uint16_t)0x0103);
  28099. +}
  28100. +
  28101. +/**
  28102. + * Start the SRP timer to detect when the SRP does not complete within
  28103. + * 6 seconds.
  28104. + *
  28105. + * @param core_if the pointer to core_if strucure.
  28106. + */
  28107. +void dwc_otg_pcd_start_srp_timer(dwc_otg_core_if_t * core_if)
  28108. +{
  28109. + core_if->srp_timer_started = 1;
  28110. + DWC_TIMER_SCHEDULE(core_if->srp_timer, 6000 /* 6 secs */ );
  28111. +}
  28112. +
  28113. +void dwc_otg_initiate_srp(dwc_otg_core_if_t * core_if)
  28114. +{
  28115. + uint32_t *addr = (uint32_t *) & (core_if->core_global_regs->gotgctl);
  28116. + gotgctl_data_t mem;
  28117. + gotgctl_data_t val;
  28118. +
  28119. + val.d32 = DWC_READ_REG32(addr);
  28120. + if (val.b.sesreq) {
  28121. + DWC_ERROR("Session Request Already active!\n");
  28122. + return;
  28123. + }
  28124. +
  28125. + DWC_INFO("Session Request Initated\n"); //NOTICE
  28126. + mem.d32 = DWC_READ_REG32(addr);
  28127. + mem.b.sesreq = 1;
  28128. + DWC_WRITE_REG32(addr, mem.d32);
  28129. +
  28130. + /* Start the SRP timer */
  28131. + dwc_otg_pcd_start_srp_timer(core_if);
  28132. + return;
  28133. +}
  28134. --- /dev/null
  28135. +++ b/drivers/usb/host/dwc_otg/dwc_otg_cil.h
  28136. @@ -0,0 +1,1464 @@
  28137. +/* ==========================================================================
  28138. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_cil.h $
  28139. + * $Revision: #123 $
  28140. + * $Date: 2012/08/10 $
  28141. + * $Change: 2047372 $
  28142. + *
  28143. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  28144. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  28145. + * otherwise expressly agreed to in writing between Synopsys and you.
  28146. + *
  28147. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  28148. + * any End User Software License Agreement or Agreement for Licensed Product
  28149. + * with Synopsys or any supplement thereto. You are permitted to use and
  28150. + * redistribute this Software in source and binary forms, with or without
  28151. + * modification, provided that redistributions of source code must retain this
  28152. + * notice. You may not view, use, disclose, copy or distribute this file or
  28153. + * any information contained herein except pursuant to this license grant from
  28154. + * Synopsys. If you do not agree with this notice, including the disclaimer
  28155. + * below, then you are not authorized to use the Software.
  28156. + *
  28157. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  28158. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28159. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28160. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  28161. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  28162. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  28163. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28164. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28165. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28166. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  28167. + * DAMAGE.
  28168. + * ========================================================================== */
  28169. +
  28170. +#if !defined(__DWC_CIL_H__)
  28171. +#define __DWC_CIL_H__
  28172. +
  28173. +#include "dwc_list.h"
  28174. +#include "dwc_otg_dbg.h"
  28175. +#include "dwc_otg_regs.h"
  28176. +
  28177. +#include "dwc_otg_core_if.h"
  28178. +#include "dwc_otg_adp.h"
  28179. +
  28180. +/**
  28181. + * @file
  28182. + * This file contains the interface to the Core Interface Layer.
  28183. + */
  28184. +
  28185. +#ifdef DWC_UTE_CFI
  28186. +
  28187. +#define MAX_DMA_DESCS_PER_EP 256
  28188. +
  28189. +/**
  28190. + * Enumeration for the data buffer mode
  28191. + */
  28192. +typedef enum _data_buffer_mode {
  28193. + BM_STANDARD = 0, /* data buffer is in normal mode */
  28194. + BM_SG = 1, /* data buffer uses the scatter/gather mode */
  28195. + BM_CONCAT = 2, /* data buffer uses the concatenation mode */
  28196. + BM_CIRCULAR = 3, /* data buffer uses the circular DMA mode */
  28197. + BM_ALIGN = 4 /* data buffer is in buffer alignment mode */
  28198. +} data_buffer_mode_e;
  28199. +#endif //DWC_UTE_CFI
  28200. +
  28201. +/** Macros defined for DWC OTG HW Release version */
  28202. +
  28203. +#define OTG_CORE_REV_2_60a 0x4F54260A
  28204. +#define OTG_CORE_REV_2_71a 0x4F54271A
  28205. +#define OTG_CORE_REV_2_72a 0x4F54272A
  28206. +#define OTG_CORE_REV_2_80a 0x4F54280A
  28207. +#define OTG_CORE_REV_2_81a 0x4F54281A
  28208. +#define OTG_CORE_REV_2_90a 0x4F54290A
  28209. +#define OTG_CORE_REV_2_91a 0x4F54291A
  28210. +#define OTG_CORE_REV_2_92a 0x4F54292A
  28211. +#define OTG_CORE_REV_2_93a 0x4F54293A
  28212. +#define OTG_CORE_REV_2_94a 0x4F54294A
  28213. +#define OTG_CORE_REV_3_00a 0x4F54300A
  28214. +
  28215. +/**
  28216. + * Information for each ISOC packet.
  28217. + */
  28218. +typedef struct iso_pkt_info {
  28219. + uint32_t offset;
  28220. + uint32_t length;
  28221. + int32_t status;
  28222. +} iso_pkt_info_t;
  28223. +
  28224. +/**
  28225. + * The <code>dwc_ep</code> structure represents the state of a single
  28226. + * endpoint when acting in device mode. It contains the data items
  28227. + * needed for an endpoint to be activated and transfer packets.
  28228. + */
  28229. +typedef struct dwc_ep {
  28230. + /** EP number used for register address lookup */
  28231. + uint8_t num;
  28232. + /** EP direction 0 = OUT */
  28233. + unsigned is_in:1;
  28234. + /** EP active. */
  28235. + unsigned active:1;
  28236. +
  28237. + /**
  28238. + * Periodic Tx FIFO # for IN EPs For INTR EP set to 0 to use non-periodic
  28239. + * Tx FIFO. If dedicated Tx FIFOs are enabled Tx FIFO # FOR IN EPs*/
  28240. + unsigned tx_fifo_num:4;
  28241. + /** EP type: 0 - Control, 1 - ISOC, 2 - BULK, 3 - INTR */
  28242. + unsigned type:2;
  28243. +#define DWC_OTG_EP_TYPE_CONTROL 0
  28244. +#define DWC_OTG_EP_TYPE_ISOC 1
  28245. +#define DWC_OTG_EP_TYPE_BULK 2
  28246. +#define DWC_OTG_EP_TYPE_INTR 3
  28247. +
  28248. + /** DATA start PID for INTR and BULK EP */
  28249. + unsigned data_pid_start:1;
  28250. + /** Frame (even/odd) for ISOC EP */
  28251. + unsigned even_odd_frame:1;
  28252. + /** Max Packet bytes */
  28253. + unsigned maxpacket:11;
  28254. +
  28255. + /** Max Transfer size */
  28256. + uint32_t maxxfer;
  28257. +
  28258. + /** @name Transfer state */
  28259. + /** @{ */
  28260. +
  28261. + /**
  28262. + * Pointer to the beginning of the transfer buffer -- do not modify
  28263. + * during transfer.
  28264. + */
  28265. +
  28266. + dwc_dma_t dma_addr;
  28267. +
  28268. + dwc_dma_t dma_desc_addr;
  28269. + dwc_otg_dev_dma_desc_t *desc_addr;
  28270. +
  28271. + uint8_t *start_xfer_buff;
  28272. + /** pointer to the transfer buffer */
  28273. + uint8_t *xfer_buff;
  28274. + /** Number of bytes to transfer */
  28275. + unsigned xfer_len:19;
  28276. + /** Number of bytes transferred. */
  28277. + unsigned xfer_count:19;
  28278. + /** Sent ZLP */
  28279. + unsigned sent_zlp:1;
  28280. + /** Total len for control transfer */
  28281. + unsigned total_len:19;
  28282. +
  28283. + /** stall clear flag */
  28284. + unsigned stall_clear_flag:1;
  28285. +
  28286. + /** SETUP pkt cnt rollover flag for EP0 out*/
  28287. + unsigned stp_rollover;
  28288. +
  28289. +#ifdef DWC_UTE_CFI
  28290. + /* The buffer mode */
  28291. + data_buffer_mode_e buff_mode;
  28292. +
  28293. + /* The chain of DMA descriptors.
  28294. + * MAX_DMA_DESCS_PER_EP will be allocated for each active EP.
  28295. + */
  28296. + dwc_otg_dma_desc_t *descs;
  28297. +
  28298. + /* The DMA address of the descriptors chain start */
  28299. + dma_addr_t descs_dma_addr;
  28300. + /** This variable stores the length of the last enqueued request */
  28301. + uint32_t cfi_req_len;
  28302. +#endif //DWC_UTE_CFI
  28303. +
  28304. +/** Max DMA Descriptor count for any EP */
  28305. +#define MAX_DMA_DESC_CNT 256
  28306. + /** Allocated DMA Desc count */
  28307. + uint32_t desc_cnt;
  28308. +
  28309. + /** bInterval */
  28310. + uint32_t bInterval;
  28311. + /** Next frame num to setup next ISOC transfer */
  28312. + uint32_t frame_num;
  28313. + /** Indicates SOF number overrun in DSTS */
  28314. + uint8_t frm_overrun;
  28315. +
  28316. +#ifdef DWC_UTE_PER_IO
  28317. + /** Next frame num for which will be setup DMA Desc */
  28318. + uint32_t xiso_frame_num;
  28319. + /** bInterval */
  28320. + uint32_t xiso_bInterval;
  28321. + /** Count of currently active transfers - shall be either 0 or 1 */
  28322. + int xiso_active_xfers;
  28323. + int xiso_queued_xfers;
  28324. +#endif
  28325. +#ifdef DWC_EN_ISOC
  28326. + /**
  28327. + * Variables specific for ISOC EPs
  28328. + *
  28329. + */
  28330. + /** DMA addresses of ISOC buffers */
  28331. + dwc_dma_t dma_addr0;
  28332. + dwc_dma_t dma_addr1;
  28333. +
  28334. + dwc_dma_t iso_dma_desc_addr;
  28335. + dwc_otg_dev_dma_desc_t *iso_desc_addr;
  28336. +
  28337. + /** pointer to the transfer buffers */
  28338. + uint8_t *xfer_buff0;
  28339. + uint8_t *xfer_buff1;
  28340. +
  28341. + /** number of ISOC Buffer is processing */
  28342. + uint32_t proc_buf_num;
  28343. + /** Interval of ISOC Buffer processing */
  28344. + uint32_t buf_proc_intrvl;
  28345. + /** Data size for regular frame */
  28346. + uint32_t data_per_frame;
  28347. +
  28348. + /* todo - pattern data support is to be implemented in the future */
  28349. + /** Data size for pattern frame */
  28350. + uint32_t data_pattern_frame;
  28351. + /** Frame number of pattern data */
  28352. + uint32_t sync_frame;
  28353. +
  28354. + /** bInterval */
  28355. + uint32_t bInterval;
  28356. + /** ISO Packet number per frame */
  28357. + uint32_t pkt_per_frm;
  28358. + /** Next frame num for which will be setup DMA Desc */
  28359. + uint32_t next_frame;
  28360. + /** Number of packets per buffer processing */
  28361. + uint32_t pkt_cnt;
  28362. + /** Info for all isoc packets */
  28363. + iso_pkt_info_t *pkt_info;
  28364. + /** current pkt number */
  28365. + uint32_t cur_pkt;
  28366. + /** current pkt number */
  28367. + uint8_t *cur_pkt_addr;
  28368. + /** current pkt number */
  28369. + uint32_t cur_pkt_dma_addr;
  28370. +#endif /* DWC_EN_ISOC */
  28371. +
  28372. +/** @} */
  28373. +} dwc_ep_t;
  28374. +
  28375. +/*
  28376. + * Reasons for halting a host channel.
  28377. + */
  28378. +typedef enum dwc_otg_halt_status {
  28379. + DWC_OTG_HC_XFER_NO_HALT_STATUS,
  28380. + DWC_OTG_HC_XFER_COMPLETE,
  28381. + DWC_OTG_HC_XFER_URB_COMPLETE,
  28382. + DWC_OTG_HC_XFER_ACK,
  28383. + DWC_OTG_HC_XFER_NAK,
  28384. + DWC_OTG_HC_XFER_NYET,
  28385. + DWC_OTG_HC_XFER_STALL,
  28386. + DWC_OTG_HC_XFER_XACT_ERR,
  28387. + DWC_OTG_HC_XFER_FRAME_OVERRUN,
  28388. + DWC_OTG_HC_XFER_BABBLE_ERR,
  28389. + DWC_OTG_HC_XFER_DATA_TOGGLE_ERR,
  28390. + DWC_OTG_HC_XFER_AHB_ERR,
  28391. + DWC_OTG_HC_XFER_PERIODIC_INCOMPLETE,
  28392. + DWC_OTG_HC_XFER_URB_DEQUEUE
  28393. +} dwc_otg_halt_status_e;
  28394. +
  28395. +/**
  28396. + * Host channel descriptor. This structure represents the state of a single
  28397. + * host channel when acting in host mode. It contains the data items needed to
  28398. + * transfer packets to an endpoint via a host channel.
  28399. + */
  28400. +typedef struct dwc_hc {
  28401. + /** Host channel number used for register address lookup */
  28402. + uint8_t hc_num;
  28403. +
  28404. + /** Device to access */
  28405. + unsigned dev_addr:7;
  28406. +
  28407. + /** EP to access */
  28408. + unsigned ep_num:4;
  28409. +
  28410. + /** EP direction. 0: OUT, 1: IN */
  28411. + unsigned ep_is_in:1;
  28412. +
  28413. + /**
  28414. + * EP speed.
  28415. + * One of the following values:
  28416. + * - DWC_OTG_EP_SPEED_LOW
  28417. + * - DWC_OTG_EP_SPEED_FULL
  28418. + * - DWC_OTG_EP_SPEED_HIGH
  28419. + */
  28420. + unsigned speed:2;
  28421. +#define DWC_OTG_EP_SPEED_LOW 0
  28422. +#define DWC_OTG_EP_SPEED_FULL 1
  28423. +#define DWC_OTG_EP_SPEED_HIGH 2
  28424. +
  28425. + /**
  28426. + * Endpoint type.
  28427. + * One of the following values:
  28428. + * - DWC_OTG_EP_TYPE_CONTROL: 0
  28429. + * - DWC_OTG_EP_TYPE_ISOC: 1
  28430. + * - DWC_OTG_EP_TYPE_BULK: 2
  28431. + * - DWC_OTG_EP_TYPE_INTR: 3
  28432. + */
  28433. + unsigned ep_type:2;
  28434. +
  28435. + /** Max packet size in bytes */
  28436. + unsigned max_packet:11;
  28437. +
  28438. + /**
  28439. + * PID for initial transaction.
  28440. + * 0: DATA0,<br>
  28441. + * 1: DATA2,<br>
  28442. + * 2: DATA1,<br>
  28443. + * 3: MDATA (non-Control EP),
  28444. + * SETUP (Control EP)
  28445. + */
  28446. + unsigned data_pid_start:2;
  28447. +#define DWC_OTG_HC_PID_DATA0 0
  28448. +#define DWC_OTG_HC_PID_DATA2 1
  28449. +#define DWC_OTG_HC_PID_DATA1 2
  28450. +#define DWC_OTG_HC_PID_MDATA 3
  28451. +#define DWC_OTG_HC_PID_SETUP 3
  28452. +
  28453. + /** Number of periodic transactions per (micro)frame */
  28454. + unsigned multi_count:2;
  28455. +
  28456. + /** @name Transfer State */
  28457. + /** @{ */
  28458. +
  28459. + /** Pointer to the current transfer buffer position. */
  28460. + uint8_t *xfer_buff;
  28461. + /**
  28462. + * In Buffer DMA mode this buffer will be used
  28463. + * if xfer_buff is not DWORD aligned.
  28464. + */
  28465. + dwc_dma_t align_buff;
  28466. + /** Total number of bytes to transfer. */
  28467. + uint32_t xfer_len;
  28468. + /** Number of bytes transferred so far. */
  28469. + uint32_t xfer_count;
  28470. + /** Packet count at start of transfer.*/
  28471. + uint16_t start_pkt_count;
  28472. +
  28473. + /**
  28474. + * Flag to indicate whether the transfer has been started. Set to 1 if
  28475. + * it has been started, 0 otherwise.
  28476. + */
  28477. + uint8_t xfer_started;
  28478. +
  28479. + /**
  28480. + * Set to 1 to indicate that a PING request should be issued on this
  28481. + * channel. If 0, process normally.
  28482. + */
  28483. + uint8_t do_ping;
  28484. +
  28485. + /**
  28486. + * Set to 1 to indicate that the error count for this transaction is
  28487. + * non-zero. Set to 0 if the error count is 0.
  28488. + */
  28489. + uint8_t error_state;
  28490. +
  28491. + /**
  28492. + * Set to 1 to indicate that this channel should be halted the next
  28493. + * time a request is queued for the channel. This is necessary in
  28494. + * slave mode if no request queue space is available when an attempt
  28495. + * is made to halt the channel.
  28496. + */
  28497. + uint8_t halt_on_queue;
  28498. +
  28499. + /**
  28500. + * Set to 1 if the host channel has been halted, but the core is not
  28501. + * finished flushing queued requests. Otherwise 0.
  28502. + */
  28503. + uint8_t halt_pending;
  28504. +
  28505. + /**
  28506. + * Reason for halting the host channel.
  28507. + */
  28508. + dwc_otg_halt_status_e halt_status;
  28509. +
  28510. + /*
  28511. + * Split settings for the host channel
  28512. + */
  28513. + uint8_t do_split; /**< Enable split for the channel */
  28514. + uint8_t complete_split; /**< Enable complete split */
  28515. + uint8_t hub_addr; /**< Address of high speed hub */
  28516. +
  28517. + uint8_t port_addr; /**< Port of the low/full speed device */
  28518. + /** Split transaction position
  28519. + * One of the following values:
  28520. + * - DWC_HCSPLIT_XACTPOS_MID
  28521. + * - DWC_HCSPLIT_XACTPOS_BEGIN
  28522. + * - DWC_HCSPLIT_XACTPOS_END
  28523. + * - DWC_HCSPLIT_XACTPOS_ALL */
  28524. + uint8_t xact_pos;
  28525. +
  28526. + /** Set when the host channel does a short read. */
  28527. + uint8_t short_read;
  28528. +
  28529. + /**
  28530. + * Number of requests issued for this channel since it was assigned to
  28531. + * the current transfer (not counting PINGs).
  28532. + */
  28533. + uint8_t requests;
  28534. +
  28535. + /**
  28536. + * Queue Head for the transfer being processed by this channel.
  28537. + */
  28538. + struct dwc_otg_qh *qh;
  28539. +
  28540. + /** @} */
  28541. +
  28542. + /** Entry in list of host channels. */
  28543. + DWC_CIRCLEQ_ENTRY(dwc_hc) hc_list_entry;
  28544. +
  28545. + /** @name Descriptor DMA support */
  28546. + /** @{ */
  28547. +
  28548. + /** Number of Transfer Descriptors */
  28549. + uint16_t ntd;
  28550. +
  28551. + /** Descriptor List DMA address */
  28552. + dwc_dma_t desc_list_addr;
  28553. +
  28554. + /** Scheduling micro-frame bitmap. */
  28555. + uint8_t schinfo;
  28556. +
  28557. + /** @} */
  28558. +} dwc_hc_t;
  28559. +
  28560. +/**
  28561. + * The following parameters may be specified when starting the module. These
  28562. + * parameters define how the DWC_otg controller should be configured.
  28563. + */
  28564. +typedef struct dwc_otg_core_params {
  28565. + int32_t opt;
  28566. +
  28567. + /**
  28568. + * Specifies the OTG capabilities. The driver will automatically
  28569. + * detect the value for this parameter if none is specified.
  28570. + * 0 - HNP and SRP capable (default)
  28571. + * 1 - SRP Only capable
  28572. + * 2 - No HNP/SRP capable
  28573. + */
  28574. + int32_t otg_cap;
  28575. +
  28576. + /**
  28577. + * Specifies whether to use slave or DMA mode for accessing the data
  28578. + * FIFOs. The driver will automatically detect the value for this
  28579. + * parameter if none is specified.
  28580. + * 0 - Slave
  28581. + * 1 - DMA (default, if available)
  28582. + */
  28583. + int32_t dma_enable;
  28584. +
  28585. + /**
  28586. + * When DMA mode is enabled specifies whether to use address DMA or DMA
  28587. + * Descriptor mode for accessing the data FIFOs in device mode. The driver
  28588. + * will automatically detect the value for this if none is specified.
  28589. + * 0 - address DMA
  28590. + * 1 - DMA Descriptor(default, if available)
  28591. + */
  28592. + int32_t dma_desc_enable;
  28593. + /** The DMA Burst size (applicable only for External DMA
  28594. + * Mode). 1, 4, 8 16, 32, 64, 128, 256 (default 32)
  28595. + */
  28596. + int32_t dma_burst_size; /* Translate this to GAHBCFG values */
  28597. +
  28598. + /**
  28599. + * Specifies the maximum speed of operation in host and device mode.
  28600. + * The actual speed depends on the speed of the attached device and
  28601. + * the value of phy_type. The actual speed depends on the speed of the
  28602. + * attached device.
  28603. + * 0 - High Speed (default)
  28604. + * 1 - Full Speed
  28605. + */
  28606. + int32_t speed;
  28607. + /** Specifies whether low power mode is supported when attached
  28608. + * to a Full Speed or Low Speed device in host mode.
  28609. + * 0 - Don't support low power mode (default)
  28610. + * 1 - Support low power mode
  28611. + */
  28612. + int32_t host_support_fs_ls_low_power;
  28613. +
  28614. + /** Specifies the PHY clock rate in low power mode when connected to a
  28615. + * Low Speed device in host mode. This parameter is applicable only if
  28616. + * HOST_SUPPORT_FS_LS_LOW_POWER is enabled. If PHY_TYPE is set to FS
  28617. + * then defaults to 6 MHZ otherwise 48 MHZ.
  28618. + *
  28619. + * 0 - 48 MHz
  28620. + * 1 - 6 MHz
  28621. + */
  28622. + int32_t host_ls_low_power_phy_clk;
  28623. +
  28624. + /**
  28625. + * 0 - Use cC FIFO size parameters
  28626. + * 1 - Allow dynamic FIFO sizing (default)
  28627. + */
  28628. + int32_t enable_dynamic_fifo;
  28629. +
  28630. + /** Total number of 4-byte words in the data FIFO memory. This
  28631. + * memory includes the Rx FIFO, non-periodic Tx FIFO, and periodic
  28632. + * Tx FIFOs.
  28633. + * 32 to 32768 (default 8192)
  28634. + * Note: The total FIFO memory depth in the FPGA configuration is 8192.
  28635. + */
  28636. + int32_t data_fifo_size;
  28637. +
  28638. + /** Number of 4-byte words in the Rx FIFO in device mode when dynamic
  28639. + * FIFO sizing is enabled.
  28640. + * 16 to 32768 (default 1064)
  28641. + */
  28642. + int32_t dev_rx_fifo_size;
  28643. +
  28644. + /** Number of 4-byte words in the non-periodic Tx FIFO in device mode
  28645. + * when dynamic FIFO sizing is enabled.
  28646. + * 16 to 32768 (default 1024)
  28647. + */
  28648. + int32_t dev_nperio_tx_fifo_size;
  28649. +
  28650. + /** Number of 4-byte words in each of the periodic Tx FIFOs in device
  28651. + * mode when dynamic FIFO sizing is enabled.
  28652. + * 4 to 768 (default 256)
  28653. + */
  28654. + uint32_t dev_perio_tx_fifo_size[MAX_PERIO_FIFOS];
  28655. +
  28656. + /** Number of 4-byte words in the Rx FIFO in host mode when dynamic
  28657. + * FIFO sizing is enabled.
  28658. + * 16 to 32768 (default 1024)
  28659. + */
  28660. + int32_t host_rx_fifo_size;
  28661. +
  28662. + /** Number of 4-byte words in the non-periodic Tx FIFO in host mode
  28663. + * when Dynamic FIFO sizing is enabled in the core.
  28664. + * 16 to 32768 (default 1024)
  28665. + */
  28666. + int32_t host_nperio_tx_fifo_size;
  28667. +
  28668. + /** Number of 4-byte words in the host periodic Tx FIFO when dynamic
  28669. + * FIFO sizing is enabled.
  28670. + * 16 to 32768 (default 1024)
  28671. + */
  28672. + int32_t host_perio_tx_fifo_size;
  28673. +
  28674. + /** The maximum transfer size supported in bytes.
  28675. + * 2047 to 65,535 (default 65,535)
  28676. + */
  28677. + int32_t max_transfer_size;
  28678. +
  28679. + /** The maximum number of packets in a transfer.
  28680. + * 15 to 511 (default 511)
  28681. + */
  28682. + int32_t max_packet_count;
  28683. +
  28684. + /** The number of host channel registers to use.
  28685. + * 1 to 16 (default 12)
  28686. + * Note: The FPGA configuration supports a maximum of 12 host channels.
  28687. + */
  28688. + int32_t host_channels;
  28689. +
  28690. + /** The number of endpoints in addition to EP0 available for device
  28691. + * mode operations.
  28692. + * 1 to 15 (default 6 IN and OUT)
  28693. + * Note: The FPGA configuration supports a maximum of 6 IN and OUT
  28694. + * endpoints in addition to EP0.
  28695. + */
  28696. + int32_t dev_endpoints;
  28697. +
  28698. + /**
  28699. + * Specifies the type of PHY interface to use. By default, the driver
  28700. + * will automatically detect the phy_type.
  28701. + *
  28702. + * 0 - Full Speed PHY
  28703. + * 1 - UTMI+ (default)
  28704. + * 2 - ULPI
  28705. + */
  28706. + int32_t phy_type;
  28707. +
  28708. + /**
  28709. + * Specifies the UTMI+ Data Width. This parameter is
  28710. + * applicable for a PHY_TYPE of UTMI+ or ULPI. (For a ULPI
  28711. + * PHY_TYPE, this parameter indicates the data width between
  28712. + * the MAC and the ULPI Wrapper.) Also, this parameter is
  28713. + * applicable only if the OTG_HSPHY_WIDTH cC parameter was set
  28714. + * to "8 and 16 bits", meaning that the core has been
  28715. + * configured to work at either data path width.
  28716. + *
  28717. + * 8 or 16 bits (default 16)
  28718. + */
  28719. + int32_t phy_utmi_width;
  28720. +
  28721. + /**
  28722. + * Specifies whether the ULPI operates at double or single
  28723. + * data rate. This parameter is only applicable if PHY_TYPE is
  28724. + * ULPI.
  28725. + *
  28726. + * 0 - single data rate ULPI interface with 8 bit wide data
  28727. + * bus (default)
  28728. + * 1 - double data rate ULPI interface with 4 bit wide data
  28729. + * bus
  28730. + */
  28731. + int32_t phy_ulpi_ddr;
  28732. +
  28733. + /**
  28734. + * Specifies whether to use the internal or external supply to
  28735. + * drive the vbus with a ULPI phy.
  28736. + */
  28737. + int32_t phy_ulpi_ext_vbus;
  28738. +
  28739. + /**
  28740. + * Specifies whether to use the I2Cinterface for full speed PHY. This
  28741. + * parameter is only applicable if PHY_TYPE is FS.
  28742. + * 0 - No (default)
  28743. + * 1 - Yes
  28744. + */
  28745. + int32_t i2c_enable;
  28746. +
  28747. + int32_t ulpi_fs_ls;
  28748. +
  28749. + int32_t ts_dline;
  28750. +
  28751. + /**
  28752. + * Specifies whether dedicated transmit FIFOs are
  28753. + * enabled for non periodic IN endpoints in device mode
  28754. + * 0 - No
  28755. + * 1 - Yes
  28756. + */
  28757. + int32_t en_multiple_tx_fifo;
  28758. +
  28759. + /** Number of 4-byte words in each of the Tx FIFOs in device
  28760. + * mode when dynamic FIFO sizing is enabled.
  28761. + * 4 to 768 (default 256)
  28762. + */
  28763. + uint32_t dev_tx_fifo_size[MAX_TX_FIFOS];
  28764. +
  28765. + /** Thresholding enable flag-
  28766. + * bit 0 - enable non-ISO Tx thresholding
  28767. + * bit 1 - enable ISO Tx thresholding
  28768. + * bit 2 - enable Rx thresholding
  28769. + */
  28770. + uint32_t thr_ctl;
  28771. +
  28772. + /** Thresholding length for Tx
  28773. + * FIFOs in 32 bit DWORDs
  28774. + */
  28775. + uint32_t tx_thr_length;
  28776. +
  28777. + /** Thresholding length for Rx
  28778. + * FIFOs in 32 bit DWORDs
  28779. + */
  28780. + uint32_t rx_thr_length;
  28781. +
  28782. + /**
  28783. + * Specifies whether LPM (Link Power Management) support is enabled
  28784. + */
  28785. + int32_t lpm_enable;
  28786. +
  28787. + /** Per Transfer Interrupt
  28788. + * mode enable flag
  28789. + * 1 - Enabled
  28790. + * 0 - Disabled
  28791. + */
  28792. + int32_t pti_enable;
  28793. +
  28794. + /** Multi Processor Interrupt
  28795. + * mode enable flag
  28796. + * 1 - Enabled
  28797. + * 0 - Disabled
  28798. + */
  28799. + int32_t mpi_enable;
  28800. +
  28801. + /** IS_USB Capability
  28802. + * 1 - Enabled
  28803. + * 0 - Disabled
  28804. + */
  28805. + int32_t ic_usb_cap;
  28806. +
  28807. + /** AHB Threshold Ratio
  28808. + * 2'b00 AHB Threshold = MAC Threshold
  28809. + * 2'b01 AHB Threshold = 1/2 MAC Threshold
  28810. + * 2'b10 AHB Threshold = 1/4 MAC Threshold
  28811. + * 2'b11 AHB Threshold = 1/8 MAC Threshold
  28812. + */
  28813. + int32_t ahb_thr_ratio;
  28814. +
  28815. + /** ADP Support
  28816. + * 1 - Enabled
  28817. + * 0 - Disabled
  28818. + */
  28819. + int32_t adp_supp_enable;
  28820. +
  28821. + /** HFIR Reload Control
  28822. + * 0 - The HFIR cannot be reloaded dynamically.
  28823. + * 1 - Allow dynamic reloading of the HFIR register during runtime.
  28824. + */
  28825. + int32_t reload_ctl;
  28826. +
  28827. + /** DCFG: Enable device Out NAK
  28828. + * 0 - The core does not set NAK after Bulk Out transfer complete.
  28829. + * 1 - The core sets NAK after Bulk OUT transfer complete.
  28830. + */
  28831. + int32_t dev_out_nak;
  28832. +
  28833. + /** DCFG: Enable Continue on BNA
  28834. + * After receiving BNA interrupt the core disables the endpoint,when the
  28835. + * endpoint is re-enabled by the application the core starts processing
  28836. + * 0 - from the DOEPDMA descriptor
  28837. + * 1 - from the descriptor which received the BNA.
  28838. + */
  28839. + int32_t cont_on_bna;
  28840. +
  28841. + /** GAHBCFG: AHB Single Support
  28842. + * This bit when programmed supports SINGLE transfers for remainder
  28843. + * data in a transfer for DMA mode of operation.
  28844. + * 0 - in this case the remainder data will be sent using INCR burst size.
  28845. + * 1 - in this case the remainder data will be sent using SINGLE burst size.
  28846. + */
  28847. + int32_t ahb_single;
  28848. +
  28849. + /** Core Power down mode
  28850. + * 0 - No Power Down is enabled
  28851. + * 1 - Reserved
  28852. + * 2 - Complete Power Down (Hibernation)
  28853. + */
  28854. + int32_t power_down;
  28855. +
  28856. + /** OTG revision supported
  28857. + * 0 - OTG 1.3 revision
  28858. + * 1 - OTG 2.0 revision
  28859. + */
  28860. + int32_t otg_ver;
  28861. +
  28862. +} dwc_otg_core_params_t;
  28863. +
  28864. +#ifdef DEBUG
  28865. +struct dwc_otg_core_if;
  28866. +typedef struct hc_xfer_info {
  28867. + struct dwc_otg_core_if *core_if;
  28868. + dwc_hc_t *hc;
  28869. +} hc_xfer_info_t;
  28870. +#endif
  28871. +
  28872. +typedef struct ep_xfer_info {
  28873. + struct dwc_otg_core_if *core_if;
  28874. + dwc_ep_t *ep;
  28875. + uint8_t state;
  28876. +} ep_xfer_info_t;
  28877. +/*
  28878. + * Device States
  28879. + */
  28880. +typedef enum dwc_otg_lx_state {
  28881. + /** On state */
  28882. + DWC_OTG_L0,
  28883. + /** LPM sleep state*/
  28884. + DWC_OTG_L1,
  28885. + /** USB suspend state*/
  28886. + DWC_OTG_L2,
  28887. + /** Off state*/
  28888. + DWC_OTG_L3
  28889. +} dwc_otg_lx_state_e;
  28890. +
  28891. +struct dwc_otg_global_regs_backup {
  28892. + uint32_t gotgctl_local;
  28893. + uint32_t gintmsk_local;
  28894. + uint32_t gahbcfg_local;
  28895. + uint32_t gusbcfg_local;
  28896. + uint32_t grxfsiz_local;
  28897. + uint32_t gnptxfsiz_local;
  28898. +#ifdef CONFIG_USB_DWC_OTG_LPM
  28899. + uint32_t glpmcfg_local;
  28900. +#endif
  28901. + uint32_t gi2cctl_local;
  28902. + uint32_t hptxfsiz_local;
  28903. + uint32_t pcgcctl_local;
  28904. + uint32_t gdfifocfg_local;
  28905. + uint32_t dtxfsiz_local[MAX_EPS_CHANNELS];
  28906. + uint32_t gpwrdn_local;
  28907. + uint32_t xhib_pcgcctl;
  28908. + uint32_t xhib_gpwrdn;
  28909. +};
  28910. +
  28911. +struct dwc_otg_host_regs_backup {
  28912. + uint32_t hcfg_local;
  28913. + uint32_t haintmsk_local;
  28914. + uint32_t hcintmsk_local[MAX_EPS_CHANNELS];
  28915. + uint32_t hprt0_local;
  28916. + uint32_t hfir_local;
  28917. +};
  28918. +
  28919. +struct dwc_otg_dev_regs_backup {
  28920. + uint32_t dcfg;
  28921. + uint32_t dctl;
  28922. + uint32_t daintmsk;
  28923. + uint32_t diepmsk;
  28924. + uint32_t doepmsk;
  28925. + uint32_t diepctl[MAX_EPS_CHANNELS];
  28926. + uint32_t dieptsiz[MAX_EPS_CHANNELS];
  28927. + uint32_t diepdma[MAX_EPS_CHANNELS];
  28928. +};
  28929. +/**
  28930. + * The <code>dwc_otg_core_if</code> structure contains information needed to manage
  28931. + * the DWC_otg controller acting in either host or device mode. It
  28932. + * represents the programming view of the controller as a whole.
  28933. + */
  28934. +struct dwc_otg_core_if {
  28935. + /** Parameters that define how the core should be configured.*/
  28936. + dwc_otg_core_params_t *core_params;
  28937. +
  28938. + /** Core Global registers starting at offset 000h. */
  28939. + dwc_otg_core_global_regs_t *core_global_regs;
  28940. +
  28941. + /** Device-specific information */
  28942. + dwc_otg_dev_if_t *dev_if;
  28943. + /** Host-specific information */
  28944. + dwc_otg_host_if_t *host_if;
  28945. +
  28946. + /** Value from SNPSID register */
  28947. + uint32_t snpsid;
  28948. +
  28949. + /*
  28950. + * Set to 1 if the core PHY interface bits in USBCFG have been
  28951. + * initialized.
  28952. + */
  28953. + uint8_t phy_init_done;
  28954. +
  28955. + /*
  28956. + * SRP Success flag, set by srp success interrupt in FS I2C mode
  28957. + */
  28958. + uint8_t srp_success;
  28959. + uint8_t srp_timer_started;
  28960. + /** Timer for SRP. If it expires before SRP is successful
  28961. + * clear the SRP. */
  28962. + dwc_timer_t *srp_timer;
  28963. +
  28964. +#ifdef DWC_DEV_SRPCAP
  28965. + /* This timer is needed to power on the hibernated host core if SRP is not
  28966. + * initiated on connected SRP capable device for limited period of time
  28967. + */
  28968. + uint8_t pwron_timer_started;
  28969. + dwc_timer_t *pwron_timer;
  28970. +#endif
  28971. + /* Common configuration information */
  28972. + /** Power and Clock Gating Control Register */
  28973. + volatile uint32_t *pcgcctl;
  28974. +#define DWC_OTG_PCGCCTL_OFFSET 0xE00
  28975. +
  28976. + /** Push/pop addresses for endpoints or host channels.*/
  28977. + uint32_t *data_fifo[MAX_EPS_CHANNELS];
  28978. +#define DWC_OTG_DATA_FIFO_OFFSET 0x1000
  28979. +#define DWC_OTG_DATA_FIFO_SIZE 0x1000
  28980. +
  28981. + /** Total RAM for FIFOs (Bytes) */
  28982. + uint16_t total_fifo_size;
  28983. + /** Size of Rx FIFO (Bytes) */
  28984. + uint16_t rx_fifo_size;
  28985. + /** Size of Non-periodic Tx FIFO (Bytes) */
  28986. + uint16_t nperio_tx_fifo_size;
  28987. +
  28988. + /** 1 if DMA is enabled, 0 otherwise. */
  28989. + uint8_t dma_enable;
  28990. +
  28991. + /** 1 if DMA descriptor is enabled, 0 otherwise. */
  28992. + uint8_t dma_desc_enable;
  28993. +
  28994. + /** 1 if PTI Enhancement mode is enabled, 0 otherwise. */
  28995. + uint8_t pti_enh_enable;
  28996. +
  28997. + /** 1 if MPI Enhancement mode is enabled, 0 otherwise. */
  28998. + uint8_t multiproc_int_enable;
  28999. +
  29000. + /** 1 if dedicated Tx FIFOs are enabled, 0 otherwise. */
  29001. + uint8_t en_multiple_tx_fifo;
  29002. +
  29003. + /** Set to 1 if multiple packets of a high-bandwidth transfer is in
  29004. + * process of being queued */
  29005. + uint8_t queuing_high_bandwidth;
  29006. +
  29007. + /** Hardware Configuration -- stored here for convenience.*/
  29008. + hwcfg1_data_t hwcfg1;
  29009. + hwcfg2_data_t hwcfg2;
  29010. + hwcfg3_data_t hwcfg3;
  29011. + hwcfg4_data_t hwcfg4;
  29012. + fifosize_data_t hptxfsiz;
  29013. +
  29014. + /** Host and Device Configuration -- stored here for convenience.*/
  29015. + hcfg_data_t hcfg;
  29016. + dcfg_data_t dcfg;
  29017. +
  29018. + /** The operational State, during transations
  29019. + * (a_host>>a_peripherial and b_device=>b_host) this may not
  29020. + * match the core but allows the software to determine
  29021. + * transitions.
  29022. + */
  29023. + uint8_t op_state;
  29024. +
  29025. + /**
  29026. + * Set to 1 if the HCD needs to be restarted on a session request
  29027. + * interrupt. This is required if no connector ID status change has
  29028. + * occurred since the HCD was last disconnected.
  29029. + */
  29030. + uint8_t restart_hcd_on_session_req;
  29031. +
  29032. + /** HCD callbacks */
  29033. + /** A-Device is a_host */
  29034. +#define A_HOST (1)
  29035. + /** A-Device is a_suspend */
  29036. +#define A_SUSPEND (2)
  29037. + /** A-Device is a_peripherial */
  29038. +#define A_PERIPHERAL (3)
  29039. + /** B-Device is operating as a Peripheral. */
  29040. +#define B_PERIPHERAL (4)
  29041. + /** B-Device is operating as a Host. */
  29042. +#define B_HOST (5)
  29043. +
  29044. + /** HCD callbacks */
  29045. + struct dwc_otg_cil_callbacks *hcd_cb;
  29046. + /** PCD callbacks */
  29047. + struct dwc_otg_cil_callbacks *pcd_cb;
  29048. +
  29049. + /** Device mode Periodic Tx FIFO Mask */
  29050. + uint32_t p_tx_msk;
  29051. + /** Device mode Periodic Tx FIFO Mask */
  29052. + uint32_t tx_msk;
  29053. +
  29054. + /** Workqueue object used for handling several interrupts */
  29055. + dwc_workq_t *wq_otg;
  29056. +
  29057. + /** Timer object used for handling "Wakeup Detected" Interrupt */
  29058. + dwc_timer_t *wkp_timer;
  29059. + /** This arrays used for debug purposes for DEV OUT NAK enhancement */
  29060. + uint32_t start_doeptsiz_val[MAX_EPS_CHANNELS];
  29061. + ep_xfer_info_t ep_xfer_info[MAX_EPS_CHANNELS];
  29062. + dwc_timer_t *ep_xfer_timer[MAX_EPS_CHANNELS];
  29063. +#ifdef DEBUG
  29064. + uint32_t start_hcchar_val[MAX_EPS_CHANNELS];
  29065. +
  29066. + hc_xfer_info_t hc_xfer_info[MAX_EPS_CHANNELS];
  29067. + dwc_timer_t *hc_xfer_timer[MAX_EPS_CHANNELS];
  29068. +
  29069. + uint32_t hfnum_7_samples;
  29070. + uint64_t hfnum_7_frrem_accum;
  29071. + uint32_t hfnum_0_samples;
  29072. + uint64_t hfnum_0_frrem_accum;
  29073. + uint32_t hfnum_other_samples;
  29074. + uint64_t hfnum_other_frrem_accum;
  29075. +#endif
  29076. +
  29077. +#ifdef DWC_UTE_CFI
  29078. + uint16_t pwron_rxfsiz;
  29079. + uint16_t pwron_gnptxfsiz;
  29080. + uint16_t pwron_txfsiz[15];
  29081. +
  29082. + uint16_t init_rxfsiz;
  29083. + uint16_t init_gnptxfsiz;
  29084. + uint16_t init_txfsiz[15];
  29085. +#endif
  29086. +
  29087. + /** Lx state of device */
  29088. + dwc_otg_lx_state_e lx_state;
  29089. +
  29090. + /** Saved Core Global registers */
  29091. + struct dwc_otg_global_regs_backup *gr_backup;
  29092. + /** Saved Host registers */
  29093. + struct dwc_otg_host_regs_backup *hr_backup;
  29094. + /** Saved Device registers */
  29095. + struct dwc_otg_dev_regs_backup *dr_backup;
  29096. +
  29097. + /** Power Down Enable */
  29098. + uint32_t power_down;
  29099. +
  29100. + /** ADP support Enable */
  29101. + uint32_t adp_enable;
  29102. +
  29103. + /** ADP structure object */
  29104. + dwc_otg_adp_t adp;
  29105. +
  29106. + /** hibernation/suspend flag */
  29107. + int hibernation_suspend;
  29108. +
  29109. + /** Device mode extended hibernation flag */
  29110. + int xhib;
  29111. +
  29112. + /** OTG revision supported */
  29113. + uint32_t otg_ver;
  29114. +
  29115. + /** OTG status flag used for HNP polling */
  29116. + uint8_t otg_sts;
  29117. +
  29118. + /** Pointer to either hcd->lock or pcd->lock */
  29119. + dwc_spinlock_t *lock;
  29120. +
  29121. + /** Start predict NextEP based on Learning Queue if equal 1,
  29122. + * also used as counter of disabled NP IN EP's */
  29123. + uint8_t start_predict;
  29124. +
  29125. + /** NextEp sequence, including EP0: nextep_seq[] = EP if non-periodic and
  29126. + * active, 0xff otherwise */
  29127. + uint8_t nextep_seq[MAX_EPS_CHANNELS];
  29128. +
  29129. + /** Index of fisrt EP in nextep_seq array which should be re-enabled **/
  29130. + uint8_t first_in_nextep_seq;
  29131. +
  29132. + /** Frame number while entering to ISR - needed for ISOCs **/
  29133. + uint32_t frame_num;
  29134. +
  29135. +};
  29136. +
  29137. +#ifdef DEBUG
  29138. +/*
  29139. + * This function is called when transfer is timed out.
  29140. + */
  29141. +extern void hc_xfer_timeout(void *ptr);
  29142. +#endif
  29143. +
  29144. +/*
  29145. + * This function is called when transfer is timed out on endpoint.
  29146. + */
  29147. +extern void ep_xfer_timeout(void *ptr);
  29148. +
  29149. +/*
  29150. + * The following functions are functions for works
  29151. + * using during handling some interrupts
  29152. + */
  29153. +extern void w_conn_id_status_change(void *p);
  29154. +
  29155. +extern void w_wakeup_detected(void *p);
  29156. +
  29157. +/** Saves global register values into system memory. */
  29158. +extern int dwc_otg_save_global_regs(dwc_otg_core_if_t * core_if);
  29159. +/** Saves device register values into system memory. */
  29160. +extern int dwc_otg_save_dev_regs(dwc_otg_core_if_t * core_if);
  29161. +/** Saves host register values into system memory. */
  29162. +extern int dwc_otg_save_host_regs(dwc_otg_core_if_t * core_if);
  29163. +/** Restore global register values. */
  29164. +extern int dwc_otg_restore_global_regs(dwc_otg_core_if_t * core_if);
  29165. +/** Restore host register values. */
  29166. +extern int dwc_otg_restore_host_regs(dwc_otg_core_if_t * core_if, int reset);
  29167. +/** Restore device register values. */
  29168. +extern int dwc_otg_restore_dev_regs(dwc_otg_core_if_t * core_if,
  29169. + int rem_wakeup);
  29170. +extern int restore_lpm_i2c_regs(dwc_otg_core_if_t * core_if);
  29171. +extern int restore_essential_regs(dwc_otg_core_if_t * core_if, int rmode,
  29172. + int is_host);
  29173. +
  29174. +extern int dwc_otg_host_hibernation_restore(dwc_otg_core_if_t * core_if,
  29175. + int restore_mode, int reset);
  29176. +extern int dwc_otg_device_hibernation_restore(dwc_otg_core_if_t * core_if,
  29177. + int rem_wakeup, int reset);
  29178. +
  29179. +/*
  29180. + * The following functions support initialization of the CIL driver component
  29181. + * and the DWC_otg controller.
  29182. + */
  29183. +extern void dwc_otg_core_host_init(dwc_otg_core_if_t * _core_if);
  29184. +extern void dwc_otg_core_dev_init(dwc_otg_core_if_t * _core_if);
  29185. +
  29186. +/** @name Device CIL Functions
  29187. + * The following functions support managing the DWC_otg controller in device
  29188. + * mode.
  29189. + */
  29190. +/**@{*/
  29191. +extern void dwc_otg_wakeup(dwc_otg_core_if_t * _core_if);
  29192. +extern void dwc_otg_read_setup_packet(dwc_otg_core_if_t * _core_if,
  29193. + uint32_t * _dest);
  29194. +extern uint32_t dwc_otg_get_frame_number(dwc_otg_core_if_t * _core_if);
  29195. +extern void dwc_otg_ep0_activate(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
  29196. +extern void dwc_otg_ep_activate(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
  29197. +extern void dwc_otg_ep_deactivate(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
  29198. +extern void dwc_otg_ep_start_transfer(dwc_otg_core_if_t * _core_if,
  29199. + dwc_ep_t * _ep);
  29200. +extern void dwc_otg_ep_start_zl_transfer(dwc_otg_core_if_t * _core_if,
  29201. + dwc_ep_t * _ep);
  29202. +extern void dwc_otg_ep0_start_transfer(dwc_otg_core_if_t * _core_if,
  29203. + dwc_ep_t * _ep);
  29204. +extern void dwc_otg_ep0_continue_transfer(dwc_otg_core_if_t * _core_if,
  29205. + dwc_ep_t * _ep);
  29206. +extern void dwc_otg_ep_write_packet(dwc_otg_core_if_t * _core_if,
  29207. + dwc_ep_t * _ep, int _dma);
  29208. +extern void dwc_otg_ep_set_stall(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
  29209. +extern void dwc_otg_ep_clear_stall(dwc_otg_core_if_t * _core_if,
  29210. + dwc_ep_t * _ep);
  29211. +extern void dwc_otg_enable_device_interrupts(dwc_otg_core_if_t * _core_if);
  29212. +
  29213. +#ifdef DWC_EN_ISOC
  29214. +extern void dwc_otg_iso_ep_start_frm_transfer(dwc_otg_core_if_t * core_if,
  29215. + dwc_ep_t * ep);
  29216. +extern void dwc_otg_iso_ep_start_buf_transfer(dwc_otg_core_if_t * core_if,
  29217. + dwc_ep_t * ep);
  29218. +#endif /* DWC_EN_ISOC */
  29219. +/**@}*/
  29220. +
  29221. +/** @name Host CIL Functions
  29222. + * The following functions support managing the DWC_otg controller in host
  29223. + * mode.
  29224. + */
  29225. +/**@{*/
  29226. +extern void dwc_otg_hc_init(dwc_otg_core_if_t * _core_if, dwc_hc_t * _hc);
  29227. +extern void dwc_otg_hc_halt(dwc_otg_core_if_t * _core_if,
  29228. + dwc_hc_t * _hc, dwc_otg_halt_status_e _halt_status);
  29229. +extern void dwc_otg_hc_cleanup(dwc_otg_core_if_t * _core_if, dwc_hc_t * _hc);
  29230. +extern void dwc_otg_hc_start_transfer(dwc_otg_core_if_t * _core_if,
  29231. + dwc_hc_t * _hc);
  29232. +extern int dwc_otg_hc_continue_transfer(dwc_otg_core_if_t * _core_if,
  29233. + dwc_hc_t * _hc);
  29234. +extern void dwc_otg_hc_do_ping(dwc_otg_core_if_t * _core_if, dwc_hc_t * _hc);
  29235. +extern void dwc_otg_hc_write_packet(dwc_otg_core_if_t * _core_if,
  29236. + dwc_hc_t * _hc);
  29237. +extern void dwc_otg_enable_host_interrupts(dwc_otg_core_if_t * _core_if);
  29238. +extern void dwc_otg_disable_host_interrupts(dwc_otg_core_if_t * _core_if);
  29239. +
  29240. +extern void dwc_otg_hc_start_transfer_ddma(dwc_otg_core_if_t * core_if,
  29241. + dwc_hc_t * hc);
  29242. +
  29243. +extern uint32_t calc_frame_interval(dwc_otg_core_if_t * core_if);
  29244. +
  29245. +/* Macro used to clear one channel interrupt */
  29246. +#define clear_hc_int(_hc_regs_, _intr_) \
  29247. +do { \
  29248. + hcint_data_t hcint_clear = {.d32 = 0}; \
  29249. + hcint_clear.b._intr_ = 1; \
  29250. + DWC_WRITE_REG32(&(_hc_regs_)->hcint, hcint_clear.d32); \
  29251. +} while (0)
  29252. +
  29253. +/*
  29254. + * Macro used to disable one channel interrupt. Channel interrupts are
  29255. + * disabled when the channel is halted or released by the interrupt handler.
  29256. + * There is no need to handle further interrupts of that type until the
  29257. + * channel is re-assigned. In fact, subsequent handling may cause crashes
  29258. + * because the channel structures are cleaned up when the channel is released.
  29259. + */
  29260. +#define disable_hc_int(_hc_regs_, _intr_) \
  29261. +do { \
  29262. + hcintmsk_data_t hcintmsk = {.d32 = 0}; \
  29263. + hcintmsk.b._intr_ = 1; \
  29264. + DWC_MODIFY_REG32(&(_hc_regs_)->hcintmsk, hcintmsk.d32, 0); \
  29265. +} while (0)
  29266. +
  29267. +/**
  29268. + * This function Reads HPRT0 in preparation to modify. It keeps the
  29269. + * WC bits 0 so that if they are read as 1, they won't clear when you
  29270. + * write it back
  29271. + */
  29272. +static inline uint32_t dwc_otg_read_hprt0(dwc_otg_core_if_t * _core_if)
  29273. +{
  29274. + hprt0_data_t hprt0;
  29275. + hprt0.d32 = DWC_READ_REG32(_core_if->host_if->hprt0);
  29276. + hprt0.b.prtena = 0;
  29277. + hprt0.b.prtconndet = 0;
  29278. + hprt0.b.prtenchng = 0;
  29279. + hprt0.b.prtovrcurrchng = 0;
  29280. + return hprt0.d32;
  29281. +}
  29282. +
  29283. +/**@}*/
  29284. +
  29285. +/** @name Common CIL Functions
  29286. + * The following functions support managing the DWC_otg controller in either
  29287. + * device or host mode.
  29288. + */
  29289. +/**@{*/
  29290. +
  29291. +extern void dwc_otg_read_packet(dwc_otg_core_if_t * core_if,
  29292. + uint8_t * dest, uint16_t bytes);
  29293. +
  29294. +extern void dwc_otg_flush_tx_fifo(dwc_otg_core_if_t * _core_if, const int _num);
  29295. +extern void dwc_otg_flush_rx_fifo(dwc_otg_core_if_t * _core_if);
  29296. +extern void dwc_otg_core_reset(dwc_otg_core_if_t * _core_if);
  29297. +
  29298. +/**
  29299. + * This function returns the Core Interrupt register.
  29300. + */
  29301. +static inline uint32_t dwc_otg_read_core_intr(dwc_otg_core_if_t * core_if)
  29302. +{
  29303. + return (DWC_READ_REG32(&core_if->core_global_regs->gintsts) &
  29304. + DWC_READ_REG32(&core_if->core_global_regs->gintmsk));
  29305. +}
  29306. +
  29307. +/**
  29308. + * This function returns the OTG Interrupt register.
  29309. + */
  29310. +static inline uint32_t dwc_otg_read_otg_intr(dwc_otg_core_if_t * core_if)
  29311. +{
  29312. + return (DWC_READ_REG32(&core_if->core_global_regs->gotgint));
  29313. +}
  29314. +
  29315. +/**
  29316. + * This function reads the Device All Endpoints Interrupt register and
  29317. + * returns the IN endpoint interrupt bits.
  29318. + */
  29319. +static inline uint32_t dwc_otg_read_dev_all_in_ep_intr(dwc_otg_core_if_t *
  29320. + core_if)
  29321. +{
  29322. +
  29323. + uint32_t v;
  29324. +
  29325. + if (core_if->multiproc_int_enable) {
  29326. + v = DWC_READ_REG32(&core_if->dev_if->
  29327. + dev_global_regs->deachint) &
  29328. + DWC_READ_REG32(&core_if->
  29329. + dev_if->dev_global_regs->deachintmsk);
  29330. + } else {
  29331. + v = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daint) &
  29332. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daintmsk);
  29333. + }
  29334. + return (v & 0xffff);
  29335. +}
  29336. +
  29337. +/**
  29338. + * This function reads the Device All Endpoints Interrupt register and
  29339. + * returns the OUT endpoint interrupt bits.
  29340. + */
  29341. +static inline uint32_t dwc_otg_read_dev_all_out_ep_intr(dwc_otg_core_if_t *
  29342. + core_if)
  29343. +{
  29344. + uint32_t v;
  29345. +
  29346. + if (core_if->multiproc_int_enable) {
  29347. + v = DWC_READ_REG32(&core_if->dev_if->
  29348. + dev_global_regs->deachint) &
  29349. + DWC_READ_REG32(&core_if->
  29350. + dev_if->dev_global_regs->deachintmsk);
  29351. + } else {
  29352. + v = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daint) &
  29353. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daintmsk);
  29354. + }
  29355. +
  29356. + return ((v & 0xffff0000) >> 16);
  29357. +}
  29358. +
  29359. +/**
  29360. + * This function returns the Device IN EP Interrupt register
  29361. + */
  29362. +static inline uint32_t dwc_otg_read_dev_in_ep_intr(dwc_otg_core_if_t * core_if,
  29363. + dwc_ep_t * ep)
  29364. +{
  29365. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  29366. + uint32_t v, msk, emp;
  29367. +
  29368. + if (core_if->multiproc_int_enable) {
  29369. + msk =
  29370. + DWC_READ_REG32(&dev_if->
  29371. + dev_global_regs->diepeachintmsk[ep->num]);
  29372. + emp =
  29373. + DWC_READ_REG32(&dev_if->
  29374. + dev_global_regs->dtknqr4_fifoemptymsk);
  29375. + msk |= ((emp >> ep->num) & 0x1) << 7;
  29376. + v = DWC_READ_REG32(&dev_if->in_ep_regs[ep->num]->diepint) & msk;
  29377. + } else {
  29378. + msk = DWC_READ_REG32(&dev_if->dev_global_regs->diepmsk);
  29379. + emp =
  29380. + DWC_READ_REG32(&dev_if->
  29381. + dev_global_regs->dtknqr4_fifoemptymsk);
  29382. + msk |= ((emp >> ep->num) & 0x1) << 7;
  29383. + v = DWC_READ_REG32(&dev_if->in_ep_regs[ep->num]->diepint) & msk;
  29384. + }
  29385. +
  29386. + return v;
  29387. +}
  29388. +
  29389. +/**
  29390. + * This function returns the Device OUT EP Interrupt register
  29391. + */
  29392. +static inline uint32_t dwc_otg_read_dev_out_ep_intr(dwc_otg_core_if_t *
  29393. + _core_if, dwc_ep_t * _ep)
  29394. +{
  29395. + dwc_otg_dev_if_t *dev_if = _core_if->dev_if;
  29396. + uint32_t v;
  29397. + doepmsk_data_t msk = {.d32 = 0 };
  29398. +
  29399. + if (_core_if->multiproc_int_enable) {
  29400. + msk.d32 =
  29401. + DWC_READ_REG32(&dev_if->
  29402. + dev_global_regs->doepeachintmsk[_ep->num]);
  29403. + if (_core_if->pti_enh_enable) {
  29404. + msk.b.pktdrpsts = 1;
  29405. + }
  29406. + v = DWC_READ_REG32(&dev_if->
  29407. + out_ep_regs[_ep->num]->doepint) & msk.d32;
  29408. + } else {
  29409. + msk.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->doepmsk);
  29410. + if (_core_if->pti_enh_enable) {
  29411. + msk.b.pktdrpsts = 1;
  29412. + }
  29413. + v = DWC_READ_REG32(&dev_if->
  29414. + out_ep_regs[_ep->num]->doepint) & msk.d32;
  29415. + }
  29416. + return v;
  29417. +}
  29418. +
  29419. +/**
  29420. + * This function returns the Host All Channel Interrupt register
  29421. + */
  29422. +static inline uint32_t dwc_otg_read_host_all_channels_intr(dwc_otg_core_if_t *
  29423. + _core_if)
  29424. +{
  29425. + return (DWC_READ_REG32(&_core_if->host_if->host_global_regs->haint));
  29426. +}
  29427. +
  29428. +static inline uint32_t dwc_otg_read_host_channel_intr(dwc_otg_core_if_t *
  29429. + _core_if, dwc_hc_t * _hc)
  29430. +{
  29431. + return (DWC_READ_REG32
  29432. + (&_core_if->host_if->hc_regs[_hc->hc_num]->hcint));
  29433. +}
  29434. +
  29435. +/**
  29436. + * This function returns the mode of the operation, host or device.
  29437. + *
  29438. + * @return 0 - Device Mode, 1 - Host Mode
  29439. + */
  29440. +static inline uint32_t dwc_otg_mode(dwc_otg_core_if_t * _core_if)
  29441. +{
  29442. + return (DWC_READ_REG32(&_core_if->core_global_regs->gintsts) & 0x1);
  29443. +}
  29444. +
  29445. +/**@}*/
  29446. +
  29447. +/**
  29448. + * DWC_otg CIL callback structure. This structure allows the HCD and
  29449. + * PCD to register functions used for starting and stopping the PCD
  29450. + * and HCD for role change on for a DRD.
  29451. + */
  29452. +typedef struct dwc_otg_cil_callbacks {
  29453. + /** Start function for role change */
  29454. + int (*start) (void *_p);
  29455. + /** Stop Function for role change */
  29456. + int (*stop) (void *_p);
  29457. + /** Disconnect Function for role change */
  29458. + int (*disconnect) (void *_p);
  29459. + /** Resume/Remote wakeup Function */
  29460. + int (*resume_wakeup) (void *_p);
  29461. + /** Suspend function */
  29462. + int (*suspend) (void *_p);
  29463. + /** Session Start (SRP) */
  29464. + int (*session_start) (void *_p);
  29465. +#ifdef CONFIG_USB_DWC_OTG_LPM
  29466. + /** Sleep (switch to L0 state) */
  29467. + int (*sleep) (void *_p);
  29468. +#endif
  29469. + /** Pointer passed to start() and stop() */
  29470. + void *p;
  29471. +} dwc_otg_cil_callbacks_t;
  29472. +
  29473. +extern void dwc_otg_cil_register_pcd_callbacks(dwc_otg_core_if_t * _core_if,
  29474. + dwc_otg_cil_callbacks_t * _cb,
  29475. + void *_p);
  29476. +extern void dwc_otg_cil_register_hcd_callbacks(dwc_otg_core_if_t * _core_if,
  29477. + dwc_otg_cil_callbacks_t * _cb,
  29478. + void *_p);
  29479. +
  29480. +void dwc_otg_initiate_srp(dwc_otg_core_if_t * core_if);
  29481. +
  29482. +//////////////////////////////////////////////////////////////////////
  29483. +/** Start the HCD. Helper function for using the HCD callbacks.
  29484. + *
  29485. + * @param core_if Programming view of DWC_otg controller.
  29486. + */
  29487. +static inline void cil_hcd_start(dwc_otg_core_if_t * core_if)
  29488. +{
  29489. + if (core_if->hcd_cb && core_if->hcd_cb->start) {
  29490. + core_if->hcd_cb->start(core_if->hcd_cb->p);
  29491. + }
  29492. +}
  29493. +
  29494. +/** Stop the HCD. Helper function for using the HCD callbacks.
  29495. + *
  29496. + * @param core_if Programming view of DWC_otg controller.
  29497. + */
  29498. +static inline void cil_hcd_stop(dwc_otg_core_if_t * core_if)
  29499. +{
  29500. + if (core_if->hcd_cb && core_if->hcd_cb->stop) {
  29501. + core_if->hcd_cb->stop(core_if->hcd_cb->p);
  29502. + }
  29503. +}
  29504. +
  29505. +/** Disconnect the HCD. Helper function for using the HCD callbacks.
  29506. + *
  29507. + * @param core_if Programming view of DWC_otg controller.
  29508. + */
  29509. +static inline void cil_hcd_disconnect(dwc_otg_core_if_t * core_if)
  29510. +{
  29511. + if (core_if->hcd_cb && core_if->hcd_cb->disconnect) {
  29512. + core_if->hcd_cb->disconnect(core_if->hcd_cb->p);
  29513. + }
  29514. +}
  29515. +
  29516. +/** Inform the HCD the a New Session has begun. Helper function for
  29517. + * using the HCD callbacks.
  29518. + *
  29519. + * @param core_if Programming view of DWC_otg controller.
  29520. + */
  29521. +static inline void cil_hcd_session_start(dwc_otg_core_if_t * core_if)
  29522. +{
  29523. + if (core_if->hcd_cb && core_if->hcd_cb->session_start) {
  29524. + core_if->hcd_cb->session_start(core_if->hcd_cb->p);
  29525. + }
  29526. +}
  29527. +
  29528. +#ifdef CONFIG_USB_DWC_OTG_LPM
  29529. +/**
  29530. + * Inform the HCD about LPM sleep.
  29531. + * Helper function for using the HCD callbacks.
  29532. + *
  29533. + * @param core_if Programming view of DWC_otg controller.
  29534. + */
  29535. +static inline void cil_hcd_sleep(dwc_otg_core_if_t * core_if)
  29536. +{
  29537. + if (core_if->hcd_cb && core_if->hcd_cb->sleep) {
  29538. + core_if->hcd_cb->sleep(core_if->hcd_cb->p);
  29539. + }
  29540. +}
  29541. +#endif
  29542. +
  29543. +/** Resume the HCD. Helper function for using the HCD callbacks.
  29544. + *
  29545. + * @param core_if Programming view of DWC_otg controller.
  29546. + */
  29547. +static inline void cil_hcd_resume(dwc_otg_core_if_t * core_if)
  29548. +{
  29549. + if (core_if->hcd_cb && core_if->hcd_cb->resume_wakeup) {
  29550. + core_if->hcd_cb->resume_wakeup(core_if->hcd_cb->p);
  29551. + }
  29552. +}
  29553. +
  29554. +/** Start the PCD. Helper function for using the PCD callbacks.
  29555. + *
  29556. + * @param core_if Programming view of DWC_otg controller.
  29557. + */
  29558. +static inline void cil_pcd_start(dwc_otg_core_if_t * core_if)
  29559. +{
  29560. + if (core_if->pcd_cb && core_if->pcd_cb->start) {
  29561. + core_if->pcd_cb->start(core_if->pcd_cb->p);
  29562. + }
  29563. +}
  29564. +
  29565. +/** Stop the PCD. Helper function for using the PCD callbacks.
  29566. + *
  29567. + * @param core_if Programming view of DWC_otg controller.
  29568. + */
  29569. +static inline void cil_pcd_stop(dwc_otg_core_if_t * core_if)
  29570. +{
  29571. + if (core_if->pcd_cb && core_if->pcd_cb->stop) {
  29572. + core_if->pcd_cb->stop(core_if->pcd_cb->p);
  29573. + }
  29574. +}
  29575. +
  29576. +/** Suspend the PCD. Helper function for using the PCD callbacks.
  29577. + *
  29578. + * @param core_if Programming view of DWC_otg controller.
  29579. + */
  29580. +static inline void cil_pcd_suspend(dwc_otg_core_if_t * core_if)
  29581. +{
  29582. + if (core_if->pcd_cb && core_if->pcd_cb->suspend) {
  29583. + core_if->pcd_cb->suspend(core_if->pcd_cb->p);
  29584. + }
  29585. +}
  29586. +
  29587. +/** Resume the PCD. Helper function for using the PCD callbacks.
  29588. + *
  29589. + * @param core_if Programming view of DWC_otg controller.
  29590. + */
  29591. +static inline void cil_pcd_resume(dwc_otg_core_if_t * core_if)
  29592. +{
  29593. + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
  29594. + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
  29595. + }
  29596. +}
  29597. +
  29598. +//////////////////////////////////////////////////////////////////////
  29599. +
  29600. +#endif
  29601. --- /dev/null
  29602. +++ b/drivers/usb/host/dwc_otg/dwc_otg_cil_intr.c
  29603. @@ -0,0 +1,1601 @@
  29604. +/* ==========================================================================
  29605. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_cil_intr.c $
  29606. + * $Revision: #32 $
  29607. + * $Date: 2012/08/10 $
  29608. + * $Change: 2047372 $
  29609. + *
  29610. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  29611. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  29612. + * otherwise expressly agreed to in writing between Synopsys and you.
  29613. + *
  29614. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  29615. + * any End User Software License Agreement or Agreement for Licensed Product
  29616. + * with Synopsys or any supplement thereto. You are permitted to use and
  29617. + * redistribute this Software in source and binary forms, with or without
  29618. + * modification, provided that redistributions of source code must retain this
  29619. + * notice. You may not view, use, disclose, copy or distribute this file or
  29620. + * any information contained herein except pursuant to this license grant from
  29621. + * Synopsys. If you do not agree with this notice, including the disclaimer
  29622. + * below, then you are not authorized to use the Software.
  29623. + *
  29624. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  29625. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  29626. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29627. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  29628. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  29629. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  29630. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  29631. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29632. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29633. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  29634. + * DAMAGE.
  29635. + * ========================================================================== */
  29636. +
  29637. +/** @file
  29638. + *
  29639. + * The Core Interface Layer provides basic services for accessing and
  29640. + * managing the DWC_otg hardware. These services are used by both the
  29641. + * Host Controller Driver and the Peripheral Controller Driver.
  29642. + *
  29643. + * This file contains the Common Interrupt handlers.
  29644. + */
  29645. +#include "dwc_os.h"
  29646. +#include "dwc_otg_regs.h"
  29647. +#include "dwc_otg_cil.h"
  29648. +#include "dwc_otg_driver.h"
  29649. +#include "dwc_otg_pcd.h"
  29650. +#include "dwc_otg_hcd.h"
  29651. +
  29652. +#ifdef DEBUG
  29653. +inline const char *op_state_str(dwc_otg_core_if_t * core_if)
  29654. +{
  29655. + return (core_if->op_state == A_HOST ? "a_host" :
  29656. + (core_if->op_state == A_SUSPEND ? "a_suspend" :
  29657. + (core_if->op_state == A_PERIPHERAL ? "a_peripheral" :
  29658. + (core_if->op_state == B_PERIPHERAL ? "b_peripheral" :
  29659. + (core_if->op_state == B_HOST ? "b_host" : "unknown")))));
  29660. +}
  29661. +#endif
  29662. +
  29663. +/** This function will log a debug message
  29664. + *
  29665. + * @param core_if Programming view of DWC_otg controller.
  29666. + */
  29667. +int32_t dwc_otg_handle_mode_mismatch_intr(dwc_otg_core_if_t * core_if)
  29668. +{
  29669. + gintsts_data_t gintsts;
  29670. + DWC_WARN("Mode Mismatch Interrupt: currently in %s mode\n",
  29671. + dwc_otg_mode(core_if) ? "Host" : "Device");
  29672. +
  29673. + /* Clear interrupt */
  29674. + gintsts.d32 = 0;
  29675. + gintsts.b.modemismatch = 1;
  29676. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  29677. + return 1;
  29678. +}
  29679. +
  29680. +/**
  29681. + * This function handles the OTG Interrupts. It reads the OTG
  29682. + * Interrupt Register (GOTGINT) to determine what interrupt has
  29683. + * occurred.
  29684. + *
  29685. + * @param core_if Programming view of DWC_otg controller.
  29686. + */
  29687. +int32_t dwc_otg_handle_otg_intr(dwc_otg_core_if_t * core_if)
  29688. +{
  29689. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  29690. + gotgint_data_t gotgint;
  29691. + gotgctl_data_t gotgctl;
  29692. + gintmsk_data_t gintmsk;
  29693. + gpwrdn_data_t gpwrdn;
  29694. +
  29695. + gotgint.d32 = DWC_READ_REG32(&global_regs->gotgint);
  29696. + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
  29697. + DWC_DEBUGPL(DBG_CIL, "++OTG Interrupt gotgint=%0x [%s]\n", gotgint.d32,
  29698. + op_state_str(core_if));
  29699. +
  29700. + if (gotgint.b.sesenddet) {
  29701. + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
  29702. + "Session End Detected++ (%s)\n",
  29703. + op_state_str(core_if));
  29704. + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
  29705. +
  29706. + if (core_if->op_state == B_HOST) {
  29707. + cil_pcd_start(core_if);
  29708. + core_if->op_state = B_PERIPHERAL;
  29709. + } else {
  29710. + /* If not B_HOST and Device HNP still set. HNP
  29711. + * Did not succeed!*/
  29712. + if (gotgctl.b.devhnpen) {
  29713. + DWC_DEBUGPL(DBG_ANY, "Session End Detected\n");
  29714. + __DWC_ERROR("Device Not Connected/Responding!\n");
  29715. + }
  29716. +
  29717. + /* If Session End Detected the B-Cable has
  29718. + * been disconnected. */
  29719. + /* Reset PCD and Gadget driver to a
  29720. + * clean state. */
  29721. + core_if->lx_state = DWC_OTG_L0;
  29722. + DWC_SPINUNLOCK(core_if->lock);
  29723. + cil_pcd_stop(core_if);
  29724. + DWC_SPINLOCK(core_if->lock);
  29725. +
  29726. + if (core_if->adp_enable) {
  29727. + if (core_if->power_down == 2) {
  29728. + gpwrdn.d32 = 0;
  29729. + gpwrdn.b.pwrdnswtch = 1;
  29730. + DWC_MODIFY_REG32(&core_if->
  29731. + core_global_regs->
  29732. + gpwrdn, gpwrdn.d32, 0);
  29733. + }
  29734. +
  29735. + gpwrdn.d32 = 0;
  29736. + gpwrdn.b.pmuintsel = 1;
  29737. + gpwrdn.b.pmuactv = 1;
  29738. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  29739. + gpwrdn, 0, gpwrdn.d32);
  29740. +
  29741. + dwc_otg_adp_sense_start(core_if);
  29742. + }
  29743. + }
  29744. +
  29745. + gotgctl.d32 = 0;
  29746. + gotgctl.b.devhnpen = 1;
  29747. + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
  29748. + }
  29749. + if (gotgint.b.sesreqsucstschng) {
  29750. + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
  29751. + "Session Reqeust Success Status Change++\n");
  29752. + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
  29753. + if (gotgctl.b.sesreqscs) {
  29754. +
  29755. + if ((core_if->core_params->phy_type ==
  29756. + DWC_PHY_TYPE_PARAM_FS) && (core_if->core_params->i2c_enable)) {
  29757. + core_if->srp_success = 1;
  29758. + } else {
  29759. + DWC_SPINUNLOCK(core_if->lock);
  29760. + cil_pcd_resume(core_if);
  29761. + DWC_SPINLOCK(core_if->lock);
  29762. + /* Clear Session Request */
  29763. + gotgctl.d32 = 0;
  29764. + gotgctl.b.sesreq = 1;
  29765. + DWC_MODIFY_REG32(&global_regs->gotgctl,
  29766. + gotgctl.d32, 0);
  29767. + }
  29768. + }
  29769. + }
  29770. + if (gotgint.b.hstnegsucstschng) {
  29771. + /* Print statements during the HNP interrupt handling
  29772. + * can cause it to fail.*/
  29773. + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
  29774. + /* WA for 3.00a- HW is not setting cur_mode, even sometimes
  29775. + * this does not help*/
  29776. + if (core_if->snpsid >= OTG_CORE_REV_3_00a)
  29777. + dwc_udelay(100);
  29778. + if (gotgctl.b.hstnegscs) {
  29779. + if (dwc_otg_is_host_mode(core_if)) {
  29780. + core_if->op_state = B_HOST;
  29781. + /*
  29782. + * Need to disable SOF interrupt immediately.
  29783. + * When switching from device to host, the PCD
  29784. + * interrupt handler won't handle the
  29785. + * interrupt if host mode is already set. The
  29786. + * HCD interrupt handler won't get called if
  29787. + * the HCD state is HALT. This means that the
  29788. + * interrupt does not get handled and Linux
  29789. + * complains loudly.
  29790. + */
  29791. + gintmsk.d32 = 0;
  29792. + gintmsk.b.sofintr = 1;
  29793. + DWC_MODIFY_REG32(&global_regs->gintmsk,
  29794. + gintmsk.d32, 0);
  29795. + /* Call callback function with spin lock released */
  29796. + DWC_SPINUNLOCK(core_if->lock);
  29797. + cil_pcd_stop(core_if);
  29798. + /*
  29799. + * Initialize the Core for Host mode.
  29800. + */
  29801. + cil_hcd_start(core_if);
  29802. + DWC_SPINLOCK(core_if->lock);
  29803. + core_if->op_state = B_HOST;
  29804. + }
  29805. + } else {
  29806. + gotgctl.d32 = 0;
  29807. + gotgctl.b.hnpreq = 1;
  29808. + gotgctl.b.devhnpen = 1;
  29809. + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
  29810. + DWC_DEBUGPL(DBG_ANY, "HNP Failed\n");
  29811. + __DWC_ERROR("Device Not Connected/Responding\n");
  29812. + }
  29813. + }
  29814. + if (gotgint.b.hstnegdet) {
  29815. + /* The disconnect interrupt is set at the same time as
  29816. + * Host Negotiation Detected. During the mode
  29817. + * switch all interrupts are cleared so the disconnect
  29818. + * interrupt handler will not get executed.
  29819. + */
  29820. + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
  29821. + "Host Negotiation Detected++ (%s)\n",
  29822. + (dwc_otg_is_host_mode(core_if) ? "Host" :
  29823. + "Device"));
  29824. + if (dwc_otg_is_device_mode(core_if)) {
  29825. + DWC_DEBUGPL(DBG_ANY, "a_suspend->a_peripheral (%d)\n",
  29826. + core_if->op_state);
  29827. + DWC_SPINUNLOCK(core_if->lock);
  29828. + cil_hcd_disconnect(core_if);
  29829. + cil_pcd_start(core_if);
  29830. + DWC_SPINLOCK(core_if->lock);
  29831. + core_if->op_state = A_PERIPHERAL;
  29832. + } else {
  29833. + /*
  29834. + * Need to disable SOF interrupt immediately. When
  29835. + * switching from device to host, the PCD interrupt
  29836. + * handler won't handle the interrupt if host mode is
  29837. + * already set. The HCD interrupt handler won't get
  29838. + * called if the HCD state is HALT. This means that
  29839. + * the interrupt does not get handled and Linux
  29840. + * complains loudly.
  29841. + */
  29842. + gintmsk.d32 = 0;
  29843. + gintmsk.b.sofintr = 1;
  29844. + DWC_MODIFY_REG32(&global_regs->gintmsk, gintmsk.d32, 0);
  29845. + DWC_SPINUNLOCK(core_if->lock);
  29846. + cil_pcd_stop(core_if);
  29847. + cil_hcd_start(core_if);
  29848. + DWC_SPINLOCK(core_if->lock);
  29849. + core_if->op_state = A_HOST;
  29850. + }
  29851. + }
  29852. + if (gotgint.b.adevtoutchng) {
  29853. + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
  29854. + "A-Device Timeout Change++\n");
  29855. + }
  29856. + if (gotgint.b.debdone) {
  29857. + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: " "Debounce Done++\n");
  29858. + }
  29859. +
  29860. + /* Clear GOTGINT */
  29861. + DWC_WRITE_REG32(&core_if->core_global_regs->gotgint, gotgint.d32);
  29862. +
  29863. + return 1;
  29864. +}
  29865. +
  29866. +void w_conn_id_status_change(void *p)
  29867. +{
  29868. + dwc_otg_core_if_t *core_if = p;
  29869. + uint32_t count = 0;
  29870. + gotgctl_data_t gotgctl = {.d32 = 0 };
  29871. +
  29872. + gotgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  29873. + DWC_DEBUGPL(DBG_CIL, "gotgctl=%0x\n", gotgctl.d32);
  29874. + DWC_DEBUGPL(DBG_CIL, "gotgctl.b.conidsts=%d\n", gotgctl.b.conidsts);
  29875. +
  29876. + /* B-Device connector (Device Mode) */
  29877. + if (gotgctl.b.conidsts) {
  29878. + /* Wait for switch to device mode. */
  29879. + while (!dwc_otg_is_device_mode(core_if)) {
  29880. + DWC_PRINTF("Waiting for Peripheral Mode, Mode=%s\n",
  29881. + (dwc_otg_is_host_mode(core_if) ? "Host" :
  29882. + "Peripheral"));
  29883. + dwc_mdelay(100);
  29884. + if (++count > 10000)
  29885. + break;
  29886. + }
  29887. + DWC_ASSERT(++count < 10000,
  29888. + "Connection id status change timed out");
  29889. + core_if->op_state = B_PERIPHERAL;
  29890. + dwc_otg_core_init(core_if);
  29891. + dwc_otg_enable_global_interrupts(core_if);
  29892. + cil_pcd_start(core_if);
  29893. + } else {
  29894. + /* A-Device connector (Host Mode) */
  29895. + while (!dwc_otg_is_host_mode(core_if)) {
  29896. + DWC_PRINTF("Waiting for Host Mode, Mode=%s\n",
  29897. + (dwc_otg_is_host_mode(core_if) ? "Host" :
  29898. + "Peripheral"));
  29899. + dwc_mdelay(100);
  29900. + if (++count > 10000)
  29901. + break;
  29902. + }
  29903. + DWC_ASSERT(++count < 10000,
  29904. + "Connection id status change timed out");
  29905. + core_if->op_state = A_HOST;
  29906. + /*
  29907. + * Initialize the Core for Host mode.
  29908. + */
  29909. + dwc_otg_core_init(core_if);
  29910. + dwc_otg_enable_global_interrupts(core_if);
  29911. + cil_hcd_start(core_if);
  29912. + }
  29913. +}
  29914. +
  29915. +/**
  29916. + * This function handles the Connector ID Status Change Interrupt. It
  29917. + * reads the OTG Interrupt Register (GOTCTL) to determine whether this
  29918. + * is a Device to Host Mode transition or a Host Mode to Device
  29919. + * Transition.
  29920. + *
  29921. + * This only occurs when the cable is connected/removed from the PHY
  29922. + * connector.
  29923. + *
  29924. + * @param core_if Programming view of DWC_otg controller.
  29925. + */
  29926. +int32_t dwc_otg_handle_conn_id_status_change_intr(dwc_otg_core_if_t * core_if)
  29927. +{
  29928. +
  29929. + /*
  29930. + * Need to disable SOF interrupt immediately. If switching from device
  29931. + * to host, the PCD interrupt handler won't handle the interrupt if
  29932. + * host mode is already set. The HCD interrupt handler won't get
  29933. + * called if the HCD state is HALT. This means that the interrupt does
  29934. + * not get handled and Linux complains loudly.
  29935. + */
  29936. + gintmsk_data_t gintmsk = {.d32 = 0 };
  29937. + gintsts_data_t gintsts = {.d32 = 0 };
  29938. +
  29939. + gintmsk.b.sofintr = 1;
  29940. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32, 0);
  29941. +
  29942. + DWC_DEBUGPL(DBG_CIL,
  29943. + " ++Connector ID Status Change Interrupt++ (%s)\n",
  29944. + (dwc_otg_is_host_mode(core_if) ? "Host" : "Device"));
  29945. +
  29946. + DWC_SPINUNLOCK(core_if->lock);
  29947. +
  29948. + /*
  29949. + * Need to schedule a work, as there are possible DELAY function calls
  29950. + * Release lock before scheduling workq as it holds spinlock during scheduling
  29951. + */
  29952. +
  29953. + DWC_WORKQ_SCHEDULE(core_if->wq_otg, w_conn_id_status_change,
  29954. + core_if, "connection id status change");
  29955. + DWC_SPINLOCK(core_if->lock);
  29956. +
  29957. + /* Set flag and clear interrupt */
  29958. + gintsts.b.conidstschng = 1;
  29959. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  29960. +
  29961. + return 1;
  29962. +}
  29963. +
  29964. +/**
  29965. + * This interrupt indicates that a device is initiating the Session
  29966. + * Request Protocol to request the host to turn on bus power so a new
  29967. + * session can begin. The handler responds by turning on bus power. If
  29968. + * the DWC_otg controller is in low power mode, the handler brings the
  29969. + * controller out of low power mode before turning on bus power.
  29970. + *
  29971. + * @param core_if Programming view of DWC_otg controller.
  29972. + */
  29973. +int32_t dwc_otg_handle_session_req_intr(dwc_otg_core_if_t * core_if)
  29974. +{
  29975. + gintsts_data_t gintsts;
  29976. +
  29977. +#ifndef DWC_HOST_ONLY
  29978. + DWC_DEBUGPL(DBG_ANY, "++Session Request Interrupt++\n");
  29979. +
  29980. + if (dwc_otg_is_device_mode(core_if)) {
  29981. + DWC_PRINTF("SRP: Device mode\n");
  29982. + } else {
  29983. + hprt0_data_t hprt0;
  29984. + DWC_PRINTF("SRP: Host mode\n");
  29985. +
  29986. + /* Turn on the port power bit. */
  29987. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  29988. + hprt0.b.prtpwr = 1;
  29989. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  29990. +
  29991. + /* Start the Connection timer. So a message can be displayed
  29992. + * if connect does not occur within 10 seconds. */
  29993. + cil_hcd_session_start(core_if);
  29994. + }
  29995. +#endif
  29996. +
  29997. + /* Clear interrupt */
  29998. + gintsts.d32 = 0;
  29999. + gintsts.b.sessreqintr = 1;
  30000. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  30001. +
  30002. + return 1;
  30003. +}
  30004. +
  30005. +void w_wakeup_detected(void *p)
  30006. +{
  30007. + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) p;
  30008. + /*
  30009. + * Clear the Resume after 70ms. (Need 20 ms minimum. Use 70 ms
  30010. + * so that OPT tests pass with all PHYs).
  30011. + */
  30012. + hprt0_data_t hprt0 = {.d32 = 0 };
  30013. +#if 0
  30014. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  30015. + /* Restart the Phy Clock */
  30016. + pcgcctl.b.stoppclk = 1;
  30017. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  30018. + dwc_udelay(10);
  30019. +#endif //0
  30020. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  30021. + DWC_DEBUGPL(DBG_ANY, "Resume: HPRT0=%0x\n", hprt0.d32);
  30022. +// dwc_mdelay(70);
  30023. + hprt0.b.prtres = 0; /* Resume */
  30024. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  30025. + DWC_DEBUGPL(DBG_ANY, "Clear Resume: HPRT0=%0x\n",
  30026. + DWC_READ_REG32(core_if->host_if->hprt0));
  30027. +
  30028. + cil_hcd_resume(core_if);
  30029. +
  30030. + /** Change to L0 state*/
  30031. + core_if->lx_state = DWC_OTG_L0;
  30032. +}
  30033. +
  30034. +/**
  30035. + * This interrupt indicates that the DWC_otg controller has detected a
  30036. + * resume or remote wakeup sequence. If the DWC_otg controller is in
  30037. + * low power mode, the handler must brings the controller out of low
  30038. + * power mode. The controller automatically begins resume
  30039. + * signaling. The handler schedules a time to stop resume signaling.
  30040. + */
  30041. +int32_t dwc_otg_handle_wakeup_detected_intr(dwc_otg_core_if_t * core_if)
  30042. +{
  30043. + gintsts_data_t gintsts;
  30044. +
  30045. + DWC_DEBUGPL(DBG_ANY,
  30046. + "++Resume and Remote Wakeup Detected Interrupt++\n");
  30047. +
  30048. + DWC_PRINTF("%s lxstate = %d\n", __func__, core_if->lx_state);
  30049. +
  30050. + if (dwc_otg_is_device_mode(core_if)) {
  30051. + dctl_data_t dctl = {.d32 = 0 };
  30052. + DWC_DEBUGPL(DBG_PCD, "DSTS=0x%0x\n",
  30053. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->
  30054. + dsts));
  30055. + if (core_if->lx_state == DWC_OTG_L2) {
  30056. +#ifdef PARTIAL_POWER_DOWN
  30057. + if (core_if->hwcfg4.b.power_optimiz) {
  30058. + pcgcctl_data_t power = {.d32 = 0 };
  30059. +
  30060. + power.d32 = DWC_READ_REG32(core_if->pcgcctl);
  30061. + DWC_DEBUGPL(DBG_CIL, "PCGCCTL=%0x\n",
  30062. + power.d32);
  30063. +
  30064. + power.b.stoppclk = 0;
  30065. + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
  30066. +
  30067. + power.b.pwrclmp = 0;
  30068. + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
  30069. +
  30070. + power.b.rstpdwnmodule = 0;
  30071. + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
  30072. + }
  30073. +#endif
  30074. + /* Clear the Remote Wakeup Signaling */
  30075. + dctl.b.rmtwkupsig = 1;
  30076. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  30077. + dctl, dctl.d32, 0);
  30078. +
  30079. + DWC_SPINUNLOCK(core_if->lock);
  30080. + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
  30081. + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
  30082. + }
  30083. + DWC_SPINLOCK(core_if->lock);
  30084. + } else {
  30085. + glpmcfg_data_t lpmcfg;
  30086. + lpmcfg.d32 =
  30087. + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  30088. + lpmcfg.b.hird_thres &= (~(1 << 4));
  30089. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg,
  30090. + lpmcfg.d32);
  30091. + }
  30092. + /** Change to L0 state*/
  30093. + core_if->lx_state = DWC_OTG_L0;
  30094. + } else {
  30095. + if (core_if->lx_state != DWC_OTG_L1) {
  30096. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  30097. +
  30098. + /* Restart the Phy Clock */
  30099. + pcgcctl.b.stoppclk = 1;
  30100. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  30101. + DWC_TIMER_SCHEDULE(core_if->wkp_timer, 71);
  30102. + } else {
  30103. + /** Change to L0 state*/
  30104. + core_if->lx_state = DWC_OTG_L0;
  30105. + }
  30106. + }
  30107. +
  30108. + /* Clear interrupt */
  30109. + gintsts.d32 = 0;
  30110. + gintsts.b.wkupintr = 1;
  30111. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  30112. +
  30113. + return 1;
  30114. +}
  30115. +
  30116. +/**
  30117. + * This interrupt indicates that the Wakeup Logic has detected a
  30118. + * Device disconnect.
  30119. + */
  30120. +static int32_t dwc_otg_handle_pwrdn_disconnect_intr(dwc_otg_core_if_t *core_if)
  30121. +{
  30122. + gpwrdn_data_t gpwrdn = { .d32 = 0 };
  30123. + gpwrdn_data_t gpwrdn_temp = { .d32 = 0 };
  30124. + gpwrdn_temp.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  30125. +
  30126. + DWC_PRINTF("%s called\n", __FUNCTION__);
  30127. +
  30128. + if (!core_if->hibernation_suspend) {
  30129. + DWC_PRINTF("Already exited from Hibernation\n");
  30130. + return 1;
  30131. + }
  30132. +
  30133. + /* Switch on the voltage to the core */
  30134. + gpwrdn.b.pwrdnswtch = 1;
  30135. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30136. + dwc_udelay(10);
  30137. +
  30138. + /* Reset the core */
  30139. + gpwrdn.d32 = 0;
  30140. + gpwrdn.b.pwrdnrstn = 1;
  30141. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30142. + dwc_udelay(10);
  30143. +
  30144. + /* Disable power clamps*/
  30145. + gpwrdn.d32 = 0;
  30146. + gpwrdn.b.pwrdnclmp = 1;
  30147. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30148. +
  30149. + /* Remove reset the core signal */
  30150. + gpwrdn.d32 = 0;
  30151. + gpwrdn.b.pwrdnrstn = 1;
  30152. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  30153. + dwc_udelay(10);
  30154. +
  30155. + /* Disable PMU interrupt */
  30156. + gpwrdn.d32 = 0;
  30157. + gpwrdn.b.pmuintsel = 1;
  30158. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30159. +
  30160. + core_if->hibernation_suspend = 0;
  30161. +
  30162. + /* Disable PMU */
  30163. + gpwrdn.d32 = 0;
  30164. + gpwrdn.b.pmuactv = 1;
  30165. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30166. + dwc_udelay(10);
  30167. +
  30168. + if (gpwrdn_temp.b.idsts) {
  30169. + core_if->op_state = B_PERIPHERAL;
  30170. + dwc_otg_core_init(core_if);
  30171. + dwc_otg_enable_global_interrupts(core_if);
  30172. + cil_pcd_start(core_if);
  30173. + } else {
  30174. + core_if->op_state = A_HOST;
  30175. + dwc_otg_core_init(core_if);
  30176. + dwc_otg_enable_global_interrupts(core_if);
  30177. + cil_hcd_start(core_if);
  30178. + }
  30179. +
  30180. + return 1;
  30181. +}
  30182. +
  30183. +/**
  30184. + * This interrupt indicates that the Wakeup Logic has detected a
  30185. + * remote wakeup sequence.
  30186. + */
  30187. +static int32_t dwc_otg_handle_pwrdn_wakeup_detected_intr(dwc_otg_core_if_t * core_if)
  30188. +{
  30189. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  30190. + DWC_DEBUGPL(DBG_ANY,
  30191. + "++Powerdown Remote Wakeup Detected Interrupt++\n");
  30192. +
  30193. + if (!core_if->hibernation_suspend) {
  30194. + DWC_PRINTF("Already exited from Hibernation\n");
  30195. + return 1;
  30196. + }
  30197. +
  30198. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  30199. + if (gpwrdn.b.idsts) { // Device Mode
  30200. + if ((core_if->power_down == 2)
  30201. + && (core_if->hibernation_suspend == 1)) {
  30202. + dwc_otg_device_hibernation_restore(core_if, 0, 0);
  30203. + }
  30204. + } else {
  30205. + if ((core_if->power_down == 2)
  30206. + && (core_if->hibernation_suspend == 1)) {
  30207. + dwc_otg_host_hibernation_restore(core_if, 1, 0);
  30208. + }
  30209. + }
  30210. + return 1;
  30211. +}
  30212. +
  30213. +static int32_t dwc_otg_handle_pwrdn_idsts_change(dwc_otg_device_t *otg_dev)
  30214. +{
  30215. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  30216. + gpwrdn_data_t gpwrdn_temp = {.d32 = 0 };
  30217. + dwc_otg_core_if_t *core_if = otg_dev->core_if;
  30218. +
  30219. + DWC_DEBUGPL(DBG_ANY, "%s called\n", __FUNCTION__);
  30220. + gpwrdn_temp.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  30221. + if (core_if->power_down == 2) {
  30222. + if (!core_if->hibernation_suspend) {
  30223. + DWC_PRINTF("Already exited from Hibernation\n");
  30224. + return 1;
  30225. + }
  30226. + DWC_DEBUGPL(DBG_ANY, "Exit from hibernation on ID sts change\n");
  30227. + /* Switch on the voltage to the core */
  30228. + gpwrdn.b.pwrdnswtch = 1;
  30229. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30230. + dwc_udelay(10);
  30231. +
  30232. + /* Reset the core */
  30233. + gpwrdn.d32 = 0;
  30234. + gpwrdn.b.pwrdnrstn = 1;
  30235. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30236. + dwc_udelay(10);
  30237. +
  30238. + /* Disable power clamps */
  30239. + gpwrdn.d32 = 0;
  30240. + gpwrdn.b.pwrdnclmp = 1;
  30241. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30242. +
  30243. + /* Remove reset the core signal */
  30244. + gpwrdn.d32 = 0;
  30245. + gpwrdn.b.pwrdnrstn = 1;
  30246. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  30247. + dwc_udelay(10);
  30248. +
  30249. + /* Disable PMU interrupt */
  30250. + gpwrdn.d32 = 0;
  30251. + gpwrdn.b.pmuintsel = 1;
  30252. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30253. +
  30254. + /*Indicates that we are exiting from hibernation */
  30255. + core_if->hibernation_suspend = 0;
  30256. +
  30257. + /* Disable PMU */
  30258. + gpwrdn.d32 = 0;
  30259. + gpwrdn.b.pmuactv = 1;
  30260. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30261. + dwc_udelay(10);
  30262. +
  30263. + gpwrdn.d32 = core_if->gr_backup->gpwrdn_local;
  30264. + if (gpwrdn.b.dis_vbus == 1) {
  30265. + gpwrdn.d32 = 0;
  30266. + gpwrdn.b.dis_vbus = 1;
  30267. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30268. + }
  30269. +
  30270. + if (gpwrdn_temp.b.idsts) {
  30271. + core_if->op_state = B_PERIPHERAL;
  30272. + dwc_otg_core_init(core_if);
  30273. + dwc_otg_enable_global_interrupts(core_if);
  30274. + cil_pcd_start(core_if);
  30275. + } else {
  30276. + core_if->op_state = A_HOST;
  30277. + dwc_otg_core_init(core_if);
  30278. + dwc_otg_enable_global_interrupts(core_if);
  30279. + cil_hcd_start(core_if);
  30280. + }
  30281. + }
  30282. +
  30283. + if (core_if->adp_enable) {
  30284. + uint8_t is_host = 0;
  30285. + DWC_SPINUNLOCK(core_if->lock);
  30286. + /* Change the core_if's lock to hcd/pcd lock depend on mode? */
  30287. +#ifndef DWC_HOST_ONLY
  30288. + if (gpwrdn_temp.b.idsts)
  30289. + core_if->lock = otg_dev->pcd->lock;
  30290. +#endif
  30291. +#ifndef DWC_DEVICE_ONLY
  30292. + if (!gpwrdn_temp.b.idsts) {
  30293. + core_if->lock = otg_dev->hcd->lock;
  30294. + is_host = 1;
  30295. + }
  30296. +#endif
  30297. + DWC_PRINTF("RESTART ADP\n");
  30298. + if (core_if->adp.probe_enabled)
  30299. + dwc_otg_adp_probe_stop(core_if);
  30300. + if (core_if->adp.sense_enabled)
  30301. + dwc_otg_adp_sense_stop(core_if);
  30302. + if (core_if->adp.sense_timer_started)
  30303. + DWC_TIMER_CANCEL(core_if->adp.sense_timer);
  30304. + if (core_if->adp.vbuson_timer_started)
  30305. + DWC_TIMER_CANCEL(core_if->adp.vbuson_timer);
  30306. + core_if->adp.probe_timer_values[0] = -1;
  30307. + core_if->adp.probe_timer_values[1] = -1;
  30308. + core_if->adp.sense_timer_started = 0;
  30309. + core_if->adp.vbuson_timer_started = 0;
  30310. + core_if->adp.probe_counter = 0;
  30311. + core_if->adp.gpwrdn = 0;
  30312. +
  30313. + /* Disable PMU and restart ADP */
  30314. + gpwrdn_temp.d32 = 0;
  30315. + gpwrdn_temp.b.pmuactv = 1;
  30316. + gpwrdn_temp.b.pmuintsel = 1;
  30317. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30318. + DWC_PRINTF("Check point 1\n");
  30319. + dwc_mdelay(110);
  30320. + dwc_otg_adp_start(core_if, is_host);
  30321. + DWC_SPINLOCK(core_if->lock);
  30322. + }
  30323. +
  30324. +
  30325. + return 1;
  30326. +}
  30327. +
  30328. +static int32_t dwc_otg_handle_pwrdn_session_change(dwc_otg_core_if_t * core_if)
  30329. +{
  30330. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  30331. + int32_t otg_cap_param = core_if->core_params->otg_cap;
  30332. + DWC_DEBUGPL(DBG_ANY, "%s called\n", __FUNCTION__);
  30333. +
  30334. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  30335. + if (core_if->power_down == 2) {
  30336. + if (!core_if->hibernation_suspend) {
  30337. + DWC_PRINTF("Already exited from Hibernation\n");
  30338. + return 1;
  30339. + }
  30340. +
  30341. + if ((otg_cap_param != DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE ||
  30342. + otg_cap_param != DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE) &&
  30343. + gpwrdn.b.bsessvld == 0) {
  30344. + /* Save gpwrdn register for further usage if stschng interrupt */
  30345. + core_if->gr_backup->gpwrdn_local =
  30346. + DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  30347. + /*Exit from ISR and wait for stschng interrupt with bsessvld = 1 */
  30348. + return 1;
  30349. + }
  30350. +
  30351. + /* Switch on the voltage to the core */
  30352. + gpwrdn.d32 = 0;
  30353. + gpwrdn.b.pwrdnswtch = 1;
  30354. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30355. + dwc_udelay(10);
  30356. +
  30357. + /* Reset the core */
  30358. + gpwrdn.d32 = 0;
  30359. + gpwrdn.b.pwrdnrstn = 1;
  30360. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30361. + dwc_udelay(10);
  30362. +
  30363. + /* Disable power clamps */
  30364. + gpwrdn.d32 = 0;
  30365. + gpwrdn.b.pwrdnclmp = 1;
  30366. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30367. +
  30368. + /* Remove reset the core signal */
  30369. + gpwrdn.d32 = 0;
  30370. + gpwrdn.b.pwrdnrstn = 1;
  30371. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  30372. + dwc_udelay(10);
  30373. +
  30374. + /* Disable PMU interrupt */
  30375. + gpwrdn.d32 = 0;
  30376. + gpwrdn.b.pmuintsel = 1;
  30377. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30378. + dwc_udelay(10);
  30379. +
  30380. + /*Indicates that we are exiting from hibernation */
  30381. + core_if->hibernation_suspend = 0;
  30382. +
  30383. + /* Disable PMU */
  30384. + gpwrdn.d32 = 0;
  30385. + gpwrdn.b.pmuactv = 1;
  30386. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30387. + dwc_udelay(10);
  30388. +
  30389. + core_if->op_state = B_PERIPHERAL;
  30390. + dwc_otg_core_init(core_if);
  30391. + dwc_otg_enable_global_interrupts(core_if);
  30392. + cil_pcd_start(core_if);
  30393. +
  30394. + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE ||
  30395. + otg_cap_param == DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE) {
  30396. + /*
  30397. + * Initiate SRP after initial ADP probe.
  30398. + */
  30399. + dwc_otg_initiate_srp(core_if);
  30400. + }
  30401. + }
  30402. +
  30403. + return 1;
  30404. +}
  30405. +/**
  30406. + * This interrupt indicates that the Wakeup Logic has detected a
  30407. + * status change either on IDDIG or BSessVld.
  30408. + */
  30409. +static uint32_t dwc_otg_handle_pwrdn_stschng_intr(dwc_otg_device_t *otg_dev)
  30410. +{
  30411. + uint32_t retval = 0;
  30412. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  30413. + gpwrdn_data_t gpwrdn_temp = {.d32 = 0 };
  30414. + dwc_otg_core_if_t *core_if = otg_dev->core_if;
  30415. +
  30416. + DWC_PRINTF("%s called\n", __FUNCTION__);
  30417. +
  30418. + if (core_if->power_down == 2) {
  30419. + if (core_if->hibernation_suspend <= 0) {
  30420. + DWC_PRINTF("Already exited from Hibernation\n");
  30421. + return 1;
  30422. + } else
  30423. + gpwrdn_temp.d32 = core_if->gr_backup->gpwrdn_local;
  30424. +
  30425. + } else {
  30426. + gpwrdn_temp.d32 = core_if->adp.gpwrdn;
  30427. + }
  30428. +
  30429. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  30430. +
  30431. + if (gpwrdn.b.idsts ^ gpwrdn_temp.b.idsts) {
  30432. + retval = dwc_otg_handle_pwrdn_idsts_change(otg_dev);
  30433. + } else if (gpwrdn.b.bsessvld ^ gpwrdn_temp.b.bsessvld) {
  30434. + retval = dwc_otg_handle_pwrdn_session_change(core_if);
  30435. + }
  30436. +
  30437. + return retval;
  30438. +}
  30439. +
  30440. +/**
  30441. + * This interrupt indicates that the Wakeup Logic has detected a
  30442. + * SRP.
  30443. + */
  30444. +static int32_t dwc_otg_handle_pwrdn_srp_intr(dwc_otg_core_if_t * core_if)
  30445. +{
  30446. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  30447. +
  30448. + DWC_PRINTF("%s called\n", __FUNCTION__);
  30449. +
  30450. + if (!core_if->hibernation_suspend) {
  30451. + DWC_PRINTF("Already exited from Hibernation\n");
  30452. + return 1;
  30453. + }
  30454. +#ifdef DWC_DEV_SRPCAP
  30455. + if (core_if->pwron_timer_started) {
  30456. + core_if->pwron_timer_started = 0;
  30457. + DWC_TIMER_CANCEL(core_if->pwron_timer);
  30458. + }
  30459. +#endif
  30460. +
  30461. + /* Switch on the voltage to the core */
  30462. + gpwrdn.b.pwrdnswtch = 1;
  30463. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30464. + dwc_udelay(10);
  30465. +
  30466. + /* Reset the core */
  30467. + gpwrdn.d32 = 0;
  30468. + gpwrdn.b.pwrdnrstn = 1;
  30469. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30470. + dwc_udelay(10);
  30471. +
  30472. + /* Disable power clamps */
  30473. + gpwrdn.d32 = 0;
  30474. + gpwrdn.b.pwrdnclmp = 1;
  30475. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30476. +
  30477. + /* Remove reset the core signal */
  30478. + gpwrdn.d32 = 0;
  30479. + gpwrdn.b.pwrdnrstn = 1;
  30480. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  30481. + dwc_udelay(10);
  30482. +
  30483. + /* Disable PMU interrupt */
  30484. + gpwrdn.d32 = 0;
  30485. + gpwrdn.b.pmuintsel = 1;
  30486. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30487. +
  30488. + /* Indicates that we are exiting from hibernation */
  30489. + core_if->hibernation_suspend = 0;
  30490. +
  30491. + /* Disable PMU */
  30492. + gpwrdn.d32 = 0;
  30493. + gpwrdn.b.pmuactv = 1;
  30494. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30495. + dwc_udelay(10);
  30496. +
  30497. + /* Programm Disable VBUS to 0 */
  30498. + gpwrdn.d32 = 0;
  30499. + gpwrdn.b.dis_vbus = 1;
  30500. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30501. +
  30502. + /*Initialize the core as Host */
  30503. + core_if->op_state = A_HOST;
  30504. + dwc_otg_core_init(core_if);
  30505. + dwc_otg_enable_global_interrupts(core_if);
  30506. + cil_hcd_start(core_if);
  30507. +
  30508. + return 1;
  30509. +}
  30510. +
  30511. +/** This interrupt indicates that restore command after Hibernation
  30512. + * was completed by the core. */
  30513. +int32_t dwc_otg_handle_restore_done_intr(dwc_otg_core_if_t * core_if)
  30514. +{
  30515. + pcgcctl_data_t pcgcctl;
  30516. + DWC_DEBUGPL(DBG_ANY, "++Restore Done Interrupt++\n");
  30517. +
  30518. + //TODO De-assert restore signal. 8.a
  30519. + pcgcctl.d32 = DWC_READ_REG32(core_if->pcgcctl);
  30520. + if (pcgcctl.b.restoremode == 1) {
  30521. + gintmsk_data_t gintmsk = {.d32 = 0 };
  30522. + /*
  30523. + * If restore mode is Remote Wakeup,
  30524. + * unmask Remote Wakeup interrupt.
  30525. + */
  30526. + gintmsk.b.wkupintr = 1;
  30527. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
  30528. + 0, gintmsk.d32);
  30529. + }
  30530. +
  30531. + return 1;
  30532. +}
  30533. +
  30534. +/**
  30535. + * This interrupt indicates that a device has been disconnected from
  30536. + * the root port.
  30537. + */
  30538. +int32_t dwc_otg_handle_disconnect_intr(dwc_otg_core_if_t * core_if)
  30539. +{
  30540. + gintsts_data_t gintsts;
  30541. +
  30542. + DWC_DEBUGPL(DBG_ANY, "++Disconnect Detected Interrupt++ (%s) %s\n",
  30543. + (dwc_otg_is_host_mode(core_if) ? "Host" : "Device"),
  30544. + op_state_str(core_if));
  30545. +
  30546. +/** @todo Consolidate this if statement. */
  30547. +#ifndef DWC_HOST_ONLY
  30548. + if (core_if->op_state == B_HOST) {
  30549. + /* If in device mode Disconnect and stop the HCD, then
  30550. + * start the PCD. */
  30551. + DWC_SPINUNLOCK(core_if->lock);
  30552. + cil_hcd_disconnect(core_if);
  30553. + cil_pcd_start(core_if);
  30554. + DWC_SPINLOCK(core_if->lock);
  30555. + core_if->op_state = B_PERIPHERAL;
  30556. + } else if (dwc_otg_is_device_mode(core_if)) {
  30557. + gotgctl_data_t gotgctl = {.d32 = 0 };
  30558. + gotgctl.d32 =
  30559. + DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  30560. + if (gotgctl.b.hstsethnpen == 1) {
  30561. + /* Do nothing, if HNP in process the OTG
  30562. + * interrupt "Host Negotiation Detected"
  30563. + * interrupt will do the mode switch.
  30564. + */
  30565. + } else if (gotgctl.b.devhnpen == 0) {
  30566. + /* If in device mode Disconnect and stop the HCD, then
  30567. + * start the PCD. */
  30568. + DWC_SPINUNLOCK(core_if->lock);
  30569. + cil_hcd_disconnect(core_if);
  30570. + cil_pcd_start(core_if);
  30571. + DWC_SPINLOCK(core_if->lock);
  30572. + core_if->op_state = B_PERIPHERAL;
  30573. + } else {
  30574. + DWC_DEBUGPL(DBG_ANY, "!a_peripheral && !devhnpen\n");
  30575. + }
  30576. + } else {
  30577. + if (core_if->op_state == A_HOST) {
  30578. + /* A-Cable still connected but device disconnected. */
  30579. + DWC_SPINUNLOCK(core_if->lock);
  30580. + cil_hcd_disconnect(core_if);
  30581. + DWC_SPINLOCK(core_if->lock);
  30582. + if (core_if->adp_enable) {
  30583. + gpwrdn_data_t gpwrdn = { .d32 = 0 };
  30584. + cil_hcd_stop(core_if);
  30585. + /* Enable Power Down Logic */
  30586. + gpwrdn.b.pmuintsel = 1;
  30587. + gpwrdn.b.pmuactv = 1;
  30588. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  30589. + gpwrdn, 0, gpwrdn.d32);
  30590. + dwc_otg_adp_probe_start(core_if);
  30591. +
  30592. + /* Power off the core */
  30593. + if (core_if->power_down == 2) {
  30594. + gpwrdn.d32 = 0;
  30595. + gpwrdn.b.pwrdnswtch = 1;
  30596. + DWC_MODIFY_REG32
  30597. + (&core_if->core_global_regs->gpwrdn,
  30598. + gpwrdn.d32, 0);
  30599. + }
  30600. + }
  30601. + }
  30602. + }
  30603. +#endif
  30604. + /* Change to L3(OFF) state */
  30605. + core_if->lx_state = DWC_OTG_L3;
  30606. +
  30607. + gintsts.d32 = 0;
  30608. + gintsts.b.disconnect = 1;
  30609. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  30610. + return 1;
  30611. +}
  30612. +
  30613. +/**
  30614. + * This interrupt indicates that SUSPEND state has been detected on
  30615. + * the USB.
  30616. + *
  30617. + * For HNP the USB Suspend interrupt signals the change from
  30618. + * "a_peripheral" to "a_host".
  30619. + *
  30620. + * When power management is enabled the core will be put in low power
  30621. + * mode.
  30622. + */
  30623. +int32_t dwc_otg_handle_usb_suspend_intr(dwc_otg_core_if_t * core_if)
  30624. +{
  30625. + dsts_data_t dsts;
  30626. + gintsts_data_t gintsts;
  30627. + dcfg_data_t dcfg;
  30628. +
  30629. + DWC_DEBUGPL(DBG_ANY, "USB SUSPEND\n");
  30630. +
  30631. + if (dwc_otg_is_device_mode(core_if)) {
  30632. + /* Check the Device status register to determine if the Suspend
  30633. + * state is active. */
  30634. + dsts.d32 =
  30635. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  30636. + DWC_DEBUGPL(DBG_PCD, "DSTS=0x%0x\n", dsts.d32);
  30637. + DWC_DEBUGPL(DBG_PCD, "DSTS.Suspend Status=%d "
  30638. + "HWCFG4.power Optimize=%d\n",
  30639. + dsts.b.suspsts, core_if->hwcfg4.b.power_optimiz);
  30640. +
  30641. +#ifdef PARTIAL_POWER_DOWN
  30642. +/** @todo Add a module parameter for power management. */
  30643. +
  30644. + if (dsts.b.suspsts && core_if->hwcfg4.b.power_optimiz) {
  30645. + pcgcctl_data_t power = {.d32 = 0 };
  30646. + DWC_DEBUGPL(DBG_CIL, "suspend\n");
  30647. +
  30648. + power.b.pwrclmp = 1;
  30649. + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
  30650. +
  30651. + power.b.rstpdwnmodule = 1;
  30652. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, power.d32);
  30653. +
  30654. + power.b.stoppclk = 1;
  30655. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, power.d32);
  30656. +
  30657. + } else {
  30658. + DWC_DEBUGPL(DBG_ANY, "disconnect?\n");
  30659. + }
  30660. +#endif
  30661. + /* PCD callback for suspend. Release the lock inside of callback function */
  30662. + cil_pcd_suspend(core_if);
  30663. + if (core_if->power_down == 2)
  30664. + {
  30665. + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  30666. + DWC_DEBUGPL(DBG_ANY,"lx_state = %08x\n",core_if->lx_state);
  30667. + DWC_DEBUGPL(DBG_ANY," device address = %08d\n",dcfg.b.devaddr);
  30668. +
  30669. + if (core_if->lx_state != DWC_OTG_L3 && dcfg.b.devaddr) {
  30670. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  30671. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  30672. + gusbcfg_data_t gusbcfg = {.d32 = 0 };
  30673. +
  30674. + /* Change to L2(suspend) state */
  30675. + core_if->lx_state = DWC_OTG_L2;
  30676. +
  30677. + /* Clear interrupt in gintsts */
  30678. + gintsts.d32 = 0;
  30679. + gintsts.b.usbsuspend = 1;
  30680. + DWC_WRITE_REG32(&core_if->core_global_regs->
  30681. + gintsts, gintsts.d32);
  30682. + DWC_PRINTF("Start of hibernation completed\n");
  30683. + dwc_otg_save_global_regs(core_if);
  30684. + dwc_otg_save_dev_regs(core_if);
  30685. +
  30686. + gusbcfg.d32 =
  30687. + DWC_READ_REG32(&core_if->core_global_regs->
  30688. + gusbcfg);
  30689. + if (gusbcfg.b.ulpi_utmi_sel == 1) {
  30690. + /* ULPI interface */
  30691. + /* Suspend the Phy Clock */
  30692. + pcgcctl.d32 = 0;
  30693. + pcgcctl.b.stoppclk = 1;
  30694. + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
  30695. + pcgcctl.d32);
  30696. + dwc_udelay(10);
  30697. + gpwrdn.b.pmuactv = 1;
  30698. + DWC_MODIFY_REG32(&core_if->
  30699. + core_global_regs->
  30700. + gpwrdn, 0, gpwrdn.d32);
  30701. + } else {
  30702. + /* UTMI+ Interface */
  30703. + gpwrdn.b.pmuactv = 1;
  30704. + DWC_MODIFY_REG32(&core_if->
  30705. + core_global_regs->
  30706. + gpwrdn, 0, gpwrdn.d32);
  30707. + dwc_udelay(10);
  30708. + pcgcctl.b.stoppclk = 1;
  30709. + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
  30710. + pcgcctl.d32);
  30711. + dwc_udelay(10);
  30712. + }
  30713. +
  30714. + /* Set flag to indicate that we are in hibernation */
  30715. + core_if->hibernation_suspend = 1;
  30716. + /* Enable interrupts from wake up logic */
  30717. + gpwrdn.d32 = 0;
  30718. + gpwrdn.b.pmuintsel = 1;
  30719. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  30720. + gpwrdn, 0, gpwrdn.d32);
  30721. + dwc_udelay(10);
  30722. +
  30723. + /* Unmask device mode interrupts in GPWRDN */
  30724. + gpwrdn.d32 = 0;
  30725. + gpwrdn.b.rst_det_msk = 1;
  30726. + gpwrdn.b.lnstchng_msk = 1;
  30727. + gpwrdn.b.sts_chngint_msk = 1;
  30728. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  30729. + gpwrdn, 0, gpwrdn.d32);
  30730. + dwc_udelay(10);
  30731. +
  30732. + /* Enable Power Down Clamp */
  30733. + gpwrdn.d32 = 0;
  30734. + gpwrdn.b.pwrdnclmp = 1;
  30735. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  30736. + gpwrdn, 0, gpwrdn.d32);
  30737. + dwc_udelay(10);
  30738. +
  30739. + /* Switch off VDD */
  30740. + gpwrdn.d32 = 0;
  30741. + gpwrdn.b.pwrdnswtch = 1;
  30742. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  30743. + gpwrdn, 0, gpwrdn.d32);
  30744. +
  30745. + /* Save gpwrdn register for further usage if stschng interrupt */
  30746. + core_if->gr_backup->gpwrdn_local =
  30747. + DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  30748. + DWC_PRINTF("Hibernation completed\n");
  30749. +
  30750. + return 1;
  30751. + }
  30752. + } else if (core_if->power_down == 3) {
  30753. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  30754. + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  30755. + DWC_DEBUGPL(DBG_ANY, "lx_state = %08x\n",core_if->lx_state);
  30756. + DWC_DEBUGPL(DBG_ANY, " device address = %08d\n",dcfg.b.devaddr);
  30757. +
  30758. + if (core_if->lx_state != DWC_OTG_L3 && dcfg.b.devaddr) {
  30759. + DWC_DEBUGPL(DBG_ANY, "Start entering to extended hibernation\n");
  30760. + core_if->xhib = 1;
  30761. +
  30762. + /* Clear interrupt in gintsts */
  30763. + gintsts.d32 = 0;
  30764. + gintsts.b.usbsuspend = 1;
  30765. + DWC_WRITE_REG32(&core_if->core_global_regs->
  30766. + gintsts, gintsts.d32);
  30767. +
  30768. + dwc_otg_save_global_regs(core_if);
  30769. + dwc_otg_save_dev_regs(core_if);
  30770. +
  30771. + /* Wait for 10 PHY clocks */
  30772. + dwc_udelay(10);
  30773. +
  30774. + /* Program GPIO register while entering to xHib */
  30775. + DWC_WRITE_REG32(&core_if->core_global_regs->ggpio, 0x1);
  30776. +
  30777. + pcgcctl.b.enbl_extnd_hiber = 1;
  30778. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  30779. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  30780. +
  30781. + pcgcctl.d32 = 0;
  30782. + pcgcctl.b.extnd_hiber_pwrclmp = 1;
  30783. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  30784. +
  30785. + pcgcctl.d32 = 0;
  30786. + pcgcctl.b.extnd_hiber_switch = 1;
  30787. + core_if->gr_backup->xhib_gpwrdn = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  30788. + core_if->gr_backup->xhib_pcgcctl = DWC_READ_REG32(core_if->pcgcctl) | pcgcctl.d32;
  30789. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  30790. +
  30791. + DWC_DEBUGPL(DBG_ANY, "Finished entering to extended hibernation\n");
  30792. +
  30793. + return 1;
  30794. + }
  30795. + }
  30796. + } else {
  30797. + if (core_if->op_state == A_PERIPHERAL) {
  30798. + DWC_DEBUGPL(DBG_ANY, "a_peripheral->a_host\n");
  30799. + /* Clear the a_peripheral flag, back to a_host. */
  30800. + DWC_SPINUNLOCK(core_if->lock);
  30801. + cil_pcd_stop(core_if);
  30802. + cil_hcd_start(core_if);
  30803. + DWC_SPINLOCK(core_if->lock);
  30804. + core_if->op_state = A_HOST;
  30805. + }
  30806. + }
  30807. +
  30808. + /* Change to L2(suspend) state */
  30809. + core_if->lx_state = DWC_OTG_L2;
  30810. +
  30811. + /* Clear interrupt */
  30812. + gintsts.d32 = 0;
  30813. + gintsts.b.usbsuspend = 1;
  30814. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  30815. +
  30816. + return 1;
  30817. +}
  30818. +
  30819. +static int32_t dwc_otg_handle_xhib_exit_intr(dwc_otg_core_if_t * core_if)
  30820. +{
  30821. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  30822. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  30823. + gahbcfg_data_t gahbcfg = {.d32 = 0 };
  30824. +
  30825. + dwc_udelay(10);
  30826. +
  30827. + /* Program GPIO register while entering to xHib */
  30828. + DWC_WRITE_REG32(&core_if->core_global_regs->ggpio, 0x0);
  30829. +
  30830. + pcgcctl.d32 = core_if->gr_backup->xhib_pcgcctl;
  30831. + pcgcctl.b.extnd_hiber_pwrclmp = 0;
  30832. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  30833. + dwc_udelay(10);
  30834. +
  30835. + gpwrdn.d32 = core_if->gr_backup->xhib_gpwrdn;
  30836. + gpwrdn.b.restore = 1;
  30837. + DWC_WRITE_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32);
  30838. + dwc_udelay(10);
  30839. +
  30840. + restore_lpm_i2c_regs(core_if);
  30841. +
  30842. + pcgcctl.d32 = core_if->gr_backup->pcgcctl_local & (0x3FFFF << 14);
  30843. + pcgcctl.b.max_xcvrselect = 1;
  30844. + pcgcctl.b.ess_reg_restored = 0;
  30845. + pcgcctl.b.extnd_hiber_switch = 0;
  30846. + pcgcctl.b.extnd_hiber_pwrclmp = 0;
  30847. + pcgcctl.b.enbl_extnd_hiber = 1;
  30848. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  30849. +
  30850. + gahbcfg.d32 = core_if->gr_backup->gahbcfg_local;
  30851. + gahbcfg.b.glblintrmsk = 1;
  30852. + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg, gahbcfg.d32);
  30853. +
  30854. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  30855. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, 0x1 << 16);
  30856. +
  30857. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg,
  30858. + core_if->gr_backup->gusbcfg_local);
  30859. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg,
  30860. + core_if->dr_backup->dcfg);
  30861. +
  30862. + pcgcctl.d32 = 0;
  30863. + pcgcctl.d32 = core_if->gr_backup->pcgcctl_local & (0x3FFFF << 14);
  30864. + pcgcctl.b.max_xcvrselect = 1;
  30865. + pcgcctl.d32 |= 0x608;
  30866. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  30867. + dwc_udelay(10);
  30868. +
  30869. + pcgcctl.d32 = 0;
  30870. + pcgcctl.d32 = core_if->gr_backup->pcgcctl_local & (0x3FFFF << 14);
  30871. + pcgcctl.b.max_xcvrselect = 1;
  30872. + pcgcctl.b.ess_reg_restored = 1;
  30873. + pcgcctl.b.enbl_extnd_hiber = 1;
  30874. + pcgcctl.b.rstpdwnmodule = 1;
  30875. + pcgcctl.b.restoremode = 1;
  30876. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  30877. +
  30878. + DWC_DEBUGPL(DBG_ANY, "%s called\n", __FUNCTION__);
  30879. +
  30880. + return 1;
  30881. +}
  30882. +
  30883. +#ifdef CONFIG_USB_DWC_OTG_LPM
  30884. +/**
  30885. + * This function hadles LPM transaction received interrupt.
  30886. + */
  30887. +static int32_t dwc_otg_handle_lpm_intr(dwc_otg_core_if_t * core_if)
  30888. +{
  30889. + glpmcfg_data_t lpmcfg;
  30890. + gintsts_data_t gintsts;
  30891. +
  30892. + if (!core_if->core_params->lpm_enable) {
  30893. + DWC_PRINTF("Unexpected LPM interrupt\n");
  30894. + }
  30895. +
  30896. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  30897. + DWC_PRINTF("LPM config register = 0x%08x\n", lpmcfg.d32);
  30898. +
  30899. + if (dwc_otg_is_host_mode(core_if)) {
  30900. + cil_hcd_sleep(core_if);
  30901. + } else {
  30902. + lpmcfg.b.hird_thres |= (1 << 4);
  30903. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg,
  30904. + lpmcfg.d32);
  30905. + }
  30906. +
  30907. + /* Examine prt_sleep_sts after TL1TokenTetry period max (10 us) */
  30908. + dwc_udelay(10);
  30909. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  30910. + if (lpmcfg.b.prt_sleep_sts) {
  30911. + /* Save the current state */
  30912. + core_if->lx_state = DWC_OTG_L1;
  30913. + }
  30914. +
  30915. + /* Clear interrupt */
  30916. + gintsts.d32 = 0;
  30917. + gintsts.b.lpmtranrcvd = 1;
  30918. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  30919. + return 1;
  30920. +}
  30921. +#endif /* CONFIG_USB_DWC_OTG_LPM */
  30922. +
  30923. +/**
  30924. + * This function returns the Core Interrupt register.
  30925. + */
  30926. +static inline uint32_t dwc_otg_read_common_intr(dwc_otg_core_if_t * core_if, gintmsk_data_t *reenable_gintmsk, dwc_otg_hcd_t *hcd)
  30927. +{
  30928. + gahbcfg_data_t gahbcfg = {.d32 = 0 };
  30929. + gintsts_data_t gintsts;
  30930. + gintmsk_data_t gintmsk;
  30931. + gintmsk_data_t gintmsk_common = {.d32 = 0 };
  30932. + gintmsk_common.b.wkupintr = 1;
  30933. + gintmsk_common.b.sessreqintr = 1;
  30934. + gintmsk_common.b.conidstschng = 1;
  30935. + gintmsk_common.b.otgintr = 1;
  30936. + gintmsk_common.b.modemismatch = 1;
  30937. + gintmsk_common.b.disconnect = 1;
  30938. + gintmsk_common.b.usbsuspend = 1;
  30939. +#ifdef CONFIG_USB_DWC_OTG_LPM
  30940. + gintmsk_common.b.lpmtranrcvd = 1;
  30941. +#endif
  30942. + gintmsk_common.b.restoredone = 1;
  30943. + if(dwc_otg_is_device_mode(core_if))
  30944. + {
  30945. + /** @todo: The port interrupt occurs while in device
  30946. + * mode. Added code to CIL to clear the interrupt for now!
  30947. + */
  30948. + gintmsk_common.b.portintr = 1;
  30949. + }
  30950. + if(fiq_enable) {
  30951. + local_fiq_disable();
  30952. + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
  30953. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  30954. + gintmsk.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  30955. + /* Pull in the interrupts that the FIQ has masked */
  30956. + gintmsk.d32 |= ~(hcd->fiq_state->gintmsk_saved.d32);
  30957. + gintmsk.d32 |= gintmsk_common.d32;
  30958. + /* for the upstairs function to reenable - have to read it here in case FIQ triggers again */
  30959. + reenable_gintmsk->d32 = gintmsk.d32;
  30960. + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
  30961. + local_fiq_enable();
  30962. + } else {
  30963. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  30964. + gintmsk.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  30965. + }
  30966. +
  30967. + gahbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gahbcfg);
  30968. +
  30969. +#ifdef DEBUG
  30970. + /* if any common interrupts set */
  30971. + if (gintsts.d32 & gintmsk_common.d32) {
  30972. + DWC_DEBUGPL(DBG_ANY, "common_intr: gintsts=%08x gintmsk=%08x\n",
  30973. + gintsts.d32, gintmsk.d32);
  30974. + }
  30975. +#endif
  30976. + if (!fiq_enable){
  30977. + if (gahbcfg.b.glblintrmsk)
  30978. + return ((gintsts.d32 & gintmsk.d32) & gintmsk_common.d32);
  30979. + else
  30980. + return 0;
  30981. + } else {
  30982. + /* Our IRQ kicker is no longer the USB hardware, it's the MPHI interface.
  30983. + * Can't trust the global interrupt mask bit in this case.
  30984. + */
  30985. + return ((gintsts.d32 & gintmsk.d32) & gintmsk_common.d32);
  30986. + }
  30987. +
  30988. +}
  30989. +
  30990. +/* MACRO for clearing interupt bits in GPWRDN register */
  30991. +#define CLEAR_GPWRDN_INTR(__core_if,__intr) \
  30992. +do { \
  30993. + gpwrdn_data_t gpwrdn = {.d32=0}; \
  30994. + gpwrdn.b.__intr = 1; \
  30995. + DWC_MODIFY_REG32(&__core_if->core_global_regs->gpwrdn, \
  30996. + 0, gpwrdn.d32); \
  30997. +} while (0)
  30998. +
  30999. +/**
  31000. + * Common interrupt handler.
  31001. + *
  31002. + * The common interrupts are those that occur in both Host and Device mode.
  31003. + * This handler handles the following interrupts:
  31004. + * - Mode Mismatch Interrupt
  31005. + * - Disconnect Interrupt
  31006. + * - OTG Interrupt
  31007. + * - Connector ID Status Change Interrupt
  31008. + * - Session Request Interrupt.
  31009. + * - Resume / Remote Wakeup Detected Interrupt.
  31010. + * - LPM Transaction Received Interrupt
  31011. + * - ADP Transaction Received Interrupt
  31012. + *
  31013. + */
  31014. +int32_t dwc_otg_handle_common_intr(void *dev)
  31015. +{
  31016. + int retval = 0;
  31017. + gintsts_data_t gintsts;
  31018. + gintmsk_data_t gintmsk_reenable = { .d32 = 0 };
  31019. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  31020. + dwc_otg_device_t *otg_dev = dev;
  31021. + dwc_otg_core_if_t *core_if = otg_dev->core_if;
  31022. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  31023. + if (dwc_otg_is_device_mode(core_if))
  31024. + core_if->frame_num = dwc_otg_get_frame_number(core_if);
  31025. +
  31026. + if (core_if->lock)
  31027. + DWC_SPINLOCK(core_if->lock);
  31028. +
  31029. + if (core_if->power_down == 3 && core_if->xhib == 1) {
  31030. + DWC_DEBUGPL(DBG_ANY, "Exiting from xHIB state\n");
  31031. + retval |= dwc_otg_handle_xhib_exit_intr(core_if);
  31032. + core_if->xhib = 2;
  31033. + if (core_if->lock)
  31034. + DWC_SPINUNLOCK(core_if->lock);
  31035. +
  31036. + return retval;
  31037. + }
  31038. +
  31039. + if (core_if->hibernation_suspend <= 0) {
  31040. + /* read_common will have to poke the FIQ's saved mask. We must then clear this mask at the end
  31041. + * of this handler - god only knows why it's done like this
  31042. + */
  31043. + gintsts.d32 = dwc_otg_read_common_intr(core_if, &gintmsk_reenable, otg_dev->hcd);
  31044. +
  31045. + if (gintsts.b.modemismatch) {
  31046. + retval |= dwc_otg_handle_mode_mismatch_intr(core_if);
  31047. + }
  31048. + if (gintsts.b.otgintr) {
  31049. + retval |= dwc_otg_handle_otg_intr(core_if);
  31050. + }
  31051. + if (gintsts.b.conidstschng) {
  31052. + retval |=
  31053. + dwc_otg_handle_conn_id_status_change_intr(core_if);
  31054. + }
  31055. + if (gintsts.b.disconnect) {
  31056. + retval |= dwc_otg_handle_disconnect_intr(core_if);
  31057. + }
  31058. + if (gintsts.b.sessreqintr) {
  31059. + retval |= dwc_otg_handle_session_req_intr(core_if);
  31060. + }
  31061. + if (gintsts.b.wkupintr) {
  31062. + retval |= dwc_otg_handle_wakeup_detected_intr(core_if);
  31063. + }
  31064. + if (gintsts.b.usbsuspend) {
  31065. + retval |= dwc_otg_handle_usb_suspend_intr(core_if);
  31066. + }
  31067. +#ifdef CONFIG_USB_DWC_OTG_LPM
  31068. + if (gintsts.b.lpmtranrcvd) {
  31069. + retval |= dwc_otg_handle_lpm_intr(core_if);
  31070. + }
  31071. +#endif
  31072. + if (gintsts.b.restoredone) {
  31073. + gintsts.d32 = 0;
  31074. + if (core_if->power_down == 2)
  31075. + core_if->hibernation_suspend = -1;
  31076. + else if (core_if->power_down == 3 && core_if->xhib == 2) {
  31077. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  31078. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  31079. + dctl_data_t dctl = {.d32 = 0 };
  31080. +
  31081. + DWC_WRITE_REG32(&core_if->core_global_regs->
  31082. + gintsts, 0xFFFFFFFF);
  31083. +
  31084. + DWC_DEBUGPL(DBG_ANY,
  31085. + "RESTORE DONE generated\n");
  31086. +
  31087. + gpwrdn.b.restore = 1;
  31088. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  31089. + dwc_udelay(10);
  31090. +
  31091. + pcgcctl.b.rstpdwnmodule = 1;
  31092. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  31093. +
  31094. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, core_if->gr_backup->gusbcfg_local);
  31095. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, core_if->dr_backup->dcfg);
  31096. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, core_if->dr_backup->dctl);
  31097. + dwc_udelay(50);
  31098. +
  31099. + dctl.b.pwronprgdone = 1;
  31100. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  31101. + dwc_udelay(10);
  31102. +
  31103. + dwc_otg_restore_global_regs(core_if);
  31104. + dwc_otg_restore_dev_regs(core_if, 0);
  31105. +
  31106. + dctl.d32 = 0;
  31107. + dctl.b.pwronprgdone = 1;
  31108. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, 0);
  31109. + dwc_udelay(10);
  31110. +
  31111. + pcgcctl.d32 = 0;
  31112. + pcgcctl.b.enbl_extnd_hiber = 1;
  31113. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  31114. +
  31115. + /* The core will be in ON STATE */
  31116. + core_if->lx_state = DWC_OTG_L0;
  31117. + core_if->xhib = 0;
  31118. +
  31119. + DWC_SPINUNLOCK(core_if->lock);
  31120. + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
  31121. + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
  31122. + }
  31123. + DWC_SPINLOCK(core_if->lock);
  31124. +
  31125. + }
  31126. +
  31127. + gintsts.b.restoredone = 1;
  31128. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts,gintsts.d32);
  31129. + DWC_PRINTF(" --Restore done interrupt received-- \n");
  31130. + retval |= 1;
  31131. + }
  31132. + if (gintsts.b.portintr && dwc_otg_is_device_mode(core_if)) {
  31133. + /* The port interrupt occurs while in device mode with HPRT0
  31134. + * Port Enable/Disable.
  31135. + */
  31136. + gintsts.d32 = 0;
  31137. + gintsts.b.portintr = 1;
  31138. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts,gintsts.d32);
  31139. + retval |= 1;
  31140. + gintmsk_reenable.b.portintr = 1;
  31141. +
  31142. + }
  31143. + /* Did we actually handle anything? if so, unmask the interrupt */
  31144. +// fiq_print(FIQDBG_INT, otg_dev->hcd->fiq_state, "CILOUT %1d", retval);
  31145. +// fiq_print(FIQDBG_INT, otg_dev->hcd->fiq_state, "%08x", gintsts.d32);
  31146. +// fiq_print(FIQDBG_INT, otg_dev->hcd->fiq_state, "%08x", gintmsk_reenable.d32);
  31147. + if (retval && fiq_enable) {
  31148. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk_reenable.d32);
  31149. + }
  31150. +
  31151. + } else {
  31152. + DWC_DEBUGPL(DBG_ANY, "gpwrdn=%08x\n", gpwrdn.d32);
  31153. +
  31154. + if (gpwrdn.b.disconn_det && gpwrdn.b.disconn_det_msk) {
  31155. + CLEAR_GPWRDN_INTR(core_if, disconn_det);
  31156. + if (gpwrdn.b.linestate == 0) {
  31157. + dwc_otg_handle_pwrdn_disconnect_intr(core_if);
  31158. + } else {
  31159. + DWC_PRINTF("Disconnect detected while linestate is not 0\n");
  31160. + }
  31161. +
  31162. + retval |= 1;
  31163. + }
  31164. + if (gpwrdn.b.lnstschng && gpwrdn.b.lnstchng_msk) {
  31165. + CLEAR_GPWRDN_INTR(core_if, lnstschng);
  31166. + /* remote wakeup from hibernation */
  31167. + if (gpwrdn.b.linestate == 2 || gpwrdn.b.linestate == 1) {
  31168. + dwc_otg_handle_pwrdn_wakeup_detected_intr(core_if);
  31169. + } else {
  31170. + DWC_PRINTF("gpwrdn.linestate = %d\n", gpwrdn.b.linestate);
  31171. + }
  31172. + retval |= 1;
  31173. + }
  31174. + if (gpwrdn.b.rst_det && gpwrdn.b.rst_det_msk) {
  31175. + CLEAR_GPWRDN_INTR(core_if, rst_det);
  31176. + if (gpwrdn.b.linestate == 0) {
  31177. + DWC_PRINTF("Reset detected\n");
  31178. + retval |= dwc_otg_device_hibernation_restore(core_if, 0, 1);
  31179. + }
  31180. + }
  31181. + if (gpwrdn.b.srp_det && gpwrdn.b.srp_det_msk) {
  31182. + CLEAR_GPWRDN_INTR(core_if, srp_det);
  31183. + dwc_otg_handle_pwrdn_srp_intr(core_if);
  31184. + retval |= 1;
  31185. + }
  31186. + }
  31187. + /* Handle ADP interrupt here */
  31188. + if (gpwrdn.b.adp_int) {
  31189. + DWC_PRINTF("ADP interrupt\n");
  31190. + CLEAR_GPWRDN_INTR(core_if, adp_int);
  31191. + dwc_otg_adp_handle_intr(core_if);
  31192. + retval |= 1;
  31193. + }
  31194. + if (gpwrdn.b.sts_chngint && gpwrdn.b.sts_chngint_msk) {
  31195. + DWC_PRINTF("STS CHNG interrupt asserted\n");
  31196. + CLEAR_GPWRDN_INTR(core_if, sts_chngint);
  31197. + dwc_otg_handle_pwrdn_stschng_intr(otg_dev);
  31198. +
  31199. + retval |= 1;
  31200. + }
  31201. + if (core_if->lock)
  31202. + DWC_SPINUNLOCK(core_if->lock);
  31203. + return retval;
  31204. +}
  31205. --- /dev/null
  31206. +++ b/drivers/usb/host/dwc_otg/dwc_otg_core_if.h
  31207. @@ -0,0 +1,705 @@
  31208. +/* ==========================================================================
  31209. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_core_if.h $
  31210. + * $Revision: #13 $
  31211. + * $Date: 2012/08/10 $
  31212. + * $Change: 2047372 $
  31213. + *
  31214. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  31215. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  31216. + * otherwise expressly agreed to in writing between Synopsys and you.
  31217. + *
  31218. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  31219. + * any End User Software License Agreement or Agreement for Licensed Product
  31220. + * with Synopsys or any supplement thereto. You are permitted to use and
  31221. + * redistribute this Software in source and binary forms, with or without
  31222. + * modification, provided that redistributions of source code must retain this
  31223. + * notice. You may not view, use, disclose, copy or distribute this file or
  31224. + * any information contained herein except pursuant to this license grant from
  31225. + * Synopsys. If you do not agree with this notice, including the disclaimer
  31226. + * below, then you are not authorized to use the Software.
  31227. + *
  31228. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  31229. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  31230. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  31231. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  31232. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  31233. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  31234. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  31235. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31236. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  31237. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  31238. + * DAMAGE.
  31239. + * ========================================================================== */
  31240. +#if !defined(__DWC_CORE_IF_H__)
  31241. +#define __DWC_CORE_IF_H__
  31242. +
  31243. +#include "dwc_os.h"
  31244. +
  31245. +/** @file
  31246. + * This file defines DWC_OTG Core API
  31247. + */
  31248. +
  31249. +struct dwc_otg_core_if;
  31250. +typedef struct dwc_otg_core_if dwc_otg_core_if_t;
  31251. +
  31252. +/** Maximum number of Periodic FIFOs */
  31253. +#define MAX_PERIO_FIFOS 15
  31254. +/** Maximum number of Periodic FIFOs */
  31255. +#define MAX_TX_FIFOS 15
  31256. +
  31257. +/** Maximum number of Endpoints/HostChannels */
  31258. +#define MAX_EPS_CHANNELS 16
  31259. +
  31260. +extern dwc_otg_core_if_t *dwc_otg_cil_init(const uint32_t * _reg_base_addr);
  31261. +extern void dwc_otg_core_init(dwc_otg_core_if_t * _core_if);
  31262. +extern void dwc_otg_cil_remove(dwc_otg_core_if_t * _core_if);
  31263. +
  31264. +extern void dwc_otg_enable_global_interrupts(dwc_otg_core_if_t * _core_if);
  31265. +extern void dwc_otg_disable_global_interrupts(dwc_otg_core_if_t * _core_if);
  31266. +
  31267. +extern uint8_t dwc_otg_is_device_mode(dwc_otg_core_if_t * _core_if);
  31268. +extern uint8_t dwc_otg_is_host_mode(dwc_otg_core_if_t * _core_if);
  31269. +
  31270. +extern uint8_t dwc_otg_is_dma_enable(dwc_otg_core_if_t * core_if);
  31271. +
  31272. +/** This function should be called on every hardware interrupt. */
  31273. +extern int32_t dwc_otg_handle_common_intr(void *otg_dev);
  31274. +
  31275. +/** @name OTG Core Parameters */
  31276. +/** @{ */
  31277. +
  31278. +/**
  31279. + * Specifies the OTG capabilities. The driver will automatically
  31280. + * detect the value for this parameter if none is specified.
  31281. + * 0 - HNP and SRP capable (default)
  31282. + * 1 - SRP Only capable
  31283. + * 2 - No HNP/SRP capable
  31284. + */
  31285. +extern int dwc_otg_set_param_otg_cap(dwc_otg_core_if_t * core_if, int32_t val);
  31286. +extern int32_t dwc_otg_get_param_otg_cap(dwc_otg_core_if_t * core_if);
  31287. +#define DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE 0
  31288. +#define DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE 1
  31289. +#define DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE 2
  31290. +#define dwc_param_otg_cap_default DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE
  31291. +
  31292. +extern int dwc_otg_set_param_opt(dwc_otg_core_if_t * core_if, int32_t val);
  31293. +extern int32_t dwc_otg_get_param_opt(dwc_otg_core_if_t * core_if);
  31294. +#define dwc_param_opt_default 1
  31295. +
  31296. +/**
  31297. + * Specifies whether to use slave or DMA mode for accessing the data
  31298. + * FIFOs. The driver will automatically detect the value for this
  31299. + * parameter if none is specified.
  31300. + * 0 - Slave
  31301. + * 1 - DMA (default, if available)
  31302. + */
  31303. +extern int dwc_otg_set_param_dma_enable(dwc_otg_core_if_t * core_if,
  31304. + int32_t val);
  31305. +extern int32_t dwc_otg_get_param_dma_enable(dwc_otg_core_if_t * core_if);
  31306. +#define dwc_param_dma_enable_default 1
  31307. +
  31308. +/**
  31309. + * When DMA mode is enabled specifies whether to use
  31310. + * address DMA or DMA Descritor mode for accessing the data
  31311. + * FIFOs in device mode. The driver will automatically detect
  31312. + * the value for this parameter if none is specified.
  31313. + * 0 - address DMA
  31314. + * 1 - DMA Descriptor(default, if available)
  31315. + */
  31316. +extern int dwc_otg_set_param_dma_desc_enable(dwc_otg_core_if_t * core_if,
  31317. + int32_t val);
  31318. +extern int32_t dwc_otg_get_param_dma_desc_enable(dwc_otg_core_if_t * core_if);
  31319. +//#define dwc_param_dma_desc_enable_default 1
  31320. +#define dwc_param_dma_desc_enable_default 0 // Broadcom BCM2708
  31321. +
  31322. +/** The DMA Burst size (applicable only for External DMA
  31323. + * Mode). 1, 4, 8 16, 32, 64, 128, 256 (default 32)
  31324. + */
  31325. +extern int dwc_otg_set_param_dma_burst_size(dwc_otg_core_if_t * core_if,
  31326. + int32_t val);
  31327. +extern int32_t dwc_otg_get_param_dma_burst_size(dwc_otg_core_if_t * core_if);
  31328. +#define dwc_param_dma_burst_size_default 32
  31329. +
  31330. +/**
  31331. + * Specifies the maximum speed of operation in host and device mode.
  31332. + * The actual speed depends on the speed of the attached device and
  31333. + * the value of phy_type. The actual speed depends on the speed of the
  31334. + * attached device.
  31335. + * 0 - High Speed (default)
  31336. + * 1 - Full Speed
  31337. + */
  31338. +extern int dwc_otg_set_param_speed(dwc_otg_core_if_t * core_if, int32_t val);
  31339. +extern int32_t dwc_otg_get_param_speed(dwc_otg_core_if_t * core_if);
  31340. +#define dwc_param_speed_default 0
  31341. +#define DWC_SPEED_PARAM_HIGH 0
  31342. +#define DWC_SPEED_PARAM_FULL 1
  31343. +
  31344. +/** Specifies whether low power mode is supported when attached
  31345. + * to a Full Speed or Low Speed device in host mode.
  31346. + * 0 - Don't support low power mode (default)
  31347. + * 1 - Support low power mode
  31348. + */
  31349. +extern int dwc_otg_set_param_host_support_fs_ls_low_power(dwc_otg_core_if_t *
  31350. + core_if, int32_t val);
  31351. +extern int32_t dwc_otg_get_param_host_support_fs_ls_low_power(dwc_otg_core_if_t
  31352. + * core_if);
  31353. +#define dwc_param_host_support_fs_ls_low_power_default 0
  31354. +
  31355. +/** Specifies the PHY clock rate in low power mode when connected to a
  31356. + * Low Speed device in host mode. This parameter is applicable only if
  31357. + * HOST_SUPPORT_FS_LS_LOW_POWER is enabled. If PHY_TYPE is set to FS
  31358. + * then defaults to 6 MHZ otherwise 48 MHZ.
  31359. + *
  31360. + * 0 - 48 MHz
  31361. + * 1 - 6 MHz
  31362. + */
  31363. +extern int dwc_otg_set_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t *
  31364. + core_if, int32_t val);
  31365. +extern int32_t dwc_otg_get_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t *
  31366. + core_if);
  31367. +#define dwc_param_host_ls_low_power_phy_clk_default 0
  31368. +#define DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ 0
  31369. +#define DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ 1
  31370. +
  31371. +/**
  31372. + * 0 - Use cC FIFO size parameters
  31373. + * 1 - Allow dynamic FIFO sizing (default)
  31374. + */
  31375. +extern int dwc_otg_set_param_enable_dynamic_fifo(dwc_otg_core_if_t * core_if,
  31376. + int32_t val);
  31377. +extern int32_t dwc_otg_get_param_enable_dynamic_fifo(dwc_otg_core_if_t *
  31378. + core_if);
  31379. +#define dwc_param_enable_dynamic_fifo_default 1
  31380. +
  31381. +/** Total number of 4-byte words in the data FIFO memory. This
  31382. + * memory includes the Rx FIFO, non-periodic Tx FIFO, and periodic
  31383. + * Tx FIFOs.
  31384. + * 32 to 32768 (default 8192)
  31385. + * Note: The total FIFO memory depth in the FPGA configuration is 8192.
  31386. + */
  31387. +extern int dwc_otg_set_param_data_fifo_size(dwc_otg_core_if_t * core_if,
  31388. + int32_t val);
  31389. +extern int32_t dwc_otg_get_param_data_fifo_size(dwc_otg_core_if_t * core_if);
  31390. +//#define dwc_param_data_fifo_size_default 8192
  31391. +#define dwc_param_data_fifo_size_default 0xFF0 // Broadcom BCM2708
  31392. +
  31393. +/** Number of 4-byte words in the Rx FIFO in device mode when dynamic
  31394. + * FIFO sizing is enabled.
  31395. + * 16 to 32768 (default 1064)
  31396. + */
  31397. +extern int dwc_otg_set_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if,
  31398. + int32_t val);
  31399. +extern int32_t dwc_otg_get_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if);
  31400. +#define dwc_param_dev_rx_fifo_size_default 1064
  31401. +
  31402. +/** Number of 4-byte words in the non-periodic Tx FIFO in device mode
  31403. + * when dynamic FIFO sizing is enabled.
  31404. + * 16 to 32768 (default 1024)
  31405. + */
  31406. +extern int dwc_otg_set_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t *
  31407. + core_if, int32_t val);
  31408. +extern int32_t dwc_otg_get_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t *
  31409. + core_if);
  31410. +#define dwc_param_dev_nperio_tx_fifo_size_default 1024
  31411. +
  31412. +/** Number of 4-byte words in each of the periodic Tx FIFOs in device
  31413. + * mode when dynamic FIFO sizing is enabled.
  31414. + * 4 to 768 (default 256)
  31415. + */
  31416. +extern int dwc_otg_set_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  31417. + int32_t val, int fifo_num);
  31418. +extern int32_t dwc_otg_get_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t *
  31419. + core_if, int fifo_num);
  31420. +#define dwc_param_dev_perio_tx_fifo_size_default 256
  31421. +
  31422. +/** Number of 4-byte words in the Rx FIFO in host mode when dynamic
  31423. + * FIFO sizing is enabled.
  31424. + * 16 to 32768 (default 1024)
  31425. + */
  31426. +extern int dwc_otg_set_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if,
  31427. + int32_t val);
  31428. +extern int32_t dwc_otg_get_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if);
  31429. +//#define dwc_param_host_rx_fifo_size_default 1024
  31430. +#define dwc_param_host_rx_fifo_size_default 774 // Broadcom BCM2708
  31431. +
  31432. +/** Number of 4-byte words in the non-periodic Tx FIFO in host mode
  31433. + * when Dynamic FIFO sizing is enabled in the core.
  31434. + * 16 to 32768 (default 1024)
  31435. + */
  31436. +extern int dwc_otg_set_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t *
  31437. + core_if, int32_t val);
  31438. +extern int32_t dwc_otg_get_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t *
  31439. + core_if);
  31440. +//#define dwc_param_host_nperio_tx_fifo_size_default 1024
  31441. +#define dwc_param_host_nperio_tx_fifo_size_default 0x100 // Broadcom BCM2708
  31442. +
  31443. +/** Number of 4-byte words in the host periodic Tx FIFO when dynamic
  31444. + * FIFO sizing is enabled.
  31445. + * 16 to 32768 (default 1024)
  31446. + */
  31447. +extern int dwc_otg_set_param_host_perio_tx_fifo_size(dwc_otg_core_if_t *
  31448. + core_if, int32_t val);
  31449. +extern int32_t dwc_otg_get_param_host_perio_tx_fifo_size(dwc_otg_core_if_t *
  31450. + core_if);
  31451. +//#define dwc_param_host_perio_tx_fifo_size_default 1024
  31452. +#define dwc_param_host_perio_tx_fifo_size_default 0x200 // Broadcom BCM2708
  31453. +
  31454. +/** The maximum transfer size supported in bytes.
  31455. + * 2047 to 65,535 (default 65,535)
  31456. + */
  31457. +extern int dwc_otg_set_param_max_transfer_size(dwc_otg_core_if_t * core_if,
  31458. + int32_t val);
  31459. +extern int32_t dwc_otg_get_param_max_transfer_size(dwc_otg_core_if_t * core_if);
  31460. +#define dwc_param_max_transfer_size_default 65535
  31461. +
  31462. +/** The maximum number of packets in a transfer.
  31463. + * 15 to 511 (default 511)
  31464. + */
  31465. +extern int dwc_otg_set_param_max_packet_count(dwc_otg_core_if_t * core_if,
  31466. + int32_t val);
  31467. +extern int32_t dwc_otg_get_param_max_packet_count(dwc_otg_core_if_t * core_if);
  31468. +#define dwc_param_max_packet_count_default 511
  31469. +
  31470. +/** The number of host channel registers to use.
  31471. + * 1 to 16 (default 12)
  31472. + * Note: The FPGA configuration supports a maximum of 12 host channels.
  31473. + */
  31474. +extern int dwc_otg_set_param_host_channels(dwc_otg_core_if_t * core_if,
  31475. + int32_t val);
  31476. +extern int32_t dwc_otg_get_param_host_channels(dwc_otg_core_if_t * core_if);
  31477. +//#define dwc_param_host_channels_default 12
  31478. +#define dwc_param_host_channels_default 8 // Broadcom BCM2708
  31479. +
  31480. +/** The number of endpoints in addition to EP0 available for device
  31481. + * mode operations.
  31482. + * 1 to 15 (default 6 IN and OUT)
  31483. + * Note: The FPGA configuration supports a maximum of 6 IN and OUT
  31484. + * endpoints in addition to EP0.
  31485. + */
  31486. +extern int dwc_otg_set_param_dev_endpoints(dwc_otg_core_if_t * core_if,
  31487. + int32_t val);
  31488. +extern int32_t dwc_otg_get_param_dev_endpoints(dwc_otg_core_if_t * core_if);
  31489. +#define dwc_param_dev_endpoints_default 6
  31490. +
  31491. +/**
  31492. + * Specifies the type of PHY interface to use. By default, the driver
  31493. + * will automatically detect the phy_type.
  31494. + *
  31495. + * 0 - Full Speed PHY
  31496. + * 1 - UTMI+ (default)
  31497. + * 2 - ULPI
  31498. + */
  31499. +extern int dwc_otg_set_param_phy_type(dwc_otg_core_if_t * core_if, int32_t val);
  31500. +extern int32_t dwc_otg_get_param_phy_type(dwc_otg_core_if_t * core_if);
  31501. +#define DWC_PHY_TYPE_PARAM_FS 0
  31502. +#define DWC_PHY_TYPE_PARAM_UTMI 1
  31503. +#define DWC_PHY_TYPE_PARAM_ULPI 2
  31504. +#define dwc_param_phy_type_default DWC_PHY_TYPE_PARAM_UTMI
  31505. +
  31506. +/**
  31507. + * Specifies the UTMI+ Data Width. This parameter is
  31508. + * applicable for a PHY_TYPE of UTMI+ or ULPI. (For a ULPI
  31509. + * PHY_TYPE, this parameter indicates the data width between
  31510. + * the MAC and the ULPI Wrapper.) Also, this parameter is
  31511. + * applicable only if the OTG_HSPHY_WIDTH cC parameter was set
  31512. + * to "8 and 16 bits", meaning that the core has been
  31513. + * configured to work at either data path width.
  31514. + *
  31515. + * 8 or 16 bits (default 16)
  31516. + */
  31517. +extern int dwc_otg_set_param_phy_utmi_width(dwc_otg_core_if_t * core_if,
  31518. + int32_t val);
  31519. +extern int32_t dwc_otg_get_param_phy_utmi_width(dwc_otg_core_if_t * core_if);
  31520. +//#define dwc_param_phy_utmi_width_default 16
  31521. +#define dwc_param_phy_utmi_width_default 8 // Broadcom BCM2708
  31522. +
  31523. +/**
  31524. + * Specifies whether the ULPI operates at double or single
  31525. + * data rate. This parameter is only applicable if PHY_TYPE is
  31526. + * ULPI.
  31527. + *
  31528. + * 0 - single data rate ULPI interface with 8 bit wide data
  31529. + * bus (default)
  31530. + * 1 - double data rate ULPI interface with 4 bit wide data
  31531. + * bus
  31532. + */
  31533. +extern int dwc_otg_set_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if,
  31534. + int32_t val);
  31535. +extern int32_t dwc_otg_get_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if);
  31536. +#define dwc_param_phy_ulpi_ddr_default 0
  31537. +
  31538. +/**
  31539. + * Specifies whether to use the internal or external supply to
  31540. + * drive the vbus with a ULPI phy.
  31541. + */
  31542. +extern int dwc_otg_set_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if,
  31543. + int32_t val);
  31544. +extern int32_t dwc_otg_get_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if);
  31545. +#define DWC_PHY_ULPI_INTERNAL_VBUS 0
  31546. +#define DWC_PHY_ULPI_EXTERNAL_VBUS 1
  31547. +#define dwc_param_phy_ulpi_ext_vbus_default DWC_PHY_ULPI_INTERNAL_VBUS
  31548. +
  31549. +/**
  31550. + * Specifies whether to use the I2Cinterface for full speed PHY. This
  31551. + * parameter is only applicable if PHY_TYPE is FS.
  31552. + * 0 - No (default)
  31553. + * 1 - Yes
  31554. + */
  31555. +extern int dwc_otg_set_param_i2c_enable(dwc_otg_core_if_t * core_if,
  31556. + int32_t val);
  31557. +extern int32_t dwc_otg_get_param_i2c_enable(dwc_otg_core_if_t * core_if);
  31558. +#define dwc_param_i2c_enable_default 0
  31559. +
  31560. +extern int dwc_otg_set_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if,
  31561. + int32_t val);
  31562. +extern int32_t dwc_otg_get_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if);
  31563. +#define dwc_param_ulpi_fs_ls_default 0
  31564. +
  31565. +extern int dwc_otg_set_param_ts_dline(dwc_otg_core_if_t * core_if, int32_t val);
  31566. +extern int32_t dwc_otg_get_param_ts_dline(dwc_otg_core_if_t * core_if);
  31567. +#define dwc_param_ts_dline_default 0
  31568. +
  31569. +/**
  31570. + * Specifies whether dedicated transmit FIFOs are
  31571. + * enabled for non periodic IN endpoints in device mode
  31572. + * 0 - No
  31573. + * 1 - Yes
  31574. + */
  31575. +extern int dwc_otg_set_param_en_multiple_tx_fifo(dwc_otg_core_if_t * core_if,
  31576. + int32_t val);
  31577. +extern int32_t dwc_otg_get_param_en_multiple_tx_fifo(dwc_otg_core_if_t *
  31578. + core_if);
  31579. +#define dwc_param_en_multiple_tx_fifo_default 1
  31580. +
  31581. +/** Number of 4-byte words in each of the Tx FIFOs in device
  31582. + * mode when dynamic FIFO sizing is enabled.
  31583. + * 4 to 768 (default 256)
  31584. + */
  31585. +extern int dwc_otg_set_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if,
  31586. + int fifo_num, int32_t val);
  31587. +extern int32_t dwc_otg_get_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if,
  31588. + int fifo_num);
  31589. +#define dwc_param_dev_tx_fifo_size_default 768
  31590. +
  31591. +/** Thresholding enable flag-
  31592. + * bit 0 - enable non-ISO Tx thresholding
  31593. + * bit 1 - enable ISO Tx thresholding
  31594. + * bit 2 - enable Rx thresholding
  31595. + */
  31596. +extern int dwc_otg_set_param_thr_ctl(dwc_otg_core_if_t * core_if, int32_t val);
  31597. +extern int32_t dwc_otg_get_thr_ctl(dwc_otg_core_if_t * core_if, int fifo_num);
  31598. +#define dwc_param_thr_ctl_default 0
  31599. +
  31600. +/** Thresholding length for Tx
  31601. + * FIFOs in 32 bit DWORDs
  31602. + */
  31603. +extern int dwc_otg_set_param_tx_thr_length(dwc_otg_core_if_t * core_if,
  31604. + int32_t val);
  31605. +extern int32_t dwc_otg_get_tx_thr_length(dwc_otg_core_if_t * core_if);
  31606. +#define dwc_param_tx_thr_length_default 64
  31607. +
  31608. +/** Thresholding length for Rx
  31609. + * FIFOs in 32 bit DWORDs
  31610. + */
  31611. +extern int dwc_otg_set_param_rx_thr_length(dwc_otg_core_if_t * core_if,
  31612. + int32_t val);
  31613. +extern int32_t dwc_otg_get_rx_thr_length(dwc_otg_core_if_t * core_if);
  31614. +#define dwc_param_rx_thr_length_default 64
  31615. +
  31616. +/**
  31617. + * Specifies whether LPM (Link Power Management) support is enabled
  31618. + */
  31619. +extern int dwc_otg_set_param_lpm_enable(dwc_otg_core_if_t * core_if,
  31620. + int32_t val);
  31621. +extern int32_t dwc_otg_get_param_lpm_enable(dwc_otg_core_if_t * core_if);
  31622. +#define dwc_param_lpm_enable_default 1
  31623. +
  31624. +/**
  31625. + * Specifies whether PTI enhancement is enabled
  31626. + */
  31627. +extern int dwc_otg_set_param_pti_enable(dwc_otg_core_if_t * core_if,
  31628. + int32_t val);
  31629. +extern int32_t dwc_otg_get_param_pti_enable(dwc_otg_core_if_t * core_if);
  31630. +#define dwc_param_pti_enable_default 0
  31631. +
  31632. +/**
  31633. + * Specifies whether MPI enhancement is enabled
  31634. + */
  31635. +extern int dwc_otg_set_param_mpi_enable(dwc_otg_core_if_t * core_if,
  31636. + int32_t val);
  31637. +extern int32_t dwc_otg_get_param_mpi_enable(dwc_otg_core_if_t * core_if);
  31638. +#define dwc_param_mpi_enable_default 0
  31639. +
  31640. +/**
  31641. + * Specifies whether ADP capability is enabled
  31642. + */
  31643. +extern int dwc_otg_set_param_adp_enable(dwc_otg_core_if_t * core_if,
  31644. + int32_t val);
  31645. +extern int32_t dwc_otg_get_param_adp_enable(dwc_otg_core_if_t * core_if);
  31646. +#define dwc_param_adp_enable_default 0
  31647. +
  31648. +/**
  31649. + * Specifies whether IC_USB capability is enabled
  31650. + */
  31651. +
  31652. +extern int dwc_otg_set_param_ic_usb_cap(dwc_otg_core_if_t * core_if,
  31653. + int32_t val);
  31654. +extern int32_t dwc_otg_get_param_ic_usb_cap(dwc_otg_core_if_t * core_if);
  31655. +#define dwc_param_ic_usb_cap_default 0
  31656. +
  31657. +extern int dwc_otg_set_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if,
  31658. + int32_t val);
  31659. +extern int32_t dwc_otg_get_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if);
  31660. +#define dwc_param_ahb_thr_ratio_default 0
  31661. +
  31662. +extern int dwc_otg_set_param_power_down(dwc_otg_core_if_t * core_if,
  31663. + int32_t val);
  31664. +extern int32_t dwc_otg_get_param_power_down(dwc_otg_core_if_t * core_if);
  31665. +#define dwc_param_power_down_default 0
  31666. +
  31667. +extern int dwc_otg_set_param_reload_ctl(dwc_otg_core_if_t * core_if,
  31668. + int32_t val);
  31669. +extern int32_t dwc_otg_get_param_reload_ctl(dwc_otg_core_if_t * core_if);
  31670. +#define dwc_param_reload_ctl_default 0
  31671. +
  31672. +extern int dwc_otg_set_param_dev_out_nak(dwc_otg_core_if_t * core_if,
  31673. + int32_t val);
  31674. +extern int32_t dwc_otg_get_param_dev_out_nak(dwc_otg_core_if_t * core_if);
  31675. +#define dwc_param_dev_out_nak_default 0
  31676. +
  31677. +extern int dwc_otg_set_param_cont_on_bna(dwc_otg_core_if_t * core_if,
  31678. + int32_t val);
  31679. +extern int32_t dwc_otg_get_param_cont_on_bna(dwc_otg_core_if_t * core_if);
  31680. +#define dwc_param_cont_on_bna_default 0
  31681. +
  31682. +extern int dwc_otg_set_param_ahb_single(dwc_otg_core_if_t * core_if,
  31683. + int32_t val);
  31684. +extern int32_t dwc_otg_get_param_ahb_single(dwc_otg_core_if_t * core_if);
  31685. +#define dwc_param_ahb_single_default 0
  31686. +
  31687. +extern int dwc_otg_set_param_otg_ver(dwc_otg_core_if_t * core_if, int32_t val);
  31688. +extern int32_t dwc_otg_get_param_otg_ver(dwc_otg_core_if_t * core_if);
  31689. +#define dwc_param_otg_ver_default 0
  31690. +
  31691. +/** @} */
  31692. +
  31693. +/** @name Access to registers and bit-fields */
  31694. +
  31695. +/**
  31696. + * Dump core registers and SPRAM
  31697. + */
  31698. +extern void dwc_otg_dump_dev_registers(dwc_otg_core_if_t * _core_if);
  31699. +extern void dwc_otg_dump_spram(dwc_otg_core_if_t * _core_if);
  31700. +extern void dwc_otg_dump_host_registers(dwc_otg_core_if_t * _core_if);
  31701. +extern void dwc_otg_dump_global_registers(dwc_otg_core_if_t * _core_if);
  31702. +
  31703. +/**
  31704. + * Get host negotiation status.
  31705. + */
  31706. +extern uint32_t dwc_otg_get_hnpstatus(dwc_otg_core_if_t * core_if);
  31707. +
  31708. +/**
  31709. + * Get srp status
  31710. + */
  31711. +extern uint32_t dwc_otg_get_srpstatus(dwc_otg_core_if_t * core_if);
  31712. +
  31713. +/**
  31714. + * Set hnpreq bit in the GOTGCTL register.
  31715. + */
  31716. +extern void dwc_otg_set_hnpreq(dwc_otg_core_if_t * core_if, uint32_t val);
  31717. +
  31718. +/**
  31719. + * Get Content of SNPSID register.
  31720. + */
  31721. +extern uint32_t dwc_otg_get_gsnpsid(dwc_otg_core_if_t * core_if);
  31722. +
  31723. +/**
  31724. + * Get current mode.
  31725. + * Returns 0 if in device mode, and 1 if in host mode.
  31726. + */
  31727. +extern uint32_t dwc_otg_get_mode(dwc_otg_core_if_t * core_if);
  31728. +
  31729. +/**
  31730. + * Get value of hnpcapable field in the GUSBCFG register
  31731. + */
  31732. +extern uint32_t dwc_otg_get_hnpcapable(dwc_otg_core_if_t * core_if);
  31733. +/**
  31734. + * Set value of hnpcapable field in the GUSBCFG register
  31735. + */
  31736. +extern void dwc_otg_set_hnpcapable(dwc_otg_core_if_t * core_if, uint32_t val);
  31737. +
  31738. +/**
  31739. + * Get value of srpcapable field in the GUSBCFG register
  31740. + */
  31741. +extern uint32_t dwc_otg_get_srpcapable(dwc_otg_core_if_t * core_if);
  31742. +/**
  31743. + * Set value of srpcapable field in the GUSBCFG register
  31744. + */
  31745. +extern void dwc_otg_set_srpcapable(dwc_otg_core_if_t * core_if, uint32_t val);
  31746. +
  31747. +/**
  31748. + * Get value of devspeed field in the DCFG register
  31749. + */
  31750. +extern uint32_t dwc_otg_get_devspeed(dwc_otg_core_if_t * core_if);
  31751. +/**
  31752. + * Set value of devspeed field in the DCFG register
  31753. + */
  31754. +extern void dwc_otg_set_devspeed(dwc_otg_core_if_t * core_if, uint32_t val);
  31755. +
  31756. +/**
  31757. + * Get the value of busconnected field from the HPRT0 register
  31758. + */
  31759. +extern uint32_t dwc_otg_get_busconnected(dwc_otg_core_if_t * core_if);
  31760. +
  31761. +/**
  31762. + * Gets the device enumeration Speed.
  31763. + */
  31764. +extern uint32_t dwc_otg_get_enumspeed(dwc_otg_core_if_t * core_if);
  31765. +
  31766. +/**
  31767. + * Get value of prtpwr field from the HPRT0 register
  31768. + */
  31769. +extern uint32_t dwc_otg_get_prtpower(dwc_otg_core_if_t * core_if);
  31770. +
  31771. +/**
  31772. + * Get value of flag indicating core state - hibernated or not
  31773. + */
  31774. +extern uint32_t dwc_otg_get_core_state(dwc_otg_core_if_t * core_if);
  31775. +
  31776. +/**
  31777. + * Set value of prtpwr field from the HPRT0 register
  31778. + */
  31779. +extern void dwc_otg_set_prtpower(dwc_otg_core_if_t * core_if, uint32_t val);
  31780. +
  31781. +/**
  31782. + * Get value of prtsusp field from the HPRT0 regsiter
  31783. + */
  31784. +extern uint32_t dwc_otg_get_prtsuspend(dwc_otg_core_if_t * core_if);
  31785. +/**
  31786. + * Set value of prtpwr field from the HPRT0 register
  31787. + */
  31788. +extern void dwc_otg_set_prtsuspend(dwc_otg_core_if_t * core_if, uint32_t val);
  31789. +
  31790. +/**
  31791. + * Get value of ModeChTimEn field from the HCFG regsiter
  31792. + */
  31793. +extern uint32_t dwc_otg_get_mode_ch_tim(dwc_otg_core_if_t * core_if);
  31794. +/**
  31795. + * Set value of ModeChTimEn field from the HCFG regsiter
  31796. + */
  31797. +extern void dwc_otg_set_mode_ch_tim(dwc_otg_core_if_t * core_if, uint32_t val);
  31798. +
  31799. +/**
  31800. + * Get value of Fram Interval field from the HFIR regsiter
  31801. + */
  31802. +extern uint32_t dwc_otg_get_fr_interval(dwc_otg_core_if_t * core_if);
  31803. +/**
  31804. + * Set value of Frame Interval field from the HFIR regsiter
  31805. + */
  31806. +extern void dwc_otg_set_fr_interval(dwc_otg_core_if_t * core_if, uint32_t val);
  31807. +
  31808. +/**
  31809. + * Set value of prtres field from the HPRT0 register
  31810. + *FIXME Remove?
  31811. + */
  31812. +extern void dwc_otg_set_prtresume(dwc_otg_core_if_t * core_if, uint32_t val);
  31813. +
  31814. +/**
  31815. + * Get value of rmtwkupsig bit in DCTL register
  31816. + */
  31817. +extern uint32_t dwc_otg_get_remotewakesig(dwc_otg_core_if_t * core_if);
  31818. +
  31819. +/**
  31820. + * Get value of prt_sleep_sts field from the GLPMCFG register
  31821. + */
  31822. +extern uint32_t dwc_otg_get_lpm_portsleepstatus(dwc_otg_core_if_t * core_if);
  31823. +
  31824. +/**
  31825. + * Get value of rem_wkup_en field from the GLPMCFG register
  31826. + */
  31827. +extern uint32_t dwc_otg_get_lpm_remotewakeenabled(dwc_otg_core_if_t * core_if);
  31828. +
  31829. +/**
  31830. + * Get value of appl_resp field from the GLPMCFG register
  31831. + */
  31832. +extern uint32_t dwc_otg_get_lpmresponse(dwc_otg_core_if_t * core_if);
  31833. +/**
  31834. + * Set value of appl_resp field from the GLPMCFG register
  31835. + */
  31836. +extern void dwc_otg_set_lpmresponse(dwc_otg_core_if_t * core_if, uint32_t val);
  31837. +
  31838. +/**
  31839. + * Get value of hsic_connect field from the GLPMCFG register
  31840. + */
  31841. +extern uint32_t dwc_otg_get_hsic_connect(dwc_otg_core_if_t * core_if);
  31842. +/**
  31843. + * Set value of hsic_connect field from the GLPMCFG register
  31844. + */
  31845. +extern void dwc_otg_set_hsic_connect(dwc_otg_core_if_t * core_if, uint32_t val);
  31846. +
  31847. +/**
  31848. + * Get value of inv_sel_hsic field from the GLPMCFG register.
  31849. + */
  31850. +extern uint32_t dwc_otg_get_inv_sel_hsic(dwc_otg_core_if_t * core_if);
  31851. +/**
  31852. + * Set value of inv_sel_hsic field from the GLPMFG register.
  31853. + */
  31854. +extern void dwc_otg_set_inv_sel_hsic(dwc_otg_core_if_t * core_if, uint32_t val);
  31855. +
  31856. +/*
  31857. + * Some functions for accessing registers
  31858. + */
  31859. +
  31860. +/**
  31861. + * GOTGCTL register
  31862. + */
  31863. +extern uint32_t dwc_otg_get_gotgctl(dwc_otg_core_if_t * core_if);
  31864. +extern void dwc_otg_set_gotgctl(dwc_otg_core_if_t * core_if, uint32_t val);
  31865. +
  31866. +/**
  31867. + * GUSBCFG register
  31868. + */
  31869. +extern uint32_t dwc_otg_get_gusbcfg(dwc_otg_core_if_t * core_if);
  31870. +extern void dwc_otg_set_gusbcfg(dwc_otg_core_if_t * core_if, uint32_t val);
  31871. +
  31872. +/**
  31873. + * GRXFSIZ register
  31874. + */
  31875. +extern uint32_t dwc_otg_get_grxfsiz(dwc_otg_core_if_t * core_if);
  31876. +extern void dwc_otg_set_grxfsiz(dwc_otg_core_if_t * core_if, uint32_t val);
  31877. +
  31878. +/**
  31879. + * GNPTXFSIZ register
  31880. + */
  31881. +extern uint32_t dwc_otg_get_gnptxfsiz(dwc_otg_core_if_t * core_if);
  31882. +extern void dwc_otg_set_gnptxfsiz(dwc_otg_core_if_t * core_if, uint32_t val);
  31883. +
  31884. +extern uint32_t dwc_otg_get_gpvndctl(dwc_otg_core_if_t * core_if);
  31885. +extern void dwc_otg_set_gpvndctl(dwc_otg_core_if_t * core_if, uint32_t val);
  31886. +
  31887. +/**
  31888. + * GGPIO register
  31889. + */
  31890. +extern uint32_t dwc_otg_get_ggpio(dwc_otg_core_if_t * core_if);
  31891. +extern void dwc_otg_set_ggpio(dwc_otg_core_if_t * core_if, uint32_t val);
  31892. +
  31893. +/**
  31894. + * GUID register
  31895. + */
  31896. +extern uint32_t dwc_otg_get_guid(dwc_otg_core_if_t * core_if);
  31897. +extern void dwc_otg_set_guid(dwc_otg_core_if_t * core_if, uint32_t val);
  31898. +
  31899. +/**
  31900. + * HPRT0 register
  31901. + */
  31902. +extern uint32_t dwc_otg_get_hprt0(dwc_otg_core_if_t * core_if);
  31903. +extern void dwc_otg_set_hprt0(dwc_otg_core_if_t * core_if, uint32_t val);
  31904. +
  31905. +/**
  31906. + * GHPTXFSIZE
  31907. + */
  31908. +extern uint32_t dwc_otg_get_hptxfsiz(dwc_otg_core_if_t * core_if);
  31909. +
  31910. +/** @} */
  31911. +
  31912. +#endif /* __DWC_CORE_IF_H__ */
  31913. --- /dev/null
  31914. +++ b/drivers/usb/host/dwc_otg/dwc_otg_dbg.h
  31915. @@ -0,0 +1,117 @@
  31916. +/* ==========================================================================
  31917. + *
  31918. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  31919. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  31920. + * otherwise expressly agreed to in writing between Synopsys and you.
  31921. + *
  31922. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  31923. + * any End User Software License Agreement or Agreement for Licensed Product
  31924. + * with Synopsys or any supplement thereto. You are permitted to use and
  31925. + * redistribute this Software in source and binary forms, with or without
  31926. + * modification, provided that redistributions of source code must retain this
  31927. + * notice. You may not view, use, disclose, copy or distribute this file or
  31928. + * any information contained herein except pursuant to this license grant from
  31929. + * Synopsys. If you do not agree with this notice, including the disclaimer
  31930. + * below, then you are not authorized to use the Software.
  31931. + *
  31932. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  31933. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  31934. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  31935. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  31936. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  31937. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  31938. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  31939. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31940. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  31941. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  31942. + * DAMAGE.
  31943. + * ========================================================================== */
  31944. +
  31945. +#ifndef __DWC_OTG_DBG_H__
  31946. +#define __DWC_OTG_DBG_H__
  31947. +
  31948. +/** @file
  31949. + * This file defines debug levels.
  31950. + * Debugging support vanishes in non-debug builds.
  31951. + */
  31952. +
  31953. +/**
  31954. + * The Debug Level bit-mask variable.
  31955. + */
  31956. +extern uint32_t g_dbg_lvl;
  31957. +/**
  31958. + * Set the Debug Level variable.
  31959. + */
  31960. +static inline uint32_t SET_DEBUG_LEVEL(const uint32_t new)
  31961. +{
  31962. + uint32_t old = g_dbg_lvl;
  31963. + g_dbg_lvl = new;
  31964. + return old;
  31965. +}
  31966. +
  31967. +#define DBG_USER (0x1)
  31968. +/** When debug level has the DBG_CIL bit set, display CIL Debug messages. */
  31969. +#define DBG_CIL (0x2)
  31970. +/** When debug level has the DBG_CILV bit set, display CIL Verbose debug
  31971. + * messages */
  31972. +#define DBG_CILV (0x20)
  31973. +/** When debug level has the DBG_PCD bit set, display PCD (Device) debug
  31974. + * messages */
  31975. +#define DBG_PCD (0x4)
  31976. +/** When debug level has the DBG_PCDV set, display PCD (Device) Verbose debug
  31977. + * messages */
  31978. +#define DBG_PCDV (0x40)
  31979. +/** When debug level has the DBG_HCD bit set, display Host debug messages */
  31980. +#define DBG_HCD (0x8)
  31981. +/** When debug level has the DBG_HCDV bit set, display Verbose Host debug
  31982. + * messages */
  31983. +#define DBG_HCDV (0x80)
  31984. +/** When debug level has the DBG_HCD_URB bit set, display enqueued URBs in host
  31985. + * mode. */
  31986. +#define DBG_HCD_URB (0x800)
  31987. +/** When debug level has the DBG_HCDI bit set, display host interrupt
  31988. + * messages. */
  31989. +#define DBG_HCDI (0x1000)
  31990. +
  31991. +/** When debug level has any bit set, display debug messages */
  31992. +#define DBG_ANY (0xFF)
  31993. +
  31994. +/** All debug messages off */
  31995. +#define DBG_OFF 0
  31996. +
  31997. +/** Prefix string for DWC_DEBUG print macros. */
  31998. +#define USB_DWC "DWC_otg: "
  31999. +
  32000. +/**
  32001. + * Print a debug message when the Global debug level variable contains
  32002. + * the bit defined in <code>lvl</code>.
  32003. + *
  32004. + * @param[in] lvl - Debug level, use one of the DBG_ constants above.
  32005. + * @param[in] x - like printf
  32006. + *
  32007. + * Example:<p>
  32008. + * <code>
  32009. + * DWC_DEBUGPL( DBG_ANY, "%s(%p)\n", __func__, _reg_base_addr);
  32010. + * </code>
  32011. + * <br>
  32012. + * results in:<br>
  32013. + * <code>
  32014. + * usb-DWC_otg: dwc_otg_cil_init(ca867000)
  32015. + * </code>
  32016. + */
  32017. +#ifdef DEBUG
  32018. +
  32019. +# define DWC_DEBUGPL(lvl, x...) do{ if ((lvl)&g_dbg_lvl)__DWC_DEBUG(USB_DWC x ); }while(0)
  32020. +# define DWC_DEBUGP(x...) DWC_DEBUGPL(DBG_ANY, x )
  32021. +
  32022. +# define CHK_DEBUG_LEVEL(level) ((level) & g_dbg_lvl)
  32023. +
  32024. +#else
  32025. +
  32026. +# define DWC_DEBUGPL(lvl, x...) do{}while(0)
  32027. +# define DWC_DEBUGP(x...)
  32028. +
  32029. +# define CHK_DEBUG_LEVEL(level) (0)
  32030. +
  32031. +#endif /*DEBUG*/
  32032. +#endif
  32033. --- /dev/null
  32034. +++ b/drivers/usb/host/dwc_otg/dwc_otg_driver.c
  32035. @@ -0,0 +1,1772 @@
  32036. +/* ==========================================================================
  32037. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_driver.c $
  32038. + * $Revision: #92 $
  32039. + * $Date: 2012/08/10 $
  32040. + * $Change: 2047372 $
  32041. + *
  32042. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  32043. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  32044. + * otherwise expressly agreed to in writing between Synopsys and you.
  32045. + *
  32046. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  32047. + * any End User Software License Agreement or Agreement for Licensed Product
  32048. + * with Synopsys or any supplement thereto. You are permitted to use and
  32049. + * redistribute this Software in source and binary forms, with or without
  32050. + * modification, provided that redistributions of source code must retain this
  32051. + * notice. You may not view, use, disclose, copy or distribute this file or
  32052. + * any information contained herein except pursuant to this license grant from
  32053. + * Synopsys. If you do not agree with this notice, including the disclaimer
  32054. + * below, then you are not authorized to use the Software.
  32055. + *
  32056. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  32057. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  32058. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  32059. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  32060. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  32061. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  32062. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32063. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32064. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  32065. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  32066. + * DAMAGE.
  32067. + * ========================================================================== */
  32068. +
  32069. +/** @file
  32070. + * The dwc_otg_driver module provides the initialization and cleanup entry
  32071. + * points for the DWC_otg driver. This module will be dynamically installed
  32072. + * after Linux is booted using the insmod command. When the module is
  32073. + * installed, the dwc_otg_driver_init function is called. When the module is
  32074. + * removed (using rmmod), the dwc_otg_driver_cleanup function is called.
  32075. + *
  32076. + * This module also defines a data structure for the dwc_otg_driver, which is
  32077. + * used in conjunction with the standard ARM lm_device structure. These
  32078. + * structures allow the OTG driver to comply with the standard Linux driver
  32079. + * model in which devices and drivers are registered with a bus driver. This
  32080. + * has the benefit that Linux can expose attributes of the driver and device
  32081. + * in its special sysfs file system. Users can then read or write files in
  32082. + * this file system to perform diagnostics on the driver components or the
  32083. + * device.
  32084. + */
  32085. +
  32086. +#include "dwc_otg_os_dep.h"
  32087. +#include "dwc_os.h"
  32088. +#include "dwc_otg_dbg.h"
  32089. +#include "dwc_otg_driver.h"
  32090. +#include "dwc_otg_attr.h"
  32091. +#include "dwc_otg_core_if.h"
  32092. +#include "dwc_otg_pcd_if.h"
  32093. +#include "dwc_otg_hcd_if.h"
  32094. +#include "dwc_otg_fiq_fsm.h"
  32095. +
  32096. +#define DWC_DRIVER_VERSION "3.00a 10-AUG-2012"
  32097. +#define DWC_DRIVER_DESC "HS OTG USB Controller driver"
  32098. +
  32099. +bool microframe_schedule=true;
  32100. +
  32101. +static const char dwc_driver_name[] = "dwc_otg";
  32102. +
  32103. +
  32104. +extern int pcd_init(
  32105. +#ifdef LM_INTERFACE
  32106. + struct lm_device *_dev
  32107. +#elif defined(PCI_INTERFACE)
  32108. + struct pci_dev *_dev
  32109. +#elif defined(PLATFORM_INTERFACE)
  32110. + struct platform_device *dev
  32111. +#endif
  32112. + );
  32113. +extern int hcd_init(
  32114. +#ifdef LM_INTERFACE
  32115. + struct lm_device *_dev
  32116. +#elif defined(PCI_INTERFACE)
  32117. + struct pci_dev *_dev
  32118. +#elif defined(PLATFORM_INTERFACE)
  32119. + struct platform_device *dev
  32120. +#endif
  32121. + );
  32122. +
  32123. +extern int pcd_remove(
  32124. +#ifdef LM_INTERFACE
  32125. + struct lm_device *_dev
  32126. +#elif defined(PCI_INTERFACE)
  32127. + struct pci_dev *_dev
  32128. +#elif defined(PLATFORM_INTERFACE)
  32129. + struct platform_device *_dev
  32130. +#endif
  32131. + );
  32132. +
  32133. +extern void hcd_remove(
  32134. +#ifdef LM_INTERFACE
  32135. + struct lm_device *_dev
  32136. +#elif defined(PCI_INTERFACE)
  32137. + struct pci_dev *_dev
  32138. +#elif defined(PLATFORM_INTERFACE)
  32139. + struct platform_device *_dev
  32140. +#endif
  32141. + );
  32142. +
  32143. +extern void dwc_otg_adp_start(dwc_otg_core_if_t * core_if, uint8_t is_host);
  32144. +
  32145. +/*-------------------------------------------------------------------------*/
  32146. +/* Encapsulate the module parameter settings */
  32147. +
  32148. +struct dwc_otg_driver_module_params {
  32149. + int32_t opt;
  32150. + int32_t otg_cap;
  32151. + int32_t dma_enable;
  32152. + int32_t dma_desc_enable;
  32153. + int32_t dma_burst_size;
  32154. + int32_t speed;
  32155. + int32_t host_support_fs_ls_low_power;
  32156. + int32_t host_ls_low_power_phy_clk;
  32157. + int32_t enable_dynamic_fifo;
  32158. + int32_t data_fifo_size;
  32159. + int32_t dev_rx_fifo_size;
  32160. + int32_t dev_nperio_tx_fifo_size;
  32161. + uint32_t dev_perio_tx_fifo_size[MAX_PERIO_FIFOS];
  32162. + int32_t host_rx_fifo_size;
  32163. + int32_t host_nperio_tx_fifo_size;
  32164. + int32_t host_perio_tx_fifo_size;
  32165. + int32_t max_transfer_size;
  32166. + int32_t max_packet_count;
  32167. + int32_t host_channels;
  32168. + int32_t dev_endpoints;
  32169. + int32_t phy_type;
  32170. + int32_t phy_utmi_width;
  32171. + int32_t phy_ulpi_ddr;
  32172. + int32_t phy_ulpi_ext_vbus;
  32173. + int32_t i2c_enable;
  32174. + int32_t ulpi_fs_ls;
  32175. + int32_t ts_dline;
  32176. + int32_t en_multiple_tx_fifo;
  32177. + uint32_t dev_tx_fifo_size[MAX_TX_FIFOS];
  32178. + uint32_t thr_ctl;
  32179. + uint32_t tx_thr_length;
  32180. + uint32_t rx_thr_length;
  32181. + int32_t pti_enable;
  32182. + int32_t mpi_enable;
  32183. + int32_t lpm_enable;
  32184. + int32_t ic_usb_cap;
  32185. + int32_t ahb_thr_ratio;
  32186. + int32_t power_down;
  32187. + int32_t reload_ctl;
  32188. + int32_t dev_out_nak;
  32189. + int32_t cont_on_bna;
  32190. + int32_t ahb_single;
  32191. + int32_t otg_ver;
  32192. + int32_t adp_enable;
  32193. +};
  32194. +
  32195. +static struct dwc_otg_driver_module_params dwc_otg_module_params = {
  32196. + .opt = -1,
  32197. + .otg_cap = -1,
  32198. + .dma_enable = -1,
  32199. + .dma_desc_enable = -1,
  32200. + .dma_burst_size = -1,
  32201. + .speed = -1,
  32202. + .host_support_fs_ls_low_power = -1,
  32203. + .host_ls_low_power_phy_clk = -1,
  32204. + .enable_dynamic_fifo = -1,
  32205. + .data_fifo_size = -1,
  32206. + .dev_rx_fifo_size = -1,
  32207. + .dev_nperio_tx_fifo_size = -1,
  32208. + .dev_perio_tx_fifo_size = {
  32209. + /* dev_perio_tx_fifo_size_1 */
  32210. + -1,
  32211. + -1,
  32212. + -1,
  32213. + -1,
  32214. + -1,
  32215. + -1,
  32216. + -1,
  32217. + -1,
  32218. + -1,
  32219. + -1,
  32220. + -1,
  32221. + -1,
  32222. + -1,
  32223. + -1,
  32224. + -1
  32225. + /* 15 */
  32226. + },
  32227. + .host_rx_fifo_size = -1,
  32228. + .host_nperio_tx_fifo_size = -1,
  32229. + .host_perio_tx_fifo_size = -1,
  32230. + .max_transfer_size = -1,
  32231. + .max_packet_count = -1,
  32232. + .host_channels = -1,
  32233. + .dev_endpoints = -1,
  32234. + .phy_type = -1,
  32235. + .phy_utmi_width = -1,
  32236. + .phy_ulpi_ddr = -1,
  32237. + .phy_ulpi_ext_vbus = -1,
  32238. + .i2c_enable = -1,
  32239. + .ulpi_fs_ls = -1,
  32240. + .ts_dline = -1,
  32241. + .en_multiple_tx_fifo = -1,
  32242. + .dev_tx_fifo_size = {
  32243. + /* dev_tx_fifo_size */
  32244. + -1,
  32245. + -1,
  32246. + -1,
  32247. + -1,
  32248. + -1,
  32249. + -1,
  32250. + -1,
  32251. + -1,
  32252. + -1,
  32253. + -1,
  32254. + -1,
  32255. + -1,
  32256. + -1,
  32257. + -1,
  32258. + -1
  32259. + /* 15 */
  32260. + },
  32261. + .thr_ctl = -1,
  32262. + .tx_thr_length = -1,
  32263. + .rx_thr_length = -1,
  32264. + .pti_enable = -1,
  32265. + .mpi_enable = -1,
  32266. + .lpm_enable = 0,
  32267. + .ic_usb_cap = -1,
  32268. + .ahb_thr_ratio = -1,
  32269. + .power_down = -1,
  32270. + .reload_ctl = -1,
  32271. + .dev_out_nak = -1,
  32272. + .cont_on_bna = -1,
  32273. + .ahb_single = -1,
  32274. + .otg_ver = -1,
  32275. + .adp_enable = -1,
  32276. +};
  32277. +
  32278. +//Global variable to switch the fiq fix on or off
  32279. +bool fiq_enable = 1;
  32280. +// Global variable to enable the split transaction fix
  32281. +bool fiq_fsm_enable = true;
  32282. +//Bulk split-transaction NAK holdoff in microframes
  32283. +uint16_t nak_holdoff = 8;
  32284. +
  32285. +//Force host mode during CIL re-init
  32286. +bool cil_force_host = true;
  32287. +
  32288. +unsigned short fiq_fsm_mask = 0x0F;
  32289. +
  32290. +unsigned short int_ep_interval_min = 0;
  32291. +/**
  32292. + * This function shows the Driver Version.
  32293. + */
  32294. +static ssize_t version_show(struct device_driver *dev, char *buf)
  32295. +{
  32296. + return snprintf(buf, sizeof(DWC_DRIVER_VERSION) + 2, "%s\n",
  32297. + DWC_DRIVER_VERSION);
  32298. +}
  32299. +
  32300. +static DRIVER_ATTR_RO(version);
  32301. +
  32302. +/**
  32303. + * Global Debug Level Mask.
  32304. + */
  32305. +uint32_t g_dbg_lvl = 0; /* OFF */
  32306. +
  32307. +/**
  32308. + * This function shows the driver Debug Level.
  32309. + */
  32310. +static ssize_t debuglevel_show(struct device_driver *drv, char *buf)
  32311. +{
  32312. + return sprintf(buf, "0x%0x\n", g_dbg_lvl);
  32313. +}
  32314. +
  32315. +/**
  32316. + * This function stores the driver Debug Level.
  32317. + */
  32318. +static ssize_t debuglevel_store(struct device_driver *drv, const char *buf,
  32319. + size_t count)
  32320. +{
  32321. + g_dbg_lvl = simple_strtoul(buf, NULL, 16);
  32322. + return count;
  32323. +}
  32324. +
  32325. +static DRIVER_ATTR_RW(debuglevel);
  32326. +
  32327. +/**
  32328. + * This function is called during module intialization
  32329. + * to pass module parameters to the DWC_OTG CORE.
  32330. + */
  32331. +static int set_parameters(dwc_otg_core_if_t * core_if)
  32332. +{
  32333. + int retval = 0;
  32334. + int i;
  32335. +
  32336. + if (dwc_otg_module_params.otg_cap != -1) {
  32337. + retval +=
  32338. + dwc_otg_set_param_otg_cap(core_if,
  32339. + dwc_otg_module_params.otg_cap);
  32340. + }
  32341. + if (dwc_otg_module_params.dma_enable != -1) {
  32342. + retval +=
  32343. + dwc_otg_set_param_dma_enable(core_if,
  32344. + dwc_otg_module_params.
  32345. + dma_enable);
  32346. + }
  32347. + if (dwc_otg_module_params.dma_desc_enable != -1) {
  32348. + retval +=
  32349. + dwc_otg_set_param_dma_desc_enable(core_if,
  32350. + dwc_otg_module_params.
  32351. + dma_desc_enable);
  32352. + }
  32353. + if (dwc_otg_module_params.opt != -1) {
  32354. + retval +=
  32355. + dwc_otg_set_param_opt(core_if, dwc_otg_module_params.opt);
  32356. + }
  32357. + if (dwc_otg_module_params.dma_burst_size != -1) {
  32358. + retval +=
  32359. + dwc_otg_set_param_dma_burst_size(core_if,
  32360. + dwc_otg_module_params.
  32361. + dma_burst_size);
  32362. + }
  32363. + if (dwc_otg_module_params.host_support_fs_ls_low_power != -1) {
  32364. + retval +=
  32365. + dwc_otg_set_param_host_support_fs_ls_low_power(core_if,
  32366. + dwc_otg_module_params.
  32367. + host_support_fs_ls_low_power);
  32368. + }
  32369. + if (dwc_otg_module_params.enable_dynamic_fifo != -1) {
  32370. + retval +=
  32371. + dwc_otg_set_param_enable_dynamic_fifo(core_if,
  32372. + dwc_otg_module_params.
  32373. + enable_dynamic_fifo);
  32374. + }
  32375. + if (dwc_otg_module_params.data_fifo_size != -1) {
  32376. + retval +=
  32377. + dwc_otg_set_param_data_fifo_size(core_if,
  32378. + dwc_otg_module_params.
  32379. + data_fifo_size);
  32380. + }
  32381. + if (dwc_otg_module_params.dev_rx_fifo_size != -1) {
  32382. + retval +=
  32383. + dwc_otg_set_param_dev_rx_fifo_size(core_if,
  32384. + dwc_otg_module_params.
  32385. + dev_rx_fifo_size);
  32386. + }
  32387. + if (dwc_otg_module_params.dev_nperio_tx_fifo_size != -1) {
  32388. + retval +=
  32389. + dwc_otg_set_param_dev_nperio_tx_fifo_size(core_if,
  32390. + dwc_otg_module_params.
  32391. + dev_nperio_tx_fifo_size);
  32392. + }
  32393. + if (dwc_otg_module_params.host_rx_fifo_size != -1) {
  32394. + retval +=
  32395. + dwc_otg_set_param_host_rx_fifo_size(core_if,
  32396. + dwc_otg_module_params.host_rx_fifo_size);
  32397. + }
  32398. + if (dwc_otg_module_params.host_nperio_tx_fifo_size != -1) {
  32399. + retval +=
  32400. + dwc_otg_set_param_host_nperio_tx_fifo_size(core_if,
  32401. + dwc_otg_module_params.
  32402. + host_nperio_tx_fifo_size);
  32403. + }
  32404. + if (dwc_otg_module_params.host_perio_tx_fifo_size != -1) {
  32405. + retval +=
  32406. + dwc_otg_set_param_host_perio_tx_fifo_size(core_if,
  32407. + dwc_otg_module_params.
  32408. + host_perio_tx_fifo_size);
  32409. + }
  32410. + if (dwc_otg_module_params.max_transfer_size != -1) {
  32411. + retval +=
  32412. + dwc_otg_set_param_max_transfer_size(core_if,
  32413. + dwc_otg_module_params.
  32414. + max_transfer_size);
  32415. + }
  32416. + if (dwc_otg_module_params.max_packet_count != -1) {
  32417. + retval +=
  32418. + dwc_otg_set_param_max_packet_count(core_if,
  32419. + dwc_otg_module_params.
  32420. + max_packet_count);
  32421. + }
  32422. + if (dwc_otg_module_params.host_channels != -1) {
  32423. + retval +=
  32424. + dwc_otg_set_param_host_channels(core_if,
  32425. + dwc_otg_module_params.
  32426. + host_channels);
  32427. + }
  32428. + if (dwc_otg_module_params.dev_endpoints != -1) {
  32429. + retval +=
  32430. + dwc_otg_set_param_dev_endpoints(core_if,
  32431. + dwc_otg_module_params.
  32432. + dev_endpoints);
  32433. + }
  32434. + if (dwc_otg_module_params.phy_type != -1) {
  32435. + retval +=
  32436. + dwc_otg_set_param_phy_type(core_if,
  32437. + dwc_otg_module_params.phy_type);
  32438. + }
  32439. + if (dwc_otg_module_params.speed != -1) {
  32440. + retval +=
  32441. + dwc_otg_set_param_speed(core_if,
  32442. + dwc_otg_module_params.speed);
  32443. + }
  32444. + if (dwc_otg_module_params.host_ls_low_power_phy_clk != -1) {
  32445. + retval +=
  32446. + dwc_otg_set_param_host_ls_low_power_phy_clk(core_if,
  32447. + dwc_otg_module_params.
  32448. + host_ls_low_power_phy_clk);
  32449. + }
  32450. + if (dwc_otg_module_params.phy_ulpi_ddr != -1) {
  32451. + retval +=
  32452. + dwc_otg_set_param_phy_ulpi_ddr(core_if,
  32453. + dwc_otg_module_params.
  32454. + phy_ulpi_ddr);
  32455. + }
  32456. + if (dwc_otg_module_params.phy_ulpi_ext_vbus != -1) {
  32457. + retval +=
  32458. + dwc_otg_set_param_phy_ulpi_ext_vbus(core_if,
  32459. + dwc_otg_module_params.
  32460. + phy_ulpi_ext_vbus);
  32461. + }
  32462. + if (dwc_otg_module_params.phy_utmi_width != -1) {
  32463. + retval +=
  32464. + dwc_otg_set_param_phy_utmi_width(core_if,
  32465. + dwc_otg_module_params.
  32466. + phy_utmi_width);
  32467. + }
  32468. + if (dwc_otg_module_params.ulpi_fs_ls != -1) {
  32469. + retval +=
  32470. + dwc_otg_set_param_ulpi_fs_ls(core_if,
  32471. + dwc_otg_module_params.ulpi_fs_ls);
  32472. + }
  32473. + if (dwc_otg_module_params.ts_dline != -1) {
  32474. + retval +=
  32475. + dwc_otg_set_param_ts_dline(core_if,
  32476. + dwc_otg_module_params.ts_dline);
  32477. + }
  32478. + if (dwc_otg_module_params.i2c_enable != -1) {
  32479. + retval +=
  32480. + dwc_otg_set_param_i2c_enable(core_if,
  32481. + dwc_otg_module_params.
  32482. + i2c_enable);
  32483. + }
  32484. + if (dwc_otg_module_params.en_multiple_tx_fifo != -1) {
  32485. + retval +=
  32486. + dwc_otg_set_param_en_multiple_tx_fifo(core_if,
  32487. + dwc_otg_module_params.
  32488. + en_multiple_tx_fifo);
  32489. + }
  32490. + for (i = 0; i < 15; i++) {
  32491. + if (dwc_otg_module_params.dev_perio_tx_fifo_size[i] != -1) {
  32492. + retval +=
  32493. + dwc_otg_set_param_dev_perio_tx_fifo_size(core_if,
  32494. + dwc_otg_module_params.
  32495. + dev_perio_tx_fifo_size
  32496. + [i], i);
  32497. + }
  32498. + }
  32499. +
  32500. + for (i = 0; i < 15; i++) {
  32501. + if (dwc_otg_module_params.dev_tx_fifo_size[i] != -1) {
  32502. + retval += dwc_otg_set_param_dev_tx_fifo_size(core_if,
  32503. + dwc_otg_module_params.
  32504. + dev_tx_fifo_size
  32505. + [i], i);
  32506. + }
  32507. + }
  32508. + if (dwc_otg_module_params.thr_ctl != -1) {
  32509. + retval +=
  32510. + dwc_otg_set_param_thr_ctl(core_if,
  32511. + dwc_otg_module_params.thr_ctl);
  32512. + }
  32513. + if (dwc_otg_module_params.mpi_enable != -1) {
  32514. + retval +=
  32515. + dwc_otg_set_param_mpi_enable(core_if,
  32516. + dwc_otg_module_params.
  32517. + mpi_enable);
  32518. + }
  32519. + if (dwc_otg_module_params.pti_enable != -1) {
  32520. + retval +=
  32521. + dwc_otg_set_param_pti_enable(core_if,
  32522. + dwc_otg_module_params.
  32523. + pti_enable);
  32524. + }
  32525. + if (dwc_otg_module_params.lpm_enable != -1) {
  32526. + retval +=
  32527. + dwc_otg_set_param_lpm_enable(core_if,
  32528. + dwc_otg_module_params.
  32529. + lpm_enable);
  32530. + }
  32531. + if (dwc_otg_module_params.ic_usb_cap != -1) {
  32532. + retval +=
  32533. + dwc_otg_set_param_ic_usb_cap(core_if,
  32534. + dwc_otg_module_params.
  32535. + ic_usb_cap);
  32536. + }
  32537. + if (dwc_otg_module_params.tx_thr_length != -1) {
  32538. + retval +=
  32539. + dwc_otg_set_param_tx_thr_length(core_if,
  32540. + dwc_otg_module_params.tx_thr_length);
  32541. + }
  32542. + if (dwc_otg_module_params.rx_thr_length != -1) {
  32543. + retval +=
  32544. + dwc_otg_set_param_rx_thr_length(core_if,
  32545. + dwc_otg_module_params.
  32546. + rx_thr_length);
  32547. + }
  32548. + if (dwc_otg_module_params.ahb_thr_ratio != -1) {
  32549. + retval +=
  32550. + dwc_otg_set_param_ahb_thr_ratio(core_if,
  32551. + dwc_otg_module_params.ahb_thr_ratio);
  32552. + }
  32553. + if (dwc_otg_module_params.power_down != -1) {
  32554. + retval +=
  32555. + dwc_otg_set_param_power_down(core_if,
  32556. + dwc_otg_module_params.power_down);
  32557. + }
  32558. + if (dwc_otg_module_params.reload_ctl != -1) {
  32559. + retval +=
  32560. + dwc_otg_set_param_reload_ctl(core_if,
  32561. + dwc_otg_module_params.reload_ctl);
  32562. + }
  32563. +
  32564. + if (dwc_otg_module_params.dev_out_nak != -1) {
  32565. + retval +=
  32566. + dwc_otg_set_param_dev_out_nak(core_if,
  32567. + dwc_otg_module_params.dev_out_nak);
  32568. + }
  32569. +
  32570. + if (dwc_otg_module_params.cont_on_bna != -1) {
  32571. + retval +=
  32572. + dwc_otg_set_param_cont_on_bna(core_if,
  32573. + dwc_otg_module_params.cont_on_bna);
  32574. + }
  32575. +
  32576. + if (dwc_otg_module_params.ahb_single != -1) {
  32577. + retval +=
  32578. + dwc_otg_set_param_ahb_single(core_if,
  32579. + dwc_otg_module_params.ahb_single);
  32580. + }
  32581. +
  32582. + if (dwc_otg_module_params.otg_ver != -1) {
  32583. + retval +=
  32584. + dwc_otg_set_param_otg_ver(core_if,
  32585. + dwc_otg_module_params.otg_ver);
  32586. + }
  32587. + if (dwc_otg_module_params.adp_enable != -1) {
  32588. + retval +=
  32589. + dwc_otg_set_param_adp_enable(core_if,
  32590. + dwc_otg_module_params.
  32591. + adp_enable);
  32592. + }
  32593. + return retval;
  32594. +}
  32595. +
  32596. +/**
  32597. + * This function is the top level interrupt handler for the Common
  32598. + * (Device and host modes) interrupts.
  32599. + */
  32600. +static irqreturn_t dwc_otg_common_irq(int irq, void *dev)
  32601. +{
  32602. + int32_t retval = IRQ_NONE;
  32603. +
  32604. + retval = dwc_otg_handle_common_intr(dev);
  32605. + if (retval != 0) {
  32606. + S3C2410X_CLEAR_EINTPEND();
  32607. + }
  32608. + return IRQ_RETVAL(retval);
  32609. +}
  32610. +
  32611. +/**
  32612. + * This function is called when a lm_device is unregistered with the
  32613. + * dwc_otg_driver. This happens, for example, when the rmmod command is
  32614. + * executed. The device may or may not be electrically present. If it is
  32615. + * present, the driver stops device processing. Any resources used on behalf
  32616. + * of this device are freed.
  32617. + *
  32618. + * @param _dev
  32619. + */
  32620. +#ifdef LM_INTERFACE
  32621. +#define REM_RETVAL(n)
  32622. +static void dwc_otg_driver_remove( struct lm_device *_dev )
  32623. +{ dwc_otg_device_t *otg_dev = lm_get_drvdata(_dev);
  32624. +#elif defined(PCI_INTERFACE)
  32625. +#define REM_RETVAL(n)
  32626. +static void dwc_otg_driver_remove( struct pci_dev *_dev )
  32627. +{ dwc_otg_device_t *otg_dev = pci_get_drvdata(_dev);
  32628. +#elif defined(PLATFORM_INTERFACE)
  32629. +#define REM_RETVAL(n) n
  32630. +static int dwc_otg_driver_remove( struct platform_device *_dev )
  32631. +{ dwc_otg_device_t *otg_dev = platform_get_drvdata(_dev);
  32632. +#endif
  32633. +
  32634. + DWC_DEBUGPL(DBG_ANY, "%s(%p) otg_dev %p\n", __func__, _dev, otg_dev);
  32635. +
  32636. + if (!otg_dev) {
  32637. + /* Memory allocation for the dwc_otg_device failed. */
  32638. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev NULL!\n", __func__);
  32639. + return REM_RETVAL(-ENOMEM);
  32640. + }
  32641. +#ifndef DWC_DEVICE_ONLY
  32642. + if (otg_dev->hcd) {
  32643. + hcd_remove(_dev);
  32644. + } else {
  32645. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->hcd NULL!\n", __func__);
  32646. + return REM_RETVAL(-EINVAL);
  32647. + }
  32648. +#endif
  32649. +
  32650. +#ifndef DWC_HOST_ONLY
  32651. + if (otg_dev->pcd) {
  32652. + pcd_remove(_dev);
  32653. + } else {
  32654. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->pcd NULL!\n", __func__);
  32655. + return REM_RETVAL(-EINVAL);
  32656. + }
  32657. +#endif
  32658. + /*
  32659. + * Free the IRQ
  32660. + */
  32661. + if (otg_dev->common_irq_installed) {
  32662. + free_irq(otg_dev->os_dep.irq_num, otg_dev);
  32663. + } else {
  32664. + DWC_DEBUGPL(DBG_ANY, "%s: There is no installed irq!\n", __func__);
  32665. + return REM_RETVAL(-ENXIO);
  32666. + }
  32667. +
  32668. + if (otg_dev->core_if) {
  32669. + dwc_otg_cil_remove(otg_dev->core_if);
  32670. + } else {
  32671. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->core_if NULL!\n", __func__);
  32672. + return REM_RETVAL(-ENXIO);
  32673. + }
  32674. +
  32675. + /*
  32676. + * Remove the device attributes
  32677. + */
  32678. + dwc_otg_attr_remove(_dev);
  32679. +
  32680. + /*
  32681. + * Return the memory.
  32682. + */
  32683. + if (otg_dev->os_dep.base) {
  32684. + iounmap(otg_dev->os_dep.base);
  32685. + }
  32686. + DWC_FREE(otg_dev);
  32687. +
  32688. + /*
  32689. + * Clear the drvdata pointer.
  32690. + */
  32691. +#ifdef LM_INTERFACE
  32692. + lm_set_drvdata(_dev, 0);
  32693. +#elif defined(PCI_INTERFACE)
  32694. + release_mem_region(otg_dev->os_dep.rsrc_start,
  32695. + otg_dev->os_dep.rsrc_len);
  32696. + pci_set_drvdata(_dev, 0);
  32697. +#elif defined(PLATFORM_INTERFACE)
  32698. + platform_set_drvdata(_dev, 0);
  32699. +#endif
  32700. + return REM_RETVAL(0);
  32701. +}
  32702. +
  32703. +/**
  32704. + * This function is called when an lm_device is bound to a
  32705. + * dwc_otg_driver. It creates the driver components required to
  32706. + * control the device (CIL, HCD, and PCD) and it initializes the
  32707. + * device. The driver components are stored in a dwc_otg_device
  32708. + * structure. A reference to the dwc_otg_device is saved in the
  32709. + * lm_device. This allows the driver to access the dwc_otg_device
  32710. + * structure on subsequent calls to driver methods for this device.
  32711. + *
  32712. + * @param _dev Bus device
  32713. + */
  32714. +static int dwc_otg_driver_probe(
  32715. +#ifdef LM_INTERFACE
  32716. + struct lm_device *_dev
  32717. +#elif defined(PCI_INTERFACE)
  32718. + struct pci_dev *_dev,
  32719. + const struct pci_device_id *id
  32720. +#elif defined(PLATFORM_INTERFACE)
  32721. + struct platform_device *_dev
  32722. +#endif
  32723. + )
  32724. +{
  32725. + int retval = 0;
  32726. + dwc_otg_device_t *dwc_otg_device;
  32727. + int devirq;
  32728. +
  32729. + dev_dbg(&_dev->dev, "dwc_otg_driver_probe(%p)\n", _dev);
  32730. +#ifdef LM_INTERFACE
  32731. + dev_dbg(&_dev->dev, "start=0x%08x\n", (unsigned)_dev->resource.start);
  32732. +#elif defined(PCI_INTERFACE)
  32733. + if (!id) {
  32734. + DWC_ERROR("Invalid pci_device_id %p", id);
  32735. + return -EINVAL;
  32736. + }
  32737. +
  32738. + if (!_dev || (pci_enable_device(_dev) < 0)) {
  32739. + DWC_ERROR("Invalid pci_device %p", _dev);
  32740. + return -ENODEV;
  32741. + }
  32742. + dev_dbg(&_dev->dev, "start=0x%08x\n", (unsigned)pci_resource_start(_dev,0));
  32743. + /* other stuff needed as well? */
  32744. +
  32745. +#elif defined(PLATFORM_INTERFACE)
  32746. + dev_dbg(&_dev->dev, "start=0x%08x (len 0x%x)\n",
  32747. + (unsigned)_dev->resource->start,
  32748. + (unsigned)(_dev->resource->end - _dev->resource->start));
  32749. +#endif
  32750. +
  32751. + dwc_otg_device = DWC_ALLOC(sizeof(dwc_otg_device_t));
  32752. +
  32753. + if (!dwc_otg_device) {
  32754. + dev_err(&_dev->dev, "kmalloc of dwc_otg_device failed\n");
  32755. + return -ENOMEM;
  32756. + }
  32757. +
  32758. + memset(dwc_otg_device, 0, sizeof(*dwc_otg_device));
  32759. + dwc_otg_device->os_dep.reg_offset = 0xFFFFFFFF;
  32760. + dwc_otg_device->os_dep.platformdev = _dev;
  32761. +
  32762. + /*
  32763. + * Map the DWC_otg Core memory into virtual address space.
  32764. + */
  32765. +#ifdef LM_INTERFACE
  32766. + dwc_otg_device->os_dep.base = ioremap(_dev->resource.start, SZ_256K);
  32767. +
  32768. + if (!dwc_otg_device->os_dep.base) {
  32769. + dev_err(&_dev->dev, "ioremap() failed\n");
  32770. + DWC_FREE(dwc_otg_device);
  32771. + return -ENOMEM;
  32772. + }
  32773. + dev_dbg(&_dev->dev, "base=0x%08x\n",
  32774. + (unsigned)dwc_otg_device->os_dep.base);
  32775. +#elif defined(PCI_INTERFACE)
  32776. + _dev->current_state = PCI_D0;
  32777. + _dev->dev.power.power_state = PMSG_ON;
  32778. +
  32779. + if (!_dev->irq) {
  32780. + DWC_ERROR("Found HC with no IRQ. Check BIOS/PCI %s setup!",
  32781. + pci_name(_dev));
  32782. + iounmap(dwc_otg_device->os_dep.base);
  32783. + DWC_FREE(dwc_otg_device);
  32784. + return -ENODEV;
  32785. + }
  32786. +
  32787. + dwc_otg_device->os_dep.rsrc_start = pci_resource_start(_dev, 0);
  32788. + dwc_otg_device->os_dep.rsrc_len = pci_resource_len(_dev, 0);
  32789. + DWC_DEBUGPL(DBG_ANY, "PCI resource: start=%08x, len=%08x\n",
  32790. + (unsigned)dwc_otg_device->os_dep.rsrc_start,
  32791. + (unsigned)dwc_otg_device->os_dep.rsrc_len);
  32792. + if (!request_mem_region
  32793. + (dwc_otg_device->os_dep.rsrc_start, dwc_otg_device->os_dep.rsrc_len,
  32794. + "dwc_otg")) {
  32795. + dev_dbg(&_dev->dev, "error requesting memory\n");
  32796. + iounmap(dwc_otg_device->os_dep.base);
  32797. + DWC_FREE(dwc_otg_device);
  32798. + return -EFAULT;
  32799. + }
  32800. +
  32801. + dwc_otg_device->os_dep.base =
  32802. + ioremap(dwc_otg_device->os_dep.rsrc_start,
  32803. + dwc_otg_device->os_dep.rsrc_len);
  32804. + if (dwc_otg_device->os_dep.base == NULL) {
  32805. + dev_dbg(&_dev->dev, "error mapping memory\n");
  32806. + release_mem_region(dwc_otg_device->os_dep.rsrc_start,
  32807. + dwc_otg_device->os_dep.rsrc_len);
  32808. + iounmap(dwc_otg_device->os_dep.base);
  32809. + DWC_FREE(dwc_otg_device);
  32810. + return -EFAULT;
  32811. + }
  32812. + dev_dbg(&_dev->dev, "base=0x%p (before adjust) \n",
  32813. + dwc_otg_device->os_dep.base);
  32814. + dwc_otg_device->os_dep.base = (char *)dwc_otg_device->os_dep.base;
  32815. + dev_dbg(&_dev->dev, "base=0x%p (after adjust) \n",
  32816. + dwc_otg_device->os_dep.base);
  32817. + dev_dbg(&_dev->dev, "%s: mapped PA 0x%x to VA 0x%p\n", __func__,
  32818. + (unsigned)dwc_otg_device->os_dep.rsrc_start,
  32819. + dwc_otg_device->os_dep.base);
  32820. +
  32821. + pci_set_master(_dev);
  32822. + pci_set_drvdata(_dev, dwc_otg_device);
  32823. +#elif defined(PLATFORM_INTERFACE)
  32824. + DWC_DEBUGPL(DBG_ANY,"Platform resource: start=%08x, len=%08x\n",
  32825. + _dev->resource->start,
  32826. + _dev->resource->end - _dev->resource->start + 1);
  32827. +#if 1
  32828. + if (!request_mem_region(_dev->resource[0].start,
  32829. + _dev->resource[0].end - _dev->resource[0].start + 1,
  32830. + "dwc_otg")) {
  32831. + dev_dbg(&_dev->dev, "error reserving mapped memory\n");
  32832. + retval = -EFAULT;
  32833. + goto fail;
  32834. + }
  32835. +
  32836. + dwc_otg_device->os_dep.base = ioremap(_dev->resource[0].start,
  32837. + _dev->resource[0].end -
  32838. + _dev->resource[0].start+1);
  32839. + if (fiq_enable)
  32840. + {
  32841. + if (!request_mem_region(_dev->resource[1].start,
  32842. + _dev->resource[1].end - _dev->resource[1].start + 1,
  32843. + "dwc_otg")) {
  32844. + dev_dbg(&_dev->dev, "error reserving mapped memory\n");
  32845. + retval = -EFAULT;
  32846. + goto fail;
  32847. + }
  32848. +
  32849. + dwc_otg_device->os_dep.mphi_base = ioremap(_dev->resource[1].start,
  32850. + _dev->resource[1].end -
  32851. + _dev->resource[1].start + 1);
  32852. + dwc_otg_device->os_dep.use_swirq = (_dev->resource[1].end - _dev->resource[1].start) == 0x200;
  32853. + }
  32854. +
  32855. +#else
  32856. + {
  32857. + struct map_desc desc = {
  32858. + .virtual = IO_ADDRESS((unsigned)_dev->resource->start),
  32859. + .pfn = __phys_to_pfn((unsigned)_dev->resource->start),
  32860. + .length = SZ_128K,
  32861. + .type = MT_DEVICE
  32862. + };
  32863. + iotable_init(&desc, 1);
  32864. + dwc_otg_device->os_dep.base = (void *)desc.virtual;
  32865. + }
  32866. +#endif
  32867. + if (!dwc_otg_device->os_dep.base) {
  32868. + dev_err(&_dev->dev, "ioremap() failed\n");
  32869. + retval = -ENOMEM;
  32870. + goto fail;
  32871. + }
  32872. +#endif
  32873. +
  32874. + /*
  32875. + * Initialize driver data to point to the global DWC_otg
  32876. + * Device structure.
  32877. + */
  32878. +#ifdef LM_INTERFACE
  32879. + lm_set_drvdata(_dev, dwc_otg_device);
  32880. +#elif defined(PLATFORM_INTERFACE)
  32881. + platform_set_drvdata(_dev, dwc_otg_device);
  32882. +#endif
  32883. + dev_dbg(&_dev->dev, "dwc_otg_device=0x%p\n", dwc_otg_device);
  32884. +
  32885. + dwc_otg_device->core_if = dwc_otg_cil_init(dwc_otg_device->os_dep.base);
  32886. + DWC_DEBUGPL(DBG_HCDV, "probe of device %p given core_if %p\n",
  32887. + dwc_otg_device, dwc_otg_device->core_if);//GRAYG
  32888. +
  32889. + if (!dwc_otg_device->core_if) {
  32890. + dev_err(&_dev->dev, "CIL initialization failed!\n");
  32891. + retval = -ENOMEM;
  32892. + goto fail;
  32893. + }
  32894. +
  32895. + dev_dbg(&_dev->dev, "Calling get_gsnpsid\n");
  32896. + /*
  32897. + * Attempt to ensure this device is really a DWC_otg Controller.
  32898. + * Read and verify the SNPSID register contents. The value should be
  32899. + * 0x45F42XXX or 0x45F42XXX, which corresponds to either "OT2" or "OTG3",
  32900. + * as in "OTG version 2.XX" or "OTG version 3.XX".
  32901. + */
  32902. +
  32903. + if (((dwc_otg_get_gsnpsid(dwc_otg_device->core_if) & 0xFFFFF000) != 0x4F542000) &&
  32904. + ((dwc_otg_get_gsnpsid(dwc_otg_device->core_if) & 0xFFFFF000) != 0x4F543000)) {
  32905. + dev_err(&_dev->dev, "Bad value for SNPSID: 0x%08x\n",
  32906. + dwc_otg_get_gsnpsid(dwc_otg_device->core_if));
  32907. + retval = -EINVAL;
  32908. + goto fail;
  32909. + }
  32910. +
  32911. + /*
  32912. + * Validate parameter values.
  32913. + */
  32914. + dev_dbg(&_dev->dev, "Calling set_parameters\n");
  32915. + if (set_parameters(dwc_otg_device->core_if)) {
  32916. + retval = -EINVAL;
  32917. + goto fail;
  32918. + }
  32919. +
  32920. + /*
  32921. + * Create Device Attributes in sysfs
  32922. + */
  32923. + dev_dbg(&_dev->dev, "Calling attr_create\n");
  32924. + dwc_otg_attr_create(_dev);
  32925. +
  32926. + /*
  32927. + * Disable the global interrupt until all the interrupt
  32928. + * handlers are installed.
  32929. + */
  32930. + dev_dbg(&_dev->dev, "Calling disable_global_interrupts\n");
  32931. + dwc_otg_disable_global_interrupts(dwc_otg_device->core_if);
  32932. +
  32933. + /*
  32934. + * Install the interrupt handler for the common interrupts before
  32935. + * enabling common interrupts in core_init below.
  32936. + */
  32937. +
  32938. +#if defined(PLATFORM_INTERFACE)
  32939. + devirq = platform_get_irq_byname(_dev, fiq_enable ? "soft" : "usb");
  32940. + if (devirq < 0)
  32941. + devirq = platform_get_irq(_dev, fiq_enable ? 0 : 1);
  32942. +#else
  32943. + devirq = _dev->irq;
  32944. +#endif
  32945. + DWC_DEBUGPL(DBG_CIL, "registering (common) handler for irq%d\n",
  32946. + devirq);
  32947. + dev_dbg(&_dev->dev, "Calling request_irq(%d)\n", devirq);
  32948. + retval = request_irq(devirq, dwc_otg_common_irq,
  32949. + IRQF_SHARED,
  32950. + "dwc_otg", dwc_otg_device);
  32951. + if (retval) {
  32952. + DWC_ERROR("request of irq%d failed\n", devirq);
  32953. + retval = -EBUSY;
  32954. + goto fail;
  32955. + } else {
  32956. + dwc_otg_device->common_irq_installed = 1;
  32957. + }
  32958. + dwc_otg_device->os_dep.irq_num = devirq;
  32959. + dwc_otg_device->os_dep.fiq_num = -EINVAL;
  32960. + if (fiq_enable) {
  32961. + int devfiq = platform_get_irq_byname(_dev, "usb");
  32962. + if (devfiq < 0)
  32963. + devfiq = platform_get_irq(_dev, 1);
  32964. + dwc_otg_device->os_dep.fiq_num = devfiq;
  32965. + }
  32966. +
  32967. +#ifndef IRQF_TRIGGER_LOW
  32968. +#if defined(LM_INTERFACE) || defined(PLATFORM_INTERFACE)
  32969. + dev_dbg(&_dev->dev, "Calling set_irq_type\n");
  32970. + set_irq_type(devirq,
  32971. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30))
  32972. + IRQT_LOW
  32973. +#else
  32974. + IRQ_TYPE_LEVEL_LOW
  32975. +#endif
  32976. + );
  32977. +#endif
  32978. +#endif /*IRQF_TRIGGER_LOW*/
  32979. +
  32980. + /*
  32981. + * Initialize the DWC_otg core.
  32982. + */
  32983. + dev_dbg(&_dev->dev, "Calling dwc_otg_core_init\n");
  32984. + dwc_otg_core_init(dwc_otg_device->core_if);
  32985. +
  32986. +#ifndef DWC_HOST_ONLY
  32987. + /*
  32988. + * Initialize the PCD
  32989. + */
  32990. + dev_dbg(&_dev->dev, "Calling pcd_init\n");
  32991. + retval = pcd_init(_dev);
  32992. + if (retval != 0) {
  32993. + DWC_ERROR("pcd_init failed\n");
  32994. + dwc_otg_device->pcd = NULL;
  32995. + goto fail;
  32996. + }
  32997. +#endif
  32998. +#ifndef DWC_DEVICE_ONLY
  32999. + /*
  33000. + * Initialize the HCD
  33001. + */
  33002. + dev_dbg(&_dev->dev, "Calling hcd_init\n");
  33003. + retval = hcd_init(_dev);
  33004. + if (retval != 0) {
  33005. + DWC_ERROR("hcd_init failed\n");
  33006. + dwc_otg_device->hcd = NULL;
  33007. + goto fail;
  33008. + }
  33009. +#endif
  33010. + /* Recover from drvdata having been overwritten by hcd_init() */
  33011. +#ifdef LM_INTERFACE
  33012. + lm_set_drvdata(_dev, dwc_otg_device);
  33013. +#elif defined(PLATFORM_INTERFACE)
  33014. + platform_set_drvdata(_dev, dwc_otg_device);
  33015. +#elif defined(PCI_INTERFACE)
  33016. + pci_set_drvdata(_dev, dwc_otg_device);
  33017. + dwc_otg_device->os_dep.pcidev = _dev;
  33018. +#endif
  33019. +
  33020. + /*
  33021. + * Enable the global interrupt after all the interrupt
  33022. + * handlers are installed if there is no ADP support else
  33023. + * perform initial actions required for Internal ADP logic.
  33024. + */
  33025. + if (!dwc_otg_get_param_adp_enable(dwc_otg_device->core_if)) {
  33026. + dev_dbg(&_dev->dev, "Calling enable_global_interrupts\n");
  33027. + dwc_otg_enable_global_interrupts(dwc_otg_device->core_if);
  33028. + dev_dbg(&_dev->dev, "Done\n");
  33029. + } else
  33030. + dwc_otg_adp_start(dwc_otg_device->core_if,
  33031. + dwc_otg_is_host_mode(dwc_otg_device->core_if));
  33032. +
  33033. + return 0;
  33034. +
  33035. +fail:
  33036. + dwc_otg_driver_remove(_dev);
  33037. + return retval;
  33038. +}
  33039. +
  33040. +/**
  33041. + * This structure defines the methods to be called by a bus driver
  33042. + * during the lifecycle of a device on that bus. Both drivers and
  33043. + * devices are registered with a bus driver. The bus driver matches
  33044. + * devices to drivers based on information in the device and driver
  33045. + * structures.
  33046. + *
  33047. + * The probe function is called when the bus driver matches a device
  33048. + * to this driver. The remove function is called when a device is
  33049. + * unregistered with the bus driver.
  33050. + */
  33051. +#ifdef LM_INTERFACE
  33052. +static struct lm_driver dwc_otg_driver = {
  33053. + .drv = {.name = (char *)dwc_driver_name,},
  33054. + .probe = dwc_otg_driver_probe,
  33055. + .remove = dwc_otg_driver_remove,
  33056. + // 'suspend' and 'resume' absent
  33057. +};
  33058. +#elif defined(PCI_INTERFACE)
  33059. +static const struct pci_device_id pci_ids[] = { {
  33060. + PCI_DEVICE(0x16c3, 0xabcd),
  33061. + .driver_data =
  33062. + (unsigned long)0xdeadbeef,
  33063. + }, { /* end: all zeroes */ }
  33064. +};
  33065. +
  33066. +MODULE_DEVICE_TABLE(pci, pci_ids);
  33067. +
  33068. +/* pci driver glue; this is a "new style" PCI driver module */
  33069. +static struct pci_driver dwc_otg_driver = {
  33070. + .name = "dwc_otg",
  33071. + .id_table = pci_ids,
  33072. +
  33073. + .probe = dwc_otg_driver_probe,
  33074. + .remove = dwc_otg_driver_remove,
  33075. +
  33076. + .driver = {
  33077. + .name = (char *)dwc_driver_name,
  33078. + },
  33079. +};
  33080. +#elif defined(PLATFORM_INTERFACE)
  33081. +static struct platform_device_id platform_ids[] = {
  33082. + {
  33083. + .name = "bcm2708_usb",
  33084. + .driver_data = (kernel_ulong_t) 0xdeadbeef,
  33085. + },
  33086. + { /* end: all zeroes */ }
  33087. +};
  33088. +MODULE_DEVICE_TABLE(platform, platform_ids);
  33089. +
  33090. +static const struct of_device_id dwc_otg_of_match_table[] = {
  33091. + { .compatible = "brcm,bcm2708-usb", },
  33092. + {},
  33093. +};
  33094. +MODULE_DEVICE_TABLE(of, dwc_otg_of_match_table);
  33095. +
  33096. +static struct platform_driver dwc_otg_driver = {
  33097. + .driver = {
  33098. + .name = (char *)dwc_driver_name,
  33099. + .of_match_table = dwc_otg_of_match_table,
  33100. + },
  33101. + .id_table = platform_ids,
  33102. +
  33103. + .probe = dwc_otg_driver_probe,
  33104. + .remove = dwc_otg_driver_remove,
  33105. + // no 'shutdown', 'suspend', 'resume', 'suspend_late' or 'resume_early'
  33106. +};
  33107. +#endif
  33108. +
  33109. +/**
  33110. + * This function is called when the dwc_otg_driver is installed with the
  33111. + * insmod command. It registers the dwc_otg_driver structure with the
  33112. + * appropriate bus driver. This will cause the dwc_otg_driver_probe function
  33113. + * to be called. In addition, the bus driver will automatically expose
  33114. + * attributes defined for the device and driver in the special sysfs file
  33115. + * system.
  33116. + *
  33117. + * @return
  33118. + */
  33119. +static int __init dwc_otg_driver_init(void)
  33120. +{
  33121. + int retval = 0;
  33122. + int error;
  33123. + struct device_driver *drv;
  33124. +
  33125. + if(fiq_fsm_enable && !fiq_enable) {
  33126. + printk(KERN_WARNING "dwc_otg: fiq_fsm_enable was set without fiq_enable! Correcting.\n");
  33127. + fiq_enable = 1;
  33128. + }
  33129. +
  33130. + printk(KERN_INFO "%s: version %s (%s bus)\n", dwc_driver_name,
  33131. + DWC_DRIVER_VERSION,
  33132. +#ifdef LM_INTERFACE
  33133. + "logicmodule");
  33134. + retval = lm_driver_register(&dwc_otg_driver);
  33135. + drv = &dwc_otg_driver.drv;
  33136. +#elif defined(PCI_INTERFACE)
  33137. + "pci");
  33138. + retval = pci_register_driver(&dwc_otg_driver);
  33139. + drv = &dwc_otg_driver.driver;
  33140. +#elif defined(PLATFORM_INTERFACE)
  33141. + "platform");
  33142. + retval = platform_driver_register(&dwc_otg_driver);
  33143. + drv = &dwc_otg_driver.driver;
  33144. +#endif
  33145. + if (retval < 0) {
  33146. + printk(KERN_ERR "%s retval=%d\n", __func__, retval);
  33147. + return retval;
  33148. + }
  33149. + printk(KERN_DEBUG "dwc_otg: FIQ %s\n", fiq_enable ? "enabled":"disabled");
  33150. + printk(KERN_DEBUG "dwc_otg: NAK holdoff %s\n", nak_holdoff ? "enabled":"disabled");
  33151. + printk(KERN_DEBUG "dwc_otg: FIQ split-transaction FSM %s\n", fiq_fsm_enable ? "enabled":"disabled");
  33152. +
  33153. + error = driver_create_file(drv, &driver_attr_version);
  33154. +#ifdef DEBUG
  33155. + error = driver_create_file(drv, &driver_attr_debuglevel);
  33156. +#endif
  33157. + return retval;
  33158. +}
  33159. +
  33160. +module_init(dwc_otg_driver_init);
  33161. +
  33162. +/**
  33163. + * This function is called when the driver is removed from the kernel
  33164. + * with the rmmod command. The driver unregisters itself with its bus
  33165. + * driver.
  33166. + *
  33167. + */
  33168. +static void __exit dwc_otg_driver_cleanup(void)
  33169. +{
  33170. + printk(KERN_DEBUG "dwc_otg_driver_cleanup()\n");
  33171. +
  33172. +#ifdef LM_INTERFACE
  33173. + driver_remove_file(&dwc_otg_driver.drv, &driver_attr_debuglevel);
  33174. + driver_remove_file(&dwc_otg_driver.drv, &driver_attr_version);
  33175. + lm_driver_unregister(&dwc_otg_driver);
  33176. +#elif defined(PCI_INTERFACE)
  33177. + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_debuglevel);
  33178. + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_version);
  33179. + pci_unregister_driver(&dwc_otg_driver);
  33180. +#elif defined(PLATFORM_INTERFACE)
  33181. + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_debuglevel);
  33182. + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_version);
  33183. + platform_driver_unregister(&dwc_otg_driver);
  33184. +#endif
  33185. +
  33186. + printk(KERN_INFO "%s module removed\n", dwc_driver_name);
  33187. +}
  33188. +
  33189. +module_exit(dwc_otg_driver_cleanup);
  33190. +
  33191. +MODULE_DESCRIPTION(DWC_DRIVER_DESC);
  33192. +MODULE_AUTHOR("Synopsys Inc.");
  33193. +MODULE_LICENSE("GPL");
  33194. +
  33195. +module_param_named(otg_cap, dwc_otg_module_params.otg_cap, int, 0444);
  33196. +MODULE_PARM_DESC(otg_cap, "OTG Capabilities 0=HNP&SRP 1=SRP Only 2=None");
  33197. +module_param_named(opt, dwc_otg_module_params.opt, int, 0444);
  33198. +MODULE_PARM_DESC(opt, "OPT Mode");
  33199. +module_param_named(dma_enable, dwc_otg_module_params.dma_enable, int, 0444);
  33200. +MODULE_PARM_DESC(dma_enable, "DMA Mode 0=Slave 1=DMA enabled");
  33201. +
  33202. +module_param_named(dma_desc_enable, dwc_otg_module_params.dma_desc_enable, int,
  33203. + 0444);
  33204. +MODULE_PARM_DESC(dma_desc_enable,
  33205. + "DMA Desc Mode 0=Address DMA 1=DMA Descriptor enabled");
  33206. +
  33207. +module_param_named(dma_burst_size, dwc_otg_module_params.dma_burst_size, int,
  33208. + 0444);
  33209. +MODULE_PARM_DESC(dma_burst_size,
  33210. + "DMA Burst Size 1, 4, 8, 16, 32, 64, 128, 256");
  33211. +module_param_named(speed, dwc_otg_module_params.speed, int, 0444);
  33212. +MODULE_PARM_DESC(speed, "Speed 0=High Speed 1=Full Speed");
  33213. +module_param_named(host_support_fs_ls_low_power,
  33214. + dwc_otg_module_params.host_support_fs_ls_low_power, int,
  33215. + 0444);
  33216. +MODULE_PARM_DESC(host_support_fs_ls_low_power,
  33217. + "Support Low Power w/FS or LS 0=Support 1=Don't Support");
  33218. +module_param_named(host_ls_low_power_phy_clk,
  33219. + dwc_otg_module_params.host_ls_low_power_phy_clk, int, 0444);
  33220. +MODULE_PARM_DESC(host_ls_low_power_phy_clk,
  33221. + "Low Speed Low Power Clock 0=48Mhz 1=6Mhz");
  33222. +module_param_named(enable_dynamic_fifo,
  33223. + dwc_otg_module_params.enable_dynamic_fifo, int, 0444);
  33224. +MODULE_PARM_DESC(enable_dynamic_fifo, "0=cC Setting 1=Allow Dynamic Sizing");
  33225. +module_param_named(data_fifo_size, dwc_otg_module_params.data_fifo_size, int,
  33226. + 0444);
  33227. +MODULE_PARM_DESC(data_fifo_size,
  33228. + "Total number of words in the data FIFO memory 32-32768");
  33229. +module_param_named(dev_rx_fifo_size, dwc_otg_module_params.dev_rx_fifo_size,
  33230. + int, 0444);
  33231. +MODULE_PARM_DESC(dev_rx_fifo_size, "Number of words in the Rx FIFO 16-32768");
  33232. +module_param_named(dev_nperio_tx_fifo_size,
  33233. + dwc_otg_module_params.dev_nperio_tx_fifo_size, int, 0444);
  33234. +MODULE_PARM_DESC(dev_nperio_tx_fifo_size,
  33235. + "Number of words in the non-periodic Tx FIFO 16-32768");
  33236. +module_param_named(dev_perio_tx_fifo_size_1,
  33237. + dwc_otg_module_params.dev_perio_tx_fifo_size[0], int, 0444);
  33238. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_1,
  33239. + "Number of words in the periodic Tx FIFO 4-768");
  33240. +module_param_named(dev_perio_tx_fifo_size_2,
  33241. + dwc_otg_module_params.dev_perio_tx_fifo_size[1], int, 0444);
  33242. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_2,
  33243. + "Number of words in the periodic Tx FIFO 4-768");
  33244. +module_param_named(dev_perio_tx_fifo_size_3,
  33245. + dwc_otg_module_params.dev_perio_tx_fifo_size[2], int, 0444);
  33246. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_3,
  33247. + "Number of words in the periodic Tx FIFO 4-768");
  33248. +module_param_named(dev_perio_tx_fifo_size_4,
  33249. + dwc_otg_module_params.dev_perio_tx_fifo_size[3], int, 0444);
  33250. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_4,
  33251. + "Number of words in the periodic Tx FIFO 4-768");
  33252. +module_param_named(dev_perio_tx_fifo_size_5,
  33253. + dwc_otg_module_params.dev_perio_tx_fifo_size[4], int, 0444);
  33254. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_5,
  33255. + "Number of words in the periodic Tx FIFO 4-768");
  33256. +module_param_named(dev_perio_tx_fifo_size_6,
  33257. + dwc_otg_module_params.dev_perio_tx_fifo_size[5], int, 0444);
  33258. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_6,
  33259. + "Number of words in the periodic Tx FIFO 4-768");
  33260. +module_param_named(dev_perio_tx_fifo_size_7,
  33261. + dwc_otg_module_params.dev_perio_tx_fifo_size[6], int, 0444);
  33262. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_7,
  33263. + "Number of words in the periodic Tx FIFO 4-768");
  33264. +module_param_named(dev_perio_tx_fifo_size_8,
  33265. + dwc_otg_module_params.dev_perio_tx_fifo_size[7], int, 0444);
  33266. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_8,
  33267. + "Number of words in the periodic Tx FIFO 4-768");
  33268. +module_param_named(dev_perio_tx_fifo_size_9,
  33269. + dwc_otg_module_params.dev_perio_tx_fifo_size[8], int, 0444);
  33270. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_9,
  33271. + "Number of words in the periodic Tx FIFO 4-768");
  33272. +module_param_named(dev_perio_tx_fifo_size_10,
  33273. + dwc_otg_module_params.dev_perio_tx_fifo_size[9], int, 0444);
  33274. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_10,
  33275. + "Number of words in the periodic Tx FIFO 4-768");
  33276. +module_param_named(dev_perio_tx_fifo_size_11,
  33277. + dwc_otg_module_params.dev_perio_tx_fifo_size[10], int, 0444);
  33278. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_11,
  33279. + "Number of words in the periodic Tx FIFO 4-768");
  33280. +module_param_named(dev_perio_tx_fifo_size_12,
  33281. + dwc_otg_module_params.dev_perio_tx_fifo_size[11], int, 0444);
  33282. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_12,
  33283. + "Number of words in the periodic Tx FIFO 4-768");
  33284. +module_param_named(dev_perio_tx_fifo_size_13,
  33285. + dwc_otg_module_params.dev_perio_tx_fifo_size[12], int, 0444);
  33286. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_13,
  33287. + "Number of words in the periodic Tx FIFO 4-768");
  33288. +module_param_named(dev_perio_tx_fifo_size_14,
  33289. + dwc_otg_module_params.dev_perio_tx_fifo_size[13], int, 0444);
  33290. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_14,
  33291. + "Number of words in the periodic Tx FIFO 4-768");
  33292. +module_param_named(dev_perio_tx_fifo_size_15,
  33293. + dwc_otg_module_params.dev_perio_tx_fifo_size[14], int, 0444);
  33294. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_15,
  33295. + "Number of words in the periodic Tx FIFO 4-768");
  33296. +module_param_named(host_rx_fifo_size, dwc_otg_module_params.host_rx_fifo_size,
  33297. + int, 0444);
  33298. +MODULE_PARM_DESC(host_rx_fifo_size, "Number of words in the Rx FIFO 16-32768");
  33299. +module_param_named(host_nperio_tx_fifo_size,
  33300. + dwc_otg_module_params.host_nperio_tx_fifo_size, int, 0444);
  33301. +MODULE_PARM_DESC(host_nperio_tx_fifo_size,
  33302. + "Number of words in the non-periodic Tx FIFO 16-32768");
  33303. +module_param_named(host_perio_tx_fifo_size,
  33304. + dwc_otg_module_params.host_perio_tx_fifo_size, int, 0444);
  33305. +MODULE_PARM_DESC(host_perio_tx_fifo_size,
  33306. + "Number of words in the host periodic Tx FIFO 16-32768");
  33307. +module_param_named(max_transfer_size, dwc_otg_module_params.max_transfer_size,
  33308. + int, 0444);
  33309. +/** @todo Set the max to 512K, modify checks */
  33310. +MODULE_PARM_DESC(max_transfer_size,
  33311. + "The maximum transfer size supported in bytes 2047-65535");
  33312. +module_param_named(max_packet_count, dwc_otg_module_params.max_packet_count,
  33313. + int, 0444);
  33314. +MODULE_PARM_DESC(max_packet_count,
  33315. + "The maximum number of packets in a transfer 15-511");
  33316. +module_param_named(host_channels, dwc_otg_module_params.host_channels, int,
  33317. + 0444);
  33318. +MODULE_PARM_DESC(host_channels,
  33319. + "The number of host channel registers to use 1-16");
  33320. +module_param_named(dev_endpoints, dwc_otg_module_params.dev_endpoints, int,
  33321. + 0444);
  33322. +MODULE_PARM_DESC(dev_endpoints,
  33323. + "The number of endpoints in addition to EP0 available for device mode 1-15");
  33324. +module_param_named(phy_type, dwc_otg_module_params.phy_type, int, 0444);
  33325. +MODULE_PARM_DESC(phy_type, "0=Reserved 1=UTMI+ 2=ULPI");
  33326. +module_param_named(phy_utmi_width, dwc_otg_module_params.phy_utmi_width, int,
  33327. + 0444);
  33328. +MODULE_PARM_DESC(phy_utmi_width, "Specifies the UTMI+ Data Width 8 or 16 bits");
  33329. +module_param_named(phy_ulpi_ddr, dwc_otg_module_params.phy_ulpi_ddr, int, 0444);
  33330. +MODULE_PARM_DESC(phy_ulpi_ddr,
  33331. + "ULPI at double or single data rate 0=Single 1=Double");
  33332. +module_param_named(phy_ulpi_ext_vbus, dwc_otg_module_params.phy_ulpi_ext_vbus,
  33333. + int, 0444);
  33334. +MODULE_PARM_DESC(phy_ulpi_ext_vbus,
  33335. + "ULPI PHY using internal or external vbus 0=Internal");
  33336. +module_param_named(i2c_enable, dwc_otg_module_params.i2c_enable, int, 0444);
  33337. +MODULE_PARM_DESC(i2c_enable, "FS PHY Interface");
  33338. +module_param_named(ulpi_fs_ls, dwc_otg_module_params.ulpi_fs_ls, int, 0444);
  33339. +MODULE_PARM_DESC(ulpi_fs_ls, "ULPI PHY FS/LS mode only");
  33340. +module_param_named(ts_dline, dwc_otg_module_params.ts_dline, int, 0444);
  33341. +MODULE_PARM_DESC(ts_dline, "Term select Dline pulsing for all PHYs");
  33342. +module_param_named(debug, g_dbg_lvl, int, 0444);
  33343. +MODULE_PARM_DESC(debug, "");
  33344. +
  33345. +module_param_named(en_multiple_tx_fifo,
  33346. + dwc_otg_module_params.en_multiple_tx_fifo, int, 0444);
  33347. +MODULE_PARM_DESC(en_multiple_tx_fifo,
  33348. + "Dedicated Non Periodic Tx FIFOs 0=disabled 1=enabled");
  33349. +module_param_named(dev_tx_fifo_size_1,
  33350. + dwc_otg_module_params.dev_tx_fifo_size[0], int, 0444);
  33351. +MODULE_PARM_DESC(dev_tx_fifo_size_1, "Number of words in the Tx FIFO 4-768");
  33352. +module_param_named(dev_tx_fifo_size_2,
  33353. + dwc_otg_module_params.dev_tx_fifo_size[1], int, 0444);
  33354. +MODULE_PARM_DESC(dev_tx_fifo_size_2, "Number of words in the Tx FIFO 4-768");
  33355. +module_param_named(dev_tx_fifo_size_3,
  33356. + dwc_otg_module_params.dev_tx_fifo_size[2], int, 0444);
  33357. +MODULE_PARM_DESC(dev_tx_fifo_size_3, "Number of words in the Tx FIFO 4-768");
  33358. +module_param_named(dev_tx_fifo_size_4,
  33359. + dwc_otg_module_params.dev_tx_fifo_size[3], int, 0444);
  33360. +MODULE_PARM_DESC(dev_tx_fifo_size_4, "Number of words in the Tx FIFO 4-768");
  33361. +module_param_named(dev_tx_fifo_size_5,
  33362. + dwc_otg_module_params.dev_tx_fifo_size[4], int, 0444);
  33363. +MODULE_PARM_DESC(dev_tx_fifo_size_5, "Number of words in the Tx FIFO 4-768");
  33364. +module_param_named(dev_tx_fifo_size_6,
  33365. + dwc_otg_module_params.dev_tx_fifo_size[5], int, 0444);
  33366. +MODULE_PARM_DESC(dev_tx_fifo_size_6, "Number of words in the Tx FIFO 4-768");
  33367. +module_param_named(dev_tx_fifo_size_7,
  33368. + dwc_otg_module_params.dev_tx_fifo_size[6], int, 0444);
  33369. +MODULE_PARM_DESC(dev_tx_fifo_size_7, "Number of words in the Tx FIFO 4-768");
  33370. +module_param_named(dev_tx_fifo_size_8,
  33371. + dwc_otg_module_params.dev_tx_fifo_size[7], int, 0444);
  33372. +MODULE_PARM_DESC(dev_tx_fifo_size_8, "Number of words in the Tx FIFO 4-768");
  33373. +module_param_named(dev_tx_fifo_size_9,
  33374. + dwc_otg_module_params.dev_tx_fifo_size[8], int, 0444);
  33375. +MODULE_PARM_DESC(dev_tx_fifo_size_9, "Number of words in the Tx FIFO 4-768");
  33376. +module_param_named(dev_tx_fifo_size_10,
  33377. + dwc_otg_module_params.dev_tx_fifo_size[9], int, 0444);
  33378. +MODULE_PARM_DESC(dev_tx_fifo_size_10, "Number of words in the Tx FIFO 4-768");
  33379. +module_param_named(dev_tx_fifo_size_11,
  33380. + dwc_otg_module_params.dev_tx_fifo_size[10], int, 0444);
  33381. +MODULE_PARM_DESC(dev_tx_fifo_size_11, "Number of words in the Tx FIFO 4-768");
  33382. +module_param_named(dev_tx_fifo_size_12,
  33383. + dwc_otg_module_params.dev_tx_fifo_size[11], int, 0444);
  33384. +MODULE_PARM_DESC(dev_tx_fifo_size_12, "Number of words in the Tx FIFO 4-768");
  33385. +module_param_named(dev_tx_fifo_size_13,
  33386. + dwc_otg_module_params.dev_tx_fifo_size[12], int, 0444);
  33387. +MODULE_PARM_DESC(dev_tx_fifo_size_13, "Number of words in the Tx FIFO 4-768");
  33388. +module_param_named(dev_tx_fifo_size_14,
  33389. + dwc_otg_module_params.dev_tx_fifo_size[13], int, 0444);
  33390. +MODULE_PARM_DESC(dev_tx_fifo_size_14, "Number of words in the Tx FIFO 4-768");
  33391. +module_param_named(dev_tx_fifo_size_15,
  33392. + dwc_otg_module_params.dev_tx_fifo_size[14], int, 0444);
  33393. +MODULE_PARM_DESC(dev_tx_fifo_size_15, "Number of words in the Tx FIFO 4-768");
  33394. +
  33395. +module_param_named(thr_ctl, dwc_otg_module_params.thr_ctl, int, 0444);
  33396. +MODULE_PARM_DESC(thr_ctl,
  33397. + "Thresholding enable flag bit 0 - non ISO Tx thr., 1 - ISO Tx thr., 2 - Rx thr.- bit 0=disabled 1=enabled");
  33398. +module_param_named(tx_thr_length, dwc_otg_module_params.tx_thr_length, int,
  33399. + 0444);
  33400. +MODULE_PARM_DESC(tx_thr_length, "Tx Threshold length in 32 bit DWORDs");
  33401. +module_param_named(rx_thr_length, dwc_otg_module_params.rx_thr_length, int,
  33402. + 0444);
  33403. +MODULE_PARM_DESC(rx_thr_length, "Rx Threshold length in 32 bit DWORDs");
  33404. +
  33405. +module_param_named(pti_enable, dwc_otg_module_params.pti_enable, int, 0444);
  33406. +module_param_named(mpi_enable, dwc_otg_module_params.mpi_enable, int, 0444);
  33407. +module_param_named(lpm_enable, dwc_otg_module_params.lpm_enable, int, 0444);
  33408. +MODULE_PARM_DESC(lpm_enable, "LPM Enable 0=LPM Disabled 1=LPM Enabled");
  33409. +module_param_named(ic_usb_cap, dwc_otg_module_params.ic_usb_cap, int, 0444);
  33410. +MODULE_PARM_DESC(ic_usb_cap,
  33411. + "IC_USB Capability 0=IC_USB Disabled 1=IC_USB Enabled");
  33412. +module_param_named(ahb_thr_ratio, dwc_otg_module_params.ahb_thr_ratio, int,
  33413. + 0444);
  33414. +MODULE_PARM_DESC(ahb_thr_ratio, "AHB Threshold Ratio");
  33415. +module_param_named(power_down, dwc_otg_module_params.power_down, int, 0444);
  33416. +MODULE_PARM_DESC(power_down, "Power Down Mode");
  33417. +module_param_named(reload_ctl, dwc_otg_module_params.reload_ctl, int, 0444);
  33418. +MODULE_PARM_DESC(reload_ctl, "HFIR Reload Control");
  33419. +module_param_named(dev_out_nak, dwc_otg_module_params.dev_out_nak, int, 0444);
  33420. +MODULE_PARM_DESC(dev_out_nak, "Enable Device OUT NAK");
  33421. +module_param_named(cont_on_bna, dwc_otg_module_params.cont_on_bna, int, 0444);
  33422. +MODULE_PARM_DESC(cont_on_bna, "Enable Enable Continue on BNA");
  33423. +module_param_named(ahb_single, dwc_otg_module_params.ahb_single, int, 0444);
  33424. +MODULE_PARM_DESC(ahb_single, "Enable AHB Single Support");
  33425. +module_param_named(adp_enable, dwc_otg_module_params.adp_enable, int, 0444);
  33426. +MODULE_PARM_DESC(adp_enable, "ADP Enable 0=ADP Disabled 1=ADP Enabled");
  33427. +module_param_named(otg_ver, dwc_otg_module_params.otg_ver, int, 0444);
  33428. +MODULE_PARM_DESC(otg_ver, "OTG revision supported 0=OTG 1.3 1=OTG 2.0");
  33429. +module_param(microframe_schedule, bool, 0444);
  33430. +MODULE_PARM_DESC(microframe_schedule, "Enable the microframe scheduler");
  33431. +
  33432. +module_param(fiq_enable, bool, 0444);
  33433. +MODULE_PARM_DESC(fiq_enable, "Enable the FIQ");
  33434. +module_param(nak_holdoff, ushort, 0644);
  33435. +MODULE_PARM_DESC(nak_holdoff, "Throttle duration for bulk split-transaction endpoints on a NAK. Default 8");
  33436. +module_param(fiq_fsm_enable, bool, 0444);
  33437. +MODULE_PARM_DESC(fiq_fsm_enable, "Enable the FIQ to perform split transactions as defined by fiq_fsm_mask");
  33438. +module_param(fiq_fsm_mask, ushort, 0444);
  33439. +MODULE_PARM_DESC(fiq_fsm_mask, "Bitmask of transactions to perform in the FIQ.\n"
  33440. + "Bit 0 : Non-periodic split transactions\n"
  33441. + "Bit 1 : Periodic split transactions\n"
  33442. + "Bit 2 : High-speed multi-transfer isochronous\n"
  33443. + "All other bits should be set 0.");
  33444. +module_param(int_ep_interval_min, ushort, 0644);
  33445. +MODULE_PARM_DESC(int_ep_interval_min, "Clamp high-speed Interrupt endpoints to a minimum polling interval.\n"
  33446. + "0..1 = Use endpoint default\n"
  33447. + "2..n = Minimum interval n microframes. Use powers of 2.\n");
  33448. +
  33449. +module_param(cil_force_host, bool, 0644);
  33450. +MODULE_PARM_DESC(cil_force_host, "On a connector-ID status change, "
  33451. + "force Host Mode regardless of OTG state.");
  33452. +
  33453. +/** @page "Module Parameters"
  33454. + *
  33455. + * The following parameters may be specified when starting the module.
  33456. + * These parameters define how the DWC_otg controller should be
  33457. + * configured. Parameter values are passed to the CIL initialization
  33458. + * function dwc_otg_cil_init
  33459. + *
  33460. + * Example: <code>modprobe dwc_otg speed=1 otg_cap=1</code>
  33461. + *
  33462. +
  33463. + <table>
  33464. + <tr><td>Parameter Name</td><td>Meaning</td></tr>
  33465. +
  33466. + <tr>
  33467. + <td>otg_cap</td>
  33468. + <td>Specifies the OTG capabilities. The driver will automatically detect the
  33469. + value for this parameter if none is specified.
  33470. + - 0: HNP and SRP capable (default, if available)
  33471. + - 1: SRP Only capable
  33472. + - 2: No HNP/SRP capable
  33473. + </td></tr>
  33474. +
  33475. + <tr>
  33476. + <td>dma_enable</td>
  33477. + <td>Specifies whether to use slave or DMA mode for accessing the data FIFOs.
  33478. + The driver will automatically detect the value for this parameter if none is
  33479. + specified.
  33480. + - 0: Slave
  33481. + - 1: DMA (default, if available)
  33482. + </td></tr>
  33483. +
  33484. + <tr>
  33485. + <td>dma_burst_size</td>
  33486. + <td>The DMA Burst size (applicable only for External DMA Mode).
  33487. + - Values: 1, 4, 8 16, 32, 64, 128, 256 (default 32)
  33488. + </td></tr>
  33489. +
  33490. + <tr>
  33491. + <td>speed</td>
  33492. + <td>Specifies the maximum speed of operation in host and device mode. The
  33493. + actual speed depends on the speed of the attached device and the value of
  33494. + phy_type.
  33495. + - 0: High Speed (default)
  33496. + - 1: Full Speed
  33497. + </td></tr>
  33498. +
  33499. + <tr>
  33500. + <td>host_support_fs_ls_low_power</td>
  33501. + <td>Specifies whether low power mode is supported when attached to a Full
  33502. + Speed or Low Speed device in host mode.
  33503. + - 0: Don't support low power mode (default)
  33504. + - 1: Support low power mode
  33505. + </td></tr>
  33506. +
  33507. + <tr>
  33508. + <td>host_ls_low_power_phy_clk</td>
  33509. + <td>Specifies the PHY clock rate in low power mode when connected to a Low
  33510. + Speed device in host mode. This parameter is applicable only if
  33511. + HOST_SUPPORT_FS_LS_LOW_POWER is enabled.
  33512. + - 0: 48 MHz (default)
  33513. + - 1: 6 MHz
  33514. + </td></tr>
  33515. +
  33516. + <tr>
  33517. + <td>enable_dynamic_fifo</td>
  33518. + <td> Specifies whether FIFOs may be resized by the driver software.
  33519. + - 0: Use cC FIFO size parameters
  33520. + - 1: Allow dynamic FIFO sizing (default)
  33521. + </td></tr>
  33522. +
  33523. + <tr>
  33524. + <td>data_fifo_size</td>
  33525. + <td>Total number of 4-byte words in the data FIFO memory. This memory
  33526. + includes the Rx FIFO, non-periodic Tx FIFO, and periodic Tx FIFOs.
  33527. + - Values: 32 to 32768 (default 8192)
  33528. +
  33529. + Note: The total FIFO memory depth in the FPGA configuration is 8192.
  33530. + </td></tr>
  33531. +
  33532. + <tr>
  33533. + <td>dev_rx_fifo_size</td>
  33534. + <td>Number of 4-byte words in the Rx FIFO in device mode when dynamic
  33535. + FIFO sizing is enabled.
  33536. + - Values: 16 to 32768 (default 1064)
  33537. + </td></tr>
  33538. +
  33539. + <tr>
  33540. + <td>dev_nperio_tx_fifo_size</td>
  33541. + <td>Number of 4-byte words in the non-periodic Tx FIFO in device mode when
  33542. + dynamic FIFO sizing is enabled.
  33543. + - Values: 16 to 32768 (default 1024)
  33544. + </td></tr>
  33545. +
  33546. + <tr>
  33547. + <td>dev_perio_tx_fifo_size_n (n = 1 to 15)</td>
  33548. + <td>Number of 4-byte words in each of the periodic Tx FIFOs in device mode
  33549. + when dynamic FIFO sizing is enabled.
  33550. + - Values: 4 to 768 (default 256)
  33551. + </td></tr>
  33552. +
  33553. + <tr>
  33554. + <td>host_rx_fifo_size</td>
  33555. + <td>Number of 4-byte words in the Rx FIFO in host mode when dynamic FIFO
  33556. + sizing is enabled.
  33557. + - Values: 16 to 32768 (default 1024)
  33558. + </td></tr>
  33559. +
  33560. + <tr>
  33561. + <td>host_nperio_tx_fifo_size</td>
  33562. + <td>Number of 4-byte words in the non-periodic Tx FIFO in host mode when
  33563. + dynamic FIFO sizing is enabled in the core.
  33564. + - Values: 16 to 32768 (default 1024)
  33565. + </td></tr>
  33566. +
  33567. + <tr>
  33568. + <td>host_perio_tx_fifo_size</td>
  33569. + <td>Number of 4-byte words in the host periodic Tx FIFO when dynamic FIFO
  33570. + sizing is enabled.
  33571. + - Values: 16 to 32768 (default 1024)
  33572. + </td></tr>
  33573. +
  33574. + <tr>
  33575. + <td>max_transfer_size</td>
  33576. + <td>The maximum transfer size supported in bytes.
  33577. + - Values: 2047 to 65,535 (default 65,535)
  33578. + </td></tr>
  33579. +
  33580. + <tr>
  33581. + <td>max_packet_count</td>
  33582. + <td>The maximum number of packets in a transfer.
  33583. + - Values: 15 to 511 (default 511)
  33584. + </td></tr>
  33585. +
  33586. + <tr>
  33587. + <td>host_channels</td>
  33588. + <td>The number of host channel registers to use.
  33589. + - Values: 1 to 16 (default 12)
  33590. +
  33591. + Note: The FPGA configuration supports a maximum of 12 host channels.
  33592. + </td></tr>
  33593. +
  33594. + <tr>
  33595. + <td>dev_endpoints</td>
  33596. + <td>The number of endpoints in addition to EP0 available for device mode
  33597. + operations.
  33598. + - Values: 1 to 15 (default 6 IN and OUT)
  33599. +
  33600. + Note: The FPGA configuration supports a maximum of 6 IN and OUT endpoints in
  33601. + addition to EP0.
  33602. + </td></tr>
  33603. +
  33604. + <tr>
  33605. + <td>phy_type</td>
  33606. + <td>Specifies the type of PHY interface to use. By default, the driver will
  33607. + automatically detect the phy_type.
  33608. + - 0: Full Speed
  33609. + - 1: UTMI+ (default, if available)
  33610. + - 2: ULPI
  33611. + </td></tr>
  33612. +
  33613. + <tr>
  33614. + <td>phy_utmi_width</td>
  33615. + <td>Specifies the UTMI+ Data Width. This parameter is applicable for a
  33616. + phy_type of UTMI+. Also, this parameter is applicable only if the
  33617. + OTG_HSPHY_WIDTH cC parameter was set to "8 and 16 bits", meaning that the
  33618. + core has been configured to work at either data path width.
  33619. + - Values: 8 or 16 bits (default 16)
  33620. + </td></tr>
  33621. +
  33622. + <tr>
  33623. + <td>phy_ulpi_ddr</td>
  33624. + <td>Specifies whether the ULPI operates at double or single data rate. This
  33625. + parameter is only applicable if phy_type is ULPI.
  33626. + - 0: single data rate ULPI interface with 8 bit wide data bus (default)
  33627. + - 1: double data rate ULPI interface with 4 bit wide data bus
  33628. + </td></tr>
  33629. +
  33630. + <tr>
  33631. + <td>i2c_enable</td>
  33632. + <td>Specifies whether to use the I2C interface for full speed PHY. This
  33633. + parameter is only applicable if PHY_TYPE is FS.
  33634. + - 0: Disabled (default)
  33635. + - 1: Enabled
  33636. + </td></tr>
  33637. +
  33638. + <tr>
  33639. + <td>ulpi_fs_ls</td>
  33640. + <td>Specifies whether to use ULPI FS/LS mode only.
  33641. + - 0: Disabled (default)
  33642. + - 1: Enabled
  33643. + </td></tr>
  33644. +
  33645. + <tr>
  33646. + <td>ts_dline</td>
  33647. + <td>Specifies whether term select D-Line pulsing for all PHYs is enabled.
  33648. + - 0: Disabled (default)
  33649. + - 1: Enabled
  33650. + </td></tr>
  33651. +
  33652. + <tr>
  33653. + <td>en_multiple_tx_fifo</td>
  33654. + <td>Specifies whether dedicatedto tx fifos are enabled for non periodic IN EPs.
  33655. + The driver will automatically detect the value for this parameter if none is
  33656. + specified.
  33657. + - 0: Disabled
  33658. + - 1: Enabled (default, if available)
  33659. + </td></tr>
  33660. +
  33661. + <tr>
  33662. + <td>dev_tx_fifo_size_n (n = 1 to 15)</td>
  33663. + <td>Number of 4-byte words in each of the Tx FIFOs in device mode
  33664. + when dynamic FIFO sizing is enabled.
  33665. + - Values: 4 to 768 (default 256)
  33666. + </td></tr>
  33667. +
  33668. + <tr>
  33669. + <td>tx_thr_length</td>
  33670. + <td>Transmit Threshold length in 32 bit double words
  33671. + - Values: 8 to 128 (default 64)
  33672. + </td></tr>
  33673. +
  33674. + <tr>
  33675. + <td>rx_thr_length</td>
  33676. + <td>Receive Threshold length in 32 bit double words
  33677. + - Values: 8 to 128 (default 64)
  33678. + </td></tr>
  33679. +
  33680. +<tr>
  33681. + <td>thr_ctl</td>
  33682. + <td>Specifies whether to enable Thresholding for Device mode. Bits 0, 1, 2 of
  33683. + this parmater specifies if thresholding is enabled for non-Iso Tx, Iso Tx and
  33684. + Rx transfers accordingly.
  33685. + The driver will automatically detect the value for this parameter if none is
  33686. + specified.
  33687. + - Values: 0 to 7 (default 0)
  33688. + Bit values indicate:
  33689. + - 0: Thresholding disabled
  33690. + - 1: Thresholding enabled
  33691. + </td></tr>
  33692. +
  33693. +<tr>
  33694. + <td>dma_desc_enable</td>
  33695. + <td>Specifies whether to enable Descriptor DMA mode.
  33696. + The driver will automatically detect the value for this parameter if none is
  33697. + specified.
  33698. + - 0: Descriptor DMA disabled
  33699. + - 1: Descriptor DMA (default, if available)
  33700. + </td></tr>
  33701. +
  33702. +<tr>
  33703. + <td>mpi_enable</td>
  33704. + <td>Specifies whether to enable MPI enhancement mode.
  33705. + The driver will automatically detect the value for this parameter if none is
  33706. + specified.
  33707. + - 0: MPI disabled (default)
  33708. + - 1: MPI enable
  33709. + </td></tr>
  33710. +
  33711. +<tr>
  33712. + <td>pti_enable</td>
  33713. + <td>Specifies whether to enable PTI enhancement support.
  33714. + The driver will automatically detect the value for this parameter if none is
  33715. + specified.
  33716. + - 0: PTI disabled (default)
  33717. + - 1: PTI enable
  33718. + </td></tr>
  33719. +
  33720. +<tr>
  33721. + <td>lpm_enable</td>
  33722. + <td>Specifies whether to enable LPM support.
  33723. + The driver will automatically detect the value for this parameter if none is
  33724. + specified.
  33725. + - 0: LPM disabled
  33726. + - 1: LPM enable (default, if available)
  33727. + </td></tr>
  33728. +
  33729. +<tr>
  33730. + <td>ic_usb_cap</td>
  33731. + <td>Specifies whether to enable IC_USB capability.
  33732. + The driver will automatically detect the value for this parameter if none is
  33733. + specified.
  33734. + - 0: IC_USB disabled (default, if available)
  33735. + - 1: IC_USB enable
  33736. + </td></tr>
  33737. +
  33738. +<tr>
  33739. + <td>ahb_thr_ratio</td>
  33740. + <td>Specifies AHB Threshold ratio.
  33741. + - Values: 0 to 3 (default 0)
  33742. + </td></tr>
  33743. +
  33744. +<tr>
  33745. + <td>power_down</td>
  33746. + <td>Specifies Power Down(Hibernation) Mode.
  33747. + The driver will automatically detect the value for this parameter if none is
  33748. + specified.
  33749. + - 0: Power Down disabled (default)
  33750. + - 2: Power Down enabled
  33751. + </td></tr>
  33752. +
  33753. + <tr>
  33754. + <td>reload_ctl</td>
  33755. + <td>Specifies whether dynamic reloading of the HFIR register is allowed during
  33756. + run time. The driver will automatically detect the value for this parameter if
  33757. + none is specified. In case the HFIR value is reloaded when HFIR.RldCtrl == 1'b0
  33758. + the core might misbehave.
  33759. + - 0: Reload Control disabled (default)
  33760. + - 1: Reload Control enabled
  33761. + </td></tr>
  33762. +
  33763. + <tr>
  33764. + <td>dev_out_nak</td>
  33765. + <td>Specifies whether Device OUT NAK enhancement enabled or no.
  33766. + The driver will automatically detect the value for this parameter if
  33767. + none is specified. This parameter is valid only when OTG_EN_DESC_DMA == 1b1.
  33768. + - 0: The core does not set NAK after Bulk OUT transfer complete (default)
  33769. + - 1: The core sets NAK after Bulk OUT transfer complete
  33770. + </td></tr>
  33771. +
  33772. + <tr>
  33773. + <td>cont_on_bna</td>
  33774. + <td>Specifies whether Enable Continue on BNA enabled or no.
  33775. + After receiving BNA interrupt the core disables the endpoint,when the
  33776. + endpoint is re-enabled by the application the
  33777. + - 0: Core starts processing from the DOEPDMA descriptor (default)
  33778. + - 1: Core starts processing from the descriptor which received the BNA.
  33779. + This parameter is valid only when OTG_EN_DESC_DMA == 1b1.
  33780. + </td></tr>
  33781. +
  33782. + <tr>
  33783. + <td>ahb_single</td>
  33784. + <td>This bit when programmed supports SINGLE transfers for remainder data
  33785. + in a transfer for DMA mode of operation.
  33786. + - 0: The remainder data will be sent using INCR burst size (default)
  33787. + - 1: The remainder data will be sent using SINGLE burst size.
  33788. + </td></tr>
  33789. +
  33790. +<tr>
  33791. + <td>adp_enable</td>
  33792. + <td>Specifies whether ADP feature is enabled.
  33793. + The driver will automatically detect the value for this parameter if none is
  33794. + specified.
  33795. + - 0: ADP feature disabled (default)
  33796. + - 1: ADP feature enabled
  33797. + </td></tr>
  33798. +
  33799. + <tr>
  33800. + <td>otg_ver</td>
  33801. + <td>Specifies whether OTG is performing as USB OTG Revision 2.0 or Revision 1.3
  33802. + USB OTG device.
  33803. + - 0: OTG 2.0 support disabled (default)
  33804. + - 1: OTG 2.0 support enabled
  33805. + </td></tr>
  33806. +
  33807. +*/
  33808. --- /dev/null
  33809. +++ b/drivers/usb/host/dwc_otg/dwc_otg_driver.h
  33810. @@ -0,0 +1,86 @@
  33811. +/* ==========================================================================
  33812. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_driver.h $
  33813. + * $Revision: #19 $
  33814. + * $Date: 2010/11/15 $
  33815. + * $Change: 1627671 $
  33816. + *
  33817. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  33818. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  33819. + * otherwise expressly agreed to in writing between Synopsys and you.
  33820. + *
  33821. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  33822. + * any End User Software License Agreement or Agreement for Licensed Product
  33823. + * with Synopsys or any supplement thereto. You are permitted to use and
  33824. + * redistribute this Software in source and binary forms, with or without
  33825. + * modification, provided that redistributions of source code must retain this
  33826. + * notice. You may not view, use, disclose, copy or distribute this file or
  33827. + * any information contained herein except pursuant to this license grant from
  33828. + * Synopsys. If you do not agree with this notice, including the disclaimer
  33829. + * below, then you are not authorized to use the Software.
  33830. + *
  33831. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  33832. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  33833. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33834. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  33835. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  33836. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  33837. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  33838. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33839. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33840. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  33841. + * DAMAGE.
  33842. + * ========================================================================== */
  33843. +
  33844. +#ifndef __DWC_OTG_DRIVER_H__
  33845. +#define __DWC_OTG_DRIVER_H__
  33846. +
  33847. +/** @file
  33848. + * This file contains the interface to the Linux driver.
  33849. + */
  33850. +#include "dwc_otg_os_dep.h"
  33851. +#include "dwc_otg_core_if.h"
  33852. +
  33853. +/* Type declarations */
  33854. +struct dwc_otg_pcd;
  33855. +struct dwc_otg_hcd;
  33856. +
  33857. +/**
  33858. + * This structure is a wrapper that encapsulates the driver components used to
  33859. + * manage a single DWC_otg controller.
  33860. + */
  33861. +typedef struct dwc_otg_device {
  33862. + /** Structure containing OS-dependent stuff. KEEP THIS STRUCT AT THE
  33863. + * VERY BEGINNING OF THE DEVICE STRUCT. OSes such as FreeBSD and NetBSD
  33864. + * require this. */
  33865. + struct os_dependent os_dep;
  33866. +
  33867. + /** Pointer to the core interface structure. */
  33868. + dwc_otg_core_if_t *core_if;
  33869. +
  33870. + /** Pointer to the PCD structure. */
  33871. + struct dwc_otg_pcd *pcd;
  33872. +
  33873. + /** Pointer to the HCD structure. */
  33874. + struct dwc_otg_hcd *hcd;
  33875. +
  33876. + /** Flag to indicate whether the common IRQ handler is installed. */
  33877. + uint8_t common_irq_installed;
  33878. +
  33879. +} dwc_otg_device_t;
  33880. +
  33881. +/*We must clear S3C24XX_EINTPEND external interrupt register
  33882. + * because after clearing in this register trigerred IRQ from
  33883. + * H/W core in kernel interrupt can be occured again before OTG
  33884. + * handlers clear all IRQ sources of Core registers because of
  33885. + * timing latencies and Low Level IRQ Type.
  33886. + */
  33887. +#ifdef CONFIG_MACH_IPMATE
  33888. +#define S3C2410X_CLEAR_EINTPEND() \
  33889. +do { \
  33890. + __raw_writel(1UL << 11,S3C24XX_EINTPEND); \
  33891. +} while (0)
  33892. +#else
  33893. +#define S3C2410X_CLEAR_EINTPEND() do { } while (0)
  33894. +#endif
  33895. +
  33896. +#endif
  33897. --- /dev/null
  33898. +++ b/drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.c
  33899. @@ -0,0 +1,1433 @@
  33900. +/*
  33901. + * dwc_otg_fiq_fsm.c - The finite state machine FIQ
  33902. + *
  33903. + * Copyright (c) 2013 Raspberry Pi Foundation
  33904. + *
  33905. + * Author: Jonathan Bell <[email protected]>
  33906. + * All rights reserved.
  33907. + *
  33908. + * Redistribution and use in source and binary forms, with or without
  33909. + * modification, are permitted provided that the following conditions are met:
  33910. + * * Redistributions of source code must retain the above copyright
  33911. + * notice, this list of conditions and the following disclaimer.
  33912. + * * Redistributions in binary form must reproduce the above copyright
  33913. + * notice, this list of conditions and the following disclaimer in the
  33914. + * documentation and/or other materials provided with the distribution.
  33915. + * * Neither the name of Raspberry Pi nor the
  33916. + * names of its contributors may be used to endorse or promote products
  33917. + * derived from this software without specific prior written permission.
  33918. + *
  33919. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  33920. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  33921. + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  33922. + * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  33923. + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  33924. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  33925. + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  33926. + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33927. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  33928. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33929. + *
  33930. + * This FIQ implements functionality that performs split transactions on
  33931. + * the dwc_otg hardware without any outside intervention. A split transaction
  33932. + * is "queued" by nominating a specific host channel to perform the entirety
  33933. + * of a split transaction. This FIQ will then perform the microframe-precise
  33934. + * scheduling required in each phase of the transaction until completion.
  33935. + *
  33936. + * The FIQ functionality is glued into the Synopsys driver via the entry point
  33937. + * in the FSM enqueue function, and at the exit point in handling a HC interrupt
  33938. + * for a FSM-enabled channel.
  33939. + *
  33940. + * NB: Large parts of this implementation have architecture-specific code.
  33941. + * For porting this functionality to other ARM machines, the minimum is required:
  33942. + * - An interrupt controller allowing the top-level dwc USB interrupt to be routed
  33943. + * to the FIQ
  33944. + * - A method of forcing a software generated interrupt from FIQ mode that then
  33945. + * triggers an IRQ entry (with the dwc USB handler called by this IRQ number)
  33946. + * - Guaranteed interrupt routing such that both the FIQ and SGI occur on the same
  33947. + * processor core - there is no locking between the FIQ and IRQ (aside from
  33948. + * local_fiq_disable)
  33949. + *
  33950. + */
  33951. +
  33952. +#include "dwc_otg_fiq_fsm.h"
  33953. +
  33954. +
  33955. +char buffer[1000*16];
  33956. +int wptr;
  33957. +void notrace _fiq_print(enum fiq_debug_level dbg_lvl, volatile struct fiq_state *state, char *fmt, ...)
  33958. +{
  33959. + enum fiq_debug_level dbg_lvl_req = FIQDBG_ERR;
  33960. + va_list args;
  33961. + char text[17];
  33962. + hfnum_data_t hfnum = { .d32 = FIQ_READ(state->dwc_regs_base + 0x408) };
  33963. +
  33964. + if((dbg_lvl & dbg_lvl_req) || dbg_lvl == FIQDBG_ERR)
  33965. + {
  33966. + snprintf(text, 9, " %4d:%1u ", hfnum.b.frnum/8, hfnum.b.frnum & 7);
  33967. + va_start(args, fmt);
  33968. + vsnprintf(text+8, 9, fmt, args);
  33969. + va_end(args);
  33970. +
  33971. + memcpy(buffer + wptr, text, 16);
  33972. + wptr = (wptr + 16) % sizeof(buffer);
  33973. + }
  33974. +}
  33975. +
  33976. +
  33977. +#ifdef CONFIG_ARM64
  33978. +
  33979. +inline void fiq_fsm_spin_lock(fiq_lock_t *lock)
  33980. +{
  33981. + spin_lock((spinlock_t *)lock);
  33982. +}
  33983. +
  33984. +inline void fiq_fsm_spin_unlock(fiq_lock_t *lock)
  33985. +{
  33986. + spin_unlock((spinlock_t *)lock);
  33987. +}
  33988. +
  33989. +#else
  33990. +
  33991. +/**
  33992. + * fiq_fsm_spin_lock() - ARMv6+ bare bones spinlock
  33993. + * Must be called with local interrupts and FIQ disabled.
  33994. + */
  33995. +#if defined(CONFIG_ARCH_BCM2835) && defined(CONFIG_SMP)
  33996. +inline void fiq_fsm_spin_lock(fiq_lock_t *lock)
  33997. +{
  33998. + unsigned long tmp;
  33999. + uint32_t newval;
  34000. + fiq_lock_t lockval;
  34001. + /* Nested locking, yay. If we are on the same CPU as the fiq, then the disable
  34002. + * will be sufficient. If we are on a different CPU, then the lock protects us. */
  34003. + prefetchw(&lock->slock);
  34004. + asm volatile (
  34005. + "1: ldrex %0, [%3]\n"
  34006. + " add %1, %0, %4\n"
  34007. + " strex %2, %1, [%3]\n"
  34008. + " teq %2, #0\n"
  34009. + " bne 1b"
  34010. + : "=&r" (lockval), "=&r" (newval), "=&r" (tmp)
  34011. + : "r" (&lock->slock), "I" (1 << 16)
  34012. + : "cc");
  34013. +
  34014. + while (lockval.tickets.next != lockval.tickets.owner) {
  34015. + wfe();
  34016. + lockval.tickets.owner = READ_ONCE(lock->tickets.owner);
  34017. + }
  34018. + smp_mb();
  34019. +}
  34020. +#else
  34021. +inline void fiq_fsm_spin_lock(fiq_lock_t *lock) { }
  34022. +#endif
  34023. +
  34024. +/**
  34025. + * fiq_fsm_spin_unlock() - ARMv6+ bare bones spinunlock
  34026. + */
  34027. +#if defined(CONFIG_ARCH_BCM2835) && defined(CONFIG_SMP)
  34028. +inline void fiq_fsm_spin_unlock(fiq_lock_t *lock)
  34029. +{
  34030. + smp_mb();
  34031. + lock->tickets.owner++;
  34032. + dsb_sev();
  34033. +}
  34034. +#else
  34035. +inline void fiq_fsm_spin_unlock(fiq_lock_t *lock) { }
  34036. +#endif
  34037. +
  34038. +#endif
  34039. +
  34040. +/**
  34041. + * fiq_fsm_restart_channel() - Poke channel enable bit for a split transaction
  34042. + * @channel: channel to re-enable
  34043. + */
  34044. +static void fiq_fsm_restart_channel(struct fiq_state *st, int n, int force)
  34045. +{
  34046. + hcchar_data_t hcchar = { .d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR) };
  34047. +
  34048. + hcchar.b.chen = 0;
  34049. + if (st->channel[n].hcchar_copy.b.eptype & 0x1) {
  34050. + hfnum_data_t hfnum = { .d32 = FIQ_READ(st->dwc_regs_base + HFNUM) };
  34051. + /* Hardware bug workaround: update the ssplit index */
  34052. + if (st->channel[n].hcsplt_copy.b.spltena)
  34053. + st->channel[n].expected_uframe = (hfnum.b.frnum + 1) & 0x3FFF;
  34054. +
  34055. + hcchar.b.oddfrm = (hfnum.b.frnum & 0x1) ? 0 : 1;
  34056. + }
  34057. +
  34058. + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR, hcchar.d32);
  34059. + hcchar.d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR);
  34060. + hcchar.b.chen = 1;
  34061. +
  34062. + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR, hcchar.d32);
  34063. + fiq_print(FIQDBG_INT, st, "HCGO %01d %01d", n, force);
  34064. +}
  34065. +
  34066. +/**
  34067. + * fiq_fsm_setup_csplit() - Prepare a host channel for a CSplit transaction stage
  34068. + * @st: Pointer to the channel's state
  34069. + * @n : channel number
  34070. + *
  34071. + * Change host channel registers to perform a complete-split transaction. Being mindful of the
  34072. + * endpoint direction, set control regs up correctly.
  34073. + */
  34074. +static void notrace fiq_fsm_setup_csplit(struct fiq_state *st, int n)
  34075. +{
  34076. + hcsplt_data_t hcsplt = { .d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCSPLT) };
  34077. + hctsiz_data_t hctsiz = { .d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ) };
  34078. +
  34079. + hcsplt.b.compsplt = 1;
  34080. + if (st->channel[n].hcchar_copy.b.epdir == 1) {
  34081. + // If IN, the CSPLIT result contains the data or a hub handshake. hctsiz = maxpacket.
  34082. + hctsiz.b.xfersize = st->channel[n].hctsiz_copy.b.xfersize;
  34083. + } else {
  34084. + // If OUT, the CSPLIT result contains handshake only.
  34085. + hctsiz.b.xfersize = 0;
  34086. + }
  34087. + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCSPLT, hcsplt.d32);
  34088. + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ, hctsiz.d32);
  34089. + mb();
  34090. +}
  34091. +
  34092. +/**
  34093. + * fiq_fsm_restart_np_pending() - Restart a single non-periodic contended transfer
  34094. + * @st: Pointer to the channel's state
  34095. + * @num_channels: Total number of host channels
  34096. + * @orig_channel: Channel index of completed transfer
  34097. + *
  34098. + * In the case where an IN and OUT transfer are simultaneously scheduled to the
  34099. + * same device/EP, inadequate hub implementations will misbehave. Once the first
  34100. + * transfer is complete, a pending non-periodic split can then be issued.
  34101. + */
  34102. +static void notrace fiq_fsm_restart_np_pending(struct fiq_state *st, int num_channels, int orig_channel)
  34103. +{
  34104. + int i;
  34105. + int dev_addr = st->channel[orig_channel].hcchar_copy.b.devaddr;
  34106. + int ep_num = st->channel[orig_channel].hcchar_copy.b.epnum;
  34107. + for (i = 0; i < num_channels; i++) {
  34108. + if (st->channel[i].fsm == FIQ_NP_SSPLIT_PENDING &&
  34109. + st->channel[i].hcchar_copy.b.devaddr == dev_addr &&
  34110. + st->channel[i].hcchar_copy.b.epnum == ep_num) {
  34111. + st->channel[i].fsm = FIQ_NP_SSPLIT_STARTED;
  34112. + fiq_fsm_restart_channel(st, i, 0);
  34113. + break;
  34114. + }
  34115. + }
  34116. +}
  34117. +
  34118. +static inline int notrace fiq_get_xfer_len(struct fiq_state *st, int n)
  34119. +{
  34120. + /* The xfersize register is a bit wonky. For IN transfers, it decrements by the packet size. */
  34121. + hctsiz_data_t hctsiz = { .d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ) };
  34122. +
  34123. + if (st->channel[n].hcchar_copy.b.epdir == 0) {
  34124. + return st->channel[n].hctsiz_copy.b.xfersize;
  34125. + } else {
  34126. + return st->channel[n].hctsiz_copy.b.xfersize - hctsiz.b.xfersize;
  34127. + }
  34128. +
  34129. +}
  34130. +
  34131. +
  34132. +/**
  34133. + * fiq_increment_dma_buf() - update DMA address for bounce buffers after a CSPLIT
  34134. + *
  34135. + * Of use only for IN periodic transfers.
  34136. + */
  34137. +static int notrace fiq_increment_dma_buf(struct fiq_state *st, int num_channels, int n)
  34138. +{
  34139. + hcdma_data_t hcdma;
  34140. + int i = st->channel[n].dma_info.index;
  34141. + int len;
  34142. + struct fiq_dma_blob *blob =
  34143. + (struct fiq_dma_blob *)(uintptr_t)st->dma_base;
  34144. +
  34145. + len = fiq_get_xfer_len(st, n);
  34146. + fiq_print(FIQDBG_INT, st, "LEN: %03d", len);
  34147. + st->channel[n].dma_info.slot_len[i] = len;
  34148. + i++;
  34149. + if (i > 6)
  34150. + BUG();
  34151. +
  34152. + hcdma.d32 = (u32)(uintptr_t)&blob->channel[n].index[i].buf[0];
  34153. + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HC_DMA, hcdma.d32);
  34154. + st->channel[n].dma_info.index = i;
  34155. + return 0;
  34156. +}
  34157. +
  34158. +/**
  34159. + * fiq_reload_hctsiz() - for IN transactions, reset HCTSIZ
  34160. + */
  34161. +static void notrace fiq_fsm_reload_hctsiz(struct fiq_state *st, int n)
  34162. +{
  34163. + hctsiz_data_t hctsiz = { .d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ) };
  34164. + hctsiz.b.xfersize = st->channel[n].hctsiz_copy.b.xfersize;
  34165. + hctsiz.b.pktcnt = 1;
  34166. + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ, hctsiz.d32);
  34167. +}
  34168. +
  34169. +/**
  34170. + * fiq_fsm_reload_hcdma() - for OUT transactions, rewind DMA pointer
  34171. + */
  34172. +static void notrace fiq_fsm_reload_hcdma(struct fiq_state *st, int n)
  34173. +{
  34174. + hcdma_data_t hcdma = st->channel[n].hcdma_copy;
  34175. + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HC_DMA, hcdma.d32);
  34176. +}
  34177. +
  34178. +/**
  34179. + * fiq_iso_out_advance() - update DMA address and split position bits
  34180. + * for isochronous OUT transactions.
  34181. + *
  34182. + * Returns 1 if this is the last packet queued, 0 otherwise. Split-ALL and
  34183. + * Split-BEGIN states are not handled - this is done when the transaction was queued.
  34184. + *
  34185. + * This function must only be called from the FIQ_ISO_OUT_ACTIVE state.
  34186. + */
  34187. +static int notrace fiq_iso_out_advance(struct fiq_state *st, int num_channels, int n)
  34188. +{
  34189. + hcsplt_data_t hcsplt;
  34190. + hctsiz_data_t hctsiz;
  34191. + hcdma_data_t hcdma;
  34192. + struct fiq_dma_blob *blob =
  34193. + (struct fiq_dma_blob *)(uintptr_t)st->dma_base;
  34194. + int last = 0;
  34195. + int i = st->channel[n].dma_info.index;
  34196. +
  34197. + fiq_print(FIQDBG_INT, st, "ADV %01d %01d ", n, i);
  34198. + i++;
  34199. + if (i == 4)
  34200. + last = 1;
  34201. + if (st->channel[n].dma_info.slot_len[i+1] == 255)
  34202. + last = 1;
  34203. +
  34204. + /* New DMA address - address of bounce buffer referred to in index */
  34205. + hcdma.d32 = (u32)(uintptr_t)blob->channel[n].index[i].buf;
  34206. + //hcdma.d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HC_DMA);
  34207. + //hcdma.d32 += st->channel[n].dma_info.slot_len[i];
  34208. + fiq_print(FIQDBG_INT, st, "LAST: %01d ", last);
  34209. + fiq_print(FIQDBG_INT, st, "LEN: %03d", st->channel[n].dma_info.slot_len[i]);
  34210. + hcsplt.d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCSPLT);
  34211. + hctsiz.d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ);
  34212. + hcsplt.b.xactpos = (last) ? ISOC_XACTPOS_END : ISOC_XACTPOS_MID;
  34213. + /* Set up new packet length */
  34214. + hctsiz.b.pktcnt = 1;
  34215. + hctsiz.b.xfersize = st->channel[n].dma_info.slot_len[i];
  34216. + fiq_print(FIQDBG_INT, st, "%08x", hctsiz.d32);
  34217. +
  34218. + st->channel[n].dma_info.index++;
  34219. + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCSPLT, hcsplt.d32);
  34220. + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ, hctsiz.d32);
  34221. + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HC_DMA, hcdma.d32);
  34222. + return last;
  34223. +}
  34224. +
  34225. +/**
  34226. + * fiq_fsm_tt_next_isoc() - queue next pending isochronous out start-split on a TT
  34227. + *
  34228. + * Despite the limitations of the DWC core, we can force a microframe pipeline of
  34229. + * isochronous OUT start-split transactions while waiting for a corresponding other-type
  34230. + * of endpoint to finish its CSPLITs. TTs have big periodic buffers therefore it
  34231. + * is very unlikely that filling the start-split FIFO will cause data loss.
  34232. + * This allows much better interleaving of transactions in an order-independent way-
  34233. + * there is no requirement to prioritise isochronous, just a state-space search has
  34234. + * to be performed on each periodic start-split complete interrupt.
  34235. + */
  34236. +static int notrace fiq_fsm_tt_next_isoc(struct fiq_state *st, int num_channels, int n)
  34237. +{
  34238. + int hub_addr = st->channel[n].hub_addr;
  34239. + int port_addr = st->channel[n].port_addr;
  34240. + int i, poked = 0;
  34241. + for (i = 0; i < num_channels; i++) {
  34242. + if (i == n || st->channel[i].fsm == FIQ_PASSTHROUGH)
  34243. + continue;
  34244. + if (st->channel[i].hub_addr == hub_addr &&
  34245. + st->channel[i].port_addr == port_addr) {
  34246. + switch (st->channel[i].fsm) {
  34247. + case FIQ_PER_ISO_OUT_PENDING:
  34248. + if (st->channel[i].nrpackets == 1) {
  34249. + st->channel[i].fsm = FIQ_PER_ISO_OUT_LAST;
  34250. + } else {
  34251. + st->channel[i].fsm = FIQ_PER_ISO_OUT_ACTIVE;
  34252. + }
  34253. + fiq_fsm_restart_channel(st, i, 0);
  34254. + poked = 1;
  34255. + break;
  34256. +
  34257. + default:
  34258. + break;
  34259. + }
  34260. + }
  34261. + if (poked)
  34262. + break;
  34263. + }
  34264. + return poked;
  34265. +}
  34266. +
  34267. +/**
  34268. + * fiq_fsm_tt_in_use() - search for host channels using this TT
  34269. + * @n: Channel to use as reference
  34270. + *
  34271. + */
  34272. +int notrace noinline fiq_fsm_tt_in_use(struct fiq_state *st, int num_channels, int n)
  34273. +{
  34274. + int hub_addr = st->channel[n].hub_addr;
  34275. + int port_addr = st->channel[n].port_addr;
  34276. + int i, in_use = 0;
  34277. + for (i = 0; i < num_channels; i++) {
  34278. + if (i == n || st->channel[i].fsm == FIQ_PASSTHROUGH)
  34279. + continue;
  34280. + switch (st->channel[i].fsm) {
  34281. + /* TT is reserved for channels that are in the middle of a periodic
  34282. + * split transaction.
  34283. + */
  34284. + case FIQ_PER_SSPLIT_STARTED:
  34285. + case FIQ_PER_CSPLIT_WAIT:
  34286. + case FIQ_PER_CSPLIT_NYET1:
  34287. + //case FIQ_PER_CSPLIT_POLL:
  34288. + case FIQ_PER_ISO_OUT_ACTIVE:
  34289. + case FIQ_PER_ISO_OUT_LAST:
  34290. + if (st->channel[i].hub_addr == hub_addr &&
  34291. + st->channel[i].port_addr == port_addr) {
  34292. + in_use = 1;
  34293. + }
  34294. + break;
  34295. + default:
  34296. + break;
  34297. + }
  34298. + if (in_use)
  34299. + break;
  34300. + }
  34301. + return in_use;
  34302. +}
  34303. +
  34304. +/**
  34305. + * fiq_fsm_more_csplits() - determine whether additional CSPLITs need
  34306. + * to be issued for this IN transaction.
  34307. + *
  34308. + * We cannot tell the inbound PID of a data packet due to hardware limitations.
  34309. + * we need to make an educated guess as to whether we need to queue another CSPLIT
  34310. + * or not. A no-brainer is when we have received enough data to fill the endpoint
  34311. + * size, but for endpoints that give variable-length data then we have to resort
  34312. + * to heuristics.
  34313. + *
  34314. + * We also return whether this is the last CSPLIT to be queued, again based on
  34315. + * heuristics. This is to allow a 1-uframe overlap of periodic split transactions.
  34316. + * Note: requires at least 1 CSPLIT to have been performed prior to being called.
  34317. + */
  34318. +
  34319. +/*
  34320. + * We need some way of guaranteeing if a returned periodic packet of size X
  34321. + * has a DATA0 PID.
  34322. + * The heuristic value of 144 bytes assumes that the received data has maximal
  34323. + * bit-stuffing and the clock frequency of the transmitting device is at the lowest
  34324. + * permissible limit. If the transfer length results in a final packet size
  34325. + * 144 < p <= 188, then an erroneous CSPLIT will be issued.
  34326. + * Also used to ensure that an endpoint will nominally only return a single
  34327. + * complete-split worth of data.
  34328. + */
  34329. +#define DATA0_PID_HEURISTIC 144
  34330. +
  34331. +static int notrace noinline fiq_fsm_more_csplits(struct fiq_state *state, int n, int *probably_last)
  34332. +{
  34333. +
  34334. + int i;
  34335. + int total_len = 0;
  34336. + int more_needed = 1;
  34337. + struct fiq_channel_state *st = &state->channel[n];
  34338. +
  34339. + for (i = 0; i < st->dma_info.index; i++) {
  34340. + total_len += st->dma_info.slot_len[i];
  34341. + }
  34342. +
  34343. + *probably_last = 0;
  34344. +
  34345. + if (st->hcchar_copy.b.eptype == 0x3) {
  34346. + /*
  34347. + * An interrupt endpoint will take max 2 CSPLITs. if we are receiving data
  34348. + * then this is definitely the last CSPLIT.
  34349. + */
  34350. + *probably_last = 1;
  34351. + } else {
  34352. + /* Isoc IN. This is a bit risky if we are the first transaction:
  34353. + * we may have been held off slightly. */
  34354. + if (i > 1 && st->dma_info.slot_len[st->dma_info.index-1] <= DATA0_PID_HEURISTIC) {
  34355. + more_needed = 0;
  34356. + }
  34357. + /* If in the next uframe we will receive enough data to fill the endpoint,
  34358. + * then only issue 1 more csplit.
  34359. + */
  34360. + if (st->hctsiz_copy.b.xfersize - total_len <= DATA0_PID_HEURISTIC)
  34361. + *probably_last = 1;
  34362. + }
  34363. +
  34364. + if (total_len >= st->hctsiz_copy.b.xfersize ||
  34365. + i == 6 || total_len == 0)
  34366. + /* Note: due to bit stuffing it is possible to have > 6 CSPLITs for
  34367. + * a single endpoint. Accepting more would completely break our scheduling mechanism though
  34368. + * - in these extreme cases we will pass through a truncated packet.
  34369. + */
  34370. + more_needed = 0;
  34371. +
  34372. + return more_needed;
  34373. +}
  34374. +
  34375. +/**
  34376. + * fiq_fsm_too_late() - Test transaction for lateness
  34377. + *
  34378. + * If a SSPLIT for a large IN transaction is issued too late in a frame,
  34379. + * the hub will disable the port to the device and respond with ERR handshakes.
  34380. + * The hub status endpoint will not reflect this change.
  34381. + * Returns 1 if we will issue a SSPLIT that will result in a device babble.
  34382. + */
  34383. +int notrace fiq_fsm_too_late(struct fiq_state *st, int n)
  34384. +{
  34385. + int uframe;
  34386. + hfnum_data_t hfnum = { .d32 = FIQ_READ(st->dwc_regs_base + HFNUM) };
  34387. + uframe = hfnum.b.frnum & 0x7;
  34388. + if ((uframe < 6) && (st->channel[n].nrpackets + 1 + uframe > 7)) {
  34389. + return 1;
  34390. + } else {
  34391. + return 0;
  34392. + }
  34393. +}
  34394. +
  34395. +
  34396. +/**
  34397. + * fiq_fsm_start_next_periodic() - A half-arsed attempt at a microframe pipeline
  34398. + *
  34399. + * Search pending transactions in the start-split pending state and queue them.
  34400. + * Don't queue packets in uframe .5 (comes out in .6) (USB2.0 11.18.4).
  34401. + * Note: we specifically don't do isochronous OUT transactions first because better
  34402. + * use of the TT's start-split fifo can be achieved by pipelining an IN before an OUT.
  34403. + */
  34404. +static void notrace noinline fiq_fsm_start_next_periodic(struct fiq_state *st, int num_channels)
  34405. +{
  34406. + int n;
  34407. + hfnum_data_t hfnum = { .d32 = FIQ_READ(st->dwc_regs_base + HFNUM) };
  34408. + if ((hfnum.b.frnum & 0x7) == 5)
  34409. + return;
  34410. + for (n = 0; n < num_channels; n++) {
  34411. + if (st->channel[n].fsm == FIQ_PER_SSPLIT_QUEUED) {
  34412. + /* Check to see if any other transactions are using this TT */
  34413. + if(!fiq_fsm_tt_in_use(st, num_channels, n)) {
  34414. + if (!fiq_fsm_too_late(st, n)) {
  34415. + st->channel[n].fsm = FIQ_PER_SSPLIT_STARTED;
  34416. + fiq_print(FIQDBG_INT, st, "NEXTPER ");
  34417. + fiq_fsm_restart_channel(st, n, 0);
  34418. + } else {
  34419. + st->channel[n].fsm = FIQ_PER_SPLIT_TIMEOUT;
  34420. + }
  34421. + break;
  34422. + }
  34423. + }
  34424. + }
  34425. + for (n = 0; n < num_channels; n++) {
  34426. + if (st->channel[n].fsm == FIQ_PER_ISO_OUT_PENDING) {
  34427. + if (!fiq_fsm_tt_in_use(st, num_channels, n)) {
  34428. + fiq_print(FIQDBG_INT, st, "NEXTISO ");
  34429. + if (st->channel[n].nrpackets == 1)
  34430. + st->channel[n].fsm = FIQ_PER_ISO_OUT_LAST;
  34431. + else
  34432. + st->channel[n].fsm = FIQ_PER_ISO_OUT_ACTIVE;
  34433. + fiq_fsm_restart_channel(st, n, 0);
  34434. + break;
  34435. + }
  34436. + }
  34437. + }
  34438. +}
  34439. +
  34440. +/**
  34441. + * fiq_fsm_update_hs_isoc() - update isochronous frame and transfer data
  34442. + * @state: Pointer to fiq_state
  34443. + * @n: Channel transaction is active on
  34444. + * @hcint: Copy of host channel interrupt register
  34445. + *
  34446. + * Returns 0 if there are no more transactions for this HC to do, 1
  34447. + * otherwise.
  34448. + */
  34449. +static int notrace noinline fiq_fsm_update_hs_isoc(struct fiq_state *state, int n, hcint_data_t hcint)
  34450. +{
  34451. + struct fiq_channel_state *st = &state->channel[n];
  34452. + int xfer_len = 0, nrpackets = 0;
  34453. + hcdma_data_t hcdma;
  34454. + fiq_print(FIQDBG_INT, state, "HSISO %02d", n);
  34455. +
  34456. + xfer_len = fiq_get_xfer_len(state, n);
  34457. + st->hs_isoc_info.iso_desc[st->hs_isoc_info.index].actual_length = xfer_len;
  34458. +
  34459. + st->hs_isoc_info.iso_desc[st->hs_isoc_info.index].status = hcint.d32;
  34460. +
  34461. + st->hs_isoc_info.index++;
  34462. + if (st->hs_isoc_info.index == st->hs_isoc_info.nrframes) {
  34463. + return 0;
  34464. + }
  34465. +
  34466. + /* grab the next DMA address offset from the array */
  34467. + hcdma.d32 = st->hcdma_copy.d32 + st->hs_isoc_info.iso_desc[st->hs_isoc_info.index].offset;
  34468. + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HC_DMA, hcdma.d32);
  34469. +
  34470. + /* We need to set multi_count. This is a bit tricky - has to be set per-transaction as
  34471. + * the core needs to be told to send the correct number. Caution: for IN transfers,
  34472. + * this is always set to the maximum size of the endpoint. */
  34473. + xfer_len = st->hs_isoc_info.iso_desc[st->hs_isoc_info.index].length;
  34474. + /* Integer divide in a FIQ: fun. FIXME: make this not suck */
  34475. + nrpackets = (xfer_len + st->hcchar_copy.b.mps - 1) / st->hcchar_copy.b.mps;
  34476. + if (nrpackets == 0)
  34477. + nrpackets = 1;
  34478. + st->hcchar_copy.b.multicnt = nrpackets;
  34479. + st->hctsiz_copy.b.pktcnt = nrpackets;
  34480. +
  34481. + /* Initial PID also needs to be set */
  34482. + if (st->hcchar_copy.b.epdir == 0) {
  34483. + st->hctsiz_copy.b.xfersize = xfer_len;
  34484. + switch (st->hcchar_copy.b.multicnt) {
  34485. + case 1:
  34486. + st->hctsiz_copy.b.pid = DWC_PID_DATA0;
  34487. + break;
  34488. + case 2:
  34489. + case 3:
  34490. + st->hctsiz_copy.b.pid = DWC_PID_MDATA;
  34491. + break;
  34492. + }
  34493. +
  34494. + } else {
  34495. + st->hctsiz_copy.b.xfersize = nrpackets * st->hcchar_copy.b.mps;
  34496. + switch (st->hcchar_copy.b.multicnt) {
  34497. + case 1:
  34498. + st->hctsiz_copy.b.pid = DWC_PID_DATA0;
  34499. + break;
  34500. + case 2:
  34501. + st->hctsiz_copy.b.pid = DWC_PID_DATA1;
  34502. + break;
  34503. + case 3:
  34504. + st->hctsiz_copy.b.pid = DWC_PID_DATA2;
  34505. + break;
  34506. + }
  34507. + }
  34508. + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ, st->hctsiz_copy.d32);
  34509. + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR, st->hcchar_copy.d32);
  34510. + /* Channel is enabled on hcint handler exit */
  34511. + fiq_print(FIQDBG_INT, state, "HSISOOUT");
  34512. + return 1;
  34513. +}
  34514. +
  34515. +
  34516. +/**
  34517. + * fiq_fsm_do_sof() - FSM start-of-frame interrupt handler
  34518. + * @state: Pointer to the state struct passed from banked FIQ mode registers.
  34519. + * @num_channels: set according to the DWC hardware configuration
  34520. + *
  34521. + * The SOF handler in FSM mode has two functions
  34522. + * 1. Hold off SOF from causing schedule advancement in IRQ context if there's
  34523. + * nothing to do
  34524. + * 2. Advance certain FSM states that require either a microframe delay, or a microframe
  34525. + * of holdoff.
  34526. + *
  34527. + * The second part is architecture-specific to mach-bcm2835 -
  34528. + * a sane interrupt controller would have a mask register for ARM interrupt sources
  34529. + * to be promoted to the nFIQ line, but it doesn't. Instead a single interrupt
  34530. + * number (USB) can be enabled. This means that certain parts of the USB specification
  34531. + * that require "wait a little while, then issue another packet" cannot be fulfilled with
  34532. + * the timing granularity required to achieve optimal throughout. The workaround is to use
  34533. + * the SOF "timer" (125uS) to perform this task.
  34534. + */
  34535. +static int notrace noinline fiq_fsm_do_sof(struct fiq_state *state, int num_channels)
  34536. +{
  34537. + hfnum_data_t hfnum = { .d32 = FIQ_READ(state->dwc_regs_base + HFNUM) };
  34538. + int n;
  34539. + int kick_irq = 0;
  34540. +
  34541. + if ((hfnum.b.frnum & 0x7) == 1) {
  34542. + /* We cannot issue csplits for transactions in the last frame past (n+1).1
  34543. + * Check to see if there are any transactions that are stale.
  34544. + * Boot them out.
  34545. + */
  34546. + for (n = 0; n < num_channels; n++) {
  34547. + switch (state->channel[n].fsm) {
  34548. + case FIQ_PER_CSPLIT_WAIT:
  34549. + case FIQ_PER_CSPLIT_NYET1:
  34550. + case FIQ_PER_CSPLIT_POLL:
  34551. + case FIQ_PER_CSPLIT_LAST:
  34552. + /* Check if we are no longer in the same full-speed frame. */
  34553. + if (((state->channel[n].expected_uframe & 0x3FFF) & ~0x7) <
  34554. + (hfnum.b.frnum & ~0x7))
  34555. + state->channel[n].fsm = FIQ_PER_SPLIT_TIMEOUT;
  34556. + break;
  34557. + default:
  34558. + break;
  34559. + }
  34560. + }
  34561. + }
  34562. +
  34563. + for (n = 0; n < num_channels; n++) {
  34564. + switch (state->channel[n].fsm) {
  34565. +
  34566. + case FIQ_NP_SSPLIT_RETRY:
  34567. + case FIQ_NP_IN_CSPLIT_RETRY:
  34568. + case FIQ_NP_OUT_CSPLIT_RETRY:
  34569. + fiq_fsm_restart_channel(state, n, 0);
  34570. + break;
  34571. +
  34572. + case FIQ_HS_ISOC_SLEEPING:
  34573. + /* Is it time to wake this channel yet? */
  34574. + if (--state->channel[n].uframe_sleeps == 0) {
  34575. + state->channel[n].fsm = FIQ_HS_ISOC_TURBO;
  34576. + fiq_fsm_restart_channel(state, n, 0);
  34577. + }
  34578. + break;
  34579. +
  34580. + case FIQ_PER_SSPLIT_QUEUED:
  34581. + if ((hfnum.b.frnum & 0x7) == 5)
  34582. + break;
  34583. + if(!fiq_fsm_tt_in_use(state, num_channels, n)) {
  34584. + if (!fiq_fsm_too_late(state, n)) {
  34585. + fiq_print(FIQDBG_INT, state, "SOF GO %01d", n);
  34586. + fiq_fsm_restart_channel(state, n, 0);
  34587. + state->channel[n].fsm = FIQ_PER_SSPLIT_STARTED;
  34588. + } else {
  34589. + /* Transaction cannot be started without risking a device babble error */
  34590. + state->channel[n].fsm = FIQ_PER_SPLIT_TIMEOUT;
  34591. + state->haintmsk_saved.b2.chint &= ~(1 << n);
  34592. + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINTMSK, 0);
  34593. + kick_irq |= 1;
  34594. + }
  34595. + }
  34596. + break;
  34597. +
  34598. + case FIQ_PER_ISO_OUT_PENDING:
  34599. + /* Ordinarily, this should be poked after the SSPLIT
  34600. + * complete interrupt for a competing transfer on the same
  34601. + * TT. Doesn't happen for aborted transactions though.
  34602. + */
  34603. + if ((hfnum.b.frnum & 0x7) >= 5)
  34604. + break;
  34605. + if (!fiq_fsm_tt_in_use(state, num_channels, n)) {
  34606. + /* Hardware bug. SOF can sometimes occur after the channel halt interrupt
  34607. + * that caused this.
  34608. + */
  34609. + fiq_fsm_restart_channel(state, n, 0);
  34610. + fiq_print(FIQDBG_INT, state, "SOF ISOC");
  34611. + if (state->channel[n].nrpackets == 1) {
  34612. + state->channel[n].fsm = FIQ_PER_ISO_OUT_LAST;
  34613. + } else {
  34614. + state->channel[n].fsm = FIQ_PER_ISO_OUT_ACTIVE;
  34615. + }
  34616. + }
  34617. + break;
  34618. +
  34619. + case FIQ_PER_CSPLIT_WAIT:
  34620. + /* we are guaranteed to be in this state if and only if the SSPLIT interrupt
  34621. + * occurred when the bus transaction occurred. The SOF interrupt reversal bug
  34622. + * will utterly bugger this up though.
  34623. + */
  34624. + if (hfnum.b.frnum != state->channel[n].expected_uframe) {
  34625. + fiq_print(FIQDBG_INT, state, "SOFCS %d ", n);
  34626. + state->channel[n].fsm = FIQ_PER_CSPLIT_POLL;
  34627. + fiq_fsm_restart_channel(state, n, 0);
  34628. + fiq_fsm_start_next_periodic(state, num_channels);
  34629. +
  34630. + }
  34631. + break;
  34632. +
  34633. + case FIQ_PER_SPLIT_TIMEOUT:
  34634. + case FIQ_DEQUEUE_ISSUED:
  34635. + /* Ugly: we have to force a HCD interrupt.
  34636. + * Poke the mask for the channel in question.
  34637. + * We will take a fake SOF because of this, but
  34638. + * that's OK.
  34639. + */
  34640. + state->haintmsk_saved.b2.chint &= ~(1 << n);
  34641. + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINTMSK, 0);
  34642. + kick_irq |= 1;
  34643. + break;
  34644. +
  34645. + default:
  34646. + break;
  34647. + }
  34648. + }
  34649. +
  34650. + if (state->kick_np_queues ||
  34651. + dwc_frame_num_le(state->next_sched_frame, hfnum.b.frnum))
  34652. + kick_irq |= 1;
  34653. +
  34654. + return !kick_irq;
  34655. +}
  34656. +
  34657. +
  34658. +/**
  34659. + * fiq_fsm_do_hcintr() - FSM host channel interrupt handler
  34660. + * @state: Pointer to the FIQ state struct
  34661. + * @num_channels: Number of channels as per hardware config
  34662. + * @n: channel for which HAINT(i) was raised
  34663. + *
  34664. + * An important property is that only the CHHLT interrupt is unmasked. Unfortunately, AHBerr is as well.
  34665. + */
  34666. +static int notrace noinline fiq_fsm_do_hcintr(struct fiq_state *state, int num_channels, int n)
  34667. +{
  34668. + hcint_data_t hcint;
  34669. + hcintmsk_data_t hcintmsk;
  34670. + hcint_data_t hcint_probe;
  34671. + hcchar_data_t hcchar;
  34672. + int handled = 0;
  34673. + int restart = 0;
  34674. + int last_csplit = 0;
  34675. + int start_next_periodic = 0;
  34676. + struct fiq_channel_state *st = &state->channel[n];
  34677. + hfnum_data_t hfnum;
  34678. +
  34679. + hcint.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINT);
  34680. + hcintmsk.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINTMSK);
  34681. + hcint_probe.d32 = hcint.d32 & hcintmsk.d32;
  34682. +
  34683. + if (st->fsm != FIQ_PASSTHROUGH) {
  34684. + fiq_print(FIQDBG_INT, state, "HC%01d ST%02d", n, st->fsm);
  34685. + fiq_print(FIQDBG_INT, state, "%08x", hcint.d32);
  34686. + }
  34687. +
  34688. + switch (st->fsm) {
  34689. +
  34690. + case FIQ_PASSTHROUGH:
  34691. + case FIQ_DEQUEUE_ISSUED:
  34692. + /* doesn't belong to us, kick it upstairs */
  34693. + break;
  34694. +
  34695. + case FIQ_PASSTHROUGH_ERRORSTATE:
  34696. + /* We are here to emulate the error recovery mechanism of the dwc HCD.
  34697. + * Several interrupts are unmasked if a previous transaction failed - it's
  34698. + * death for the FIQ to attempt to handle them as the channel isn't halted.
  34699. + * Emulate what the HCD does in this situation: mask and continue.
  34700. + * The FSM has no other state setup so this has to be handled out-of-band.
  34701. + */
  34702. + fiq_print(FIQDBG_ERR, state, "ERRST %02d", n);
  34703. + if (hcint_probe.b.nak || hcint_probe.b.ack || hcint_probe.b.datatglerr) {
  34704. + fiq_print(FIQDBG_ERR, state, "RESET %02d", n);
  34705. + /* In some random cases we can get a NAK interrupt coincident with a Xacterr
  34706. + * interrupt, after the device has disappeared.
  34707. + */
  34708. + if (!hcint.b.xacterr)
  34709. + st->nr_errors = 0;
  34710. + hcintmsk.b.nak = 0;
  34711. + hcintmsk.b.ack = 0;
  34712. + hcintmsk.b.datatglerr = 0;
  34713. + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINTMSK, hcintmsk.d32);
  34714. + return 1;
  34715. + }
  34716. + if (hcint_probe.b.chhltd) {
  34717. + fiq_print(FIQDBG_ERR, state, "CHHLT %02d", n);
  34718. + fiq_print(FIQDBG_ERR, state, "%08x", hcint.d32);
  34719. + return 0;
  34720. + }
  34721. + break;
  34722. +
  34723. + /* Non-periodic state groups */
  34724. + case FIQ_NP_SSPLIT_STARTED:
  34725. + case FIQ_NP_SSPLIT_RETRY:
  34726. + /* Got a HCINT for a NP SSPLIT. Expected ACK / NAK / fail */
  34727. + if (hcint.b.ack) {
  34728. + /* SSPLIT complete. For OUT, the data has been sent. For IN, the LS transaction
  34729. + * will start shortly. SOF needs to kick the transaction to prevent a NYET flood.
  34730. + */
  34731. + if(st->hcchar_copy.b.epdir == 1)
  34732. + st->fsm = FIQ_NP_IN_CSPLIT_RETRY;
  34733. + else
  34734. + st->fsm = FIQ_NP_OUT_CSPLIT_RETRY;
  34735. + st->nr_errors = 0;
  34736. + handled = 1;
  34737. + fiq_fsm_setup_csplit(state, n);
  34738. + } else if (hcint.b.nak) {
  34739. + // No buffer space in TT. Retry on a uframe boundary.
  34740. + fiq_fsm_reload_hcdma(state, n);
  34741. + st->fsm = FIQ_NP_SSPLIT_RETRY;
  34742. + handled = 1;
  34743. + } else if (hcint.b.xacterr) {
  34744. + // The only other one we care about is xacterr. This implies HS bus error - retry.
  34745. + st->nr_errors++;
  34746. + if(st->hcchar_copy.b.epdir == 0)
  34747. + fiq_fsm_reload_hcdma(state, n);
  34748. + st->fsm = FIQ_NP_SSPLIT_RETRY;
  34749. + if (st->nr_errors >= 3) {
  34750. + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
  34751. + } else {
  34752. + handled = 1;
  34753. + restart = 1;
  34754. + }
  34755. + } else {
  34756. + st->fsm = FIQ_NP_SPLIT_LS_ABORTED;
  34757. + handled = 0;
  34758. + restart = 0;
  34759. + }
  34760. + break;
  34761. +
  34762. + case FIQ_NP_IN_CSPLIT_RETRY:
  34763. + /* Received a CSPLIT done interrupt.
  34764. + * Expected Data/NAK/STALL/NYET for IN.
  34765. + */
  34766. + if (hcint.b.xfercomp) {
  34767. + /* For IN, data is present. */
  34768. + st->fsm = FIQ_NP_SPLIT_DONE;
  34769. + } else if (hcint.b.nak) {
  34770. + /* no endpoint data. Punt it upstairs */
  34771. + st->fsm = FIQ_NP_SPLIT_DONE;
  34772. + } else if (hcint.b.nyet) {
  34773. + /* CSPLIT NYET - retry on a uframe boundary. */
  34774. + handled = 1;
  34775. + st->nr_errors = 0;
  34776. + } else if (hcint.b.datatglerr) {
  34777. + /* data toggle errors do not set the xfercomp bit. */
  34778. + st->fsm = FIQ_NP_SPLIT_LS_ABORTED;
  34779. + } else if (hcint.b.xacterr) {
  34780. + /* HS error. Retry immediate */
  34781. + st->fsm = FIQ_NP_IN_CSPLIT_RETRY;
  34782. + st->nr_errors++;
  34783. + if (st->nr_errors >= 3) {
  34784. + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
  34785. + } else {
  34786. + handled = 1;
  34787. + restart = 1;
  34788. + }
  34789. + } else if (hcint.b.stall || hcint.b.bblerr) {
  34790. + /* A STALL implies either a LS bus error or a genuine STALL. */
  34791. + st->fsm = FIQ_NP_SPLIT_LS_ABORTED;
  34792. + } else {
  34793. + /* Hardware bug. It's possible in some cases to
  34794. + * get a channel halt with nothing else set when
  34795. + * the response was a NYET. Treat as local 3-strikes retry.
  34796. + */
  34797. + hcint_data_t hcint_test = hcint;
  34798. + hcint_test.b.chhltd = 0;
  34799. + if (!hcint_test.d32) {
  34800. + st->nr_errors++;
  34801. + if (st->nr_errors >= 3) {
  34802. + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
  34803. + } else {
  34804. + handled = 1;
  34805. + }
  34806. + } else {
  34807. + /* Bail out if something unexpected happened */
  34808. + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
  34809. + }
  34810. + }
  34811. + if (st->fsm != FIQ_NP_IN_CSPLIT_RETRY) {
  34812. + fiq_fsm_restart_np_pending(state, num_channels, n);
  34813. + }
  34814. + break;
  34815. +
  34816. + case FIQ_NP_OUT_CSPLIT_RETRY:
  34817. + /* Received a CSPLIT done interrupt.
  34818. + * Expected ACK/NAK/STALL/NYET/XFERCOMP for OUT.*/
  34819. + if (hcint.b.xfercomp) {
  34820. + st->fsm = FIQ_NP_SPLIT_DONE;
  34821. + } else if (hcint.b.nak) {
  34822. + // The HCD will implement the holdoff on frame boundaries.
  34823. + st->fsm = FIQ_NP_SPLIT_DONE;
  34824. + } else if (hcint.b.nyet) {
  34825. + // Hub still processing.
  34826. + st->fsm = FIQ_NP_OUT_CSPLIT_RETRY;
  34827. + handled = 1;
  34828. + st->nr_errors = 0;
  34829. + //restart = 1;
  34830. + } else if (hcint.b.xacterr) {
  34831. + /* HS error. retry immediate */
  34832. + st->fsm = FIQ_NP_OUT_CSPLIT_RETRY;
  34833. + st->nr_errors++;
  34834. + if (st->nr_errors >= 3) {
  34835. + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
  34836. + } else {
  34837. + handled = 1;
  34838. + restart = 1;
  34839. + }
  34840. + } else if (hcint.b.stall) {
  34841. + /* LS bus error or genuine stall */
  34842. + st->fsm = FIQ_NP_SPLIT_LS_ABORTED;
  34843. + } else {
  34844. + /*
  34845. + * Hardware bug. It's possible in some cases to get a
  34846. + * channel halt with nothing else set when the response was a NYET.
  34847. + * Treat as local 3-strikes retry.
  34848. + */
  34849. + hcint_data_t hcint_test = hcint;
  34850. + hcint_test.b.chhltd = 0;
  34851. + if (!hcint_test.d32) {
  34852. + st->nr_errors++;
  34853. + if (st->nr_errors >= 3) {
  34854. + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
  34855. + } else {
  34856. + handled = 1;
  34857. + }
  34858. + } else {
  34859. + // Something unexpected happened. AHBerror or babble perhaps. Let the IRQ deal with it.
  34860. + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
  34861. + }
  34862. + }
  34863. + if (st->fsm != FIQ_NP_OUT_CSPLIT_RETRY) {
  34864. + fiq_fsm_restart_np_pending(state, num_channels, n);
  34865. + }
  34866. + break;
  34867. +
  34868. + /* Periodic split states (except isoc out) */
  34869. + case FIQ_PER_SSPLIT_STARTED:
  34870. + /* Expect an ACK or failure for SSPLIT */
  34871. + if (hcint.b.ack) {
  34872. + /*
  34873. + * SSPLIT transfer complete interrupt - the generation of this interrupt is fraught with bugs.
  34874. + * For a packet queued in microframe n-3 to appear in n-2, if the channel is enabled near the EOF1
  34875. + * point for microframe n-3, the packet will not appear on the bus until microframe n.
  34876. + * Additionally, the generation of the actual interrupt is dodgy. For a packet appearing on the bus
  34877. + * in microframe n, sometimes the interrupt is generated immediately. Sometimes, it appears in n+1
  34878. + * coincident with SOF for n+1.
  34879. + * SOF is also buggy. It can sometimes be raised AFTER the first bus transaction has taken place.
  34880. + * These appear to be caused by timing/clock crossing bugs within the core itself.
  34881. + * State machine workaround.
  34882. + */
  34883. + hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
  34884. + hcchar.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR);
  34885. + fiq_fsm_setup_csplit(state, n);
  34886. + /* Poke the oddfrm bit. If we are equivalent, we received the interrupt at the correct
  34887. + * time. If not, then we're in the next SOF.
  34888. + */
  34889. + if ((hfnum.b.frnum & 0x1) == hcchar.b.oddfrm) {
  34890. + fiq_print(FIQDBG_INT, state, "CSWAIT %01d", n);
  34891. + st->expected_uframe = hfnum.b.frnum;
  34892. + st->fsm = FIQ_PER_CSPLIT_WAIT;
  34893. + } else {
  34894. + fiq_print(FIQDBG_INT, state, "CSPOL %01d", n);
  34895. + /* For isochronous IN endpoints,
  34896. + * we need to hold off if we are expecting a lot of data */
  34897. + if (st->hcchar_copy.b.mps < DATA0_PID_HEURISTIC) {
  34898. + start_next_periodic = 1;
  34899. + }
  34900. + /* Danger will robinson: we are in a broken state. If our first interrupt after
  34901. + * this is a NYET, it will be delayed by 1 uframe and result in an unrecoverable
  34902. + * lag. Unmask the NYET interrupt.
  34903. + */
  34904. + st->expected_uframe = (hfnum.b.frnum + 1) & 0x3FFF;
  34905. + st->fsm = FIQ_PER_CSPLIT_BROKEN_NYET1;
  34906. + restart = 1;
  34907. + }
  34908. + handled = 1;
  34909. + } else if (hcint.b.xacterr) {
  34910. + /* 3-strikes retry is enabled, we have hit our max nr_errors */
  34911. + st->fsm = FIQ_PER_SPLIT_HS_ABORTED;
  34912. + start_next_periodic = 1;
  34913. + } else {
  34914. + st->fsm = FIQ_PER_SPLIT_HS_ABORTED;
  34915. + start_next_periodic = 1;
  34916. + }
  34917. + /* We can now queue the next isochronous OUT transaction, if one is pending. */
  34918. + if(fiq_fsm_tt_next_isoc(state, num_channels, n)) {
  34919. + fiq_print(FIQDBG_INT, state, "NEXTISO ");
  34920. + }
  34921. + break;
  34922. +
  34923. + case FIQ_PER_CSPLIT_NYET1:
  34924. + /* First CSPLIT attempt was a NYET. If we get a subsequent NYET,
  34925. + * we are too late and the TT has dropped its CSPLIT fifo.
  34926. + */
  34927. + hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
  34928. + hcchar.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR);
  34929. + start_next_periodic = 1;
  34930. + if (hcint.b.nak) {
  34931. + st->fsm = FIQ_PER_SPLIT_DONE;
  34932. + } else if (hcint.b.xfercomp) {
  34933. + fiq_increment_dma_buf(state, num_channels, n);
  34934. + st->fsm = FIQ_PER_CSPLIT_POLL;
  34935. + st->nr_errors = 0;
  34936. + if (fiq_fsm_more_csplits(state, n, &last_csplit)) {
  34937. + handled = 1;
  34938. + restart = 1;
  34939. + if (!last_csplit)
  34940. + start_next_periodic = 0;
  34941. + } else {
  34942. + st->fsm = FIQ_PER_SPLIT_DONE;
  34943. + }
  34944. + } else if (hcint.b.nyet) {
  34945. + /* Doh. Data lost. */
  34946. + st->fsm = FIQ_PER_SPLIT_NYET_ABORTED;
  34947. + } else if (hcint.b.xacterr || hcint.b.stall || hcint.b.bblerr) {
  34948. + st->fsm = FIQ_PER_SPLIT_LS_ABORTED;
  34949. + } else {
  34950. + st->fsm = FIQ_PER_SPLIT_HS_ABORTED;
  34951. + }
  34952. + break;
  34953. +
  34954. + case FIQ_PER_CSPLIT_BROKEN_NYET1:
  34955. + /*
  34956. + * we got here because our host channel is in the delayed-interrupt
  34957. + * state and we cannot take a NYET interrupt any later than when it
  34958. + * occurred. Disable then re-enable the channel if this happens to force
  34959. + * CSPLITs to occur at the right time.
  34960. + */
  34961. + hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
  34962. + hcchar.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR);
  34963. + fiq_print(FIQDBG_INT, state, "BROK: %01d ", n);
  34964. + if (hcint.b.nak) {
  34965. + st->fsm = FIQ_PER_SPLIT_DONE;
  34966. + start_next_periodic = 1;
  34967. + } else if (hcint.b.xfercomp) {
  34968. + fiq_increment_dma_buf(state, num_channels, n);
  34969. + if (fiq_fsm_more_csplits(state, n, &last_csplit)) {
  34970. + st->fsm = FIQ_PER_CSPLIT_POLL;
  34971. + handled = 1;
  34972. + restart = 1;
  34973. + start_next_periodic = 1;
  34974. + /* Reload HCTSIZ for the next transfer */
  34975. + fiq_fsm_reload_hctsiz(state, n);
  34976. + if (!last_csplit)
  34977. + start_next_periodic = 0;
  34978. + } else {
  34979. + st->fsm = FIQ_PER_SPLIT_DONE;
  34980. + }
  34981. + } else if (hcint.b.nyet) {
  34982. + st->fsm = FIQ_PER_SPLIT_NYET_ABORTED;
  34983. + start_next_periodic = 1;
  34984. + } else if (hcint.b.xacterr || hcint.b.stall || hcint.b.bblerr) {
  34985. + /* Local 3-strikes retry is handled by the core. This is a ERR response.*/
  34986. + st->fsm = FIQ_PER_SPLIT_LS_ABORTED;
  34987. + } else {
  34988. + st->fsm = FIQ_PER_SPLIT_HS_ABORTED;
  34989. + }
  34990. + break;
  34991. +
  34992. + case FIQ_PER_CSPLIT_POLL:
  34993. + hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
  34994. + hcchar.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR);
  34995. + start_next_periodic = 1;
  34996. + if (hcint.b.nak) {
  34997. + st->fsm = FIQ_PER_SPLIT_DONE;
  34998. + } else if (hcint.b.xfercomp) {
  34999. + fiq_increment_dma_buf(state, num_channels, n);
  35000. + if (fiq_fsm_more_csplits(state, n, &last_csplit)) {
  35001. + handled = 1;
  35002. + restart = 1;
  35003. + /* Reload HCTSIZ for the next transfer */
  35004. + fiq_fsm_reload_hctsiz(state, n);
  35005. + if (!last_csplit)
  35006. + start_next_periodic = 0;
  35007. + } else {
  35008. + st->fsm = FIQ_PER_SPLIT_DONE;
  35009. + }
  35010. + } else if (hcint.b.nyet) {
  35011. + /* Are we a NYET after the first data packet? */
  35012. + if (st->nrpackets == 0) {
  35013. + st->fsm = FIQ_PER_CSPLIT_NYET1;
  35014. + handled = 1;
  35015. + restart = 1;
  35016. + } else {
  35017. + /* We got a NYET when polling CSPLITs. Can happen
  35018. + * if our heuristic fails, or if someone disables us
  35019. + * for any significant length of time.
  35020. + */
  35021. + if (st->nr_errors >= 3) {
  35022. + st->fsm = FIQ_PER_SPLIT_NYET_ABORTED;
  35023. + } else {
  35024. + st->fsm = FIQ_PER_SPLIT_DONE;
  35025. + }
  35026. + }
  35027. + } else if (hcint.b.xacterr || hcint.b.stall || hcint.b.bblerr) {
  35028. + /* For xacterr, Local 3-strikes retry is handled by the core. This is a ERR response.*/
  35029. + st->fsm = FIQ_PER_SPLIT_LS_ABORTED;
  35030. + } else {
  35031. + st->fsm = FIQ_PER_SPLIT_HS_ABORTED;
  35032. + }
  35033. + break;
  35034. +
  35035. + case FIQ_HS_ISOC_TURBO:
  35036. + if (fiq_fsm_update_hs_isoc(state, n, hcint)) {
  35037. + /* more transactions to come */
  35038. + handled = 1;
  35039. + fiq_print(FIQDBG_INT, state, "HSISO M ");
  35040. + /* For strided transfers, put ourselves to sleep */
  35041. + if (st->hs_isoc_info.stride > 1) {
  35042. + st->uframe_sleeps = st->hs_isoc_info.stride - 1;
  35043. + st->fsm = FIQ_HS_ISOC_SLEEPING;
  35044. + } else {
  35045. + restart = 1;
  35046. + }
  35047. + } else {
  35048. + st->fsm = FIQ_HS_ISOC_DONE;
  35049. + fiq_print(FIQDBG_INT, state, "HSISO F ");
  35050. + }
  35051. + break;
  35052. +
  35053. + case FIQ_HS_ISOC_ABORTED:
  35054. + /* This abort is called by the driver rewriting the state mid-transaction
  35055. + * which allows the dequeue mechanism to work more effectively.
  35056. + */
  35057. + break;
  35058. +
  35059. + case FIQ_PER_ISO_OUT_ACTIVE:
  35060. + if (hcint.b.ack) {
  35061. + if(fiq_iso_out_advance(state, num_channels, n)) {
  35062. + /* last OUT transfer */
  35063. + st->fsm = FIQ_PER_ISO_OUT_LAST;
  35064. + /*
  35065. + * Assuming the periodic FIFO in the dwc core
  35066. + * actually does its job properly, we can queue
  35067. + * the next ssplit now and in theory, the wire
  35068. + * transactions will be in-order.
  35069. + */
  35070. + // No it doesn't. It appears to process requests in host channel order.
  35071. + //start_next_periodic = 1;
  35072. + }
  35073. + handled = 1;
  35074. + restart = 1;
  35075. + } else {
  35076. + /*
  35077. + * Isochronous transactions carry on regardless. Log the error
  35078. + * and continue.
  35079. + */
  35080. + //explode += 1;
  35081. + st->nr_errors++;
  35082. + if(fiq_iso_out_advance(state, num_channels, n)) {
  35083. + st->fsm = FIQ_PER_ISO_OUT_LAST;
  35084. + //start_next_periodic = 1;
  35085. + }
  35086. + handled = 1;
  35087. + restart = 1;
  35088. + }
  35089. + break;
  35090. +
  35091. + case FIQ_PER_ISO_OUT_LAST:
  35092. + if (hcint.b.ack) {
  35093. + /* All done here */
  35094. + st->fsm = FIQ_PER_ISO_OUT_DONE;
  35095. + } else {
  35096. + st->fsm = FIQ_PER_ISO_OUT_DONE;
  35097. + st->nr_errors++;
  35098. + }
  35099. + start_next_periodic = 1;
  35100. + break;
  35101. +
  35102. + case FIQ_PER_SPLIT_TIMEOUT:
  35103. + /* SOF kicked us because we overran. */
  35104. + start_next_periodic = 1;
  35105. + break;
  35106. +
  35107. + default:
  35108. + break;
  35109. + }
  35110. +
  35111. + if (handled) {
  35112. + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINT, hcint.d32);
  35113. + } else {
  35114. + /* Copy the regs into the state so the IRQ knows what to do */
  35115. + st->hcint_copy.d32 = hcint.d32;
  35116. + }
  35117. +
  35118. + if (restart) {
  35119. + /* Restart always implies handled. */
  35120. + if (restart == 2) {
  35121. + /* For complete-split INs, the show must go on.
  35122. + * Force a channel restart */
  35123. + fiq_fsm_restart_channel(state, n, 1);
  35124. + } else {
  35125. + fiq_fsm_restart_channel(state, n, 0);
  35126. + }
  35127. + }
  35128. + if (start_next_periodic) {
  35129. + fiq_fsm_start_next_periodic(state, num_channels);
  35130. + }
  35131. + if (st->fsm != FIQ_PASSTHROUGH)
  35132. + fiq_print(FIQDBG_INT, state, "FSMOUT%02d", st->fsm);
  35133. +
  35134. + return handled;
  35135. +}
  35136. +
  35137. +
  35138. +/**
  35139. + * dwc_otg_fiq_fsm() - Flying State Machine (monster) FIQ
  35140. + * @state: pointer to state struct passed from the banked FIQ mode registers.
  35141. + * @num_channels: set according to the DWC hardware configuration
  35142. + * @dma: pointer to DMA bounce buffers for split transaction slots
  35143. + *
  35144. + * The FSM FIQ performs the low-level tasks that normally would be performed by the microcode
  35145. + * inside an EHCI or similar host controller regarding split transactions. The DWC core
  35146. + * interrupts each and every time a split transaction packet is received or sent successfully.
  35147. + * This results in either an interrupt storm when everything is working "properly", or
  35148. + * the interrupt latency of the system in general breaks time-sensitive periodic split
  35149. + * transactions. Pushing the low-level, but relatively easy state machine work into the FIQ
  35150. + * solves these problems.
  35151. + *
  35152. + * Return: void
  35153. + */
  35154. +void notrace dwc_otg_fiq_fsm(struct fiq_state *state, int num_channels)
  35155. +{
  35156. + gintsts_data_t gintsts, gintsts_handled;
  35157. + gintmsk_data_t gintmsk;
  35158. + //hfnum_data_t hfnum;
  35159. + haint_data_t haint, haint_handled;
  35160. + haintmsk_data_t haintmsk;
  35161. + int kick_irq = 0;
  35162. +
  35163. + /* Ensure peripheral reads issued prior to FIQ entry are complete */
  35164. + dsb(sy);
  35165. +
  35166. + gintsts_handled.d32 = 0;
  35167. + haint_handled.d32 = 0;
  35168. +
  35169. + fiq_fsm_spin_lock(&state->lock);
  35170. + gintsts.d32 = FIQ_READ(state->dwc_regs_base + GINTSTS);
  35171. + gintmsk.d32 = FIQ_READ(state->dwc_regs_base + GINTMSK);
  35172. + gintsts.d32 &= gintmsk.d32;
  35173. +
  35174. + if (gintsts.b.sofintr) {
  35175. + /* For FSM mode, SOF is required to keep the state machine advance for
  35176. + * certain stages of the periodic pipeline. It's death to mask this
  35177. + * interrupt in that case.
  35178. + */
  35179. +
  35180. + if (!fiq_fsm_do_sof(state, num_channels)) {
  35181. + /* Kick IRQ once. Queue advancement means that all pending transactions
  35182. + * will get serviced when the IRQ finally executes.
  35183. + */
  35184. + if (state->gintmsk_saved.b.sofintr == 1)
  35185. + kick_irq |= 1;
  35186. + state->gintmsk_saved.b.sofintr = 0;
  35187. + }
  35188. + gintsts_handled.b.sofintr = 1;
  35189. + }
  35190. +
  35191. + if (gintsts.b.hcintr) {
  35192. + int i;
  35193. + haint.d32 = FIQ_READ(state->dwc_regs_base + HAINT);
  35194. + haintmsk.d32 = FIQ_READ(state->dwc_regs_base + HAINTMSK);
  35195. + haint.d32 &= haintmsk.d32;
  35196. + haint_handled.d32 = 0;
  35197. + for (i=0; i<num_channels; i++) {
  35198. + if (haint.b2.chint & (1 << i)) {
  35199. + if(!fiq_fsm_do_hcintr(state, num_channels, i)) {
  35200. + /* HCINT was not handled in FIQ
  35201. + * HAINT is level-sensitive, leading to level-sensitive ginststs.b.hcint bit.
  35202. + * Mask HAINT(i) but keep top-level hcint unmasked.
  35203. + */
  35204. + state->haintmsk_saved.b2.chint &= ~(1 << i);
  35205. + } else {
  35206. + /* do_hcintr cleaned up after itself, but clear haint */
  35207. + haint_handled.b2.chint |= (1 << i);
  35208. + }
  35209. + }
  35210. + }
  35211. +
  35212. + if (haint_handled.b2.chint) {
  35213. + FIQ_WRITE(state->dwc_regs_base + HAINT, haint_handled.d32);
  35214. + }
  35215. +
  35216. + if (haintmsk.d32 != (haintmsk.d32 & state->haintmsk_saved.d32)) {
  35217. + /*
  35218. + * This is necessary to avoid multiple retriggers of the MPHI in the case
  35219. + * where interrupts are held off and HCINTs start to pile up.
  35220. + * Only wake up the IRQ if a new interrupt came in, was not handled and was
  35221. + * masked.
  35222. + */
  35223. + haintmsk.d32 &= state->haintmsk_saved.d32;
  35224. + FIQ_WRITE(state->dwc_regs_base + HAINTMSK, haintmsk.d32);
  35225. + kick_irq |= 1;
  35226. + }
  35227. + /* Top-Level interrupt - always handled because it's level-sensitive */
  35228. + gintsts_handled.b.hcintr = 1;
  35229. + }
  35230. +
  35231. +
  35232. + /* Clear the bits in the saved register that were not handled but were triggered. */
  35233. + state->gintmsk_saved.d32 &= ~(gintsts.d32 & ~gintsts_handled.d32);
  35234. +
  35235. + /* FIQ didn't handle something - mask has changed - write new mask */
  35236. + if (gintmsk.d32 != (gintmsk.d32 & state->gintmsk_saved.d32)) {
  35237. + gintmsk.d32 &= state->gintmsk_saved.d32;
  35238. + gintmsk.b.sofintr = 1;
  35239. + FIQ_WRITE(state->dwc_regs_base + GINTMSK, gintmsk.d32);
  35240. +// fiq_print(FIQDBG_INT, state, "KICKGINT");
  35241. +// fiq_print(FIQDBG_INT, state, "%08x", gintmsk.d32);
  35242. +// fiq_print(FIQDBG_INT, state, "%08x", state->gintmsk_saved.d32);
  35243. + kick_irq |= 1;
  35244. + }
  35245. +
  35246. + if (gintsts_handled.d32) {
  35247. + /* Only applies to edge-sensitive bits in GINTSTS */
  35248. + FIQ_WRITE(state->dwc_regs_base + GINTSTS, gintsts_handled.d32);
  35249. + }
  35250. +
  35251. + /* We got an interrupt, didn't handle it. */
  35252. + if (kick_irq) {
  35253. + state->mphi_int_count++;
  35254. + if (state->mphi_regs.swirq_set) {
  35255. + FIQ_WRITE(state->mphi_regs.swirq_set, 1);
  35256. + } else {
  35257. + FIQ_WRITE(state->mphi_regs.outdda, state->dummy_send_dma);
  35258. + FIQ_WRITE(state->mphi_regs.outddb, (1<<29));
  35259. + }
  35260. +
  35261. + }
  35262. + state->fiq_done++;
  35263. + mb();
  35264. + fiq_fsm_spin_unlock(&state->lock);
  35265. +}
  35266. +
  35267. +
  35268. +/**
  35269. + * dwc_otg_fiq_nop() - FIQ "lite"
  35270. + * @state: pointer to state struct passed from the banked FIQ mode registers.
  35271. + *
  35272. + * The "nop" handler does not intervene on any interrupts other than SOF.
  35273. + * It is limited in scope to deciding at each SOF if the IRQ SOF handler (which deals
  35274. + * with non-periodic/periodic queues) needs to be kicked.
  35275. + *
  35276. + * This is done to hold off the SOF interrupt, which occurs at a rate of 8000 per second.
  35277. + *
  35278. + * Return: void
  35279. + */
  35280. +void notrace dwc_otg_fiq_nop(struct fiq_state *state)
  35281. +{
  35282. + gintsts_data_t gintsts, gintsts_handled;
  35283. + gintmsk_data_t gintmsk;
  35284. + hfnum_data_t hfnum;
  35285. +
  35286. + /* Ensure peripheral reads issued prior to FIQ entry are complete */
  35287. + dsb(sy);
  35288. +
  35289. + fiq_fsm_spin_lock(&state->lock);
  35290. + hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
  35291. + gintsts.d32 = FIQ_READ(state->dwc_regs_base + GINTSTS);
  35292. + gintmsk.d32 = FIQ_READ(state->dwc_regs_base + GINTMSK);
  35293. + gintsts.d32 &= gintmsk.d32;
  35294. + gintsts_handled.d32 = 0;
  35295. +
  35296. + if (gintsts.b.sofintr) {
  35297. + if (!state->kick_np_queues &&
  35298. + dwc_frame_num_gt(state->next_sched_frame, hfnum.b.frnum)) {
  35299. + /* SOF handled, no work to do, just ACK interrupt */
  35300. + gintsts_handled.b.sofintr = 1;
  35301. + } else {
  35302. + /* Kick IRQ */
  35303. + state->gintmsk_saved.b.sofintr = 0;
  35304. + }
  35305. + }
  35306. +
  35307. + /* Reset handled interrupts */
  35308. + if(gintsts_handled.d32) {
  35309. + FIQ_WRITE(state->dwc_regs_base + GINTSTS, gintsts_handled.d32);
  35310. + }
  35311. +
  35312. + /* Clear the bits in the saved register that were not handled but were triggered. */
  35313. + state->gintmsk_saved.d32 &= ~(gintsts.d32 & ~gintsts_handled.d32);
  35314. +
  35315. + /* We got an interrupt, didn't handle it and want to mask it */
  35316. + if (~(state->gintmsk_saved.d32)) {
  35317. + state->mphi_int_count++;
  35318. + gintmsk.d32 &= state->gintmsk_saved.d32;
  35319. + FIQ_WRITE(state->dwc_regs_base + GINTMSK, gintmsk.d32);
  35320. + if (state->mphi_regs.swirq_set) {
  35321. + FIQ_WRITE(state->mphi_regs.swirq_set, 1);
  35322. + } else {
  35323. + /* Force a clear before another dummy send */
  35324. + FIQ_WRITE(state->mphi_regs.intstat, (1<<29));
  35325. + FIQ_WRITE(state->mphi_regs.outdda, state->dummy_send_dma);
  35326. + FIQ_WRITE(state->mphi_regs.outddb, (1<<29));
  35327. + }
  35328. + }
  35329. + state->fiq_done++;
  35330. + mb();
  35331. + fiq_fsm_spin_unlock(&state->lock);
  35332. +}
  35333. --- /dev/null
  35334. +++ b/drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.h
  35335. @@ -0,0 +1,399 @@
  35336. +/*
  35337. + * dwc_otg_fiq_fsm.h - Finite state machine FIQ header definitions
  35338. + *
  35339. + * Copyright (c) 2013 Raspberry Pi Foundation
  35340. + *
  35341. + * Author: Jonathan Bell <[email protected]>
  35342. + * All rights reserved.
  35343. + *
  35344. + * Redistribution and use in source and binary forms, with or without
  35345. + * modification, are permitted provided that the following conditions are met:
  35346. + * * Redistributions of source code must retain the above copyright
  35347. + * notice, this list of conditions and the following disclaimer.
  35348. + * * Redistributions in binary form must reproduce the above copyright
  35349. + * notice, this list of conditions and the following disclaimer in the
  35350. + * documentation and/or other materials provided with the distribution.
  35351. + * * Neither the name of Raspberry Pi nor the
  35352. + * names of its contributors may be used to endorse or promote products
  35353. + * derived from this software without specific prior written permission.
  35354. + *
  35355. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  35356. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  35357. + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  35358. + * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  35359. + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  35360. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  35361. + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  35362. + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  35363. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35364. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35365. + *
  35366. + * This FIQ implements functionality that performs split transactions on
  35367. + * the dwc_otg hardware without any outside intervention. A split transaction
  35368. + * is "queued" by nominating a specific host channel to perform the entirety
  35369. + * of a split transaction. This FIQ will then perform the microframe-precise
  35370. + * scheduling required in each phase of the transaction until completion.
  35371. + *
  35372. + * The FIQ functionality has been surgically implanted into the Synopsys
  35373. + * vendor-provided driver.
  35374. + *
  35375. + */
  35376. +
  35377. +#ifndef DWC_OTG_FIQ_FSM_H_
  35378. +#define DWC_OTG_FIQ_FSM_H_
  35379. +
  35380. +#include "dwc_otg_regs.h"
  35381. +#include "dwc_otg_cil.h"
  35382. +#include "dwc_otg_hcd.h"
  35383. +#include <linux/kernel.h>
  35384. +#include <linux/irqflags.h>
  35385. +#include <linux/string.h>
  35386. +#include <asm/barrier.h>
  35387. +
  35388. +#if 0
  35389. +#define FLAME_ON(x) \
  35390. +do { \
  35391. + int gpioreg; \
  35392. + \
  35393. + gpioreg = readl(__io_address(0x20200000+0x8)); \
  35394. + gpioreg &= ~(7 << (x-20)*3); \
  35395. + gpioreg |= 0x1 << (x-20)*3; \
  35396. + writel(gpioreg, __io_address(0x20200000+0x8)); \
  35397. + \
  35398. + writel(1<<x, __io_address(0x20200000+(0x1C))); \
  35399. +} while (0)
  35400. +
  35401. +#define FLAME_OFF(x) \
  35402. +do { \
  35403. + writel(1<<x, __io_address(0x20200000+(0x28))); \
  35404. +} while (0)
  35405. +#else
  35406. +#define FLAME_ON(x) do { } while (0)
  35407. +#define FLAME_OFF(X) do { } while (0)
  35408. +#endif
  35409. +
  35410. +/* This is a quick-and-dirty arch-specific register read/write. We know that
  35411. + * writes to a peripheral on BCM2835 will always arrive in-order, also that
  35412. + * reads and writes are executed in-order therefore the need for memory barriers
  35413. + * is obviated if we're only talking to USB.
  35414. + */
  35415. +#define FIQ_WRITE(_addr_,_data_) (*(volatile unsigned int *) (_addr_) = (_data_))
  35416. +#define FIQ_READ(_addr_) (*(volatile unsigned int *) (_addr_))
  35417. +
  35418. +/* FIQ-ified register definitions. Offsets are from dwc_regs_base. */
  35419. +#define GINTSTS 0x014
  35420. +#define GINTMSK 0x018
  35421. +/* Debug register. Poll the top of the received packets FIFO. */
  35422. +#define GRXSTSR 0x01C
  35423. +#define HFNUM 0x408
  35424. +#define HAINT 0x414
  35425. +#define HAINTMSK 0x418
  35426. +#define HPRT0 0x440
  35427. +
  35428. +/* HC_regs start from an offset of 0x500 */
  35429. +#define HC_START 0x500
  35430. +#define HC_OFFSET 0x020
  35431. +
  35432. +#define HC_DMA 0x14
  35433. +
  35434. +#define HCCHAR 0x00
  35435. +#define HCSPLT 0x04
  35436. +#define HCINT 0x08
  35437. +#define HCINTMSK 0x0C
  35438. +#define HCTSIZ 0x10
  35439. +
  35440. +#define ISOC_XACTPOS_ALL 0b11
  35441. +#define ISOC_XACTPOS_BEGIN 0b10
  35442. +#define ISOC_XACTPOS_MID 0b00
  35443. +#define ISOC_XACTPOS_END 0b01
  35444. +
  35445. +#define DWC_PID_DATA2 0b01
  35446. +#define DWC_PID_MDATA 0b11
  35447. +#define DWC_PID_DATA1 0b10
  35448. +#define DWC_PID_DATA0 0b00
  35449. +
  35450. +typedef struct {
  35451. + volatile void* base;
  35452. + volatile void* ctrl;
  35453. + volatile void* outdda;
  35454. + volatile void* outddb;
  35455. + volatile void* intstat;
  35456. + volatile void* swirq_set;
  35457. + volatile void* swirq_clr;
  35458. +} mphi_regs_t;
  35459. +
  35460. +enum fiq_debug_level {
  35461. + FIQDBG_SCHED = (1 << 0),
  35462. + FIQDBG_INT = (1 << 1),
  35463. + FIQDBG_ERR = (1 << 2),
  35464. + FIQDBG_PORTHUB = (1 << 3),
  35465. +};
  35466. +
  35467. +#ifdef CONFIG_ARM64
  35468. +
  35469. +typedef spinlock_t fiq_lock_t;
  35470. +
  35471. +#else
  35472. +
  35473. +typedef struct {
  35474. + union {
  35475. + uint32_t slock;
  35476. + struct _tickets {
  35477. + uint16_t owner;
  35478. + uint16_t next;
  35479. + } tickets;
  35480. + };
  35481. +} fiq_lock_t;
  35482. +
  35483. +#endif
  35484. +
  35485. +struct fiq_state;
  35486. +
  35487. +extern void _fiq_print (enum fiq_debug_level dbg_lvl, volatile struct fiq_state *state, char *fmt, ...);
  35488. +#if 0
  35489. +#define fiq_print _fiq_print
  35490. +#else
  35491. +#define fiq_print(x, y, ...)
  35492. +#endif
  35493. +
  35494. +extern bool fiq_enable, fiq_fsm_enable;
  35495. +extern ushort nak_holdoff;
  35496. +
  35497. +/**
  35498. + * enum fiq_fsm_state - The FIQ FSM states.
  35499. + *
  35500. + * This is the "core" of the FIQ FSM. Broadly, the FSM states follow the
  35501. + * USB2.0 specification for host responses to various transaction states.
  35502. + * There are modifications to this host state machine because of a variety of
  35503. + * quirks and limitations in the dwc_otg hardware.
  35504. + *
  35505. + * The fsm state is also used to communicate back to the driver on completion of
  35506. + * a split transaction. The end states are used in conjunction with the interrupts
  35507. + * raised by the final transaction.
  35508. + */
  35509. +enum fiq_fsm_state {
  35510. + /* FIQ isn't enabled for this host channel */
  35511. + FIQ_PASSTHROUGH = 0,
  35512. + /* For the first interrupt received for this channel,
  35513. + * the FIQ has to ack any interrupts indicating success. */
  35514. + FIQ_PASSTHROUGH_ERRORSTATE = 31,
  35515. + /* Nonperiodic state groups */
  35516. + FIQ_NP_SSPLIT_STARTED = 1,
  35517. + FIQ_NP_SSPLIT_RETRY = 2,
  35518. + /* TT contention - working around hub bugs */
  35519. + FIQ_NP_SSPLIT_PENDING = 33,
  35520. + FIQ_NP_OUT_CSPLIT_RETRY = 3,
  35521. + FIQ_NP_IN_CSPLIT_RETRY = 4,
  35522. + FIQ_NP_SPLIT_DONE = 5,
  35523. + FIQ_NP_SPLIT_LS_ABORTED = 6,
  35524. + /* This differentiates a HS transaction error from a LS one
  35525. + * (handling the hub state is different) */
  35526. + FIQ_NP_SPLIT_HS_ABORTED = 7,
  35527. +
  35528. + /* Periodic state groups */
  35529. + /* Periodic transactions are either started directly by the IRQ handler
  35530. + * or deferred if the TT is already in use.
  35531. + */
  35532. + FIQ_PER_SSPLIT_QUEUED = 8,
  35533. + FIQ_PER_SSPLIT_STARTED = 9,
  35534. + FIQ_PER_SSPLIT_LAST = 10,
  35535. +
  35536. +
  35537. + FIQ_PER_ISO_OUT_PENDING = 11,
  35538. + FIQ_PER_ISO_OUT_ACTIVE = 12,
  35539. + FIQ_PER_ISO_OUT_LAST = 13,
  35540. + FIQ_PER_ISO_OUT_DONE = 27,
  35541. +
  35542. + FIQ_PER_CSPLIT_WAIT = 14,
  35543. + FIQ_PER_CSPLIT_NYET1 = 15,
  35544. + FIQ_PER_CSPLIT_BROKEN_NYET1 = 28,
  35545. + FIQ_PER_CSPLIT_NYET_FAFF = 29,
  35546. + /* For multiple CSPLITs (large isoc IN, or delayed interrupt) */
  35547. + FIQ_PER_CSPLIT_POLL = 16,
  35548. + /* The last CSPLIT for a transaction has been issued, differentiates
  35549. + * for the state machine to queue the next packet.
  35550. + */
  35551. + FIQ_PER_CSPLIT_LAST = 17,
  35552. +
  35553. + FIQ_PER_SPLIT_DONE = 18,
  35554. + FIQ_PER_SPLIT_LS_ABORTED = 19,
  35555. + FIQ_PER_SPLIT_HS_ABORTED = 20,
  35556. + FIQ_PER_SPLIT_NYET_ABORTED = 21,
  35557. + /* Frame rollover has occurred without the transaction finishing. */
  35558. + FIQ_PER_SPLIT_TIMEOUT = 22,
  35559. +
  35560. + /* FIQ-accelerated HS Isochronous state groups */
  35561. + FIQ_HS_ISOC_TURBO = 23,
  35562. + /* For interval > 1, SOF wakes up the isochronous FSM */
  35563. + FIQ_HS_ISOC_SLEEPING = 24,
  35564. + FIQ_HS_ISOC_DONE = 25,
  35565. + FIQ_HS_ISOC_ABORTED = 26,
  35566. + FIQ_DEQUEUE_ISSUED = 30,
  35567. + FIQ_TEST = 32,
  35568. +};
  35569. +
  35570. +struct fiq_stack {
  35571. + int magic1;
  35572. + uint8_t stack[2048];
  35573. + int magic2;
  35574. +};
  35575. +
  35576. +
  35577. +/**
  35578. + * struct fiq_dma_info - DMA bounce buffer utilisation information (per-channel)
  35579. + * @index: Number of slots reported used for IN transactions / number of slots
  35580. + * transmitted for an OUT transaction
  35581. + * @slot_len[6]: Number of actual transfer bytes in each slot (255 if unused)
  35582. + *
  35583. + * Split transaction transfers can have variable length depending on other bus
  35584. + * traffic. The OTG core DMA engine requires 4-byte aligned addresses therefore
  35585. + * each transaction needs a guaranteed aligned address. A maximum of 6 split transfers
  35586. + * can happen per-frame.
  35587. + */
  35588. +struct fiq_dma_info {
  35589. + u8 index;
  35590. + u8 slot_len[6];
  35591. +};
  35592. +
  35593. +struct fiq_split_dma_slot {
  35594. + u8 buf[188];
  35595. +} __attribute__((packed));
  35596. +
  35597. +struct fiq_dma_channel {
  35598. + struct fiq_split_dma_slot index[6];
  35599. +} __attribute__((packed));
  35600. +
  35601. +struct fiq_dma_blob {
  35602. + struct fiq_dma_channel channel[0];
  35603. +} __attribute__((packed));
  35604. +
  35605. +/**
  35606. + * struct fiq_hs_isoc_info - USB2.0 isochronous data
  35607. + * @iso_frame: Pointer to the array of OTG URB iso_frame_descs.
  35608. + * @nrframes: Total length of iso_frame_desc array
  35609. + * @index: Current index (FIQ-maintained)
  35610. + * @stride: Interval in uframes between HS isoc transactions
  35611. + */
  35612. +struct fiq_hs_isoc_info {
  35613. + struct dwc_otg_hcd_iso_packet_desc *iso_desc;
  35614. + unsigned int nrframes;
  35615. + unsigned int index;
  35616. + unsigned int stride;
  35617. +};
  35618. +
  35619. +/**
  35620. + * struct fiq_channel_state - FIQ state machine storage
  35621. + * @fsm: Current state of the channel as understood by the FIQ
  35622. + * @nr_errors: Number of transaction errors on this split-transaction
  35623. + * @hub_addr: SSPLIT/CSPLIT destination hub
  35624. + * @port_addr: SSPLIT/CSPLIT destination port - always 1 if single TT hub
  35625. + * @nrpackets: For isoc OUT, the number of split-OUT packets to transmit. For
  35626. + * split-IN, number of CSPLIT data packets that were received.
  35627. + * @hcchar_copy:
  35628. + * @hcsplt_copy:
  35629. + * @hcintmsk_copy:
  35630. + * @hctsiz_copy: Copies of the host channel registers.
  35631. + * For use as scratch, or for returning state.
  35632. + *
  35633. + * The fiq_channel_state is state storage between interrupts for a host channel. The
  35634. + * FSM state is stored here. Members of this structure must only be set up by the
  35635. + * driver prior to enabling the FIQ for this host channel, and not touched until the FIQ
  35636. + * has updated the state to either a COMPLETE state group or ABORT state group.
  35637. + */
  35638. +
  35639. +struct fiq_channel_state {
  35640. + enum fiq_fsm_state fsm;
  35641. + unsigned int nr_errors;
  35642. + unsigned int hub_addr;
  35643. + unsigned int port_addr;
  35644. + /* Hardware bug workaround: sometimes channel halt interrupts are
  35645. + * delayed until the next SOF. Keep track of when we expected to get interrupted. */
  35646. + unsigned int expected_uframe;
  35647. + /* number of uframes remaining (for interval > 1 HS isoc transfers) before next transfer */
  35648. + unsigned int uframe_sleeps;
  35649. + /* in/out for communicating number of dma buffers used, or number of ISOC to do */
  35650. + unsigned int nrpackets;
  35651. + struct fiq_dma_info dma_info;
  35652. + struct fiq_hs_isoc_info hs_isoc_info;
  35653. + /* Copies of HC registers - in/out communication from/to IRQ handler
  35654. + * and for ease of channel setup. A bit of mungeing is performed - for
  35655. + * example the hctsiz.b.maxp is _always_ the max packet size of the endpoint.
  35656. + */
  35657. + hcchar_data_t hcchar_copy;
  35658. + hcsplt_data_t hcsplt_copy;
  35659. + hcint_data_t hcint_copy;
  35660. + hcintmsk_data_t hcintmsk_copy;
  35661. + hctsiz_data_t hctsiz_copy;
  35662. + hcdma_data_t hcdma_copy;
  35663. +};
  35664. +
  35665. +/**
  35666. + * struct fiq_state - top-level FIQ state machine storage
  35667. + * @mphi_regs: virtual address of the MPHI peripheral register file
  35668. + * @dwc_regs_base: virtual address of the base of the DWC core register file
  35669. + * @dma_base: physical address for the base of the DMA bounce buffers
  35670. + * @dummy_send: Scratch area for sending a fake message to the MPHI peripheral
  35671. + * @gintmsk_saved: Top-level mask of interrupts that the FIQ has not handled.
  35672. + * Used for determining which interrupts fired to set off the IRQ handler.
  35673. + * @haintmsk_saved: Mask of interrupts from host channels that the FIQ did not handle internally.
  35674. + * @np_count: Non-periodic transactions in the active queue
  35675. + * @np_sent: Count of non-periodic transactions that have completed
  35676. + * @next_sched_frame: For periodic transactions handled by the driver's SOF-driven queuing mechanism,
  35677. + * this is the next frame on which a SOF interrupt is required. Used to hold off
  35678. + * passing SOF through to the driver until necessary.
  35679. + * @channel[n]: Per-channel FIQ state. Allocated during init depending on the number of host
  35680. + * channels configured into the core logic.
  35681. + *
  35682. + * This is passed as the first argument to the dwc_otg_fiq_fsm top-level FIQ handler from the asm stub.
  35683. + * It contains top-level state information.
  35684. + */
  35685. +struct fiq_state {
  35686. + fiq_lock_t lock;
  35687. + mphi_regs_t mphi_regs;
  35688. + void *dwc_regs_base;
  35689. + dma_addr_t dma_base;
  35690. + struct fiq_dma_blob *fiq_dmab;
  35691. + void *dummy_send;
  35692. + dma_addr_t dummy_send_dma;
  35693. + gintmsk_data_t gintmsk_saved;
  35694. + haintmsk_data_t haintmsk_saved;
  35695. + int mphi_int_count;
  35696. + unsigned int fiq_done;
  35697. + unsigned int kick_np_queues;
  35698. + unsigned int next_sched_frame;
  35699. +#ifdef FIQ_DEBUG
  35700. + char * buffer;
  35701. + unsigned int bufsiz;
  35702. +#endif
  35703. + struct fiq_channel_state channel[0];
  35704. +};
  35705. +
  35706. +#ifdef CONFIG_ARM64
  35707. +
  35708. +#ifdef local_fiq_enable
  35709. +#undef local_fiq_enable
  35710. +#endif
  35711. +
  35712. +#ifdef local_fiq_disable
  35713. +#undef local_fiq_disable
  35714. +#endif
  35715. +
  35716. +extern void local_fiq_enable(void);
  35717. +
  35718. +extern void local_fiq_disable(void);
  35719. +
  35720. +#endif
  35721. +
  35722. +extern void fiq_fsm_spin_lock(fiq_lock_t *lock);
  35723. +
  35724. +extern void fiq_fsm_spin_unlock(fiq_lock_t *lock);
  35725. +
  35726. +extern int fiq_fsm_too_late(struct fiq_state *st, int n);
  35727. +
  35728. +extern int fiq_fsm_tt_in_use(struct fiq_state *st, int num_channels, int n);
  35729. +
  35730. +extern void dwc_otg_fiq_fsm(struct fiq_state *state, int num_channels);
  35731. +
  35732. +extern void dwc_otg_fiq_nop(struct fiq_state *state);
  35733. +
  35734. +#endif /* DWC_OTG_FIQ_FSM_H_ */
  35735. --- /dev/null
  35736. +++ b/drivers/usb/host/dwc_otg/dwc_otg_fiq_stub.S
  35737. @@ -0,0 +1,80 @@
  35738. +/*
  35739. + * dwc_otg_fiq_fsm.S - assembly stub for the FSM FIQ
  35740. + *
  35741. + * Copyright (c) 2013 Raspberry Pi Foundation
  35742. + *
  35743. + * Author: Jonathan Bell <[email protected]>
  35744. + * All rights reserved.
  35745. + *
  35746. + * Redistribution and use in source and binary forms, with or without
  35747. + * modification, are permitted provided that the following conditions are met:
  35748. + * * Redistributions of source code must retain the above copyright
  35749. + * notice, this list of conditions and the following disclaimer.
  35750. + * * Redistributions in binary form must reproduce the above copyright
  35751. + * notice, this list of conditions and the following disclaimer in the
  35752. + * documentation and/or other materials provided with the distribution.
  35753. + * * Neither the name of Raspberry Pi nor the
  35754. + * names of its contributors may be used to endorse or promote products
  35755. + * derived from this software without specific prior written permission.
  35756. + *
  35757. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  35758. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  35759. + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  35760. + * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  35761. + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  35762. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  35763. + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  35764. + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  35765. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35766. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35767. + */
  35768. +
  35769. +
  35770. +#include <asm/assembler.h>
  35771. +#include <linux/linkage.h>
  35772. +
  35773. +
  35774. +.text
  35775. +
  35776. +.global _dwc_otg_fiq_stub_end;
  35777. +
  35778. +/**
  35779. + * _dwc_otg_fiq_stub() - entry copied to the FIQ vector page to allow
  35780. + * a C-style function call with arguments from the FIQ banked registers.
  35781. + * r0 = &hcd->fiq_state
  35782. + * r1 = &hcd->num_channels
  35783. + * r2 = &hcd->dma_buffers
  35784. + * Tramples: r0, r1, r2, r4, fp, ip
  35785. + */
  35786. +
  35787. +ENTRY(_dwc_otg_fiq_stub)
  35788. + /* Stash unbanked regs - SP will have been set up for us */
  35789. + mov ip, sp;
  35790. + stmdb sp!, {r0-r12, lr};
  35791. +#ifdef FIQ_DEBUG
  35792. + // Cycle profiling - read cycle counter at start
  35793. + mrc p15, 0, r5, c15, c12, 1;
  35794. +#endif
  35795. + /* r11 = fp, don't trample it */
  35796. + mov r4, fp;
  35797. + /* set EABI frame size */
  35798. + sub fp, ip, #512;
  35799. +
  35800. + /* for fiq NOP mode - just need state */
  35801. + mov r0, r8;
  35802. + /* r9 = num_channels */
  35803. + mov r1, r9;
  35804. + /* r10 = struct *dma_bufs */
  35805. +// mov r2, r10;
  35806. +
  35807. + /* r4 = &fiq_c_function */
  35808. + blx r4;
  35809. +#ifdef FIQ_DEBUG
  35810. + mrc p15, 0, r4, c15, c12, 1;
  35811. + subs r5, r5, r4;
  35812. + // r5 is now the cycle count time for executing the FIQ. Store it somewhere?
  35813. +#endif
  35814. + ldmia sp!, {r0-r12, lr};
  35815. + subs pc, lr, #4;
  35816. +_dwc_otg_fiq_stub_end:
  35817. +END(_dwc_otg_fiq_stub)
  35818. --- /dev/null
  35819. +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd.c
  35820. @@ -0,0 +1,4363 @@
  35821. +
  35822. +/* ==========================================================================
  35823. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd.c $
  35824. + * $Revision: #104 $
  35825. + * $Date: 2011/10/24 $
  35826. + * $Change: 1871159 $
  35827. + *
  35828. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  35829. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  35830. + * otherwise expressly agreed to in writing between Synopsys and you.
  35831. + *
  35832. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  35833. + * any End User Software License Agreement or Agreement for Licensed Product
  35834. + * with Synopsys or any supplement thereto. You are permitted to use and
  35835. + * redistribute this Software in source and binary forms, with or without
  35836. + * modification, provided that redistributions of source code must retain this
  35837. + * notice. You may not view, use, disclose, copy or distribute this file or
  35838. + * any information contained herein except pursuant to this license grant from
  35839. + * Synopsys. If you do not agree with this notice, including the disclaimer
  35840. + * below, then you are not authorized to use the Software.
  35841. + *
  35842. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  35843. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  35844. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35845. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  35846. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  35847. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  35848. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  35849. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  35850. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  35851. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  35852. + * DAMAGE.
  35853. + * ========================================================================== */
  35854. +#ifndef DWC_DEVICE_ONLY
  35855. +
  35856. +/** @file
  35857. + * This file implements HCD Core. All code in this file is portable and doesn't
  35858. + * use any OS specific functions.
  35859. + * Interface provided by HCD Core is defined in <code><hcd_if.h></code>
  35860. + * header file.
  35861. + */
  35862. +
  35863. +#include <linux/usb.h>
  35864. +#include <linux/usb/hcd.h>
  35865. +
  35866. +#include "dwc_otg_hcd.h"
  35867. +#include "dwc_otg_regs.h"
  35868. +#include "dwc_otg_fiq_fsm.h"
  35869. +
  35870. +extern bool microframe_schedule;
  35871. +extern uint16_t fiq_fsm_mask, nak_holdoff;
  35872. +
  35873. +//#define DEBUG_HOST_CHANNELS
  35874. +#ifdef DEBUG_HOST_CHANNELS
  35875. +static int last_sel_trans_num_per_scheduled = 0;
  35876. +static int last_sel_trans_num_nonper_scheduled = 0;
  35877. +static int last_sel_trans_num_avail_hc_at_start = 0;
  35878. +static int last_sel_trans_num_avail_hc_at_end = 0;
  35879. +#endif /* DEBUG_HOST_CHANNELS */
  35880. +
  35881. +
  35882. +dwc_otg_hcd_t *dwc_otg_hcd_alloc_hcd(void)
  35883. +{
  35884. + return DWC_ALLOC(sizeof(dwc_otg_hcd_t));
  35885. +}
  35886. +
  35887. +/**
  35888. + * Connection timeout function. An OTG host is required to display a
  35889. + * message if the device does not connect within 10 seconds.
  35890. + */
  35891. +void dwc_otg_hcd_connect_timeout(void *ptr)
  35892. +{
  35893. + DWC_DEBUGPL(DBG_HCDV, "%s(%p)\n", __func__, ptr);
  35894. + DWC_PRINTF("Connect Timeout\n");
  35895. + __DWC_ERROR("Device Not Connected/Responding\n");
  35896. +}
  35897. +
  35898. +#if defined(DEBUG)
  35899. +static void dump_channel_info(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  35900. +{
  35901. + if (qh->channel != NULL) {
  35902. + dwc_hc_t *hc = qh->channel;
  35903. + dwc_list_link_t *item;
  35904. + dwc_otg_qh_t *qh_item;
  35905. + int num_channels = hcd->core_if->core_params->host_channels;
  35906. + int i;
  35907. +
  35908. + dwc_otg_hc_regs_t *hc_regs;
  35909. + hcchar_data_t hcchar;
  35910. + hcsplt_data_t hcsplt;
  35911. + hctsiz_data_t hctsiz;
  35912. + uint32_t hcdma;
  35913. +
  35914. + hc_regs = hcd->core_if->host_if->hc_regs[hc->hc_num];
  35915. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  35916. + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
  35917. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  35918. + hcdma = DWC_READ_REG32(&hc_regs->hcdma);
  35919. +
  35920. + DWC_PRINTF(" Assigned to channel %p:\n", hc);
  35921. + DWC_PRINTF(" hcchar 0x%08x, hcsplt 0x%08x\n", hcchar.d32,
  35922. + hcsplt.d32);
  35923. + DWC_PRINTF(" hctsiz 0x%08x, hcdma 0x%08x\n", hctsiz.d32,
  35924. + hcdma);
  35925. + DWC_PRINTF(" dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
  35926. + hc->dev_addr, hc->ep_num, hc->ep_is_in);
  35927. + DWC_PRINTF(" ep_type: %d\n", hc->ep_type);
  35928. + DWC_PRINTF(" max_packet: %d\n", hc->max_packet);
  35929. + DWC_PRINTF(" data_pid_start: %d\n", hc->data_pid_start);
  35930. + DWC_PRINTF(" xfer_started: %d\n", hc->xfer_started);
  35931. + DWC_PRINTF(" halt_status: %d\n", hc->halt_status);
  35932. + DWC_PRINTF(" xfer_buff: %p\n", hc->xfer_buff);
  35933. + DWC_PRINTF(" xfer_len: %d\n", hc->xfer_len);
  35934. + DWC_PRINTF(" qh: %p\n", hc->qh);
  35935. + DWC_PRINTF(" NP inactive sched:\n");
  35936. + DWC_LIST_FOREACH(item, &hcd->non_periodic_sched_inactive) {
  35937. + qh_item =
  35938. + DWC_LIST_ENTRY(item, dwc_otg_qh_t, qh_list_entry);
  35939. + DWC_PRINTF(" %p\n", qh_item);
  35940. + }
  35941. + DWC_PRINTF(" NP active sched:\n");
  35942. + DWC_LIST_FOREACH(item, &hcd->non_periodic_sched_active) {
  35943. + qh_item =
  35944. + DWC_LIST_ENTRY(item, dwc_otg_qh_t, qh_list_entry);
  35945. + DWC_PRINTF(" %p\n", qh_item);
  35946. + }
  35947. + DWC_PRINTF(" Channels: \n");
  35948. + for (i = 0; i < num_channels; i++) {
  35949. + dwc_hc_t *hc = hcd->hc_ptr_array[i];
  35950. + DWC_PRINTF(" %2d: %p\n", i, hc);
  35951. + }
  35952. + }
  35953. +}
  35954. +#else
  35955. +#define dump_channel_info(hcd, qh)
  35956. +#endif /* DEBUG */
  35957. +
  35958. +/**
  35959. + * Work queue function for starting the HCD when A-Cable is connected.
  35960. + * The hcd_start() must be called in a process context.
  35961. + */
  35962. +static void hcd_start_func(void *_vp)
  35963. +{
  35964. + dwc_otg_hcd_t *hcd = (dwc_otg_hcd_t *) _vp;
  35965. +
  35966. + DWC_DEBUGPL(DBG_HCDV, "%s() %p\n", __func__, hcd);
  35967. + if (hcd) {
  35968. + hcd->fops->start(hcd);
  35969. + }
  35970. +}
  35971. +
  35972. +static void del_xfer_timers(dwc_otg_hcd_t * hcd)
  35973. +{
  35974. +#ifdef DEBUG
  35975. + int i;
  35976. + int num_channels = hcd->core_if->core_params->host_channels;
  35977. + for (i = 0; i < num_channels; i++) {
  35978. + DWC_TIMER_CANCEL(hcd->core_if->hc_xfer_timer[i]);
  35979. + }
  35980. +#endif
  35981. +}
  35982. +
  35983. +static void del_timers(dwc_otg_hcd_t * hcd)
  35984. +{
  35985. + del_xfer_timers(hcd);
  35986. + DWC_TIMER_CANCEL(hcd->conn_timer);
  35987. +}
  35988. +
  35989. +/**
  35990. + * Processes all the URBs in a single list of QHs. Completes them with
  35991. + * -ESHUTDOWN and frees the QTD.
  35992. + */
  35993. +static void kill_urbs_in_qh_list(dwc_otg_hcd_t * hcd, dwc_list_link_t * qh_list)
  35994. +{
  35995. + dwc_list_link_t *qh_item, *qh_tmp;
  35996. + dwc_otg_qh_t *qh;
  35997. + dwc_otg_qtd_t *qtd, *qtd_tmp;
  35998. + int quiesced = 0;
  35999. +
  36000. + DWC_LIST_FOREACH_SAFE(qh_item, qh_tmp, qh_list) {
  36001. + qh = DWC_LIST_ENTRY(qh_item, dwc_otg_qh_t, qh_list_entry);
  36002. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp,
  36003. + &qh->qtd_list, qtd_list_entry) {
  36004. + qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
  36005. + if (qtd->urb != NULL) {
  36006. + hcd->fops->complete(hcd, qtd->urb->priv,
  36007. + qtd->urb, -DWC_E_SHUTDOWN);
  36008. + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
  36009. + }
  36010. +
  36011. + }
  36012. + if(qh->channel) {
  36013. + int n = qh->channel->hc_num;
  36014. + /* Using hcchar.chen == 1 is not a reliable test.
  36015. + * It is possible that the channel has already halted
  36016. + * but not yet been through the IRQ handler.
  36017. + */
  36018. + if (fiq_fsm_enable && (hcd->fiq_state->channel[qh->channel->hc_num].fsm != FIQ_PASSTHROUGH)) {
  36019. + qh->channel->halt_status = DWC_OTG_HC_XFER_URB_DEQUEUE;
  36020. + qh->channel->halt_pending = 1;
  36021. + if (hcd->fiq_state->channel[n].fsm == FIQ_HS_ISOC_TURBO ||
  36022. + hcd->fiq_state->channel[n].fsm == FIQ_HS_ISOC_SLEEPING)
  36023. + hcd->fiq_state->channel[n].fsm = FIQ_HS_ISOC_ABORTED;
  36024. + /* We're called from disconnect callback or in the middle of freeing the HCD here,
  36025. + * so FIQ is disabled, top-level interrupts masked and we're holding the spinlock.
  36026. + * No further URBs will be submitted, but wait 1 microframe for any previously
  36027. + * submitted periodic DMA to finish.
  36028. + */
  36029. + if (!quiesced) {
  36030. + udelay(125);
  36031. + quiesced = 1;
  36032. + }
  36033. + } else {
  36034. + dwc_otg_hc_halt(hcd->core_if, qh->channel,
  36035. + DWC_OTG_HC_XFER_URB_DEQUEUE);
  36036. + }
  36037. + qh->channel = NULL;
  36038. + }
  36039. + dwc_otg_hcd_qh_remove(hcd, qh);
  36040. + }
  36041. +}
  36042. +
  36043. +/**
  36044. + * Responds with an error status of ESHUTDOWN to all URBs in the non-periodic
  36045. + * and periodic schedules. The QTD associated with each URB is removed from
  36046. + * the schedule and freed. This function may be called when a disconnect is
  36047. + * detected or when the HCD is being stopped.
  36048. + */
  36049. +static void kill_all_urbs(dwc_otg_hcd_t * hcd)
  36050. +{
  36051. + kill_urbs_in_qh_list(hcd, &hcd->non_periodic_sched_inactive);
  36052. + kill_urbs_in_qh_list(hcd, &hcd->non_periodic_sched_active);
  36053. + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_inactive);
  36054. + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_ready);
  36055. + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_assigned);
  36056. + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_queued);
  36057. +}
  36058. +
  36059. +/**
  36060. + * Start the connection timer. An OTG host is required to display a
  36061. + * message if the device does not connect within 10 seconds. The
  36062. + * timer is deleted if a port connect interrupt occurs before the
  36063. + * timer expires.
  36064. + */
  36065. +static void dwc_otg_hcd_start_connect_timer(dwc_otg_hcd_t * hcd)
  36066. +{
  36067. + DWC_TIMER_SCHEDULE(hcd->conn_timer, 10000 /* 10 secs */ );
  36068. +}
  36069. +
  36070. +/**
  36071. + * HCD Callback function for disconnect of the HCD.
  36072. + *
  36073. + * @param p void pointer to the <code>struct usb_hcd</code>
  36074. + */
  36075. +static int32_t dwc_otg_hcd_session_start_cb(void *p)
  36076. +{
  36077. + dwc_otg_hcd_t *dwc_otg_hcd;
  36078. + DWC_DEBUGPL(DBG_HCDV, "%s(%p)\n", __func__, p);
  36079. + dwc_otg_hcd = p;
  36080. + dwc_otg_hcd_start_connect_timer(dwc_otg_hcd);
  36081. + return 1;
  36082. +}
  36083. +
  36084. +/**
  36085. + * HCD Callback function for starting the HCD when A-Cable is
  36086. + * connected.
  36087. + *
  36088. + * @param p void pointer to the <code>struct usb_hcd</code>
  36089. + */
  36090. +static int32_t dwc_otg_hcd_start_cb(void *p)
  36091. +{
  36092. + dwc_otg_hcd_t *dwc_otg_hcd = p;
  36093. + dwc_otg_core_if_t *core_if;
  36094. + hprt0_data_t hprt0;
  36095. +
  36096. + core_if = dwc_otg_hcd->core_if;
  36097. +
  36098. + if (core_if->op_state == B_HOST) {
  36099. + /*
  36100. + * Reset the port. During a HNP mode switch the reset
  36101. + * needs to occur within 1ms and have a duration of at
  36102. + * least 50ms.
  36103. + */
  36104. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  36105. + hprt0.b.prtrst = 1;
  36106. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  36107. + }
  36108. + DWC_WORKQ_SCHEDULE_DELAYED(core_if->wq_otg,
  36109. + hcd_start_func, dwc_otg_hcd, 50,
  36110. + "start hcd");
  36111. +
  36112. + return 1;
  36113. +}
  36114. +
  36115. +/**
  36116. + * HCD Callback function for disconnect of the HCD.
  36117. + *
  36118. + * @param p void pointer to the <code>struct usb_hcd</code>
  36119. + */
  36120. +static int32_t dwc_otg_hcd_disconnect_cb(void *p)
  36121. +{
  36122. + gintsts_data_t intr;
  36123. + dwc_otg_hcd_t *dwc_otg_hcd = p;
  36124. +
  36125. + DWC_SPINLOCK(dwc_otg_hcd->lock);
  36126. + /*
  36127. + * Set status flags for the hub driver.
  36128. + */
  36129. + dwc_otg_hcd->flags.b.port_connect_status_change = 1;
  36130. + dwc_otg_hcd->flags.b.port_connect_status = 0;
  36131. + if(fiq_enable) {
  36132. + local_fiq_disable();
  36133. + fiq_fsm_spin_lock(&dwc_otg_hcd->fiq_state->lock);
  36134. + }
  36135. + /*
  36136. + * Shutdown any transfers in process by clearing the Tx FIFO Empty
  36137. + * interrupt mask and status bits and disabling subsequent host
  36138. + * channel interrupts.
  36139. + */
  36140. + intr.d32 = 0;
  36141. + intr.b.nptxfempty = 1;
  36142. + intr.b.ptxfempty = 1;
  36143. + intr.b.hcintr = 1;
  36144. + DWC_MODIFY_REG32(&dwc_otg_hcd->core_if->core_global_regs->gintmsk,
  36145. + intr.d32, 0);
  36146. + DWC_MODIFY_REG32(&dwc_otg_hcd->core_if->core_global_regs->gintsts,
  36147. + intr.d32, 0);
  36148. +
  36149. + del_timers(dwc_otg_hcd);
  36150. +
  36151. + /*
  36152. + * Turn off the vbus power only if the core has transitioned to device
  36153. + * mode. If still in host mode, need to keep power on to detect a
  36154. + * reconnection.
  36155. + */
  36156. + if (dwc_otg_is_device_mode(dwc_otg_hcd->core_if)) {
  36157. + if (dwc_otg_hcd->core_if->op_state != A_SUSPEND) {
  36158. + hprt0_data_t hprt0 = {.d32 = 0 };
  36159. + DWC_PRINTF("Disconnect: PortPower off\n");
  36160. + hprt0.b.prtpwr = 0;
  36161. + DWC_WRITE_REG32(dwc_otg_hcd->core_if->host_if->hprt0,
  36162. + hprt0.d32);
  36163. + }
  36164. +
  36165. + dwc_otg_disable_host_interrupts(dwc_otg_hcd->core_if);
  36166. + }
  36167. +
  36168. + /* Respond with an error status to all URBs in the schedule. */
  36169. + kill_all_urbs(dwc_otg_hcd);
  36170. +
  36171. + if (dwc_otg_is_host_mode(dwc_otg_hcd->core_if)) {
  36172. + /* Clean up any host channels that were in use. */
  36173. + int num_channels;
  36174. + int i;
  36175. + dwc_hc_t *channel;
  36176. + dwc_otg_hc_regs_t *hc_regs;
  36177. + hcchar_data_t hcchar;
  36178. +
  36179. + num_channels = dwc_otg_hcd->core_if->core_params->host_channels;
  36180. +
  36181. + if (!dwc_otg_hcd->core_if->dma_enable) {
  36182. + /* Flush out any channel requests in slave mode. */
  36183. + for (i = 0; i < num_channels; i++) {
  36184. + channel = dwc_otg_hcd->hc_ptr_array[i];
  36185. + if (DWC_CIRCLEQ_EMPTY_ENTRY
  36186. + (channel, hc_list_entry)) {
  36187. + hc_regs =
  36188. + dwc_otg_hcd->core_if->
  36189. + host_if->hc_regs[i];
  36190. + hcchar.d32 =
  36191. + DWC_READ_REG32(&hc_regs->hcchar);
  36192. + if (hcchar.b.chen) {
  36193. + hcchar.b.chen = 0;
  36194. + hcchar.b.chdis = 1;
  36195. + hcchar.b.epdir = 0;
  36196. + DWC_WRITE_REG32
  36197. + (&hc_regs->hcchar,
  36198. + hcchar.d32);
  36199. + }
  36200. + }
  36201. + }
  36202. + }
  36203. +
  36204. + if(fiq_fsm_enable) {
  36205. + for(i=0; i < 128; i++) {
  36206. + dwc_otg_hcd->hub_port[i] = 0;
  36207. + }
  36208. + }
  36209. + }
  36210. +
  36211. + if(fiq_enable) {
  36212. + fiq_fsm_spin_unlock(&dwc_otg_hcd->fiq_state->lock);
  36213. + local_fiq_enable();
  36214. + }
  36215. +
  36216. + if (dwc_otg_hcd->fops->disconnect) {
  36217. + dwc_otg_hcd->fops->disconnect(dwc_otg_hcd);
  36218. + }
  36219. +
  36220. + DWC_SPINUNLOCK(dwc_otg_hcd->lock);
  36221. + return 1;
  36222. +}
  36223. +
  36224. +/**
  36225. + * HCD Callback function for stopping the HCD.
  36226. + *
  36227. + * @param p void pointer to the <code>struct usb_hcd</code>
  36228. + */
  36229. +static int32_t dwc_otg_hcd_stop_cb(void *p)
  36230. +{
  36231. + dwc_otg_hcd_t *dwc_otg_hcd = p;
  36232. +
  36233. + DWC_DEBUGPL(DBG_HCDV, "%s(%p)\n", __func__, p);
  36234. + dwc_otg_hcd_stop(dwc_otg_hcd);
  36235. + return 1;
  36236. +}
  36237. +
  36238. +#ifdef CONFIG_USB_DWC_OTG_LPM
  36239. +/**
  36240. + * HCD Callback function for sleep of HCD.
  36241. + *
  36242. + * @param p void pointer to the <code>struct usb_hcd</code>
  36243. + */
  36244. +static int dwc_otg_hcd_sleep_cb(void *p)
  36245. +{
  36246. + dwc_otg_hcd_t *hcd = p;
  36247. +
  36248. + dwc_otg_hcd_free_hc_from_lpm(hcd);
  36249. +
  36250. + return 0;
  36251. +}
  36252. +#endif
  36253. +
  36254. +
  36255. +/**
  36256. + * HCD Callback function for Remote Wakeup.
  36257. + *
  36258. + * @param p void pointer to the <code>struct usb_hcd</code>
  36259. + */
  36260. +static int dwc_otg_hcd_rem_wakeup_cb(void *p)
  36261. +{
  36262. + dwc_otg_hcd_t *hcd = p;
  36263. +
  36264. + if (hcd->core_if->lx_state == DWC_OTG_L2) {
  36265. + hcd->flags.b.port_suspend_change = 1;
  36266. + }
  36267. +#ifdef CONFIG_USB_DWC_OTG_LPM
  36268. + else {
  36269. + hcd->flags.b.port_l1_change = 1;
  36270. + }
  36271. +#endif
  36272. + return 0;
  36273. +}
  36274. +
  36275. +/**
  36276. + * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
  36277. + * stopped.
  36278. + */
  36279. +void dwc_otg_hcd_stop(dwc_otg_hcd_t * hcd)
  36280. +{
  36281. + hprt0_data_t hprt0 = {.d32 = 0 };
  36282. +
  36283. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD STOP\n");
  36284. +
  36285. + /*
  36286. + * The root hub should be disconnected before this function is called.
  36287. + * The disconnect will clear the QTD lists (via ..._hcd_urb_dequeue)
  36288. + * and the QH lists (via ..._hcd_endpoint_disable).
  36289. + */
  36290. +
  36291. + /* Turn off all host-specific interrupts. */
  36292. + dwc_otg_disable_host_interrupts(hcd->core_if);
  36293. +
  36294. + /* Turn off the vbus power */
  36295. + DWC_PRINTF("PortPower off\n");
  36296. + hprt0.b.prtpwr = 0;
  36297. + DWC_WRITE_REG32(hcd->core_if->host_if->hprt0, hprt0.d32);
  36298. + dwc_mdelay(1);
  36299. +}
  36300. +
  36301. +int dwc_otg_hcd_urb_enqueue(dwc_otg_hcd_t * hcd,
  36302. + dwc_otg_hcd_urb_t * dwc_otg_urb, void **ep_handle,
  36303. + int atomic_alloc)
  36304. +{
  36305. + int retval = 0;
  36306. + uint8_t needs_scheduling = 0;
  36307. + dwc_otg_transaction_type_e tr_type;
  36308. + dwc_otg_qtd_t *qtd;
  36309. + gintmsk_data_t intr_mask = {.d32 = 0 };
  36310. + hprt0_data_t hprt0 = { .d32 = 0 };
  36311. +
  36312. +#ifdef DEBUG /* integrity checks (Broadcom) */
  36313. + if (NULL == hcd->core_if) {
  36314. + DWC_ERROR("**** DWC OTG HCD URB Enqueue - HCD has NULL core_if\n");
  36315. + /* No longer connected. */
  36316. + return -DWC_E_INVALID;
  36317. + }
  36318. +#endif
  36319. + if (!hcd->flags.b.port_connect_status) {
  36320. + /* No longer connected. */
  36321. + DWC_ERROR("Not connected\n");
  36322. + return -DWC_E_NO_DEVICE;
  36323. + }
  36324. +
  36325. + /* Some core configurations cannot support LS traffic on a FS root port */
  36326. + if ((hcd->fops->speed(hcd, dwc_otg_urb->priv) == USB_SPEED_LOW) &&
  36327. + (hcd->core_if->hwcfg2.b.fs_phy_type == 1) &&
  36328. + (hcd->core_if->hwcfg2.b.hs_phy_type == 1)) {
  36329. + hprt0.d32 = DWC_READ_REG32(hcd->core_if->host_if->hprt0);
  36330. + if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_FULL_SPEED) {
  36331. + return -DWC_E_NO_DEVICE;
  36332. + }
  36333. + }
  36334. +
  36335. + qtd = dwc_otg_hcd_qtd_create(dwc_otg_urb, atomic_alloc);
  36336. + if (qtd == NULL) {
  36337. + DWC_ERROR("DWC OTG HCD URB Enqueue failed creating QTD\n");
  36338. + return -DWC_E_NO_MEMORY;
  36339. + }
  36340. +#ifdef DEBUG /* integrity checks (Broadcom) */
  36341. + if (qtd->urb == NULL) {
  36342. + DWC_ERROR("**** DWC OTG HCD URB Enqueue created QTD with no URBs\n");
  36343. + return -DWC_E_NO_MEMORY;
  36344. + }
  36345. + if (qtd->urb->priv == NULL) {
  36346. + DWC_ERROR("**** DWC OTG HCD URB Enqueue created QTD URB with no URB handle\n");
  36347. + return -DWC_E_NO_MEMORY;
  36348. + }
  36349. +#endif
  36350. + intr_mask.d32 = DWC_READ_REG32(&hcd->core_if->core_global_regs->gintmsk);
  36351. + if(!intr_mask.b.sofintr || fiq_enable) needs_scheduling = 1;
  36352. + if((((dwc_otg_qh_t *)ep_handle)->ep_type == UE_BULK) && !(qtd->urb->flags & URB_GIVEBACK_ASAP))
  36353. + /* Do not schedule SG transactions until qtd has URB_GIVEBACK_ASAP set */
  36354. + needs_scheduling = 0;
  36355. +
  36356. + retval = dwc_otg_hcd_qtd_add(qtd, hcd, (dwc_otg_qh_t **) ep_handle, atomic_alloc);
  36357. + // creates a new queue in ep_handle if it doesn't exist already
  36358. + if (retval < 0) {
  36359. + DWC_ERROR("DWC OTG HCD URB Enqueue failed adding QTD. "
  36360. + "Error status %d\n", retval);
  36361. + dwc_otg_hcd_qtd_free(qtd);
  36362. + return retval;
  36363. + }
  36364. +
  36365. + if(needs_scheduling) {
  36366. + tr_type = dwc_otg_hcd_select_transactions(hcd);
  36367. + if (tr_type != DWC_OTG_TRANSACTION_NONE) {
  36368. + dwc_otg_hcd_queue_transactions(hcd, tr_type);
  36369. + }
  36370. + }
  36371. + return retval;
  36372. +}
  36373. +
  36374. +int dwc_otg_hcd_urb_dequeue(dwc_otg_hcd_t * hcd,
  36375. + dwc_otg_hcd_urb_t * dwc_otg_urb)
  36376. +{
  36377. + dwc_otg_qh_t *qh;
  36378. + dwc_otg_qtd_t *urb_qtd;
  36379. + BUG_ON(!hcd);
  36380. + BUG_ON(!dwc_otg_urb);
  36381. +
  36382. +#ifdef DEBUG /* integrity checks (Broadcom) */
  36383. +
  36384. + if (hcd == NULL) {
  36385. + DWC_ERROR("**** DWC OTG HCD URB Dequeue has NULL HCD\n");
  36386. + return -DWC_E_INVALID;
  36387. + }
  36388. + if (dwc_otg_urb == NULL) {
  36389. + DWC_ERROR("**** DWC OTG HCD URB Dequeue has NULL URB\n");
  36390. + return -DWC_E_INVALID;
  36391. + }
  36392. + if (dwc_otg_urb->qtd == NULL) {
  36393. + DWC_ERROR("**** DWC OTG HCD URB Dequeue with NULL QTD\n");
  36394. + return -DWC_E_INVALID;
  36395. + }
  36396. + urb_qtd = dwc_otg_urb->qtd;
  36397. + BUG_ON(!urb_qtd);
  36398. + if (urb_qtd->qh == NULL) {
  36399. + DWC_ERROR("**** DWC OTG HCD URB Dequeue with QTD with NULL Q handler\n");
  36400. + return -DWC_E_INVALID;
  36401. + }
  36402. +#else
  36403. + urb_qtd = dwc_otg_urb->qtd;
  36404. + BUG_ON(!urb_qtd);
  36405. +#endif
  36406. + qh = urb_qtd->qh;
  36407. + BUG_ON(!qh);
  36408. + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
  36409. + if (urb_qtd->in_process) {
  36410. + dump_channel_info(hcd, qh);
  36411. + }
  36412. + }
  36413. +#ifdef DEBUG /* integrity checks (Broadcom) */
  36414. + if (hcd->core_if == NULL) {
  36415. + DWC_ERROR("**** DWC OTG HCD URB Dequeue HCD has NULL core_if\n");
  36416. + return -DWC_E_INVALID;
  36417. + }
  36418. +#endif
  36419. + if (urb_qtd->in_process && qh->channel) {
  36420. + /* The QTD is in process (it has been assigned to a channel). */
  36421. + if (hcd->flags.b.port_connect_status) {
  36422. + int n = qh->channel->hc_num;
  36423. + /*
  36424. + * If still connected (i.e. in host mode), halt the
  36425. + * channel so it can be used for other transfers. If
  36426. + * no longer connected, the host registers can't be
  36427. + * written to halt the channel since the core is in
  36428. + * device mode.
  36429. + */
  36430. + /* In FIQ FSM mode, we need to shut down carefully.
  36431. + * The FIQ may attempt to restart a disabled channel */
  36432. + if (fiq_fsm_enable && (hcd->fiq_state->channel[n].fsm != FIQ_PASSTHROUGH)) {
  36433. + int retries = 3;
  36434. + int running = 0;
  36435. + enum fiq_fsm_state state;
  36436. +
  36437. + local_fiq_disable();
  36438. + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
  36439. + qh->channel->halt_status = DWC_OTG_HC_XFER_URB_DEQUEUE;
  36440. + qh->channel->halt_pending = 1;
  36441. + if (hcd->fiq_state->channel[n].fsm == FIQ_HS_ISOC_TURBO ||
  36442. + hcd->fiq_state->channel[n].fsm == FIQ_HS_ISOC_SLEEPING)
  36443. + hcd->fiq_state->channel[n].fsm = FIQ_HS_ISOC_ABORTED;
  36444. + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
  36445. + local_fiq_enable();
  36446. +
  36447. + if (dwc_qh_is_non_per(qh)) {
  36448. + do {
  36449. + state = READ_ONCE(hcd->fiq_state->channel[n].fsm);
  36450. + running = (state != FIQ_NP_SPLIT_DONE) &&
  36451. + (state != FIQ_NP_SPLIT_LS_ABORTED) &&
  36452. + (state != FIQ_NP_SPLIT_HS_ABORTED);
  36453. + if (!running)
  36454. + break;
  36455. + udelay(125);
  36456. + } while(--retries);
  36457. + if (!retries)
  36458. + DWC_WARN("Timed out waiting for FSM NP transfer to complete on %d",
  36459. + qh->channel->hc_num);
  36460. + }
  36461. + } else {
  36462. + dwc_otg_hc_halt(hcd->core_if, qh->channel,
  36463. + DWC_OTG_HC_XFER_URB_DEQUEUE);
  36464. + }
  36465. + }
  36466. + }
  36467. +
  36468. + /*
  36469. + * Free the QTD and clean up the associated QH. Leave the QH in the
  36470. + * schedule if it has any remaining QTDs.
  36471. + */
  36472. +
  36473. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue - "
  36474. + "delete %sQueue handler\n",
  36475. + hcd->core_if->dma_desc_enable?"DMA ":"");
  36476. + if (!hcd->core_if->dma_desc_enable) {
  36477. + uint8_t b = urb_qtd->in_process;
  36478. + if (nak_holdoff && qh->do_split && dwc_qh_is_non_per(qh))
  36479. + qh->nak_frame = 0xFFFF;
  36480. + dwc_otg_hcd_qtd_remove_and_free(hcd, urb_qtd, qh);
  36481. + if (b) {
  36482. + dwc_otg_hcd_qh_deactivate(hcd, qh, 0);
  36483. + qh->channel = NULL;
  36484. + } else if (DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
  36485. + dwc_otg_hcd_qh_remove(hcd, qh);
  36486. + }
  36487. + } else {
  36488. + dwc_otg_hcd_qtd_remove_and_free(hcd, urb_qtd, qh);
  36489. + }
  36490. + return 0;
  36491. +}
  36492. +
  36493. +int dwc_otg_hcd_endpoint_disable(dwc_otg_hcd_t * hcd, void *ep_handle,
  36494. + int retry)
  36495. +{
  36496. + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
  36497. + int retval = 0;
  36498. + dwc_irqflags_t flags;
  36499. +
  36500. + if (retry < 0) {
  36501. + retval = -DWC_E_INVALID;
  36502. + goto done;
  36503. + }
  36504. +
  36505. + if (!qh) {
  36506. + retval = -DWC_E_INVALID;
  36507. + goto done;
  36508. + }
  36509. +
  36510. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  36511. +
  36512. + while (!DWC_CIRCLEQ_EMPTY(&qh->qtd_list) && retry) {
  36513. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  36514. + retry--;
  36515. + dwc_msleep(5);
  36516. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  36517. + }
  36518. +
  36519. + dwc_otg_hcd_qh_remove(hcd, qh);
  36520. +
  36521. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  36522. + /*
  36523. + * Split dwc_otg_hcd_qh_remove_and_free() into qh_remove
  36524. + * and qh_free to prevent stack dump on DWC_DMA_FREE() with
  36525. + * irq_disabled (spinlock_irqsave) in dwc_otg_hcd_desc_list_free()
  36526. + * and dwc_otg_hcd_frame_list_alloc().
  36527. + */
  36528. + dwc_otg_hcd_qh_free(hcd, qh);
  36529. +
  36530. +done:
  36531. + return retval;
  36532. +}
  36533. +
  36534. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
  36535. +int dwc_otg_hcd_endpoint_reset(dwc_otg_hcd_t * hcd, void *ep_handle)
  36536. +{
  36537. + int retval = 0;
  36538. + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
  36539. + if (!qh)
  36540. + return -DWC_E_INVALID;
  36541. +
  36542. + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
  36543. + return retval;
  36544. +}
  36545. +#endif
  36546. +
  36547. +/**
  36548. + * HCD Callback structure for handling mode switching.
  36549. + */
  36550. +static dwc_otg_cil_callbacks_t hcd_cil_callbacks = {
  36551. + .start = dwc_otg_hcd_start_cb,
  36552. + .stop = dwc_otg_hcd_stop_cb,
  36553. + .disconnect = dwc_otg_hcd_disconnect_cb,
  36554. + .session_start = dwc_otg_hcd_session_start_cb,
  36555. + .resume_wakeup = dwc_otg_hcd_rem_wakeup_cb,
  36556. +#ifdef CONFIG_USB_DWC_OTG_LPM
  36557. + .sleep = dwc_otg_hcd_sleep_cb,
  36558. +#endif
  36559. + .p = 0,
  36560. +};
  36561. +
  36562. +/**
  36563. + * Reset tasklet function
  36564. + */
  36565. +static void reset_tasklet_func(void *data)
  36566. +{
  36567. + dwc_otg_hcd_t *dwc_otg_hcd = (dwc_otg_hcd_t *) data;
  36568. + dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
  36569. + hprt0_data_t hprt0;
  36570. +
  36571. + DWC_DEBUGPL(DBG_HCDV, "USB RESET tasklet called\n");
  36572. +
  36573. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  36574. + hprt0.b.prtrst = 1;
  36575. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  36576. + dwc_mdelay(60);
  36577. +
  36578. + hprt0.b.prtrst = 0;
  36579. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  36580. + dwc_otg_hcd->flags.b.port_reset_change = 1;
  36581. +}
  36582. +
  36583. +static void completion_tasklet_func(void *ptr)
  36584. +{
  36585. + dwc_otg_hcd_t *hcd = (dwc_otg_hcd_t *) ptr;
  36586. + struct urb *urb;
  36587. + urb_tq_entry_t *item;
  36588. + dwc_irqflags_t flags;
  36589. +
  36590. + /* This could just be spin_lock_irq */
  36591. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  36592. + while (!DWC_TAILQ_EMPTY(&hcd->completed_urb_list)) {
  36593. + item = DWC_TAILQ_FIRST(&hcd->completed_urb_list);
  36594. + urb = item->urb;
  36595. + DWC_TAILQ_REMOVE(&hcd->completed_urb_list, item,
  36596. + urb_tq_entries);
  36597. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  36598. + DWC_FREE(item);
  36599. +
  36600. + usb_hcd_giveback_urb(hcd->priv, urb, urb->status);
  36601. +
  36602. +
  36603. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  36604. + }
  36605. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  36606. + return;
  36607. +}
  36608. +
  36609. +static void qh_list_free(dwc_otg_hcd_t * hcd, dwc_list_link_t * qh_list)
  36610. +{
  36611. + dwc_list_link_t *item;
  36612. + dwc_otg_qh_t *qh;
  36613. + dwc_irqflags_t flags;
  36614. +
  36615. + if (!qh_list->next) {
  36616. + /* The list hasn't been initialized yet. */
  36617. + return;
  36618. + }
  36619. + /*
  36620. + * Hold spinlock here. Not needed in that case if bellow
  36621. + * function is being called from ISR
  36622. + */
  36623. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  36624. + /* Ensure there are no QTDs or URBs left. */
  36625. + kill_urbs_in_qh_list(hcd, qh_list);
  36626. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  36627. +
  36628. + DWC_LIST_FOREACH(item, qh_list) {
  36629. + qh = DWC_LIST_ENTRY(item, dwc_otg_qh_t, qh_list_entry);
  36630. + dwc_otg_hcd_qh_remove_and_free(hcd, qh);
  36631. + }
  36632. +}
  36633. +
  36634. +/**
  36635. + * Exit from Hibernation if Host did not detect SRP from connected SRP capable
  36636. + * Device during SRP time by host power up.
  36637. + */
  36638. +void dwc_otg_hcd_power_up(void *ptr)
  36639. +{
  36640. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  36641. + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
  36642. +
  36643. + DWC_PRINTF("%s called\n", __FUNCTION__);
  36644. +
  36645. + if (!core_if->hibernation_suspend) {
  36646. + DWC_PRINTF("Already exited from Hibernation\n");
  36647. + return;
  36648. + }
  36649. +
  36650. + /* Switch on the voltage to the core */
  36651. + gpwrdn.b.pwrdnswtch = 1;
  36652. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  36653. + dwc_udelay(10);
  36654. +
  36655. + /* Reset the core */
  36656. + gpwrdn.d32 = 0;
  36657. + gpwrdn.b.pwrdnrstn = 1;
  36658. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  36659. + dwc_udelay(10);
  36660. +
  36661. + /* Disable power clamps */
  36662. + gpwrdn.d32 = 0;
  36663. + gpwrdn.b.pwrdnclmp = 1;
  36664. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  36665. +
  36666. + /* Remove reset the core signal */
  36667. + gpwrdn.d32 = 0;
  36668. + gpwrdn.b.pwrdnrstn = 1;
  36669. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  36670. + dwc_udelay(10);
  36671. +
  36672. + /* Disable PMU interrupt */
  36673. + gpwrdn.d32 = 0;
  36674. + gpwrdn.b.pmuintsel = 1;
  36675. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  36676. +
  36677. + core_if->hibernation_suspend = 0;
  36678. +
  36679. + /* Disable PMU */
  36680. + gpwrdn.d32 = 0;
  36681. + gpwrdn.b.pmuactv = 1;
  36682. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  36683. + dwc_udelay(10);
  36684. +
  36685. + /* Enable VBUS */
  36686. + gpwrdn.d32 = 0;
  36687. + gpwrdn.b.dis_vbus = 1;
  36688. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  36689. +
  36690. + core_if->op_state = A_HOST;
  36691. + dwc_otg_core_init(core_if);
  36692. + dwc_otg_enable_global_interrupts(core_if);
  36693. + cil_hcd_start(core_if);
  36694. +}
  36695. +
  36696. +void dwc_otg_cleanup_fiq_channel(dwc_otg_hcd_t *hcd, uint32_t num)
  36697. +{
  36698. + struct fiq_channel_state *st = &hcd->fiq_state->channel[num];
  36699. + struct fiq_dma_blob *blob = hcd->fiq_dmab;
  36700. + int i;
  36701. +
  36702. + st->fsm = FIQ_PASSTHROUGH;
  36703. + st->hcchar_copy.d32 = 0;
  36704. + st->hcsplt_copy.d32 = 0;
  36705. + st->hcint_copy.d32 = 0;
  36706. + st->hcintmsk_copy.d32 = 0;
  36707. + st->hctsiz_copy.d32 = 0;
  36708. + st->hcdma_copy.d32 = 0;
  36709. + st->nr_errors = 0;
  36710. + st->hub_addr = 0;
  36711. + st->port_addr = 0;
  36712. + st->expected_uframe = 0;
  36713. + st->nrpackets = 0;
  36714. + st->dma_info.index = 0;
  36715. + for (i = 0; i < 6; i++)
  36716. + st->dma_info.slot_len[i] = 255;
  36717. + st->hs_isoc_info.index = 0;
  36718. + st->hs_isoc_info.iso_desc = NULL;
  36719. + st->hs_isoc_info.nrframes = 0;
  36720. +
  36721. + DWC_MEMSET(&blob->channel[num].index[0], 0x6b, 1128);
  36722. +}
  36723. +
  36724. +/**
  36725. + * Frees secondary storage associated with the dwc_otg_hcd structure contained
  36726. + * in the struct usb_hcd field.
  36727. + */
  36728. +static void dwc_otg_hcd_free(dwc_otg_hcd_t * dwc_otg_hcd)
  36729. +{
  36730. + struct device *dev = dwc_otg_hcd_to_dev(dwc_otg_hcd);
  36731. + int i;
  36732. +
  36733. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD FREE\n");
  36734. +
  36735. + del_timers(dwc_otg_hcd);
  36736. +
  36737. + /* Free memory for QH/QTD lists */
  36738. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->non_periodic_sched_inactive);
  36739. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->non_periodic_sched_active);
  36740. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_inactive);
  36741. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_ready);
  36742. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_assigned);
  36743. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_queued);
  36744. +
  36745. + /* Free memory for the host channels. */
  36746. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  36747. + dwc_hc_t *hc = dwc_otg_hcd->hc_ptr_array[i];
  36748. +
  36749. +#ifdef DEBUG
  36750. + if (dwc_otg_hcd->core_if->hc_xfer_timer[i]) {
  36751. + DWC_TIMER_FREE(dwc_otg_hcd->core_if->hc_xfer_timer[i]);
  36752. + }
  36753. +#endif
  36754. + if (hc != NULL) {
  36755. + DWC_DEBUGPL(DBG_HCDV, "HCD Free channel #%i, hc=%p\n",
  36756. + i, hc);
  36757. + DWC_FREE(hc);
  36758. + }
  36759. + }
  36760. +
  36761. + if (dwc_otg_hcd->core_if->dma_enable) {
  36762. + if (dwc_otg_hcd->status_buf_dma) {
  36763. + DWC_DMA_FREE(dev, DWC_OTG_HCD_STATUS_BUF_SIZE,
  36764. + dwc_otg_hcd->status_buf,
  36765. + dwc_otg_hcd->status_buf_dma);
  36766. + }
  36767. + } else if (dwc_otg_hcd->status_buf != NULL) {
  36768. + DWC_FREE(dwc_otg_hcd->status_buf);
  36769. + }
  36770. + DWC_SPINLOCK_FREE(dwc_otg_hcd->lock);
  36771. + /* Set core_if's lock pointer to NULL */
  36772. + dwc_otg_hcd->core_if->lock = NULL;
  36773. +
  36774. + DWC_TIMER_FREE(dwc_otg_hcd->conn_timer);
  36775. + DWC_TASK_FREE(dwc_otg_hcd->reset_tasklet);
  36776. + DWC_TASK_FREE(dwc_otg_hcd->completion_tasklet);
  36777. + DWC_DMA_FREE(dev, 16, dwc_otg_hcd->fiq_state->dummy_send,
  36778. + dwc_otg_hcd->fiq_state->dummy_send_dma);
  36779. + DWC_FREE(dwc_otg_hcd->fiq_state);
  36780. +
  36781. +#ifdef DWC_DEV_SRPCAP
  36782. + if (dwc_otg_hcd->core_if->power_down == 2 &&
  36783. + dwc_otg_hcd->core_if->pwron_timer) {
  36784. + DWC_TIMER_FREE(dwc_otg_hcd->core_if->pwron_timer);
  36785. + }
  36786. +#endif
  36787. + DWC_FREE(dwc_otg_hcd);
  36788. +}
  36789. +
  36790. +int dwc_otg_hcd_init(dwc_otg_hcd_t * hcd, dwc_otg_core_if_t * core_if)
  36791. +{
  36792. + struct device *dev = dwc_otg_hcd_to_dev(hcd);
  36793. + int retval = 0;
  36794. + int num_channels;
  36795. + int i;
  36796. + dwc_hc_t *channel;
  36797. +
  36798. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_SPINLOCK))
  36799. + DWC_SPINLOCK_ALLOC_LINUX_DEBUG(hcd->lock);
  36800. +#else
  36801. + hcd->lock = DWC_SPINLOCK_ALLOC();
  36802. +#endif
  36803. + DWC_DEBUGPL(DBG_HCDV, "init of HCD %p given core_if %p\n",
  36804. + hcd, core_if);
  36805. + if (!hcd->lock) {
  36806. + DWC_ERROR("Could not allocate lock for pcd");
  36807. + DWC_FREE(hcd);
  36808. + retval = -DWC_E_NO_MEMORY;
  36809. + goto out;
  36810. + }
  36811. + hcd->core_if = core_if;
  36812. +
  36813. + /* Register the HCD CIL Callbacks */
  36814. + dwc_otg_cil_register_hcd_callbacks(hcd->core_if,
  36815. + &hcd_cil_callbacks, hcd);
  36816. +
  36817. + /* Initialize the non-periodic schedule. */
  36818. + DWC_LIST_INIT(&hcd->non_periodic_sched_inactive);
  36819. + DWC_LIST_INIT(&hcd->non_periodic_sched_active);
  36820. +
  36821. + /* Initialize the periodic schedule. */
  36822. + DWC_LIST_INIT(&hcd->periodic_sched_inactive);
  36823. + DWC_LIST_INIT(&hcd->periodic_sched_ready);
  36824. + DWC_LIST_INIT(&hcd->periodic_sched_assigned);
  36825. + DWC_LIST_INIT(&hcd->periodic_sched_queued);
  36826. + DWC_TAILQ_INIT(&hcd->completed_urb_list);
  36827. + /*
  36828. + * Create a host channel descriptor for each host channel implemented
  36829. + * in the controller. Initialize the channel descriptor array.
  36830. + */
  36831. + DWC_CIRCLEQ_INIT(&hcd->free_hc_list);
  36832. + num_channels = hcd->core_if->core_params->host_channels;
  36833. + DWC_MEMSET(hcd->hc_ptr_array, 0, sizeof(hcd->hc_ptr_array));
  36834. + for (i = 0; i < num_channels; i++) {
  36835. + channel = DWC_ALLOC(sizeof(dwc_hc_t));
  36836. + if (channel == NULL) {
  36837. + retval = -DWC_E_NO_MEMORY;
  36838. + DWC_ERROR("%s: host channel allocation failed\n",
  36839. + __func__);
  36840. + dwc_otg_hcd_free(hcd);
  36841. + goto out;
  36842. + }
  36843. + channel->hc_num = i;
  36844. + hcd->hc_ptr_array[i] = channel;
  36845. +#ifdef DEBUG
  36846. + hcd->core_if->hc_xfer_timer[i] =
  36847. + DWC_TIMER_ALLOC("hc timer", hc_xfer_timeout,
  36848. + &hcd->core_if->hc_xfer_info[i]);
  36849. +#endif
  36850. + DWC_DEBUGPL(DBG_HCDV, "HCD Added channel #%d, hc=%p\n", i,
  36851. + channel);
  36852. + }
  36853. +
  36854. + if (fiq_enable) {
  36855. + hcd->fiq_state = DWC_ALLOC(sizeof(struct fiq_state) + (sizeof(struct fiq_channel_state) * num_channels));
  36856. + if (!hcd->fiq_state) {
  36857. + retval = -DWC_E_NO_MEMORY;
  36858. + DWC_ERROR("%s: cannot allocate fiq_state structure\n", __func__);
  36859. + dwc_otg_hcd_free(hcd);
  36860. + goto out;
  36861. + }
  36862. + DWC_MEMSET(hcd->fiq_state, 0, (sizeof(struct fiq_state) + (sizeof(struct fiq_channel_state) * num_channels)));
  36863. +
  36864. +#ifdef CONFIG_ARM64
  36865. + spin_lock_init(&hcd->fiq_state->lock);
  36866. +#endif
  36867. +
  36868. + for (i = 0; i < num_channels; i++) {
  36869. + hcd->fiq_state->channel[i].fsm = FIQ_PASSTHROUGH;
  36870. + }
  36871. + hcd->fiq_state->dummy_send = DWC_DMA_ALLOC_ATOMIC(dev, 16,
  36872. + &hcd->fiq_state->dummy_send_dma);
  36873. +
  36874. + hcd->fiq_stack = DWC_ALLOC(sizeof(struct fiq_stack));
  36875. + if (!hcd->fiq_stack) {
  36876. + retval = -DWC_E_NO_MEMORY;
  36877. + DWC_ERROR("%s: cannot allocate fiq_stack structure\n", __func__);
  36878. + dwc_otg_hcd_free(hcd);
  36879. + goto out;
  36880. + }
  36881. + hcd->fiq_stack->magic1 = 0xDEADBEEF;
  36882. + hcd->fiq_stack->magic2 = 0xD00DFEED;
  36883. + hcd->fiq_state->gintmsk_saved.d32 = ~0;
  36884. + hcd->fiq_state->haintmsk_saved.b2.chint = ~0;
  36885. +
  36886. + /* This bit is terrible and uses no API, but necessary. The FIQ has no concept of DMA pools
  36887. + * (and if it did, would be a lot slower). This allocates a chunk of memory (~9kiB for 8 host channels)
  36888. + * for use as transaction bounce buffers in a 2-D array. Our access into this chunk is done by some
  36889. + * moderately readable array casts.
  36890. + */
  36891. + hcd->fiq_dmab = DWC_DMA_ALLOC(dev, (sizeof(struct fiq_dma_channel) * num_channels), &hcd->fiq_state->dma_base);
  36892. + DWC_WARN("FIQ DMA bounce buffers: virt = %px dma = %pad len=%zu",
  36893. + hcd->fiq_dmab, &hcd->fiq_state->dma_base,
  36894. + sizeof(struct fiq_dma_channel) * num_channels);
  36895. +
  36896. + DWC_MEMSET(hcd->fiq_dmab, 0x6b, 9024);
  36897. +
  36898. + /* pointer for debug in fiq_print */
  36899. + hcd->fiq_state->fiq_dmab = hcd->fiq_dmab;
  36900. + if (fiq_fsm_enable) {
  36901. + int i;
  36902. + for (i=0; i < hcd->core_if->core_params->host_channels; i++) {
  36903. + dwc_otg_cleanup_fiq_channel(hcd, i);
  36904. + }
  36905. + DWC_PRINTF("FIQ FSM acceleration enabled for :\n%s%s%s%s",
  36906. + (fiq_fsm_mask & 0x1) ? "Non-periodic Split Transactions\n" : "",
  36907. + (fiq_fsm_mask & 0x2) ? "Periodic Split Transactions\n" : "",
  36908. + (fiq_fsm_mask & 0x4) ? "High-Speed Isochronous Endpoints\n" : "",
  36909. + (fiq_fsm_mask & 0x8) ? "Interrupt/Control Split Transaction hack enabled\n" : "");
  36910. + }
  36911. + }
  36912. +
  36913. + /* Initialize the Connection timeout timer. */
  36914. + hcd->conn_timer = DWC_TIMER_ALLOC("Connection timer",
  36915. + dwc_otg_hcd_connect_timeout, 0);
  36916. +
  36917. + printk(KERN_DEBUG "dwc_otg: Microframe scheduler %s\n", microframe_schedule ? "enabled":"disabled");
  36918. + if (microframe_schedule)
  36919. + init_hcd_usecs(hcd);
  36920. +
  36921. + /* Initialize reset tasklet. */
  36922. + hcd->reset_tasklet = DWC_TASK_ALLOC("reset_tasklet", reset_tasklet_func, hcd);
  36923. +
  36924. + hcd->completion_tasklet = DWC_TASK_ALLOC("completion_tasklet",
  36925. + completion_tasklet_func, hcd);
  36926. +#ifdef DWC_DEV_SRPCAP
  36927. + if (hcd->core_if->power_down == 2) {
  36928. + /* Initialize Power on timer for Host power up in case hibernation */
  36929. + hcd->core_if->pwron_timer = DWC_TIMER_ALLOC("PWRON TIMER",
  36930. + dwc_otg_hcd_power_up, core_if);
  36931. + }
  36932. +#endif
  36933. +
  36934. + /*
  36935. + * Allocate space for storing data on status transactions. Normally no
  36936. + * data is sent, but this space acts as a bit bucket. This must be
  36937. + * done after usb_add_hcd since that function allocates the DMA buffer
  36938. + * pool.
  36939. + */
  36940. + if (hcd->core_if->dma_enable) {
  36941. + hcd->status_buf =
  36942. + DWC_DMA_ALLOC(dev, DWC_OTG_HCD_STATUS_BUF_SIZE,
  36943. + &hcd->status_buf_dma);
  36944. + } else {
  36945. + hcd->status_buf = DWC_ALLOC(DWC_OTG_HCD_STATUS_BUF_SIZE);
  36946. + }
  36947. + if (!hcd->status_buf) {
  36948. + retval = -DWC_E_NO_MEMORY;
  36949. + DWC_ERROR("%s: status_buf allocation failed\n", __func__);
  36950. + dwc_otg_hcd_free(hcd);
  36951. + goto out;
  36952. + }
  36953. +
  36954. + hcd->otg_port = 1;
  36955. + hcd->frame_list = NULL;
  36956. + hcd->frame_list_dma = 0;
  36957. + hcd->periodic_qh_count = 0;
  36958. +
  36959. + DWC_MEMSET(hcd->hub_port, 0, sizeof(hcd->hub_port));
  36960. +#ifdef FIQ_DEBUG
  36961. + DWC_MEMSET(hcd->hub_port_alloc, -1, sizeof(hcd->hub_port_alloc));
  36962. +#endif
  36963. +
  36964. +out:
  36965. + return retval;
  36966. +}
  36967. +
  36968. +void dwc_otg_hcd_remove(dwc_otg_hcd_t * hcd)
  36969. +{
  36970. + /* Turn off all host-specific interrupts. */
  36971. + dwc_otg_disable_host_interrupts(hcd->core_if);
  36972. +
  36973. + dwc_otg_hcd_free(hcd);
  36974. +}
  36975. +
  36976. +/**
  36977. + * Initializes dynamic portions of the DWC_otg HCD state.
  36978. + */
  36979. +static void dwc_otg_hcd_reinit(dwc_otg_hcd_t * hcd)
  36980. +{
  36981. + int num_channels;
  36982. + int i;
  36983. + dwc_hc_t *channel;
  36984. + dwc_hc_t *channel_tmp;
  36985. +
  36986. + hcd->flags.d32 = 0;
  36987. +
  36988. + hcd->non_periodic_qh_ptr = &hcd->non_periodic_sched_active;
  36989. + if (!microframe_schedule) {
  36990. + hcd->non_periodic_channels = 0;
  36991. + hcd->periodic_channels = 0;
  36992. + } else {
  36993. + hcd->available_host_channels = hcd->core_if->core_params->host_channels;
  36994. + }
  36995. + /*
  36996. + * Put all channels in the free channel list and clean up channel
  36997. + * states.
  36998. + */
  36999. + DWC_CIRCLEQ_FOREACH_SAFE(channel, channel_tmp,
  37000. + &hcd->free_hc_list, hc_list_entry) {
  37001. + DWC_CIRCLEQ_REMOVE(&hcd->free_hc_list, channel, hc_list_entry);
  37002. + }
  37003. +
  37004. + num_channels = hcd->core_if->core_params->host_channels;
  37005. + for (i = 0; i < num_channels; i++) {
  37006. + channel = hcd->hc_ptr_array[i];
  37007. + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, channel,
  37008. + hc_list_entry);
  37009. + dwc_otg_hc_cleanup(hcd->core_if, channel);
  37010. + }
  37011. +
  37012. + /* Initialize the DWC core for host mode operation. */
  37013. + dwc_otg_core_host_init(hcd->core_if);
  37014. +
  37015. + /* Set core_if's lock pointer to the hcd->lock */
  37016. + hcd->core_if->lock = hcd->lock;
  37017. +}
  37018. +
  37019. +/**
  37020. + * Assigns transactions from a QTD to a free host channel and initializes the
  37021. + * host channel to perform the transactions. The host channel is removed from
  37022. + * the free list.
  37023. + *
  37024. + * @param hcd The HCD state structure.
  37025. + * @param qh Transactions from the first QTD for this QH are selected and
  37026. + * assigned to a free host channel.
  37027. + */
  37028. +static void assign_and_init_hc(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  37029. +{
  37030. + dwc_hc_t *hc;
  37031. + dwc_otg_qtd_t *qtd;
  37032. + dwc_otg_hcd_urb_t *urb;
  37033. + void* ptr = NULL;
  37034. + uint16_t wLength;
  37035. + uint32_t intr_enable;
  37036. + unsigned long flags;
  37037. + gintmsk_data_t gintmsk = { .d32 = 0, };
  37038. + struct device *dev = dwc_otg_hcd_to_dev(hcd);
  37039. +
  37040. + qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
  37041. +
  37042. + urb = qtd->urb;
  37043. +
  37044. + DWC_DEBUGPL(DBG_HCDV, "%s(%p,%p) - urb %x, actual_length %d\n", __func__, hcd, qh, (unsigned int)urb, urb->actual_length);
  37045. +
  37046. + if (((urb->actual_length < 0) || (urb->actual_length > urb->length)) && !dwc_otg_hcd_is_pipe_in(&urb->pipe_info))
  37047. + urb->actual_length = urb->length;
  37048. +
  37049. +
  37050. + hc = DWC_CIRCLEQ_FIRST(&hcd->free_hc_list);
  37051. +
  37052. + /* Remove the host channel from the free list. */
  37053. + DWC_CIRCLEQ_REMOVE_INIT(&hcd->free_hc_list, hc, hc_list_entry);
  37054. +
  37055. + qh->channel = hc;
  37056. +
  37057. + qtd->in_process = 1;
  37058. +
  37059. + /*
  37060. + * Use usb_pipedevice to determine device address. This address is
  37061. + * 0 before the SET_ADDRESS command and the correct address afterward.
  37062. + */
  37063. + hc->dev_addr = dwc_otg_hcd_get_dev_addr(&urb->pipe_info);
  37064. + hc->ep_num = dwc_otg_hcd_get_ep_num(&urb->pipe_info);
  37065. + hc->speed = qh->dev_speed;
  37066. + hc->max_packet = dwc_max_packet(qh->maxp);
  37067. +
  37068. + hc->xfer_started = 0;
  37069. + hc->halt_status = DWC_OTG_HC_XFER_NO_HALT_STATUS;
  37070. + hc->error_state = (qtd->error_count > 0);
  37071. + hc->halt_on_queue = 0;
  37072. + hc->halt_pending = 0;
  37073. + hc->requests = 0;
  37074. +
  37075. + /*
  37076. + * The following values may be modified in the transfer type section
  37077. + * below. The xfer_len value may be reduced when the transfer is
  37078. + * started to accommodate the max widths of the XferSize and PktCnt
  37079. + * fields in the HCTSIZn register.
  37080. + */
  37081. +
  37082. + hc->ep_is_in = (dwc_otg_hcd_is_pipe_in(&urb->pipe_info) != 0);
  37083. + if (hc->ep_is_in) {
  37084. + hc->do_ping = 0;
  37085. + } else {
  37086. + hc->do_ping = qh->ping_state;
  37087. + }
  37088. +
  37089. + hc->data_pid_start = qh->data_toggle;
  37090. + hc->multi_count = 1;
  37091. +
  37092. + if (hcd->core_if->dma_enable) {
  37093. + hc->xfer_buff =
  37094. + (uint8_t *)(uintptr_t)urb->dma + urb->actual_length;
  37095. +
  37096. + /* For non-dword aligned case */
  37097. + if (((unsigned long)hc->xfer_buff & 0x3)
  37098. + && !hcd->core_if->dma_desc_enable) {
  37099. + ptr = (uint8_t *) urb->buf + urb->actual_length;
  37100. + }
  37101. + } else {
  37102. + hc->xfer_buff = (uint8_t *) urb->buf + urb->actual_length;
  37103. + }
  37104. + hc->xfer_len = urb->length - urb->actual_length;
  37105. + hc->xfer_count = 0;
  37106. +
  37107. + /*
  37108. + * Set the split attributes
  37109. + */
  37110. + hc->do_split = 0;
  37111. + if (qh->do_split) {
  37112. + uint32_t hub_addr, port_addr;
  37113. + hc->do_split = 1;
  37114. + hc->start_pkt_count = 1;
  37115. + hc->xact_pos = qtd->isoc_split_pos;
  37116. + /* We don't need to do complete splits anymore */
  37117. +// if(fiq_fsm_enable)
  37118. + if (0)
  37119. + hc->complete_split = qtd->complete_split = 0;
  37120. + else
  37121. + hc->complete_split = qtd->complete_split;
  37122. +
  37123. + hcd->fops->hub_info(hcd, urb->priv, &hub_addr, &port_addr);
  37124. + hc->hub_addr = (uint8_t) hub_addr;
  37125. + hc->port_addr = (uint8_t) port_addr;
  37126. + }
  37127. +
  37128. + switch (dwc_otg_hcd_get_pipe_type(&urb->pipe_info)) {
  37129. + case UE_CONTROL:
  37130. + hc->ep_type = DWC_OTG_EP_TYPE_CONTROL;
  37131. + switch (qtd->control_phase) {
  37132. + case DWC_OTG_CONTROL_SETUP:
  37133. + DWC_DEBUGPL(DBG_HCDV, " Control setup transaction\n");
  37134. + hc->do_ping = 0;
  37135. + hc->ep_is_in = 0;
  37136. + hc->data_pid_start = DWC_OTG_HC_PID_SETUP;
  37137. + if (hcd->core_if->dma_enable) {
  37138. + hc->xfer_buff =
  37139. + (uint8_t *)(uintptr_t)urb->setup_dma;
  37140. + } else {
  37141. + hc->xfer_buff = (uint8_t *) urb->setup_packet;
  37142. + }
  37143. + hc->xfer_len = 8;
  37144. + ptr = NULL;
  37145. + break;
  37146. + case DWC_OTG_CONTROL_DATA:
  37147. + DWC_DEBUGPL(DBG_HCDV, " Control data transaction\n");
  37148. + /*
  37149. + * Hardware bug: small IN packets with length < 4
  37150. + * cause a 4-byte write to memory. We can only catch
  37151. + * the case where we know a short packet is going to be
  37152. + * returned in a control transfer, as the length is
  37153. + * specified in the setup packet. This is only an issue
  37154. + * for drivers that insist on packing a device's various
  37155. + * properties into a struct and querying them one at a
  37156. + * time (uvcvideo).
  37157. + * Force the use of align_buf so that the subsequent
  37158. + * memcpy puts the right number of bytes in the URB's
  37159. + * buffer.
  37160. + */
  37161. + wLength = ((uint16_t *)urb->setup_packet)[3];
  37162. + if (hc->ep_is_in && wLength < 4)
  37163. + ptr = hc->xfer_buff;
  37164. +
  37165. + hc->data_pid_start = qtd->data_toggle;
  37166. + break;
  37167. + case DWC_OTG_CONTROL_STATUS:
  37168. + /*
  37169. + * Direction is opposite of data direction or IN if no
  37170. + * data.
  37171. + */
  37172. + DWC_DEBUGPL(DBG_HCDV, " Control status transaction\n");
  37173. + if (urb->length == 0) {
  37174. + hc->ep_is_in = 1;
  37175. + } else {
  37176. + hc->ep_is_in =
  37177. + dwc_otg_hcd_is_pipe_out(&urb->pipe_info);
  37178. + }
  37179. + if (hc->ep_is_in) {
  37180. + hc->do_ping = 0;
  37181. + }
  37182. +
  37183. + hc->data_pid_start = DWC_OTG_HC_PID_DATA1;
  37184. +
  37185. + hc->xfer_len = 0;
  37186. + if (hcd->core_if->dma_enable) {
  37187. + hc->xfer_buff = (uint8_t *)
  37188. + (uintptr_t)hcd->status_buf_dma;
  37189. + } else {
  37190. + hc->xfer_buff = (uint8_t *) hcd->status_buf;
  37191. + }
  37192. + ptr = NULL;
  37193. + break;
  37194. + }
  37195. + break;
  37196. + case UE_BULK:
  37197. + hc->ep_type = DWC_OTG_EP_TYPE_BULK;
  37198. + break;
  37199. + case UE_INTERRUPT:
  37200. + hc->ep_type = DWC_OTG_EP_TYPE_INTR;
  37201. + break;
  37202. + case UE_ISOCHRONOUS:
  37203. + {
  37204. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  37205. +
  37206. + hc->ep_type = DWC_OTG_EP_TYPE_ISOC;
  37207. +
  37208. + if (hcd->core_if->dma_desc_enable)
  37209. + break;
  37210. +
  37211. + frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
  37212. +
  37213. + frame_desc->status = 0;
  37214. +
  37215. + if (hcd->core_if->dma_enable) {
  37216. + hc->xfer_buff = (uint8_t *)(uintptr_t)urb->dma;
  37217. + } else {
  37218. + hc->xfer_buff = (uint8_t *) urb->buf;
  37219. + }
  37220. + hc->xfer_buff +=
  37221. + frame_desc->offset + qtd->isoc_split_offset;
  37222. + hc->xfer_len =
  37223. + frame_desc->length - qtd->isoc_split_offset;
  37224. +
  37225. + /* For non-dword aligned buffers */
  37226. + if (((unsigned long)hc->xfer_buff & 0x3)
  37227. + && hcd->core_if->dma_enable) {
  37228. + ptr =
  37229. + (uint8_t *) urb->buf + frame_desc->offset +
  37230. + qtd->isoc_split_offset;
  37231. + } else
  37232. + ptr = NULL;
  37233. +
  37234. + if (hc->xact_pos == DWC_HCSPLIT_XACTPOS_ALL) {
  37235. + if (hc->xfer_len <= 188) {
  37236. + hc->xact_pos = DWC_HCSPLIT_XACTPOS_ALL;
  37237. + } else {
  37238. + hc->xact_pos =
  37239. + DWC_HCSPLIT_XACTPOS_BEGIN;
  37240. + }
  37241. + }
  37242. + }
  37243. + break;
  37244. + }
  37245. + /* non DWORD-aligned buffer case */
  37246. + if (ptr) {
  37247. + uint32_t buf_size;
  37248. + if (hc->ep_type != DWC_OTG_EP_TYPE_ISOC) {
  37249. + buf_size = hcd->core_if->core_params->max_transfer_size;
  37250. + } else {
  37251. + buf_size = 4096;
  37252. + }
  37253. + if (!qh->dw_align_buf) {
  37254. + qh->dw_align_buf = DWC_DMA_ALLOC_ATOMIC(dev, buf_size,
  37255. + &qh->dw_align_buf_dma);
  37256. + if (!qh->dw_align_buf) {
  37257. + DWC_ERROR
  37258. + ("%s: Failed to allocate memory to handle "
  37259. + "non-dword aligned buffer case\n",
  37260. + __func__);
  37261. + return;
  37262. + }
  37263. + }
  37264. + if (!hc->ep_is_in) {
  37265. + dwc_memcpy(qh->dw_align_buf, ptr, hc->xfer_len);
  37266. + }
  37267. + hc->align_buff = qh->dw_align_buf_dma;
  37268. + } else {
  37269. + hc->align_buff = 0;
  37270. + }
  37271. +
  37272. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  37273. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  37274. + /*
  37275. + * This value may be modified when the transfer is started to
  37276. + * reflect the actual transfer length.
  37277. + */
  37278. + hc->multi_count = dwc_hb_mult(qh->maxp);
  37279. + }
  37280. +
  37281. + if (hcd->core_if->dma_desc_enable)
  37282. + hc->desc_list_addr = qh->desc_list_dma;
  37283. +
  37284. + dwc_otg_hc_init(hcd->core_if, hc);
  37285. +
  37286. + local_irq_save(flags);
  37287. +
  37288. + if (fiq_enable) {
  37289. + local_fiq_disable();
  37290. + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
  37291. + }
  37292. +
  37293. + /* Enable the top level host channel interrupt. */
  37294. + intr_enable = (1 << hc->hc_num);
  37295. + DWC_MODIFY_REG32(&hcd->core_if->host_if->host_global_regs->haintmsk, 0, intr_enable);
  37296. +
  37297. + /* Make sure host channel interrupts are enabled. */
  37298. + gintmsk.b.hcintr = 1;
  37299. + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk, 0, gintmsk.d32);
  37300. +
  37301. + if (fiq_enable) {
  37302. + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
  37303. + local_fiq_enable();
  37304. + }
  37305. +
  37306. + local_irq_restore(flags);
  37307. + hc->qh = qh;
  37308. +}
  37309. +
  37310. +
  37311. +/**
  37312. + * fiq_fsm_transaction_suitable() - Test a QH for compatibility with the FIQ
  37313. + * @hcd: Pointer to the dwc_otg_hcd struct
  37314. + * @qh: pointer to the endpoint's queue head
  37315. + *
  37316. + * Transaction start/end control flow is grafted onto the existing dwc_otg
  37317. + * mechanisms, to avoid spaghettifying the functions more than they already are.
  37318. + * This function's eligibility check is altered by debug parameter.
  37319. + *
  37320. + * Returns: 0 for unsuitable, 1 implies the FIQ can be enabled for this transaction.
  37321. + */
  37322. +
  37323. +int fiq_fsm_transaction_suitable(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh)
  37324. +{
  37325. + if (qh->do_split) {
  37326. + switch (qh->ep_type) {
  37327. + case UE_CONTROL:
  37328. + case UE_BULK:
  37329. + if (fiq_fsm_mask & (1 << 0))
  37330. + return 1;
  37331. + break;
  37332. + case UE_INTERRUPT:
  37333. + case UE_ISOCHRONOUS:
  37334. + if (fiq_fsm_mask & (1 << 1))
  37335. + return 1;
  37336. + break;
  37337. + default:
  37338. + break;
  37339. + }
  37340. + } else if (qh->ep_type == UE_ISOCHRONOUS) {
  37341. + if (fiq_fsm_mask & (1 << 2)) {
  37342. + /* ISOCH support. We test for compatibility:
  37343. + * - DWORD aligned buffers
  37344. + * - Must be at least 2 transfers (otherwise pointless to use the FIQ)
  37345. + * If yes, then the fsm enqueue function will handle the state machine setup.
  37346. + */
  37347. + dwc_otg_qtd_t *qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
  37348. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  37349. + dwc_dma_t ptr;
  37350. + int i;
  37351. +
  37352. + if (urb->packet_count < 2)
  37353. + return 0;
  37354. + for (i = 0; i < urb->packet_count; i++) {
  37355. + ptr = urb->dma + urb->iso_descs[i].offset;
  37356. + if (ptr & 0x3)
  37357. + return 0;
  37358. + }
  37359. + return 1;
  37360. + }
  37361. + }
  37362. + return 0;
  37363. +}
  37364. +
  37365. +/**
  37366. + * fiq_fsm_setup_periodic_dma() - Set up DMA bounce buffers
  37367. + * @hcd: Pointer to the dwc_otg_hcd struct
  37368. + * @qh: Pointer to the endpoint's queue head
  37369. + *
  37370. + * Periodic split transactions are transmitted modulo 188 bytes.
  37371. + * This necessitates slicing data up into buckets for isochronous out
  37372. + * and fixing up the DMA address for all IN transfers.
  37373. + *
  37374. + * Returns 1 if the DMA bounce buffers have been used, 0 if the default
  37375. + * HC buffer has been used.
  37376. + */
  37377. +int fiq_fsm_setup_periodic_dma(dwc_otg_hcd_t *hcd, struct fiq_channel_state *st, dwc_otg_qh_t *qh)
  37378. + {
  37379. + int frame_length, i = 0;
  37380. + uint8_t *ptr = NULL;
  37381. + dwc_hc_t *hc = qh->channel;
  37382. + struct fiq_dma_blob *blob;
  37383. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  37384. +
  37385. + for (i = 0; i < 6; i++) {
  37386. + st->dma_info.slot_len[i] = 255;
  37387. + }
  37388. + st->dma_info.index = 0;
  37389. + i = 0;
  37390. + if (hc->ep_is_in) {
  37391. + /*
  37392. + * Set dma_regs to bounce buffer. FIQ will update the
  37393. + * state depending on transaction progress.
  37394. + * Pointer arithmetic on hcd->fiq_state->dma_base (a dma_addr_t)
  37395. + * to point it to the correct offset in the allocated buffers.
  37396. + */
  37397. + blob = (struct fiq_dma_blob *)
  37398. + (uintptr_t)hcd->fiq_state->dma_base;
  37399. + st->hcdma_copy.d32 =(u32)(uintptr_t)
  37400. + blob->channel[hc->hc_num].index[0].buf;
  37401. +
  37402. + /* Calculate the max number of CSPLITS such that the FIQ can time out
  37403. + * a transaction if it fails.
  37404. + */
  37405. + frame_length = st->hcchar_copy.b.mps;
  37406. + do {
  37407. + i++;
  37408. + frame_length -= 188;
  37409. + } while (frame_length >= 0);
  37410. + st->nrpackets = i;
  37411. + return 1;
  37412. + } else {
  37413. + if (qh->ep_type == UE_ISOCHRONOUS) {
  37414. +
  37415. + dwc_otg_qtd_t *qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
  37416. +
  37417. + frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  37418. + frame_length = frame_desc->length;
  37419. +
  37420. + /* Virtual address for bounce buffers */
  37421. + blob = hcd->fiq_dmab;
  37422. +
  37423. + ptr = qtd->urb->buf + frame_desc->offset;
  37424. + if (frame_length == 0) {
  37425. + /*
  37426. + * for isochronous transactions, we must still transmit a packet
  37427. + * even if the length is zero.
  37428. + */
  37429. + st->dma_info.slot_len[0] = 0;
  37430. + st->nrpackets = 1;
  37431. + } else {
  37432. + do {
  37433. + if (frame_length <= 188) {
  37434. + dwc_memcpy(&blob->channel[hc->hc_num].index[i].buf[0], ptr, frame_length);
  37435. + st->dma_info.slot_len[i] = frame_length;
  37436. + ptr += frame_length;
  37437. + } else {
  37438. + dwc_memcpy(&blob->channel[hc->hc_num].index[i].buf[0], ptr, 188);
  37439. + st->dma_info.slot_len[i] = 188;
  37440. + ptr += 188;
  37441. + }
  37442. + i++;
  37443. + frame_length -= 188;
  37444. + } while (frame_length > 0);
  37445. + st->nrpackets = i;
  37446. + }
  37447. + ptr = qtd->urb->buf + frame_desc->offset;
  37448. + /*
  37449. + * Point the HC at the DMA address of the bounce buffers
  37450. + *
  37451. + * Pointer arithmetic on hcd->fiq_state->dma_base (a
  37452. + * dma_addr_t) to point it to the correct offset in the
  37453. + * allocated buffers.
  37454. + */
  37455. + blob = (struct fiq_dma_blob *)
  37456. + (uintptr_t)hcd->fiq_state->dma_base;
  37457. + st->hcdma_copy.d32 = (u32)(uintptr_t)
  37458. + blob->channel[hc->hc_num].index[0].buf;
  37459. +
  37460. + /* fixup xfersize to the actual packet size */
  37461. + st->hctsiz_copy.b.pid = 0;
  37462. + st->hctsiz_copy.b.xfersize = st->dma_info.slot_len[0];
  37463. + return 1;
  37464. + } else {
  37465. + /* For interrupt, single OUT packet required, goes in the SSPLIT from hc_buff. */
  37466. + return 0;
  37467. + }
  37468. + }
  37469. +}
  37470. +
  37471. +/**
  37472. + * fiq_fsm_np_tt_contended() - Avoid performing contended non-periodic transfers
  37473. + * @hcd: Pointer to the dwc_otg_hcd struct
  37474. + * @qh: Pointer to the endpoint's queue head
  37475. + *
  37476. + * Certain hub chips don't differentiate between IN and OUT non-periodic pipes
  37477. + * with the same endpoint number. If transfers get completed out of order
  37478. + * (disregarding the direction token) then the hub can lock up
  37479. + * or return erroneous responses.
  37480. + *
  37481. + * Returns 1 if initiating the transfer would cause contention, 0 otherwise.
  37482. + */
  37483. +int fiq_fsm_np_tt_contended(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh)
  37484. +{
  37485. + int i;
  37486. + struct fiq_channel_state *st;
  37487. + int dev_addr = qh->channel->dev_addr;
  37488. + int ep_num = qh->channel->ep_num;
  37489. + for (i = 0; i < hcd->core_if->core_params->host_channels; i++) {
  37490. + if (i == qh->channel->hc_num)
  37491. + continue;
  37492. + st = &hcd->fiq_state->channel[i];
  37493. + switch (st->fsm) {
  37494. + case FIQ_NP_SSPLIT_STARTED:
  37495. + case FIQ_NP_SSPLIT_RETRY:
  37496. + case FIQ_NP_SSPLIT_PENDING:
  37497. + case FIQ_NP_OUT_CSPLIT_RETRY:
  37498. + case FIQ_NP_IN_CSPLIT_RETRY:
  37499. + if (st->hcchar_copy.b.devaddr == dev_addr &&
  37500. + st->hcchar_copy.b.epnum == ep_num)
  37501. + return 1;
  37502. + break;
  37503. + default:
  37504. + break;
  37505. + }
  37506. + }
  37507. + return 0;
  37508. +}
  37509. +
  37510. +/*
  37511. + * Pushing a periodic request into the queue near the EOF1 point
  37512. + * in a microframe causes erroneous behaviour (frmovrun) interrupt.
  37513. + * Usually, the request goes out on the bus causing a transfer but
  37514. + * the core does not transfer the data to memory.
  37515. + * This guard interval (in number of 60MHz clocks) is required which
  37516. + * must cater for CPU latency between reading the value and enabling
  37517. + * the channel.
  37518. + */
  37519. +#define PERIODIC_FRREM_BACKOFF 1000
  37520. +
  37521. +int fiq_fsm_queue_isoc_transaction(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh)
  37522. +{
  37523. + dwc_hc_t *hc = qh->channel;
  37524. + dwc_otg_hc_regs_t *hc_regs = hcd->core_if->host_if->hc_regs[hc->hc_num];
  37525. + dwc_otg_qtd_t *qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
  37526. + int frame;
  37527. + struct fiq_channel_state *st = &hcd->fiq_state->channel[hc->hc_num];
  37528. + int xfer_len, nrpackets;
  37529. + hcdma_data_t hcdma;
  37530. + hfnum_data_t hfnum;
  37531. +
  37532. + if (st->fsm != FIQ_PASSTHROUGH)
  37533. + return 0;
  37534. +
  37535. + st->nr_errors = 0;
  37536. +
  37537. + st->hcchar_copy.d32 = 0;
  37538. + st->hcchar_copy.b.mps = hc->max_packet;
  37539. + st->hcchar_copy.b.epdir = hc->ep_is_in;
  37540. + st->hcchar_copy.b.devaddr = hc->dev_addr;
  37541. + st->hcchar_copy.b.epnum = hc->ep_num;
  37542. + st->hcchar_copy.b.eptype = hc->ep_type;
  37543. +
  37544. + st->hcintmsk_copy.b.chhltd = 1;
  37545. +
  37546. + frame = dwc_otg_hcd_get_frame_number(hcd);
  37547. + st->hcchar_copy.b.oddfrm = (frame & 0x1) ? 0 : 1;
  37548. +
  37549. + st->hcchar_copy.b.lspddev = 0;
  37550. + /* Enable the channel later as a final register write. */
  37551. +
  37552. + st->hcsplt_copy.d32 = 0;
  37553. +
  37554. + st->hs_isoc_info.iso_desc = (struct dwc_otg_hcd_iso_packet_desc *) &qtd->urb->iso_descs;
  37555. + st->hs_isoc_info.nrframes = qtd->urb->packet_count;
  37556. + /* grab the next DMA address offset from the array */
  37557. + st->hcdma_copy.d32 = qtd->urb->dma;
  37558. + hcdma.d32 = st->hcdma_copy.d32 + st->hs_isoc_info.iso_desc[0].offset;
  37559. +
  37560. + /* We need to set multi_count. This is a bit tricky - has to be set per-transaction as
  37561. + * the core needs to be told to send the correct number. Caution: for IN transfers,
  37562. + * this is always set to the maximum size of the endpoint. */
  37563. + xfer_len = st->hs_isoc_info.iso_desc[0].length;
  37564. + nrpackets = (xfer_len + st->hcchar_copy.b.mps - 1) / st->hcchar_copy.b.mps;
  37565. + if (nrpackets == 0)
  37566. + nrpackets = 1;
  37567. + st->hcchar_copy.b.multicnt = nrpackets;
  37568. + st->hctsiz_copy.b.pktcnt = nrpackets;
  37569. +
  37570. + /* Initial PID also needs to be set */
  37571. + if (st->hcchar_copy.b.epdir == 0) {
  37572. + st->hctsiz_copy.b.xfersize = xfer_len;
  37573. + switch (st->hcchar_copy.b.multicnt) {
  37574. + case 1:
  37575. + st->hctsiz_copy.b.pid = DWC_PID_DATA0;
  37576. + break;
  37577. + case 2:
  37578. + case 3:
  37579. + st->hctsiz_copy.b.pid = DWC_PID_MDATA;
  37580. + break;
  37581. + }
  37582. +
  37583. + } else {
  37584. + st->hctsiz_copy.b.xfersize = nrpackets * st->hcchar_copy.b.mps;
  37585. + switch (st->hcchar_copy.b.multicnt) {
  37586. + case 1:
  37587. + st->hctsiz_copy.b.pid = DWC_PID_DATA0;
  37588. + break;
  37589. + case 2:
  37590. + st->hctsiz_copy.b.pid = DWC_PID_DATA1;
  37591. + break;
  37592. + case 3:
  37593. + st->hctsiz_copy.b.pid = DWC_PID_DATA2;
  37594. + break;
  37595. + }
  37596. + }
  37597. +
  37598. + st->hs_isoc_info.stride = qh->interval;
  37599. + st->uframe_sleeps = 0;
  37600. +
  37601. + fiq_print(FIQDBG_INT, hcd->fiq_state, "FSMQ %01d ", hc->hc_num);
  37602. + fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hcchar_copy.d32);
  37603. + fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hctsiz_copy.d32);
  37604. + fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hcdma_copy.d32);
  37605. + hfnum.d32 = DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hfnum);
  37606. + local_fiq_disable();
  37607. + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
  37608. + DWC_WRITE_REG32(&hc_regs->hctsiz, st->hctsiz_copy.d32);
  37609. + DWC_WRITE_REG32(&hc_regs->hcsplt, st->hcsplt_copy.d32);
  37610. + DWC_WRITE_REG32(&hc_regs->hcdma, st->hcdma_copy.d32);
  37611. + DWC_WRITE_REG32(&hc_regs->hcchar, st->hcchar_copy.d32);
  37612. + DWC_WRITE_REG32(&hc_regs->hcintmsk, st->hcintmsk_copy.d32);
  37613. + if (hfnum.b.frrem < PERIODIC_FRREM_BACKOFF) {
  37614. + /* Prevent queueing near EOF1. Bad things happen if a periodic
  37615. + * split transaction is queued very close to EOF. SOF interrupt handler
  37616. + * will wake this channel at the next interrupt.
  37617. + */
  37618. + st->fsm = FIQ_HS_ISOC_SLEEPING;
  37619. + st->uframe_sleeps = 1;
  37620. + } else {
  37621. + st->fsm = FIQ_HS_ISOC_TURBO;
  37622. + st->hcchar_copy.b.chen = 1;
  37623. + DWC_WRITE_REG32(&hc_regs->hcchar, st->hcchar_copy.d32);
  37624. + }
  37625. + mb();
  37626. + st->hcchar_copy.b.chen = 0;
  37627. + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
  37628. + local_fiq_enable();
  37629. + return 0;
  37630. +}
  37631. +
  37632. +
  37633. +/**
  37634. + * fiq_fsm_queue_split_transaction() - Set up a host channel and FIQ state
  37635. + * @hcd: Pointer to the dwc_otg_hcd struct
  37636. + * @qh: Pointer to the endpoint's queue head
  37637. + *
  37638. + * This overrides the dwc_otg driver's normal method of queueing a transaction.
  37639. + * Called from dwc_otg_hcd_queue_transactions(), this performs specific setup
  37640. + * for the nominated host channel.
  37641. + *
  37642. + * For periodic transfers, it also peeks at the FIQ state to see if an immediate
  37643. + * start is possible. If not, then the FIQ is left to start the transfer.
  37644. + */
  37645. +int fiq_fsm_queue_split_transaction(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh)
  37646. +{
  37647. + int start_immediate = 1, i;
  37648. + hfnum_data_t hfnum;
  37649. + dwc_hc_t *hc = qh->channel;
  37650. + dwc_otg_hc_regs_t *hc_regs = hcd->core_if->host_if->hc_regs[hc->hc_num];
  37651. + /* Program HC registers, setup FIQ_state, examine FIQ if periodic, start transfer (not if uframe 5) */
  37652. + int hub_addr, port_addr, frame, uframe;
  37653. + struct fiq_channel_state *st = &hcd->fiq_state->channel[hc->hc_num];
  37654. +
  37655. + /*
  37656. + * Non-periodic channel assignments stay in the non_periodic_active queue.
  37657. + * Therefore we get repeatedly called until the FIQ's done processing this channel.
  37658. + */
  37659. + if (qh->channel->xfer_started == 1)
  37660. + return 0;
  37661. +
  37662. + if (st->fsm != FIQ_PASSTHROUGH) {
  37663. + pr_warn_ratelimited("%s:%d: Queue called for an active channel\n", __func__, __LINE__);
  37664. + return 0;
  37665. + }
  37666. +
  37667. + qh->channel->xfer_started = 1;
  37668. +
  37669. + st->nr_errors = 0;
  37670. +
  37671. + st->hcchar_copy.d32 = 0;
  37672. + st->hcchar_copy.b.mps = min_t(uint32_t, hc->xfer_len, hc->max_packet);
  37673. + st->hcchar_copy.b.epdir = hc->ep_is_in;
  37674. + st->hcchar_copy.b.devaddr = hc->dev_addr;
  37675. + st->hcchar_copy.b.epnum = hc->ep_num;
  37676. + st->hcchar_copy.b.eptype = hc->ep_type;
  37677. + if (hc->ep_type & 0x1) {
  37678. + if (hc->ep_is_in)
  37679. + st->hcchar_copy.b.multicnt = 3;
  37680. + else
  37681. + /* Docs say set this to 1, but driver sets to 0! */
  37682. + st->hcchar_copy.b.multicnt = 0;
  37683. + } else {
  37684. + st->hcchar_copy.b.multicnt = 1;
  37685. + st->hcchar_copy.b.oddfrm = 0;
  37686. + }
  37687. + st->hcchar_copy.b.lspddev = (hc->speed == DWC_OTG_EP_SPEED_LOW) ? 1 : 0;
  37688. + /* Enable the channel later as a final register write. */
  37689. +
  37690. + st->hcsplt_copy.d32 = 0;
  37691. + if(qh->do_split) {
  37692. + hcd->fops->hub_info(hcd, DWC_CIRCLEQ_FIRST(&qh->qtd_list)->urb->priv, &hub_addr, &port_addr);
  37693. + st->hcsplt_copy.b.compsplt = 0;
  37694. + st->hcsplt_copy.b.spltena = 1;
  37695. + // XACTPOS is for isoc-out only but needs initialising anyway.
  37696. + st->hcsplt_copy.b.xactpos = ISOC_XACTPOS_ALL;
  37697. + if((qh->ep_type == DWC_OTG_EP_TYPE_ISOC) && (!qh->ep_is_in)) {
  37698. + /* For packetsize 0 < L < 188, ISOC_XACTPOS_ALL.
  37699. + * for longer than this, ISOC_XACTPOS_BEGIN and the FIQ
  37700. + * will update as necessary.
  37701. + */
  37702. + if (hc->xfer_len > 188) {
  37703. + st->hcsplt_copy.b.xactpos = ISOC_XACTPOS_BEGIN;
  37704. + }
  37705. + }
  37706. + st->hcsplt_copy.b.hubaddr = (uint8_t) hub_addr;
  37707. + st->hcsplt_copy.b.prtaddr = (uint8_t) port_addr;
  37708. + st->hub_addr = hub_addr;
  37709. + st->port_addr = port_addr;
  37710. + }
  37711. +
  37712. + st->hctsiz_copy.d32 = 0;
  37713. + st->hctsiz_copy.b.dopng = 0;
  37714. + st->hctsiz_copy.b.pid = hc->data_pid_start;
  37715. +
  37716. + if (hc->ep_is_in || (hc->xfer_len > hc->max_packet)) {
  37717. + hc->xfer_len = min_t(uint32_t, hc->xfer_len, hc->max_packet);
  37718. + } else if (!hc->ep_is_in && (hc->xfer_len > 188)) {
  37719. + hc->xfer_len = 188;
  37720. + }
  37721. + st->hctsiz_copy.b.xfersize = hc->xfer_len;
  37722. +
  37723. + st->hctsiz_copy.b.pktcnt = 1;
  37724. +
  37725. + if (hc->ep_type & 0x1) {
  37726. + /*
  37727. + * For potentially multi-packet transfers, must use the DMA bounce buffers. For IN transfers,
  37728. + * the DMA address is the address of the first 188byte slot buffer in the bounce buffer array.
  37729. + * For multi-packet OUT transfers, we need to copy the data into the bounce buffer array so the FIQ can punt
  37730. + * the right address out as necessary. hc->xfer_buff and hc->xfer_len have already been set
  37731. + * in assign_and_init_hc(), but this is for the eventual transaction completion only. The FIQ
  37732. + * must not touch internal driver state.
  37733. + */
  37734. + if(!fiq_fsm_setup_periodic_dma(hcd, st, qh)) {
  37735. + if (hc->align_buff) {
  37736. + st->hcdma_copy.d32 = hc->align_buff;
  37737. + } else {
  37738. + st->hcdma_copy.d32 = ((unsigned long) hc->xfer_buff & 0xFFFFFFFF);
  37739. + }
  37740. + }
  37741. + } else {
  37742. + if (hc->align_buff) {
  37743. + st->hcdma_copy.d32 = hc->align_buff;
  37744. + } else {
  37745. + st->hcdma_copy.d32 = ((unsigned long) hc->xfer_buff & 0xFFFFFFFF);
  37746. + }
  37747. + }
  37748. + /* The FIQ depends upon no other interrupts being enabled except channel halt.
  37749. + * Fixup channel interrupt mask. */
  37750. + st->hcintmsk_copy.d32 = 0;
  37751. + st->hcintmsk_copy.b.chhltd = 1;
  37752. + st->hcintmsk_copy.b.ahberr = 1;
  37753. +
  37754. + /* Hack courtesy of FreeBSD: apparently forcing Interrupt Split transactions
  37755. + * as Control puts the transfer into the non-periodic request queue and the
  37756. + * non-periodic handler in the hub. Makes things lots easier.
  37757. + */
  37758. + if ((fiq_fsm_mask & 0x8) && hc->ep_type == UE_INTERRUPT) {
  37759. + st->hcchar_copy.b.multicnt = 0;
  37760. + st->hcchar_copy.b.oddfrm = 0;
  37761. + st->hcchar_copy.b.eptype = UE_CONTROL;
  37762. + if (hc->align_buff) {
  37763. + st->hcdma_copy.d32 = hc->align_buff;
  37764. + } else {
  37765. + st->hcdma_copy.d32 = ((unsigned long) hc->xfer_buff & 0xFFFFFFFF);
  37766. + }
  37767. + }
  37768. + DWC_WRITE_REG32(&hc_regs->hcdma, st->hcdma_copy.d32);
  37769. + DWC_WRITE_REG32(&hc_regs->hctsiz, st->hctsiz_copy.d32);
  37770. + DWC_WRITE_REG32(&hc_regs->hcsplt, st->hcsplt_copy.d32);
  37771. + DWC_WRITE_REG32(&hc_regs->hcchar, st->hcchar_copy.d32);
  37772. + DWC_WRITE_REG32(&hc_regs->hcintmsk, st->hcintmsk_copy.d32);
  37773. +
  37774. + local_fiq_disable();
  37775. + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
  37776. +
  37777. + if (hc->ep_type & 0x1) {
  37778. + hfnum.d32 = DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hfnum);
  37779. + frame = (hfnum.b.frnum & ~0x7) >> 3;
  37780. + uframe = hfnum.b.frnum & 0x7;
  37781. + if (hfnum.b.frrem < PERIODIC_FRREM_BACKOFF) {
  37782. + /* Prevent queueing near EOF1. Bad things happen if a periodic
  37783. + * split transaction is queued very close to EOF.
  37784. + */
  37785. + start_immediate = 0;
  37786. + } else if (uframe == 5) {
  37787. + start_immediate = 0;
  37788. + } else if (hc->ep_type == UE_ISOCHRONOUS && !hc->ep_is_in) {
  37789. + start_immediate = 0;
  37790. + } else if (hc->ep_is_in && fiq_fsm_too_late(hcd->fiq_state, hc->hc_num)) {
  37791. + start_immediate = 0;
  37792. + } else {
  37793. + /* Search through all host channels to determine if a transaction
  37794. + * is currently in progress */
  37795. + for (i = 0; i < hcd->core_if->core_params->host_channels; i++) {
  37796. + if (i == hc->hc_num || hcd->fiq_state->channel[i].fsm == FIQ_PASSTHROUGH)
  37797. + continue;
  37798. + switch (hcd->fiq_state->channel[i].fsm) {
  37799. + /* TT is reserved for channels that are in the middle of a periodic
  37800. + * split transaction.
  37801. + */
  37802. + case FIQ_PER_SSPLIT_STARTED:
  37803. + case FIQ_PER_CSPLIT_WAIT:
  37804. + case FIQ_PER_CSPLIT_NYET1:
  37805. + case FIQ_PER_CSPLIT_POLL:
  37806. + case FIQ_PER_ISO_OUT_ACTIVE:
  37807. + case FIQ_PER_ISO_OUT_LAST:
  37808. + if (hcd->fiq_state->channel[i].hub_addr == hub_addr &&
  37809. + hcd->fiq_state->channel[i].port_addr == port_addr) {
  37810. + start_immediate = 0;
  37811. + }
  37812. + break;
  37813. + default:
  37814. + break;
  37815. + }
  37816. + if (!start_immediate)
  37817. + break;
  37818. + }
  37819. + }
  37820. + }
  37821. + if ((fiq_fsm_mask & 0x8) && hc->ep_type == UE_INTERRUPT)
  37822. + start_immediate = 1;
  37823. +
  37824. + fiq_print(FIQDBG_INT, hcd->fiq_state, "FSMQ %01d %01d", hc->hc_num, start_immediate);
  37825. + fiq_print(FIQDBG_INT, hcd->fiq_state, "%08d", hfnum.b.frrem);
  37826. + //fiq_print(FIQDBG_INT, hcd->fiq_state, "H:%02dP:%02d", hub_addr, port_addr);
  37827. + //fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hctsiz_copy.d32);
  37828. + //fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hcdma_copy.d32);
  37829. + switch (hc->ep_type) {
  37830. + case UE_CONTROL:
  37831. + case UE_BULK:
  37832. + if (fiq_fsm_np_tt_contended(hcd, qh)) {
  37833. + st->fsm = FIQ_NP_SSPLIT_PENDING;
  37834. + start_immediate = 0;
  37835. + } else {
  37836. + st->fsm = FIQ_NP_SSPLIT_STARTED;
  37837. + }
  37838. + break;
  37839. + case UE_ISOCHRONOUS:
  37840. + if (hc->ep_is_in) {
  37841. + if (start_immediate) {
  37842. + st->fsm = FIQ_PER_SSPLIT_STARTED;
  37843. + } else {
  37844. + st->fsm = FIQ_PER_SSPLIT_QUEUED;
  37845. + }
  37846. + } else {
  37847. + if (start_immediate) {
  37848. + /* Single-isoc OUT packets don't require FIQ involvement */
  37849. + if (st->nrpackets == 1) {
  37850. + st->fsm = FIQ_PER_ISO_OUT_LAST;
  37851. + } else {
  37852. + st->fsm = FIQ_PER_ISO_OUT_ACTIVE;
  37853. + }
  37854. + } else {
  37855. + st->fsm = FIQ_PER_ISO_OUT_PENDING;
  37856. + }
  37857. + }
  37858. + break;
  37859. + case UE_INTERRUPT:
  37860. + if (fiq_fsm_mask & 0x8) {
  37861. + if (fiq_fsm_np_tt_contended(hcd, qh)) {
  37862. + st->fsm = FIQ_NP_SSPLIT_PENDING;
  37863. + start_immediate = 0;
  37864. + } else {
  37865. + st->fsm = FIQ_NP_SSPLIT_STARTED;
  37866. + }
  37867. + } else if (start_immediate) {
  37868. + st->fsm = FIQ_PER_SSPLIT_STARTED;
  37869. + } else {
  37870. + st->fsm = FIQ_PER_SSPLIT_QUEUED;
  37871. + }
  37872. + default:
  37873. + break;
  37874. + }
  37875. + if (start_immediate) {
  37876. + /* Set the oddfrm bit as close as possible to actual queueing */
  37877. + frame = dwc_otg_hcd_get_frame_number(hcd);
  37878. + st->expected_uframe = (frame + 1) & 0x3FFF;
  37879. + st->hcchar_copy.b.oddfrm = (frame & 0x1) ? 0 : 1;
  37880. + st->hcchar_copy.b.chen = 1;
  37881. + DWC_WRITE_REG32(&hc_regs->hcchar, st->hcchar_copy.d32);
  37882. + }
  37883. + mb();
  37884. + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
  37885. + local_fiq_enable();
  37886. + return 0;
  37887. +}
  37888. +
  37889. +
  37890. +/**
  37891. + * This function selects transactions from the HCD transfer schedule and
  37892. + * assigns them to available host channels. It is called from HCD interrupt
  37893. + * handler functions.
  37894. + *
  37895. + * @param hcd The HCD state structure.
  37896. + *
  37897. + * @return The types of new transactions that were assigned to host channels.
  37898. + */
  37899. +dwc_otg_transaction_type_e dwc_otg_hcd_select_transactions(dwc_otg_hcd_t * hcd)
  37900. +{
  37901. + dwc_list_link_t *qh_ptr;
  37902. + dwc_otg_qh_t *qh;
  37903. + int num_channels;
  37904. + dwc_otg_transaction_type_e ret_val = DWC_OTG_TRANSACTION_NONE;
  37905. +
  37906. +#ifdef DEBUG_HOST_CHANNELS
  37907. + last_sel_trans_num_per_scheduled = 0;
  37908. + last_sel_trans_num_nonper_scheduled = 0;
  37909. + last_sel_trans_num_avail_hc_at_start = hcd->available_host_channels;
  37910. +#endif /* DEBUG_HOST_CHANNELS */
  37911. +
  37912. + /* Process entries in the periodic ready list. */
  37913. + qh_ptr = DWC_LIST_FIRST(&hcd->periodic_sched_ready);
  37914. +
  37915. + while (qh_ptr != &hcd->periodic_sched_ready &&
  37916. + !DWC_CIRCLEQ_EMPTY(&hcd->free_hc_list)) {
  37917. +
  37918. + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
  37919. +
  37920. + if (microframe_schedule) {
  37921. + // Make sure we leave one channel for non periodic transactions.
  37922. + if (hcd->available_host_channels <= 1) {
  37923. + break;
  37924. + }
  37925. + hcd->available_host_channels--;
  37926. +#ifdef DEBUG_HOST_CHANNELS
  37927. + last_sel_trans_num_per_scheduled++;
  37928. +#endif /* DEBUG_HOST_CHANNELS */
  37929. + }
  37930. + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
  37931. + assign_and_init_hc(hcd, qh);
  37932. +
  37933. + /*
  37934. + * Move the QH from the periodic ready schedule to the
  37935. + * periodic assigned schedule.
  37936. + */
  37937. + qh_ptr = DWC_LIST_NEXT(qh_ptr);
  37938. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_assigned,
  37939. + &qh->qh_list_entry);
  37940. + }
  37941. +
  37942. + /*
  37943. + * Process entries in the inactive portion of the non-periodic
  37944. + * schedule. Some free host channels may not be used if they are
  37945. + * reserved for periodic transfers.
  37946. + */
  37947. + qh_ptr = hcd->non_periodic_sched_inactive.next;
  37948. + num_channels = hcd->core_if->core_params->host_channels;
  37949. + while (qh_ptr != &hcd->non_periodic_sched_inactive &&
  37950. + (microframe_schedule || hcd->non_periodic_channels <
  37951. + num_channels - hcd->periodic_channels) &&
  37952. + !DWC_CIRCLEQ_EMPTY(&hcd->free_hc_list)) {
  37953. +
  37954. + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
  37955. + /*
  37956. + * Check to see if this is a NAK'd retransmit, in which case ignore for retransmission
  37957. + * we hold off on bulk retransmissions to reduce NAK interrupt overhead for full-speed
  37958. + * cheeky devices that just hold off using NAKs
  37959. + */
  37960. + if (fiq_enable && nak_holdoff && qh->do_split) {
  37961. + if (qh->nak_frame != 0xffff) {
  37962. + uint16_t next_frame = dwc_frame_num_inc(qh->nak_frame, (qh->ep_type == UE_BULK) ? nak_holdoff : 8);
  37963. + uint16_t frame = dwc_otg_hcd_get_frame_number(hcd);
  37964. + if (dwc_frame_num_le(frame, next_frame)) {
  37965. + if(dwc_frame_num_le(next_frame, hcd->fiq_state->next_sched_frame)) {
  37966. + hcd->fiq_state->next_sched_frame = next_frame;
  37967. + }
  37968. + qh_ptr = DWC_LIST_NEXT(qh_ptr);
  37969. + continue;
  37970. + } else {
  37971. + qh->nak_frame = 0xFFFF;
  37972. + }
  37973. + }
  37974. + }
  37975. +
  37976. + if (microframe_schedule) {
  37977. + if (hcd->available_host_channels < 1) {
  37978. + break;
  37979. + }
  37980. + hcd->available_host_channels--;
  37981. +#ifdef DEBUG_HOST_CHANNELS
  37982. + last_sel_trans_num_nonper_scheduled++;
  37983. +#endif /* DEBUG_HOST_CHANNELS */
  37984. + }
  37985. +
  37986. + assign_and_init_hc(hcd, qh);
  37987. +
  37988. + /*
  37989. + * Move the QH from the non-periodic inactive schedule to the
  37990. + * non-periodic active schedule.
  37991. + */
  37992. + qh_ptr = DWC_LIST_NEXT(qh_ptr);
  37993. + DWC_LIST_MOVE_HEAD(&hcd->non_periodic_sched_active,
  37994. + &qh->qh_list_entry);
  37995. +
  37996. + if (!microframe_schedule)
  37997. + hcd->non_periodic_channels++;
  37998. + }
  37999. + /* we moved a non-periodic QH to the active schedule. If the inactive queue is empty,
  38000. + * stop the FIQ from kicking us. We could potentially still have elements here if we
  38001. + * ran out of host channels.
  38002. + */
  38003. + if (fiq_enable) {
  38004. + if (DWC_LIST_EMPTY(&hcd->non_periodic_sched_inactive)) {
  38005. + hcd->fiq_state->kick_np_queues = 0;
  38006. + } else {
  38007. + /* For each entry remaining in the NP inactive queue,
  38008. + * if this a NAK'd retransmit then don't set the kick flag.
  38009. + */
  38010. + if(nak_holdoff) {
  38011. + DWC_LIST_FOREACH(qh_ptr, &hcd->non_periodic_sched_inactive) {
  38012. + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
  38013. + if (qh->nak_frame == 0xFFFF) {
  38014. + hcd->fiq_state->kick_np_queues = 1;
  38015. + }
  38016. + }
  38017. + }
  38018. + }
  38019. + }
  38020. + if(!DWC_LIST_EMPTY(&hcd->periodic_sched_assigned))
  38021. + ret_val |= DWC_OTG_TRANSACTION_PERIODIC;
  38022. +
  38023. + if(!DWC_LIST_EMPTY(&hcd->non_periodic_sched_active))
  38024. + ret_val |= DWC_OTG_TRANSACTION_NON_PERIODIC;
  38025. +
  38026. +
  38027. +#ifdef DEBUG_HOST_CHANNELS
  38028. + last_sel_trans_num_avail_hc_at_end = hcd->available_host_channels;
  38029. +#endif /* DEBUG_HOST_CHANNELS */
  38030. + return ret_val;
  38031. +}
  38032. +
  38033. +/**
  38034. + * Attempts to queue a single transaction request for a host channel
  38035. + * associated with either a periodic or non-periodic transfer. This function
  38036. + * assumes that there is space available in the appropriate request queue. For
  38037. + * an OUT transfer or SETUP transaction in Slave mode, it checks whether space
  38038. + * is available in the appropriate Tx FIFO.
  38039. + *
  38040. + * @param hcd The HCD state structure.
  38041. + * @param hc Host channel descriptor associated with either a periodic or
  38042. + * non-periodic transfer.
  38043. + * @param fifo_dwords_avail Number of DWORDs available in the periodic Tx
  38044. + * FIFO for periodic transfers or the non-periodic Tx FIFO for non-periodic
  38045. + * transfers.
  38046. + *
  38047. + * @return 1 if a request is queued and more requests may be needed to
  38048. + * complete the transfer, 0 if no more requests are required for this
  38049. + * transfer, -1 if there is insufficient space in the Tx FIFO.
  38050. + */
  38051. +static int queue_transaction(dwc_otg_hcd_t * hcd,
  38052. + dwc_hc_t * hc, uint16_t fifo_dwords_avail)
  38053. +{
  38054. + int retval;
  38055. +
  38056. + if (hcd->core_if->dma_enable) {
  38057. + if (hcd->core_if->dma_desc_enable) {
  38058. + if (!hc->xfer_started
  38059. + || (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)) {
  38060. + dwc_otg_hcd_start_xfer_ddma(hcd, hc->qh);
  38061. + hc->qh->ping_state = 0;
  38062. + }
  38063. + } else if (!hc->xfer_started) {
  38064. + if (fiq_fsm_enable && hc->error_state) {
  38065. + hcd->fiq_state->channel[hc->hc_num].nr_errors =
  38066. + DWC_CIRCLEQ_FIRST(&hc->qh->qtd_list)->error_count;
  38067. + hcd->fiq_state->channel[hc->hc_num].fsm =
  38068. + FIQ_PASSTHROUGH_ERRORSTATE;
  38069. + }
  38070. + dwc_otg_hc_start_transfer(hcd->core_if, hc);
  38071. + hc->qh->ping_state = 0;
  38072. + }
  38073. + retval = 0;
  38074. + } else if (hc->halt_pending) {
  38075. + /* Don't queue a request if the channel has been halted. */
  38076. + retval = 0;
  38077. + } else if (hc->halt_on_queue) {
  38078. + dwc_otg_hc_halt(hcd->core_if, hc, hc->halt_status);
  38079. + retval = 0;
  38080. + } else if (hc->do_ping) {
  38081. + if (!hc->xfer_started) {
  38082. + dwc_otg_hc_start_transfer(hcd->core_if, hc);
  38083. + }
  38084. + retval = 0;
  38085. + } else if (!hc->ep_is_in || hc->data_pid_start == DWC_OTG_HC_PID_SETUP) {
  38086. + if ((fifo_dwords_avail * 4) >= hc->max_packet) {
  38087. + if (!hc->xfer_started) {
  38088. + dwc_otg_hc_start_transfer(hcd->core_if, hc);
  38089. + retval = 1;
  38090. + } else {
  38091. + retval =
  38092. + dwc_otg_hc_continue_transfer(hcd->core_if,
  38093. + hc);
  38094. + }
  38095. + } else {
  38096. + retval = -1;
  38097. + }
  38098. + } else {
  38099. + if (!hc->xfer_started) {
  38100. + dwc_otg_hc_start_transfer(hcd->core_if, hc);
  38101. + retval = 1;
  38102. + } else {
  38103. + retval = dwc_otg_hc_continue_transfer(hcd->core_if, hc);
  38104. + }
  38105. + }
  38106. +
  38107. + return retval;
  38108. +}
  38109. +
  38110. +/**
  38111. + * Processes periodic channels for the next frame and queues transactions for
  38112. + * these channels to the DWC_otg controller. After queueing transactions, the
  38113. + * Periodic Tx FIFO Empty interrupt is enabled if there are more transactions
  38114. + * to queue as Periodic Tx FIFO or request queue space becomes available.
  38115. + * Otherwise, the Periodic Tx FIFO Empty interrupt is disabled.
  38116. + */
  38117. +static void process_periodic_channels(dwc_otg_hcd_t * hcd)
  38118. +{
  38119. + hptxsts_data_t tx_status;
  38120. + dwc_list_link_t *qh_ptr;
  38121. + dwc_otg_qh_t *qh;
  38122. + int status = 0;
  38123. + int no_queue_space = 0;
  38124. + int no_fifo_space = 0;
  38125. +
  38126. + dwc_otg_host_global_regs_t *host_regs;
  38127. + host_regs = hcd->core_if->host_if->host_global_regs;
  38128. +
  38129. + DWC_DEBUGPL(DBG_HCDV, "Queue periodic transactions\n");
  38130. +#ifdef DEBUG
  38131. + tx_status.d32 = DWC_READ_REG32(&host_regs->hptxsts);
  38132. + DWC_DEBUGPL(DBG_HCDV,
  38133. + " P Tx Req Queue Space Avail (before queue): %d\n",
  38134. + tx_status.b.ptxqspcavail);
  38135. + DWC_DEBUGPL(DBG_HCDV, " P Tx FIFO Space Avail (before queue): %d\n",
  38136. + tx_status.b.ptxfspcavail);
  38137. +#endif
  38138. +
  38139. + qh_ptr = hcd->periodic_sched_assigned.next;
  38140. + while (qh_ptr != &hcd->periodic_sched_assigned) {
  38141. + tx_status.d32 = DWC_READ_REG32(&host_regs->hptxsts);
  38142. + if (tx_status.b.ptxqspcavail == 0) {
  38143. + no_queue_space = 1;
  38144. + break;
  38145. + }
  38146. +
  38147. + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
  38148. +
  38149. + // Do not send a split start transaction any later than frame .6
  38150. + // Note, we have to schedule a periodic in .5 to make it go in .6
  38151. + if(fiq_fsm_enable && qh->do_split && ((dwc_otg_hcd_get_frame_number(hcd) + 1) & 7) > 6)
  38152. + {
  38153. + qh_ptr = qh_ptr->next;
  38154. + hcd->fiq_state->next_sched_frame = dwc_otg_hcd_get_frame_number(hcd) | 7;
  38155. + continue;
  38156. + }
  38157. +
  38158. + if (fiq_fsm_enable && fiq_fsm_transaction_suitable(hcd, qh)) {
  38159. + if (qh->do_split)
  38160. + fiq_fsm_queue_split_transaction(hcd, qh);
  38161. + else
  38162. + fiq_fsm_queue_isoc_transaction(hcd, qh);
  38163. + } else {
  38164. +
  38165. + /*
  38166. + * Set a flag if we're queueing high-bandwidth in slave mode.
  38167. + * The flag prevents any halts to get into the request queue in
  38168. + * the middle of multiple high-bandwidth packets getting queued.
  38169. + */
  38170. + if (!hcd->core_if->dma_enable && qh->channel->multi_count > 1) {
  38171. + hcd->core_if->queuing_high_bandwidth = 1;
  38172. + }
  38173. + status = queue_transaction(hcd, qh->channel,
  38174. + tx_status.b.ptxfspcavail);
  38175. + if (status < 0) {
  38176. + no_fifo_space = 1;
  38177. + break;
  38178. + }
  38179. + }
  38180. +
  38181. + /*
  38182. + * In Slave mode, stay on the current transfer until there is
  38183. + * nothing more to do or the high-bandwidth request count is
  38184. + * reached. In DMA mode, only need to queue one request. The
  38185. + * controller automatically handles multiple packets for
  38186. + * high-bandwidth transfers.
  38187. + */
  38188. + if (hcd->core_if->dma_enable || status == 0 ||
  38189. + qh->channel->requests == qh->channel->multi_count) {
  38190. + qh_ptr = qh_ptr->next;
  38191. + /*
  38192. + * Move the QH from the periodic assigned schedule to
  38193. + * the periodic queued schedule.
  38194. + */
  38195. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_queued,
  38196. + &qh->qh_list_entry);
  38197. +
  38198. + /* done queuing high bandwidth */
  38199. + hcd->core_if->queuing_high_bandwidth = 0;
  38200. + }
  38201. + }
  38202. +
  38203. + if (!hcd->core_if->dma_enable) {
  38204. + dwc_otg_core_global_regs_t *global_regs;
  38205. + gintmsk_data_t intr_mask = {.d32 = 0 };
  38206. +
  38207. + global_regs = hcd->core_if->core_global_regs;
  38208. + intr_mask.b.ptxfempty = 1;
  38209. +#ifdef DEBUG
  38210. + tx_status.d32 = DWC_READ_REG32(&host_regs->hptxsts);
  38211. + DWC_DEBUGPL(DBG_HCDV,
  38212. + " P Tx Req Queue Space Avail (after queue): %d\n",
  38213. + tx_status.b.ptxqspcavail);
  38214. + DWC_DEBUGPL(DBG_HCDV,
  38215. + " P Tx FIFO Space Avail (after queue): %d\n",
  38216. + tx_status.b.ptxfspcavail);
  38217. +#endif
  38218. + if (!DWC_LIST_EMPTY(&hcd->periodic_sched_assigned) ||
  38219. + no_queue_space || no_fifo_space) {
  38220. + /*
  38221. + * May need to queue more transactions as the request
  38222. + * queue or Tx FIFO empties. Enable the periodic Tx
  38223. + * FIFO empty interrupt. (Always use the half-empty
  38224. + * level to ensure that new requests are loaded as
  38225. + * soon as possible.)
  38226. + */
  38227. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0,
  38228. + intr_mask.d32);
  38229. + } else {
  38230. + /*
  38231. + * Disable the Tx FIFO empty interrupt since there are
  38232. + * no more transactions that need to be queued right
  38233. + * now. This function is called from interrupt
  38234. + * handlers to queue more transactions as transfer
  38235. + * states change.
  38236. + */
  38237. + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32,
  38238. + 0);
  38239. + }
  38240. + }
  38241. +}
  38242. +
  38243. +/**
  38244. + * Processes active non-periodic channels and queues transactions for these
  38245. + * channels to the DWC_otg controller. After queueing transactions, the NP Tx
  38246. + * FIFO Empty interrupt is enabled if there are more transactions to queue as
  38247. + * NP Tx FIFO or request queue space becomes available. Otherwise, the NP Tx
  38248. + * FIFO Empty interrupt is disabled.
  38249. + */
  38250. +static void process_non_periodic_channels(dwc_otg_hcd_t * hcd)
  38251. +{
  38252. + gnptxsts_data_t tx_status;
  38253. + dwc_list_link_t *orig_qh_ptr;
  38254. + dwc_otg_qh_t *qh;
  38255. + int status;
  38256. + int no_queue_space = 0;
  38257. + int no_fifo_space = 0;
  38258. + int more_to_do = 0;
  38259. +
  38260. + dwc_otg_core_global_regs_t *global_regs =
  38261. + hcd->core_if->core_global_regs;
  38262. +
  38263. + DWC_DEBUGPL(DBG_HCDV, "Queue non-periodic transactions\n");
  38264. +#ifdef DEBUG
  38265. + tx_status.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  38266. + DWC_DEBUGPL(DBG_HCDV,
  38267. + " NP Tx Req Queue Space Avail (before queue): %d\n",
  38268. + tx_status.b.nptxqspcavail);
  38269. + DWC_DEBUGPL(DBG_HCDV, " NP Tx FIFO Space Avail (before queue): %d\n",
  38270. + tx_status.b.nptxfspcavail);
  38271. +#endif
  38272. + /*
  38273. + * Keep track of the starting point. Skip over the start-of-list
  38274. + * entry.
  38275. + */
  38276. + if (hcd->non_periodic_qh_ptr == &hcd->non_periodic_sched_active) {
  38277. + hcd->non_periodic_qh_ptr = hcd->non_periodic_qh_ptr->next;
  38278. + }
  38279. + orig_qh_ptr = hcd->non_periodic_qh_ptr;
  38280. +
  38281. + /*
  38282. + * Process once through the active list or until no more space is
  38283. + * available in the request queue or the Tx FIFO.
  38284. + */
  38285. + do {
  38286. + tx_status.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  38287. + if (!hcd->core_if->dma_enable && tx_status.b.nptxqspcavail == 0) {
  38288. + no_queue_space = 1;
  38289. + break;
  38290. + }
  38291. +
  38292. + qh = DWC_LIST_ENTRY(hcd->non_periodic_qh_ptr, dwc_otg_qh_t,
  38293. + qh_list_entry);
  38294. +
  38295. + if(fiq_fsm_enable && fiq_fsm_transaction_suitable(hcd, qh)) {
  38296. + fiq_fsm_queue_split_transaction(hcd, qh);
  38297. + } else {
  38298. + status = queue_transaction(hcd, qh->channel,
  38299. + tx_status.b.nptxfspcavail);
  38300. +
  38301. + if (status > 0) {
  38302. + more_to_do = 1;
  38303. + } else if (status < 0) {
  38304. + no_fifo_space = 1;
  38305. + break;
  38306. + }
  38307. + }
  38308. + /* Advance to next QH, skipping start-of-list entry. */
  38309. + hcd->non_periodic_qh_ptr = hcd->non_periodic_qh_ptr->next;
  38310. + if (hcd->non_periodic_qh_ptr == &hcd->non_periodic_sched_active) {
  38311. + hcd->non_periodic_qh_ptr =
  38312. + hcd->non_periodic_qh_ptr->next;
  38313. + }
  38314. +
  38315. + } while (hcd->non_periodic_qh_ptr != orig_qh_ptr);
  38316. +
  38317. + if (!hcd->core_if->dma_enable) {
  38318. + gintmsk_data_t intr_mask = {.d32 = 0 };
  38319. + intr_mask.b.nptxfempty = 1;
  38320. +
  38321. +#ifdef DEBUG
  38322. + tx_status.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  38323. + DWC_DEBUGPL(DBG_HCDV,
  38324. + " NP Tx Req Queue Space Avail (after queue): %d\n",
  38325. + tx_status.b.nptxqspcavail);
  38326. + DWC_DEBUGPL(DBG_HCDV,
  38327. + " NP Tx FIFO Space Avail (after queue): %d\n",
  38328. + tx_status.b.nptxfspcavail);
  38329. +#endif
  38330. + if (more_to_do || no_queue_space || no_fifo_space) {
  38331. + /*
  38332. + * May need to queue more transactions as the request
  38333. + * queue or Tx FIFO empties. Enable the non-periodic
  38334. + * Tx FIFO empty interrupt. (Always use the half-empty
  38335. + * level to ensure that new requests are loaded as
  38336. + * soon as possible.)
  38337. + */
  38338. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0,
  38339. + intr_mask.d32);
  38340. + } else {
  38341. + /*
  38342. + * Disable the Tx FIFO empty interrupt since there are
  38343. + * no more transactions that need to be queued right
  38344. + * now. This function is called from interrupt
  38345. + * handlers to queue more transactions as transfer
  38346. + * states change.
  38347. + */
  38348. + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32,
  38349. + 0);
  38350. + }
  38351. + }
  38352. +}
  38353. +
  38354. +/**
  38355. + * This function processes the currently active host channels and queues
  38356. + * transactions for these channels to the DWC_otg controller. It is called
  38357. + * from HCD interrupt handler functions.
  38358. + *
  38359. + * @param hcd The HCD state structure.
  38360. + * @param tr_type The type(s) of transactions to queue (non-periodic,
  38361. + * periodic, or both).
  38362. + */
  38363. +void dwc_otg_hcd_queue_transactions(dwc_otg_hcd_t * hcd,
  38364. + dwc_otg_transaction_type_e tr_type)
  38365. +{
  38366. +#ifdef DEBUG_SOF
  38367. + DWC_DEBUGPL(DBG_HCD, "Queue Transactions\n");
  38368. +#endif
  38369. + /* Process host channels associated with periodic transfers. */
  38370. + if ((tr_type == DWC_OTG_TRANSACTION_PERIODIC ||
  38371. + tr_type == DWC_OTG_TRANSACTION_ALL) &&
  38372. + !DWC_LIST_EMPTY(&hcd->periodic_sched_assigned)) {
  38373. +
  38374. + process_periodic_channels(hcd);
  38375. + }
  38376. +
  38377. + /* Process host channels associated with non-periodic transfers. */
  38378. + if (tr_type == DWC_OTG_TRANSACTION_NON_PERIODIC ||
  38379. + tr_type == DWC_OTG_TRANSACTION_ALL) {
  38380. + if (!DWC_LIST_EMPTY(&hcd->non_periodic_sched_active)) {
  38381. + process_non_periodic_channels(hcd);
  38382. + } else {
  38383. + /*
  38384. + * Ensure NP Tx FIFO empty interrupt is disabled when
  38385. + * there are no non-periodic transfers to process.
  38386. + */
  38387. + gintmsk_data_t gintmsk = {.d32 = 0 };
  38388. + gintmsk.b.nptxfempty = 1;
  38389. +
  38390. + if (fiq_enable) {
  38391. + local_fiq_disable();
  38392. + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
  38393. + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk, gintmsk.d32, 0);
  38394. + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
  38395. + local_fiq_enable();
  38396. + } else {
  38397. + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk, gintmsk.d32, 0);
  38398. + }
  38399. + }
  38400. + }
  38401. +}
  38402. +
  38403. +#ifdef DWC_HS_ELECT_TST
  38404. +/*
  38405. + * Quick and dirty hack to implement the HS Electrical Test
  38406. + * SINGLE_STEP_GET_DEVICE_DESCRIPTOR feature.
  38407. + *
  38408. + * This code was copied from our userspace app "hset". It sends a
  38409. + * Get Device Descriptor control sequence in two parts, first the
  38410. + * Setup packet by itself, followed some time later by the In and
  38411. + * Ack packets. Rather than trying to figure out how to add this
  38412. + * functionality to the normal driver code, we just hijack the
  38413. + * hardware, using these two function to drive the hardware
  38414. + * directly.
  38415. + */
  38416. +
  38417. +static dwc_otg_core_global_regs_t *global_regs;
  38418. +static dwc_otg_host_global_regs_t *hc_global_regs;
  38419. +static dwc_otg_hc_regs_t *hc_regs;
  38420. +static uint32_t *data_fifo;
  38421. +
  38422. +static void do_setup(void)
  38423. +{
  38424. + gintsts_data_t gintsts;
  38425. + hctsiz_data_t hctsiz;
  38426. + hcchar_data_t hcchar;
  38427. + haint_data_t haint;
  38428. + hcint_data_t hcint;
  38429. +
  38430. + /* Enable HAINTs */
  38431. + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0001);
  38432. +
  38433. + /* Enable HCINTs */
  38434. + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x04a3);
  38435. +
  38436. + /* Read GINTSTS */
  38437. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38438. +
  38439. + /* Read HAINT */
  38440. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  38441. +
  38442. + /* Read HCINT */
  38443. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  38444. +
  38445. + /* Read HCCHAR */
  38446. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38447. +
  38448. + /* Clear HCINT */
  38449. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  38450. +
  38451. + /* Clear HAINT */
  38452. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  38453. +
  38454. + /* Clear GINTSTS */
  38455. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  38456. +
  38457. + /* Read GINTSTS */
  38458. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38459. +
  38460. + /*
  38461. + * Send Setup packet (Get Device Descriptor)
  38462. + */
  38463. +
  38464. + /* Make sure channel is disabled */
  38465. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38466. + if (hcchar.b.chen) {
  38467. + hcchar.b.chdis = 1;
  38468. +// hcchar.b.chen = 1;
  38469. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  38470. + //sleep(1);
  38471. + dwc_mdelay(1000);
  38472. +
  38473. + /* Read GINTSTS */
  38474. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38475. +
  38476. + /* Read HAINT */
  38477. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  38478. +
  38479. + /* Read HCINT */
  38480. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  38481. +
  38482. + /* Read HCCHAR */
  38483. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38484. +
  38485. + /* Clear HCINT */
  38486. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  38487. +
  38488. + /* Clear HAINT */
  38489. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  38490. +
  38491. + /* Clear GINTSTS */
  38492. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  38493. +
  38494. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38495. + }
  38496. +
  38497. + /* Set HCTSIZ */
  38498. + hctsiz.d32 = 0;
  38499. + hctsiz.b.xfersize = 8;
  38500. + hctsiz.b.pktcnt = 1;
  38501. + hctsiz.b.pid = DWC_OTG_HC_PID_SETUP;
  38502. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  38503. +
  38504. + /* Set HCCHAR */
  38505. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38506. + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
  38507. + hcchar.b.epdir = 0;
  38508. + hcchar.b.epnum = 0;
  38509. + hcchar.b.mps = 8;
  38510. + hcchar.b.chen = 1;
  38511. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  38512. +
  38513. + /* Fill FIFO with Setup data for Get Device Descriptor */
  38514. + data_fifo = (uint32_t *) ((char *)global_regs + 0x1000);
  38515. + DWC_WRITE_REG32(data_fifo++, 0x01000680);
  38516. + DWC_WRITE_REG32(data_fifo++, 0x00080000);
  38517. +
  38518. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38519. +
  38520. + /* Wait for host channel interrupt */
  38521. + do {
  38522. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38523. + } while (gintsts.b.hcintr == 0);
  38524. +
  38525. + /* Disable HCINTs */
  38526. + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x0000);
  38527. +
  38528. + /* Disable HAINTs */
  38529. + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0000);
  38530. +
  38531. + /* Read HAINT */
  38532. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  38533. +
  38534. + /* Read HCINT */
  38535. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  38536. +
  38537. + /* Read HCCHAR */
  38538. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38539. +
  38540. + /* Clear HCINT */
  38541. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  38542. +
  38543. + /* Clear HAINT */
  38544. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  38545. +
  38546. + /* Clear GINTSTS */
  38547. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  38548. +
  38549. + /* Read GINTSTS */
  38550. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38551. +}
  38552. +
  38553. +static void do_in_ack(void)
  38554. +{
  38555. + gintsts_data_t gintsts;
  38556. + hctsiz_data_t hctsiz;
  38557. + hcchar_data_t hcchar;
  38558. + haint_data_t haint;
  38559. + hcint_data_t hcint;
  38560. + host_grxsts_data_t grxsts;
  38561. +
  38562. + /* Enable HAINTs */
  38563. + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0001);
  38564. +
  38565. + /* Enable HCINTs */
  38566. + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x04a3);
  38567. +
  38568. + /* Read GINTSTS */
  38569. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38570. +
  38571. + /* Read HAINT */
  38572. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  38573. +
  38574. + /* Read HCINT */
  38575. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  38576. +
  38577. + /* Read HCCHAR */
  38578. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38579. +
  38580. + /* Clear HCINT */
  38581. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  38582. +
  38583. + /* Clear HAINT */
  38584. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  38585. +
  38586. + /* Clear GINTSTS */
  38587. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  38588. +
  38589. + /* Read GINTSTS */
  38590. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38591. +
  38592. + /*
  38593. + * Receive Control In packet
  38594. + */
  38595. +
  38596. + /* Make sure channel is disabled */
  38597. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38598. + if (hcchar.b.chen) {
  38599. + hcchar.b.chdis = 1;
  38600. + hcchar.b.chen = 1;
  38601. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  38602. + //sleep(1);
  38603. + dwc_mdelay(1000);
  38604. +
  38605. + /* Read GINTSTS */
  38606. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38607. +
  38608. + /* Read HAINT */
  38609. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  38610. +
  38611. + /* Read HCINT */
  38612. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  38613. +
  38614. + /* Read HCCHAR */
  38615. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38616. +
  38617. + /* Clear HCINT */
  38618. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  38619. +
  38620. + /* Clear HAINT */
  38621. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  38622. +
  38623. + /* Clear GINTSTS */
  38624. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  38625. +
  38626. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38627. + }
  38628. +
  38629. + /* Set HCTSIZ */
  38630. + hctsiz.d32 = 0;
  38631. + hctsiz.b.xfersize = 8;
  38632. + hctsiz.b.pktcnt = 1;
  38633. + hctsiz.b.pid = DWC_OTG_HC_PID_DATA1;
  38634. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  38635. +
  38636. + /* Set HCCHAR */
  38637. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38638. + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
  38639. + hcchar.b.epdir = 1;
  38640. + hcchar.b.epnum = 0;
  38641. + hcchar.b.mps = 8;
  38642. + hcchar.b.chen = 1;
  38643. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  38644. +
  38645. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38646. +
  38647. + /* Wait for receive status queue interrupt */
  38648. + do {
  38649. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38650. + } while (gintsts.b.rxstsqlvl == 0);
  38651. +
  38652. + /* Read RXSTS */
  38653. + grxsts.d32 = DWC_READ_REG32(&global_regs->grxstsp);
  38654. +
  38655. + /* Clear RXSTSQLVL in GINTSTS */
  38656. + gintsts.d32 = 0;
  38657. + gintsts.b.rxstsqlvl = 1;
  38658. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  38659. +
  38660. + switch (grxsts.b.pktsts) {
  38661. + case DWC_GRXSTS_PKTSTS_IN:
  38662. + /* Read the data into the host buffer */
  38663. + if (grxsts.b.bcnt > 0) {
  38664. + int i;
  38665. + int word_count = (grxsts.b.bcnt + 3) / 4;
  38666. +
  38667. + data_fifo = (uint32_t *) ((char *)global_regs + 0x1000);
  38668. +
  38669. + for (i = 0; i < word_count; i++) {
  38670. + (void)DWC_READ_REG32(data_fifo++);
  38671. + }
  38672. + }
  38673. + break;
  38674. +
  38675. + default:
  38676. + break;
  38677. + }
  38678. +
  38679. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38680. +
  38681. + /* Wait for receive status queue interrupt */
  38682. + do {
  38683. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38684. + } while (gintsts.b.rxstsqlvl == 0);
  38685. +
  38686. + /* Read RXSTS */
  38687. + grxsts.d32 = DWC_READ_REG32(&global_regs->grxstsp);
  38688. +
  38689. + /* Clear RXSTSQLVL in GINTSTS */
  38690. + gintsts.d32 = 0;
  38691. + gintsts.b.rxstsqlvl = 1;
  38692. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  38693. +
  38694. + switch (grxsts.b.pktsts) {
  38695. + case DWC_GRXSTS_PKTSTS_IN_XFER_COMP:
  38696. + break;
  38697. +
  38698. + default:
  38699. + break;
  38700. + }
  38701. +
  38702. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38703. +
  38704. + /* Wait for host channel interrupt */
  38705. + do {
  38706. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38707. + } while (gintsts.b.hcintr == 0);
  38708. +
  38709. + /* Read HAINT */
  38710. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  38711. +
  38712. + /* Read HCINT */
  38713. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  38714. +
  38715. + /* Read HCCHAR */
  38716. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38717. +
  38718. + /* Clear HCINT */
  38719. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  38720. +
  38721. + /* Clear HAINT */
  38722. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  38723. +
  38724. + /* Clear GINTSTS */
  38725. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  38726. +
  38727. + /* Read GINTSTS */
  38728. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38729. +
  38730. +// usleep(100000);
  38731. +// mdelay(100);
  38732. + dwc_mdelay(1);
  38733. +
  38734. + /*
  38735. + * Send handshake packet
  38736. + */
  38737. +
  38738. + /* Read HAINT */
  38739. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  38740. +
  38741. + /* Read HCINT */
  38742. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  38743. +
  38744. + /* Read HCCHAR */
  38745. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38746. +
  38747. + /* Clear HCINT */
  38748. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  38749. +
  38750. + /* Clear HAINT */
  38751. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  38752. +
  38753. + /* Clear GINTSTS */
  38754. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  38755. +
  38756. + /* Read GINTSTS */
  38757. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38758. +
  38759. + /* Make sure channel is disabled */
  38760. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38761. + if (hcchar.b.chen) {
  38762. + hcchar.b.chdis = 1;
  38763. + hcchar.b.chen = 1;
  38764. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  38765. + //sleep(1);
  38766. + dwc_mdelay(1000);
  38767. +
  38768. + /* Read GINTSTS */
  38769. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38770. +
  38771. + /* Read HAINT */
  38772. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  38773. +
  38774. + /* Read HCINT */
  38775. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  38776. +
  38777. + /* Read HCCHAR */
  38778. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38779. +
  38780. + /* Clear HCINT */
  38781. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  38782. +
  38783. + /* Clear HAINT */
  38784. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  38785. +
  38786. + /* Clear GINTSTS */
  38787. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  38788. +
  38789. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38790. + }
  38791. +
  38792. + /* Set HCTSIZ */
  38793. + hctsiz.d32 = 0;
  38794. + hctsiz.b.xfersize = 0;
  38795. + hctsiz.b.pktcnt = 1;
  38796. + hctsiz.b.pid = DWC_OTG_HC_PID_DATA1;
  38797. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  38798. +
  38799. + /* Set HCCHAR */
  38800. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38801. + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
  38802. + hcchar.b.epdir = 0;
  38803. + hcchar.b.epnum = 0;
  38804. + hcchar.b.mps = 8;
  38805. + hcchar.b.chen = 1;
  38806. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  38807. +
  38808. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38809. +
  38810. + /* Wait for host channel interrupt */
  38811. + do {
  38812. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38813. + } while (gintsts.b.hcintr == 0);
  38814. +
  38815. + /* Disable HCINTs */
  38816. + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x0000);
  38817. +
  38818. + /* Disable HAINTs */
  38819. + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0000);
  38820. +
  38821. + /* Read HAINT */
  38822. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  38823. +
  38824. + /* Read HCINT */
  38825. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  38826. +
  38827. + /* Read HCCHAR */
  38828. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38829. +
  38830. + /* Clear HCINT */
  38831. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  38832. +
  38833. + /* Clear HAINT */
  38834. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  38835. +
  38836. + /* Clear GINTSTS */
  38837. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  38838. +
  38839. + /* Read GINTSTS */
  38840. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38841. +}
  38842. +#endif
  38843. +
  38844. +/** Handles hub class-specific requests. */
  38845. +int dwc_otg_hcd_hub_control(dwc_otg_hcd_t * dwc_otg_hcd,
  38846. + uint16_t typeReq,
  38847. + uint16_t wValue,
  38848. + uint16_t wIndex, uint8_t * buf, uint16_t wLength)
  38849. +{
  38850. + int retval = 0;
  38851. +
  38852. + dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
  38853. + usb_hub_descriptor_t *hub_desc;
  38854. + hprt0_data_t hprt0 = {.d32 = 0 };
  38855. +
  38856. + uint32_t port_status;
  38857. +
  38858. + switch (typeReq) {
  38859. + case UCR_CLEAR_HUB_FEATURE:
  38860. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  38861. + "ClearHubFeature 0x%x\n", wValue);
  38862. + switch (wValue) {
  38863. + case UHF_C_HUB_LOCAL_POWER:
  38864. + case UHF_C_HUB_OVER_CURRENT:
  38865. + /* Nothing required here */
  38866. + break;
  38867. + default:
  38868. + retval = -DWC_E_INVALID;
  38869. + DWC_ERROR("DWC OTG HCD - "
  38870. + "ClearHubFeature request %xh unknown\n",
  38871. + wValue);
  38872. + }
  38873. + break;
  38874. + case UCR_CLEAR_PORT_FEATURE:
  38875. +#ifdef CONFIG_USB_DWC_OTG_LPM
  38876. + if (wValue != UHF_PORT_L1)
  38877. +#endif
  38878. + if (!wIndex || wIndex > 1)
  38879. + goto error;
  38880. +
  38881. + switch (wValue) {
  38882. + case UHF_PORT_ENABLE:
  38883. + DWC_DEBUGPL(DBG_ANY, "DWC OTG HCD HUB CONTROL - "
  38884. + "ClearPortFeature USB_PORT_FEAT_ENABLE\n");
  38885. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  38886. + hprt0.b.prtena = 1;
  38887. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  38888. + break;
  38889. + case UHF_PORT_SUSPEND:
  38890. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  38891. + "ClearPortFeature USB_PORT_FEAT_SUSPEND\n");
  38892. +
  38893. + if (core_if->power_down == 2) {
  38894. + dwc_otg_host_hibernation_restore(core_if, 0, 0);
  38895. + } else {
  38896. + DWC_WRITE_REG32(core_if->pcgcctl, 0);
  38897. + dwc_mdelay(5);
  38898. +
  38899. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  38900. + hprt0.b.prtres = 1;
  38901. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  38902. + hprt0.b.prtsusp = 0;
  38903. + /* Clear Resume bit */
  38904. + dwc_mdelay(100);
  38905. + hprt0.b.prtres = 0;
  38906. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  38907. + }
  38908. + break;
  38909. +#ifdef CONFIG_USB_DWC_OTG_LPM
  38910. + case UHF_PORT_L1:
  38911. + {
  38912. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  38913. + glpmcfg_data_t lpmcfg = {.d32 = 0 };
  38914. +
  38915. + lpmcfg.d32 =
  38916. + DWC_READ_REG32(&core_if->
  38917. + core_global_regs->glpmcfg);
  38918. + lpmcfg.b.en_utmi_sleep = 0;
  38919. + lpmcfg.b.hird_thres &= (~(1 << 4));
  38920. + lpmcfg.b.prt_sleep_sts = 1;
  38921. + DWC_WRITE_REG32(&core_if->
  38922. + core_global_regs->glpmcfg,
  38923. + lpmcfg.d32);
  38924. +
  38925. + /* Clear Enbl_L1Gating bit. */
  38926. + pcgcctl.b.enbl_sleep_gating = 1;
  38927. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32,
  38928. + 0);
  38929. +
  38930. + dwc_mdelay(5);
  38931. +
  38932. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  38933. + hprt0.b.prtres = 1;
  38934. + DWC_WRITE_REG32(core_if->host_if->hprt0,
  38935. + hprt0.d32);
  38936. + /* This bit will be cleared in wakeup interrupt handle */
  38937. + break;
  38938. + }
  38939. +#endif
  38940. + case UHF_PORT_POWER:
  38941. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  38942. + "ClearPortFeature USB_PORT_FEAT_POWER\n");
  38943. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  38944. + hprt0.b.prtpwr = 0;
  38945. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  38946. + break;
  38947. + case UHF_PORT_INDICATOR:
  38948. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  38949. + "ClearPortFeature USB_PORT_FEAT_INDICATOR\n");
  38950. + /* Port inidicator not supported */
  38951. + break;
  38952. + case UHF_C_PORT_CONNECTION:
  38953. + /* Clears drivers internal connect status change
  38954. + * flag */
  38955. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  38956. + "ClearPortFeature USB_PORT_FEAT_C_CONNECTION\n");
  38957. + dwc_otg_hcd->flags.b.port_connect_status_change = 0;
  38958. + break;
  38959. + case UHF_C_PORT_RESET:
  38960. + /* Clears the driver's internal Port Reset Change
  38961. + * flag */
  38962. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  38963. + "ClearPortFeature USB_PORT_FEAT_C_RESET\n");
  38964. + dwc_otg_hcd->flags.b.port_reset_change = 0;
  38965. + break;
  38966. + case UHF_C_PORT_ENABLE:
  38967. + /* Clears the driver's internal Port
  38968. + * Enable/Disable Change flag */
  38969. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  38970. + "ClearPortFeature USB_PORT_FEAT_C_ENABLE\n");
  38971. + dwc_otg_hcd->flags.b.port_enable_change = 0;
  38972. + break;
  38973. + case UHF_C_PORT_SUSPEND:
  38974. + /* Clears the driver's internal Port Suspend
  38975. + * Change flag, which is set when resume signaling on
  38976. + * the host port is complete */
  38977. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  38978. + "ClearPortFeature USB_PORT_FEAT_C_SUSPEND\n");
  38979. + dwc_otg_hcd->flags.b.port_suspend_change = 0;
  38980. + break;
  38981. +#ifdef CONFIG_USB_DWC_OTG_LPM
  38982. + case UHF_C_PORT_L1:
  38983. + dwc_otg_hcd->flags.b.port_l1_change = 0;
  38984. + break;
  38985. +#endif
  38986. + case UHF_C_PORT_OVER_CURRENT:
  38987. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  38988. + "ClearPortFeature USB_PORT_FEAT_C_OVER_CURRENT\n");
  38989. + dwc_otg_hcd->flags.b.port_over_current_change = 0;
  38990. + break;
  38991. + default:
  38992. + retval = -DWC_E_INVALID;
  38993. + DWC_ERROR("DWC OTG HCD - "
  38994. + "ClearPortFeature request %xh "
  38995. + "unknown or unsupported\n", wValue);
  38996. + }
  38997. + break;
  38998. + case UCR_GET_HUB_DESCRIPTOR:
  38999. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  39000. + "GetHubDescriptor\n");
  39001. + hub_desc = (usb_hub_descriptor_t *) buf;
  39002. + hub_desc->bDescLength = 9;
  39003. + hub_desc->bDescriptorType = 0x29;
  39004. + hub_desc->bNbrPorts = 1;
  39005. + USETW(hub_desc->wHubCharacteristics, 0x08);
  39006. + hub_desc->bPwrOn2PwrGood = 1;
  39007. + hub_desc->bHubContrCurrent = 0;
  39008. + hub_desc->DeviceRemovable[0] = 0;
  39009. + hub_desc->DeviceRemovable[1] = 0xff;
  39010. + break;
  39011. + case UCR_GET_HUB_STATUS:
  39012. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  39013. + "GetHubStatus\n");
  39014. + DWC_MEMSET(buf, 0, 4);
  39015. + break;
  39016. + case UCR_GET_PORT_STATUS:
  39017. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  39018. + "GetPortStatus wIndex = 0x%04x FLAGS=0x%08x\n",
  39019. + wIndex, dwc_otg_hcd->flags.d32);
  39020. + if (!wIndex || wIndex > 1)
  39021. + goto error;
  39022. +
  39023. + port_status = 0;
  39024. +
  39025. + if (dwc_otg_hcd->flags.b.port_connect_status_change)
  39026. + port_status |= (1 << UHF_C_PORT_CONNECTION);
  39027. +
  39028. + if (dwc_otg_hcd->flags.b.port_enable_change)
  39029. + port_status |= (1 << UHF_C_PORT_ENABLE);
  39030. +
  39031. + if (dwc_otg_hcd->flags.b.port_suspend_change)
  39032. + port_status |= (1 << UHF_C_PORT_SUSPEND);
  39033. +
  39034. + if (dwc_otg_hcd->flags.b.port_l1_change)
  39035. + port_status |= (1 << UHF_C_PORT_L1);
  39036. +
  39037. + if (dwc_otg_hcd->flags.b.port_reset_change) {
  39038. + port_status |= (1 << UHF_C_PORT_RESET);
  39039. + }
  39040. +
  39041. + if (dwc_otg_hcd->flags.b.port_over_current_change) {
  39042. + DWC_WARN("Overcurrent change detected\n");
  39043. + port_status |= (1 << UHF_C_PORT_OVER_CURRENT);
  39044. + }
  39045. +
  39046. + if (!dwc_otg_hcd->flags.b.port_connect_status) {
  39047. + /*
  39048. + * The port is disconnected, which means the core is
  39049. + * either in device mode or it soon will be. Just
  39050. + * return 0's for the remainder of the port status
  39051. + * since the port register can't be read if the core
  39052. + * is in device mode.
  39053. + */
  39054. + *((__le32 *) buf) = dwc_cpu_to_le32(&port_status);
  39055. + break;
  39056. + }
  39057. +
  39058. + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
  39059. + DWC_DEBUGPL(DBG_HCDV, " HPRT0: 0x%08x\n", hprt0.d32);
  39060. +
  39061. + if (hprt0.b.prtconnsts)
  39062. + port_status |= (1 << UHF_PORT_CONNECTION);
  39063. +
  39064. + if (hprt0.b.prtena)
  39065. + port_status |= (1 << UHF_PORT_ENABLE);
  39066. +
  39067. + if (hprt0.b.prtsusp)
  39068. + port_status |= (1 << UHF_PORT_SUSPEND);
  39069. +
  39070. + if (hprt0.b.prtovrcurract)
  39071. + port_status |= (1 << UHF_PORT_OVER_CURRENT);
  39072. +
  39073. + if (hprt0.b.prtrst)
  39074. + port_status |= (1 << UHF_PORT_RESET);
  39075. +
  39076. + if (hprt0.b.prtpwr)
  39077. + port_status |= (1 << UHF_PORT_POWER);
  39078. +
  39079. + if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_HIGH_SPEED)
  39080. + port_status |= (1 << UHF_PORT_HIGH_SPEED);
  39081. + else if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_LOW_SPEED)
  39082. + port_status |= (1 << UHF_PORT_LOW_SPEED);
  39083. +
  39084. + if (hprt0.b.prttstctl)
  39085. + port_status |= (1 << UHF_PORT_TEST);
  39086. + if (dwc_otg_get_lpm_portsleepstatus(dwc_otg_hcd->core_if)) {
  39087. + port_status |= (1 << UHF_PORT_L1);
  39088. + }
  39089. + /*
  39090. + For Synopsys HW emulation of Power down wkup_control asserts the
  39091. + hreset_n and prst_n on suspned. This causes the HPRT0 to be zero.
  39092. + We intentionally tell the software that port is in L2Suspend state.
  39093. + Only for STE.
  39094. + */
  39095. + if ((core_if->power_down == 2)
  39096. + && (core_if->hibernation_suspend == 1)) {
  39097. + port_status |= (1 << UHF_PORT_SUSPEND);
  39098. + }
  39099. + /* USB_PORT_FEAT_INDICATOR unsupported always 0 */
  39100. +
  39101. + *((__le32 *) buf) = dwc_cpu_to_le32(&port_status);
  39102. +
  39103. + break;
  39104. + case UCR_SET_HUB_FEATURE:
  39105. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  39106. + "SetHubFeature\n");
  39107. + /* No HUB features supported */
  39108. + break;
  39109. + case UCR_SET_PORT_FEATURE:
  39110. + if (wValue != UHF_PORT_TEST && (!wIndex || wIndex > 1))
  39111. + goto error;
  39112. +
  39113. + if (!dwc_otg_hcd->flags.b.port_connect_status) {
  39114. + /*
  39115. + * The port is disconnected, which means the core is
  39116. + * either in device mode or it soon will be. Just
  39117. + * return without doing anything since the port
  39118. + * register can't be written if the core is in device
  39119. + * mode.
  39120. + */
  39121. + break;
  39122. + }
  39123. +
  39124. + switch (wValue) {
  39125. + case UHF_PORT_SUSPEND:
  39126. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  39127. + "SetPortFeature - USB_PORT_FEAT_SUSPEND\n");
  39128. + if (dwc_otg_hcd_otg_port(dwc_otg_hcd) != wIndex) {
  39129. + goto error;
  39130. + }
  39131. + if (core_if->power_down == 2) {
  39132. + int timeout = 300;
  39133. + dwc_irqflags_t flags;
  39134. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  39135. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  39136. + gusbcfg_data_t gusbcfg = {.d32 = 0 };
  39137. +#ifdef DWC_DEV_SRPCAP
  39138. + int32_t otg_cap_param = core_if->core_params->otg_cap;
  39139. +#endif
  39140. + DWC_PRINTF("Preparing for complete power-off\n");
  39141. +
  39142. + /* Save registers before hibernation */
  39143. + dwc_otg_save_global_regs(core_if);
  39144. + dwc_otg_save_host_regs(core_if);
  39145. +
  39146. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  39147. + hprt0.b.prtsusp = 1;
  39148. + hprt0.b.prtena = 0;
  39149. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  39150. + /* Spin hprt0.b.prtsusp to became 1 */
  39151. + do {
  39152. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  39153. + if (hprt0.b.prtsusp) {
  39154. + break;
  39155. + }
  39156. + dwc_mdelay(1);
  39157. + } while (--timeout);
  39158. + if (!timeout) {
  39159. + DWC_WARN("Suspend wasn't genereted\n");
  39160. + }
  39161. + dwc_udelay(10);
  39162. +
  39163. + /*
  39164. + * We need to disable interrupts to prevent servicing of any IRQ
  39165. + * during going to hibernation
  39166. + */
  39167. + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
  39168. + core_if->lx_state = DWC_OTG_L2;
  39169. +#ifdef DWC_DEV_SRPCAP
  39170. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  39171. + hprt0.b.prtpwr = 0;
  39172. + hprt0.b.prtena = 0;
  39173. + DWC_WRITE_REG32(core_if->host_if->hprt0,
  39174. + hprt0.d32);
  39175. +#endif
  39176. + gusbcfg.d32 =
  39177. + DWC_READ_REG32(&core_if->core_global_regs->
  39178. + gusbcfg);
  39179. + if (gusbcfg.b.ulpi_utmi_sel == 1) {
  39180. + /* ULPI interface */
  39181. + /* Suspend the Phy Clock */
  39182. + pcgcctl.d32 = 0;
  39183. + pcgcctl.b.stoppclk = 1;
  39184. + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
  39185. + pcgcctl.d32);
  39186. + dwc_udelay(10);
  39187. + gpwrdn.b.pmuactv = 1;
  39188. + DWC_MODIFY_REG32(&core_if->
  39189. + core_global_regs->
  39190. + gpwrdn, 0, gpwrdn.d32);
  39191. + } else {
  39192. + /* UTMI+ Interface */
  39193. + gpwrdn.b.pmuactv = 1;
  39194. + DWC_MODIFY_REG32(&core_if->
  39195. + core_global_regs->
  39196. + gpwrdn, 0, gpwrdn.d32);
  39197. + dwc_udelay(10);
  39198. + pcgcctl.b.stoppclk = 1;
  39199. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  39200. + dwc_udelay(10);
  39201. + }
  39202. +#ifdef DWC_DEV_SRPCAP
  39203. + gpwrdn.d32 = 0;
  39204. + gpwrdn.b.dis_vbus = 1;
  39205. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  39206. + gpwrdn, 0, gpwrdn.d32);
  39207. +#endif
  39208. + gpwrdn.d32 = 0;
  39209. + gpwrdn.b.pmuintsel = 1;
  39210. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  39211. + gpwrdn, 0, gpwrdn.d32);
  39212. + dwc_udelay(10);
  39213. +
  39214. + gpwrdn.d32 = 0;
  39215. +#ifdef DWC_DEV_SRPCAP
  39216. + gpwrdn.b.srp_det_msk = 1;
  39217. +#endif
  39218. + gpwrdn.b.disconn_det_msk = 1;
  39219. + gpwrdn.b.lnstchng_msk = 1;
  39220. + gpwrdn.b.sts_chngint_msk = 1;
  39221. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  39222. + gpwrdn, 0, gpwrdn.d32);
  39223. + dwc_udelay(10);
  39224. +
  39225. + /* Enable Power Down Clamp and all interrupts in GPWRDN */
  39226. + gpwrdn.d32 = 0;
  39227. + gpwrdn.b.pwrdnclmp = 1;
  39228. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  39229. + gpwrdn, 0, gpwrdn.d32);
  39230. + dwc_udelay(10);
  39231. +
  39232. + /* Switch off VDD */
  39233. + gpwrdn.d32 = 0;
  39234. + gpwrdn.b.pwrdnswtch = 1;
  39235. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  39236. + gpwrdn, 0, gpwrdn.d32);
  39237. +
  39238. +#ifdef DWC_DEV_SRPCAP
  39239. + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE)
  39240. + {
  39241. + core_if->pwron_timer_started = 1;
  39242. + DWC_TIMER_SCHEDULE(core_if->pwron_timer, 6000 /* 6 secs */ );
  39243. + }
  39244. +#endif
  39245. + /* Save gpwrdn register for further usage if stschng interrupt */
  39246. + core_if->gr_backup->gpwrdn_local =
  39247. + DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  39248. +
  39249. + /* Set flag to indicate that we are in hibernation */
  39250. + core_if->hibernation_suspend = 1;
  39251. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock,flags);
  39252. +
  39253. + DWC_PRINTF("Host hibernation completed\n");
  39254. + // Exit from case statement
  39255. + break;
  39256. +
  39257. + }
  39258. + if (dwc_otg_hcd_otg_port(dwc_otg_hcd) == wIndex &&
  39259. + dwc_otg_hcd->fops->get_b_hnp_enable(dwc_otg_hcd)) {
  39260. + gotgctl_data_t gotgctl = {.d32 = 0 };
  39261. + gotgctl.b.hstsethnpen = 1;
  39262. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  39263. + gotgctl, 0, gotgctl.d32);
  39264. + core_if->op_state = A_SUSPEND;
  39265. + }
  39266. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  39267. + hprt0.b.prtsusp = 1;
  39268. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  39269. + {
  39270. + dwc_irqflags_t flags;
  39271. + /* Update lx_state */
  39272. + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
  39273. + core_if->lx_state = DWC_OTG_L2;
  39274. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
  39275. + }
  39276. + /* Suspend the Phy Clock */
  39277. + {
  39278. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  39279. + pcgcctl.b.stoppclk = 1;
  39280. + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
  39281. + pcgcctl.d32);
  39282. + dwc_udelay(10);
  39283. + }
  39284. +
  39285. + /* For HNP the bus must be suspended for at least 200ms. */
  39286. + if (dwc_otg_hcd->fops->get_b_hnp_enable(dwc_otg_hcd)) {
  39287. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  39288. + pcgcctl.b.stoppclk = 1;
  39289. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  39290. + dwc_mdelay(200);
  39291. + }
  39292. +
  39293. + /** @todo - check how sw can wait for 1 sec to check asesvld??? */
  39294. +#if 0 //vahrama !!!!!!!!!!!!!!!!!!
  39295. + if (core_if->adp_enable) {
  39296. + gotgctl_data_t gotgctl = {.d32 = 0 };
  39297. + gpwrdn_data_t gpwrdn;
  39298. +
  39299. + while (gotgctl.b.asesvld == 1) {
  39300. + gotgctl.d32 =
  39301. + DWC_READ_REG32(&core_if->
  39302. + core_global_regs->
  39303. + gotgctl);
  39304. + dwc_mdelay(100);
  39305. + }
  39306. +
  39307. + /* Enable Power Down Logic */
  39308. + gpwrdn.d32 = 0;
  39309. + gpwrdn.b.pmuactv = 1;
  39310. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  39311. + gpwrdn, 0, gpwrdn.d32);
  39312. +
  39313. + /* Unmask SRP detected interrupt from Power Down Logic */
  39314. + gpwrdn.d32 = 0;
  39315. + gpwrdn.b.srp_det_msk = 1;
  39316. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  39317. + gpwrdn, 0, gpwrdn.d32);
  39318. +
  39319. + dwc_otg_adp_probe_start(core_if);
  39320. + }
  39321. +#endif
  39322. + break;
  39323. + case UHF_PORT_POWER:
  39324. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  39325. + "SetPortFeature - USB_PORT_FEAT_POWER\n");
  39326. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  39327. + hprt0.b.prtpwr = 1;
  39328. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  39329. + break;
  39330. + case UHF_PORT_RESET:
  39331. + if ((core_if->power_down == 2)
  39332. + && (core_if->hibernation_suspend == 1)) {
  39333. + /* If we are going to exit from Hibernated
  39334. + * state via USB RESET.
  39335. + */
  39336. + dwc_otg_host_hibernation_restore(core_if, 0, 1);
  39337. + } else {
  39338. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  39339. +
  39340. + DWC_DEBUGPL(DBG_HCD,
  39341. + "DWC OTG HCD HUB CONTROL - "
  39342. + "SetPortFeature - USB_PORT_FEAT_RESET\n");
  39343. + {
  39344. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  39345. + pcgcctl.b.enbl_sleep_gating = 1;
  39346. + pcgcctl.b.stoppclk = 1;
  39347. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  39348. + DWC_WRITE_REG32(core_if->pcgcctl, 0);
  39349. + }
  39350. +#ifdef CONFIG_USB_DWC_OTG_LPM
  39351. + {
  39352. + glpmcfg_data_t lpmcfg;
  39353. + lpmcfg.d32 =
  39354. + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  39355. + if (lpmcfg.b.prt_sleep_sts) {
  39356. + lpmcfg.b.en_utmi_sleep = 0;
  39357. + lpmcfg.b.hird_thres &= (~(1 << 4));
  39358. + DWC_WRITE_REG32
  39359. + (&core_if->core_global_regs->glpmcfg,
  39360. + lpmcfg.d32);
  39361. + dwc_mdelay(1);
  39362. + }
  39363. + }
  39364. +#endif
  39365. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  39366. + /* Clear suspend bit if resetting from suspended state. */
  39367. + hprt0.b.prtsusp = 0;
  39368. + /* When B-Host the Port reset bit is set in
  39369. + * the Start HCD Callback function, so that
  39370. + * the reset is started within 1ms of the HNP
  39371. + * success interrupt. */
  39372. + if (!dwc_otg_hcd_is_b_host(dwc_otg_hcd)) {
  39373. + hprt0.b.prtpwr = 1;
  39374. + hprt0.b.prtrst = 1;
  39375. + DWC_PRINTF("Indeed it is in host mode hprt0 = %08x\n",hprt0.d32);
  39376. + DWC_WRITE_REG32(core_if->host_if->hprt0,
  39377. + hprt0.d32);
  39378. + }
  39379. + /* Clear reset bit in 10ms (FS/LS) or 50ms (HS) */
  39380. + dwc_mdelay(60);
  39381. + hprt0.b.prtrst = 0;
  39382. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  39383. + core_if->lx_state = DWC_OTG_L0; /* Now back to the on state */
  39384. + }
  39385. + break;
  39386. +#ifdef DWC_HS_ELECT_TST
  39387. + case UHF_PORT_TEST:
  39388. + {
  39389. + uint32_t t;
  39390. + gintmsk_data_t gintmsk;
  39391. +
  39392. + t = (wIndex >> 8); /* MSB wIndex USB */
  39393. + DWC_DEBUGPL(DBG_HCD,
  39394. + "DWC OTG HCD HUB CONTROL - "
  39395. + "SetPortFeature - USB_PORT_FEAT_TEST %d\n",
  39396. + t);
  39397. + DWC_WARN("USB_PORT_FEAT_TEST %d\n", t);
  39398. + if (t < 6) {
  39399. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  39400. + hprt0.b.prttstctl = t;
  39401. + DWC_WRITE_REG32(core_if->host_if->hprt0,
  39402. + hprt0.d32);
  39403. + } else {
  39404. + /* Setup global vars with reg addresses (quick and
  39405. + * dirty hack, should be cleaned up)
  39406. + */
  39407. + global_regs = core_if->core_global_regs;
  39408. + hc_global_regs =
  39409. + core_if->host_if->host_global_regs;
  39410. + hc_regs =
  39411. + (dwc_otg_hc_regs_t *) ((char *)
  39412. + global_regs +
  39413. + 0x500);
  39414. + data_fifo =
  39415. + (uint32_t *) ((char *)global_regs +
  39416. + 0x1000);
  39417. +
  39418. + if (t == 6) { /* HS_HOST_PORT_SUSPEND_RESUME */
  39419. + /* Save current interrupt mask */
  39420. + gintmsk.d32 =
  39421. + DWC_READ_REG32
  39422. + (&global_regs->gintmsk);
  39423. +
  39424. + /* Disable all interrupts while we muck with
  39425. + * the hardware directly
  39426. + */
  39427. + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
  39428. +
  39429. + /* 15 second delay per the test spec */
  39430. + dwc_mdelay(15000);
  39431. +
  39432. + /* Drive suspend on the root port */
  39433. + hprt0.d32 =
  39434. + dwc_otg_read_hprt0(core_if);
  39435. + hprt0.b.prtsusp = 1;
  39436. + hprt0.b.prtres = 0;
  39437. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  39438. +
  39439. + /* 15 second delay per the test spec */
  39440. + dwc_mdelay(15000);
  39441. +
  39442. + /* Drive resume on the root port */
  39443. + hprt0.d32 =
  39444. + dwc_otg_read_hprt0(core_if);
  39445. + hprt0.b.prtsusp = 0;
  39446. + hprt0.b.prtres = 1;
  39447. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  39448. + dwc_mdelay(100);
  39449. +
  39450. + /* Clear the resume bit */
  39451. + hprt0.b.prtres = 0;
  39452. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  39453. +
  39454. + /* Restore interrupts */
  39455. + DWC_WRITE_REG32(&global_regs->gintmsk, gintmsk.d32);
  39456. + } else if (t == 7) { /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR setup */
  39457. + /* Save current interrupt mask */
  39458. + gintmsk.d32 =
  39459. + DWC_READ_REG32
  39460. + (&global_regs->gintmsk);
  39461. +
  39462. + /* Disable all interrupts while we muck with
  39463. + * the hardware directly
  39464. + */
  39465. + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
  39466. +
  39467. + /* 15 second delay per the test spec */
  39468. + dwc_mdelay(15000);
  39469. +
  39470. + /* Send the Setup packet */
  39471. + do_setup();
  39472. +
  39473. + /* 15 second delay so nothing else happens for awhile */
  39474. + dwc_mdelay(15000);
  39475. +
  39476. + /* Restore interrupts */
  39477. + DWC_WRITE_REG32(&global_regs->gintmsk, gintmsk.d32);
  39478. + } else if (t == 8) { /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR execute */
  39479. + /* Save current interrupt mask */
  39480. + gintmsk.d32 =
  39481. + DWC_READ_REG32
  39482. + (&global_regs->gintmsk);
  39483. +
  39484. + /* Disable all interrupts while we muck with
  39485. + * the hardware directly
  39486. + */
  39487. + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
  39488. +
  39489. + /* Send the Setup packet */
  39490. + do_setup();
  39491. +
  39492. + /* 15 second delay so nothing else happens for awhile */
  39493. + dwc_mdelay(15000);
  39494. +
  39495. + /* Send the In and Ack packets */
  39496. + do_in_ack();
  39497. +
  39498. + /* 15 second delay so nothing else happens for awhile */
  39499. + dwc_mdelay(15000);
  39500. +
  39501. + /* Restore interrupts */
  39502. + DWC_WRITE_REG32(&global_regs->gintmsk, gintmsk.d32);
  39503. + }
  39504. + }
  39505. + break;
  39506. + }
  39507. +#endif /* DWC_HS_ELECT_TST */
  39508. +
  39509. + case UHF_PORT_INDICATOR:
  39510. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  39511. + "SetPortFeature - USB_PORT_FEAT_INDICATOR\n");
  39512. + /* Not supported */
  39513. + break;
  39514. + default:
  39515. + retval = -DWC_E_INVALID;
  39516. + DWC_ERROR("DWC OTG HCD - "
  39517. + "SetPortFeature request %xh "
  39518. + "unknown or unsupported\n", wValue);
  39519. + break;
  39520. + }
  39521. + break;
  39522. +#ifdef CONFIG_USB_DWC_OTG_LPM
  39523. + case UCR_SET_AND_TEST_PORT_FEATURE:
  39524. + if (wValue != UHF_PORT_L1) {
  39525. + goto error;
  39526. + }
  39527. + {
  39528. + int portnum, hird, devaddr, remwake;
  39529. + glpmcfg_data_t lpmcfg;
  39530. + uint32_t time_usecs;
  39531. + gintsts_data_t gintsts;
  39532. + gintmsk_data_t gintmsk;
  39533. +
  39534. + if (!dwc_otg_get_param_lpm_enable(core_if)) {
  39535. + goto error;
  39536. + }
  39537. + if (wValue != UHF_PORT_L1 || wLength != 1) {
  39538. + goto error;
  39539. + }
  39540. + /* Check if the port currently is in SLEEP state */
  39541. + lpmcfg.d32 =
  39542. + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  39543. + if (lpmcfg.b.prt_sleep_sts) {
  39544. + DWC_INFO("Port is already in sleep mode\n");
  39545. + buf[0] = 0; /* Return success */
  39546. + break;
  39547. + }
  39548. +
  39549. + portnum = wIndex & 0xf;
  39550. + hird = (wIndex >> 4) & 0xf;
  39551. + devaddr = (wIndex >> 8) & 0x7f;
  39552. + remwake = (wIndex >> 15);
  39553. +
  39554. + if (portnum != 1) {
  39555. + retval = -DWC_E_INVALID;
  39556. + DWC_WARN
  39557. + ("Wrong port number(%d) in SetandTestPortFeature request\n",
  39558. + portnum);
  39559. + break;
  39560. + }
  39561. +
  39562. + DWC_PRINTF
  39563. + ("SetandTestPortFeature request: portnum = %d, hird = %d, devaddr = %d, rewake = %d\n",
  39564. + portnum, hird, devaddr, remwake);
  39565. + /* Disable LPM interrupt */
  39566. + gintmsk.d32 = 0;
  39567. + gintmsk.b.lpmtranrcvd = 1;
  39568. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
  39569. + gintmsk.d32, 0);
  39570. +
  39571. + if (dwc_otg_hcd_send_lpm
  39572. + (dwc_otg_hcd, devaddr, hird, remwake)) {
  39573. + retval = -DWC_E_INVALID;
  39574. + break;
  39575. + }
  39576. +
  39577. + time_usecs = 10 * (lpmcfg.b.retry_count + 1);
  39578. + /* We will consider timeout if time_usecs microseconds pass,
  39579. + * and we don't receive LPM transaction status.
  39580. + * After receiving non-error responce(ACK/NYET/STALL) from device,
  39581. + * core will set lpmtranrcvd bit.
  39582. + */
  39583. + do {
  39584. + gintsts.d32 =
  39585. + DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  39586. + if (gintsts.b.lpmtranrcvd) {
  39587. + break;
  39588. + }
  39589. + dwc_udelay(1);
  39590. + } while (--time_usecs);
  39591. + /* lpm_int bit will be cleared in LPM interrupt handler */
  39592. +
  39593. + /* Now fill status
  39594. + * 0x00 - Success
  39595. + * 0x10 - NYET
  39596. + * 0x11 - Timeout
  39597. + */
  39598. + if (!gintsts.b.lpmtranrcvd) {
  39599. + buf[0] = 0x3; /* Completion code is Timeout */
  39600. + dwc_otg_hcd_free_hc_from_lpm(dwc_otg_hcd);
  39601. + } else {
  39602. + lpmcfg.d32 =
  39603. + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  39604. + if (lpmcfg.b.lpm_resp == 0x3) {
  39605. + /* ACK responce from the device */
  39606. + buf[0] = 0x00; /* Success */
  39607. + } else if (lpmcfg.b.lpm_resp == 0x2) {
  39608. + /* NYET responce from the device */
  39609. + buf[0] = 0x2;
  39610. + } else {
  39611. + /* Otherwise responce with Timeout */
  39612. + buf[0] = 0x3;
  39613. + }
  39614. + }
  39615. + DWC_PRINTF("Device responce to LPM trans is %x\n",
  39616. + lpmcfg.b.lpm_resp);
  39617. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0,
  39618. + gintmsk.d32);
  39619. +
  39620. + break;
  39621. + }
  39622. +#endif /* CONFIG_USB_DWC_OTG_LPM */
  39623. + default:
  39624. +error:
  39625. + retval = -DWC_E_INVALID;
  39626. + DWC_WARN("DWC OTG HCD - "
  39627. + "Unknown hub control request type or invalid typeReq: %xh wIndex: %xh wValue: %xh\n",
  39628. + typeReq, wIndex, wValue);
  39629. + break;
  39630. + }
  39631. +
  39632. + return retval;
  39633. +}
  39634. +
  39635. +#ifdef CONFIG_USB_DWC_OTG_LPM
  39636. +/** Returns index of host channel to perform LPM transaction. */
  39637. +int dwc_otg_hcd_get_hc_for_lpm_tran(dwc_otg_hcd_t * hcd, uint8_t devaddr)
  39638. +{
  39639. + dwc_otg_core_if_t *core_if = hcd->core_if;
  39640. + dwc_hc_t *hc;
  39641. + hcchar_data_t hcchar;
  39642. + gintmsk_data_t gintmsk = {.d32 = 0 };
  39643. +
  39644. + if (DWC_CIRCLEQ_EMPTY(&hcd->free_hc_list)) {
  39645. + DWC_PRINTF("No free channel to select for LPM transaction\n");
  39646. + return -1;
  39647. + }
  39648. +
  39649. + hc = DWC_CIRCLEQ_FIRST(&hcd->free_hc_list);
  39650. +
  39651. + /* Mask host channel interrupts. */
  39652. + gintmsk.b.hcintr = 1;
  39653. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32, 0);
  39654. +
  39655. + /* Fill fields that core needs for LPM transaction */
  39656. + hcchar.b.devaddr = devaddr;
  39657. + hcchar.b.epnum = 0;
  39658. + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
  39659. + hcchar.b.mps = 64;
  39660. + hcchar.b.lspddev = (hc->speed == DWC_OTG_EP_SPEED_LOW);
  39661. + hcchar.b.epdir = 0; /* OUT */
  39662. + DWC_WRITE_REG32(&core_if->host_if->hc_regs[hc->hc_num]->hcchar,
  39663. + hcchar.d32);
  39664. +
  39665. + /* Remove the host channel from the free list. */
  39666. + DWC_CIRCLEQ_REMOVE_INIT(&hcd->free_hc_list, hc, hc_list_entry);
  39667. +
  39668. + DWC_PRINTF("hcnum = %d devaddr = %d\n", hc->hc_num, devaddr);
  39669. +
  39670. + return hc->hc_num;
  39671. +}
  39672. +
  39673. +/** Release hc after performing LPM transaction */
  39674. +void dwc_otg_hcd_free_hc_from_lpm(dwc_otg_hcd_t * hcd)
  39675. +{
  39676. + dwc_hc_t *hc;
  39677. + glpmcfg_data_t lpmcfg;
  39678. + uint8_t hc_num;
  39679. +
  39680. + lpmcfg.d32 = DWC_READ_REG32(&hcd->core_if->core_global_regs->glpmcfg);
  39681. + hc_num = lpmcfg.b.lpm_chan_index;
  39682. +
  39683. + hc = hcd->hc_ptr_array[hc_num];
  39684. +
  39685. + DWC_PRINTF("Freeing channel %d after LPM\n", hc_num);
  39686. + /* Return host channel to free list */
  39687. + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, hc, hc_list_entry);
  39688. +}
  39689. +
  39690. +int dwc_otg_hcd_send_lpm(dwc_otg_hcd_t * hcd, uint8_t devaddr, uint8_t hird,
  39691. + uint8_t bRemoteWake)
  39692. +{
  39693. + glpmcfg_data_t lpmcfg;
  39694. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  39695. + int channel;
  39696. +
  39697. + channel = dwc_otg_hcd_get_hc_for_lpm_tran(hcd, devaddr);
  39698. + if (channel < 0) {
  39699. + return channel;
  39700. + }
  39701. +
  39702. + pcgcctl.b.enbl_sleep_gating = 1;
  39703. + DWC_MODIFY_REG32(hcd->core_if->pcgcctl, 0, pcgcctl.d32);
  39704. +
  39705. + /* Read LPM config register */
  39706. + lpmcfg.d32 = DWC_READ_REG32(&hcd->core_if->core_global_regs->glpmcfg);
  39707. +
  39708. + /* Program LPM transaction fields */
  39709. + lpmcfg.b.rem_wkup_en = bRemoteWake;
  39710. + lpmcfg.b.hird = hird;
  39711. + lpmcfg.b.hird_thres = 0x1c;
  39712. + lpmcfg.b.lpm_chan_index = channel;
  39713. + lpmcfg.b.en_utmi_sleep = 1;
  39714. + /* Program LPM config register */
  39715. + DWC_WRITE_REG32(&hcd->core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  39716. +
  39717. + /* Send LPM transaction */
  39718. + lpmcfg.b.send_lpm = 1;
  39719. + DWC_WRITE_REG32(&hcd->core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  39720. +
  39721. + return 0;
  39722. +}
  39723. +
  39724. +#endif /* CONFIG_USB_DWC_OTG_LPM */
  39725. +
  39726. +int dwc_otg_hcd_is_status_changed(dwc_otg_hcd_t * hcd, int port)
  39727. +{
  39728. + int retval;
  39729. +
  39730. + if (port != 1) {
  39731. + return -DWC_E_INVALID;
  39732. + }
  39733. +
  39734. + retval = (hcd->flags.b.port_connect_status_change ||
  39735. + hcd->flags.b.port_reset_change ||
  39736. + hcd->flags.b.port_enable_change ||
  39737. + hcd->flags.b.port_suspend_change ||
  39738. + hcd->flags.b.port_over_current_change);
  39739. +#ifdef DEBUG
  39740. + if (retval) {
  39741. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB STATUS DATA:"
  39742. + " Root port status changed\n");
  39743. + DWC_DEBUGPL(DBG_HCDV, " port_connect_status_change: %d\n",
  39744. + hcd->flags.b.port_connect_status_change);
  39745. + DWC_DEBUGPL(DBG_HCDV, " port_reset_change: %d\n",
  39746. + hcd->flags.b.port_reset_change);
  39747. + DWC_DEBUGPL(DBG_HCDV, " port_enable_change: %d\n",
  39748. + hcd->flags.b.port_enable_change);
  39749. + DWC_DEBUGPL(DBG_HCDV, " port_suspend_change: %d\n",
  39750. + hcd->flags.b.port_suspend_change);
  39751. + DWC_DEBUGPL(DBG_HCDV, " port_over_current_change: %d\n",
  39752. + hcd->flags.b.port_over_current_change);
  39753. + }
  39754. +#endif
  39755. + return retval;
  39756. +}
  39757. +
  39758. +int dwc_otg_hcd_get_frame_number(dwc_otg_hcd_t * dwc_otg_hcd)
  39759. +{
  39760. + hfnum_data_t hfnum;
  39761. + hfnum.d32 =
  39762. + DWC_READ_REG32(&dwc_otg_hcd->core_if->host_if->host_global_regs->
  39763. + hfnum);
  39764. +
  39765. +#ifdef DEBUG_SOF
  39766. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD GET FRAME NUMBER %d\n",
  39767. + hfnum.b.frnum);
  39768. +#endif
  39769. + return hfnum.b.frnum;
  39770. +}
  39771. +
  39772. +int dwc_otg_hcd_start(dwc_otg_hcd_t * hcd,
  39773. + struct dwc_otg_hcd_function_ops *fops)
  39774. +{
  39775. + int retval = 0;
  39776. +
  39777. + hcd->fops = fops;
  39778. + if (!dwc_otg_is_device_mode(hcd->core_if) &&
  39779. + (!hcd->core_if->adp_enable || hcd->core_if->adp.adp_started)) {
  39780. + dwc_otg_hcd_reinit(hcd);
  39781. + } else {
  39782. + retval = -DWC_E_NO_DEVICE;
  39783. + }
  39784. +
  39785. + return retval;
  39786. +}
  39787. +
  39788. +void *dwc_otg_hcd_get_priv_data(dwc_otg_hcd_t * hcd)
  39789. +{
  39790. + return hcd->priv;
  39791. +}
  39792. +
  39793. +void dwc_otg_hcd_set_priv_data(dwc_otg_hcd_t * hcd, void *priv_data)
  39794. +{
  39795. + hcd->priv = priv_data;
  39796. +}
  39797. +
  39798. +uint32_t dwc_otg_hcd_otg_port(dwc_otg_hcd_t * hcd)
  39799. +{
  39800. + return hcd->otg_port;
  39801. +}
  39802. +
  39803. +uint32_t dwc_otg_hcd_is_b_host(dwc_otg_hcd_t * hcd)
  39804. +{
  39805. + uint32_t is_b_host;
  39806. + if (hcd->core_if->op_state == B_HOST) {
  39807. + is_b_host = 1;
  39808. + } else {
  39809. + is_b_host = 0;
  39810. + }
  39811. +
  39812. + return is_b_host;
  39813. +}
  39814. +
  39815. +dwc_otg_hcd_urb_t *dwc_otg_hcd_urb_alloc(dwc_otg_hcd_t * hcd,
  39816. + int iso_desc_count, int atomic_alloc)
  39817. +{
  39818. + dwc_otg_hcd_urb_t *dwc_otg_urb;
  39819. + uint32_t size;
  39820. +
  39821. + size =
  39822. + sizeof(*dwc_otg_urb) +
  39823. + iso_desc_count * sizeof(struct dwc_otg_hcd_iso_packet_desc);
  39824. + if (atomic_alloc)
  39825. + dwc_otg_urb = DWC_ALLOC_ATOMIC(size);
  39826. + else
  39827. + dwc_otg_urb = DWC_ALLOC(size);
  39828. +
  39829. + if (dwc_otg_urb)
  39830. + dwc_otg_urb->packet_count = iso_desc_count;
  39831. + else {
  39832. + DWC_ERROR("**** DWC OTG HCD URB alloc - "
  39833. + "%salloc of %db failed\n",
  39834. + atomic_alloc?"atomic ":"", size);
  39835. + }
  39836. + return dwc_otg_urb;
  39837. +}
  39838. +
  39839. +void dwc_otg_hcd_urb_set_pipeinfo(dwc_otg_hcd_urb_t * dwc_otg_urb,
  39840. + uint8_t dev_addr, uint8_t ep_num,
  39841. + uint8_t ep_type, uint8_t ep_dir, uint16_t mps)
  39842. +{
  39843. + dwc_otg_hcd_fill_pipe(&dwc_otg_urb->pipe_info, dev_addr, ep_num,
  39844. + ep_type, ep_dir, mps);
  39845. +#if 0
  39846. + DWC_PRINTF
  39847. + ("addr = %d, ep_num = %d, ep_dir = 0x%x, ep_type = 0x%x, mps = %d\n",
  39848. + dev_addr, ep_num, ep_dir, ep_type, mps);
  39849. +#endif
  39850. +}
  39851. +
  39852. +void dwc_otg_hcd_urb_set_params(dwc_otg_hcd_urb_t * dwc_otg_urb,
  39853. + void *urb_handle, void *buf, dwc_dma_t dma,
  39854. + uint32_t buflen, void *setup_packet,
  39855. + dwc_dma_t setup_dma, uint32_t flags,
  39856. + uint16_t interval)
  39857. +{
  39858. + dwc_otg_urb->priv = urb_handle;
  39859. + dwc_otg_urb->buf = buf;
  39860. + dwc_otg_urb->dma = dma;
  39861. + dwc_otg_urb->length = buflen;
  39862. + dwc_otg_urb->setup_packet = setup_packet;
  39863. + dwc_otg_urb->setup_dma = setup_dma;
  39864. + dwc_otg_urb->flags = flags;
  39865. + dwc_otg_urb->interval = interval;
  39866. + dwc_otg_urb->status = -DWC_E_IN_PROGRESS;
  39867. +}
  39868. +
  39869. +uint32_t dwc_otg_hcd_urb_get_status(dwc_otg_hcd_urb_t * dwc_otg_urb)
  39870. +{
  39871. + return dwc_otg_urb->status;
  39872. +}
  39873. +
  39874. +uint32_t dwc_otg_hcd_urb_get_actual_length(dwc_otg_hcd_urb_t * dwc_otg_urb)
  39875. +{
  39876. + return dwc_otg_urb->actual_length;
  39877. +}
  39878. +
  39879. +uint32_t dwc_otg_hcd_urb_get_error_count(dwc_otg_hcd_urb_t * dwc_otg_urb)
  39880. +{
  39881. + return dwc_otg_urb->error_count;
  39882. +}
  39883. +
  39884. +void dwc_otg_hcd_urb_set_iso_desc_params(dwc_otg_hcd_urb_t * dwc_otg_urb,
  39885. + int desc_num, uint32_t offset,
  39886. + uint32_t length)
  39887. +{
  39888. + dwc_otg_urb->iso_descs[desc_num].offset = offset;
  39889. + dwc_otg_urb->iso_descs[desc_num].length = length;
  39890. +}
  39891. +
  39892. +uint32_t dwc_otg_hcd_urb_get_iso_desc_status(dwc_otg_hcd_urb_t * dwc_otg_urb,
  39893. + int desc_num)
  39894. +{
  39895. + return dwc_otg_urb->iso_descs[desc_num].status;
  39896. +}
  39897. +
  39898. +uint32_t dwc_otg_hcd_urb_get_iso_desc_actual_length(dwc_otg_hcd_urb_t *
  39899. + dwc_otg_urb, int desc_num)
  39900. +{
  39901. + return dwc_otg_urb->iso_descs[desc_num].actual_length;
  39902. +}
  39903. +
  39904. +int dwc_otg_hcd_is_bandwidth_allocated(dwc_otg_hcd_t * hcd, void *ep_handle)
  39905. +{
  39906. + int allocated = 0;
  39907. + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
  39908. +
  39909. + if (qh) {
  39910. + if (!DWC_LIST_EMPTY(&qh->qh_list_entry)) {
  39911. + allocated = 1;
  39912. + }
  39913. + }
  39914. + return allocated;
  39915. +}
  39916. +
  39917. +int dwc_otg_hcd_is_bandwidth_freed(dwc_otg_hcd_t * hcd, void *ep_handle)
  39918. +{
  39919. + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
  39920. + int freed = 0;
  39921. + DWC_ASSERT(qh, "qh is not allocated\n");
  39922. +
  39923. + if (DWC_LIST_EMPTY(&qh->qh_list_entry)) {
  39924. + freed = 1;
  39925. + }
  39926. +
  39927. + return freed;
  39928. +}
  39929. +
  39930. +uint8_t dwc_otg_hcd_get_ep_bandwidth(dwc_otg_hcd_t * hcd, void *ep_handle)
  39931. +{
  39932. + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
  39933. + DWC_ASSERT(qh, "qh is not allocated\n");
  39934. + return qh->usecs;
  39935. +}
  39936. +
  39937. +void dwc_otg_hcd_dump_state(dwc_otg_hcd_t * hcd)
  39938. +{
  39939. +#ifdef DEBUG
  39940. + int num_channels;
  39941. + int i;
  39942. + gnptxsts_data_t np_tx_status;
  39943. + hptxsts_data_t p_tx_status;
  39944. +
  39945. + num_channels = hcd->core_if->core_params->host_channels;
  39946. + DWC_PRINTF("\n");
  39947. + DWC_PRINTF
  39948. + ("************************************************************\n");
  39949. + DWC_PRINTF("HCD State:\n");
  39950. + DWC_PRINTF(" Num channels: %d\n", num_channels);
  39951. + for (i = 0; i < num_channels; i++) {
  39952. + dwc_hc_t *hc = hcd->hc_ptr_array[i];
  39953. + DWC_PRINTF(" Channel %d:\n", i);
  39954. + DWC_PRINTF(" dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
  39955. + hc->dev_addr, hc->ep_num, hc->ep_is_in);
  39956. + DWC_PRINTF(" speed: %d\n", hc->speed);
  39957. + DWC_PRINTF(" ep_type: %d\n", hc->ep_type);
  39958. + DWC_PRINTF(" max_packet: %d\n", hc->max_packet);
  39959. + DWC_PRINTF(" data_pid_start: %d\n", hc->data_pid_start);
  39960. + DWC_PRINTF(" multi_count: %d\n", hc->multi_count);
  39961. + DWC_PRINTF(" xfer_started: %d\n", hc->xfer_started);
  39962. + DWC_PRINTF(" xfer_buff: %p\n", hc->xfer_buff);
  39963. + DWC_PRINTF(" xfer_len: %d\n", hc->xfer_len);
  39964. + DWC_PRINTF(" xfer_count: %d\n", hc->xfer_count);
  39965. + DWC_PRINTF(" halt_on_queue: %d\n", hc->halt_on_queue);
  39966. + DWC_PRINTF(" halt_pending: %d\n", hc->halt_pending);
  39967. + DWC_PRINTF(" halt_status: %d\n", hc->halt_status);
  39968. + DWC_PRINTF(" do_split: %d\n", hc->do_split);
  39969. + DWC_PRINTF(" complete_split: %d\n", hc->complete_split);
  39970. + DWC_PRINTF(" hub_addr: %d\n", hc->hub_addr);
  39971. + DWC_PRINTF(" port_addr: %d\n", hc->port_addr);
  39972. + DWC_PRINTF(" xact_pos: %d\n", hc->xact_pos);
  39973. + DWC_PRINTF(" requests: %d\n", hc->requests);
  39974. + DWC_PRINTF(" qh: %p\n", hc->qh);
  39975. + if (hc->xfer_started) {
  39976. + hfnum_data_t hfnum;
  39977. + hcchar_data_t hcchar;
  39978. + hctsiz_data_t hctsiz;
  39979. + hcint_data_t hcint;
  39980. + hcintmsk_data_t hcintmsk;
  39981. + hfnum.d32 =
  39982. + DWC_READ_REG32(&hcd->core_if->
  39983. + host_if->host_global_regs->hfnum);
  39984. + hcchar.d32 =
  39985. + DWC_READ_REG32(&hcd->core_if->host_if->
  39986. + hc_regs[i]->hcchar);
  39987. + hctsiz.d32 =
  39988. + DWC_READ_REG32(&hcd->core_if->host_if->
  39989. + hc_regs[i]->hctsiz);
  39990. + hcint.d32 =
  39991. + DWC_READ_REG32(&hcd->core_if->host_if->
  39992. + hc_regs[i]->hcint);
  39993. + hcintmsk.d32 =
  39994. + DWC_READ_REG32(&hcd->core_if->host_if->
  39995. + hc_regs[i]->hcintmsk);
  39996. + DWC_PRINTF(" hfnum: 0x%08x\n", hfnum.d32);
  39997. + DWC_PRINTF(" hcchar: 0x%08x\n", hcchar.d32);
  39998. + DWC_PRINTF(" hctsiz: 0x%08x\n", hctsiz.d32);
  39999. + DWC_PRINTF(" hcint: 0x%08x\n", hcint.d32);
  40000. + DWC_PRINTF(" hcintmsk: 0x%08x\n", hcintmsk.d32);
  40001. + }
  40002. + if (hc->xfer_started && hc->qh) {
  40003. + dwc_otg_qtd_t *qtd;
  40004. + dwc_otg_hcd_urb_t *urb;
  40005. +
  40006. + DWC_CIRCLEQ_FOREACH(qtd, &hc->qh->qtd_list, qtd_list_entry) {
  40007. + if (!qtd->in_process)
  40008. + break;
  40009. +
  40010. + urb = qtd->urb;
  40011. + DWC_PRINTF(" URB Info:\n");
  40012. + DWC_PRINTF(" qtd: %p, urb: %p\n", qtd, urb);
  40013. + if (urb) {
  40014. + DWC_PRINTF(" Dev: %d, EP: %d %s\n",
  40015. + dwc_otg_hcd_get_dev_addr(&urb->
  40016. + pipe_info),
  40017. + dwc_otg_hcd_get_ep_num(&urb->
  40018. + pipe_info),
  40019. + dwc_otg_hcd_is_pipe_in(&urb->
  40020. + pipe_info) ?
  40021. + "IN" : "OUT");
  40022. + DWC_PRINTF(" Max packet size: %d\n",
  40023. + dwc_otg_hcd_get_mps(&urb->
  40024. + pipe_info));
  40025. + DWC_PRINTF(" transfer_buffer: %p\n",
  40026. + urb->buf);
  40027. + DWC_PRINTF(" transfer_dma: %p\n",
  40028. + (void *)urb->dma);
  40029. + DWC_PRINTF(" transfer_buffer_length: %d\n",
  40030. + urb->length);
  40031. + DWC_PRINTF(" actual_length: %d\n",
  40032. + urb->actual_length);
  40033. + }
  40034. + }
  40035. + }
  40036. + }
  40037. + DWC_PRINTF(" non_periodic_channels: %d\n", hcd->non_periodic_channels);
  40038. + DWC_PRINTF(" periodic_channels: %d\n", hcd->periodic_channels);
  40039. + DWC_PRINTF(" periodic_usecs: %d\n", hcd->periodic_usecs);
  40040. + np_tx_status.d32 =
  40041. + DWC_READ_REG32(&hcd->core_if->core_global_regs->gnptxsts);
  40042. + DWC_PRINTF(" NP Tx Req Queue Space Avail: %d\n",
  40043. + np_tx_status.b.nptxqspcavail);
  40044. + DWC_PRINTF(" NP Tx FIFO Space Avail: %d\n",
  40045. + np_tx_status.b.nptxfspcavail);
  40046. + p_tx_status.d32 =
  40047. + DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hptxsts);
  40048. + DWC_PRINTF(" P Tx Req Queue Space Avail: %d\n",
  40049. + p_tx_status.b.ptxqspcavail);
  40050. + DWC_PRINTF(" P Tx FIFO Space Avail: %d\n", p_tx_status.b.ptxfspcavail);
  40051. + dwc_otg_hcd_dump_frrem(hcd);
  40052. + dwc_otg_dump_global_registers(hcd->core_if);
  40053. + dwc_otg_dump_host_registers(hcd->core_if);
  40054. + DWC_PRINTF
  40055. + ("************************************************************\n");
  40056. + DWC_PRINTF("\n");
  40057. +#endif
  40058. +}
  40059. +
  40060. +#ifdef DEBUG
  40061. +void dwc_print_setup_data(uint8_t * setup)
  40062. +{
  40063. + int i;
  40064. + if (CHK_DEBUG_LEVEL(DBG_HCD)) {
  40065. + DWC_PRINTF("Setup Data = MSB ");
  40066. + for (i = 7; i >= 0; i--)
  40067. + DWC_PRINTF("%02x ", setup[i]);
  40068. + DWC_PRINTF("\n");
  40069. + DWC_PRINTF(" bmRequestType Tranfer = %s\n",
  40070. + (setup[0] & 0x80) ? "Device-to-Host" :
  40071. + "Host-to-Device");
  40072. + DWC_PRINTF(" bmRequestType Type = ");
  40073. + switch ((setup[0] & 0x60) >> 5) {
  40074. + case 0:
  40075. + DWC_PRINTF("Standard\n");
  40076. + break;
  40077. + case 1:
  40078. + DWC_PRINTF("Class\n");
  40079. + break;
  40080. + case 2:
  40081. + DWC_PRINTF("Vendor\n");
  40082. + break;
  40083. + case 3:
  40084. + DWC_PRINTF("Reserved\n");
  40085. + break;
  40086. + }
  40087. + DWC_PRINTF(" bmRequestType Recipient = ");
  40088. + switch (setup[0] & 0x1f) {
  40089. + case 0:
  40090. + DWC_PRINTF("Device\n");
  40091. + break;
  40092. + case 1:
  40093. + DWC_PRINTF("Interface\n");
  40094. + break;
  40095. + case 2:
  40096. + DWC_PRINTF("Endpoint\n");
  40097. + break;
  40098. + case 3:
  40099. + DWC_PRINTF("Other\n");
  40100. + break;
  40101. + default:
  40102. + DWC_PRINTF("Reserved\n");
  40103. + break;
  40104. + }
  40105. + DWC_PRINTF(" bRequest = 0x%0x\n", setup[1]);
  40106. + DWC_PRINTF(" wValue = 0x%0x\n", *((uint16_t *) & setup[2]));
  40107. + DWC_PRINTF(" wIndex = 0x%0x\n", *((uint16_t *) & setup[4]));
  40108. + DWC_PRINTF(" wLength = 0x%0x\n\n", *((uint16_t *) & setup[6]));
  40109. + }
  40110. +}
  40111. +#endif
  40112. +
  40113. +void dwc_otg_hcd_dump_frrem(dwc_otg_hcd_t * hcd)
  40114. +{
  40115. +#if 0
  40116. + DWC_PRINTF("Frame remaining at SOF:\n");
  40117. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  40118. + hcd->frrem_samples, hcd->frrem_accum,
  40119. + (hcd->frrem_samples > 0) ?
  40120. + hcd->frrem_accum / hcd->frrem_samples : 0);
  40121. +
  40122. + DWC_PRINTF("\n");
  40123. + DWC_PRINTF("Frame remaining at start_transfer (uframe 7):\n");
  40124. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  40125. + hcd->core_if->hfnum_7_samples,
  40126. + hcd->core_if->hfnum_7_frrem_accum,
  40127. + (hcd->core_if->hfnum_7_samples >
  40128. + 0) ? hcd->core_if->hfnum_7_frrem_accum /
  40129. + hcd->core_if->hfnum_7_samples : 0);
  40130. + DWC_PRINTF("Frame remaining at start_transfer (uframe 0):\n");
  40131. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  40132. + hcd->core_if->hfnum_0_samples,
  40133. + hcd->core_if->hfnum_0_frrem_accum,
  40134. + (hcd->core_if->hfnum_0_samples >
  40135. + 0) ? hcd->core_if->hfnum_0_frrem_accum /
  40136. + hcd->core_if->hfnum_0_samples : 0);
  40137. + DWC_PRINTF("Frame remaining at start_transfer (uframe 1-6):\n");
  40138. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  40139. + hcd->core_if->hfnum_other_samples,
  40140. + hcd->core_if->hfnum_other_frrem_accum,
  40141. + (hcd->core_if->hfnum_other_samples >
  40142. + 0) ? hcd->core_if->hfnum_other_frrem_accum /
  40143. + hcd->core_if->hfnum_other_samples : 0);
  40144. +
  40145. + DWC_PRINTF("\n");
  40146. + DWC_PRINTF("Frame remaining at sample point A (uframe 7):\n");
  40147. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  40148. + hcd->hfnum_7_samples_a, hcd->hfnum_7_frrem_accum_a,
  40149. + (hcd->hfnum_7_samples_a > 0) ?
  40150. + hcd->hfnum_7_frrem_accum_a / hcd->hfnum_7_samples_a : 0);
  40151. + DWC_PRINTF("Frame remaining at sample point A (uframe 0):\n");
  40152. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  40153. + hcd->hfnum_0_samples_a, hcd->hfnum_0_frrem_accum_a,
  40154. + (hcd->hfnum_0_samples_a > 0) ?
  40155. + hcd->hfnum_0_frrem_accum_a / hcd->hfnum_0_samples_a : 0);
  40156. + DWC_PRINTF("Frame remaining at sample point A (uframe 1-6):\n");
  40157. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  40158. + hcd->hfnum_other_samples_a, hcd->hfnum_other_frrem_accum_a,
  40159. + (hcd->hfnum_other_samples_a > 0) ?
  40160. + hcd->hfnum_other_frrem_accum_a /
  40161. + hcd->hfnum_other_samples_a : 0);
  40162. +
  40163. + DWC_PRINTF("\n");
  40164. + DWC_PRINTF("Frame remaining at sample point B (uframe 7):\n");
  40165. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  40166. + hcd->hfnum_7_samples_b, hcd->hfnum_7_frrem_accum_b,
  40167. + (hcd->hfnum_7_samples_b > 0) ?
  40168. + hcd->hfnum_7_frrem_accum_b / hcd->hfnum_7_samples_b : 0);
  40169. + DWC_PRINTF("Frame remaining at sample point B (uframe 0):\n");
  40170. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  40171. + hcd->hfnum_0_samples_b, hcd->hfnum_0_frrem_accum_b,
  40172. + (hcd->hfnum_0_samples_b > 0) ?
  40173. + hcd->hfnum_0_frrem_accum_b / hcd->hfnum_0_samples_b : 0);
  40174. + DWC_PRINTF("Frame remaining at sample point B (uframe 1-6):\n");
  40175. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  40176. + hcd->hfnum_other_samples_b, hcd->hfnum_other_frrem_accum_b,
  40177. + (hcd->hfnum_other_samples_b > 0) ?
  40178. + hcd->hfnum_other_frrem_accum_b /
  40179. + hcd->hfnum_other_samples_b : 0);
  40180. +#endif
  40181. +}
  40182. +
  40183. +#endif /* DWC_DEVICE_ONLY */
  40184. --- /dev/null
  40185. +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd.h
  40186. @@ -0,0 +1,870 @@
  40187. +/* ==========================================================================
  40188. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd.h $
  40189. + * $Revision: #58 $
  40190. + * $Date: 2011/09/15 $
  40191. + * $Change: 1846647 $
  40192. + *
  40193. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  40194. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  40195. + * otherwise expressly agreed to in writing between Synopsys and you.
  40196. + *
  40197. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  40198. + * any End User Software License Agreement or Agreement for Licensed Product
  40199. + * with Synopsys or any supplement thereto. You are permitted to use and
  40200. + * redistribute this Software in source and binary forms, with or without
  40201. + * modification, provided that redistributions of source code must retain this
  40202. + * notice. You may not view, use, disclose, copy or distribute this file or
  40203. + * any information contained herein except pursuant to this license grant from
  40204. + * Synopsys. If you do not agree with this notice, including the disclaimer
  40205. + * below, then you are not authorized to use the Software.
  40206. + *
  40207. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  40208. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  40209. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  40210. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  40211. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  40212. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  40213. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  40214. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  40215. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  40216. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  40217. + * DAMAGE.
  40218. + * ========================================================================== */
  40219. +#ifndef DWC_DEVICE_ONLY
  40220. +#ifndef __DWC_HCD_H__
  40221. +#define __DWC_HCD_H__
  40222. +
  40223. +#include "dwc_otg_os_dep.h"
  40224. +#include "usb.h"
  40225. +#include "dwc_otg_hcd_if.h"
  40226. +#include "dwc_otg_core_if.h"
  40227. +#include "dwc_list.h"
  40228. +#include "dwc_otg_cil.h"
  40229. +#include "dwc_otg_fiq_fsm.h"
  40230. +#include "dwc_otg_driver.h"
  40231. +
  40232. +
  40233. +/**
  40234. + * @file
  40235. + *
  40236. + * This file contains the structures, constants, and interfaces for
  40237. + * the Host Contoller Driver (HCD).
  40238. + *
  40239. + * The Host Controller Driver (HCD) is responsible for translating requests
  40240. + * from the USB Driver into the appropriate actions on the DWC_otg controller.
  40241. + * It isolates the USBD from the specifics of the controller by providing an
  40242. + * API to the USBD.
  40243. + */
  40244. +
  40245. +struct dwc_otg_hcd_pipe_info {
  40246. + uint8_t dev_addr;
  40247. + uint8_t ep_num;
  40248. + uint8_t pipe_type;
  40249. + uint8_t pipe_dir;
  40250. + uint16_t mps;
  40251. +};
  40252. +
  40253. +struct dwc_otg_hcd_iso_packet_desc {
  40254. + uint32_t offset;
  40255. + uint32_t length;
  40256. + uint32_t actual_length;
  40257. + uint32_t status;
  40258. +};
  40259. +
  40260. +struct dwc_otg_qtd;
  40261. +
  40262. +struct dwc_otg_hcd_urb {
  40263. + void *priv;
  40264. + struct dwc_otg_qtd *qtd;
  40265. + void *buf;
  40266. + dwc_dma_t dma;
  40267. + void *setup_packet;
  40268. + dwc_dma_t setup_dma;
  40269. + uint32_t length;
  40270. + uint32_t actual_length;
  40271. + uint32_t status;
  40272. + uint32_t error_count;
  40273. + uint32_t packet_count;
  40274. + uint32_t flags;
  40275. + uint16_t interval;
  40276. + struct dwc_otg_hcd_pipe_info pipe_info;
  40277. + struct dwc_otg_hcd_iso_packet_desc iso_descs[0];
  40278. +};
  40279. +
  40280. +static inline uint8_t dwc_otg_hcd_get_ep_num(struct dwc_otg_hcd_pipe_info *pipe)
  40281. +{
  40282. + return pipe->ep_num;
  40283. +}
  40284. +
  40285. +static inline uint8_t dwc_otg_hcd_get_pipe_type(struct dwc_otg_hcd_pipe_info
  40286. + *pipe)
  40287. +{
  40288. + return pipe->pipe_type;
  40289. +}
  40290. +
  40291. +static inline uint16_t dwc_otg_hcd_get_mps(struct dwc_otg_hcd_pipe_info *pipe)
  40292. +{
  40293. + return pipe->mps;
  40294. +}
  40295. +
  40296. +static inline uint8_t dwc_otg_hcd_get_dev_addr(struct dwc_otg_hcd_pipe_info
  40297. + *pipe)
  40298. +{
  40299. + return pipe->dev_addr;
  40300. +}
  40301. +
  40302. +static inline uint8_t dwc_otg_hcd_is_pipe_isoc(struct dwc_otg_hcd_pipe_info
  40303. + *pipe)
  40304. +{
  40305. + return (pipe->pipe_type == UE_ISOCHRONOUS);
  40306. +}
  40307. +
  40308. +static inline uint8_t dwc_otg_hcd_is_pipe_int(struct dwc_otg_hcd_pipe_info
  40309. + *pipe)
  40310. +{
  40311. + return (pipe->pipe_type == UE_INTERRUPT);
  40312. +}
  40313. +
  40314. +static inline uint8_t dwc_otg_hcd_is_pipe_bulk(struct dwc_otg_hcd_pipe_info
  40315. + *pipe)
  40316. +{
  40317. + return (pipe->pipe_type == UE_BULK);
  40318. +}
  40319. +
  40320. +static inline uint8_t dwc_otg_hcd_is_pipe_control(struct dwc_otg_hcd_pipe_info
  40321. + *pipe)
  40322. +{
  40323. + return (pipe->pipe_type == UE_CONTROL);
  40324. +}
  40325. +
  40326. +static inline uint8_t dwc_otg_hcd_is_pipe_in(struct dwc_otg_hcd_pipe_info *pipe)
  40327. +{
  40328. + return (pipe->pipe_dir == UE_DIR_IN);
  40329. +}
  40330. +
  40331. +static inline uint8_t dwc_otg_hcd_is_pipe_out(struct dwc_otg_hcd_pipe_info
  40332. + *pipe)
  40333. +{
  40334. + return (!dwc_otg_hcd_is_pipe_in(pipe));
  40335. +}
  40336. +
  40337. +static inline void dwc_otg_hcd_fill_pipe(struct dwc_otg_hcd_pipe_info *pipe,
  40338. + uint8_t devaddr, uint8_t ep_num,
  40339. + uint8_t pipe_type, uint8_t pipe_dir,
  40340. + uint16_t mps)
  40341. +{
  40342. + pipe->dev_addr = devaddr;
  40343. + pipe->ep_num = ep_num;
  40344. + pipe->pipe_type = pipe_type;
  40345. + pipe->pipe_dir = pipe_dir;
  40346. + pipe->mps = mps;
  40347. +}
  40348. +
  40349. +/**
  40350. + * Phases for control transfers.
  40351. + */
  40352. +typedef enum dwc_otg_control_phase {
  40353. + DWC_OTG_CONTROL_SETUP,
  40354. + DWC_OTG_CONTROL_DATA,
  40355. + DWC_OTG_CONTROL_STATUS
  40356. +} dwc_otg_control_phase_e;
  40357. +
  40358. +/** Transaction types. */
  40359. +typedef enum dwc_otg_transaction_type {
  40360. + DWC_OTG_TRANSACTION_NONE = 0,
  40361. + DWC_OTG_TRANSACTION_PERIODIC = 1,
  40362. + DWC_OTG_TRANSACTION_NON_PERIODIC = 2,
  40363. + DWC_OTG_TRANSACTION_ALL = DWC_OTG_TRANSACTION_PERIODIC + DWC_OTG_TRANSACTION_NON_PERIODIC
  40364. +} dwc_otg_transaction_type_e;
  40365. +
  40366. +struct dwc_otg_qh;
  40367. +
  40368. +/**
  40369. + * A Queue Transfer Descriptor (QTD) holds the state of a bulk, control,
  40370. + * interrupt, or isochronous transfer. A single QTD is created for each URB
  40371. + * (of one of these types) submitted to the HCD. The transfer associated with
  40372. + * a QTD may require one or multiple transactions.
  40373. + *
  40374. + * A QTD is linked to a Queue Head, which is entered in either the
  40375. + * non-periodic or periodic schedule for execution. When a QTD is chosen for
  40376. + * execution, some or all of its transactions may be executed. After
  40377. + * execution, the state of the QTD is updated. The QTD may be retired if all
  40378. + * its transactions are complete or if an error occurred. Otherwise, it
  40379. + * remains in the schedule so more transactions can be executed later.
  40380. + */
  40381. +typedef struct dwc_otg_qtd {
  40382. + /**
  40383. + * Determines the PID of the next data packet for the data phase of
  40384. + * control transfers. Ignored for other transfer types.<br>
  40385. + * One of the following values:
  40386. + * - DWC_OTG_HC_PID_DATA0
  40387. + * - DWC_OTG_HC_PID_DATA1
  40388. + */
  40389. + uint8_t data_toggle;
  40390. +
  40391. + /** Current phase for control transfers (Setup, Data, or Status). */
  40392. + dwc_otg_control_phase_e control_phase;
  40393. +
  40394. + /** Keep track of the current split type
  40395. + * for FS/LS endpoints on a HS Hub */
  40396. + uint8_t complete_split;
  40397. +
  40398. + /** How many bytes transferred during SSPLIT OUT */
  40399. + uint32_t ssplit_out_xfer_count;
  40400. +
  40401. + /**
  40402. + * Holds the number of bus errors that have occurred for a transaction
  40403. + * within this transfer.
  40404. + */
  40405. + uint8_t error_count;
  40406. +
  40407. + /**
  40408. + * Index of the next frame descriptor for an isochronous transfer. A
  40409. + * frame descriptor describes the buffer position and length of the
  40410. + * data to be transferred in the next scheduled (micro)frame of an
  40411. + * isochronous transfer. It also holds status for that transaction.
  40412. + * The frame index starts at 0.
  40413. + */
  40414. + uint16_t isoc_frame_index;
  40415. +
  40416. + /** Position of the ISOC split on full/low speed */
  40417. + uint8_t isoc_split_pos;
  40418. +
  40419. + /** Position of the ISOC split in the buffer for the current frame */
  40420. + uint16_t isoc_split_offset;
  40421. +
  40422. + /** URB for this transfer */
  40423. + struct dwc_otg_hcd_urb *urb;
  40424. +
  40425. + struct dwc_otg_qh *qh;
  40426. +
  40427. + /** This list of QTDs */
  40428. + DWC_CIRCLEQ_ENTRY(dwc_otg_qtd) qtd_list_entry;
  40429. +
  40430. + /** Indicates if this QTD is currently processed by HW. */
  40431. + uint8_t in_process;
  40432. +
  40433. + /** Number of DMA descriptors for this QTD */
  40434. + uint8_t n_desc;
  40435. +
  40436. + /**
  40437. + * Last activated frame(packet) index.
  40438. + * Used in Descriptor DMA mode only.
  40439. + */
  40440. + uint16_t isoc_frame_index_last;
  40441. +
  40442. +} dwc_otg_qtd_t;
  40443. +
  40444. +DWC_CIRCLEQ_HEAD(dwc_otg_qtd_list, dwc_otg_qtd);
  40445. +
  40446. +/**
  40447. + * A Queue Head (QH) holds the static characteristics of an endpoint and
  40448. + * maintains a list of transfers (QTDs) for that endpoint. A QH structure may
  40449. + * be entered in either the non-periodic or periodic schedule.
  40450. + */
  40451. +typedef struct dwc_otg_qh {
  40452. + /**
  40453. + * Endpoint type.
  40454. + * One of the following values:
  40455. + * - UE_CONTROL
  40456. + * - UE_BULK
  40457. + * - UE_INTERRUPT
  40458. + * - UE_ISOCHRONOUS
  40459. + */
  40460. + uint8_t ep_type;
  40461. + uint8_t ep_is_in;
  40462. +
  40463. + /** wMaxPacketSize Field of Endpoint Descriptor. */
  40464. + uint16_t maxp;
  40465. +
  40466. + /**
  40467. + * Device speed.
  40468. + * One of the following values:
  40469. + * - DWC_OTG_EP_SPEED_LOW
  40470. + * - DWC_OTG_EP_SPEED_FULL
  40471. + * - DWC_OTG_EP_SPEED_HIGH
  40472. + */
  40473. + uint8_t dev_speed;
  40474. +
  40475. + /**
  40476. + * Determines the PID of the next data packet for non-control
  40477. + * transfers. Ignored for control transfers.<br>
  40478. + * One of the following values:
  40479. + * - DWC_OTG_HC_PID_DATA0
  40480. + * - DWC_OTG_HC_PID_DATA1
  40481. + */
  40482. + uint8_t data_toggle;
  40483. +
  40484. + /** Ping state if 1. */
  40485. + uint8_t ping_state;
  40486. +
  40487. + /**
  40488. + * List of QTDs for this QH.
  40489. + */
  40490. + struct dwc_otg_qtd_list qtd_list;
  40491. +
  40492. + /** Host channel currently processing transfers for this QH. */
  40493. + struct dwc_hc *channel;
  40494. +
  40495. + /** Full/low speed endpoint on high-speed hub requires split. */
  40496. + uint8_t do_split;
  40497. +
  40498. + /** @name Periodic schedule information */
  40499. + /** @{ */
  40500. +
  40501. + /** Bandwidth in microseconds per (micro)frame. */
  40502. + uint16_t usecs;
  40503. +
  40504. + /** Interval between transfers in (micro)frames. */
  40505. + uint16_t interval;
  40506. +
  40507. + /**
  40508. + * (micro)frame to initialize a periodic transfer. The transfer
  40509. + * executes in the following (micro)frame.
  40510. + */
  40511. + uint16_t sched_frame;
  40512. +
  40513. + /*
  40514. + ** Frame a NAK was received on this queue head, used to minimise NAK retransmission
  40515. + */
  40516. + uint16_t nak_frame;
  40517. +
  40518. + /** (micro)frame at which last start split was initialized. */
  40519. + uint16_t start_split_frame;
  40520. +
  40521. + /** @} */
  40522. +
  40523. + /**
  40524. + * Used instead of original buffer if
  40525. + * it(physical address) is not dword-aligned.
  40526. + */
  40527. + uint8_t *dw_align_buf;
  40528. + dwc_dma_t dw_align_buf_dma;
  40529. +
  40530. + /** Entry for QH in either the periodic or non-periodic schedule. */
  40531. + dwc_list_link_t qh_list_entry;
  40532. +
  40533. + /** @name Descriptor DMA support */
  40534. + /** @{ */
  40535. +
  40536. + /** Descriptor List. */
  40537. + dwc_otg_host_dma_desc_t *desc_list;
  40538. +
  40539. + /** Descriptor List physical address. */
  40540. + dwc_dma_t desc_list_dma;
  40541. +
  40542. + /**
  40543. + * Xfer Bytes array.
  40544. + * Each element corresponds to a descriptor and indicates
  40545. + * original XferSize size value for the descriptor.
  40546. + */
  40547. + uint32_t *n_bytes;
  40548. +
  40549. + /** Actual number of transfer descriptors in a list. */
  40550. + uint16_t ntd;
  40551. +
  40552. + /** First activated isochronous transfer descriptor index. */
  40553. + uint8_t td_first;
  40554. + /** Last activated isochronous transfer descriptor index. */
  40555. + uint8_t td_last;
  40556. +
  40557. + /** @} */
  40558. +
  40559. +
  40560. + uint16_t speed;
  40561. + uint16_t frame_usecs[8];
  40562. +
  40563. + uint32_t skip_count;
  40564. +} dwc_otg_qh_t;
  40565. +
  40566. +DWC_CIRCLEQ_HEAD(hc_list, dwc_hc);
  40567. +
  40568. +typedef struct urb_tq_entry {
  40569. + struct urb *urb;
  40570. + DWC_TAILQ_ENTRY(urb_tq_entry) urb_tq_entries;
  40571. +} urb_tq_entry_t;
  40572. +
  40573. +DWC_TAILQ_HEAD(urb_list, urb_tq_entry);
  40574. +
  40575. +/**
  40576. + * This structure holds the state of the HCD, including the non-periodic and
  40577. + * periodic schedules.
  40578. + */
  40579. +struct dwc_otg_hcd {
  40580. + /** The DWC otg device pointer */
  40581. + struct dwc_otg_device *otg_dev;
  40582. + /** DWC OTG Core Interface Layer */
  40583. + dwc_otg_core_if_t *core_if;
  40584. +
  40585. + /** Function HCD driver callbacks */
  40586. + struct dwc_otg_hcd_function_ops *fops;
  40587. +
  40588. + /** Internal DWC HCD Flags */
  40589. + volatile union dwc_otg_hcd_internal_flags {
  40590. + uint32_t d32;
  40591. + struct {
  40592. + unsigned port_connect_status_change:1;
  40593. + unsigned port_connect_status:1;
  40594. + unsigned port_reset_change:1;
  40595. + unsigned port_enable_change:1;
  40596. + unsigned port_suspend_change:1;
  40597. + unsigned port_over_current_change:1;
  40598. + unsigned port_l1_change:1;
  40599. + unsigned port_speed:2;
  40600. + unsigned reserved:24;
  40601. + } b;
  40602. + } flags;
  40603. +
  40604. + /**
  40605. + * Inactive items in the non-periodic schedule. This is a list of
  40606. + * Queue Heads. Transfers associated with these Queue Heads are not
  40607. + * currently assigned to a host channel.
  40608. + */
  40609. + dwc_list_link_t non_periodic_sched_inactive;
  40610. +
  40611. + /**
  40612. + * Active items in the non-periodic schedule. This is a list of
  40613. + * Queue Heads. Transfers associated with these Queue Heads are
  40614. + * currently assigned to a host channel.
  40615. + */
  40616. + dwc_list_link_t non_periodic_sched_active;
  40617. +
  40618. + /**
  40619. + * Pointer to the next Queue Head to process in the active
  40620. + * non-periodic schedule.
  40621. + */
  40622. + dwc_list_link_t *non_periodic_qh_ptr;
  40623. +
  40624. + /**
  40625. + * Inactive items in the periodic schedule. This is a list of QHs for
  40626. + * periodic transfers that are _not_ scheduled for the next frame.
  40627. + * Each QH in the list has an interval counter that determines when it
  40628. + * needs to be scheduled for execution. This scheduling mechanism
  40629. + * allows only a simple calculation for periodic bandwidth used (i.e.
  40630. + * must assume that all periodic transfers may need to execute in the
  40631. + * same frame). However, it greatly simplifies scheduling and should
  40632. + * be sufficient for the vast majority of OTG hosts, which need to
  40633. + * connect to a small number of peripherals at one time.
  40634. + *
  40635. + * Items move from this list to periodic_sched_ready when the QH
  40636. + * interval counter is 0 at SOF.
  40637. + */
  40638. + dwc_list_link_t periodic_sched_inactive;
  40639. +
  40640. + /**
  40641. + * List of periodic QHs that are ready for execution in the next
  40642. + * frame, but have not yet been assigned to host channels.
  40643. + *
  40644. + * Items move from this list to periodic_sched_assigned as host
  40645. + * channels become available during the current frame.
  40646. + */
  40647. + dwc_list_link_t periodic_sched_ready;
  40648. +
  40649. + /**
  40650. + * List of periodic QHs to be executed in the next frame that are
  40651. + * assigned to host channels.
  40652. + *
  40653. + * Items move from this list to periodic_sched_queued as the
  40654. + * transactions for the QH are queued to the DWC_otg controller.
  40655. + */
  40656. + dwc_list_link_t periodic_sched_assigned;
  40657. +
  40658. + /**
  40659. + * List of periodic QHs that have been queued for execution.
  40660. + *
  40661. + * Items move from this list to either periodic_sched_inactive or
  40662. + * periodic_sched_ready when the channel associated with the transfer
  40663. + * is released. If the interval for the QH is 1, the item moves to
  40664. + * periodic_sched_ready because it must be rescheduled for the next
  40665. + * frame. Otherwise, the item moves to periodic_sched_inactive.
  40666. + */
  40667. + dwc_list_link_t periodic_sched_queued;
  40668. +
  40669. + /**
  40670. + * Total bandwidth claimed so far for periodic transfers. This value
  40671. + * is in microseconds per (micro)frame. The assumption is that all
  40672. + * periodic transfers may occur in the same (micro)frame.
  40673. + */
  40674. + uint16_t periodic_usecs;
  40675. +
  40676. + /**
  40677. + * Total bandwidth claimed so far for all periodic transfers
  40678. + * in a frame.
  40679. + * This will include a mixture of HS and FS transfers.
  40680. + * Units are microseconds per (micro)frame.
  40681. + * We have a budget per frame and have to schedule
  40682. + * transactions accordingly.
  40683. + * Watch out for the fact that things are actually scheduled for the
  40684. + * "next frame".
  40685. + */
  40686. + uint16_t frame_usecs[8];
  40687. +
  40688. +
  40689. + /**
  40690. + * Frame number read from the core at SOF. The value ranges from 0 to
  40691. + * DWC_HFNUM_MAX_FRNUM.
  40692. + */
  40693. + uint16_t frame_number;
  40694. +
  40695. + /**
  40696. + * Count of periodic QHs, if using several eps. For SOF enable/disable.
  40697. + */
  40698. + uint16_t periodic_qh_count;
  40699. +
  40700. + /**
  40701. + * Free host channels in the controller. This is a list of
  40702. + * dwc_hc_t items.
  40703. + */
  40704. + struct hc_list free_hc_list;
  40705. + /**
  40706. + * Number of host channels assigned to periodic transfers. Currently
  40707. + * assuming that there is a dedicated host channel for each periodic
  40708. + * transaction and at least one host channel available for
  40709. + * non-periodic transactions.
  40710. + */
  40711. + int periodic_channels; /* microframe_schedule==0 */
  40712. +
  40713. + /**
  40714. + * Number of host channels assigned to non-periodic transfers.
  40715. + */
  40716. + int non_periodic_channels; /* microframe_schedule==0 */
  40717. +
  40718. + /**
  40719. + * Number of host channels assigned to non-periodic transfers.
  40720. + */
  40721. + int available_host_channels;
  40722. +
  40723. + /**
  40724. + * Array of pointers to the host channel descriptors. Allows accessing
  40725. + * a host channel descriptor given the host channel number. This is
  40726. + * useful in interrupt handlers.
  40727. + */
  40728. + struct dwc_hc *hc_ptr_array[MAX_EPS_CHANNELS];
  40729. +
  40730. + /**
  40731. + * Buffer to use for any data received during the status phase of a
  40732. + * control transfer. Normally no data is transferred during the status
  40733. + * phase. This buffer is used as a bit bucket.
  40734. + */
  40735. + uint8_t *status_buf;
  40736. +
  40737. + /**
  40738. + * DMA address for status_buf.
  40739. + */
  40740. + dma_addr_t status_buf_dma;
  40741. +#define DWC_OTG_HCD_STATUS_BUF_SIZE 64
  40742. +
  40743. + /**
  40744. + * Connection timer. An OTG host must display a message if the device
  40745. + * does not connect. Started when the VBus power is turned on via
  40746. + * sysfs attribute "buspower".
  40747. + */
  40748. + dwc_timer_t *conn_timer;
  40749. +
  40750. + /* Tasket to do a reset */
  40751. + dwc_tasklet_t *reset_tasklet;
  40752. +
  40753. + dwc_tasklet_t *completion_tasklet;
  40754. + struct urb_list completed_urb_list;
  40755. +
  40756. + /* */
  40757. + dwc_spinlock_t *lock;
  40758. + /**
  40759. + * Private data that could be used by OS wrapper.
  40760. + */
  40761. + void *priv;
  40762. +
  40763. + uint8_t otg_port;
  40764. +
  40765. + /** Frame List */
  40766. + uint32_t *frame_list;
  40767. +
  40768. + /** Hub - Port assignment */
  40769. + int hub_port[128];
  40770. +#ifdef FIQ_DEBUG
  40771. + int hub_port_alloc[2048];
  40772. +#endif
  40773. +
  40774. + /** Frame List DMA address */
  40775. + dma_addr_t frame_list_dma;
  40776. +
  40777. + struct fiq_stack *fiq_stack;
  40778. + struct fiq_state *fiq_state;
  40779. +
  40780. + /** Virtual address for split transaction DMA bounce buffers */
  40781. + struct fiq_dma_blob *fiq_dmab;
  40782. +
  40783. +#ifdef DEBUG
  40784. + uint32_t frrem_samples;
  40785. + uint64_t frrem_accum;
  40786. +
  40787. + uint32_t hfnum_7_samples_a;
  40788. + uint64_t hfnum_7_frrem_accum_a;
  40789. + uint32_t hfnum_0_samples_a;
  40790. + uint64_t hfnum_0_frrem_accum_a;
  40791. + uint32_t hfnum_other_samples_a;
  40792. + uint64_t hfnum_other_frrem_accum_a;
  40793. +
  40794. + uint32_t hfnum_7_samples_b;
  40795. + uint64_t hfnum_7_frrem_accum_b;
  40796. + uint32_t hfnum_0_samples_b;
  40797. + uint64_t hfnum_0_frrem_accum_b;
  40798. + uint32_t hfnum_other_samples_b;
  40799. + uint64_t hfnum_other_frrem_accum_b;
  40800. +#endif
  40801. +};
  40802. +
  40803. +static inline struct device *dwc_otg_hcd_to_dev(struct dwc_otg_hcd *hcd)
  40804. +{
  40805. + return &hcd->otg_dev->os_dep.platformdev->dev;
  40806. +}
  40807. +
  40808. +/** @name Transaction Execution Functions */
  40809. +/** @{ */
  40810. +extern dwc_otg_transaction_type_e dwc_otg_hcd_select_transactions(dwc_otg_hcd_t
  40811. + * hcd);
  40812. +extern void dwc_otg_hcd_queue_transactions(dwc_otg_hcd_t * hcd,
  40813. + dwc_otg_transaction_type_e tr_type);
  40814. +
  40815. +int dwc_otg_hcd_allocate_port(dwc_otg_hcd_t * hcd, dwc_otg_qh_t *qh);
  40816. +void dwc_otg_hcd_release_port(dwc_otg_hcd_t * dwc_otg_hcd, dwc_otg_qh_t *qh);
  40817. +
  40818. +extern int fiq_fsm_queue_transaction(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh);
  40819. +extern int fiq_fsm_transaction_suitable(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh);
  40820. +extern void dwc_otg_cleanup_fiq_channel(dwc_otg_hcd_t *hcd, uint32_t num);
  40821. +
  40822. +/** @} */
  40823. +
  40824. +/** @name Interrupt Handler Functions */
  40825. +/** @{ */
  40826. +extern int32_t dwc_otg_hcd_handle_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  40827. +extern int32_t dwc_otg_hcd_handle_sof_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  40828. +extern int32_t dwc_otg_hcd_handle_rx_status_q_level_intr(dwc_otg_hcd_t *
  40829. + dwc_otg_hcd);
  40830. +extern int32_t dwc_otg_hcd_handle_np_tx_fifo_empty_intr(dwc_otg_hcd_t *
  40831. + dwc_otg_hcd);
  40832. +extern int32_t dwc_otg_hcd_handle_perio_tx_fifo_empty_intr(dwc_otg_hcd_t *
  40833. + dwc_otg_hcd);
  40834. +extern int32_t dwc_otg_hcd_handle_incomplete_periodic_intr(dwc_otg_hcd_t *
  40835. + dwc_otg_hcd);
  40836. +extern int32_t dwc_otg_hcd_handle_port_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  40837. +extern int32_t dwc_otg_hcd_handle_conn_id_status_change_intr(dwc_otg_hcd_t *
  40838. + dwc_otg_hcd);
  40839. +extern int32_t dwc_otg_hcd_handle_disconnect_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  40840. +extern int32_t dwc_otg_hcd_handle_hc_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  40841. +extern int32_t dwc_otg_hcd_handle_hc_n_intr(dwc_otg_hcd_t * dwc_otg_hcd,
  40842. + uint32_t num);
  40843. +extern int32_t dwc_otg_hcd_handle_session_req_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  40844. +extern int32_t dwc_otg_hcd_handle_wakeup_detected_intr(dwc_otg_hcd_t *
  40845. + dwc_otg_hcd);
  40846. +/** @} */
  40847. +
  40848. +/** @name Schedule Queue Functions */
  40849. +/** @{ */
  40850. +
  40851. +/* Implemented in dwc_otg_hcd_queue.c */
  40852. +extern dwc_otg_qh_t *dwc_otg_hcd_qh_create(dwc_otg_hcd_t * hcd,
  40853. + dwc_otg_hcd_urb_t * urb, int atomic_alloc);
  40854. +extern void dwc_otg_hcd_qh_free(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  40855. +extern int dwc_otg_hcd_qh_add(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  40856. +extern void dwc_otg_hcd_qh_remove(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  40857. +extern void dwc_otg_hcd_qh_deactivate(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
  40858. + int sched_csplit);
  40859. +
  40860. +/** Remove and free a QH */
  40861. +static inline void dwc_otg_hcd_qh_remove_and_free(dwc_otg_hcd_t * hcd,
  40862. + dwc_otg_qh_t * qh)
  40863. +{
  40864. + dwc_irqflags_t flags;
  40865. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  40866. + dwc_otg_hcd_qh_remove(hcd, qh);
  40867. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  40868. + dwc_otg_hcd_qh_free(hcd, qh);
  40869. +}
  40870. +
  40871. +/** Allocates memory for a QH structure.
  40872. + * @return Returns the memory allocate or NULL on error. */
  40873. +static inline dwc_otg_qh_t *dwc_otg_hcd_qh_alloc(int atomic_alloc)
  40874. +{
  40875. + if (atomic_alloc)
  40876. + return (dwc_otg_qh_t *) DWC_ALLOC_ATOMIC(sizeof(dwc_otg_qh_t));
  40877. + else
  40878. + return (dwc_otg_qh_t *) DWC_ALLOC(sizeof(dwc_otg_qh_t));
  40879. +}
  40880. +
  40881. +extern dwc_otg_qtd_t *dwc_otg_hcd_qtd_create(dwc_otg_hcd_urb_t * urb,
  40882. + int atomic_alloc);
  40883. +extern void dwc_otg_hcd_qtd_init(dwc_otg_qtd_t * qtd, dwc_otg_hcd_urb_t * urb);
  40884. +extern int dwc_otg_hcd_qtd_add(dwc_otg_qtd_t * qtd, dwc_otg_hcd_t * dwc_otg_hcd,
  40885. + dwc_otg_qh_t ** qh, int atomic_alloc);
  40886. +
  40887. +/** Allocates memory for a QTD structure.
  40888. + * @return Returns the memory allocate or NULL on error. */
  40889. +static inline dwc_otg_qtd_t *dwc_otg_hcd_qtd_alloc(int atomic_alloc)
  40890. +{
  40891. + if (atomic_alloc)
  40892. + return (dwc_otg_qtd_t *) DWC_ALLOC_ATOMIC(sizeof(dwc_otg_qtd_t));
  40893. + else
  40894. + return (dwc_otg_qtd_t *) DWC_ALLOC(sizeof(dwc_otg_qtd_t));
  40895. +}
  40896. +
  40897. +/** Frees the memory for a QTD structure. QTD should already be removed from
  40898. + * list.
  40899. + * @param qtd QTD to free.*/
  40900. +static inline void dwc_otg_hcd_qtd_free(dwc_otg_qtd_t * qtd)
  40901. +{
  40902. + DWC_FREE(qtd);
  40903. +}
  40904. +
  40905. +/** Removes a QTD from list.
  40906. + * @param hcd HCD instance.
  40907. + * @param qtd QTD to remove from list.
  40908. + * @param qh QTD belongs to.
  40909. + */
  40910. +static inline void dwc_otg_hcd_qtd_remove(dwc_otg_hcd_t * hcd,
  40911. + dwc_otg_qtd_t * qtd,
  40912. + dwc_otg_qh_t * qh)
  40913. +{
  40914. + DWC_CIRCLEQ_REMOVE(&qh->qtd_list, qtd, qtd_list_entry);
  40915. +}
  40916. +
  40917. +/** Remove and free a QTD
  40918. + * Need to disable IRQ and hold hcd lock while calling this function out of
  40919. + * interrupt servicing chain */
  40920. +static inline void dwc_otg_hcd_qtd_remove_and_free(dwc_otg_hcd_t * hcd,
  40921. + dwc_otg_qtd_t * qtd,
  40922. + dwc_otg_qh_t * qh)
  40923. +{
  40924. + dwc_otg_hcd_qtd_remove(hcd, qtd, qh);
  40925. + dwc_otg_hcd_qtd_free(qtd);
  40926. +}
  40927. +
  40928. +/** @} */
  40929. +
  40930. +/** @name Descriptor DMA Supporting Functions */
  40931. +/** @{ */
  40932. +
  40933. +extern void dwc_otg_hcd_start_xfer_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  40934. +extern void dwc_otg_hcd_complete_xfer_ddma(dwc_otg_hcd_t * hcd,
  40935. + dwc_hc_t * hc,
  40936. + dwc_otg_hc_regs_t * hc_regs,
  40937. + dwc_otg_halt_status_e halt_status);
  40938. +
  40939. +extern int dwc_otg_hcd_qh_init_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  40940. +extern void dwc_otg_hcd_qh_free_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  40941. +
  40942. +/** @} */
  40943. +
  40944. +/** @name Internal Functions */
  40945. +/** @{ */
  40946. +dwc_otg_qh_t *dwc_urb_to_qh(dwc_otg_hcd_urb_t * urb);
  40947. +/** @} */
  40948. +
  40949. +#ifdef CONFIG_USB_DWC_OTG_LPM
  40950. +extern int dwc_otg_hcd_get_hc_for_lpm_tran(dwc_otg_hcd_t * hcd,
  40951. + uint8_t devaddr);
  40952. +extern void dwc_otg_hcd_free_hc_from_lpm(dwc_otg_hcd_t * hcd);
  40953. +#endif
  40954. +
  40955. +/** Gets the QH that contains the list_head */
  40956. +#define dwc_list_to_qh(_list_head_ptr_) container_of(_list_head_ptr_, dwc_otg_qh_t, qh_list_entry)
  40957. +
  40958. +/** Gets the QTD that contains the list_head */
  40959. +#define dwc_list_to_qtd(_list_head_ptr_) container_of(_list_head_ptr_, dwc_otg_qtd_t, qtd_list_entry)
  40960. +
  40961. +/** Check if QH is non-periodic */
  40962. +#define dwc_qh_is_non_per(_qh_ptr_) ((_qh_ptr_->ep_type == UE_BULK) || \
  40963. + (_qh_ptr_->ep_type == UE_CONTROL))
  40964. +
  40965. +/** High bandwidth multiplier as encoded in highspeed endpoint descriptors */
  40966. +#define dwc_hb_mult(wMaxPacketSize) (1 + (((wMaxPacketSize) >> 11) & 0x03))
  40967. +
  40968. +/** Packet size for any kind of endpoint descriptor */
  40969. +#define dwc_max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
  40970. +
  40971. +/**
  40972. + * Returns true if _frame1 is less than or equal to _frame2. The comparison is
  40973. + * done modulo DWC_HFNUM_MAX_FRNUM. This accounts for the rollover of the
  40974. + * frame number when the max frame number is reached.
  40975. + */
  40976. +static inline int dwc_frame_num_le(uint16_t frame1, uint16_t frame2)
  40977. +{
  40978. + return ((frame2 - frame1) & DWC_HFNUM_MAX_FRNUM) <=
  40979. + (DWC_HFNUM_MAX_FRNUM >> 1);
  40980. +}
  40981. +
  40982. +/**
  40983. + * Returns true if _frame1 is greater than _frame2. The comparison is done
  40984. + * modulo DWC_HFNUM_MAX_FRNUM. This accounts for the rollover of the frame
  40985. + * number when the max frame number is reached.
  40986. + */
  40987. +static inline int dwc_frame_num_gt(uint16_t frame1, uint16_t frame2)
  40988. +{
  40989. + return (frame1 != frame2) &&
  40990. + (((frame1 - frame2) & DWC_HFNUM_MAX_FRNUM) <
  40991. + (DWC_HFNUM_MAX_FRNUM >> 1));
  40992. +}
  40993. +
  40994. +/**
  40995. + * Increments _frame by the amount specified by _inc. The addition is done
  40996. + * modulo DWC_HFNUM_MAX_FRNUM. Returns the incremented value.
  40997. + */
  40998. +static inline uint16_t dwc_frame_num_inc(uint16_t frame, uint16_t inc)
  40999. +{
  41000. + return (frame + inc) & DWC_HFNUM_MAX_FRNUM;
  41001. +}
  41002. +
  41003. +static inline uint16_t dwc_full_frame_num(uint16_t frame)
  41004. +{
  41005. + return (frame & DWC_HFNUM_MAX_FRNUM) >> 3;
  41006. +}
  41007. +
  41008. +static inline uint16_t dwc_micro_frame_num(uint16_t frame)
  41009. +{
  41010. + return frame & 0x7;
  41011. +}
  41012. +
  41013. +extern void init_hcd_usecs(dwc_otg_hcd_t *_hcd);
  41014. +
  41015. +void dwc_otg_hcd_save_data_toggle(dwc_hc_t * hc,
  41016. + dwc_otg_hc_regs_t * hc_regs,
  41017. + dwc_otg_qtd_t * qtd);
  41018. +
  41019. +#ifdef DEBUG
  41020. +/**
  41021. + * Macro to sample the remaining PHY clocks left in the current frame. This
  41022. + * may be used during debugging to determine the average time it takes to
  41023. + * execute sections of code. There are two possible sample points, "a" and
  41024. + * "b", so the _letter argument must be one of these values.
  41025. + *
  41026. + * To dump the average sample times, read the "hcd_frrem" sysfs attribute. For
  41027. + * example, "cat /sys/devices/lm0/hcd_frrem".
  41028. + */
  41029. +#define dwc_sample_frrem(_hcd, _qh, _letter) \
  41030. +{ \
  41031. + hfnum_data_t hfnum; \
  41032. + dwc_otg_qtd_t *qtd; \
  41033. + qtd = list_entry(_qh->qtd_list.next, dwc_otg_qtd_t, qtd_list_entry); \
  41034. + if (usb_pipeint(qtd->urb->pipe) && _qh->start_split_frame != 0 && !qtd->complete_split) { \
  41035. + hfnum.d32 = DWC_READ_REG32(&_hcd->core_if->host_if->host_global_regs->hfnum); \
  41036. + switch (hfnum.b.frnum & 0x7) { \
  41037. + case 7: \
  41038. + _hcd->hfnum_7_samples_##_letter++; \
  41039. + _hcd->hfnum_7_frrem_accum_##_letter += hfnum.b.frrem; \
  41040. + break; \
  41041. + case 0: \
  41042. + _hcd->hfnum_0_samples_##_letter++; \
  41043. + _hcd->hfnum_0_frrem_accum_##_letter += hfnum.b.frrem; \
  41044. + break; \
  41045. + default: \
  41046. + _hcd->hfnum_other_samples_##_letter++; \
  41047. + _hcd->hfnum_other_frrem_accum_##_letter += hfnum.b.frrem; \
  41048. + break; \
  41049. + } \
  41050. + } \
  41051. +}
  41052. +#else
  41053. +#define dwc_sample_frrem(_hcd, _qh, _letter)
  41054. +#endif
  41055. +#endif
  41056. +#endif /* DWC_DEVICE_ONLY */
  41057. --- /dev/null
  41058. +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd_ddma.c
  41059. @@ -0,0 +1,1135 @@
  41060. +/*==========================================================================
  41061. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_ddma.c $
  41062. + * $Revision: #10 $
  41063. + * $Date: 2011/10/20 $
  41064. + * $Change: 1869464 $
  41065. + *
  41066. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  41067. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  41068. + * otherwise expressly agreed to in writing between Synopsys and you.
  41069. + *
  41070. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  41071. + * any End User Software License Agreement or Agreement for Licensed Product
  41072. + * with Synopsys or any supplement thereto. You are permitted to use and
  41073. + * redistribute this Software in source and binary forms, with or without
  41074. + * modification, provided that redistributions of source code must retain this
  41075. + * notice. You may not view, use, disclose, copy or distribute this file or
  41076. + * any information contained herein except pursuant to this license grant from
  41077. + * Synopsys. If you do not agree with this notice, including the disclaimer
  41078. + * below, then you are not authorized to use the Software.
  41079. + *
  41080. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  41081. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41082. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  41083. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  41084. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  41085. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  41086. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  41087. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  41088. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  41089. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  41090. + * DAMAGE.
  41091. + * ========================================================================== */
  41092. +#ifndef DWC_DEVICE_ONLY
  41093. +
  41094. +/** @file
  41095. + * This file contains Descriptor DMA support implementation for host mode.
  41096. + */
  41097. +
  41098. +#include "dwc_otg_hcd.h"
  41099. +#include "dwc_otg_regs.h"
  41100. +
  41101. +extern bool microframe_schedule;
  41102. +
  41103. +static inline uint8_t frame_list_idx(uint16_t frame)
  41104. +{
  41105. + return (frame & (MAX_FRLIST_EN_NUM - 1));
  41106. +}
  41107. +
  41108. +static inline uint16_t desclist_idx_inc(uint16_t idx, uint16_t inc, uint8_t speed)
  41109. +{
  41110. + return (idx + inc) &
  41111. + (((speed ==
  41112. + DWC_OTG_EP_SPEED_HIGH) ? MAX_DMA_DESC_NUM_HS_ISOC :
  41113. + MAX_DMA_DESC_NUM_GENERIC) - 1);
  41114. +}
  41115. +
  41116. +static inline uint16_t desclist_idx_dec(uint16_t idx, uint16_t inc, uint8_t speed)
  41117. +{
  41118. + return (idx - inc) &
  41119. + (((speed ==
  41120. + DWC_OTG_EP_SPEED_HIGH) ? MAX_DMA_DESC_NUM_HS_ISOC :
  41121. + MAX_DMA_DESC_NUM_GENERIC) - 1);
  41122. +}
  41123. +
  41124. +static inline uint16_t max_desc_num(dwc_otg_qh_t * qh)
  41125. +{
  41126. + return (((qh->ep_type == UE_ISOCHRONOUS)
  41127. + && (qh->dev_speed == DWC_OTG_EP_SPEED_HIGH))
  41128. + ? MAX_DMA_DESC_NUM_HS_ISOC : MAX_DMA_DESC_NUM_GENERIC);
  41129. +}
  41130. +static inline uint16_t frame_incr_val(dwc_otg_qh_t * qh)
  41131. +{
  41132. + return ((qh->dev_speed == DWC_OTG_EP_SPEED_HIGH)
  41133. + ? ((qh->interval + 8 - 1) / 8)
  41134. + : qh->interval);
  41135. +}
  41136. +
  41137. +static int desc_list_alloc(struct device *dev, dwc_otg_qh_t * qh)
  41138. +{
  41139. + int retval = 0;
  41140. +
  41141. + qh->desc_list = (dwc_otg_host_dma_desc_t *)
  41142. + DWC_DMA_ALLOC(dev, sizeof(dwc_otg_host_dma_desc_t) * max_desc_num(qh),
  41143. + &qh->desc_list_dma);
  41144. +
  41145. + if (!qh->desc_list) {
  41146. + retval = -DWC_E_NO_MEMORY;
  41147. + DWC_ERROR("%s: DMA descriptor list allocation failed\n", __func__);
  41148. +
  41149. + }
  41150. +
  41151. + dwc_memset(qh->desc_list, 0x00,
  41152. + sizeof(dwc_otg_host_dma_desc_t) * max_desc_num(qh));
  41153. +
  41154. + qh->n_bytes =
  41155. + (uint32_t *) DWC_ALLOC(sizeof(uint32_t) * max_desc_num(qh));
  41156. +
  41157. + if (!qh->n_bytes) {
  41158. + retval = -DWC_E_NO_MEMORY;
  41159. + DWC_ERROR
  41160. + ("%s: Failed to allocate array for descriptors' size actual values\n",
  41161. + __func__);
  41162. +
  41163. + }
  41164. + return retval;
  41165. +
  41166. +}
  41167. +
  41168. +static void desc_list_free(struct device *dev, dwc_otg_qh_t * qh)
  41169. +{
  41170. + if (qh->desc_list) {
  41171. + DWC_DMA_FREE(dev, max_desc_num(qh), qh->desc_list,
  41172. + qh->desc_list_dma);
  41173. + qh->desc_list = NULL;
  41174. + }
  41175. +
  41176. + if (qh->n_bytes) {
  41177. + DWC_FREE(qh->n_bytes);
  41178. + qh->n_bytes = NULL;
  41179. + }
  41180. +}
  41181. +
  41182. +static int frame_list_alloc(dwc_otg_hcd_t * hcd)
  41183. +{
  41184. + struct device *dev = dwc_otg_hcd_to_dev(hcd);
  41185. + int retval = 0;
  41186. +
  41187. + if (hcd->frame_list)
  41188. + return 0;
  41189. +
  41190. + hcd->frame_list = DWC_DMA_ALLOC(dev, 4 * MAX_FRLIST_EN_NUM,
  41191. + &hcd->frame_list_dma);
  41192. + if (!hcd->frame_list) {
  41193. + retval = -DWC_E_NO_MEMORY;
  41194. + DWC_ERROR("%s: Frame List allocation failed\n", __func__);
  41195. + }
  41196. +
  41197. + dwc_memset(hcd->frame_list, 0x00, 4 * MAX_FRLIST_EN_NUM);
  41198. +
  41199. + return retval;
  41200. +}
  41201. +
  41202. +static void frame_list_free(dwc_otg_hcd_t * hcd)
  41203. +{
  41204. + struct device *dev = dwc_otg_hcd_to_dev(hcd);
  41205. +
  41206. + if (!hcd->frame_list)
  41207. + return;
  41208. +
  41209. + DWC_DMA_FREE(dev, 4 * MAX_FRLIST_EN_NUM, hcd->frame_list, hcd->frame_list_dma);
  41210. + hcd->frame_list = NULL;
  41211. +}
  41212. +
  41213. +static void per_sched_enable(dwc_otg_hcd_t * hcd, uint16_t fr_list_en)
  41214. +{
  41215. +
  41216. + hcfg_data_t hcfg;
  41217. +
  41218. + hcfg.d32 = DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hcfg);
  41219. +
  41220. + if (hcfg.b.perschedena) {
  41221. + /* already enabled */
  41222. + return;
  41223. + }
  41224. +
  41225. + DWC_WRITE_REG32(&hcd->core_if->host_if->host_global_regs->hflbaddr,
  41226. + hcd->frame_list_dma);
  41227. +
  41228. + switch (fr_list_en) {
  41229. + case 64:
  41230. + hcfg.b.frlisten = 3;
  41231. + break;
  41232. + case 32:
  41233. + hcfg.b.frlisten = 2;
  41234. + break;
  41235. + case 16:
  41236. + hcfg.b.frlisten = 1;
  41237. + break;
  41238. + case 8:
  41239. + hcfg.b.frlisten = 0;
  41240. + break;
  41241. + default:
  41242. + break;
  41243. + }
  41244. +
  41245. + hcfg.b.perschedena = 1;
  41246. +
  41247. + DWC_DEBUGPL(DBG_HCD, "Enabling Periodic schedule\n");
  41248. + DWC_WRITE_REG32(&hcd->core_if->host_if->host_global_regs->hcfg, hcfg.d32);
  41249. +
  41250. +}
  41251. +
  41252. +static void per_sched_disable(dwc_otg_hcd_t * hcd)
  41253. +{
  41254. + hcfg_data_t hcfg;
  41255. +
  41256. + hcfg.d32 = DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hcfg);
  41257. +
  41258. + if (!hcfg.b.perschedena) {
  41259. + /* already disabled */
  41260. + return;
  41261. + }
  41262. + hcfg.b.perschedena = 0;
  41263. +
  41264. + DWC_DEBUGPL(DBG_HCD, "Disabling Periodic schedule\n");
  41265. + DWC_WRITE_REG32(&hcd->core_if->host_if->host_global_regs->hcfg, hcfg.d32);
  41266. +}
  41267. +
  41268. +/*
  41269. + * Activates/Deactivates FrameList entries for the channel
  41270. + * based on endpoint servicing period.
  41271. + */
  41272. +void update_frame_list(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh, uint8_t enable)
  41273. +{
  41274. + uint16_t i, j, inc;
  41275. + dwc_hc_t *hc = NULL;
  41276. +
  41277. + if (!qh->channel) {
  41278. + DWC_ERROR("qh->channel = %p", qh->channel);
  41279. + return;
  41280. + }
  41281. +
  41282. + if (!hcd) {
  41283. + DWC_ERROR("------hcd = %p", hcd);
  41284. + return;
  41285. + }
  41286. +
  41287. + if (!hcd->frame_list) {
  41288. + DWC_ERROR("-------hcd->frame_list = %p", hcd->frame_list);
  41289. + return;
  41290. + }
  41291. +
  41292. + hc = qh->channel;
  41293. + inc = frame_incr_val(qh);
  41294. + if (qh->ep_type == UE_ISOCHRONOUS)
  41295. + i = frame_list_idx(qh->sched_frame);
  41296. + else
  41297. + i = 0;
  41298. +
  41299. + j = i;
  41300. + do {
  41301. + if (enable)
  41302. + hcd->frame_list[j] |= (1 << hc->hc_num);
  41303. + else
  41304. + hcd->frame_list[j] &= ~(1 << hc->hc_num);
  41305. + j = (j + inc) & (MAX_FRLIST_EN_NUM - 1);
  41306. + }
  41307. + while (j != i);
  41308. + if (!enable)
  41309. + return;
  41310. + hc->schinfo = 0;
  41311. + if (qh->channel->speed == DWC_OTG_EP_SPEED_HIGH) {
  41312. + j = 1;
  41313. + /* TODO - check this */
  41314. + inc = (8 + qh->interval - 1) / qh->interval;
  41315. + for (i = 0; i < inc; i++) {
  41316. + hc->schinfo |= j;
  41317. + j = j << qh->interval;
  41318. + }
  41319. + } else {
  41320. + hc->schinfo = 0xff;
  41321. + }
  41322. +}
  41323. +
  41324. +#if 1
  41325. +void dump_frame_list(dwc_otg_hcd_t * hcd)
  41326. +{
  41327. + int i = 0;
  41328. + DWC_PRINTF("--FRAME LIST (hex) --\n");
  41329. + for (i = 0; i < MAX_FRLIST_EN_NUM; i++) {
  41330. + DWC_PRINTF("%x\t", hcd->frame_list[i]);
  41331. + if (!(i % 8) && i)
  41332. + DWC_PRINTF("\n");
  41333. + }
  41334. + DWC_PRINTF("\n----\n");
  41335. +
  41336. +}
  41337. +#endif
  41338. +
  41339. +static void release_channel_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  41340. +{
  41341. + dwc_hc_t *hc = qh->channel;
  41342. + if (dwc_qh_is_non_per(qh)) {
  41343. + if (!microframe_schedule)
  41344. + hcd->non_periodic_channels--;
  41345. + else
  41346. + hcd->available_host_channels++;
  41347. + } else
  41348. + update_frame_list(hcd, qh, 0);
  41349. +
  41350. + /*
  41351. + * The condition is added to prevent double cleanup try in case of device
  41352. + * disconnect. See channel cleanup in dwc_otg_hcd_disconnect_cb().
  41353. + */
  41354. + if (hc->qh) {
  41355. + dwc_otg_hc_cleanup(hcd->core_if, hc);
  41356. + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, hc, hc_list_entry);
  41357. + hc->qh = NULL;
  41358. + }
  41359. +
  41360. + qh->channel = NULL;
  41361. + qh->ntd = 0;
  41362. +
  41363. + if (qh->desc_list) {
  41364. + dwc_memset(qh->desc_list, 0x00,
  41365. + sizeof(dwc_otg_host_dma_desc_t) * max_desc_num(qh));
  41366. + }
  41367. +}
  41368. +
  41369. +/**
  41370. + * Initializes a QH structure's Descriptor DMA related members.
  41371. + * Allocates memory for descriptor list.
  41372. + * On first periodic QH, allocates memory for FrameList
  41373. + * and enables periodic scheduling.
  41374. + *
  41375. + * @param hcd The HCD state structure for the DWC OTG controller.
  41376. + * @param qh The QH to init.
  41377. + *
  41378. + * @return 0 if successful, negative error code otherwise.
  41379. + */
  41380. +int dwc_otg_hcd_qh_init_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  41381. +{
  41382. + struct device *dev = dwc_otg_hcd_to_dev(hcd);
  41383. + int retval = 0;
  41384. +
  41385. + if (qh->do_split) {
  41386. + DWC_ERROR("SPLIT Transfers are not supported in Descriptor DMA.\n");
  41387. + return -1;
  41388. + }
  41389. +
  41390. + retval = desc_list_alloc(dev, qh);
  41391. +
  41392. + if ((retval == 0)
  41393. + && (qh->ep_type == UE_ISOCHRONOUS || qh->ep_type == UE_INTERRUPT)) {
  41394. + if (!hcd->frame_list) {
  41395. + retval = frame_list_alloc(hcd);
  41396. + /* Enable periodic schedule on first periodic QH */
  41397. + if (retval == 0)
  41398. + per_sched_enable(hcd, MAX_FRLIST_EN_NUM);
  41399. + }
  41400. + }
  41401. +
  41402. + qh->ntd = 0;
  41403. +
  41404. + return retval;
  41405. +}
  41406. +
  41407. +/**
  41408. + * Frees descriptor list memory associated with the QH.
  41409. + * If QH is periodic and the last, frees FrameList memory
  41410. + * and disables periodic scheduling.
  41411. + *
  41412. + * @param hcd The HCD state structure for the DWC OTG controller.
  41413. + * @param qh The QH to init.
  41414. + */
  41415. +void dwc_otg_hcd_qh_free_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  41416. +{
  41417. + struct device *dev = dwc_otg_hcd_to_dev(hcd);
  41418. +
  41419. + desc_list_free(dev, qh);
  41420. +
  41421. + /*
  41422. + * Channel still assigned due to some reasons.
  41423. + * Seen on Isoc URB dequeue. Channel halted but no subsequent
  41424. + * ChHalted interrupt to release the channel. Afterwards
  41425. + * when it comes here from endpoint disable routine
  41426. + * channel remains assigned.
  41427. + */
  41428. + if (qh->channel)
  41429. + release_channel_ddma(hcd, qh);
  41430. +
  41431. + if ((qh->ep_type == UE_ISOCHRONOUS || qh->ep_type == UE_INTERRUPT)
  41432. + && (microframe_schedule || !hcd->periodic_channels) && hcd->frame_list) {
  41433. +
  41434. + per_sched_disable(hcd);
  41435. + frame_list_free(hcd);
  41436. + }
  41437. +}
  41438. +
  41439. +static uint8_t frame_to_desc_idx(dwc_otg_qh_t * qh, uint16_t frame_idx)
  41440. +{
  41441. + if (qh->dev_speed == DWC_OTG_EP_SPEED_HIGH) {
  41442. + /*
  41443. + * Descriptor set(8 descriptors) index
  41444. + * which is 8-aligned.
  41445. + */
  41446. + return (frame_idx & ((MAX_DMA_DESC_NUM_HS_ISOC / 8) - 1)) * 8;
  41447. + } else {
  41448. + return (frame_idx & (MAX_DMA_DESC_NUM_GENERIC - 1));
  41449. + }
  41450. +}
  41451. +
  41452. +/*
  41453. + * Determine starting frame for Isochronous transfer.
  41454. + * Few frames skipped to prevent race condition with HC.
  41455. + */
  41456. +static uint8_t calc_starting_frame(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
  41457. + uint8_t * skip_frames)
  41458. +{
  41459. + uint16_t frame = 0;
  41460. + hcd->frame_number = dwc_otg_hcd_get_frame_number(hcd);
  41461. +
  41462. + /* sched_frame is always frame number(not uFrame) both in FS and HS !! */
  41463. +
  41464. + /*
  41465. + * skip_frames is used to limit activated descriptors number
  41466. + * to avoid the situation when HC services the last activated
  41467. + * descriptor firstly.
  41468. + * Example for FS:
  41469. + * Current frame is 1, scheduled frame is 3. Since HC always fetches the descriptor
  41470. + * corresponding to curr_frame+1, the descriptor corresponding to frame 2
  41471. + * will be fetched. If the number of descriptors is max=64 (or greather) the
  41472. + * list will be fully programmed with Active descriptors and it is possible
  41473. + * case(rare) that the latest descriptor(considering rollback) corresponding
  41474. + * to frame 2 will be serviced first. HS case is more probable because, in fact,
  41475. + * up to 11 uframes(16 in the code) may be skipped.
  41476. + */
  41477. + if (qh->dev_speed == DWC_OTG_EP_SPEED_HIGH) {
  41478. + /*
  41479. + * Consider uframe counter also, to start xfer asap.
  41480. + * If half of the frame elapsed skip 2 frames otherwise
  41481. + * just 1 frame.
  41482. + * Starting descriptor index must be 8-aligned, so
  41483. + * if the current frame is near to complete the next one
  41484. + * is skipped as well.
  41485. + */
  41486. +
  41487. + if (dwc_micro_frame_num(hcd->frame_number) >= 5) {
  41488. + *skip_frames = 2 * 8;
  41489. + frame = dwc_frame_num_inc(hcd->frame_number, *skip_frames);
  41490. + } else {
  41491. + *skip_frames = 1 * 8;
  41492. + frame = dwc_frame_num_inc(hcd->frame_number, *skip_frames);
  41493. + }
  41494. +
  41495. + frame = dwc_full_frame_num(frame);
  41496. + } else {
  41497. + /*
  41498. + * Two frames are skipped for FS - the current and the next.
  41499. + * But for descriptor programming, 1 frame(descriptor) is enough,
  41500. + * see example above.
  41501. + */
  41502. + *skip_frames = 1;
  41503. + frame = dwc_frame_num_inc(hcd->frame_number, 2);
  41504. + }
  41505. +
  41506. + return frame;
  41507. +}
  41508. +
  41509. +/*
  41510. + * Calculate initial descriptor index for isochronous transfer
  41511. + * based on scheduled frame.
  41512. + */
  41513. +static uint8_t recalc_initial_desc_idx(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  41514. +{
  41515. + uint16_t frame = 0, fr_idx, fr_idx_tmp;
  41516. + uint8_t skip_frames = 0;
  41517. + /*
  41518. + * With current ISOC processing algorithm the channel is being
  41519. + * released when no more QTDs in the list(qh->ntd == 0).
  41520. + * Thus this function is called only when qh->ntd == 0 and qh->channel == 0.
  41521. + *
  41522. + * So qh->channel != NULL branch is not used and just not removed from the
  41523. + * source file. It is required for another possible approach which is,
  41524. + * do not disable and release the channel when ISOC session completed,
  41525. + * just move QH to inactive schedule until new QTD arrives.
  41526. + * On new QTD, the QH moved back to 'ready' schedule,
  41527. + * starting frame and therefore starting desc_index are recalculated.
  41528. + * In this case channel is released only on ep_disable.
  41529. + */
  41530. +
  41531. + /* Calculate starting descriptor index. For INTERRUPT endpoint it is always 0. */
  41532. + if (qh->channel) {
  41533. + frame = calc_starting_frame(hcd, qh, &skip_frames);
  41534. + /*
  41535. + * Calculate initial descriptor index based on FrameList current bitmap
  41536. + * and servicing period.
  41537. + */
  41538. + fr_idx_tmp = frame_list_idx(frame);
  41539. + fr_idx =
  41540. + (MAX_FRLIST_EN_NUM + frame_list_idx(qh->sched_frame) -
  41541. + fr_idx_tmp)
  41542. + % frame_incr_val(qh);
  41543. + fr_idx = (fr_idx + fr_idx_tmp) % MAX_FRLIST_EN_NUM;
  41544. + } else {
  41545. + qh->sched_frame = calc_starting_frame(hcd, qh, &skip_frames);
  41546. + fr_idx = frame_list_idx(qh->sched_frame);
  41547. + }
  41548. +
  41549. + qh->td_first = qh->td_last = frame_to_desc_idx(qh, fr_idx);
  41550. +
  41551. + return skip_frames;
  41552. +}
  41553. +
  41554. +#define ISOC_URB_GIVEBACK_ASAP
  41555. +
  41556. +#define MAX_ISOC_XFER_SIZE_FS 1023
  41557. +#define MAX_ISOC_XFER_SIZE_HS 3072
  41558. +#define DESCNUM_THRESHOLD 4
  41559. +
  41560. +static void init_isoc_dma_desc(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
  41561. + uint8_t skip_frames)
  41562. +{
  41563. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  41564. + dwc_otg_qtd_t *qtd;
  41565. + dwc_otg_host_dma_desc_t *dma_desc;
  41566. + uint16_t idx, inc, n_desc, ntd_max, max_xfer_size;
  41567. +
  41568. + idx = qh->td_last;
  41569. + inc = qh->interval;
  41570. + n_desc = 0;
  41571. +
  41572. + ntd_max = (max_desc_num(qh) + qh->interval - 1) / qh->interval;
  41573. + if (skip_frames && !qh->channel)
  41574. + ntd_max = ntd_max - skip_frames / qh->interval;
  41575. +
  41576. + max_xfer_size =
  41577. + (qh->dev_speed ==
  41578. + DWC_OTG_EP_SPEED_HIGH) ? MAX_ISOC_XFER_SIZE_HS :
  41579. + MAX_ISOC_XFER_SIZE_FS;
  41580. +
  41581. + DWC_CIRCLEQ_FOREACH(qtd, &qh->qtd_list, qtd_list_entry) {
  41582. + while ((qh->ntd < ntd_max)
  41583. + && (qtd->isoc_frame_index_last <
  41584. + qtd->urb->packet_count)) {
  41585. +
  41586. + dma_desc = &qh->desc_list[idx];
  41587. + dwc_memset(dma_desc, 0x00, sizeof(dwc_otg_host_dma_desc_t));
  41588. +
  41589. + frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index_last];
  41590. +
  41591. + if (frame_desc->length > max_xfer_size)
  41592. + qh->n_bytes[idx] = max_xfer_size;
  41593. + else
  41594. + qh->n_bytes[idx] = frame_desc->length;
  41595. + dma_desc->status.b_isoc.n_bytes = qh->n_bytes[idx];
  41596. + dma_desc->status.b_isoc.a = 1;
  41597. + dma_desc->status.b_isoc.sts = 0;
  41598. +
  41599. + dma_desc->buf = qtd->urb->dma + frame_desc->offset;
  41600. +
  41601. + qh->ntd++;
  41602. +
  41603. + qtd->isoc_frame_index_last++;
  41604. +
  41605. +#ifdef ISOC_URB_GIVEBACK_ASAP
  41606. + /*
  41607. + * Set IOC for each descriptor corresponding to the
  41608. + * last frame of the URB.
  41609. + */
  41610. + if (qtd->isoc_frame_index_last ==
  41611. + qtd->urb->packet_count)
  41612. + dma_desc->status.b_isoc.ioc = 1;
  41613. +
  41614. +#endif
  41615. + idx = desclist_idx_inc(idx, inc, qh->dev_speed);
  41616. + n_desc++;
  41617. +
  41618. + }
  41619. + qtd->in_process = 1;
  41620. + }
  41621. +
  41622. + qh->td_last = idx;
  41623. +
  41624. +#ifdef ISOC_URB_GIVEBACK_ASAP
  41625. + /* Set IOC for the last descriptor if descriptor list is full */
  41626. + if (qh->ntd == ntd_max) {
  41627. + idx = desclist_idx_dec(qh->td_last, inc, qh->dev_speed);
  41628. + qh->desc_list[idx].status.b_isoc.ioc = 1;
  41629. + }
  41630. +#else
  41631. + /*
  41632. + * Set IOC bit only for one descriptor.
  41633. + * Always try to be ahead of HW processing,
  41634. + * i.e. on IOC generation driver activates next descriptors but
  41635. + * core continues to process descriptors followed the one with IOC set.
  41636. + */
  41637. +
  41638. + if (n_desc > DESCNUM_THRESHOLD) {
  41639. + /*
  41640. + * Move IOC "up". Required even if there is only one QTD
  41641. + * in the list, cause QTDs migth continue to be queued,
  41642. + * but during the activation it was only one queued.
  41643. + * Actually more than one QTD might be in the list if this function called
  41644. + * from XferCompletion - QTDs was queued during HW processing of the previous
  41645. + * descriptor chunk.
  41646. + */
  41647. + idx = dwc_desclist_idx_dec(idx, inc * ((qh->ntd + 1) / 2), qh->dev_speed);
  41648. + } else {
  41649. + /*
  41650. + * Set the IOC for the latest descriptor
  41651. + * if either number of descriptor is not greather than threshold
  41652. + * or no more new descriptors activated.
  41653. + */
  41654. + idx = dwc_desclist_idx_dec(qh->td_last, inc, qh->dev_speed);
  41655. + }
  41656. +
  41657. + qh->desc_list[idx].status.b_isoc.ioc = 1;
  41658. +#endif
  41659. +}
  41660. +
  41661. +static void init_non_isoc_dma_desc(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  41662. +{
  41663. +
  41664. + dwc_hc_t *hc;
  41665. + dwc_otg_host_dma_desc_t *dma_desc;
  41666. + dwc_otg_qtd_t *qtd;
  41667. + int num_packets, len, n_desc = 0;
  41668. +
  41669. + hc = qh->channel;
  41670. +
  41671. + /*
  41672. + * Start with hc->xfer_buff initialized in
  41673. + * assign_and_init_hc(), then if SG transfer consists of multiple URBs,
  41674. + * this pointer re-assigned to the buffer of the currently processed QTD.
  41675. + * For non-SG request there is always one QTD active.
  41676. + */
  41677. +
  41678. + DWC_CIRCLEQ_FOREACH(qtd, &qh->qtd_list, qtd_list_entry) {
  41679. +
  41680. + if (n_desc) {
  41681. + /* SG request - more than 1 QTDs */
  41682. + hc->xfer_buff = (uint8_t *)(uintptr_t)qtd->urb->dma +
  41683. + qtd->urb->actual_length;
  41684. + hc->xfer_len = qtd->urb->length - qtd->urb->actual_length;
  41685. + }
  41686. +
  41687. + qtd->n_desc = 0;
  41688. +
  41689. + do {
  41690. + dma_desc = &qh->desc_list[n_desc];
  41691. + len = hc->xfer_len;
  41692. +
  41693. + if (len > MAX_DMA_DESC_SIZE)
  41694. + len = MAX_DMA_DESC_SIZE - hc->max_packet + 1;
  41695. +
  41696. + if (hc->ep_is_in) {
  41697. + if (len > 0) {
  41698. + num_packets = (len + hc->max_packet - 1) / hc->max_packet;
  41699. + } else {
  41700. + /* Need 1 packet for transfer length of 0. */
  41701. + num_packets = 1;
  41702. + }
  41703. + /* Always program an integral # of max packets for IN transfers. */
  41704. + len = num_packets * hc->max_packet;
  41705. + }
  41706. +
  41707. + dma_desc->status.b.n_bytes = len;
  41708. +
  41709. + qh->n_bytes[n_desc] = len;
  41710. +
  41711. + if ((qh->ep_type == UE_CONTROL)
  41712. + && (qtd->control_phase == DWC_OTG_CONTROL_SETUP))
  41713. + dma_desc->status.b.sup = 1; /* Setup Packet */
  41714. +
  41715. + dma_desc->status.b.a = 1; /* Active descriptor */
  41716. + dma_desc->status.b.sts = 0;
  41717. +
  41718. + dma_desc->buf =
  41719. + ((unsigned long)hc->xfer_buff & 0xffffffff);
  41720. +
  41721. + /*
  41722. + * Last descriptor(or single) of IN transfer
  41723. + * with actual size less than MaxPacket.
  41724. + */
  41725. + if (len > hc->xfer_len) {
  41726. + hc->xfer_len = 0;
  41727. + } else {
  41728. + hc->xfer_buff += len;
  41729. + hc->xfer_len -= len;
  41730. + }
  41731. +
  41732. + qtd->n_desc++;
  41733. + n_desc++;
  41734. + }
  41735. + while ((hc->xfer_len > 0) && (n_desc != MAX_DMA_DESC_NUM_GENERIC));
  41736. +
  41737. +
  41738. + qtd->in_process = 1;
  41739. +
  41740. + if (qh->ep_type == UE_CONTROL)
  41741. + break;
  41742. +
  41743. + if (n_desc == MAX_DMA_DESC_NUM_GENERIC)
  41744. + break;
  41745. + }
  41746. +
  41747. + if (n_desc) {
  41748. + /* Request Transfer Complete interrupt for the last descriptor */
  41749. + qh->desc_list[n_desc - 1].status.b.ioc = 1;
  41750. + /* End of List indicator */
  41751. + qh->desc_list[n_desc - 1].status.b.eol = 1;
  41752. +
  41753. + hc->ntd = n_desc;
  41754. + }
  41755. +}
  41756. +
  41757. +/**
  41758. + * For Control and Bulk endpoints initializes descriptor list
  41759. + * and starts the transfer.
  41760. + *
  41761. + * For Interrupt and Isochronous endpoints initializes descriptor list
  41762. + * then updates FrameList, marking appropriate entries as active.
  41763. + * In case of Isochronous, the starting descriptor index is calculated based
  41764. + * on the scheduled frame, but only on the first transfer descriptor within a session.
  41765. + * Then starts the transfer via enabling the channel.
  41766. + * For Isochronous endpoint the channel is not halted on XferComplete
  41767. + * interrupt so remains assigned to the endpoint(QH) until session is done.
  41768. + *
  41769. + * @param hcd The HCD state structure for the DWC OTG controller.
  41770. + * @param qh The QH to init.
  41771. + *
  41772. + * @return 0 if successful, negative error code otherwise.
  41773. + */
  41774. +void dwc_otg_hcd_start_xfer_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  41775. +{
  41776. + /* Channel is already assigned */
  41777. + dwc_hc_t *hc = qh->channel;
  41778. + uint8_t skip_frames = 0;
  41779. +
  41780. + switch (hc->ep_type) {
  41781. + case DWC_OTG_EP_TYPE_CONTROL:
  41782. + case DWC_OTG_EP_TYPE_BULK:
  41783. + init_non_isoc_dma_desc(hcd, qh);
  41784. +
  41785. + dwc_otg_hc_start_transfer_ddma(hcd->core_if, hc);
  41786. + break;
  41787. + case DWC_OTG_EP_TYPE_INTR:
  41788. + init_non_isoc_dma_desc(hcd, qh);
  41789. +
  41790. + update_frame_list(hcd, qh, 1);
  41791. +
  41792. + dwc_otg_hc_start_transfer_ddma(hcd->core_if, hc);
  41793. + break;
  41794. + case DWC_OTG_EP_TYPE_ISOC:
  41795. +
  41796. + if (!qh->ntd)
  41797. + skip_frames = recalc_initial_desc_idx(hcd, qh);
  41798. +
  41799. + init_isoc_dma_desc(hcd, qh, skip_frames);
  41800. +
  41801. + if (!hc->xfer_started) {
  41802. +
  41803. + update_frame_list(hcd, qh, 1);
  41804. +
  41805. + /*
  41806. + * Always set to max, instead of actual size.
  41807. + * Otherwise ntd will be changed with
  41808. + * channel being enabled. Not recommended.
  41809. + *
  41810. + */
  41811. + hc->ntd = max_desc_num(qh);
  41812. + /* Enable channel only once for ISOC */
  41813. + dwc_otg_hc_start_transfer_ddma(hcd->core_if, hc);
  41814. + }
  41815. +
  41816. + break;
  41817. + default:
  41818. +
  41819. + break;
  41820. + }
  41821. +}
  41822. +
  41823. +static void complete_isoc_xfer_ddma(dwc_otg_hcd_t * hcd,
  41824. + dwc_hc_t * hc,
  41825. + dwc_otg_hc_regs_t * hc_regs,
  41826. + dwc_otg_halt_status_e halt_status)
  41827. +{
  41828. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  41829. + dwc_otg_qtd_t *qtd, *qtd_tmp;
  41830. + dwc_otg_qh_t *qh;
  41831. + dwc_otg_host_dma_desc_t *dma_desc;
  41832. + uint16_t idx, remain;
  41833. + uint8_t urb_compl;
  41834. +
  41835. + qh = hc->qh;
  41836. + idx = qh->td_first;
  41837. +
  41838. + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
  41839. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry)
  41840. + qtd->in_process = 0;
  41841. + return;
  41842. + } else if ((halt_status == DWC_OTG_HC_XFER_AHB_ERR) ||
  41843. + (halt_status == DWC_OTG_HC_XFER_BABBLE_ERR)) {
  41844. + /*
  41845. + * Channel is halted in these error cases.
  41846. + * Considered as serious issues.
  41847. + * Complete all URBs marking all frames as failed,
  41848. + * irrespective whether some of the descriptors(frames) succeeded or no.
  41849. + * Pass error code to completion routine as well, to
  41850. + * update urb->status, some of class drivers might use it to stop
  41851. + * queing transfer requests.
  41852. + */
  41853. + int err = (halt_status == DWC_OTG_HC_XFER_AHB_ERR)
  41854. + ? (-DWC_E_IO)
  41855. + : (-DWC_E_OVERFLOW);
  41856. +
  41857. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry) {
  41858. + for (idx = 0; idx < qtd->urb->packet_count; idx++) {
  41859. + frame_desc = &qtd->urb->iso_descs[idx];
  41860. + frame_desc->status = err;
  41861. + }
  41862. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, err);
  41863. + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
  41864. + }
  41865. + return;
  41866. + }
  41867. +
  41868. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry) {
  41869. +
  41870. + if (!qtd->in_process)
  41871. + break;
  41872. +
  41873. + urb_compl = 0;
  41874. +
  41875. + do {
  41876. +
  41877. + dma_desc = &qh->desc_list[idx];
  41878. +
  41879. + frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  41880. + remain = hc->ep_is_in ? dma_desc->status.b_isoc.n_bytes : 0;
  41881. +
  41882. + if (dma_desc->status.b_isoc.sts == DMA_DESC_STS_PKTERR) {
  41883. + /*
  41884. + * XactError or, unable to complete all the transactions
  41885. + * in the scheduled micro-frame/frame,
  41886. + * both indicated by DMA_DESC_STS_PKTERR.
  41887. + */
  41888. + qtd->urb->error_count++;
  41889. + frame_desc->actual_length = qh->n_bytes[idx] - remain;
  41890. + frame_desc->status = -DWC_E_PROTOCOL;
  41891. + } else {
  41892. + /* Success */
  41893. +
  41894. + frame_desc->actual_length = qh->n_bytes[idx] - remain;
  41895. + frame_desc->status = 0;
  41896. + }
  41897. +
  41898. + if (++qtd->isoc_frame_index == qtd->urb->packet_count) {
  41899. + /*
  41900. + * urb->status is not used for isoc transfers here.
  41901. + * The individual frame_desc status are used instead.
  41902. + */
  41903. +
  41904. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  41905. + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
  41906. +
  41907. + /*
  41908. + * This check is necessary because urb_dequeue can be called
  41909. + * from urb complete callback(sound driver example).
  41910. + * All pending URBs are dequeued there, so no need for
  41911. + * further processing.
  41912. + */
  41913. + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
  41914. + return;
  41915. + }
  41916. +
  41917. + urb_compl = 1;
  41918. +
  41919. + }
  41920. +
  41921. + qh->ntd--;
  41922. +
  41923. + /* Stop if IOC requested descriptor reached */
  41924. + if (dma_desc->status.b_isoc.ioc) {
  41925. + idx = desclist_idx_inc(idx, qh->interval, hc->speed);
  41926. + goto stop_scan;
  41927. + }
  41928. +
  41929. + idx = desclist_idx_inc(idx, qh->interval, hc->speed);
  41930. +
  41931. + if (urb_compl)
  41932. + break;
  41933. + }
  41934. + while (idx != qh->td_first);
  41935. + }
  41936. +stop_scan:
  41937. + qh->td_first = idx;
  41938. +}
  41939. +
  41940. +uint8_t update_non_isoc_urb_state_ddma(dwc_otg_hcd_t * hcd,
  41941. + dwc_hc_t * hc,
  41942. + dwc_otg_qtd_t * qtd,
  41943. + dwc_otg_host_dma_desc_t * dma_desc,
  41944. + dwc_otg_halt_status_e halt_status,
  41945. + uint32_t n_bytes, uint8_t * xfer_done)
  41946. +{
  41947. +
  41948. + uint16_t remain = hc->ep_is_in ? dma_desc->status.b.n_bytes : 0;
  41949. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  41950. +
  41951. + if (halt_status == DWC_OTG_HC_XFER_AHB_ERR) {
  41952. + urb->status = -DWC_E_IO;
  41953. + return 1;
  41954. + }
  41955. + if (dma_desc->status.b.sts == DMA_DESC_STS_PKTERR) {
  41956. + switch (halt_status) {
  41957. + case DWC_OTG_HC_XFER_STALL:
  41958. + urb->status = -DWC_E_PIPE;
  41959. + break;
  41960. + case DWC_OTG_HC_XFER_BABBLE_ERR:
  41961. + urb->status = -DWC_E_OVERFLOW;
  41962. + break;
  41963. + case DWC_OTG_HC_XFER_XACT_ERR:
  41964. + urb->status = -DWC_E_PROTOCOL;
  41965. + break;
  41966. + default:
  41967. + DWC_ERROR("%s: Unhandled descriptor error status (%d)\n", __func__,
  41968. + halt_status);
  41969. + break;
  41970. + }
  41971. + return 1;
  41972. + }
  41973. +
  41974. + if (dma_desc->status.b.a == 1) {
  41975. + DWC_DEBUGPL(DBG_HCDV,
  41976. + "Active descriptor encountered on channel %d\n",
  41977. + hc->hc_num);
  41978. + return 0;
  41979. + }
  41980. +
  41981. + if (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL) {
  41982. + if (qtd->control_phase == DWC_OTG_CONTROL_DATA) {
  41983. + urb->actual_length += n_bytes - remain;
  41984. + if (remain || urb->actual_length == urb->length) {
  41985. + /*
  41986. + * For Control Data stage do not set urb->status=0 to prevent
  41987. + * URB callback. Set it when Status phase done. See below.
  41988. + */
  41989. + *xfer_done = 1;
  41990. + }
  41991. +
  41992. + } else if (qtd->control_phase == DWC_OTG_CONTROL_STATUS) {
  41993. + urb->status = 0;
  41994. + *xfer_done = 1;
  41995. + }
  41996. + /* No handling for SETUP stage */
  41997. + } else {
  41998. + /* BULK and INTR */
  41999. + urb->actual_length += n_bytes - remain;
  42000. + if (remain || urb->actual_length == urb->length) {
  42001. + urb->status = 0;
  42002. + *xfer_done = 1;
  42003. + }
  42004. + }
  42005. +
  42006. + return 0;
  42007. +}
  42008. +
  42009. +static void complete_non_isoc_xfer_ddma(dwc_otg_hcd_t * hcd,
  42010. + dwc_hc_t * hc,
  42011. + dwc_otg_hc_regs_t * hc_regs,
  42012. + dwc_otg_halt_status_e halt_status)
  42013. +{
  42014. + dwc_otg_hcd_urb_t *urb = NULL;
  42015. + dwc_otg_qtd_t *qtd, *qtd_tmp;
  42016. + dwc_otg_qh_t *qh;
  42017. + dwc_otg_host_dma_desc_t *dma_desc;
  42018. + uint32_t n_bytes, n_desc, i;
  42019. + uint8_t failed = 0, xfer_done;
  42020. +
  42021. + n_desc = 0;
  42022. +
  42023. + qh = hc->qh;
  42024. +
  42025. + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
  42026. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry) {
  42027. + qtd->in_process = 0;
  42028. + }
  42029. + return;
  42030. + }
  42031. +
  42032. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry) {
  42033. +
  42034. + urb = qtd->urb;
  42035. +
  42036. + n_bytes = 0;
  42037. + xfer_done = 0;
  42038. +
  42039. + for (i = 0; i < qtd->n_desc; i++) {
  42040. + dma_desc = &qh->desc_list[n_desc];
  42041. +
  42042. + n_bytes = qh->n_bytes[n_desc];
  42043. +
  42044. + failed =
  42045. + update_non_isoc_urb_state_ddma(hcd, hc, qtd,
  42046. + dma_desc,
  42047. + halt_status, n_bytes,
  42048. + &xfer_done);
  42049. +
  42050. + if (failed
  42051. + || (xfer_done
  42052. + && (urb->status != -DWC_E_IN_PROGRESS))) {
  42053. +
  42054. + hcd->fops->complete(hcd, urb->priv, urb,
  42055. + urb->status);
  42056. + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
  42057. +
  42058. + if (failed)
  42059. + goto stop_scan;
  42060. + } else if (qh->ep_type == UE_CONTROL) {
  42061. + if (qtd->control_phase == DWC_OTG_CONTROL_SETUP) {
  42062. + if (urb->length > 0) {
  42063. + qtd->control_phase = DWC_OTG_CONTROL_DATA;
  42064. + } else {
  42065. + qtd->control_phase = DWC_OTG_CONTROL_STATUS;
  42066. + }
  42067. + DWC_DEBUGPL(DBG_HCDV, " Control setup transaction done\n");
  42068. + } else if (qtd->control_phase == DWC_OTG_CONTROL_DATA) {
  42069. + if (xfer_done) {
  42070. + qtd->control_phase = DWC_OTG_CONTROL_STATUS;
  42071. + DWC_DEBUGPL(DBG_HCDV, " Control data transfer done\n");
  42072. + } else if (i + 1 == qtd->n_desc) {
  42073. + /*
  42074. + * Last descriptor for Control data stage which is
  42075. + * not completed yet.
  42076. + */
  42077. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  42078. + }
  42079. + }
  42080. + }
  42081. +
  42082. + n_desc++;
  42083. + }
  42084. +
  42085. + }
  42086. +
  42087. +stop_scan:
  42088. +
  42089. + if (qh->ep_type != UE_CONTROL) {
  42090. + /*
  42091. + * Resetting the data toggle for bulk
  42092. + * and interrupt endpoints in case of stall. See handle_hc_stall_intr()
  42093. + */
  42094. + if (halt_status == DWC_OTG_HC_XFER_STALL)
  42095. + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
  42096. + else
  42097. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  42098. + }
  42099. +
  42100. + if (halt_status == DWC_OTG_HC_XFER_COMPLETE) {
  42101. + hcint_data_t hcint;
  42102. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  42103. + if (hcint.b.nyet) {
  42104. + /*
  42105. + * Got a NYET on the last transaction of the transfer. It
  42106. + * means that the endpoint should be in the PING state at the
  42107. + * beginning of the next transfer.
  42108. + */
  42109. + qh->ping_state = 1;
  42110. + clear_hc_int(hc_regs, nyet);
  42111. + }
  42112. +
  42113. + }
  42114. +
  42115. +}
  42116. +
  42117. +/**
  42118. + * This function is called from interrupt handlers.
  42119. + * Scans the descriptor list, updates URB's status and
  42120. + * calls completion routine for the URB if it's done.
  42121. + * Releases the channel to be used by other transfers.
  42122. + * In case of Isochronous endpoint the channel is not halted until
  42123. + * the end of the session, i.e. QTD list is empty.
  42124. + * If periodic channel released the FrameList is updated accordingly.
  42125. + *
  42126. + * Calls transaction selection routines to activate pending transfers.
  42127. + *
  42128. + * @param hcd The HCD state structure for the DWC OTG controller.
  42129. + * @param hc Host channel, the transfer is completed on.
  42130. + * @param hc_regs Host channel registers.
  42131. + * @param halt_status Reason the channel is being halted,
  42132. + * or just XferComplete for isochronous transfer
  42133. + */
  42134. +void dwc_otg_hcd_complete_xfer_ddma(dwc_otg_hcd_t * hcd,
  42135. + dwc_hc_t * hc,
  42136. + dwc_otg_hc_regs_t * hc_regs,
  42137. + dwc_otg_halt_status_e halt_status)
  42138. +{
  42139. + uint8_t continue_isoc_xfer = 0;
  42140. + dwc_otg_transaction_type_e tr_type;
  42141. + dwc_otg_qh_t *qh = hc->qh;
  42142. +
  42143. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  42144. +
  42145. + complete_isoc_xfer_ddma(hcd, hc, hc_regs, halt_status);
  42146. +
  42147. + /* Release the channel if halted or session completed */
  42148. + if (halt_status != DWC_OTG_HC_XFER_COMPLETE ||
  42149. + DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
  42150. +
  42151. + /* Halt the channel if session completed */
  42152. + if (halt_status == DWC_OTG_HC_XFER_COMPLETE) {
  42153. + dwc_otg_hc_halt(hcd->core_if, hc, halt_status);
  42154. + }
  42155. +
  42156. + release_channel_ddma(hcd, qh);
  42157. + dwc_otg_hcd_qh_remove(hcd, qh);
  42158. + } else {
  42159. + /* Keep in assigned schedule to continue transfer */
  42160. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_assigned,
  42161. + &qh->qh_list_entry);
  42162. + continue_isoc_xfer = 1;
  42163. +
  42164. + }
  42165. + /** @todo Consider the case when period exceeds FrameList size.
  42166. + * Frame Rollover interrupt should be used.
  42167. + */
  42168. + } else {
  42169. + /* Scan descriptor list to complete the URB(s), then release the channel */
  42170. + complete_non_isoc_xfer_ddma(hcd, hc, hc_regs, halt_status);
  42171. +
  42172. + release_channel_ddma(hcd, qh);
  42173. + dwc_otg_hcd_qh_remove(hcd, qh);
  42174. +
  42175. + if (!DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
  42176. + /* Add back to inactive non-periodic schedule on normal completion */
  42177. + dwc_otg_hcd_qh_add(hcd, qh);
  42178. + }
  42179. +
  42180. + }
  42181. + tr_type = dwc_otg_hcd_select_transactions(hcd);
  42182. + if (tr_type != DWC_OTG_TRANSACTION_NONE || continue_isoc_xfer) {
  42183. + if (continue_isoc_xfer) {
  42184. + if (tr_type == DWC_OTG_TRANSACTION_NONE) {
  42185. + tr_type = DWC_OTG_TRANSACTION_PERIODIC;
  42186. + } else if (tr_type == DWC_OTG_TRANSACTION_NON_PERIODIC) {
  42187. + tr_type = DWC_OTG_TRANSACTION_ALL;
  42188. + }
  42189. + }
  42190. + dwc_otg_hcd_queue_transactions(hcd, tr_type);
  42191. + }
  42192. +}
  42193. +
  42194. +#endif /* DWC_DEVICE_ONLY */
  42195. --- /dev/null
  42196. +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd_if.h
  42197. @@ -0,0 +1,421 @@
  42198. +/* ==========================================================================
  42199. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_if.h $
  42200. + * $Revision: #12 $
  42201. + * $Date: 2011/10/26 $
  42202. + * $Change: 1873028 $
  42203. + *
  42204. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  42205. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  42206. + * otherwise expressly agreed to in writing between Synopsys and you.
  42207. + *
  42208. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  42209. + * any End User Software License Agreement or Agreement for Licensed Product
  42210. + * with Synopsys or any supplement thereto. You are permitted to use and
  42211. + * redistribute this Software in source and binary forms, with or without
  42212. + * modification, provided that redistributions of source code must retain this
  42213. + * notice. You may not view, use, disclose, copy or distribute this file or
  42214. + * any information contained herein except pursuant to this license grant from
  42215. + * Synopsys. If you do not agree with this notice, including the disclaimer
  42216. + * below, then you are not authorized to use the Software.
  42217. + *
  42218. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  42219. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42220. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  42221. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  42222. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  42223. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  42224. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  42225. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  42226. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  42227. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  42228. + * DAMAGE.
  42229. + * ========================================================================== */
  42230. +#ifndef DWC_DEVICE_ONLY
  42231. +#ifndef __DWC_HCD_IF_H__
  42232. +#define __DWC_HCD_IF_H__
  42233. +
  42234. +#include "dwc_otg_core_if.h"
  42235. +
  42236. +/** @file
  42237. + * This file defines DWC_OTG HCD Core API.
  42238. + */
  42239. +
  42240. +struct dwc_otg_hcd;
  42241. +typedef struct dwc_otg_hcd dwc_otg_hcd_t;
  42242. +
  42243. +struct dwc_otg_hcd_urb;
  42244. +typedef struct dwc_otg_hcd_urb dwc_otg_hcd_urb_t;
  42245. +
  42246. +/** @name HCD Function Driver Callbacks */
  42247. +/** @{ */
  42248. +
  42249. +/** This function is called whenever core switches to host mode. */
  42250. +typedef int (*dwc_otg_hcd_start_cb_t) (dwc_otg_hcd_t * hcd);
  42251. +
  42252. +/** This function is called when device has been disconnected */
  42253. +typedef int (*dwc_otg_hcd_disconnect_cb_t) (dwc_otg_hcd_t * hcd);
  42254. +
  42255. +/** Wrapper provides this function to HCD to core, so it can get hub information to which device is connected */
  42256. +typedef int (*dwc_otg_hcd_hub_info_from_urb_cb_t) (dwc_otg_hcd_t * hcd,
  42257. + void *urb_handle,
  42258. + uint32_t * hub_addr,
  42259. + uint32_t * port_addr);
  42260. +/** Via this function HCD core gets device speed */
  42261. +typedef int (*dwc_otg_hcd_speed_from_urb_cb_t) (dwc_otg_hcd_t * hcd,
  42262. + void *urb_handle);
  42263. +
  42264. +/** This function is called when urb is completed */
  42265. +typedef int (*dwc_otg_hcd_complete_urb_cb_t) (dwc_otg_hcd_t * hcd,
  42266. + void *urb_handle,
  42267. + dwc_otg_hcd_urb_t * dwc_otg_urb,
  42268. + int32_t status);
  42269. +
  42270. +/** Via this function HCD core gets b_hnp_enable parameter */
  42271. +typedef int (*dwc_otg_hcd_get_b_hnp_enable) (dwc_otg_hcd_t * hcd);
  42272. +
  42273. +struct dwc_otg_hcd_function_ops {
  42274. + dwc_otg_hcd_start_cb_t start;
  42275. + dwc_otg_hcd_disconnect_cb_t disconnect;
  42276. + dwc_otg_hcd_hub_info_from_urb_cb_t hub_info;
  42277. + dwc_otg_hcd_speed_from_urb_cb_t speed;
  42278. + dwc_otg_hcd_complete_urb_cb_t complete;
  42279. + dwc_otg_hcd_get_b_hnp_enable get_b_hnp_enable;
  42280. +};
  42281. +/** @} */
  42282. +
  42283. +/** @name HCD Core API */
  42284. +/** @{ */
  42285. +/** This function allocates dwc_otg_hcd structure and returns pointer on it. */
  42286. +extern dwc_otg_hcd_t *dwc_otg_hcd_alloc_hcd(void);
  42287. +
  42288. +/** This function should be called to initiate HCD Core.
  42289. + *
  42290. + * @param hcd The HCD
  42291. + * @param core_if The DWC_OTG Core
  42292. + *
  42293. + * Returns -DWC_E_NO_MEMORY if no enough memory.
  42294. + * Returns 0 on success
  42295. + */
  42296. +extern int dwc_otg_hcd_init(dwc_otg_hcd_t * hcd, dwc_otg_core_if_t * core_if);
  42297. +
  42298. +/** Frees HCD
  42299. + *
  42300. + * @param hcd The HCD
  42301. + */
  42302. +extern void dwc_otg_hcd_remove(dwc_otg_hcd_t * hcd);
  42303. +
  42304. +/** This function should be called on every hardware interrupt.
  42305. + *
  42306. + * @param dwc_otg_hcd The HCD
  42307. + *
  42308. + * Returns non zero if interrupt is handled
  42309. + * Return 0 if interrupt is not handled
  42310. + */
  42311. +extern int32_t dwc_otg_hcd_handle_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  42312. +
  42313. +/** This function is used to handle the fast interrupt
  42314. + *
  42315. + */
  42316. +#ifdef CONFIG_ARM64
  42317. +extern void dwc_otg_hcd_handle_fiq(void);
  42318. +#else
  42319. +extern void __attribute__ ((naked)) dwc_otg_hcd_handle_fiq(void);
  42320. +#endif
  42321. +
  42322. +/**
  42323. + * Returns private data set by
  42324. + * dwc_otg_hcd_set_priv_data function.
  42325. + *
  42326. + * @param hcd The HCD
  42327. + */
  42328. +extern void *dwc_otg_hcd_get_priv_data(dwc_otg_hcd_t * hcd);
  42329. +
  42330. +/**
  42331. + * Set private data.
  42332. + *
  42333. + * @param hcd The HCD
  42334. + * @param priv_data pointer to be stored in private data
  42335. + */
  42336. +extern void dwc_otg_hcd_set_priv_data(dwc_otg_hcd_t * hcd, void *priv_data);
  42337. +
  42338. +/**
  42339. + * This function initializes the HCD Core.
  42340. + *
  42341. + * @param hcd The HCD
  42342. + * @param fops The Function Driver Operations data structure containing pointers to all callbacks.
  42343. + *
  42344. + * Returns -DWC_E_NO_DEVICE if Core is currently is in device mode.
  42345. + * Returns 0 on success
  42346. + */
  42347. +extern int dwc_otg_hcd_start(dwc_otg_hcd_t * hcd,
  42348. + struct dwc_otg_hcd_function_ops *fops);
  42349. +
  42350. +/**
  42351. + * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
  42352. + * stopped.
  42353. + *
  42354. + * @param hcd The HCD
  42355. + */
  42356. +extern void dwc_otg_hcd_stop(dwc_otg_hcd_t * hcd);
  42357. +
  42358. +/**
  42359. + * Handles hub class-specific requests.
  42360. + *
  42361. + * @param dwc_otg_hcd The HCD
  42362. + * @param typeReq Request Type
  42363. + * @param wValue wValue from control request
  42364. + * @param wIndex wIndex from control request
  42365. + * @param buf data buffer
  42366. + * @param wLength data buffer length
  42367. + *
  42368. + * Returns -DWC_E_INVALID if invalid argument is passed
  42369. + * Returns 0 on success
  42370. + */
  42371. +extern int dwc_otg_hcd_hub_control(dwc_otg_hcd_t * dwc_otg_hcd,
  42372. + uint16_t typeReq, uint16_t wValue,
  42373. + uint16_t wIndex, uint8_t * buf,
  42374. + uint16_t wLength);
  42375. +
  42376. +/**
  42377. + * Returns otg port number.
  42378. + *
  42379. + * @param hcd The HCD
  42380. + */
  42381. +extern uint32_t dwc_otg_hcd_otg_port(dwc_otg_hcd_t * hcd);
  42382. +
  42383. +/**
  42384. + * Returns OTG version - either 1.3 or 2.0.
  42385. + *
  42386. + * @param core_if The core_if structure pointer
  42387. + */
  42388. +extern uint16_t dwc_otg_get_otg_version(dwc_otg_core_if_t * core_if);
  42389. +
  42390. +/**
  42391. + * Returns 1 if currently core is acting as B host, and 0 otherwise.
  42392. + *
  42393. + * @param hcd The HCD
  42394. + */
  42395. +extern uint32_t dwc_otg_hcd_is_b_host(dwc_otg_hcd_t * hcd);
  42396. +
  42397. +/**
  42398. + * Returns current frame number.
  42399. + *
  42400. + * @param hcd The HCD
  42401. + */
  42402. +extern int dwc_otg_hcd_get_frame_number(dwc_otg_hcd_t * hcd);
  42403. +
  42404. +/**
  42405. + * Dumps hcd state.
  42406. + *
  42407. + * @param hcd The HCD
  42408. + */
  42409. +extern void dwc_otg_hcd_dump_state(dwc_otg_hcd_t * hcd);
  42410. +
  42411. +/**
  42412. + * Dump the average frame remaining at SOF. This can be used to
  42413. + * determine average interrupt latency. Frame remaining is also shown for
  42414. + * start transfer and two additional sample points.
  42415. + * Currently this function is not implemented.
  42416. + *
  42417. + * @param hcd The HCD
  42418. + */
  42419. +extern void dwc_otg_hcd_dump_frrem(dwc_otg_hcd_t * hcd);
  42420. +
  42421. +/**
  42422. + * Sends LPM transaction to the local device.
  42423. + *
  42424. + * @param hcd The HCD
  42425. + * @param devaddr Device Address
  42426. + * @param hird Host initiated resume duration
  42427. + * @param bRemoteWake Value of bRemoteWake field in LPM transaction
  42428. + *
  42429. + * Returns negative value if sending LPM transaction was not succeeded.
  42430. + * Returns 0 on success.
  42431. + */
  42432. +extern int dwc_otg_hcd_send_lpm(dwc_otg_hcd_t * hcd, uint8_t devaddr,
  42433. + uint8_t hird, uint8_t bRemoteWake);
  42434. +
  42435. +/* URB interface */
  42436. +
  42437. +/**
  42438. + * Allocates memory for dwc_otg_hcd_urb structure.
  42439. + * Allocated memory should be freed by call of DWC_FREE.
  42440. + *
  42441. + * @param hcd The HCD
  42442. + * @param iso_desc_count Count of ISOC descriptors
  42443. + * @param atomic_alloc Specefies whether to perform atomic allocation.
  42444. + */
  42445. +extern dwc_otg_hcd_urb_t *dwc_otg_hcd_urb_alloc(dwc_otg_hcd_t * hcd,
  42446. + int iso_desc_count,
  42447. + int atomic_alloc);
  42448. +
  42449. +/**
  42450. + * Set pipe information in URB.
  42451. + *
  42452. + * @param hcd_urb DWC_OTG URB
  42453. + * @param devaddr Device Address
  42454. + * @param ep_num Endpoint Number
  42455. + * @param ep_type Endpoint Type
  42456. + * @param ep_dir Endpoint Direction
  42457. + * @param mps Max Packet Size
  42458. + */
  42459. +extern void dwc_otg_hcd_urb_set_pipeinfo(dwc_otg_hcd_urb_t * hcd_urb,
  42460. + uint8_t devaddr, uint8_t ep_num,
  42461. + uint8_t ep_type, uint8_t ep_dir,
  42462. + uint16_t mps);
  42463. +
  42464. +/* Transfer flags */
  42465. +#define URB_GIVEBACK_ASAP 0x1
  42466. +#define URB_SEND_ZERO_PACKET 0x2
  42467. +
  42468. +/**
  42469. + * Sets dwc_otg_hcd_urb parameters.
  42470. + *
  42471. + * @param urb DWC_OTG URB allocated by dwc_otg_hcd_urb_alloc function.
  42472. + * @param urb_handle Unique handle for request, this will be passed back
  42473. + * to function driver in completion callback.
  42474. + * @param buf The buffer for the data
  42475. + * @param dma The DMA buffer for the data
  42476. + * @param buflen Transfer length
  42477. + * @param sp Buffer for setup data
  42478. + * @param sp_dma DMA address of setup data buffer
  42479. + * @param flags Transfer flags
  42480. + * @param interval Polling interval for interrupt or isochronous transfers.
  42481. + */
  42482. +extern void dwc_otg_hcd_urb_set_params(dwc_otg_hcd_urb_t * urb,
  42483. + void *urb_handle, void *buf,
  42484. + dwc_dma_t dma, uint32_t buflen, void *sp,
  42485. + dwc_dma_t sp_dma, uint32_t flags,
  42486. + uint16_t interval);
  42487. +
  42488. +/** Gets status from dwc_otg_hcd_urb
  42489. + *
  42490. + * @param dwc_otg_urb DWC_OTG URB
  42491. + */
  42492. +extern uint32_t dwc_otg_hcd_urb_get_status(dwc_otg_hcd_urb_t * dwc_otg_urb);
  42493. +
  42494. +/** Gets actual length from dwc_otg_hcd_urb
  42495. + *
  42496. + * @param dwc_otg_urb DWC_OTG URB
  42497. + */
  42498. +extern uint32_t dwc_otg_hcd_urb_get_actual_length(dwc_otg_hcd_urb_t *
  42499. + dwc_otg_urb);
  42500. +
  42501. +/** Gets error count from dwc_otg_hcd_urb. Only for ISOC URBs
  42502. + *
  42503. + * @param dwc_otg_urb DWC_OTG URB
  42504. + */
  42505. +extern uint32_t dwc_otg_hcd_urb_get_error_count(dwc_otg_hcd_urb_t *
  42506. + dwc_otg_urb);
  42507. +
  42508. +/** Set ISOC descriptor offset and length
  42509. + *
  42510. + * @param dwc_otg_urb DWC_OTG URB
  42511. + * @param desc_num ISOC descriptor number
  42512. + * @param offset Offset from beginig of buffer.
  42513. + * @param length Transaction length
  42514. + */
  42515. +extern void dwc_otg_hcd_urb_set_iso_desc_params(dwc_otg_hcd_urb_t * dwc_otg_urb,
  42516. + int desc_num, uint32_t offset,
  42517. + uint32_t length);
  42518. +
  42519. +/** Get status of ISOC descriptor, specified by desc_num
  42520. + *
  42521. + * @param dwc_otg_urb DWC_OTG URB
  42522. + * @param desc_num ISOC descriptor number
  42523. + */
  42524. +extern uint32_t dwc_otg_hcd_urb_get_iso_desc_status(dwc_otg_hcd_urb_t *
  42525. + dwc_otg_urb, int desc_num);
  42526. +
  42527. +/** Get actual length of ISOC descriptor, specified by desc_num
  42528. + *
  42529. + * @param dwc_otg_urb DWC_OTG URB
  42530. + * @param desc_num ISOC descriptor number
  42531. + */
  42532. +extern uint32_t dwc_otg_hcd_urb_get_iso_desc_actual_length(dwc_otg_hcd_urb_t *
  42533. + dwc_otg_urb,
  42534. + int desc_num);
  42535. +
  42536. +/** Queue URB. After transfer is completes, the complete callback will be called with the URB status
  42537. + *
  42538. + * @param dwc_otg_hcd The HCD
  42539. + * @param dwc_otg_urb DWC_OTG URB
  42540. + * @param ep_handle Out parameter for returning endpoint handle
  42541. + * @param atomic_alloc Flag to do atomic allocation if needed
  42542. + *
  42543. + * Returns -DWC_E_NO_DEVICE if no device is connected.
  42544. + * Returns -DWC_E_NO_MEMORY if there is no enough memory.
  42545. + * Returns 0 on success.
  42546. + */
  42547. +extern int dwc_otg_hcd_urb_enqueue(dwc_otg_hcd_t * dwc_otg_hcd,
  42548. + dwc_otg_hcd_urb_t * dwc_otg_urb,
  42549. + void **ep_handle, int atomic_alloc);
  42550. +
  42551. +/** De-queue the specified URB
  42552. + *
  42553. + * @param dwc_otg_hcd The HCD
  42554. + * @param dwc_otg_urb DWC_OTG URB
  42555. + */
  42556. +extern int dwc_otg_hcd_urb_dequeue(dwc_otg_hcd_t * dwc_otg_hcd,
  42557. + dwc_otg_hcd_urb_t * dwc_otg_urb);
  42558. +
  42559. +/** Frees resources in the DWC_otg controller related to a given endpoint.
  42560. + * Any URBs for the endpoint must already be dequeued.
  42561. + *
  42562. + * @param hcd The HCD
  42563. + * @param ep_handle Endpoint handle, returned by dwc_otg_hcd_urb_enqueue function
  42564. + * @param retry Number of retries if there are queued transfers.
  42565. + *
  42566. + * Returns -DWC_E_INVALID if invalid arguments are passed.
  42567. + * Returns 0 on success
  42568. + */
  42569. +extern int dwc_otg_hcd_endpoint_disable(dwc_otg_hcd_t * hcd, void *ep_handle,
  42570. + int retry);
  42571. +
  42572. +/* Resets the data toggle in qh structure. This function can be called from
  42573. + * usb_clear_halt routine.
  42574. + *
  42575. + * @param hcd The HCD
  42576. + * @param ep_handle Endpoint handle, returned by dwc_otg_hcd_urb_enqueue function
  42577. + *
  42578. + * Returns -DWC_E_INVALID if invalid arguments are passed.
  42579. + * Returns 0 on success
  42580. + */
  42581. +extern int dwc_otg_hcd_endpoint_reset(dwc_otg_hcd_t * hcd, void *ep_handle);
  42582. +
  42583. +/** Returns 1 if status of specified port is changed and 0 otherwise.
  42584. + *
  42585. + * @param hcd The HCD
  42586. + * @param port Port number
  42587. + */
  42588. +extern int dwc_otg_hcd_is_status_changed(dwc_otg_hcd_t * hcd, int port);
  42589. +
  42590. +/** Call this function to check if bandwidth was allocated for specified endpoint.
  42591. + * Only for ISOC and INTERRUPT endpoints.
  42592. + *
  42593. + * @param hcd The HCD
  42594. + * @param ep_handle Endpoint handle
  42595. + */
  42596. +extern int dwc_otg_hcd_is_bandwidth_allocated(dwc_otg_hcd_t * hcd,
  42597. + void *ep_handle);
  42598. +
  42599. +/** Call this function to check if bandwidth was freed for specified endpoint.
  42600. + *
  42601. + * @param hcd The HCD
  42602. + * @param ep_handle Endpoint handle
  42603. + */
  42604. +extern int dwc_otg_hcd_is_bandwidth_freed(dwc_otg_hcd_t * hcd, void *ep_handle);
  42605. +
  42606. +/** Returns bandwidth allocated for specified endpoint in microseconds.
  42607. + * Only for ISOC and INTERRUPT endpoints.
  42608. + *
  42609. + * @param hcd The HCD
  42610. + * @param ep_handle Endpoint handle
  42611. + */
  42612. +extern uint8_t dwc_otg_hcd_get_ep_bandwidth(dwc_otg_hcd_t * hcd,
  42613. + void *ep_handle);
  42614. +
  42615. +/** @} */
  42616. +
  42617. +#endif /* __DWC_HCD_IF_H__ */
  42618. +#endif /* DWC_DEVICE_ONLY */
  42619. --- /dev/null
  42620. +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd_intr.c
  42621. @@ -0,0 +1,2757 @@
  42622. +/* ==========================================================================
  42623. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_intr.c $
  42624. + * $Revision: #89 $
  42625. + * $Date: 2011/10/20 $
  42626. + * $Change: 1869487 $
  42627. + *
  42628. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  42629. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  42630. + * otherwise expressly agreed to in writing between Synopsys and you.
  42631. + *
  42632. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  42633. + * any End User Software License Agreement or Agreement for Licensed Product
  42634. + * with Synopsys or any supplement thereto. You are permitted to use and
  42635. + * redistribute this Software in source and binary forms, with or without
  42636. + * modification, provided that redistributions of source code must retain this
  42637. + * notice. You may not view, use, disclose, copy or distribute this file or
  42638. + * any information contained herein except pursuant to this license grant from
  42639. + * Synopsys. If you do not agree with this notice, including the disclaimer
  42640. + * below, then you are not authorized to use the Software.
  42641. + *
  42642. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  42643. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42644. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  42645. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  42646. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  42647. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  42648. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  42649. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  42650. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  42651. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  42652. + * DAMAGE.
  42653. + * ========================================================================== */
  42654. +#ifndef DWC_DEVICE_ONLY
  42655. +
  42656. +#include "dwc_otg_hcd.h"
  42657. +#include "dwc_otg_regs.h"
  42658. +
  42659. +#include <linux/jiffies.h>
  42660. +#ifdef CONFIG_ARM
  42661. +#include <asm/fiq.h>
  42662. +#endif
  42663. +
  42664. +extern bool microframe_schedule;
  42665. +
  42666. +/** @file
  42667. + * This file contains the implementation of the HCD Interrupt handlers.
  42668. + */
  42669. +
  42670. +int fiq_done, int_done;
  42671. +
  42672. +#ifdef FIQ_DEBUG
  42673. +char buffer[1000*16];
  42674. +int wptr;
  42675. +void notrace _fiq_print(FIQDBG_T dbg_lvl, char *fmt, ...)
  42676. +{
  42677. + FIQDBG_T dbg_lvl_req = FIQDBG_PORTHUB;
  42678. + va_list args;
  42679. + char text[17];
  42680. + hfnum_data_t hfnum = { .d32 = FIQ_READ(dwc_regs_base + 0x408) };
  42681. +
  42682. + if(dbg_lvl & dbg_lvl_req || dbg_lvl == FIQDBG_ERR)
  42683. + {
  42684. + local_fiq_disable();
  42685. + snprintf(text, 9, "%4d%d:%d ", hfnum.b.frnum/8, hfnum.b.frnum%8, 8 - hfnum.b.frrem/937);
  42686. + va_start(args, fmt);
  42687. + vsnprintf(text+8, 9, fmt, args);
  42688. + va_end(args);
  42689. +
  42690. + memcpy(buffer + wptr, text, 16);
  42691. + wptr = (wptr + 16) % sizeof(buffer);
  42692. + local_fiq_enable();
  42693. + }
  42694. +}
  42695. +#endif
  42696. +
  42697. +/** This function handles interrupts for the HCD. */
  42698. +int32_t dwc_otg_hcd_handle_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  42699. +{
  42700. + int retval = 0;
  42701. + static int last_time;
  42702. + dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
  42703. + gintsts_data_t gintsts;
  42704. + gintmsk_data_t gintmsk;
  42705. + hfnum_data_t hfnum;
  42706. + haintmsk_data_t haintmsk;
  42707. +
  42708. +#ifdef DEBUG
  42709. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  42710. +
  42711. +#endif
  42712. +
  42713. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  42714. + gintmsk.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  42715. +
  42716. + /* Exit from ISR if core is hibernated */
  42717. + if (core_if->hibernation_suspend == 1) {
  42718. + goto exit_handler_routine;
  42719. + }
  42720. + DWC_SPINLOCK(dwc_otg_hcd->lock);
  42721. + /* Check if HOST Mode */
  42722. + if (dwc_otg_is_host_mode(core_if)) {
  42723. + if (fiq_enable) {
  42724. + local_fiq_disable();
  42725. + fiq_fsm_spin_lock(&dwc_otg_hcd->fiq_state->lock);
  42726. + /* Pull in from the FIQ's disabled mask */
  42727. + gintmsk.d32 = gintmsk.d32 | ~(dwc_otg_hcd->fiq_state->gintmsk_saved.d32);
  42728. + dwc_otg_hcd->fiq_state->gintmsk_saved.d32 = ~0;
  42729. + }
  42730. +
  42731. + if (fiq_fsm_enable && ( 0x0000FFFF & ~(dwc_otg_hcd->fiq_state->haintmsk_saved.b2.chint))) {
  42732. + gintsts.b.hcintr = 1;
  42733. + }
  42734. +
  42735. + /* Danger will robinson: fake a SOF if necessary */
  42736. + if (fiq_fsm_enable && (dwc_otg_hcd->fiq_state->gintmsk_saved.b.sofintr == 1)) {
  42737. + gintsts.b.sofintr = 1;
  42738. + }
  42739. + gintsts.d32 &= gintmsk.d32;
  42740. +
  42741. + if (fiq_enable) {
  42742. + fiq_fsm_spin_unlock(&dwc_otg_hcd->fiq_state->lock);
  42743. + local_fiq_enable();
  42744. + }
  42745. +
  42746. + if (!gintsts.d32) {
  42747. + goto exit_handler_routine;
  42748. + }
  42749. +
  42750. +#ifdef DEBUG
  42751. + // We should be OK doing this because the common interrupts should already have been serviced
  42752. + /* Don't print debug message in the interrupt handler on SOF */
  42753. +#ifndef DEBUG_SOF
  42754. + if (gintsts.d32 != DWC_SOF_INTR_MASK)
  42755. +#endif
  42756. + DWC_DEBUGPL(DBG_HCDI, "\n");
  42757. +#endif
  42758. +
  42759. +#ifdef DEBUG
  42760. +#ifndef DEBUG_SOF
  42761. + if (gintsts.d32 != DWC_SOF_INTR_MASK)
  42762. +#endif
  42763. + DWC_DEBUGPL(DBG_HCDI,
  42764. + "DWC OTG HCD Interrupt Detected gintsts&gintmsk=0x%08x core_if=%p\n",
  42765. + gintsts.d32, core_if);
  42766. +#endif
  42767. + hfnum.d32 = DWC_READ_REG32(&dwc_otg_hcd->core_if->host_if->host_global_regs->hfnum);
  42768. + if (gintsts.b.sofintr) {
  42769. + retval |= dwc_otg_hcd_handle_sof_intr(dwc_otg_hcd);
  42770. + }
  42771. +
  42772. + if (gintsts.b.rxstsqlvl) {
  42773. + retval |=
  42774. + dwc_otg_hcd_handle_rx_status_q_level_intr
  42775. + (dwc_otg_hcd);
  42776. + }
  42777. + if (gintsts.b.nptxfempty) {
  42778. + retval |=
  42779. + dwc_otg_hcd_handle_np_tx_fifo_empty_intr
  42780. + (dwc_otg_hcd);
  42781. + }
  42782. + if (gintsts.b.i2cintr) {
  42783. + /** @todo Implement i2cintr handler. */
  42784. + }
  42785. + if (gintsts.b.portintr) {
  42786. +
  42787. + gintmsk_data_t gintmsk = { .b.portintr = 1};
  42788. + retval |= dwc_otg_hcd_handle_port_intr(dwc_otg_hcd);
  42789. + if (fiq_enable) {
  42790. + local_fiq_disable();
  42791. + fiq_fsm_spin_lock(&dwc_otg_hcd->fiq_state->lock);
  42792. + DWC_MODIFY_REG32(&dwc_otg_hcd->core_if->core_global_regs->gintmsk, 0, gintmsk.d32);
  42793. + fiq_fsm_spin_unlock(&dwc_otg_hcd->fiq_state->lock);
  42794. + local_fiq_enable();
  42795. + } else {
  42796. + DWC_MODIFY_REG32(&dwc_otg_hcd->core_if->core_global_regs->gintmsk, 0, gintmsk.d32);
  42797. + }
  42798. + }
  42799. + if (gintsts.b.hcintr) {
  42800. + retval |= dwc_otg_hcd_handle_hc_intr(dwc_otg_hcd);
  42801. + }
  42802. + if (gintsts.b.ptxfempty) {
  42803. + retval |=
  42804. + dwc_otg_hcd_handle_perio_tx_fifo_empty_intr
  42805. + (dwc_otg_hcd);
  42806. + }
  42807. +#ifdef DEBUG
  42808. +#ifndef DEBUG_SOF
  42809. + if (gintsts.d32 != DWC_SOF_INTR_MASK)
  42810. +#endif
  42811. + {
  42812. + DWC_DEBUGPL(DBG_HCDI,
  42813. + "DWC OTG HCD Finished Servicing Interrupts\n");
  42814. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD gintsts=0x%08x\n",
  42815. + DWC_READ_REG32(&global_regs->gintsts));
  42816. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD gintmsk=0x%08x\n",
  42817. + DWC_READ_REG32(&global_regs->gintmsk));
  42818. + }
  42819. +#endif
  42820. +
  42821. +#ifdef DEBUG
  42822. +#ifndef DEBUG_SOF
  42823. + if (gintsts.d32 != DWC_SOF_INTR_MASK)
  42824. +#endif
  42825. + DWC_DEBUGPL(DBG_HCDI, "\n");
  42826. +#endif
  42827. +
  42828. + }
  42829. +
  42830. +exit_handler_routine:
  42831. + if (fiq_enable) {
  42832. + gintmsk_data_t gintmsk_new;
  42833. + haintmsk_data_t haintmsk_new;
  42834. + local_fiq_disable();
  42835. + fiq_fsm_spin_lock(&dwc_otg_hcd->fiq_state->lock);
  42836. + gintmsk_new.d32 = *(volatile uint32_t *)&dwc_otg_hcd->fiq_state->gintmsk_saved.d32;
  42837. + if(fiq_fsm_enable)
  42838. + haintmsk_new.d32 = *(volatile uint32_t *)&dwc_otg_hcd->fiq_state->haintmsk_saved.d32;
  42839. + else
  42840. + haintmsk_new.d32 = 0x0000FFFF;
  42841. +
  42842. + /* The FIQ could have sneaked another interrupt in. If so, don't clear MPHI */
  42843. + if ((gintmsk_new.d32 == ~0) && (haintmsk_new.d32 == 0x0000FFFF)) {
  42844. + if (dwc_otg_hcd->fiq_state->mphi_regs.swirq_clr) {
  42845. + DWC_WRITE_REG32(dwc_otg_hcd->fiq_state->mphi_regs.swirq_clr, 1);
  42846. + } else {
  42847. + DWC_WRITE_REG32(dwc_otg_hcd->fiq_state->mphi_regs.intstat, (1<<16));
  42848. + }
  42849. + if (dwc_otg_hcd->fiq_state->mphi_int_count >= 50) {
  42850. + fiq_print(FIQDBG_INT, dwc_otg_hcd->fiq_state, "MPHI CLR");
  42851. + DWC_WRITE_REG32(dwc_otg_hcd->fiq_state->mphi_regs.ctrl, ((1<<31) + (1<<16)));
  42852. + while (!(DWC_READ_REG32(dwc_otg_hcd->fiq_state->mphi_regs.ctrl) & (1 << 17)))
  42853. + ;
  42854. + DWC_WRITE_REG32(dwc_otg_hcd->fiq_state->mphi_regs.ctrl, (1<<31));
  42855. + dwc_otg_hcd->fiq_state->mphi_int_count = 0;
  42856. + }
  42857. + int_done++;
  42858. + }
  42859. + haintmsk.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->haintmsk);
  42860. + /* Re-enable interrupts that the FIQ masked (first time round) */
  42861. + FIQ_WRITE(dwc_otg_hcd->fiq_state->dwc_regs_base + GINTMSK, gintmsk.d32);
  42862. + fiq_fsm_spin_unlock(&dwc_otg_hcd->fiq_state->lock);
  42863. + local_fiq_enable();
  42864. +
  42865. + if ((jiffies / HZ) > last_time) {
  42866. + //dwc_otg_qh_t *qh;
  42867. + //dwc_list_link_t *cur;
  42868. + /* Once a second output the fiq and irq numbers, useful for debug */
  42869. + last_time = jiffies / HZ;
  42870. + // DWC_WARN("np_kick=%d AHC=%d sched_frame=%d cur_frame=%d int_done=%d fiq_done=%d",
  42871. + // dwc_otg_hcd->fiq_state->kick_np_queues, dwc_otg_hcd->available_host_channels,
  42872. + // dwc_otg_hcd->fiq_state->next_sched_frame, hfnum.b.frnum, int_done, dwc_otg_hcd->fiq_state->fiq_done);
  42873. + //printk(KERN_WARNING "Periodic queues:\n");
  42874. + }
  42875. + }
  42876. +
  42877. + DWC_SPINUNLOCK(dwc_otg_hcd->lock);
  42878. + return retval;
  42879. +}
  42880. +
  42881. +#ifdef DWC_TRACK_MISSED_SOFS
  42882. +
  42883. +#warning Compiling code to track missed SOFs
  42884. +#define FRAME_NUM_ARRAY_SIZE 1000
  42885. +/**
  42886. + * This function is for debug only.
  42887. + */
  42888. +static inline void track_missed_sofs(uint16_t curr_frame_number)
  42889. +{
  42890. + static uint16_t frame_num_array[FRAME_NUM_ARRAY_SIZE];
  42891. + static uint16_t last_frame_num_array[FRAME_NUM_ARRAY_SIZE];
  42892. + static int frame_num_idx = 0;
  42893. + static uint16_t last_frame_num = DWC_HFNUM_MAX_FRNUM;
  42894. + static int dumped_frame_num_array = 0;
  42895. +
  42896. + if (frame_num_idx < FRAME_NUM_ARRAY_SIZE) {
  42897. + if (((last_frame_num + 1) & DWC_HFNUM_MAX_FRNUM) !=
  42898. + curr_frame_number) {
  42899. + frame_num_array[frame_num_idx] = curr_frame_number;
  42900. + last_frame_num_array[frame_num_idx++] = last_frame_num;
  42901. + }
  42902. + } else if (!dumped_frame_num_array) {
  42903. + int i;
  42904. + DWC_PRINTF("Frame Last Frame\n");
  42905. + DWC_PRINTF("----- ----------\n");
  42906. + for (i = 0; i < FRAME_NUM_ARRAY_SIZE; i++) {
  42907. + DWC_PRINTF("0x%04x 0x%04x\n",
  42908. + frame_num_array[i], last_frame_num_array[i]);
  42909. + }
  42910. + dumped_frame_num_array = 1;
  42911. + }
  42912. + last_frame_num = curr_frame_number;
  42913. +}
  42914. +#endif
  42915. +
  42916. +/**
  42917. + * Handles the start-of-frame interrupt in host mode. Non-periodic
  42918. + * transactions may be queued to the DWC_otg controller for the current
  42919. + * (micro)frame. Periodic transactions may be queued to the controller for the
  42920. + * next (micro)frame.
  42921. + */
  42922. +int32_t dwc_otg_hcd_handle_sof_intr(dwc_otg_hcd_t * hcd)
  42923. +{
  42924. + hfnum_data_t hfnum;
  42925. + gintsts_data_t gintsts = { .d32 = 0 };
  42926. + dwc_list_link_t *qh_entry;
  42927. + dwc_otg_qh_t *qh;
  42928. + dwc_otg_transaction_type_e tr_type;
  42929. + int did_something = 0;
  42930. + int32_t next_sched_frame = -1;
  42931. +
  42932. + hfnum.d32 =
  42933. + DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hfnum);
  42934. +
  42935. +#ifdef DEBUG_SOF
  42936. + DWC_DEBUGPL(DBG_HCD, "--Start of Frame Interrupt--\n");
  42937. +#endif
  42938. + hcd->frame_number = hfnum.b.frnum;
  42939. +
  42940. +#ifdef DEBUG
  42941. + hcd->frrem_accum += hfnum.b.frrem;
  42942. + hcd->frrem_samples++;
  42943. +#endif
  42944. +
  42945. +#ifdef DWC_TRACK_MISSED_SOFS
  42946. + track_missed_sofs(hcd->frame_number);
  42947. +#endif
  42948. + /* Determine whether any periodic QHs should be executed. */
  42949. + qh_entry = DWC_LIST_FIRST(&hcd->periodic_sched_inactive);
  42950. + while (qh_entry != &hcd->periodic_sched_inactive) {
  42951. + qh = DWC_LIST_ENTRY(qh_entry, dwc_otg_qh_t, qh_list_entry);
  42952. + qh_entry = qh_entry->next;
  42953. + if (dwc_frame_num_le(qh->sched_frame, hcd->frame_number)) {
  42954. +
  42955. + /*
  42956. + * Move QH to the ready list to be executed next
  42957. + * (micro)frame.
  42958. + */
  42959. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_ready,
  42960. + &qh->qh_list_entry);
  42961. +
  42962. + did_something = 1;
  42963. + }
  42964. + else
  42965. + {
  42966. + if(next_sched_frame < 0 || dwc_frame_num_le(qh->sched_frame, next_sched_frame))
  42967. + {
  42968. + next_sched_frame = qh->sched_frame;
  42969. + }
  42970. + }
  42971. + }
  42972. + if (fiq_enable)
  42973. + hcd->fiq_state->next_sched_frame = next_sched_frame;
  42974. +
  42975. + tr_type = dwc_otg_hcd_select_transactions(hcd);
  42976. + if (tr_type != DWC_OTG_TRANSACTION_NONE) {
  42977. + dwc_otg_hcd_queue_transactions(hcd, tr_type);
  42978. + did_something = 1;
  42979. + }
  42980. +
  42981. + /* Clear interrupt - but do not trample on the FIQ sof */
  42982. + if (!fiq_fsm_enable) {
  42983. + gintsts.b.sofintr = 1;
  42984. + DWC_WRITE_REG32(&hcd->core_if->core_global_regs->gintsts, gintsts.d32);
  42985. + }
  42986. + return 1;
  42987. +}
  42988. +
  42989. +/** Handles the Rx Status Queue Level Interrupt, which indicates that there is at
  42990. + * least one packet in the Rx FIFO. The packets are moved from the FIFO to
  42991. + * memory if the DWC_otg controller is operating in Slave mode. */
  42992. +int32_t dwc_otg_hcd_handle_rx_status_q_level_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  42993. +{
  42994. + host_grxsts_data_t grxsts;
  42995. + dwc_hc_t *hc = NULL;
  42996. +
  42997. + DWC_DEBUGPL(DBG_HCD, "--RxStsQ Level Interrupt--\n");
  42998. +
  42999. + grxsts.d32 =
  43000. + DWC_READ_REG32(&dwc_otg_hcd->core_if->core_global_regs->grxstsp);
  43001. +
  43002. + hc = dwc_otg_hcd->hc_ptr_array[grxsts.b.chnum];
  43003. + if (!hc) {
  43004. + DWC_ERROR("Unable to get corresponding channel\n");
  43005. + return 0;
  43006. + }
  43007. +
  43008. + /* Packet Status */
  43009. + DWC_DEBUGPL(DBG_HCDV, " Ch num = %d\n", grxsts.b.chnum);
  43010. + DWC_DEBUGPL(DBG_HCDV, " Count = %d\n", grxsts.b.bcnt);
  43011. + DWC_DEBUGPL(DBG_HCDV, " DPID = %d, hc.dpid = %d\n", grxsts.b.dpid,
  43012. + hc->data_pid_start);
  43013. + DWC_DEBUGPL(DBG_HCDV, " PStatus = %d\n", grxsts.b.pktsts);
  43014. +
  43015. + switch (grxsts.b.pktsts) {
  43016. + case DWC_GRXSTS_PKTSTS_IN:
  43017. + /* Read the data into the host buffer. */
  43018. + if (grxsts.b.bcnt > 0) {
  43019. + dwc_otg_read_packet(dwc_otg_hcd->core_if,
  43020. + hc->xfer_buff, grxsts.b.bcnt);
  43021. +
  43022. + /* Update the HC fields for the next packet received. */
  43023. + hc->xfer_count += grxsts.b.bcnt;
  43024. + hc->xfer_buff += grxsts.b.bcnt;
  43025. + }
  43026. +
  43027. + case DWC_GRXSTS_PKTSTS_IN_XFER_COMP:
  43028. + case DWC_GRXSTS_PKTSTS_DATA_TOGGLE_ERR:
  43029. + case DWC_GRXSTS_PKTSTS_CH_HALTED:
  43030. + /* Handled in interrupt, just ignore data */
  43031. + break;
  43032. + default:
  43033. + DWC_ERROR("RX_STS_Q Interrupt: Unknown status %d\n",
  43034. + grxsts.b.pktsts);
  43035. + break;
  43036. + }
  43037. +
  43038. + return 1;
  43039. +}
  43040. +
  43041. +/** This interrupt occurs when the non-periodic Tx FIFO is half-empty. More
  43042. + * data packets may be written to the FIFO for OUT transfers. More requests
  43043. + * may be written to the non-periodic request queue for IN transfers. This
  43044. + * interrupt is enabled only in Slave mode. */
  43045. +int32_t dwc_otg_hcd_handle_np_tx_fifo_empty_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  43046. +{
  43047. + DWC_DEBUGPL(DBG_HCD, "--Non-Periodic TxFIFO Empty Interrupt--\n");
  43048. + dwc_otg_hcd_queue_transactions(dwc_otg_hcd,
  43049. + DWC_OTG_TRANSACTION_NON_PERIODIC);
  43050. + return 1;
  43051. +}
  43052. +
  43053. +/** This interrupt occurs when the periodic Tx FIFO is half-empty. More data
  43054. + * packets may be written to the FIFO for OUT transfers. More requests may be
  43055. + * written to the periodic request queue for IN transfers. This interrupt is
  43056. + * enabled only in Slave mode. */
  43057. +int32_t dwc_otg_hcd_handle_perio_tx_fifo_empty_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  43058. +{
  43059. + DWC_DEBUGPL(DBG_HCD, "--Periodic TxFIFO Empty Interrupt--\n");
  43060. + dwc_otg_hcd_queue_transactions(dwc_otg_hcd,
  43061. + DWC_OTG_TRANSACTION_PERIODIC);
  43062. + return 1;
  43063. +}
  43064. +
  43065. +/** There are multiple conditions that can cause a port interrupt. This function
  43066. + * determines which interrupt conditions have occurred and handles them
  43067. + * appropriately. */
  43068. +int32_t dwc_otg_hcd_handle_port_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  43069. +{
  43070. + int retval = 0;
  43071. + hprt0_data_t hprt0;
  43072. + hprt0_data_t hprt0_modify;
  43073. +
  43074. + hprt0.d32 = DWC_READ_REG32(dwc_otg_hcd->core_if->host_if->hprt0);
  43075. + hprt0_modify.d32 = DWC_READ_REG32(dwc_otg_hcd->core_if->host_if->hprt0);
  43076. +
  43077. + /* Clear appropriate bits in HPRT0 to clear the interrupt bit in
  43078. + * GINTSTS */
  43079. +
  43080. + hprt0_modify.b.prtena = 0;
  43081. + hprt0_modify.b.prtconndet = 0;
  43082. + hprt0_modify.b.prtenchng = 0;
  43083. + hprt0_modify.b.prtovrcurrchng = 0;
  43084. +
  43085. + /* Port Connect Detected
  43086. + * Set flag and clear if detected */
  43087. + if (dwc_otg_hcd->core_if->hibernation_suspend == 1) {
  43088. + // Dont modify port status if we are in hibernation state
  43089. + hprt0_modify.b.prtconndet = 1;
  43090. + hprt0_modify.b.prtenchng = 1;
  43091. + DWC_WRITE_REG32(dwc_otg_hcd->core_if->host_if->hprt0, hprt0_modify.d32);
  43092. + hprt0.d32 = DWC_READ_REG32(dwc_otg_hcd->core_if->host_if->hprt0);
  43093. + return retval;
  43094. + }
  43095. +
  43096. + if (hprt0.b.prtconndet) {
  43097. + /** @todo - check if steps performed in 'else' block should be perfromed regardles adp */
  43098. + if (dwc_otg_hcd->core_if->adp_enable &&
  43099. + dwc_otg_hcd->core_if->adp.vbuson_timer_started == 1) {
  43100. + DWC_PRINTF("PORT CONNECT DETECTED ----------------\n");
  43101. + DWC_TIMER_CANCEL(dwc_otg_hcd->core_if->adp.vbuson_timer);
  43102. + dwc_otg_hcd->core_if->adp.vbuson_timer_started = 0;
  43103. + /* TODO - check if this is required, as
  43104. + * host initialization was already performed
  43105. + * after initial ADP probing
  43106. + */
  43107. + /*dwc_otg_hcd->core_if->adp.vbuson_timer_started = 0;
  43108. + dwc_otg_core_init(dwc_otg_hcd->core_if);
  43109. + dwc_otg_enable_global_interrupts(dwc_otg_hcd->core_if);
  43110. + cil_hcd_start(dwc_otg_hcd->core_if);*/
  43111. + } else {
  43112. +
  43113. + DWC_DEBUGPL(DBG_HCD, "--Port Interrupt HPRT0=0x%08x "
  43114. + "Port Connect Detected--\n", hprt0.d32);
  43115. + dwc_otg_hcd->flags.b.port_connect_status_change = 1;
  43116. + dwc_otg_hcd->flags.b.port_connect_status = 1;
  43117. + hprt0_modify.b.prtconndet = 1;
  43118. +
  43119. + /* B-Device has connected, Delete the connection timer. */
  43120. + DWC_TIMER_CANCEL(dwc_otg_hcd->conn_timer);
  43121. + }
  43122. + /* The Hub driver asserts a reset when it sees port connect
  43123. + * status change flag */
  43124. + retval |= 1;
  43125. + }
  43126. +
  43127. + /* Port Enable Changed
  43128. + * Clear if detected - Set internal flag if disabled */
  43129. + if (hprt0.b.prtenchng) {
  43130. + DWC_DEBUGPL(DBG_HCD, " --Port Interrupt HPRT0=0x%08x "
  43131. + "Port Enable Changed--\n", hprt0.d32);
  43132. + hprt0_modify.b.prtenchng = 1;
  43133. + if (hprt0.b.prtena == 1) {
  43134. + hfir_data_t hfir;
  43135. + int do_reset = 0;
  43136. + dwc_otg_core_params_t *params =
  43137. + dwc_otg_hcd->core_if->core_params;
  43138. + dwc_otg_core_global_regs_t *global_regs =
  43139. + dwc_otg_hcd->core_if->core_global_regs;
  43140. + dwc_otg_host_if_t *host_if =
  43141. + dwc_otg_hcd->core_if->host_if;
  43142. +
  43143. + dwc_otg_hcd->flags.b.port_speed = hprt0.b.prtspd;
  43144. + if (microframe_schedule)
  43145. + init_hcd_usecs(dwc_otg_hcd);
  43146. +
  43147. + /* Every time when port enables calculate
  43148. + * HFIR.FrInterval
  43149. + */
  43150. + hfir.d32 = DWC_READ_REG32(&host_if->host_global_regs->hfir);
  43151. + hfir.b.frint = calc_frame_interval(dwc_otg_hcd->core_if);
  43152. + DWC_WRITE_REG32(&host_if->host_global_regs->hfir, hfir.d32);
  43153. +
  43154. + /* Check if we need to adjust the PHY clock speed for
  43155. + * low power and adjust it */
  43156. + if (params->host_support_fs_ls_low_power) {
  43157. + gusbcfg_data_t usbcfg;
  43158. +
  43159. + usbcfg.d32 =
  43160. + DWC_READ_REG32(&global_regs->gusbcfg);
  43161. +
  43162. + if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_LOW_SPEED
  43163. + || hprt0.b.prtspd ==
  43164. + DWC_HPRT0_PRTSPD_FULL_SPEED) {
  43165. + /*
  43166. + * Low power
  43167. + */
  43168. + hcfg_data_t hcfg;
  43169. + if (usbcfg.b.phylpwrclksel == 0) {
  43170. + /* Set PHY low power clock select for FS/LS devices */
  43171. + usbcfg.b.phylpwrclksel = 1;
  43172. + DWC_WRITE_REG32
  43173. + (&global_regs->gusbcfg,
  43174. + usbcfg.d32);
  43175. + do_reset = 1;
  43176. + }
  43177. +
  43178. + hcfg.d32 =
  43179. + DWC_READ_REG32
  43180. + (&host_if->host_global_regs->hcfg);
  43181. +
  43182. + if (hprt0.b.prtspd ==
  43183. + DWC_HPRT0_PRTSPD_LOW_SPEED
  43184. + && params->host_ls_low_power_phy_clk
  43185. + ==
  43186. + DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ)
  43187. + {
  43188. + /* 6 MHZ */
  43189. + DWC_DEBUGPL(DBG_CIL,
  43190. + "FS_PHY programming HCFG to 6 MHz (Low Power)\n");
  43191. + if (hcfg.b.fslspclksel !=
  43192. + DWC_HCFG_6_MHZ) {
  43193. + hcfg.b.fslspclksel =
  43194. + DWC_HCFG_6_MHZ;
  43195. + DWC_WRITE_REG32
  43196. + (&host_if->host_global_regs->hcfg,
  43197. + hcfg.d32);
  43198. + do_reset = 1;
  43199. + }
  43200. + } else {
  43201. + /* 48 MHZ */
  43202. + DWC_DEBUGPL(DBG_CIL,
  43203. + "FS_PHY programming HCFG to 48 MHz ()\n");
  43204. + if (hcfg.b.fslspclksel !=
  43205. + DWC_HCFG_48_MHZ) {
  43206. + hcfg.b.fslspclksel =
  43207. + DWC_HCFG_48_MHZ;
  43208. + DWC_WRITE_REG32
  43209. + (&host_if->host_global_regs->hcfg,
  43210. + hcfg.d32);
  43211. + do_reset = 1;
  43212. + }
  43213. + }
  43214. + } else {
  43215. + /*
  43216. + * Not low power
  43217. + */
  43218. + if (usbcfg.b.phylpwrclksel == 1) {
  43219. + usbcfg.b.phylpwrclksel = 0;
  43220. + DWC_WRITE_REG32
  43221. + (&global_regs->gusbcfg,
  43222. + usbcfg.d32);
  43223. + do_reset = 1;
  43224. + }
  43225. + }
  43226. +
  43227. + if (do_reset) {
  43228. + DWC_TASK_SCHEDULE(dwc_otg_hcd->reset_tasklet);
  43229. + }
  43230. + }
  43231. +
  43232. + if (!do_reset) {
  43233. + /* Port has been enabled set the reset change flag */
  43234. + dwc_otg_hcd->flags.b.port_reset_change = 1;
  43235. + }
  43236. + } else {
  43237. + dwc_otg_hcd->flags.b.port_enable_change = 1;
  43238. + }
  43239. + retval |= 1;
  43240. + }
  43241. +
  43242. + /** Overcurrent Change Interrupt */
  43243. + if (hprt0.b.prtovrcurrchng) {
  43244. + DWC_DEBUGPL(DBG_HCD, " --Port Interrupt HPRT0=0x%08x "
  43245. + "Port Overcurrent Changed--\n", hprt0.d32);
  43246. + dwc_otg_hcd->flags.b.port_over_current_change = 1;
  43247. + hprt0_modify.b.prtovrcurrchng = 1;
  43248. + retval |= 1;
  43249. + }
  43250. +
  43251. + /* Clear Port Interrupts */
  43252. + DWC_WRITE_REG32(dwc_otg_hcd->core_if->host_if->hprt0, hprt0_modify.d32);
  43253. +
  43254. + return retval;
  43255. +}
  43256. +
  43257. +/** This interrupt indicates that one or more host channels has a pending
  43258. + * interrupt. There are multiple conditions that can cause each host channel
  43259. + * interrupt. This function determines which conditions have occurred for each
  43260. + * host channel interrupt and handles them appropriately. */
  43261. +int32_t dwc_otg_hcd_handle_hc_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  43262. +{
  43263. + int i;
  43264. + int retval = 0;
  43265. + haint_data_t haint = { .d32 = 0 } ;
  43266. +
  43267. + /* Clear appropriate bits in HCINTn to clear the interrupt bit in
  43268. + * GINTSTS */
  43269. +
  43270. + if (!fiq_fsm_enable)
  43271. + haint.d32 = dwc_otg_read_host_all_channels_intr(dwc_otg_hcd->core_if);
  43272. +
  43273. + // Overwrite with saved interrupts from fiq handler
  43274. + if(fiq_fsm_enable)
  43275. + {
  43276. + /* check the mask? */
  43277. + local_fiq_disable();
  43278. + fiq_fsm_spin_lock(&dwc_otg_hcd->fiq_state->lock);
  43279. + haint.b2.chint |= ~(dwc_otg_hcd->fiq_state->haintmsk_saved.b2.chint);
  43280. + dwc_otg_hcd->fiq_state->haintmsk_saved.b2.chint = ~0;
  43281. + fiq_fsm_spin_unlock(&dwc_otg_hcd->fiq_state->lock);
  43282. + local_fiq_enable();
  43283. + }
  43284. +
  43285. + for (i = 0; i < dwc_otg_hcd->core_if->core_params->host_channels; i++) {
  43286. + if (haint.b2.chint & (1 << i)) {
  43287. + retval |= dwc_otg_hcd_handle_hc_n_intr(dwc_otg_hcd, i);
  43288. + }
  43289. + }
  43290. +
  43291. + return retval;
  43292. +}
  43293. +
  43294. +/**
  43295. + * Gets the actual length of a transfer after the transfer halts. _halt_status
  43296. + * holds the reason for the halt.
  43297. + *
  43298. + * For IN transfers where halt_status is DWC_OTG_HC_XFER_COMPLETE,
  43299. + * *short_read is set to 1 upon return if less than the requested
  43300. + * number of bytes were transferred. Otherwise, *short_read is set to 0 upon
  43301. + * return. short_read may also be NULL on entry, in which case it remains
  43302. + * unchanged.
  43303. + */
  43304. +static uint32_t get_actual_xfer_length(dwc_hc_t * hc,
  43305. + dwc_otg_hc_regs_t * hc_regs,
  43306. + dwc_otg_qtd_t * qtd,
  43307. + dwc_otg_halt_status_e halt_status,
  43308. + int *short_read)
  43309. +{
  43310. + hctsiz_data_t hctsiz;
  43311. + uint32_t length;
  43312. +
  43313. + if (short_read != NULL) {
  43314. + *short_read = 0;
  43315. + }
  43316. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  43317. +
  43318. + if (halt_status == DWC_OTG_HC_XFER_COMPLETE) {
  43319. + if (hc->ep_is_in) {
  43320. + length = hc->xfer_len - hctsiz.b.xfersize;
  43321. + if (short_read != NULL) {
  43322. + *short_read = (hctsiz.b.xfersize != 0);
  43323. + }
  43324. + } else if (hc->qh->do_split) {
  43325. + //length = split_out_xfersize[hc->hc_num];
  43326. + length = qtd->ssplit_out_xfer_count;
  43327. + } else {
  43328. + length = hc->xfer_len;
  43329. + }
  43330. + } else {
  43331. + /*
  43332. + * Must use the hctsiz.pktcnt field to determine how much data
  43333. + * has been transferred. This field reflects the number of
  43334. + * packets that have been transferred via the USB. This is
  43335. + * always an integral number of packets if the transfer was
  43336. + * halted before its normal completion. (Can't use the
  43337. + * hctsiz.xfersize field because that reflects the number of
  43338. + * bytes transferred via the AHB, not the USB).
  43339. + */
  43340. + length =
  43341. + (hc->start_pkt_count - hctsiz.b.pktcnt) * hc->max_packet;
  43342. + }
  43343. +
  43344. + return length;
  43345. +}
  43346. +
  43347. +/**
  43348. + * Updates the state of the URB after a Transfer Complete interrupt on the
  43349. + * host channel. Updates the actual_length field of the URB based on the
  43350. + * number of bytes transferred via the host channel. Sets the URB status
  43351. + * if the data transfer is finished.
  43352. + *
  43353. + * @return 1 if the data transfer specified by the URB is completely finished,
  43354. + * 0 otherwise.
  43355. + */
  43356. +static int update_urb_state_xfer_comp(dwc_hc_t * hc,
  43357. + dwc_otg_hc_regs_t * hc_regs,
  43358. + dwc_otg_hcd_urb_t * urb,
  43359. + dwc_otg_qtd_t * qtd)
  43360. +{
  43361. + int xfer_done = 0;
  43362. + int short_read = 0;
  43363. +
  43364. + int xfer_length;
  43365. +
  43366. + xfer_length = get_actual_xfer_length(hc, hc_regs, qtd,
  43367. + DWC_OTG_HC_XFER_COMPLETE,
  43368. + &short_read);
  43369. +
  43370. + if (urb->actual_length + xfer_length > urb->length) {
  43371. + printk_once(KERN_DEBUG "dwc_otg: DEVICE:%03d : %s:%d:trimming xfer length\n",
  43372. + hc->dev_addr, __func__, __LINE__);
  43373. + xfer_length = urb->length - urb->actual_length;
  43374. + }
  43375. +
  43376. + /* non DWORD-aligned buffer case handling. */
  43377. + if (hc->align_buff && xfer_length && hc->ep_is_in) {
  43378. + dwc_memcpy(urb->buf + urb->actual_length, hc->qh->dw_align_buf,
  43379. + xfer_length);
  43380. + }
  43381. +
  43382. + urb->actual_length += xfer_length;
  43383. +
  43384. + if (xfer_length && (hc->ep_type == DWC_OTG_EP_TYPE_BULK) &&
  43385. + (urb->flags & URB_SEND_ZERO_PACKET)
  43386. + && (urb->actual_length == urb->length)
  43387. + && !(urb->length % hc->max_packet)) {
  43388. + xfer_done = 0;
  43389. + } else if (short_read || urb->actual_length >= urb->length) {
  43390. + xfer_done = 1;
  43391. + urb->status = 0;
  43392. + }
  43393. +
  43394. +#ifdef DEBUG
  43395. + {
  43396. + hctsiz_data_t hctsiz;
  43397. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  43398. + DWC_DEBUGPL(DBG_HCDV, "DWC_otg: %s: %s, channel %d\n",
  43399. + __func__, (hc->ep_is_in ? "IN" : "OUT"),
  43400. + hc->hc_num);
  43401. + DWC_DEBUGPL(DBG_HCDV, " hc->xfer_len %d\n", hc->xfer_len);
  43402. + DWC_DEBUGPL(DBG_HCDV, " hctsiz.xfersize %d\n",
  43403. + hctsiz.b.xfersize);
  43404. + DWC_DEBUGPL(DBG_HCDV, " urb->transfer_buffer_length %d\n",
  43405. + urb->length);
  43406. + DWC_DEBUGPL(DBG_HCDV, " urb->actual_length %d\n",
  43407. + urb->actual_length);
  43408. + DWC_DEBUGPL(DBG_HCDV, " short_read %d, xfer_done %d\n",
  43409. + short_read, xfer_done);
  43410. + }
  43411. +#endif
  43412. +
  43413. + return xfer_done;
  43414. +}
  43415. +
  43416. +/*
  43417. + * Save the starting data toggle for the next transfer. The data toggle is
  43418. + * saved in the QH for non-control transfers and it's saved in the QTD for
  43419. + * control transfers.
  43420. + */
  43421. +void dwc_otg_hcd_save_data_toggle(dwc_hc_t * hc,
  43422. + dwc_otg_hc_regs_t * hc_regs, dwc_otg_qtd_t * qtd)
  43423. +{
  43424. + hctsiz_data_t hctsiz;
  43425. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  43426. +
  43427. + if (hc->ep_type != DWC_OTG_EP_TYPE_CONTROL) {
  43428. + dwc_otg_qh_t *qh = hc->qh;
  43429. + if (hctsiz.b.pid == DWC_HCTSIZ_DATA0) {
  43430. + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
  43431. + } else {
  43432. + qh->data_toggle = DWC_OTG_HC_PID_DATA1;
  43433. + }
  43434. + } else {
  43435. + if (hctsiz.b.pid == DWC_HCTSIZ_DATA0) {
  43436. + qtd->data_toggle = DWC_OTG_HC_PID_DATA0;
  43437. + } else {
  43438. + qtd->data_toggle = DWC_OTG_HC_PID_DATA1;
  43439. + }
  43440. + }
  43441. +}
  43442. +
  43443. +/**
  43444. + * Updates the state of an Isochronous URB when the transfer is stopped for
  43445. + * any reason. The fields of the current entry in the frame descriptor array
  43446. + * are set based on the transfer state and the input _halt_status. Completes
  43447. + * the Isochronous URB if all the URB frames have been completed.
  43448. + *
  43449. + * @return DWC_OTG_HC_XFER_COMPLETE if there are more frames remaining to be
  43450. + * transferred in the URB. Otherwise return DWC_OTG_HC_XFER_URB_COMPLETE.
  43451. + */
  43452. +static dwc_otg_halt_status_e
  43453. +update_isoc_urb_state(dwc_otg_hcd_t * hcd,
  43454. + dwc_hc_t * hc,
  43455. + dwc_otg_hc_regs_t * hc_regs,
  43456. + dwc_otg_qtd_t * qtd, dwc_otg_halt_status_e halt_status)
  43457. +{
  43458. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  43459. + dwc_otg_halt_status_e ret_val = halt_status;
  43460. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  43461. +
  43462. + frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
  43463. + switch (halt_status) {
  43464. + case DWC_OTG_HC_XFER_COMPLETE:
  43465. + frame_desc->status = 0;
  43466. + frame_desc->actual_length =
  43467. + get_actual_xfer_length(hc, hc_regs, qtd, halt_status, NULL);
  43468. +
  43469. + /* non DWORD-aligned buffer case handling. */
  43470. + if (hc->align_buff && frame_desc->actual_length && hc->ep_is_in) {
  43471. + dwc_memcpy(urb->buf + frame_desc->offset + qtd->isoc_split_offset,
  43472. + hc->qh->dw_align_buf, frame_desc->actual_length);
  43473. + }
  43474. +
  43475. + break;
  43476. + case DWC_OTG_HC_XFER_FRAME_OVERRUN:
  43477. + urb->error_count++;
  43478. + if (hc->ep_is_in) {
  43479. + frame_desc->status = -DWC_E_NO_STREAM_RES;
  43480. + } else {
  43481. + frame_desc->status = -DWC_E_COMMUNICATION;
  43482. + }
  43483. + frame_desc->actual_length = 0;
  43484. + break;
  43485. + case DWC_OTG_HC_XFER_BABBLE_ERR:
  43486. + urb->error_count++;
  43487. + frame_desc->status = -DWC_E_OVERFLOW;
  43488. + /* Don't need to update actual_length in this case. */
  43489. + break;
  43490. + case DWC_OTG_HC_XFER_XACT_ERR:
  43491. + urb->error_count++;
  43492. + frame_desc->status = -DWC_E_PROTOCOL;
  43493. + frame_desc->actual_length =
  43494. + get_actual_xfer_length(hc, hc_regs, qtd, halt_status, NULL);
  43495. +
  43496. + /* non DWORD-aligned buffer case handling. */
  43497. + if (hc->align_buff && frame_desc->actual_length && hc->ep_is_in) {
  43498. + dwc_memcpy(urb->buf + frame_desc->offset + qtd->isoc_split_offset,
  43499. + hc->qh->dw_align_buf, frame_desc->actual_length);
  43500. + }
  43501. + /* Skip whole frame */
  43502. + if (hc->qh->do_split && (hc->ep_type == DWC_OTG_EP_TYPE_ISOC) &&
  43503. + hc->ep_is_in && hcd->core_if->dma_enable) {
  43504. + qtd->complete_split = 0;
  43505. + qtd->isoc_split_offset = 0;
  43506. + }
  43507. +
  43508. + break;
  43509. + default:
  43510. + DWC_ASSERT(1, "Unhandled _halt_status (%d)\n", halt_status);
  43511. + break;
  43512. + }
  43513. + if (++qtd->isoc_frame_index == urb->packet_count) {
  43514. + /*
  43515. + * urb->status is not used for isoc transfers.
  43516. + * The individual frame_desc statuses are used instead.
  43517. + */
  43518. + hcd->fops->complete(hcd, urb->priv, urb, 0);
  43519. + ret_val = DWC_OTG_HC_XFER_URB_COMPLETE;
  43520. + } else {
  43521. + ret_val = DWC_OTG_HC_XFER_COMPLETE;
  43522. + }
  43523. + return ret_val;
  43524. +}
  43525. +
  43526. +/**
  43527. + * Frees the first QTD in the QH's list if free_qtd is 1. For non-periodic
  43528. + * QHs, removes the QH from the active non-periodic schedule. If any QTDs are
  43529. + * still linked to the QH, the QH is added to the end of the inactive
  43530. + * non-periodic schedule. For periodic QHs, removes the QH from the periodic
  43531. + * schedule if no more QTDs are linked to the QH.
  43532. + */
  43533. +static void deactivate_qh(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh, int free_qtd)
  43534. +{
  43535. + int continue_split = 0;
  43536. + dwc_otg_qtd_t *qtd;
  43537. +
  43538. + DWC_DEBUGPL(DBG_HCDV, " %s(%p,%p,%d)\n", __func__, hcd, qh, free_qtd);
  43539. +
  43540. + qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
  43541. +
  43542. + if (qtd->complete_split) {
  43543. + continue_split = 1;
  43544. + } else if (qtd->isoc_split_pos == DWC_HCSPLIT_XACTPOS_MID ||
  43545. + qtd->isoc_split_pos == DWC_HCSPLIT_XACTPOS_END) {
  43546. + continue_split = 1;
  43547. + }
  43548. +
  43549. + if (free_qtd) {
  43550. + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
  43551. + continue_split = 0;
  43552. + }
  43553. +
  43554. + qh->channel = NULL;
  43555. + dwc_otg_hcd_qh_deactivate(hcd, qh, continue_split);
  43556. +}
  43557. +
  43558. +/**
  43559. + * Releases a host channel for use by other transfers. Attempts to select and
  43560. + * queue more transactions since at least one host channel is available.
  43561. + *
  43562. + * @param hcd The HCD state structure.
  43563. + * @param hc The host channel to release.
  43564. + * @param qtd The QTD associated with the host channel. This QTD may be freed
  43565. + * if the transfer is complete or an error has occurred.
  43566. + * @param halt_status Reason the channel is being released. This status
  43567. + * determines the actions taken by this function.
  43568. + */
  43569. +static void release_channel(dwc_otg_hcd_t * hcd,
  43570. + dwc_hc_t * hc,
  43571. + dwc_otg_qtd_t * qtd,
  43572. + dwc_otg_halt_status_e halt_status)
  43573. +{
  43574. + dwc_otg_transaction_type_e tr_type;
  43575. + int free_qtd;
  43576. +
  43577. + int hog_port = 0;
  43578. +
  43579. + DWC_DEBUGPL(DBG_HCDV, " %s: channel %d, halt_status %d, xfer_len %d\n",
  43580. + __func__, hc->hc_num, halt_status, hc->xfer_len);
  43581. +
  43582. + if(fiq_fsm_enable && hc->do_split) {
  43583. + if(!hc->ep_is_in && hc->ep_type == UE_ISOCHRONOUS) {
  43584. + if(hc->xact_pos == DWC_HCSPLIT_XACTPOS_MID ||
  43585. + hc->xact_pos == DWC_HCSPLIT_XACTPOS_BEGIN) {
  43586. + hog_port = 0;
  43587. + }
  43588. + }
  43589. + }
  43590. +
  43591. + switch (halt_status) {
  43592. + case DWC_OTG_HC_XFER_URB_COMPLETE:
  43593. + free_qtd = 1;
  43594. + break;
  43595. + case DWC_OTG_HC_XFER_AHB_ERR:
  43596. + case DWC_OTG_HC_XFER_STALL:
  43597. + case DWC_OTG_HC_XFER_BABBLE_ERR:
  43598. + free_qtd = 1;
  43599. + break;
  43600. + case DWC_OTG_HC_XFER_XACT_ERR:
  43601. + if (qtd->error_count >= 3) {
  43602. + DWC_DEBUGPL(DBG_HCDV,
  43603. + " Complete URB with transaction error\n");
  43604. + free_qtd = 1;
  43605. + qtd->urb->status = -DWC_E_PROTOCOL;
  43606. + hcd->fops->complete(hcd, qtd->urb->priv,
  43607. + qtd->urb, -DWC_E_PROTOCOL);
  43608. + } else {
  43609. + free_qtd = 0;
  43610. + }
  43611. + break;
  43612. + case DWC_OTG_HC_XFER_URB_DEQUEUE:
  43613. + /*
  43614. + * The QTD has already been removed and the QH has been
  43615. + * deactivated. Don't want to do anything except release the
  43616. + * host channel and try to queue more transfers.
  43617. + */
  43618. + goto cleanup;
  43619. + case DWC_OTG_HC_XFER_NO_HALT_STATUS:
  43620. + free_qtd = 0;
  43621. + break;
  43622. + case DWC_OTG_HC_XFER_PERIODIC_INCOMPLETE:
  43623. + DWC_DEBUGPL(DBG_HCDV,
  43624. + " Complete URB with I/O error\n");
  43625. + free_qtd = 1;
  43626. + qtd->urb->status = -DWC_E_IO;
  43627. + hcd->fops->complete(hcd, qtd->urb->priv,
  43628. + qtd->urb, -DWC_E_IO);
  43629. + break;
  43630. + default:
  43631. + free_qtd = 0;
  43632. + break;
  43633. + }
  43634. +
  43635. + deactivate_qh(hcd, hc->qh, free_qtd);
  43636. +
  43637. +cleanup:
  43638. + /*
  43639. + * Release the host channel for use by other transfers. The cleanup
  43640. + * function clears the channel interrupt enables and conditions, so
  43641. + * there's no need to clear the Channel Halted interrupt separately.
  43642. + */
  43643. + if (fiq_fsm_enable && hcd->fiq_state->channel[hc->hc_num].fsm != FIQ_PASSTHROUGH)
  43644. + dwc_otg_cleanup_fiq_channel(hcd, hc->hc_num);
  43645. + dwc_otg_hc_cleanup(hcd->core_if, hc);
  43646. + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, hc, hc_list_entry);
  43647. +
  43648. + if (!microframe_schedule) {
  43649. + switch (hc->ep_type) {
  43650. + case DWC_OTG_EP_TYPE_CONTROL:
  43651. + case DWC_OTG_EP_TYPE_BULK:
  43652. + hcd->non_periodic_channels--;
  43653. + break;
  43654. +
  43655. + default:
  43656. + /*
  43657. + * Don't release reservations for periodic channels here.
  43658. + * That's done when a periodic transfer is descheduled (i.e.
  43659. + * when the QH is removed from the periodic schedule).
  43660. + */
  43661. + break;
  43662. + }
  43663. + } else {
  43664. + hcd->available_host_channels++;
  43665. + fiq_print(FIQDBG_INT, hcd->fiq_state, "AHC = %d ", hcd->available_host_channels);
  43666. + }
  43667. +
  43668. + /* Try to queue more transfers now that there's a free channel. */
  43669. + tr_type = dwc_otg_hcd_select_transactions(hcd);
  43670. + if (tr_type != DWC_OTG_TRANSACTION_NONE) {
  43671. + dwc_otg_hcd_queue_transactions(hcd, tr_type);
  43672. + }
  43673. +}
  43674. +
  43675. +/**
  43676. + * Halts a host channel. If the channel cannot be halted immediately because
  43677. + * the request queue is full, this function ensures that the FIFO empty
  43678. + * interrupt for the appropriate queue is enabled so that the halt request can
  43679. + * be queued when there is space in the request queue.
  43680. + *
  43681. + * This function may also be called in DMA mode. In that case, the channel is
  43682. + * simply released since the core always halts the channel automatically in
  43683. + * DMA mode.
  43684. + */
  43685. +static void halt_channel(dwc_otg_hcd_t * hcd,
  43686. + dwc_hc_t * hc,
  43687. + dwc_otg_qtd_t * qtd, dwc_otg_halt_status_e halt_status)
  43688. +{
  43689. + if (hcd->core_if->dma_enable) {
  43690. + release_channel(hcd, hc, qtd, halt_status);
  43691. + return;
  43692. + }
  43693. +
  43694. + /* Slave mode processing... */
  43695. + dwc_otg_hc_halt(hcd->core_if, hc, halt_status);
  43696. +
  43697. + if (hc->halt_on_queue) {
  43698. + gintmsk_data_t gintmsk = {.d32 = 0 };
  43699. + dwc_otg_core_global_regs_t *global_regs;
  43700. + global_regs = hcd->core_if->core_global_regs;
  43701. +
  43702. + if (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL ||
  43703. + hc->ep_type == DWC_OTG_EP_TYPE_BULK) {
  43704. + /*
  43705. + * Make sure the Non-periodic Tx FIFO empty interrupt
  43706. + * is enabled so that the non-periodic schedule will
  43707. + * be processed.
  43708. + */
  43709. + gintmsk.b.nptxfempty = 1;
  43710. + if (fiq_enable) {
  43711. + local_fiq_disable();
  43712. + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
  43713. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmsk.d32);
  43714. + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
  43715. + local_fiq_enable();
  43716. + } else {
  43717. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmsk.d32);
  43718. + }
  43719. + } else {
  43720. + /*
  43721. + * Move the QH from the periodic queued schedule to
  43722. + * the periodic assigned schedule. This allows the
  43723. + * halt to be queued when the periodic schedule is
  43724. + * processed.
  43725. + */
  43726. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_assigned,
  43727. + &hc->qh->qh_list_entry);
  43728. +
  43729. + /*
  43730. + * Make sure the Periodic Tx FIFO Empty interrupt is
  43731. + * enabled so that the periodic schedule will be
  43732. + * processed.
  43733. + */
  43734. + gintmsk.b.ptxfempty = 1;
  43735. + if (fiq_enable) {
  43736. + local_fiq_disable();
  43737. + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
  43738. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmsk.d32);
  43739. + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
  43740. + local_fiq_enable();
  43741. + } else {
  43742. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmsk.d32);
  43743. + }
  43744. + }
  43745. + }
  43746. +}
  43747. +
  43748. +/**
  43749. + * Performs common cleanup for non-periodic transfers after a Transfer
  43750. + * Complete interrupt. This function should be called after any endpoint type
  43751. + * specific handling is finished to release the host channel.
  43752. + */
  43753. +static void complete_non_periodic_xfer(dwc_otg_hcd_t * hcd,
  43754. + dwc_hc_t * hc,
  43755. + dwc_otg_hc_regs_t * hc_regs,
  43756. + dwc_otg_qtd_t * qtd,
  43757. + dwc_otg_halt_status_e halt_status)
  43758. +{
  43759. + hcint_data_t hcint;
  43760. +
  43761. + qtd->error_count = 0;
  43762. +
  43763. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  43764. + if (hcint.b.nyet) {
  43765. + /*
  43766. + * Got a NYET on the last transaction of the transfer. This
  43767. + * means that the endpoint should be in the PING state at the
  43768. + * beginning of the next transfer.
  43769. + */
  43770. + hc->qh->ping_state = 1;
  43771. + clear_hc_int(hc_regs, nyet);
  43772. + }
  43773. +
  43774. + /*
  43775. + * Always halt and release the host channel to make it available for
  43776. + * more transfers. There may still be more phases for a control
  43777. + * transfer or more data packets for a bulk transfer at this point,
  43778. + * but the host channel is still halted. A channel will be reassigned
  43779. + * to the transfer when the non-periodic schedule is processed after
  43780. + * the channel is released. This allows transactions to be queued
  43781. + * properly via dwc_otg_hcd_queue_transactions, which also enables the
  43782. + * Tx FIFO Empty interrupt if necessary.
  43783. + */
  43784. + if (hc->ep_is_in) {
  43785. + /*
  43786. + * IN transfers in Slave mode require an explicit disable to
  43787. + * halt the channel. (In DMA mode, this call simply releases
  43788. + * the channel.)
  43789. + */
  43790. + halt_channel(hcd, hc, qtd, halt_status);
  43791. + } else {
  43792. + /*
  43793. + * The channel is automatically disabled by the core for OUT
  43794. + * transfers in Slave mode.
  43795. + */
  43796. + release_channel(hcd, hc, qtd, halt_status);
  43797. + }
  43798. +}
  43799. +
  43800. +/**
  43801. + * Performs common cleanup for periodic transfers after a Transfer Complete
  43802. + * interrupt. This function should be called after any endpoint type specific
  43803. + * handling is finished to release the host channel.
  43804. + */
  43805. +static void complete_periodic_xfer(dwc_otg_hcd_t * hcd,
  43806. + dwc_hc_t * hc,
  43807. + dwc_otg_hc_regs_t * hc_regs,
  43808. + dwc_otg_qtd_t * qtd,
  43809. + dwc_otg_halt_status_e halt_status)
  43810. +{
  43811. + hctsiz_data_t hctsiz;
  43812. + qtd->error_count = 0;
  43813. +
  43814. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  43815. + if (!hc->ep_is_in || hctsiz.b.pktcnt == 0) {
  43816. + /* Core halts channel in these cases. */
  43817. + release_channel(hcd, hc, qtd, halt_status);
  43818. + } else {
  43819. + /* Flush any outstanding requests from the Tx queue. */
  43820. + halt_channel(hcd, hc, qtd, halt_status);
  43821. + }
  43822. +}
  43823. +
  43824. +static int32_t handle_xfercomp_isoc_split_in(dwc_otg_hcd_t * hcd,
  43825. + dwc_hc_t * hc,
  43826. + dwc_otg_hc_regs_t * hc_regs,
  43827. + dwc_otg_qtd_t * qtd)
  43828. +{
  43829. + uint32_t len;
  43830. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  43831. + frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  43832. +
  43833. + len = get_actual_xfer_length(hc, hc_regs, qtd,
  43834. + DWC_OTG_HC_XFER_COMPLETE, NULL);
  43835. +
  43836. + if (!len) {
  43837. + qtd->complete_split = 0;
  43838. + qtd->isoc_split_offset = 0;
  43839. + return 0;
  43840. + }
  43841. + frame_desc->actual_length += len;
  43842. +
  43843. + if (hc->align_buff && len)
  43844. + dwc_memcpy(qtd->urb->buf + frame_desc->offset +
  43845. + qtd->isoc_split_offset, hc->qh->dw_align_buf, len);
  43846. + qtd->isoc_split_offset += len;
  43847. +
  43848. + if (frame_desc->length == frame_desc->actual_length) {
  43849. + frame_desc->status = 0;
  43850. + qtd->isoc_frame_index++;
  43851. + qtd->complete_split = 0;
  43852. + qtd->isoc_split_offset = 0;
  43853. + }
  43854. +
  43855. + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
  43856. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  43857. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  43858. + } else {
  43859. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  43860. + }
  43861. +
  43862. + return 1; /* Indicates that channel released */
  43863. +}
  43864. +
  43865. +/**
  43866. + * Handles a host channel Transfer Complete interrupt. This handler may be
  43867. + * called in either DMA mode or Slave mode.
  43868. + */
  43869. +static int32_t handle_hc_xfercomp_intr(dwc_otg_hcd_t * hcd,
  43870. + dwc_hc_t * hc,
  43871. + dwc_otg_hc_regs_t * hc_regs,
  43872. + dwc_otg_qtd_t * qtd)
  43873. +{
  43874. + int urb_xfer_done;
  43875. + dwc_otg_halt_status_e halt_status = DWC_OTG_HC_XFER_COMPLETE;
  43876. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  43877. + int pipe_type = dwc_otg_hcd_get_pipe_type(&urb->pipe_info);
  43878. +
  43879. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  43880. + "Transfer Complete--\n", hc->hc_num);
  43881. +
  43882. + if (hcd->core_if->dma_desc_enable) {
  43883. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs, halt_status);
  43884. + if (pipe_type == UE_ISOCHRONOUS) {
  43885. + /* Do not disable the interrupt, just clear it */
  43886. + clear_hc_int(hc_regs, xfercomp);
  43887. + return 1;
  43888. + }
  43889. + goto handle_xfercomp_done;
  43890. + }
  43891. +
  43892. + /*
  43893. + * Handle xfer complete on CSPLIT.
  43894. + */
  43895. +
  43896. + if (hc->qh->do_split) {
  43897. + if ((hc->ep_type == DWC_OTG_EP_TYPE_ISOC) && hc->ep_is_in
  43898. + && hcd->core_if->dma_enable) {
  43899. + if (qtd->complete_split
  43900. + && handle_xfercomp_isoc_split_in(hcd, hc, hc_regs,
  43901. + qtd))
  43902. + goto handle_xfercomp_done;
  43903. + } else {
  43904. + qtd->complete_split = 0;
  43905. + }
  43906. + }
  43907. +
  43908. + /* Update the QTD and URB states. */
  43909. + switch (pipe_type) {
  43910. + case UE_CONTROL:
  43911. + switch (qtd->control_phase) {
  43912. + case DWC_OTG_CONTROL_SETUP:
  43913. + if (urb->length > 0) {
  43914. + qtd->control_phase = DWC_OTG_CONTROL_DATA;
  43915. + } else {
  43916. + qtd->control_phase = DWC_OTG_CONTROL_STATUS;
  43917. + }
  43918. + DWC_DEBUGPL(DBG_HCDV,
  43919. + " Control setup transaction done\n");
  43920. + halt_status = DWC_OTG_HC_XFER_COMPLETE;
  43921. + break;
  43922. + case DWC_OTG_CONTROL_DATA:{
  43923. + urb_xfer_done =
  43924. + update_urb_state_xfer_comp(hc, hc_regs, urb,
  43925. + qtd);
  43926. + if (urb_xfer_done) {
  43927. + qtd->control_phase =
  43928. + DWC_OTG_CONTROL_STATUS;
  43929. + DWC_DEBUGPL(DBG_HCDV,
  43930. + " Control data transfer done\n");
  43931. + } else {
  43932. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  43933. + }
  43934. + halt_status = DWC_OTG_HC_XFER_COMPLETE;
  43935. + break;
  43936. + }
  43937. + case DWC_OTG_CONTROL_STATUS:
  43938. + DWC_DEBUGPL(DBG_HCDV, " Control transfer complete\n");
  43939. + if (urb->status == -DWC_E_IN_PROGRESS) {
  43940. + urb->status = 0;
  43941. + }
  43942. + hcd->fops->complete(hcd, urb->priv, urb, urb->status);
  43943. + halt_status = DWC_OTG_HC_XFER_URB_COMPLETE;
  43944. + break;
  43945. + }
  43946. +
  43947. + complete_non_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
  43948. + break;
  43949. + case UE_BULK:
  43950. + DWC_DEBUGPL(DBG_HCDV, " Bulk transfer complete\n");
  43951. + urb_xfer_done =
  43952. + update_urb_state_xfer_comp(hc, hc_regs, urb, qtd);
  43953. + if (urb_xfer_done) {
  43954. + hcd->fops->complete(hcd, urb->priv, urb, urb->status);
  43955. + halt_status = DWC_OTG_HC_XFER_URB_COMPLETE;
  43956. + } else {
  43957. + halt_status = DWC_OTG_HC_XFER_COMPLETE;
  43958. + }
  43959. +
  43960. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  43961. + complete_non_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
  43962. + break;
  43963. + case UE_INTERRUPT:
  43964. + DWC_DEBUGPL(DBG_HCDV, " Interrupt transfer complete\n");
  43965. + urb_xfer_done =
  43966. + update_urb_state_xfer_comp(hc, hc_regs, urb, qtd);
  43967. +
  43968. + /*
  43969. + * Interrupt URB is done on the first transfer complete
  43970. + * interrupt.
  43971. + */
  43972. + if (urb_xfer_done) {
  43973. + hcd->fops->complete(hcd, urb->priv, urb, urb->status);
  43974. + halt_status = DWC_OTG_HC_XFER_URB_COMPLETE;
  43975. + } else {
  43976. + halt_status = DWC_OTG_HC_XFER_COMPLETE;
  43977. + }
  43978. +
  43979. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  43980. + complete_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
  43981. + break;
  43982. + case UE_ISOCHRONOUS:
  43983. + DWC_DEBUGPL(DBG_HCDV, " Isochronous transfer complete\n");
  43984. + if (qtd->isoc_split_pos == DWC_HCSPLIT_XACTPOS_ALL) {
  43985. + halt_status =
  43986. + update_isoc_urb_state(hcd, hc, hc_regs, qtd,
  43987. + DWC_OTG_HC_XFER_COMPLETE);
  43988. + }
  43989. + complete_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
  43990. + break;
  43991. + }
  43992. +
  43993. +handle_xfercomp_done:
  43994. + disable_hc_int(hc_regs, xfercompl);
  43995. +
  43996. + return 1;
  43997. +}
  43998. +
  43999. +/**
  44000. + * Handles a host channel STALL interrupt. This handler may be called in
  44001. + * either DMA mode or Slave mode.
  44002. + */
  44003. +static int32_t handle_hc_stall_intr(dwc_otg_hcd_t * hcd,
  44004. + dwc_hc_t * hc,
  44005. + dwc_otg_hc_regs_t * hc_regs,
  44006. + dwc_otg_qtd_t * qtd)
  44007. +{
  44008. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  44009. + int pipe_type = dwc_otg_hcd_get_pipe_type(&urb->pipe_info);
  44010. +
  44011. + DWC_DEBUGPL(DBG_HCD, "--Host Channel %d Interrupt: "
  44012. + "STALL Received--\n", hc->hc_num);
  44013. +
  44014. + if (hcd->core_if->dma_desc_enable) {
  44015. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs, DWC_OTG_HC_XFER_STALL);
  44016. + goto handle_stall_done;
  44017. + }
  44018. +
  44019. + if (pipe_type == UE_CONTROL) {
  44020. + hcd->fops->complete(hcd, urb->priv, urb, -DWC_E_PIPE);
  44021. + }
  44022. +
  44023. + if (pipe_type == UE_BULK || pipe_type == UE_INTERRUPT) {
  44024. + hcd->fops->complete(hcd, urb->priv, urb, -DWC_E_PIPE);
  44025. + /*
  44026. + * USB protocol requires resetting the data toggle for bulk
  44027. + * and interrupt endpoints when a CLEAR_FEATURE(ENDPOINT_HALT)
  44028. + * setup command is issued to the endpoint. Anticipate the
  44029. + * CLEAR_FEATURE command since a STALL has occurred and reset
  44030. + * the data toggle now.
  44031. + */
  44032. + hc->qh->data_toggle = 0;
  44033. + }
  44034. +
  44035. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_STALL);
  44036. +
  44037. +handle_stall_done:
  44038. + disable_hc_int(hc_regs, stall);
  44039. +
  44040. + return 1;
  44041. +}
  44042. +
  44043. +/*
  44044. + * Updates the state of the URB when a transfer has been stopped due to an
  44045. + * abnormal condition before the transfer completes. Modifies the
  44046. + * actual_length field of the URB to reflect the number of bytes that have
  44047. + * actually been transferred via the host channel.
  44048. + */
  44049. +static void update_urb_state_xfer_intr(dwc_hc_t * hc,
  44050. + dwc_otg_hc_regs_t * hc_regs,
  44051. + dwc_otg_hcd_urb_t * urb,
  44052. + dwc_otg_qtd_t * qtd,
  44053. + dwc_otg_halt_status_e halt_status)
  44054. +{
  44055. + uint32_t bytes_transferred = get_actual_xfer_length(hc, hc_regs, qtd,
  44056. + halt_status, NULL);
  44057. +
  44058. + if (urb->actual_length + bytes_transferred > urb->length) {
  44059. + printk_once(KERN_DEBUG "dwc_otg: DEVICE:%03d : %s:%d:trimming xfer length\n",
  44060. + hc->dev_addr, __func__, __LINE__);
  44061. + bytes_transferred = urb->length - urb->actual_length;
  44062. + }
  44063. +
  44064. + /* non DWORD-aligned buffer case handling. */
  44065. + if (hc->align_buff && bytes_transferred && hc->ep_is_in) {
  44066. + dwc_memcpy(urb->buf + urb->actual_length, hc->qh->dw_align_buf,
  44067. + bytes_transferred);
  44068. + }
  44069. +
  44070. + urb->actual_length += bytes_transferred;
  44071. +
  44072. +#ifdef DEBUG
  44073. + {
  44074. + hctsiz_data_t hctsiz;
  44075. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  44076. + DWC_DEBUGPL(DBG_HCDV, "DWC_otg: %s: %s, channel %d\n",
  44077. + __func__, (hc->ep_is_in ? "IN" : "OUT"),
  44078. + hc->hc_num);
  44079. + DWC_DEBUGPL(DBG_HCDV, " hc->start_pkt_count %d\n",
  44080. + hc->start_pkt_count);
  44081. + DWC_DEBUGPL(DBG_HCDV, " hctsiz.pktcnt %d\n", hctsiz.b.pktcnt);
  44082. + DWC_DEBUGPL(DBG_HCDV, " hc->max_packet %d\n", hc->max_packet);
  44083. + DWC_DEBUGPL(DBG_HCDV, " bytes_transferred %d\n",
  44084. + bytes_transferred);
  44085. + DWC_DEBUGPL(DBG_HCDV, " urb->actual_length %d\n",
  44086. + urb->actual_length);
  44087. + DWC_DEBUGPL(DBG_HCDV, " urb->transfer_buffer_length %d\n",
  44088. + urb->length);
  44089. + }
  44090. +#endif
  44091. +}
  44092. +
  44093. +/**
  44094. + * Handles a host channel NAK interrupt. This handler may be called in either
  44095. + * DMA mode or Slave mode.
  44096. + */
  44097. +static int32_t handle_hc_nak_intr(dwc_otg_hcd_t * hcd,
  44098. + dwc_hc_t * hc,
  44099. + dwc_otg_hc_regs_t * hc_regs,
  44100. + dwc_otg_qtd_t * qtd)
  44101. +{
  44102. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  44103. + "NAK Received--\n", hc->hc_num);
  44104. +
  44105. + /*
  44106. + * When we get bulk NAKs then remember this so we holdoff on this qh until
  44107. + * the beginning of the next frame
  44108. + */
  44109. + switch(dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
  44110. + case UE_BULK:
  44111. + case UE_CONTROL:
  44112. + if (nak_holdoff && qtd->qh->do_split)
  44113. + hc->qh->nak_frame = dwc_otg_hcd_get_frame_number(hcd);
  44114. + }
  44115. +
  44116. + /*
  44117. + * Handle NAK for IN/OUT SSPLIT/CSPLIT transfers, bulk, control, and
  44118. + * interrupt. Re-start the SSPLIT transfer.
  44119. + */
  44120. + if (hc->do_split) {
  44121. + if (hc->complete_split) {
  44122. + qtd->error_count = 0;
  44123. + }
  44124. + qtd->complete_split = 0;
  44125. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NAK);
  44126. + goto handle_nak_done;
  44127. + }
  44128. +
  44129. + switch (dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
  44130. + case UE_CONTROL:
  44131. + case UE_BULK:
  44132. + if (hcd->core_if->dma_enable && hc->ep_is_in) {
  44133. + /*
  44134. + * NAK interrupts are enabled on bulk/control IN
  44135. + * transfers in DMA mode for the sole purpose of
  44136. + * resetting the error count after a transaction error
  44137. + * occurs. The core will continue transferring data.
  44138. + * Disable other interrupts unmasked for the same
  44139. + * reason.
  44140. + */
  44141. + disable_hc_int(hc_regs, datatglerr);
  44142. + disable_hc_int(hc_regs, ack);
  44143. + qtd->error_count = 0;
  44144. + goto handle_nak_done;
  44145. + }
  44146. +
  44147. + /*
  44148. + * NAK interrupts normally occur during OUT transfers in DMA
  44149. + * or Slave mode. For IN transfers, more requests will be
  44150. + * queued as request queue space is available.
  44151. + */
  44152. + qtd->error_count = 0;
  44153. +
  44154. + if (!hc->qh->ping_state) {
  44155. + update_urb_state_xfer_intr(hc, hc_regs,
  44156. + qtd->urb, qtd,
  44157. + DWC_OTG_HC_XFER_NAK);
  44158. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  44159. +
  44160. + if (hc->speed == DWC_OTG_EP_SPEED_HIGH)
  44161. + hc->qh->ping_state = 1;
  44162. + }
  44163. +
  44164. + /*
  44165. + * Halt the channel so the transfer can be re-started from
  44166. + * the appropriate point or the PING protocol will
  44167. + * start/continue.
  44168. + */
  44169. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NAK);
  44170. + break;
  44171. + case UE_INTERRUPT:
  44172. + qtd->error_count = 0;
  44173. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NAK);
  44174. + break;
  44175. + case UE_ISOCHRONOUS:
  44176. + /* Should never get called for isochronous transfers. */
  44177. + DWC_ASSERT(1, "NACK interrupt for ISOC transfer\n");
  44178. + break;
  44179. + }
  44180. +
  44181. +handle_nak_done:
  44182. + disable_hc_int(hc_regs, nak);
  44183. +
  44184. + return 1;
  44185. +}
  44186. +
  44187. +/**
  44188. + * Handles a host channel ACK interrupt. This interrupt is enabled when
  44189. + * performing the PING protocol in Slave mode, when errors occur during
  44190. + * either Slave mode or DMA mode, and during Start Split transactions.
  44191. + */
  44192. +static int32_t handle_hc_ack_intr(dwc_otg_hcd_t * hcd,
  44193. + dwc_hc_t * hc,
  44194. + dwc_otg_hc_regs_t * hc_regs,
  44195. + dwc_otg_qtd_t * qtd)
  44196. +{
  44197. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  44198. + "ACK Received--\n", hc->hc_num);
  44199. +
  44200. + if (hc->do_split) {
  44201. + /*
  44202. + * Handle ACK on SSPLIT.
  44203. + * ACK should not occur in CSPLIT.
  44204. + */
  44205. + if (!hc->ep_is_in && hc->data_pid_start != DWC_OTG_HC_PID_SETUP) {
  44206. + qtd->ssplit_out_xfer_count = hc->xfer_len;
  44207. + }
  44208. + if (!(hc->ep_type == DWC_OTG_EP_TYPE_ISOC && !hc->ep_is_in)) {
  44209. + /* Don't need complete for isochronous out transfers. */
  44210. + qtd->complete_split = 1;
  44211. + }
  44212. +
  44213. + /* ISOC OUT */
  44214. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC && !hc->ep_is_in) {
  44215. + switch (hc->xact_pos) {
  44216. + case DWC_HCSPLIT_XACTPOS_ALL:
  44217. + break;
  44218. + case DWC_HCSPLIT_XACTPOS_END:
  44219. + qtd->isoc_split_pos = DWC_HCSPLIT_XACTPOS_ALL;
  44220. + qtd->isoc_split_offset = 0;
  44221. + break;
  44222. + case DWC_HCSPLIT_XACTPOS_BEGIN:
  44223. + case DWC_HCSPLIT_XACTPOS_MID:
  44224. + /*
  44225. + * For BEGIN or MID, calculate the length for
  44226. + * the next microframe to determine the correct
  44227. + * SSPLIT token, either MID or END.
  44228. + */
  44229. + {
  44230. + struct dwc_otg_hcd_iso_packet_desc
  44231. + *frame_desc;
  44232. +
  44233. + frame_desc =
  44234. + &qtd->urb->
  44235. + iso_descs[qtd->isoc_frame_index];
  44236. + qtd->isoc_split_offset += 188;
  44237. +
  44238. + if ((frame_desc->length -
  44239. + qtd->isoc_split_offset) <= 188) {
  44240. + qtd->isoc_split_pos =
  44241. + DWC_HCSPLIT_XACTPOS_END;
  44242. + } else {
  44243. + qtd->isoc_split_pos =
  44244. + DWC_HCSPLIT_XACTPOS_MID;
  44245. + }
  44246. +
  44247. + }
  44248. + break;
  44249. + }
  44250. + } else {
  44251. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_ACK);
  44252. + }
  44253. + } else {
  44254. + /*
  44255. + * An unmasked ACK on a non-split DMA transaction is
  44256. + * for the sole purpose of resetting error counts. Disable other
  44257. + * interrupts unmasked for the same reason.
  44258. + */
  44259. + if(hcd->core_if->dma_enable) {
  44260. + disable_hc_int(hc_regs, datatglerr);
  44261. + disable_hc_int(hc_regs, nak);
  44262. + }
  44263. + qtd->error_count = 0;
  44264. +
  44265. + if (hc->qh->ping_state) {
  44266. + hc->qh->ping_state = 0;
  44267. + /*
  44268. + * Halt the channel so the transfer can be re-started
  44269. + * from the appropriate point. This only happens in
  44270. + * Slave mode. In DMA mode, the ping_state is cleared
  44271. + * when the transfer is started because the core
  44272. + * automatically executes the PING, then the transfer.
  44273. + */
  44274. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_ACK);
  44275. + }
  44276. + }
  44277. +
  44278. + /*
  44279. + * If the ACK occurred when _not_ in the PING state, let the channel
  44280. + * continue transferring data after clearing the error count.
  44281. + */
  44282. +
  44283. + disable_hc_int(hc_regs, ack);
  44284. +
  44285. + return 1;
  44286. +}
  44287. +
  44288. +/**
  44289. + * Handles a host channel NYET interrupt. This interrupt should only occur on
  44290. + * Bulk and Control OUT endpoints and for complete split transactions. If a
  44291. + * NYET occurs at the same time as a Transfer Complete interrupt, it is
  44292. + * handled in the xfercomp interrupt handler, not here. This handler may be
  44293. + * called in either DMA mode or Slave mode.
  44294. + */
  44295. +static int32_t handle_hc_nyet_intr(dwc_otg_hcd_t * hcd,
  44296. + dwc_hc_t * hc,
  44297. + dwc_otg_hc_regs_t * hc_regs,
  44298. + dwc_otg_qtd_t * qtd)
  44299. +{
  44300. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  44301. + "NYET Received--\n", hc->hc_num);
  44302. +
  44303. + /*
  44304. + * NYET on CSPLIT
  44305. + * re-do the CSPLIT immediately on non-periodic
  44306. + */
  44307. + if (hc->do_split && hc->complete_split) {
  44308. + if (hc->ep_is_in && (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
  44309. + && hcd->core_if->dma_enable) {
  44310. + qtd->complete_split = 0;
  44311. + qtd->isoc_split_offset = 0;
  44312. + if (++qtd->isoc_frame_index == qtd->urb->packet_count) {
  44313. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  44314. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  44315. + }
  44316. + else
  44317. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  44318. + goto handle_nyet_done;
  44319. + }
  44320. +
  44321. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  44322. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  44323. + int frnum = dwc_otg_hcd_get_frame_number(hcd);
  44324. +
  44325. + // With the FIQ running we only ever see the failed NYET
  44326. + if (dwc_full_frame_num(frnum) !=
  44327. + dwc_full_frame_num(hc->qh->sched_frame) ||
  44328. + fiq_fsm_enable) {
  44329. + /*
  44330. + * No longer in the same full speed frame.
  44331. + * Treat this as a transaction error.
  44332. + */
  44333. +#if 0
  44334. + /** @todo Fix system performance so this can
  44335. + * be treated as an error. Right now complete
  44336. + * splits cannot be scheduled precisely enough
  44337. + * due to other system activity, so this error
  44338. + * occurs regularly in Slave mode.
  44339. + */
  44340. + qtd->error_count++;
  44341. +#endif
  44342. + qtd->complete_split = 0;
  44343. + halt_channel(hcd, hc, qtd,
  44344. + DWC_OTG_HC_XFER_XACT_ERR);
  44345. + /** @todo add support for isoc release */
  44346. + goto handle_nyet_done;
  44347. + }
  44348. + }
  44349. +
  44350. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NYET);
  44351. + goto handle_nyet_done;
  44352. + }
  44353. +
  44354. + hc->qh->ping_state = 1;
  44355. + qtd->error_count = 0;
  44356. +
  44357. + update_urb_state_xfer_intr(hc, hc_regs, qtd->urb, qtd,
  44358. + DWC_OTG_HC_XFER_NYET);
  44359. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  44360. +
  44361. + /*
  44362. + * Halt the channel and re-start the transfer so the PING
  44363. + * protocol will start.
  44364. + */
  44365. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NYET);
  44366. +
  44367. +handle_nyet_done:
  44368. + disable_hc_int(hc_regs, nyet);
  44369. + return 1;
  44370. +}
  44371. +
  44372. +/**
  44373. + * Handles a host channel babble interrupt. This handler may be called in
  44374. + * either DMA mode or Slave mode.
  44375. + */
  44376. +static int32_t handle_hc_babble_intr(dwc_otg_hcd_t * hcd,
  44377. + dwc_hc_t * hc,
  44378. + dwc_otg_hc_regs_t * hc_regs,
  44379. + dwc_otg_qtd_t * qtd)
  44380. +{
  44381. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  44382. + "Babble Error--\n", hc->hc_num);
  44383. +
  44384. + if (hcd->core_if->dma_desc_enable) {
  44385. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
  44386. + DWC_OTG_HC_XFER_BABBLE_ERR);
  44387. + goto handle_babble_done;
  44388. + }
  44389. +
  44390. + if (hc->ep_type != DWC_OTG_EP_TYPE_ISOC) {
  44391. + hcd->fops->complete(hcd, qtd->urb->priv,
  44392. + qtd->urb, -DWC_E_OVERFLOW);
  44393. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_BABBLE_ERR);
  44394. + } else {
  44395. + dwc_otg_halt_status_e halt_status;
  44396. + halt_status = update_isoc_urb_state(hcd, hc, hc_regs, qtd,
  44397. + DWC_OTG_HC_XFER_BABBLE_ERR);
  44398. + halt_channel(hcd, hc, qtd, halt_status);
  44399. + }
  44400. +
  44401. +handle_babble_done:
  44402. + disable_hc_int(hc_regs, bblerr);
  44403. + return 1;
  44404. +}
  44405. +
  44406. +/**
  44407. + * Handles a host channel AHB error interrupt. This handler is only called in
  44408. + * DMA mode.
  44409. + */
  44410. +static int32_t handle_hc_ahberr_intr(dwc_otg_hcd_t * hcd,
  44411. + dwc_hc_t * hc,
  44412. + dwc_otg_hc_regs_t * hc_regs,
  44413. + dwc_otg_qtd_t * qtd)
  44414. +{
  44415. + hcchar_data_t hcchar;
  44416. + hcsplt_data_t hcsplt;
  44417. + hctsiz_data_t hctsiz;
  44418. + uint32_t hcdma;
  44419. + char *pipetype, *speed;
  44420. +
  44421. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  44422. +
  44423. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  44424. + "AHB Error--\n", hc->hc_num);
  44425. +
  44426. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  44427. + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
  44428. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  44429. + hcdma = DWC_READ_REG32(&hc_regs->hcdma);
  44430. +
  44431. + DWC_ERROR("AHB ERROR, Channel %d\n", hc->hc_num);
  44432. + DWC_ERROR(" hcchar 0x%08x, hcsplt 0x%08x\n", hcchar.d32, hcsplt.d32);
  44433. + DWC_ERROR(" hctsiz 0x%08x, hcdma 0x%08x\n", hctsiz.d32, hcdma);
  44434. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Enqueue\n");
  44435. + DWC_ERROR(" Device address: %d\n",
  44436. + dwc_otg_hcd_get_dev_addr(&urb->pipe_info));
  44437. + DWC_ERROR(" Endpoint: %d, %s\n",
  44438. + dwc_otg_hcd_get_ep_num(&urb->pipe_info),
  44439. + (dwc_otg_hcd_is_pipe_in(&urb->pipe_info) ? "IN" : "OUT"));
  44440. +
  44441. + switch (dwc_otg_hcd_get_pipe_type(&urb->pipe_info)) {
  44442. + case UE_CONTROL:
  44443. + pipetype = "CONTROL";
  44444. + break;
  44445. + case UE_BULK:
  44446. + pipetype = "BULK";
  44447. + break;
  44448. + case UE_INTERRUPT:
  44449. + pipetype = "INTERRUPT";
  44450. + break;
  44451. + case UE_ISOCHRONOUS:
  44452. + pipetype = "ISOCHRONOUS";
  44453. + break;
  44454. + default:
  44455. + pipetype = "UNKNOWN";
  44456. + break;
  44457. + }
  44458. +
  44459. + DWC_ERROR(" Endpoint type: %s\n", pipetype);
  44460. +
  44461. + switch (hc->speed) {
  44462. + case DWC_OTG_EP_SPEED_HIGH:
  44463. + speed = "HIGH";
  44464. + break;
  44465. + case DWC_OTG_EP_SPEED_FULL:
  44466. + speed = "FULL";
  44467. + break;
  44468. + case DWC_OTG_EP_SPEED_LOW:
  44469. + speed = "LOW";
  44470. + break;
  44471. + default:
  44472. + speed = "UNKNOWN";
  44473. + break;
  44474. + };
  44475. +
  44476. + DWC_ERROR(" Speed: %s\n", speed);
  44477. +
  44478. + DWC_ERROR(" Max packet size: %d\n",
  44479. + dwc_otg_hcd_get_mps(&urb->pipe_info));
  44480. + DWC_ERROR(" Data buffer length: %d\n", urb->length);
  44481. + DWC_ERROR(" Transfer buffer: %p, Transfer DMA: %pad\n",
  44482. + urb->buf, &urb->dma);
  44483. + DWC_ERROR(" Setup buffer: %p, Setup DMA: %pad\n",
  44484. + urb->setup_packet, &urb->setup_dma);
  44485. + DWC_ERROR(" Interval: %d\n", urb->interval);
  44486. +
  44487. + /* Core haltes the channel for Descriptor DMA mode */
  44488. + if (hcd->core_if->dma_desc_enable) {
  44489. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
  44490. + DWC_OTG_HC_XFER_AHB_ERR);
  44491. + goto handle_ahberr_done;
  44492. + }
  44493. +
  44494. + hcd->fops->complete(hcd, urb->priv, urb, -DWC_E_IO);
  44495. +
  44496. + /*
  44497. + * Force a channel halt. Don't call halt_channel because that won't
  44498. + * write to the HCCHARn register in DMA mode to force the halt.
  44499. + */
  44500. + dwc_otg_hc_halt(hcd->core_if, hc, DWC_OTG_HC_XFER_AHB_ERR);
  44501. +handle_ahberr_done:
  44502. + disable_hc_int(hc_regs, ahberr);
  44503. + return 1;
  44504. +}
  44505. +
  44506. +/**
  44507. + * Handles a host channel transaction error interrupt. This handler may be
  44508. + * called in either DMA mode or Slave mode.
  44509. + */
  44510. +static int32_t handle_hc_xacterr_intr(dwc_otg_hcd_t * hcd,
  44511. + dwc_hc_t * hc,
  44512. + dwc_otg_hc_regs_t * hc_regs,
  44513. + dwc_otg_qtd_t * qtd)
  44514. +{
  44515. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  44516. + "Transaction Error--\n", hc->hc_num);
  44517. +
  44518. + if (hcd->core_if->dma_desc_enable) {
  44519. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
  44520. + DWC_OTG_HC_XFER_XACT_ERR);
  44521. + goto handle_xacterr_done;
  44522. + }
  44523. +
  44524. + switch (dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
  44525. + case UE_CONTROL:
  44526. + case UE_BULK:
  44527. + qtd->error_count++;
  44528. + if (!hc->qh->ping_state) {
  44529. +
  44530. + update_urb_state_xfer_intr(hc, hc_regs,
  44531. + qtd->urb, qtd,
  44532. + DWC_OTG_HC_XFER_XACT_ERR);
  44533. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  44534. + if (!hc->ep_is_in && hc->speed == DWC_OTG_EP_SPEED_HIGH) {
  44535. + hc->qh->ping_state = 1;
  44536. + }
  44537. + }
  44538. +
  44539. + /*
  44540. + * Halt the channel so the transfer can be re-started from
  44541. + * the appropriate point or the PING protocol will start.
  44542. + */
  44543. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  44544. + break;
  44545. + case UE_INTERRUPT:
  44546. + qtd->error_count++;
  44547. + if (hc->do_split && hc->complete_split) {
  44548. + qtd->complete_split = 0;
  44549. + }
  44550. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  44551. + break;
  44552. + case UE_ISOCHRONOUS:
  44553. + {
  44554. + dwc_otg_halt_status_e halt_status;
  44555. + halt_status =
  44556. + update_isoc_urb_state(hcd, hc, hc_regs, qtd,
  44557. + DWC_OTG_HC_XFER_XACT_ERR);
  44558. +
  44559. + halt_channel(hcd, hc, qtd, halt_status);
  44560. + }
  44561. + break;
  44562. + }
  44563. +handle_xacterr_done:
  44564. + disable_hc_int(hc_regs, xacterr);
  44565. +
  44566. + return 1;
  44567. +}
  44568. +
  44569. +/**
  44570. + * Handles a host channel frame overrun interrupt. This handler may be called
  44571. + * in either DMA mode or Slave mode.
  44572. + */
  44573. +static int32_t handle_hc_frmovrun_intr(dwc_otg_hcd_t * hcd,
  44574. + dwc_hc_t * hc,
  44575. + dwc_otg_hc_regs_t * hc_regs,
  44576. + dwc_otg_qtd_t * qtd)
  44577. +{
  44578. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  44579. + "Frame Overrun--\n", hc->hc_num);
  44580. +
  44581. + switch (dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
  44582. + case UE_CONTROL:
  44583. + case UE_BULK:
  44584. + break;
  44585. + case UE_INTERRUPT:
  44586. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_FRAME_OVERRUN);
  44587. + break;
  44588. + case UE_ISOCHRONOUS:
  44589. + {
  44590. + dwc_otg_halt_status_e halt_status;
  44591. + halt_status =
  44592. + update_isoc_urb_state(hcd, hc, hc_regs, qtd,
  44593. + DWC_OTG_HC_XFER_FRAME_OVERRUN);
  44594. +
  44595. + halt_channel(hcd, hc, qtd, halt_status);
  44596. + }
  44597. + break;
  44598. + }
  44599. +
  44600. + disable_hc_int(hc_regs, frmovrun);
  44601. +
  44602. + return 1;
  44603. +}
  44604. +
  44605. +/**
  44606. + * Handles a host channel data toggle error interrupt. This handler may be
  44607. + * called in either DMA mode or Slave mode.
  44608. + */
  44609. +static int32_t handle_hc_datatglerr_intr(dwc_otg_hcd_t * hcd,
  44610. + dwc_hc_t * hc,
  44611. + dwc_otg_hc_regs_t * hc_regs,
  44612. + dwc_otg_qtd_t * qtd)
  44613. +{
  44614. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  44615. + "Data Toggle Error on %s transfer--\n",
  44616. + hc->hc_num, (hc->ep_is_in ? "IN" : "OUT"));
  44617. +
  44618. + /* Data toggles on split transactions cause the hc to halt.
  44619. + * restart transfer */
  44620. + if(hc->qh->do_split)
  44621. + {
  44622. + qtd->error_count++;
  44623. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  44624. + update_urb_state_xfer_intr(hc, hc_regs,
  44625. + qtd->urb, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  44626. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  44627. + } else if (hc->ep_is_in) {
  44628. + /* An unmasked data toggle error on a non-split DMA transaction is
  44629. + * for the sole purpose of resetting error counts. Disable other
  44630. + * interrupts unmasked for the same reason.
  44631. + */
  44632. + if(hcd->core_if->dma_enable) {
  44633. + disable_hc_int(hc_regs, ack);
  44634. + disable_hc_int(hc_regs, nak);
  44635. + }
  44636. + qtd->error_count = 0;
  44637. + }
  44638. +
  44639. + disable_hc_int(hc_regs, datatglerr);
  44640. +
  44641. + return 1;
  44642. +}
  44643. +
  44644. +#ifdef DEBUG
  44645. +/**
  44646. + * This function is for debug only. It checks that a valid halt status is set
  44647. + * and that HCCHARn.chdis is clear. If there's a problem, corrective action is
  44648. + * taken and a warning is issued.
  44649. + * @return 1 if halt status is ok, 0 otherwise.
  44650. + */
  44651. +static inline int halt_status_ok(dwc_otg_hcd_t * hcd,
  44652. + dwc_hc_t * hc,
  44653. + dwc_otg_hc_regs_t * hc_regs,
  44654. + dwc_otg_qtd_t * qtd)
  44655. +{
  44656. + hcchar_data_t hcchar;
  44657. + hctsiz_data_t hctsiz;
  44658. + hcint_data_t hcint;
  44659. + hcintmsk_data_t hcintmsk;
  44660. + hcsplt_data_t hcsplt;
  44661. +
  44662. + if (hc->halt_status == DWC_OTG_HC_XFER_NO_HALT_STATUS) {
  44663. + /*
  44664. + * This code is here only as a check. This condition should
  44665. + * never happen. Ignore the halt if it does occur.
  44666. + */
  44667. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  44668. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  44669. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  44670. + hcintmsk.d32 = DWC_READ_REG32(&hc_regs->hcintmsk);
  44671. + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
  44672. + DWC_WARN
  44673. + ("%s: hc->halt_status == DWC_OTG_HC_XFER_NO_HALT_STATUS, "
  44674. + "channel %d, hcchar 0x%08x, hctsiz 0x%08x, "
  44675. + "hcint 0x%08x, hcintmsk 0x%08x, "
  44676. + "hcsplt 0x%08x, qtd->complete_split %d\n", __func__,
  44677. + hc->hc_num, hcchar.d32, hctsiz.d32, hcint.d32,
  44678. + hcintmsk.d32, hcsplt.d32, qtd->complete_split);
  44679. +
  44680. + DWC_WARN("%s: no halt status, channel %d, ignoring interrupt\n",
  44681. + __func__, hc->hc_num);
  44682. + DWC_WARN("\n");
  44683. + clear_hc_int(hc_regs, chhltd);
  44684. + return 0;
  44685. + }
  44686. +
  44687. + /*
  44688. + * This code is here only as a check. hcchar.chdis should
  44689. + * never be set when the halt interrupt occurs. Halt the
  44690. + * channel again if it does occur.
  44691. + */
  44692. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  44693. + if (hcchar.b.chdis) {
  44694. + DWC_WARN("%s: hcchar.chdis set unexpectedly, "
  44695. + "hcchar 0x%08x, trying to halt again\n",
  44696. + __func__, hcchar.d32);
  44697. + clear_hc_int(hc_regs, chhltd);
  44698. + hc->halt_pending = 0;
  44699. + halt_channel(hcd, hc, qtd, hc->halt_status);
  44700. + return 0;
  44701. + }
  44702. +
  44703. + return 1;
  44704. +}
  44705. +#endif
  44706. +
  44707. +/**
  44708. + * Handles a host Channel Halted interrupt in DMA mode. This handler
  44709. + * determines the reason the channel halted and proceeds accordingly.
  44710. + */
  44711. +static void handle_hc_chhltd_intr_dma(dwc_otg_hcd_t * hcd,
  44712. + dwc_hc_t * hc,
  44713. + dwc_otg_hc_regs_t * hc_regs,
  44714. + dwc_otg_qtd_t * qtd)
  44715. +{
  44716. + int out_nak_enh = 0;
  44717. + hcint_data_t hcint;
  44718. + hcintmsk_data_t hcintmsk;
  44719. + /* For core with OUT NAK enhancement, the flow for high-
  44720. + * speed CONTROL/BULK OUT is handled a little differently.
  44721. + */
  44722. + if (hcd->core_if->snpsid >= OTG_CORE_REV_2_71a) {
  44723. + if (hc->speed == DWC_OTG_EP_SPEED_HIGH && !hc->ep_is_in &&
  44724. + (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL ||
  44725. + hc->ep_type == DWC_OTG_EP_TYPE_BULK)) {
  44726. + out_nak_enh = 1;
  44727. + }
  44728. + }
  44729. +
  44730. + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE ||
  44731. + (hc->halt_status == DWC_OTG_HC_XFER_AHB_ERR
  44732. + && !hcd->core_if->dma_desc_enable)) {
  44733. + /*
  44734. + * Just release the channel. A dequeue can happen on a
  44735. + * transfer timeout. In the case of an AHB Error, the channel
  44736. + * was forced to halt because there's no way to gracefully
  44737. + * recover.
  44738. + */
  44739. + if (hcd->core_if->dma_desc_enable)
  44740. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
  44741. + hc->halt_status);
  44742. + else
  44743. + release_channel(hcd, hc, qtd, hc->halt_status);
  44744. + return;
  44745. + }
  44746. +
  44747. + /* Read the HCINTn register to determine the cause for the halt. */
  44748. +
  44749. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  44750. + hcintmsk.d32 = DWC_READ_REG32(&hc_regs->hcintmsk);
  44751. +
  44752. + if (hcint.b.xfercomp) {
  44753. + /** @todo This is here because of a possible hardware bug. Spec
  44754. + * says that on SPLIT-ISOC OUT transfers in DMA mode that a HALT
  44755. + * interrupt w/ACK bit set should occur, but I only see the
  44756. + * XFERCOMP bit, even with it masked out. This is a workaround
  44757. + * for that behavior. Should fix this when hardware is fixed.
  44758. + */
  44759. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC && !hc->ep_is_in) {
  44760. + handle_hc_ack_intr(hcd, hc, hc_regs, qtd);
  44761. + }
  44762. + handle_hc_xfercomp_intr(hcd, hc, hc_regs, qtd);
  44763. + } else if (hcint.b.stall) {
  44764. + handle_hc_stall_intr(hcd, hc, hc_regs, qtd);
  44765. + } else if (hcint.b.xacterr && !hcd->core_if->dma_desc_enable) {
  44766. + if (out_nak_enh) {
  44767. + if (hcint.b.nyet || hcint.b.nak || hcint.b.ack) {
  44768. + DWC_DEBUGPL(DBG_HCD, "XactErr with NYET/NAK/ACK\n");
  44769. + qtd->error_count = 0;
  44770. + } else {
  44771. + DWC_DEBUGPL(DBG_HCD, "XactErr without NYET/NAK/ACK\n");
  44772. + }
  44773. + }
  44774. +
  44775. + /*
  44776. + * Must handle xacterr before nak or ack. Could get a xacterr
  44777. + * at the same time as either of these on a BULK/CONTROL OUT
  44778. + * that started with a PING. The xacterr takes precedence.
  44779. + */
  44780. + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
  44781. + } else if (hcint.b.xcs_xact && hcd->core_if->dma_desc_enable) {
  44782. + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
  44783. + } else if (hcint.b.ahberr && hcd->core_if->dma_desc_enable) {
  44784. + handle_hc_ahberr_intr(hcd, hc, hc_regs, qtd);
  44785. + } else if (hcint.b.bblerr) {
  44786. + handle_hc_babble_intr(hcd, hc, hc_regs, qtd);
  44787. + } else if (hcint.b.frmovrun) {
  44788. + handle_hc_frmovrun_intr(hcd, hc, hc_regs, qtd);
  44789. + } else if (hcint.b.datatglerr) {
  44790. + handle_hc_datatglerr_intr(hcd, hc, hc_regs, qtd);
  44791. + } else if (!out_nak_enh) {
  44792. + if (hcint.b.nyet) {
  44793. + /*
  44794. + * Must handle nyet before nak or ack. Could get a nyet at the
  44795. + * same time as either of those on a BULK/CONTROL OUT that
  44796. + * started with a PING. The nyet takes precedence.
  44797. + */
  44798. + handle_hc_nyet_intr(hcd, hc, hc_regs, qtd);
  44799. + } else if (hcint.b.nak && !hcintmsk.b.nak) {
  44800. + /*
  44801. + * If nak is not masked, it's because a non-split IN transfer
  44802. + * is in an error state. In that case, the nak is handled by
  44803. + * the nak interrupt handler, not here. Handle nak here for
  44804. + * BULK/CONTROL OUT transfers, which halt on a NAK to allow
  44805. + * rewinding the buffer pointer.
  44806. + */
  44807. + handle_hc_nak_intr(hcd, hc, hc_regs, qtd);
  44808. + } else if (hcint.b.ack && !hcintmsk.b.ack) {
  44809. + /*
  44810. + * If ack is not masked, it's because a non-split IN transfer
  44811. + * is in an error state. In that case, the ack is handled by
  44812. + * the ack interrupt handler, not here. Handle ack here for
  44813. + * split transfers. Start splits halt on ACK.
  44814. + */
  44815. + handle_hc_ack_intr(hcd, hc, hc_regs, qtd);
  44816. + } else {
  44817. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  44818. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  44819. + /*
  44820. + * A periodic transfer halted with no other channel
  44821. + * interrupts set. Assume it was halted by the core
  44822. + * because it could not be completed in its scheduled
  44823. + * (micro)frame.
  44824. + */
  44825. +#ifdef DEBUG
  44826. + DWC_PRINTF
  44827. + ("%s: Halt channel %d (assume incomplete periodic transfer)\n",
  44828. + __func__, hc->hc_num);
  44829. +#endif
  44830. + halt_channel(hcd, hc, qtd,
  44831. + DWC_OTG_HC_XFER_PERIODIC_INCOMPLETE);
  44832. + } else {
  44833. + DWC_ERROR
  44834. + ("%s: Channel %d, DMA Mode -- ChHltd set, but reason "
  44835. + "for halting is unknown, hcint 0x%08x, intsts 0x%08x\n",
  44836. + __func__, hc->hc_num, hcint.d32,
  44837. + DWC_READ_REG32(&hcd->
  44838. + core_if->core_global_regs->
  44839. + gintsts));
  44840. + /* Failthrough: use 3-strikes rule */
  44841. + qtd->error_count++;
  44842. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  44843. + update_urb_state_xfer_intr(hc, hc_regs,
  44844. + qtd->urb, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  44845. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  44846. + }
  44847. +
  44848. + }
  44849. + } else {
  44850. + DWC_PRINTF("NYET/NAK/ACK/other in non-error case, 0x%08x\n",
  44851. + hcint.d32);
  44852. + /* Failthrough: use 3-strikes rule */
  44853. + qtd->error_count++;
  44854. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  44855. + update_urb_state_xfer_intr(hc, hc_regs,
  44856. + qtd->urb, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  44857. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  44858. + }
  44859. +}
  44860. +
  44861. +/**
  44862. + * Handles a host channel Channel Halted interrupt.
  44863. + *
  44864. + * In slave mode, this handler is called only when the driver specifically
  44865. + * requests a halt. This occurs during handling other host channel interrupts
  44866. + * (e.g. nak, xacterr, stall, nyet, etc.).
  44867. + *
  44868. + * In DMA mode, this is the interrupt that occurs when the core has finished
  44869. + * processing a transfer on a channel. Other host channel interrupts (except
  44870. + * ahberr) are disabled in DMA mode.
  44871. + */
  44872. +static int32_t handle_hc_chhltd_intr(dwc_otg_hcd_t * hcd,
  44873. + dwc_hc_t * hc,
  44874. + dwc_otg_hc_regs_t * hc_regs,
  44875. + dwc_otg_qtd_t * qtd)
  44876. +{
  44877. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  44878. + "Channel Halted--\n", hc->hc_num);
  44879. +
  44880. + if (hcd->core_if->dma_enable) {
  44881. + handle_hc_chhltd_intr_dma(hcd, hc, hc_regs, qtd);
  44882. + } else {
  44883. +#ifdef DEBUG
  44884. + if (!halt_status_ok(hcd, hc, hc_regs, qtd)) {
  44885. + return 1;
  44886. + }
  44887. +#endif
  44888. + release_channel(hcd, hc, qtd, hc->halt_status);
  44889. + }
  44890. +
  44891. + return 1;
  44892. +}
  44893. +
  44894. +
  44895. +/**
  44896. + * dwc_otg_fiq_unmangle_isoc() - Update the iso_frame_desc structure on
  44897. + * FIQ transfer completion
  44898. + * @hcd: Pointer to dwc_otg_hcd struct
  44899. + * @num: Host channel number
  44900. + *
  44901. + * 1. Un-mangle the status as recorded in each iso_frame_desc status
  44902. + * 2. Copy it from the dwc_otg_urb into the real URB
  44903. + */
  44904. +void dwc_otg_fiq_unmangle_isoc(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh, dwc_otg_qtd_t *qtd, uint32_t num)
  44905. +{
  44906. + struct dwc_otg_hcd_urb *dwc_urb = qtd->urb;
  44907. + int nr_frames = dwc_urb->packet_count;
  44908. + int i;
  44909. + hcint_data_t frame_hcint;
  44910. +
  44911. + for (i = 0; i < nr_frames; i++) {
  44912. + frame_hcint.d32 = dwc_urb->iso_descs[i].status;
  44913. + if (frame_hcint.b.xfercomp) {
  44914. + dwc_urb->iso_descs[i].status = 0;
  44915. + dwc_urb->actual_length += dwc_urb->iso_descs[i].actual_length;
  44916. + } else if (frame_hcint.b.frmovrun) {
  44917. + if (qh->ep_is_in)
  44918. + dwc_urb->iso_descs[i].status = -DWC_E_NO_STREAM_RES;
  44919. + else
  44920. + dwc_urb->iso_descs[i].status = -DWC_E_COMMUNICATION;
  44921. + dwc_urb->error_count++;
  44922. + dwc_urb->iso_descs[i].actual_length = 0;
  44923. + } else if (frame_hcint.b.xacterr) {
  44924. + dwc_urb->iso_descs[i].status = -DWC_E_PROTOCOL;
  44925. + dwc_urb->error_count++;
  44926. + dwc_urb->iso_descs[i].actual_length = 0;
  44927. + } else if (frame_hcint.b.bblerr) {
  44928. + dwc_urb->iso_descs[i].status = -DWC_E_OVERFLOW;
  44929. + dwc_urb->error_count++;
  44930. + dwc_urb->iso_descs[i].actual_length = 0;
  44931. + } else {
  44932. + /* Something went wrong */
  44933. + dwc_urb->iso_descs[i].status = -1;
  44934. + dwc_urb->iso_descs[i].actual_length = 0;
  44935. + dwc_urb->error_count++;
  44936. + }
  44937. + }
  44938. + qh->sched_frame = dwc_frame_num_inc(qh->sched_frame, qh->interval * (nr_frames - 1));
  44939. +
  44940. + //printk_ratelimited(KERN_INFO "%s: HS isochronous of %d/%d frames with %d errors complete\n",
  44941. + // __FUNCTION__, i, dwc_urb->packet_count, dwc_urb->error_count);
  44942. +}
  44943. +
  44944. +/**
  44945. + * dwc_otg_fiq_unsetup_per_dma() - Remove data from bounce buffers for split transactions
  44946. + * @hcd: Pointer to dwc_otg_hcd struct
  44947. + * @num: Host channel number
  44948. + *
  44949. + * Copies data from the FIQ bounce buffers into the URB's transfer buffer. Does not modify URB state.
  44950. + * Returns total length of data or -1 if the buffers were not used.
  44951. + *
  44952. + */
  44953. +int dwc_otg_fiq_unsetup_per_dma(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh, dwc_otg_qtd_t *qtd, uint32_t num)
  44954. +{
  44955. + dwc_hc_t *hc = qh->channel;
  44956. + struct fiq_dma_blob *blob = hcd->fiq_dmab;
  44957. + struct fiq_channel_state *st = &hcd->fiq_state->channel[num];
  44958. + uint8_t *ptr = NULL;
  44959. + int index = 0, len = 0;
  44960. + int i = 0;
  44961. + if (hc->ep_is_in) {
  44962. + /* Copy data out of the DMA bounce buffers to the URB's buffer.
  44963. + * The align_buf is ignored as this is ignored on FSM enqueue. */
  44964. + ptr = qtd->urb->buf;
  44965. + if (qh->ep_type == UE_ISOCHRONOUS) {
  44966. + /* Isoc IN transactions - grab the offset of the iso_frame_desc into the URB transfer buffer */
  44967. + index = qtd->isoc_frame_index;
  44968. + ptr += qtd->urb->iso_descs[index].offset;
  44969. + } else {
  44970. + /* Need to increment by actual_length for interrupt IN */
  44971. + ptr += qtd->urb->actual_length;
  44972. + }
  44973. +
  44974. + for (i = 0; i < st->dma_info.index; i++) {
  44975. + len += st->dma_info.slot_len[i];
  44976. + dwc_memcpy(ptr, &blob->channel[num].index[i].buf[0], st->dma_info.slot_len[i]);
  44977. + ptr += st->dma_info.slot_len[i];
  44978. + }
  44979. + return len;
  44980. + } else {
  44981. + /* OUT endpoints - nothing to do. */
  44982. + return -1;
  44983. + }
  44984. +
  44985. +}
  44986. +/**
  44987. + * dwc_otg_hcd_handle_hc_fsm() - handle an unmasked channel interrupt
  44988. + * from a channel handled in the FIQ
  44989. + * @hcd: Pointer to dwc_otg_hcd struct
  44990. + * @num: Host channel number
  44991. + *
  44992. + * If a host channel interrupt was received by the IRQ and this was a channel
  44993. + * used by the FIQ, the execution flow for transfer completion is substantially
  44994. + * different from the normal (messy) path. This function and its friends handles
  44995. + * channel cleanup and transaction completion from a FIQ transaction.
  44996. + */
  44997. +void dwc_otg_hcd_handle_hc_fsm(dwc_otg_hcd_t *hcd, uint32_t num)
  44998. +{
  44999. + struct fiq_channel_state *st = &hcd->fiq_state->channel[num];
  45000. + dwc_hc_t *hc = hcd->hc_ptr_array[num];
  45001. + dwc_otg_qtd_t *qtd;
  45002. + dwc_otg_hc_regs_t *hc_regs = hcd->core_if->host_if->hc_regs[num];
  45003. + hcint_data_t hcint = hcd->fiq_state->channel[num].hcint_copy;
  45004. + hctsiz_data_t hctsiz = hcd->fiq_state->channel[num].hctsiz_copy;
  45005. + int hostchannels = 0;
  45006. + fiq_print(FIQDBG_INT, hcd->fiq_state, "OUT %01d %01d ", num , st->fsm);
  45007. +
  45008. + hostchannels = hcd->available_host_channels;
  45009. + if (hc->halt_pending) {
  45010. + /* Dequeue: The FIQ was allowed to complete the transfer but state has been cleared. */
  45011. + if (hc->qh && st->fsm == FIQ_NP_SPLIT_DONE &&
  45012. + hcint.b.xfercomp && hc->qh->ep_type == UE_BULK) {
  45013. + if (hctsiz.b.pid == DWC_HCTSIZ_DATA0) {
  45014. + hc->qh->data_toggle = DWC_OTG_HC_PID_DATA1;
  45015. + } else {
  45016. + hc->qh->data_toggle = DWC_OTG_HC_PID_DATA0;
  45017. + }
  45018. + }
  45019. + release_channel(hcd, hc, NULL, hc->halt_status);
  45020. + return;
  45021. + }
  45022. +
  45023. + qtd = DWC_CIRCLEQ_FIRST(&hc->qh->qtd_list);
  45024. + switch (st->fsm) {
  45025. + case FIQ_TEST:
  45026. + break;
  45027. +
  45028. + case FIQ_DEQUEUE_ISSUED:
  45029. + /* Handled above, but keep for posterity */
  45030. + release_channel(hcd, hc, NULL, hc->halt_status);
  45031. + break;
  45032. +
  45033. + case FIQ_NP_SPLIT_DONE:
  45034. + /* Nonperiodic transaction complete. */
  45035. + if (!hc->ep_is_in) {
  45036. + qtd->ssplit_out_xfer_count = hc->xfer_len;
  45037. + }
  45038. + if (hcint.b.xfercomp) {
  45039. + handle_hc_xfercomp_intr(hcd, hc, hc_regs, qtd);
  45040. + } else if (hcint.b.nak) {
  45041. + handle_hc_nak_intr(hcd, hc, hc_regs, qtd);
  45042. + } else {
  45043. + DWC_WARN("Unexpected IRQ state on FSM transaction:"
  45044. + "dev_addr=%d ep=%d fsm=%d, hcint=0x%08x\n",
  45045. + hc->dev_addr, hc->ep_num, st->fsm, hcint.d32);
  45046. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  45047. + }
  45048. + break;
  45049. +
  45050. + case FIQ_NP_SPLIT_HS_ABORTED:
  45051. + /* A HS abort is a 3-strikes on the HS bus at any point in the transaction.
  45052. + * Normally a CLEAR_TT_BUFFER hub command would be required: we can't do that
  45053. + * because there's no guarantee which order a non-periodic split happened in.
  45054. + * We could end up clearing a perfectly good transaction out of the buffer.
  45055. + */
  45056. + if (hcint.b.xacterr) {
  45057. + qtd->error_count += st->nr_errors;
  45058. + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
  45059. + } else if (hcint.b.ahberr) {
  45060. + handle_hc_ahberr_intr(hcd, hc, hc_regs, qtd);
  45061. + } else {
  45062. + DWC_WARN("Unexpected IRQ state on FSM transaction:"
  45063. + "dev_addr=%d ep=%d fsm=%d, hcint=0x%08x\n",
  45064. + hc->dev_addr, hc->ep_num, st->fsm, hcint.d32);
  45065. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  45066. + }
  45067. + break;
  45068. +
  45069. + case FIQ_NP_SPLIT_LS_ABORTED:
  45070. + /* A few cases can cause this - either an unknown state on a SSPLIT or
  45071. + * STALL/data toggle error response on a CSPLIT */
  45072. + if (hcint.b.stall) {
  45073. + handle_hc_stall_intr(hcd, hc, hc_regs, qtd);
  45074. + } else if (hcint.b.datatglerr) {
  45075. + handle_hc_datatglerr_intr(hcd, hc, hc_regs, qtd);
  45076. + } else if (hcint.b.bblerr) {
  45077. + handle_hc_babble_intr(hcd, hc, hc_regs, qtd);
  45078. + } else if (hcint.b.ahberr) {
  45079. + handle_hc_ahberr_intr(hcd, hc, hc_regs, qtd);
  45080. + } else {
  45081. + DWC_WARN("Unexpected IRQ state on FSM transaction:"
  45082. + "dev_addr=%d ep=%d fsm=%d, hcint=0x%08x\n",
  45083. + hc->dev_addr, hc->ep_num, st->fsm, hcint.d32);
  45084. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  45085. + }
  45086. + break;
  45087. +
  45088. + case FIQ_PER_SPLIT_DONE:
  45089. + /* Isoc IN or Interrupt IN/OUT */
  45090. +
  45091. + /* Flow control here is different from the normal execution by the driver.
  45092. + * We need to completely ignore most of the driver's method of handling
  45093. + * split transactions and do it ourselves.
  45094. + */
  45095. + if (hc->ep_type == UE_INTERRUPT) {
  45096. + if (hcint.b.nak) {
  45097. + handle_hc_nak_intr(hcd, hc, hc_regs, qtd);
  45098. + } else if (hc->ep_is_in) {
  45099. + int len;
  45100. + len = dwc_otg_fiq_unsetup_per_dma(hcd, hc->qh, qtd, num);
  45101. + //printk(KERN_NOTICE "FIQ Transaction: hc=%d len=%d urb_len = %d\n", num, len, qtd->urb->length);
  45102. + qtd->urb->actual_length += len;
  45103. + if (qtd->urb->actual_length >= qtd->urb->length) {
  45104. + qtd->urb->status = 0;
  45105. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, qtd->urb->status);
  45106. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  45107. + } else {
  45108. + /* Interrupt transfer not complete yet - is it a short read? */
  45109. + if (len < hc->max_packet) {
  45110. + /* Interrupt transaction complete */
  45111. + qtd->urb->status = 0;
  45112. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, qtd->urb->status);
  45113. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  45114. + } else {
  45115. + /* Further transactions required */
  45116. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
  45117. + }
  45118. + }
  45119. + } else {
  45120. + /* Interrupt OUT complete. */
  45121. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  45122. + qtd->urb->actual_length += hc->xfer_len;
  45123. + if (qtd->urb->actual_length >= qtd->urb->length) {
  45124. + qtd->urb->status = 0;
  45125. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, qtd->urb->status);
  45126. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  45127. + } else {
  45128. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
  45129. + }
  45130. + }
  45131. + } else {
  45132. + /* ISOC IN complete. */
  45133. + struct dwc_otg_hcd_iso_packet_desc *frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  45134. + int len = 0;
  45135. + /* Record errors, update qtd. */
  45136. + if (st->nr_errors) {
  45137. + frame_desc->actual_length = 0;
  45138. + frame_desc->status = -DWC_E_PROTOCOL;
  45139. + } else {
  45140. + frame_desc->status = 0;
  45141. + /* Unswizzle dma */
  45142. + len = dwc_otg_fiq_unsetup_per_dma(hcd, hc->qh, qtd, num);
  45143. + frame_desc->actual_length = len;
  45144. + }
  45145. + qtd->isoc_frame_index++;
  45146. + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
  45147. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  45148. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  45149. + } else {
  45150. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
  45151. + }
  45152. + }
  45153. + break;
  45154. +
  45155. + case FIQ_PER_ISO_OUT_DONE: {
  45156. + struct dwc_otg_hcd_iso_packet_desc *frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  45157. + /* Record errors, update qtd. */
  45158. + if (st->nr_errors) {
  45159. + frame_desc->actual_length = 0;
  45160. + frame_desc->status = -DWC_E_PROTOCOL;
  45161. + } else {
  45162. + frame_desc->status = 0;
  45163. + frame_desc->actual_length = frame_desc->length;
  45164. + }
  45165. + qtd->isoc_frame_index++;
  45166. + qtd->isoc_split_offset = 0;
  45167. + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
  45168. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  45169. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  45170. + } else {
  45171. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
  45172. + }
  45173. + }
  45174. + break;
  45175. +
  45176. + case FIQ_PER_SPLIT_NYET_ABORTED:
  45177. + /* Doh. lost the data. */
  45178. + printk_ratelimited(KERN_INFO "Transfer to device %d endpoint 0x%x frame %d failed "
  45179. + "- FIQ reported NYET. Data may have been lost.\n",
  45180. + hc->dev_addr, hc->ep_num, dwc_otg_hcd_get_frame_number(hcd) >> 3);
  45181. + if (hc->ep_type == UE_ISOCHRONOUS) {
  45182. + struct dwc_otg_hcd_iso_packet_desc *frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  45183. + /* Record errors, update qtd. */
  45184. + frame_desc->actual_length = 0;
  45185. + frame_desc->status = -DWC_E_PROTOCOL;
  45186. + qtd->isoc_frame_index++;
  45187. + qtd->isoc_split_offset = 0;
  45188. + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
  45189. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  45190. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  45191. + } else {
  45192. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
  45193. + }
  45194. + } else {
  45195. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  45196. + }
  45197. + break;
  45198. +
  45199. + case FIQ_HS_ISOC_DONE:
  45200. + /* The FIQ has performed a whole pile of isochronous transactions.
  45201. + * The status is recorded as the interrupt state should the transaction
  45202. + * fail.
  45203. + */
  45204. + dwc_otg_fiq_unmangle_isoc(hcd, hc->qh, qtd, num);
  45205. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  45206. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  45207. + break;
  45208. +
  45209. + case FIQ_PER_SPLIT_LS_ABORTED:
  45210. + if (hcint.b.xacterr) {
  45211. + /* Hub has responded with an ERR packet. Device
  45212. + * has been unplugged or the port has been disabled.
  45213. + * TODO: need to issue a reset to the hub port. */
  45214. + qtd->error_count += 3;
  45215. + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
  45216. + } else if (hcint.b.stall) {
  45217. + handle_hc_stall_intr(hcd, hc, hc_regs, qtd);
  45218. + } else if (hcint.b.bblerr) {
  45219. + handle_hc_babble_intr(hcd, hc, hc_regs, qtd);
  45220. + } else {
  45221. + printk_ratelimited(KERN_INFO "Transfer to device %d endpoint 0x%x failed "
  45222. + "- FIQ reported FSM=%d. Data may have been lost.\n",
  45223. + st->fsm, hc->dev_addr, hc->ep_num);
  45224. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  45225. + }
  45226. + break;
  45227. +
  45228. + case FIQ_PER_SPLIT_HS_ABORTED:
  45229. + /* Either the SSPLIT phase suffered transaction errors or something
  45230. + * unexpected happened.
  45231. + */
  45232. + qtd->error_count += 3;
  45233. + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
  45234. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  45235. + break;
  45236. +
  45237. + case FIQ_PER_SPLIT_TIMEOUT:
  45238. + /* Couldn't complete in the nominated frame */
  45239. + printk(KERN_INFO "Transfer to device %d endpoint 0x%x frame %d failed "
  45240. + "- FIQ timed out. Data may have been lost.\n",
  45241. + hc->dev_addr, hc->ep_num, dwc_otg_hcd_get_frame_number(hcd) >> 3);
  45242. + if (hc->ep_type == UE_ISOCHRONOUS) {
  45243. + struct dwc_otg_hcd_iso_packet_desc *frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  45244. + /* Record errors, update qtd. */
  45245. + frame_desc->actual_length = 0;
  45246. + if (hc->ep_is_in) {
  45247. + frame_desc->status = -DWC_E_NO_STREAM_RES;
  45248. + } else {
  45249. + frame_desc->status = -DWC_E_COMMUNICATION;
  45250. + }
  45251. + qtd->isoc_frame_index++;
  45252. + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
  45253. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  45254. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  45255. + } else {
  45256. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
  45257. + }
  45258. + } else {
  45259. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  45260. + }
  45261. + break;
  45262. +
  45263. + default:
  45264. + DWC_WARN("Unexpected state received on hc=%d fsm=%d on transfer to device %d ep 0x%x",
  45265. + hc->hc_num, st->fsm, hc->dev_addr, hc->ep_num);
  45266. + qtd->error_count++;
  45267. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  45268. + }
  45269. + return;
  45270. +}
  45271. +
  45272. +/** Handles interrupt for a specific Host Channel */
  45273. +int32_t dwc_otg_hcd_handle_hc_n_intr(dwc_otg_hcd_t * dwc_otg_hcd, uint32_t num)
  45274. +{
  45275. + int retval = 0;
  45276. + hcint_data_t hcint;
  45277. + hcintmsk_data_t hcintmsk;
  45278. + dwc_hc_t *hc;
  45279. + dwc_otg_hc_regs_t *hc_regs;
  45280. + dwc_otg_qtd_t *qtd;
  45281. +
  45282. + DWC_DEBUGPL(DBG_HCDV, "--Host Channel Interrupt--, Channel %d\n", num);
  45283. +
  45284. + hc = dwc_otg_hcd->hc_ptr_array[num];
  45285. + hc_regs = dwc_otg_hcd->core_if->host_if->hc_regs[num];
  45286. + if(hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
  45287. + /* A dequeue was issued for this transfer. Our QTD has gone away
  45288. + * but in the case of a FIQ transfer, the transfer would have run
  45289. + * to completion.
  45290. + */
  45291. + if (fiq_fsm_enable && dwc_otg_hcd->fiq_state->channel[num].fsm != FIQ_PASSTHROUGH) {
  45292. + dwc_otg_hcd_handle_hc_fsm(dwc_otg_hcd, num);
  45293. + } else {
  45294. + release_channel(dwc_otg_hcd, hc, NULL, hc->halt_status);
  45295. + }
  45296. + return 1;
  45297. + }
  45298. + qtd = DWC_CIRCLEQ_FIRST(&hc->qh->qtd_list);
  45299. +
  45300. + /*
  45301. + * FSM mode: Check to see if this is a HC interrupt from a channel handled by the FIQ.
  45302. + * Execution path is fundamentally different for the channels after a FIQ has completed
  45303. + * a split transaction.
  45304. + */
  45305. + if (fiq_fsm_enable) {
  45306. + switch (dwc_otg_hcd->fiq_state->channel[num].fsm) {
  45307. + case FIQ_PASSTHROUGH:
  45308. + break;
  45309. + case FIQ_PASSTHROUGH_ERRORSTATE:
  45310. + /* Hook into the error count */
  45311. + fiq_print(FIQDBG_ERR, dwc_otg_hcd->fiq_state, "HCDERR%02d", num);
  45312. + if (!dwc_otg_hcd->fiq_state->channel[num].nr_errors) {
  45313. + qtd->error_count = 0;
  45314. + fiq_print(FIQDBG_ERR, dwc_otg_hcd->fiq_state, "RESET ");
  45315. + }
  45316. + break;
  45317. + default:
  45318. + dwc_otg_hcd_handle_hc_fsm(dwc_otg_hcd, num);
  45319. + return 1;
  45320. + }
  45321. + }
  45322. +
  45323. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  45324. + hcintmsk.d32 = DWC_READ_REG32(&hc_regs->hcintmsk);
  45325. + hcint.d32 = hcint.d32 & hcintmsk.d32;
  45326. + if (!dwc_otg_hcd->core_if->dma_enable) {
  45327. + if (hcint.b.chhltd && hcint.d32 != 0x2) {
  45328. + hcint.b.chhltd = 0;
  45329. + }
  45330. + }
  45331. +
  45332. + if (hcint.b.xfercomp) {
  45333. + retval |=
  45334. + handle_hc_xfercomp_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  45335. + /*
  45336. + * If NYET occurred at same time as Xfer Complete, the NYET is
  45337. + * handled by the Xfer Complete interrupt handler. Don't want
  45338. + * to call the NYET interrupt handler in this case.
  45339. + */
  45340. + hcint.b.nyet = 0;
  45341. + }
  45342. + if (hcint.b.chhltd) {
  45343. + retval |= handle_hc_chhltd_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  45344. + }
  45345. + if (hcint.b.ahberr) {
  45346. + retval |= handle_hc_ahberr_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  45347. + }
  45348. + if (hcint.b.stall) {
  45349. + retval |= handle_hc_stall_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  45350. + }
  45351. + if (hcint.b.nak) {
  45352. + retval |= handle_hc_nak_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  45353. + }
  45354. + if (hcint.b.ack) {
  45355. + if(!hcint.b.chhltd)
  45356. + retval |= handle_hc_ack_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  45357. + }
  45358. + if (hcint.b.nyet) {
  45359. + retval |= handle_hc_nyet_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  45360. + }
  45361. + if (hcint.b.xacterr) {
  45362. + retval |= handle_hc_xacterr_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  45363. + }
  45364. + if (hcint.b.bblerr) {
  45365. + retval |= handle_hc_babble_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  45366. + }
  45367. + if (hcint.b.frmovrun) {
  45368. + retval |=
  45369. + handle_hc_frmovrun_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  45370. + }
  45371. + if (hcint.b.datatglerr) {
  45372. + retval |=
  45373. + handle_hc_datatglerr_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  45374. + }
  45375. +
  45376. + return retval;
  45377. +}
  45378. +#endif /* DWC_DEVICE_ONLY */
  45379. --- /dev/null
  45380. +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c
  45381. @@ -0,0 +1,1086 @@
  45382. +
  45383. +/* ==========================================================================
  45384. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_linux.c $
  45385. + * $Revision: #20 $
  45386. + * $Date: 2011/10/26 $
  45387. + * $Change: 1872981 $
  45388. + *
  45389. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  45390. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  45391. + * otherwise expressly agreed to in writing between Synopsys and you.
  45392. + *
  45393. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  45394. + * any End User Software License Agreement or Agreement for Licensed Product
  45395. + * with Synopsys or any supplement thereto. You are permitted to use and
  45396. + * redistribute this Software in source and binary forms, with or without
  45397. + * modification, provided that redistributions of source code must retain this
  45398. + * notice. You may not view, use, disclose, copy or distribute this file or
  45399. + * any information contained herein except pursuant to this license grant from
  45400. + * Synopsys. If you do not agree with this notice, including the disclaimer
  45401. + * below, then you are not authorized to use the Software.
  45402. + *
  45403. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  45404. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  45405. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  45406. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  45407. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  45408. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  45409. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  45410. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  45411. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  45412. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  45413. + * DAMAGE.
  45414. + * ========================================================================== */
  45415. +#ifndef DWC_DEVICE_ONLY
  45416. +
  45417. +/**
  45418. + * @file
  45419. + *
  45420. + * This file contains the implementation of the HCD. In Linux, the HCD
  45421. + * implements the hc_driver API.
  45422. + */
  45423. +#include <linux/kernel.h>
  45424. +#include <linux/module.h>
  45425. +#include <linux/moduleparam.h>
  45426. +#include <linux/init.h>
  45427. +#include <linux/device.h>
  45428. +#include <linux/errno.h>
  45429. +#include <linux/list.h>
  45430. +#include <linux/interrupt.h>
  45431. +#include <linux/string.h>
  45432. +#include <linux/dma-mapping.h>
  45433. +#include <linux/version.h>
  45434. +#include <asm/io.h>
  45435. +#ifdef CONFIG_ARM
  45436. +#include <asm/fiq.h>
  45437. +#endif
  45438. +#include <linux/usb.h>
  45439. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35)
  45440. +#include <../drivers/usb/core/hcd.h>
  45441. +#else
  45442. +#include <linux/usb/hcd.h>
  45443. +#endif
  45444. +#include <asm/bug.h>
  45445. +
  45446. +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30))
  45447. +#define USB_URB_EP_LINKING 1
  45448. +#else
  45449. +#define USB_URB_EP_LINKING 0
  45450. +#endif
  45451. +
  45452. +#include "dwc_otg_hcd_if.h"
  45453. +#include "dwc_otg_dbg.h"
  45454. +#include "dwc_otg_driver.h"
  45455. +#include "dwc_otg_hcd.h"
  45456. +
  45457. +#ifndef __virt_to_bus
  45458. +#define __virt_to_bus __virt_to_phys
  45459. +#define __bus_to_virt __phys_to_virt
  45460. +#define __pfn_to_bus(x) __pfn_to_phys(x)
  45461. +#define __bus_to_pfn(x) __phys_to_pfn(x)
  45462. +#endif
  45463. +
  45464. +extern unsigned char _dwc_otg_fiq_stub, _dwc_otg_fiq_stub_end;
  45465. +
  45466. +/**
  45467. + * Gets the endpoint number from a _bEndpointAddress argument. The endpoint is
  45468. + * qualified with its direction (possible 32 endpoints per device).
  45469. + */
  45470. +#define dwc_ep_addr_to_endpoint(_bEndpointAddress_) ((_bEndpointAddress_ & USB_ENDPOINT_NUMBER_MASK) | \
  45471. + ((_bEndpointAddress_ & USB_DIR_IN) != 0) << 4)
  45472. +
  45473. +static const char dwc_otg_hcd_name[] = "dwc_otg_hcd";
  45474. +
  45475. +extern bool fiq_enable;
  45476. +
  45477. +/** @name Linux HC Driver API Functions */
  45478. +/** @{ */
  45479. +/* manage i/o requests, device state */
  45480. +static int dwc_otg_urb_enqueue(struct usb_hcd *hcd,
  45481. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  45482. + struct usb_host_endpoint *ep,
  45483. +#endif
  45484. + struct urb *urb, gfp_t mem_flags);
  45485. +
  45486. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
  45487. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  45488. +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd, struct urb *urb);
  45489. +#endif
  45490. +#else /* kernels at or post 2.6.30 */
  45491. +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd,
  45492. + struct urb *urb, int status);
  45493. +#endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30) */
  45494. +
  45495. +static void endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *ep);
  45496. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
  45497. +static void endpoint_reset(struct usb_hcd *hcd, struct usb_host_endpoint *ep);
  45498. +#endif
  45499. +static irqreturn_t dwc_otg_hcd_irq(struct usb_hcd *hcd);
  45500. +extern int hcd_start(struct usb_hcd *hcd);
  45501. +extern void hcd_stop(struct usb_hcd *hcd);
  45502. +static int get_frame_number(struct usb_hcd *hcd);
  45503. +extern int hub_status_data(struct usb_hcd *hcd, char *buf);
  45504. +extern int hub_control(struct usb_hcd *hcd,
  45505. + u16 typeReq,
  45506. + u16 wValue, u16 wIndex, char *buf, u16 wLength);
  45507. +
  45508. +struct wrapper_priv_data {
  45509. + dwc_otg_hcd_t *dwc_otg_hcd;
  45510. +};
  45511. +
  45512. +/** @} */
  45513. +
  45514. +static struct hc_driver dwc_otg_hc_driver = {
  45515. +
  45516. + .description = dwc_otg_hcd_name,
  45517. + .product_desc = "DWC OTG Controller",
  45518. + .hcd_priv_size = sizeof(struct wrapper_priv_data),
  45519. +
  45520. + .irq = dwc_otg_hcd_irq,
  45521. +
  45522. + .flags = HCD_MEMORY | HCD_DMA | HCD_USB2,
  45523. +
  45524. + //.reset =
  45525. + .start = hcd_start,
  45526. + //.suspend =
  45527. + //.resume =
  45528. + .stop = hcd_stop,
  45529. +
  45530. + .urb_enqueue = dwc_otg_urb_enqueue,
  45531. + .urb_dequeue = dwc_otg_urb_dequeue,
  45532. + .endpoint_disable = endpoint_disable,
  45533. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
  45534. + .endpoint_reset = endpoint_reset,
  45535. +#endif
  45536. + .get_frame_number = get_frame_number,
  45537. +
  45538. + .hub_status_data = hub_status_data,
  45539. + .hub_control = hub_control,
  45540. + //.bus_suspend =
  45541. + //.bus_resume =
  45542. +};
  45543. +
  45544. +/** Gets the dwc_otg_hcd from a struct usb_hcd */
  45545. +static inline dwc_otg_hcd_t *hcd_to_dwc_otg_hcd(struct usb_hcd *hcd)
  45546. +{
  45547. + struct wrapper_priv_data *p;
  45548. + p = (struct wrapper_priv_data *)(hcd->hcd_priv);
  45549. + return p->dwc_otg_hcd;
  45550. +}
  45551. +
  45552. +/** Gets the struct usb_hcd that contains a dwc_otg_hcd_t. */
  45553. +static inline struct usb_hcd *dwc_otg_hcd_to_hcd(dwc_otg_hcd_t * dwc_otg_hcd)
  45554. +{
  45555. + return dwc_otg_hcd_get_priv_data(dwc_otg_hcd);
  45556. +}
  45557. +
  45558. +/** Gets the usb_host_endpoint associated with an URB. */
  45559. +inline struct usb_host_endpoint *dwc_urb_to_endpoint(struct urb *urb)
  45560. +{
  45561. + struct usb_device *dev = urb->dev;
  45562. + int ep_num = usb_pipeendpoint(urb->pipe);
  45563. +
  45564. + if (usb_pipein(urb->pipe))
  45565. + return dev->ep_in[ep_num];
  45566. + else
  45567. + return dev->ep_out[ep_num];
  45568. +}
  45569. +
  45570. +static int _disconnect(dwc_otg_hcd_t * hcd)
  45571. +{
  45572. + struct usb_hcd *usb_hcd = dwc_otg_hcd_to_hcd(hcd);
  45573. +
  45574. + usb_hcd->self.is_b_host = 0;
  45575. + return 0;
  45576. +}
  45577. +
  45578. +static int _start(dwc_otg_hcd_t * hcd)
  45579. +{
  45580. + struct usb_hcd *usb_hcd = dwc_otg_hcd_to_hcd(hcd);
  45581. +
  45582. + usb_hcd->self.is_b_host = dwc_otg_hcd_is_b_host(hcd);
  45583. + hcd_start(usb_hcd);
  45584. +
  45585. + return 0;
  45586. +}
  45587. +
  45588. +static int _hub_info(dwc_otg_hcd_t * hcd, void *urb_handle, uint32_t * hub_addr,
  45589. + uint32_t * port_addr)
  45590. +{
  45591. + struct urb *urb = (struct urb *)urb_handle;
  45592. + struct usb_bus *bus;
  45593. +#if 1 //GRAYG - temporary
  45594. + if (NULL == urb_handle)
  45595. + DWC_ERROR("**** %s - NULL URB handle\n", __func__);//GRAYG
  45596. + if (NULL == urb->dev)
  45597. + DWC_ERROR("**** %s - URB has no device\n", __func__);//GRAYG
  45598. + if (NULL == port_addr)
  45599. + DWC_ERROR("**** %s - NULL port_address\n", __func__);//GRAYG
  45600. +#endif
  45601. + if (urb->dev->tt) {
  45602. + if (NULL == urb->dev->tt->hub) {
  45603. + DWC_ERROR("**** %s - (URB's transactor has no TT - giving no hub)\n",
  45604. + __func__); //GRAYG
  45605. + //*hub_addr = (u8)usb_pipedevice(urb->pipe); //GRAYG
  45606. + *hub_addr = 0; //GRAYG
  45607. + // we probably shouldn't have a transaction translator if
  45608. + // there's no associated hub?
  45609. + } else {
  45610. + bus = hcd_to_bus(dwc_otg_hcd_to_hcd(hcd));
  45611. + if (urb->dev->tt->hub == bus->root_hub)
  45612. + *hub_addr = 0;
  45613. + else
  45614. + *hub_addr = urb->dev->tt->hub->devnum;
  45615. + }
  45616. + *port_addr = urb->dev->ttport;
  45617. + } else {
  45618. + *hub_addr = 0;
  45619. + *port_addr = urb->dev->ttport;
  45620. + }
  45621. + return 0;
  45622. +}
  45623. +
  45624. +static int _speed(dwc_otg_hcd_t * hcd, void *urb_handle)
  45625. +{
  45626. + struct urb *urb = (struct urb *)urb_handle;
  45627. + return urb->dev->speed;
  45628. +}
  45629. +
  45630. +static int _get_b_hnp_enable(dwc_otg_hcd_t * hcd)
  45631. +{
  45632. + struct usb_hcd *usb_hcd = dwc_otg_hcd_to_hcd(hcd);
  45633. + return usb_hcd->self.b_hnp_enable;
  45634. +}
  45635. +
  45636. +static void allocate_bus_bandwidth(struct usb_hcd *hcd, uint32_t bw,
  45637. + struct urb *urb)
  45638. +{
  45639. + hcd_to_bus(hcd)->bandwidth_allocated += bw / urb->interval;
  45640. + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  45641. + hcd_to_bus(hcd)->bandwidth_isoc_reqs++;
  45642. + } else {
  45643. + hcd_to_bus(hcd)->bandwidth_int_reqs++;
  45644. + }
  45645. +}
  45646. +
  45647. +static void free_bus_bandwidth(struct usb_hcd *hcd, uint32_t bw,
  45648. + struct urb *urb)
  45649. +{
  45650. + hcd_to_bus(hcd)->bandwidth_allocated -= bw / urb->interval;
  45651. + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  45652. + hcd_to_bus(hcd)->bandwidth_isoc_reqs--;
  45653. + } else {
  45654. + hcd_to_bus(hcd)->bandwidth_int_reqs--;
  45655. + }
  45656. +}
  45657. +
  45658. +/**
  45659. + * Sets the final status of an URB and returns it to the device driver. Any
  45660. + * required cleanup of the URB is performed. The HCD lock should be held on
  45661. + * entry.
  45662. + */
  45663. +static int _complete(dwc_otg_hcd_t * hcd, void *urb_handle,
  45664. + dwc_otg_hcd_urb_t * dwc_otg_urb, int32_t status)
  45665. +{
  45666. + struct urb *urb = (struct urb *)urb_handle;
  45667. + urb_tq_entry_t *new_entry;
  45668. + int rc = 0;
  45669. + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
  45670. + DWC_PRINTF("%s: urb %p, device %d, ep %d %s, status=%d\n",
  45671. + __func__, urb, usb_pipedevice(urb->pipe),
  45672. + usb_pipeendpoint(urb->pipe),
  45673. + usb_pipein(urb->pipe) ? "IN" : "OUT", status);
  45674. + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  45675. + int i;
  45676. + for (i = 0; i < urb->number_of_packets; i++) {
  45677. + DWC_PRINTF(" ISO Desc %d status: %d\n",
  45678. + i, urb->iso_frame_desc[i].status);
  45679. + }
  45680. + }
  45681. + }
  45682. + new_entry = DWC_ALLOC_ATOMIC(sizeof(urb_tq_entry_t));
  45683. + urb->actual_length = dwc_otg_hcd_urb_get_actual_length(dwc_otg_urb);
  45684. + /* Convert status value. */
  45685. + switch (status) {
  45686. + case -DWC_E_PROTOCOL:
  45687. + status = -EPROTO;
  45688. + break;
  45689. + case -DWC_E_IN_PROGRESS:
  45690. + status = -EINPROGRESS;
  45691. + break;
  45692. + case -DWC_E_PIPE:
  45693. + status = -EPIPE;
  45694. + break;
  45695. + case -DWC_E_IO:
  45696. + status = -EIO;
  45697. + break;
  45698. + case -DWC_E_TIMEOUT:
  45699. + status = -ETIMEDOUT;
  45700. + break;
  45701. + case -DWC_E_OVERFLOW:
  45702. + status = -EOVERFLOW;
  45703. + break;
  45704. + case -DWC_E_SHUTDOWN:
  45705. + status = -ESHUTDOWN;
  45706. + break;
  45707. + default:
  45708. + if (status) {
  45709. + DWC_PRINTF("Uknown urb status %d\n", status);
  45710. +
  45711. + }
  45712. + }
  45713. +
  45714. + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  45715. + int i;
  45716. +
  45717. + urb->error_count = dwc_otg_hcd_urb_get_error_count(dwc_otg_urb);
  45718. + urb->actual_length = 0;
  45719. + for (i = 0; i < urb->number_of_packets; ++i) {
  45720. + urb->iso_frame_desc[i].actual_length =
  45721. + dwc_otg_hcd_urb_get_iso_desc_actual_length
  45722. + (dwc_otg_urb, i);
  45723. + urb->actual_length += urb->iso_frame_desc[i].actual_length;
  45724. + urb->iso_frame_desc[i].status =
  45725. + dwc_otg_hcd_urb_get_iso_desc_status(dwc_otg_urb, i);
  45726. + }
  45727. + }
  45728. +
  45729. + urb->status = status;
  45730. + urb->hcpriv = NULL;
  45731. + if (!status) {
  45732. + if ((urb->transfer_flags & URB_SHORT_NOT_OK) &&
  45733. + (urb->actual_length < urb->transfer_buffer_length)) {
  45734. + urb->status = -EREMOTEIO;
  45735. + }
  45736. + }
  45737. +
  45738. + if ((usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) ||
  45739. + (usb_pipetype(urb->pipe) == PIPE_INTERRUPT)) {
  45740. + struct usb_host_endpoint *ep = dwc_urb_to_endpoint(urb);
  45741. + if (ep) {
  45742. + free_bus_bandwidth(dwc_otg_hcd_to_hcd(hcd),
  45743. + dwc_otg_hcd_get_ep_bandwidth(hcd,
  45744. + ep->hcpriv),
  45745. + urb);
  45746. + }
  45747. + }
  45748. + DWC_FREE(dwc_otg_urb);
  45749. + if (!new_entry) {
  45750. + DWC_ERROR("dwc_otg_hcd: complete: cannot allocate URB TQ entry\n");
  45751. + urb->status = -EPROTO;
  45752. + /* don't schedule the tasklet -
  45753. + * directly return the packet here with error. */
  45754. +#if USB_URB_EP_LINKING
  45755. + usb_hcd_unlink_urb_from_ep(dwc_otg_hcd_to_hcd(hcd), urb);
  45756. +#endif
  45757. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  45758. + usb_hcd_giveback_urb(dwc_otg_hcd_to_hcd(hcd), urb);
  45759. +#else
  45760. + usb_hcd_giveback_urb(dwc_otg_hcd_to_hcd(hcd), urb, urb->status);
  45761. +#endif
  45762. + } else {
  45763. + new_entry->urb = urb;
  45764. +#if USB_URB_EP_LINKING
  45765. + rc = usb_hcd_check_unlink_urb(dwc_otg_hcd_to_hcd(hcd), urb, urb->status);
  45766. + if(0 == rc) {
  45767. + usb_hcd_unlink_urb_from_ep(dwc_otg_hcd_to_hcd(hcd), urb);
  45768. + }
  45769. +#endif
  45770. + if(0 == rc) {
  45771. + DWC_TAILQ_INSERT_TAIL(&hcd->completed_urb_list, new_entry,
  45772. + urb_tq_entries);
  45773. + DWC_TASK_HI_SCHEDULE(hcd->completion_tasklet);
  45774. + }
  45775. + }
  45776. + return 0;
  45777. +}
  45778. +
  45779. +static struct dwc_otg_hcd_function_ops hcd_fops = {
  45780. + .start = _start,
  45781. + .disconnect = _disconnect,
  45782. + .hub_info = _hub_info,
  45783. + .speed = _speed,
  45784. + .complete = _complete,
  45785. + .get_b_hnp_enable = _get_b_hnp_enable,
  45786. +};
  45787. +
  45788. +#ifdef CONFIG_ARM64
  45789. +
  45790. +static int simfiq_irq = -1;
  45791. +
  45792. +void local_fiq_enable(void)
  45793. +{
  45794. + if (simfiq_irq >= 0)
  45795. + enable_irq(simfiq_irq);
  45796. +}
  45797. +
  45798. +void local_fiq_disable(void)
  45799. +{
  45800. + if (simfiq_irq >= 0)
  45801. + disable_irq(simfiq_irq);
  45802. +}
  45803. +
  45804. +irqreturn_t fiq_irq_handler(int irq, void *dev_id)
  45805. +{
  45806. + dwc_otg_hcd_t *dwc_otg_hcd = (dwc_otg_hcd_t *)dev_id;
  45807. +
  45808. + if (fiq_fsm_enable)
  45809. + dwc_otg_fiq_fsm(dwc_otg_hcd->fiq_state, dwc_otg_hcd->core_if->core_params->host_channels);
  45810. + else
  45811. + dwc_otg_fiq_nop(dwc_otg_hcd->fiq_state);
  45812. +
  45813. + return IRQ_HANDLED;
  45814. +}
  45815. +
  45816. +#else
  45817. +static struct fiq_handler fh = {
  45818. + .name = "usb_fiq",
  45819. +};
  45820. +
  45821. +#endif
  45822. +
  45823. +static void hcd_init_fiq(void *cookie)
  45824. +{
  45825. + dwc_otg_device_t *otg_dev = cookie;
  45826. + dwc_otg_hcd_t *dwc_otg_hcd = otg_dev->hcd;
  45827. +#ifdef CONFIG_ARM64
  45828. + int retval = 0;
  45829. + int irq;
  45830. +#else
  45831. + struct pt_regs regs;
  45832. + int irq;
  45833. +
  45834. + if (claim_fiq(&fh)) {
  45835. + DWC_ERROR("Can't claim FIQ");
  45836. + BUG();
  45837. + }
  45838. + DWC_WARN("FIQ on core %d", smp_processor_id());
  45839. + DWC_WARN("FIQ ASM at %px length %d", &_dwc_otg_fiq_stub, (int)(&_dwc_otg_fiq_stub_end - &_dwc_otg_fiq_stub));
  45840. + set_fiq_handler((void *) &_dwc_otg_fiq_stub, &_dwc_otg_fiq_stub_end - &_dwc_otg_fiq_stub);
  45841. + memset(&regs,0,sizeof(regs));
  45842. +
  45843. + regs.ARM_r8 = (long) dwc_otg_hcd->fiq_state;
  45844. + if (fiq_fsm_enable) {
  45845. + regs.ARM_r9 = dwc_otg_hcd->core_if->core_params->host_channels;
  45846. + //regs.ARM_r10 = dwc_otg_hcd->dma;
  45847. + regs.ARM_fp = (long) dwc_otg_fiq_fsm;
  45848. + } else {
  45849. + regs.ARM_fp = (long) dwc_otg_fiq_nop;
  45850. + }
  45851. +
  45852. + regs.ARM_sp = (long) dwc_otg_hcd->fiq_stack + (sizeof(struct fiq_stack) - 4);
  45853. +
  45854. +// __show_regs(&regs);
  45855. + set_fiq_regs(&regs);
  45856. +#endif
  45857. +
  45858. + dwc_otg_hcd->fiq_state->dwc_regs_base = otg_dev->os_dep.base;
  45859. + //Set the mphi periph to the required registers
  45860. + dwc_otg_hcd->fiq_state->mphi_regs.base = otg_dev->os_dep.mphi_base;
  45861. + if (otg_dev->os_dep.use_swirq) {
  45862. + dwc_otg_hcd->fiq_state->mphi_regs.swirq_set =
  45863. + otg_dev->os_dep.mphi_base + 0x1f0;
  45864. + dwc_otg_hcd->fiq_state->mphi_regs.swirq_clr =
  45865. + otg_dev->os_dep.mphi_base + 0x1f4;
  45866. + DWC_WARN("Fake MPHI regs_base at %px",
  45867. + dwc_otg_hcd->fiq_state->mphi_regs.base);
  45868. + } else {
  45869. + dwc_otg_hcd->fiq_state->mphi_regs.ctrl =
  45870. + otg_dev->os_dep.mphi_base + 0x4c;
  45871. + dwc_otg_hcd->fiq_state->mphi_regs.outdda
  45872. + = otg_dev->os_dep.mphi_base + 0x28;
  45873. + dwc_otg_hcd->fiq_state->mphi_regs.outddb
  45874. + = otg_dev->os_dep.mphi_base + 0x2c;
  45875. + dwc_otg_hcd->fiq_state->mphi_regs.intstat
  45876. + = otg_dev->os_dep.mphi_base + 0x50;
  45877. + DWC_WARN("MPHI regs_base at %px",
  45878. + dwc_otg_hcd->fiq_state->mphi_regs.base);
  45879. +
  45880. + //Enable mphi peripheral
  45881. + writel((1<<31),dwc_otg_hcd->fiq_state->mphi_regs.ctrl);
  45882. +#ifdef DEBUG
  45883. + if (readl(dwc_otg_hcd->fiq_state->mphi_regs.ctrl) & 0x80000000)
  45884. + DWC_WARN("MPHI periph has been enabled");
  45885. + else
  45886. + DWC_WARN("MPHI periph has NOT been enabled");
  45887. +#endif
  45888. + }
  45889. + // Enable FIQ interrupt from USB peripheral
  45890. +#ifdef CONFIG_ARM64
  45891. + irq = otg_dev->os_dep.fiq_num;
  45892. +
  45893. + if (irq < 0) {
  45894. + DWC_ERROR("Can't get SIM-FIQ irq");
  45895. + return;
  45896. + }
  45897. +
  45898. + retval = request_irq(irq, fiq_irq_handler, 0, "dwc_otg_sim-fiq", dwc_otg_hcd);
  45899. +
  45900. + if (retval < 0) {
  45901. + DWC_ERROR("Unable to request SIM-FIQ irq\n");
  45902. + return;
  45903. + }
  45904. +
  45905. + simfiq_irq = irq;
  45906. +#else
  45907. +#ifdef CONFIG_GENERIC_IRQ_MULTI_HANDLER
  45908. + irq = otg_dev->os_dep.fiq_num;
  45909. +#else
  45910. + irq = INTERRUPT_VC_USB;
  45911. +#endif
  45912. + if (irq < 0) {
  45913. + DWC_ERROR("Can't get FIQ irq");
  45914. + return;
  45915. + }
  45916. + /*
  45917. + * We could take an interrupt immediately after enabling the FIQ.
  45918. + * Ensure coherency of hcd->fiq_state.
  45919. + */
  45920. + smp_mb();
  45921. + enable_fiq(irq);
  45922. + local_fiq_enable();
  45923. +#endif
  45924. +
  45925. +}
  45926. +
  45927. +/**
  45928. + * Initializes the HCD. This function allocates memory for and initializes the
  45929. + * static parts of the usb_hcd and dwc_otg_hcd structures. It also registers the
  45930. + * USB bus with the core and calls the hc_driver->start() function. It returns
  45931. + * a negative error on failure.
  45932. + */
  45933. +int hcd_init(dwc_bus_dev_t *_dev)
  45934. +{
  45935. + struct usb_hcd *hcd = NULL;
  45936. + dwc_otg_hcd_t *dwc_otg_hcd = NULL;
  45937. + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
  45938. + int retval = 0;
  45939. + u64 dmamask;
  45940. +
  45941. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD INIT otg_dev=%p\n", otg_dev);
  45942. +
  45943. + /* Set device flags indicating whether the HCD supports DMA. */
  45944. + if (dwc_otg_is_dma_enable(otg_dev->core_if))
  45945. + dmamask = DMA_BIT_MASK(32);
  45946. + else
  45947. + dmamask = 0;
  45948. +
  45949. +#if defined(LM_INTERFACE) || defined(PLATFORM_INTERFACE)
  45950. + dma_set_mask(&_dev->dev, dmamask);
  45951. + dma_set_coherent_mask(&_dev->dev, dmamask);
  45952. +#elif defined(PCI_INTERFACE)
  45953. + pci_set_dma_mask(_dev, dmamask);
  45954. + pci_set_consistent_dma_mask(_dev, dmamask);
  45955. +#endif
  45956. +
  45957. + /*
  45958. + * Allocate memory for the base HCD plus the DWC OTG HCD.
  45959. + * Initialize the base HCD.
  45960. + */
  45961. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
  45962. + hcd = usb_create_hcd(&dwc_otg_hc_driver, &_dev->dev, _dev->dev.bus_id);
  45963. +#else
  45964. + hcd = usb_create_hcd(&dwc_otg_hc_driver, &_dev->dev, dev_name(&_dev->dev));
  45965. + hcd->has_tt = 1;
  45966. +// hcd->uses_new_polling = 1;
  45967. +// hcd->poll_rh = 0;
  45968. +#endif
  45969. + if (!hcd) {
  45970. + retval = -ENOMEM;
  45971. + goto error1;
  45972. + }
  45973. +
  45974. + hcd->regs = otg_dev->os_dep.base;
  45975. +
  45976. +
  45977. + /* Initialize the DWC OTG HCD. */
  45978. + dwc_otg_hcd = dwc_otg_hcd_alloc_hcd();
  45979. + if (!dwc_otg_hcd) {
  45980. + goto error2;
  45981. + }
  45982. + ((struct wrapper_priv_data *)(hcd->hcd_priv))->dwc_otg_hcd =
  45983. + dwc_otg_hcd;
  45984. + otg_dev->hcd = dwc_otg_hcd;
  45985. + otg_dev->hcd->otg_dev = otg_dev;
  45986. +
  45987. +#ifdef CONFIG_ARM64
  45988. + if (dwc_otg_hcd_init(dwc_otg_hcd, otg_dev->core_if))
  45989. + goto error2;
  45990. +
  45991. + if (fiq_enable)
  45992. + hcd_init_fiq(otg_dev);
  45993. +#else
  45994. + if (dwc_otg_hcd_init(dwc_otg_hcd, otg_dev->core_if)) {
  45995. + goto error2;
  45996. + }
  45997. +
  45998. + if (fiq_enable) {
  45999. + if (num_online_cpus() > 1) {
  46000. + /*
  46001. + * bcm2709: can run the FIQ on a separate core to IRQs.
  46002. + * Ensure driver state is visible to other cores before setting up the FIQ.
  46003. + */
  46004. + smp_mb();
  46005. + smp_call_function_single(1, hcd_init_fiq, otg_dev, 1);
  46006. + } else {
  46007. + smp_call_function_single(0, hcd_init_fiq, otg_dev, 1);
  46008. + }
  46009. + }
  46010. +#endif
  46011. +
  46012. + hcd->self.otg_port = dwc_otg_hcd_otg_port(dwc_otg_hcd);
  46013. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,33) //don't support for LM(with 2.6.20.1 kernel)
  46014. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35) //version field absent later
  46015. + hcd->self.otg_version = dwc_otg_get_otg_version(otg_dev->core_if);
  46016. +#endif
  46017. + /* Don't support SG list at this point */
  46018. + hcd->self.sg_tablesize = 0;
  46019. +#endif
  46020. + /*
  46021. + * Finish generic HCD initialization and start the HCD. This function
  46022. + * allocates the DMA buffer pool, registers the USB bus, requests the
  46023. + * IRQ line, and calls hcd_start method.
  46024. + */
  46025. + retval = usb_add_hcd(hcd, otg_dev->os_dep.irq_num, IRQF_SHARED);
  46026. + if (retval < 0) {
  46027. + goto error2;
  46028. + }
  46029. +
  46030. + dwc_otg_hcd_set_priv_data(dwc_otg_hcd, hcd);
  46031. + return 0;
  46032. +
  46033. +error2:
  46034. + usb_put_hcd(hcd);
  46035. +error1:
  46036. + return retval;
  46037. +}
  46038. +
  46039. +/**
  46040. + * Removes the HCD.
  46041. + * Frees memory and resources associated with the HCD and deregisters the bus.
  46042. + */
  46043. +void hcd_remove(dwc_bus_dev_t *_dev)
  46044. +{
  46045. + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
  46046. + dwc_otg_hcd_t *dwc_otg_hcd;
  46047. + struct usb_hcd *hcd;
  46048. +
  46049. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD REMOVE otg_dev=%p\n", otg_dev);
  46050. +
  46051. + if (!otg_dev) {
  46052. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev NULL!\n", __func__);
  46053. + return;
  46054. + }
  46055. +
  46056. + dwc_otg_hcd = otg_dev->hcd;
  46057. +
  46058. + if (!dwc_otg_hcd) {
  46059. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->hcd NULL!\n", __func__);
  46060. + return;
  46061. + }
  46062. +
  46063. + hcd = dwc_otg_hcd_to_hcd(dwc_otg_hcd);
  46064. +
  46065. + if (!hcd) {
  46066. + DWC_DEBUGPL(DBG_ANY,
  46067. + "%s: dwc_otg_hcd_to_hcd(dwc_otg_hcd) NULL!\n",
  46068. + __func__);
  46069. + return;
  46070. + }
  46071. + usb_remove_hcd(hcd);
  46072. + dwc_otg_hcd_set_priv_data(dwc_otg_hcd, NULL);
  46073. + dwc_otg_hcd_remove(dwc_otg_hcd);
  46074. + usb_put_hcd(hcd);
  46075. +}
  46076. +
  46077. +/* =========================================================================
  46078. + * Linux HC Driver Functions
  46079. + * ========================================================================= */
  46080. +
  46081. +/** Initializes the DWC_otg controller and its root hub and prepares it for host
  46082. + * mode operation. Activates the root port. Returns 0 on success and a negative
  46083. + * error code on failure. */
  46084. +int hcd_start(struct usb_hcd *hcd)
  46085. +{
  46086. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  46087. + struct usb_bus *bus;
  46088. +
  46089. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD START\n");
  46090. + bus = hcd_to_bus(hcd);
  46091. +
  46092. + hcd->state = HC_STATE_RUNNING;
  46093. + if (dwc_otg_hcd_start(dwc_otg_hcd, &hcd_fops)) {
  46094. + return 0;
  46095. + }
  46096. +
  46097. + /* Initialize and connect root hub if one is not already attached */
  46098. + if (bus->root_hub) {
  46099. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD Has Root Hub\n");
  46100. + /* Inform the HUB driver to resume. */
  46101. + usb_hcd_resume_root_hub(hcd);
  46102. + }
  46103. +
  46104. + return 0;
  46105. +}
  46106. +
  46107. +/**
  46108. + * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
  46109. + * stopped.
  46110. + */
  46111. +void hcd_stop(struct usb_hcd *hcd)
  46112. +{
  46113. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  46114. +
  46115. + dwc_otg_hcd_stop(dwc_otg_hcd);
  46116. +}
  46117. +
  46118. +/** Returns the current frame number. */
  46119. +static int get_frame_number(struct usb_hcd *hcd)
  46120. +{
  46121. + hprt0_data_t hprt0;
  46122. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  46123. + hprt0.d32 = DWC_READ_REG32(dwc_otg_hcd->core_if->host_if->hprt0);
  46124. + if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_HIGH_SPEED)
  46125. + return dwc_otg_hcd_get_frame_number(dwc_otg_hcd) >> 3;
  46126. + else
  46127. + return dwc_otg_hcd_get_frame_number(dwc_otg_hcd);
  46128. +}
  46129. +
  46130. +#ifdef DEBUG
  46131. +static void dump_urb_info(struct urb *urb, char *fn_name)
  46132. +{
  46133. + DWC_PRINTF("%s, urb %p\n", fn_name, urb);
  46134. + DWC_PRINTF(" Device address: %d\n", usb_pipedevice(urb->pipe));
  46135. + DWC_PRINTF(" Endpoint: %d, %s\n", usb_pipeendpoint(urb->pipe),
  46136. + (usb_pipein(urb->pipe) ? "IN" : "OUT"));
  46137. + DWC_PRINTF(" Endpoint type: %s\n", ( {
  46138. + char *pipetype;
  46139. + switch (usb_pipetype(urb->pipe)) {
  46140. +case PIPE_CONTROL:
  46141. +pipetype = "CONTROL"; break; case PIPE_BULK:
  46142. +pipetype = "BULK"; break; case PIPE_INTERRUPT:
  46143. +pipetype = "INTERRUPT"; break; case PIPE_ISOCHRONOUS:
  46144. +pipetype = "ISOCHRONOUS"; break; default:
  46145. + pipetype = "UNKNOWN"; break;};
  46146. + pipetype;}
  46147. + )) ;
  46148. + DWC_PRINTF(" Speed: %s\n", ( {
  46149. + char *speed; switch (urb->dev->speed) {
  46150. +case USB_SPEED_HIGH:
  46151. +speed = "HIGH"; break; case USB_SPEED_FULL:
  46152. +speed = "FULL"; break; case USB_SPEED_LOW:
  46153. +speed = "LOW"; break; default:
  46154. + speed = "UNKNOWN"; break;};
  46155. + speed;}
  46156. + )) ;
  46157. + DWC_PRINTF(" Max packet size: %d\n",
  46158. + usb_maxpacket(urb->dev, urb->pipe);
  46159. + DWC_PRINTF(" Data buffer length: %d\n", urb->transfer_buffer_length);
  46160. + DWC_PRINTF(" Transfer buffer: %p, Transfer DMA: %p\n",
  46161. + urb->transfer_buffer, (void *)urb->transfer_dma);
  46162. + DWC_PRINTF(" Setup buffer: %p, Setup DMA: %p\n",
  46163. + urb->setup_packet, (void *)urb->setup_dma);
  46164. + DWC_PRINTF(" Interval: %d\n", urb->interval);
  46165. + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  46166. + int i;
  46167. + for (i = 0; i < urb->number_of_packets; i++) {
  46168. + DWC_PRINTF(" ISO Desc %d:\n", i);
  46169. + DWC_PRINTF(" offset: %d, length %d\n",
  46170. + urb->iso_frame_desc[i].offset,
  46171. + urb->iso_frame_desc[i].length);
  46172. + }
  46173. + }
  46174. +}
  46175. +#endif
  46176. +
  46177. +/** Starts processing a USB transfer request specified by a USB Request Block
  46178. + * (URB). mem_flags indicates the type of memory allocation to use while
  46179. + * processing this URB. */
  46180. +static int dwc_otg_urb_enqueue(struct usb_hcd *hcd,
  46181. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  46182. + struct usb_host_endpoint *ep,
  46183. +#endif
  46184. + struct urb *urb, gfp_t mem_flags)
  46185. +{
  46186. + int retval = 0;
  46187. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,28)
  46188. + struct usb_host_endpoint *ep = urb->ep;
  46189. +#endif
  46190. + dwc_irqflags_t irqflags;
  46191. + void **ref_ep_hcpriv = &ep->hcpriv;
  46192. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  46193. + dwc_otg_hcd_urb_t *dwc_otg_urb;
  46194. + int i;
  46195. + int alloc_bandwidth = 0;
  46196. + uint8_t ep_type = 0;
  46197. + uint32_t flags = 0;
  46198. + void *buf;
  46199. +
  46200. +#ifdef DEBUG
  46201. + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
  46202. + dump_urb_info(urb, "dwc_otg_urb_enqueue");
  46203. + }
  46204. +#endif
  46205. + if ((usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
  46206. + || (usb_pipetype(urb->pipe) == PIPE_INTERRUPT)) {
  46207. + if (!dwc_otg_hcd_is_bandwidth_allocated
  46208. + (dwc_otg_hcd, ref_ep_hcpriv)) {
  46209. + alloc_bandwidth = 1;
  46210. + }
  46211. + }
  46212. +
  46213. + switch (usb_pipetype(urb->pipe)) {
  46214. + case PIPE_CONTROL:
  46215. + ep_type = USB_ENDPOINT_XFER_CONTROL;
  46216. + break;
  46217. + case PIPE_ISOCHRONOUS:
  46218. + ep_type = USB_ENDPOINT_XFER_ISOC;
  46219. + break;
  46220. + case PIPE_BULK:
  46221. + ep_type = USB_ENDPOINT_XFER_BULK;
  46222. + break;
  46223. + case PIPE_INTERRUPT:
  46224. + ep_type = USB_ENDPOINT_XFER_INT;
  46225. + break;
  46226. + default:
  46227. + DWC_WARN("Wrong EP type - %d\n", usb_pipetype(urb->pipe));
  46228. + }
  46229. +
  46230. + /* # of packets is often 0 - do we really need to call this then? */
  46231. + dwc_otg_urb = dwc_otg_hcd_urb_alloc(dwc_otg_hcd,
  46232. + urb->number_of_packets,
  46233. + mem_flags == GFP_ATOMIC ? 1 : 0);
  46234. +
  46235. + if(dwc_otg_urb == NULL)
  46236. + return -ENOMEM;
  46237. +
  46238. + if (!dwc_otg_urb && urb->number_of_packets)
  46239. + return -ENOMEM;
  46240. +
  46241. + dwc_otg_hcd_urb_set_pipeinfo(dwc_otg_urb, usb_pipedevice(urb->pipe),
  46242. + usb_pipeendpoint(urb->pipe), ep_type,
  46243. + usb_pipein(urb->pipe),
  46244. + usb_maxpacket(urb->dev, urb->pipe));
  46245. +
  46246. + buf = urb->transfer_buffer;
  46247. + if (hcd_uses_dma(hcd) && !buf && urb->transfer_buffer_length) {
  46248. + /*
  46249. + * Calculate virtual address from physical address,
  46250. + * because some class driver may not fill transfer_buffer.
  46251. + * In Buffer DMA mode virual address is used,
  46252. + * when handling non DWORD aligned buffers.
  46253. + */
  46254. + buf = (void *)__bus_to_virt((unsigned long)urb->transfer_dma);
  46255. + dev_warn_once(&urb->dev->dev,
  46256. + "USB transfer_buffer was NULL, will use __bus_to_virt(%pad)=%p\n",
  46257. + &urb->transfer_dma, buf);
  46258. + }
  46259. +
  46260. + if (!buf && urb->transfer_buffer_length) {
  46261. + DWC_FREE(dwc_otg_urb);
  46262. + DWC_ERROR("transfer_buffer is NULL in PIO mode or both "
  46263. + "transfer_buffer and transfer_dma are NULL in DMA mode\n");
  46264. + return -EINVAL;
  46265. + }
  46266. +
  46267. + if (!(urb->transfer_flags & URB_NO_INTERRUPT))
  46268. + flags |= URB_GIVEBACK_ASAP;
  46269. + if (urb->transfer_flags & URB_ZERO_PACKET)
  46270. + flags |= URB_SEND_ZERO_PACKET;
  46271. +
  46272. + dwc_otg_hcd_urb_set_params(dwc_otg_urb, urb, buf,
  46273. + urb->transfer_dma,
  46274. + urb->transfer_buffer_length,
  46275. + urb->setup_packet,
  46276. + urb->setup_dma, flags, urb->interval);
  46277. +
  46278. + for (i = 0; i < urb->number_of_packets; ++i) {
  46279. + dwc_otg_hcd_urb_set_iso_desc_params(dwc_otg_urb, i,
  46280. + urb->
  46281. + iso_frame_desc[i].offset,
  46282. + urb->
  46283. + iso_frame_desc[i].length);
  46284. + }
  46285. +
  46286. + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &irqflags);
  46287. + urb->hcpriv = dwc_otg_urb;
  46288. +#if USB_URB_EP_LINKING
  46289. + retval = usb_hcd_link_urb_to_ep(hcd, urb);
  46290. + if (0 == retval)
  46291. +#endif
  46292. + {
  46293. + retval = dwc_otg_hcd_urb_enqueue(dwc_otg_hcd, dwc_otg_urb,
  46294. + /*(dwc_otg_qh_t **)*/
  46295. + ref_ep_hcpriv, 1);
  46296. + if (0 == retval) {
  46297. + if (alloc_bandwidth) {
  46298. + allocate_bus_bandwidth(hcd,
  46299. + dwc_otg_hcd_get_ep_bandwidth(
  46300. + dwc_otg_hcd, *ref_ep_hcpriv),
  46301. + urb);
  46302. + }
  46303. + } else {
  46304. + DWC_DEBUGPL(DBG_HCD, "DWC OTG dwc_otg_hcd_urb_enqueue failed rc %d\n", retval);
  46305. +#if USB_URB_EP_LINKING
  46306. + usb_hcd_unlink_urb_from_ep(hcd, urb);
  46307. +#endif
  46308. + DWC_FREE(dwc_otg_urb);
  46309. + urb->hcpriv = NULL;
  46310. + if (retval == -DWC_E_NO_DEVICE)
  46311. + retval = -ENODEV;
  46312. + }
  46313. + }
  46314. +#if USB_URB_EP_LINKING
  46315. + else
  46316. + {
  46317. + DWC_FREE(dwc_otg_urb);
  46318. + urb->hcpriv = NULL;
  46319. + }
  46320. +#endif
  46321. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, irqflags);
  46322. + return retval;
  46323. +}
  46324. +
  46325. +/** Aborts/cancels a USB transfer request. Always returns 0 to indicate
  46326. + * success. */
  46327. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  46328. +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
  46329. +#else
  46330. +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  46331. +#endif
  46332. +{
  46333. + dwc_irqflags_t flags;
  46334. + dwc_otg_hcd_t *dwc_otg_hcd;
  46335. + int rc;
  46336. +
  46337. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue\n");
  46338. +
  46339. + dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  46340. +
  46341. +#ifdef DEBUG
  46342. + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
  46343. + dump_urb_info(urb, "dwc_otg_urb_dequeue");
  46344. + }
  46345. +#endif
  46346. +
  46347. + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
  46348. + rc = usb_hcd_check_unlink_urb(hcd, urb, status);
  46349. + if (0 == rc) {
  46350. + if(urb->hcpriv != NULL) {
  46351. + dwc_otg_hcd_urb_dequeue(dwc_otg_hcd,
  46352. + (dwc_otg_hcd_urb_t *)urb->hcpriv);
  46353. +
  46354. + DWC_FREE(urb->hcpriv);
  46355. + urb->hcpriv = NULL;
  46356. + }
  46357. + }
  46358. +
  46359. + if (0 == rc) {
  46360. + /* Higher layer software sets URB status. */
  46361. +#if USB_URB_EP_LINKING
  46362. + usb_hcd_unlink_urb_from_ep(hcd, urb);
  46363. +#endif
  46364. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
  46365. +
  46366. +
  46367. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  46368. + usb_hcd_giveback_urb(hcd, urb);
  46369. +#else
  46370. + usb_hcd_giveback_urb(hcd, urb, status);
  46371. +#endif
  46372. + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
  46373. + DWC_PRINTF("Called usb_hcd_giveback_urb() \n");
  46374. + DWC_PRINTF(" 1urb->status = %d\n", urb->status);
  46375. + }
  46376. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue OK\n");
  46377. + } else {
  46378. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
  46379. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue failed - rc %d\n",
  46380. + rc);
  46381. + }
  46382. +
  46383. + return rc;
  46384. +}
  46385. +
  46386. +/* Frees resources in the DWC_otg controller related to a given endpoint. Also
  46387. + * clears state in the HCD related to the endpoint. Any URBs for the endpoint
  46388. + * must already be dequeued. */
  46389. +static void endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
  46390. +{
  46391. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  46392. +
  46393. + DWC_DEBUGPL(DBG_HCD,
  46394. + "DWC OTG HCD EP DISABLE: _bEndpointAddress=0x%02x, "
  46395. + "endpoint=%d\n", ep->desc.bEndpointAddress,
  46396. + dwc_ep_addr_to_endpoint(ep->desc.bEndpointAddress));
  46397. + dwc_otg_hcd_endpoint_disable(dwc_otg_hcd, ep->hcpriv, 250);
  46398. + ep->hcpriv = NULL;
  46399. +}
  46400. +
  46401. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
  46402. +/* Resets endpoint specific parameter values, in current version used to reset
  46403. + * the data toggle(as a WA). This function can be called from usb_clear_halt routine */
  46404. +static void endpoint_reset(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
  46405. +{
  46406. + dwc_irqflags_t flags;
  46407. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  46408. +
  46409. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD EP RESET: Endpoint Num=0x%02d\n",
  46410. + ep->desc.bEndpointAddress);
  46411. +
  46412. + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
  46413. + if (ep->hcpriv) {
  46414. + dwc_otg_hcd_endpoint_reset(dwc_otg_hcd, ep->hcpriv);
  46415. + }
  46416. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
  46417. +}
  46418. +#endif
  46419. +
  46420. +/** Handles host mode interrupts for the DWC_otg controller. Returns IRQ_NONE if
  46421. + * there was no interrupt to handle. Returns IRQ_HANDLED if there was a valid
  46422. + * interrupt.
  46423. + *
  46424. + * This function is called by the USB core when an interrupt occurs */
  46425. +static irqreturn_t dwc_otg_hcd_irq(struct usb_hcd *hcd)
  46426. +{
  46427. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  46428. + int32_t retval = dwc_otg_hcd_handle_intr(dwc_otg_hcd);
  46429. + if (retval != 0) {
  46430. + S3C2410X_CLEAR_EINTPEND();
  46431. + }
  46432. + return IRQ_RETVAL(retval);
  46433. +}
  46434. +
  46435. +/** Creates Status Change bitmap for the root hub and root port. The bitmap is
  46436. + * returned in buf. Bit 0 is the status change indicator for the root hub. Bit 1
  46437. + * is the status change indicator for the single root port. Returns 1 if either
  46438. + * change indicator is 1, otherwise returns 0. */
  46439. +int hub_status_data(struct usb_hcd *hcd, char *buf)
  46440. +{
  46441. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  46442. +
  46443. + buf[0] = 0;
  46444. + buf[0] |= (dwc_otg_hcd_is_status_changed(dwc_otg_hcd, 1)) << 1;
  46445. +
  46446. + return (buf[0] != 0);
  46447. +}
  46448. +
  46449. +/** Handles hub class-specific requests. */
  46450. +int hub_control(struct usb_hcd *hcd,
  46451. + u16 typeReq, u16 wValue, u16 wIndex, char *buf, u16 wLength)
  46452. +{
  46453. + int retval;
  46454. +
  46455. + retval = dwc_otg_hcd_hub_control(hcd_to_dwc_otg_hcd(hcd),
  46456. + typeReq, wValue, wIndex, buf, wLength);
  46457. +
  46458. + switch (retval) {
  46459. + case -DWC_E_INVALID:
  46460. + retval = -EINVAL;
  46461. + break;
  46462. + }
  46463. +
  46464. + return retval;
  46465. +}
  46466. +
  46467. +#endif /* DWC_DEVICE_ONLY */
  46468. --- /dev/null
  46469. +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd_queue.c
  46470. @@ -0,0 +1,974 @@
  46471. +/* ==========================================================================
  46472. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_queue.c $
  46473. + * $Revision: #44 $
  46474. + * $Date: 2011/10/26 $
  46475. + * $Change: 1873028 $
  46476. + *
  46477. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  46478. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  46479. + * otherwise expressly agreed to in writing between Synopsys and you.
  46480. + *
  46481. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  46482. + * any End User Software License Agreement or Agreement for Licensed Product
  46483. + * with Synopsys or any supplement thereto. You are permitted to use and
  46484. + * redistribute this Software in source and binary forms, with or without
  46485. + * modification, provided that redistributions of source code must retain this
  46486. + * notice. You may not view, use, disclose, copy or distribute this file or
  46487. + * any information contained herein except pursuant to this license grant from
  46488. + * Synopsys. If you do not agree with this notice, including the disclaimer
  46489. + * below, then you are not authorized to use the Software.
  46490. + *
  46491. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  46492. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  46493. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  46494. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  46495. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  46496. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  46497. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  46498. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  46499. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  46500. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  46501. + * DAMAGE.
  46502. + * ========================================================================== */
  46503. +#ifndef DWC_DEVICE_ONLY
  46504. +
  46505. +/**
  46506. + * @file
  46507. + *
  46508. + * This file contains the functions to manage Queue Heads and Queue
  46509. + * Transfer Descriptors.
  46510. + */
  46511. +
  46512. +#include "dwc_otg_hcd.h"
  46513. +#include "dwc_otg_regs.h"
  46514. +
  46515. +extern bool microframe_schedule;
  46516. +extern unsigned short int_ep_interval_min;
  46517. +
  46518. +/**
  46519. + * Free each QTD in the QH's QTD-list then free the QH. QH should already be
  46520. + * removed from a list. QTD list should already be empty if called from URB
  46521. + * Dequeue.
  46522. + *
  46523. + * @param hcd HCD instance.
  46524. + * @param qh The QH to free.
  46525. + */
  46526. +void dwc_otg_hcd_qh_free(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  46527. +{
  46528. + dwc_otg_qtd_t *qtd, *qtd_tmp;
  46529. + dwc_irqflags_t flags;
  46530. + uint32_t buf_size = 0;
  46531. + uint8_t *align_buf_virt = NULL;
  46532. + dwc_dma_t align_buf_dma;
  46533. + struct device *dev = dwc_otg_hcd_to_dev(hcd);
  46534. +
  46535. + /* Free each QTD in the QTD list */
  46536. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  46537. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry) {
  46538. + DWC_CIRCLEQ_REMOVE(&qh->qtd_list, qtd, qtd_list_entry);
  46539. + dwc_otg_hcd_qtd_free(qtd);
  46540. + }
  46541. +
  46542. + if (hcd->core_if->dma_desc_enable) {
  46543. + dwc_otg_hcd_qh_free_ddma(hcd, qh);
  46544. + } else if (qh->dw_align_buf) {
  46545. + if (qh->ep_type == UE_ISOCHRONOUS) {
  46546. + buf_size = 4096;
  46547. + } else {
  46548. + buf_size = hcd->core_if->core_params->max_transfer_size;
  46549. + }
  46550. + align_buf_virt = qh->dw_align_buf;
  46551. + align_buf_dma = qh->dw_align_buf_dma;
  46552. + }
  46553. +
  46554. + DWC_FREE(qh);
  46555. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  46556. + if (align_buf_virt)
  46557. + DWC_DMA_FREE(dev, buf_size, align_buf_virt, align_buf_dma);
  46558. + return;
  46559. +}
  46560. +
  46561. +#define BitStuffTime(bytecount) ((8 * 7* bytecount) / 6)
  46562. +#define HS_HOST_DELAY 5 /* nanoseconds */
  46563. +#define FS_LS_HOST_DELAY 1000 /* nanoseconds */
  46564. +#define HUB_LS_SETUP 333 /* nanoseconds */
  46565. +#define NS_TO_US(ns) ((ns + 500) / 1000)
  46566. + /* convert & round nanoseconds to microseconds */
  46567. +
  46568. +static uint32_t calc_bus_time(int speed, int is_in, int is_isoc, int bytecount)
  46569. +{
  46570. + unsigned long retval;
  46571. +
  46572. + switch (speed) {
  46573. + case USB_SPEED_HIGH:
  46574. + if (is_isoc) {
  46575. + retval =
  46576. + ((38 * 8 * 2083) +
  46577. + (2083 * (3 + BitStuffTime(bytecount)))) / 1000 +
  46578. + HS_HOST_DELAY;
  46579. + } else {
  46580. + retval =
  46581. + ((55 * 8 * 2083) +
  46582. + (2083 * (3 + BitStuffTime(bytecount)))) / 1000 +
  46583. + HS_HOST_DELAY;
  46584. + }
  46585. + break;
  46586. + case USB_SPEED_FULL:
  46587. + if (is_isoc) {
  46588. + retval =
  46589. + (8354 * (31 + 10 * BitStuffTime(bytecount))) / 1000;
  46590. + if (is_in) {
  46591. + retval = 7268 + FS_LS_HOST_DELAY + retval;
  46592. + } else {
  46593. + retval = 6265 + FS_LS_HOST_DELAY + retval;
  46594. + }
  46595. + } else {
  46596. + retval =
  46597. + (8354 * (31 + 10 * BitStuffTime(bytecount))) / 1000;
  46598. + retval = 9107 + FS_LS_HOST_DELAY + retval;
  46599. + }
  46600. + break;
  46601. + case USB_SPEED_LOW:
  46602. + if (is_in) {
  46603. + retval =
  46604. + (67667 * (31 + 10 * BitStuffTime(bytecount))) /
  46605. + 1000;
  46606. + retval =
  46607. + 64060 + (2 * HUB_LS_SETUP) + FS_LS_HOST_DELAY +
  46608. + retval;
  46609. + } else {
  46610. + retval =
  46611. + (66700 * (31 + 10 * BitStuffTime(bytecount))) /
  46612. + 1000;
  46613. + retval =
  46614. + 64107 + (2 * HUB_LS_SETUP) + FS_LS_HOST_DELAY +
  46615. + retval;
  46616. + }
  46617. + break;
  46618. + default:
  46619. + DWC_WARN("Unknown device speed\n");
  46620. + retval = -1;
  46621. + }
  46622. +
  46623. + return NS_TO_US(retval);
  46624. +}
  46625. +
  46626. +/**
  46627. + * Initializes a QH structure.
  46628. + *
  46629. + * @param hcd The HCD state structure for the DWC OTG controller.
  46630. + * @param qh The QH to init.
  46631. + * @param urb Holds the information about the device/endpoint that we need
  46632. + * to initialize the QH.
  46633. + */
  46634. +#define SCHEDULE_SLOP 10
  46635. +void qh_init(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh, dwc_otg_hcd_urb_t * urb)
  46636. +{
  46637. + char *speed, *type;
  46638. + int dev_speed;
  46639. + uint32_t hub_addr, hub_port;
  46640. + hprt0_data_t hprt;
  46641. +
  46642. + dwc_memset(qh, 0, sizeof(dwc_otg_qh_t));
  46643. + hprt.d32 = DWC_READ_REG32(hcd->core_if->host_if->hprt0);
  46644. +
  46645. + /* Initialize QH */
  46646. + qh->ep_type = dwc_otg_hcd_get_pipe_type(&urb->pipe_info);
  46647. + qh->ep_is_in = dwc_otg_hcd_is_pipe_in(&urb->pipe_info) ? 1 : 0;
  46648. +
  46649. + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
  46650. + qh->maxp = dwc_otg_hcd_get_mps(&urb->pipe_info);
  46651. + DWC_CIRCLEQ_INIT(&qh->qtd_list);
  46652. + DWC_LIST_INIT(&qh->qh_list_entry);
  46653. + qh->channel = NULL;
  46654. +
  46655. + /* FS/LS Enpoint on HS Hub
  46656. + * NOT virtual root hub */
  46657. + dev_speed = hcd->fops->speed(hcd, urb->priv);
  46658. +
  46659. + hcd->fops->hub_info(hcd, urb->priv, &hub_addr, &hub_port);
  46660. + qh->do_split = 0;
  46661. + if (microframe_schedule)
  46662. + qh->speed = dev_speed;
  46663. +
  46664. + qh->nak_frame = 0xffff;
  46665. +
  46666. + if (hprt.b.prtspd == DWC_HPRT0_PRTSPD_HIGH_SPEED &&
  46667. + dev_speed != USB_SPEED_HIGH) {
  46668. + DWC_DEBUGPL(DBG_HCD,
  46669. + "QH init: EP %d: TT found at hub addr %d, for port %d\n",
  46670. + dwc_otg_hcd_get_ep_num(&urb->pipe_info), hub_addr,
  46671. + hub_port);
  46672. + qh->do_split = 1;
  46673. + qh->skip_count = 0;
  46674. + }
  46675. +
  46676. + if (qh->ep_type == UE_INTERRUPT || qh->ep_type == UE_ISOCHRONOUS) {
  46677. + /* Compute scheduling parameters once and save them. */
  46678. +
  46679. + /** @todo Account for split transfers in the bus time. */
  46680. + int bytecount =
  46681. + dwc_hb_mult(qh->maxp) * dwc_max_packet(qh->maxp);
  46682. +
  46683. + qh->usecs =
  46684. + calc_bus_time((qh->do_split ? USB_SPEED_HIGH : dev_speed),
  46685. + qh->ep_is_in, (qh->ep_type == UE_ISOCHRONOUS),
  46686. + bytecount);
  46687. + /* Start in a slightly future (micro)frame. */
  46688. + qh->sched_frame = dwc_frame_num_inc(hcd->frame_number,
  46689. + SCHEDULE_SLOP);
  46690. + qh->interval = urb->interval;
  46691. +
  46692. + if (hprt.b.prtspd == DWC_HPRT0_PRTSPD_HIGH_SPEED) {
  46693. + if (dev_speed == USB_SPEED_LOW ||
  46694. + dev_speed == USB_SPEED_FULL) {
  46695. + qh->interval *= 8;
  46696. + qh->sched_frame |= 0x7;
  46697. + qh->start_split_frame = qh->sched_frame;
  46698. + } else if (int_ep_interval_min >= 2 &&
  46699. + qh->interval < int_ep_interval_min &&
  46700. + qh->ep_type == UE_INTERRUPT) {
  46701. + qh->interval = int_ep_interval_min;
  46702. + }
  46703. + }
  46704. + }
  46705. +
  46706. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD QH Initialized\n");
  46707. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - qh = %p\n", qh);
  46708. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Device Address = %d\n",
  46709. + dwc_otg_hcd_get_dev_addr(&urb->pipe_info));
  46710. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Endpoint %d, %s\n",
  46711. + dwc_otg_hcd_get_ep_num(&urb->pipe_info),
  46712. + dwc_otg_hcd_is_pipe_in(&urb->pipe_info) ? "IN" : "OUT");
  46713. + switch (dev_speed) {
  46714. + case USB_SPEED_LOW:
  46715. + qh->dev_speed = DWC_OTG_EP_SPEED_LOW;
  46716. + speed = "low";
  46717. + break;
  46718. + case USB_SPEED_FULL:
  46719. + qh->dev_speed = DWC_OTG_EP_SPEED_FULL;
  46720. + speed = "full";
  46721. + break;
  46722. + case USB_SPEED_HIGH:
  46723. + qh->dev_speed = DWC_OTG_EP_SPEED_HIGH;
  46724. + speed = "high";
  46725. + break;
  46726. + default:
  46727. + speed = "?";
  46728. + break;
  46729. + }
  46730. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Speed = %s\n", speed);
  46731. +
  46732. + switch (qh->ep_type) {
  46733. + case UE_ISOCHRONOUS:
  46734. + type = "isochronous";
  46735. + break;
  46736. + case UE_INTERRUPT:
  46737. + type = "interrupt";
  46738. + break;
  46739. + case UE_CONTROL:
  46740. + type = "control";
  46741. + break;
  46742. + case UE_BULK:
  46743. + type = "bulk";
  46744. + break;
  46745. + default:
  46746. + type = "?";
  46747. + break;
  46748. + }
  46749. +
  46750. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Type = %s\n", type);
  46751. +
  46752. +#ifdef DEBUG
  46753. + if (qh->ep_type == UE_INTERRUPT) {
  46754. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - usecs = %d\n",
  46755. + qh->usecs);
  46756. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - interval = %d\n",
  46757. + qh->interval);
  46758. + }
  46759. +#endif
  46760. +
  46761. +}
  46762. +
  46763. +/**
  46764. + * This function allocates and initializes a QH.
  46765. + *
  46766. + * @param hcd The HCD state structure for the DWC OTG controller.
  46767. + * @param urb Holds the information about the device/endpoint that we need
  46768. + * to initialize the QH.
  46769. + * @param atomic_alloc Flag to do atomic allocation if needed
  46770. + *
  46771. + * @return Returns pointer to the newly allocated QH, or NULL on error. */
  46772. +dwc_otg_qh_t *dwc_otg_hcd_qh_create(dwc_otg_hcd_t * hcd,
  46773. + dwc_otg_hcd_urb_t * urb, int atomic_alloc)
  46774. +{
  46775. + dwc_otg_qh_t *qh;
  46776. +
  46777. + /* Allocate memory */
  46778. + /** @todo add memflags argument */
  46779. + qh = dwc_otg_hcd_qh_alloc(atomic_alloc);
  46780. + if (qh == NULL) {
  46781. + DWC_ERROR("qh allocation failed");
  46782. + return NULL;
  46783. + }
  46784. +
  46785. + qh_init(hcd, qh, urb);
  46786. +
  46787. + if (hcd->core_if->dma_desc_enable
  46788. + && (dwc_otg_hcd_qh_init_ddma(hcd, qh) < 0)) {
  46789. + dwc_otg_hcd_qh_free(hcd, qh);
  46790. + return NULL;
  46791. + }
  46792. +
  46793. + return qh;
  46794. +}
  46795. +
  46796. +/* microframe_schedule=0 start */
  46797. +
  46798. +/**
  46799. + * Checks that a channel is available for a periodic transfer.
  46800. + *
  46801. + * @return 0 if successful, negative error code otherise.
  46802. + */
  46803. +static int periodic_channel_available(dwc_otg_hcd_t * hcd)
  46804. +{
  46805. + /*
  46806. + * Currently assuming that there is a dedicated host channnel for each
  46807. + * periodic transaction plus at least one host channel for
  46808. + * non-periodic transactions.
  46809. + */
  46810. + int status;
  46811. + int num_channels;
  46812. +
  46813. + num_channels = hcd->core_if->core_params->host_channels;
  46814. + if ((hcd->periodic_channels + hcd->non_periodic_channels < num_channels)
  46815. + && (hcd->periodic_channels < num_channels - 1)) {
  46816. + status = 0;
  46817. + } else {
  46818. + DWC_INFO("%s: Total channels: %d, Periodic: %d, Non-periodic: %d\n",
  46819. + __func__, num_channels, hcd->periodic_channels, hcd->non_periodic_channels); //NOTICE
  46820. + status = -DWC_E_NO_SPACE;
  46821. + }
  46822. +
  46823. + return status;
  46824. +}
  46825. +
  46826. +/**
  46827. + * Checks that there is sufficient bandwidth for the specified QH in the
  46828. + * periodic schedule. For simplicity, this calculation assumes that all the
  46829. + * transfers in the periodic schedule may occur in the same (micro)frame.
  46830. + *
  46831. + * @param hcd The HCD state structure for the DWC OTG controller.
  46832. + * @param qh QH containing periodic bandwidth required.
  46833. + *
  46834. + * @return 0 if successful, negative error code otherwise.
  46835. + */
  46836. +static int check_periodic_bandwidth(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  46837. +{
  46838. + int status;
  46839. + int16_t max_claimed_usecs;
  46840. +
  46841. + status = 0;
  46842. +
  46843. + if ((qh->dev_speed == DWC_OTG_EP_SPEED_HIGH) || qh->do_split) {
  46844. + /*
  46845. + * High speed mode.
  46846. + * Max periodic usecs is 80% x 125 usec = 100 usec.
  46847. + */
  46848. +
  46849. + max_claimed_usecs = 100 - qh->usecs;
  46850. + } else {
  46851. + /*
  46852. + * Full speed mode.
  46853. + * Max periodic usecs is 90% x 1000 usec = 900 usec.
  46854. + */
  46855. + max_claimed_usecs = 900 - qh->usecs;
  46856. + }
  46857. +
  46858. + if (hcd->periodic_usecs > max_claimed_usecs) {
  46859. + DWC_INFO("%s: already claimed usecs %d, required usecs %d\n", __func__, hcd->periodic_usecs, qh->usecs); //NOTICE
  46860. + status = -DWC_E_NO_SPACE;
  46861. + }
  46862. +
  46863. + return status;
  46864. +}
  46865. +
  46866. +/* microframe_schedule=0 end */
  46867. +
  46868. +/**
  46869. + * Microframe scheduler
  46870. + * track the total use in hcd->frame_usecs
  46871. + * keep each qh use in qh->frame_usecs
  46872. + * when surrendering the qh then donate the time back
  46873. + */
  46874. +const unsigned short max_uframe_usecs[]={ 100, 100, 100, 100, 100, 100, 30, 0 };
  46875. +
  46876. +/*
  46877. + * called from dwc_otg_hcd.c:dwc_otg_hcd_init
  46878. + */
  46879. +void init_hcd_usecs(dwc_otg_hcd_t *_hcd)
  46880. +{
  46881. + int i;
  46882. + if (_hcd->flags.b.port_speed == DWC_HPRT0_PRTSPD_FULL_SPEED) {
  46883. + _hcd->frame_usecs[0] = 900;
  46884. + for (i = 1; i < 8; i++)
  46885. + _hcd->frame_usecs[i] = 0;
  46886. + } else {
  46887. + for (i = 0; i < 8; i++)
  46888. + _hcd->frame_usecs[i] = max_uframe_usecs[i];
  46889. + }
  46890. +}
  46891. +
  46892. +static int find_single_uframe(dwc_otg_hcd_t * _hcd, dwc_otg_qh_t * _qh)
  46893. +{
  46894. + int i;
  46895. + unsigned short utime;
  46896. + int t_left;
  46897. + int ret;
  46898. + int done;
  46899. +
  46900. + ret = -1;
  46901. + utime = _qh->usecs;
  46902. + t_left = utime;
  46903. + i = 0;
  46904. + done = 0;
  46905. + while (done == 0) {
  46906. + /* At the start _hcd->frame_usecs[i] = max_uframe_usecs[i]; */
  46907. + if (utime <= _hcd->frame_usecs[i]) {
  46908. + _hcd->frame_usecs[i] -= utime;
  46909. + _qh->frame_usecs[i] += utime;
  46910. + t_left -= utime;
  46911. + ret = i;
  46912. + done = 1;
  46913. + return ret;
  46914. + } else {
  46915. + i++;
  46916. + if (i == 8) {
  46917. + done = 1;
  46918. + ret = -1;
  46919. + }
  46920. + }
  46921. + }
  46922. + return ret;
  46923. + }
  46924. +
  46925. +/*
  46926. + * use this for FS apps that can span multiple uframes
  46927. + */
  46928. +static int find_multi_uframe(dwc_otg_hcd_t * _hcd, dwc_otg_qh_t * _qh)
  46929. +{
  46930. + int i;
  46931. + int j;
  46932. + unsigned short utime;
  46933. + int t_left;
  46934. + int ret;
  46935. + int done;
  46936. + unsigned short xtime;
  46937. +
  46938. + ret = -1;
  46939. + utime = _qh->usecs;
  46940. + t_left = utime;
  46941. + i = 0;
  46942. + done = 0;
  46943. +loop:
  46944. + while (done == 0) {
  46945. + if(_hcd->frame_usecs[i] <= 0) {
  46946. + i++;
  46947. + if (i == 8) {
  46948. + done = 1;
  46949. + ret = -1;
  46950. + }
  46951. + goto loop;
  46952. + }
  46953. +
  46954. + /*
  46955. + * we need n consecutive slots
  46956. + * so use j as a start slot j plus j+1 must be enough time (for now)
  46957. + */
  46958. + xtime= _hcd->frame_usecs[i];
  46959. + for (j = i+1 ; j < 8 ; j++ ) {
  46960. + /*
  46961. + * if we add this frame remaining time to xtime we may
  46962. + * be OK, if not we need to test j for a complete frame
  46963. + */
  46964. + if ((xtime+_hcd->frame_usecs[j]) < utime) {
  46965. + if (_hcd->frame_usecs[j] < max_uframe_usecs[j]) {
  46966. + j = 8;
  46967. + ret = -1;
  46968. + continue;
  46969. + }
  46970. + }
  46971. + if (xtime >= utime) {
  46972. + ret = i;
  46973. + j = 8; /* stop loop with a good value ret */
  46974. + continue;
  46975. + }
  46976. + /* add the frame time to x time */
  46977. + xtime += _hcd->frame_usecs[j];
  46978. + /* we must have a fully available next frame or break */
  46979. + if ((xtime < utime)
  46980. + && (_hcd->frame_usecs[j] == max_uframe_usecs[j])) {
  46981. + ret = -1;
  46982. + j = 8; /* stop loop with a bad value ret */
  46983. + continue;
  46984. + }
  46985. + }
  46986. + if (ret >= 0) {
  46987. + t_left = utime;
  46988. + for (j = i; (t_left>0) && (j < 8); j++ ) {
  46989. + t_left -= _hcd->frame_usecs[j];
  46990. + if ( t_left <= 0 ) {
  46991. + _qh->frame_usecs[j] += _hcd->frame_usecs[j] + t_left;
  46992. + _hcd->frame_usecs[j]= -t_left;
  46993. + ret = i;
  46994. + done = 1;
  46995. + } else {
  46996. + _qh->frame_usecs[j] += _hcd->frame_usecs[j];
  46997. + _hcd->frame_usecs[j] = 0;
  46998. + }
  46999. + }
  47000. + } else {
  47001. + i++;
  47002. + if (i == 8) {
  47003. + done = 1;
  47004. + ret = -1;
  47005. + }
  47006. + }
  47007. + }
  47008. + return ret;
  47009. +}
  47010. +
  47011. +static int find_uframe(dwc_otg_hcd_t * _hcd, dwc_otg_qh_t * _qh)
  47012. +{
  47013. + int ret;
  47014. + ret = -1;
  47015. +
  47016. + if (_qh->speed == USB_SPEED_HIGH ||
  47017. + _hcd->flags.b.port_speed == DWC_HPRT0_PRTSPD_FULL_SPEED) {
  47018. + /* if this is a hs transaction we need a full frame - or account for FS usecs */
  47019. + ret = find_single_uframe(_hcd, _qh);
  47020. + } else {
  47021. + /* if this is a fs transaction we may need a sequence of frames */
  47022. + ret = find_multi_uframe(_hcd, _qh);
  47023. + }
  47024. + return ret;
  47025. +}
  47026. +
  47027. +/**
  47028. + * Checks that the max transfer size allowed in a host channel is large enough
  47029. + * to handle the maximum data transfer in a single (micro)frame for a periodic
  47030. + * transfer.
  47031. + *
  47032. + * @param hcd The HCD state structure for the DWC OTG controller.
  47033. + * @param qh QH for a periodic endpoint.
  47034. + *
  47035. + * @return 0 if successful, negative error code otherwise.
  47036. + */
  47037. +static int check_max_xfer_size(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  47038. +{
  47039. + int status;
  47040. + uint32_t max_xfer_size;
  47041. + uint32_t max_channel_xfer_size;
  47042. +
  47043. + status = 0;
  47044. +
  47045. + max_xfer_size = dwc_max_packet(qh->maxp) * dwc_hb_mult(qh->maxp);
  47046. + max_channel_xfer_size = hcd->core_if->core_params->max_transfer_size;
  47047. +
  47048. + if (max_xfer_size > max_channel_xfer_size) {
  47049. + DWC_INFO("%s: Periodic xfer length %d > " "max xfer length for channel %d\n",
  47050. + __func__, max_xfer_size, max_channel_xfer_size); //NOTICE
  47051. + status = -DWC_E_NO_SPACE;
  47052. + }
  47053. +
  47054. + return status;
  47055. +}
  47056. +
  47057. +
  47058. +
  47059. +/**
  47060. + * Schedules an interrupt or isochronous transfer in the periodic schedule.
  47061. + *
  47062. + * @param hcd The HCD state structure for the DWC OTG controller.
  47063. + * @param qh QH for the periodic transfer. The QH should already contain the
  47064. + * scheduling information.
  47065. + *
  47066. + * @return 0 if successful, negative error code otherwise.
  47067. + */
  47068. +static int schedule_periodic(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  47069. +{
  47070. + int status = 0;
  47071. +
  47072. + if (microframe_schedule) {
  47073. + int frame;
  47074. + status = find_uframe(hcd, qh);
  47075. + frame = -1;
  47076. + if (status == 0) {
  47077. + frame = 7;
  47078. + } else {
  47079. + if (status > 0 )
  47080. + frame = status-1;
  47081. + }
  47082. +
  47083. + /* Set the new frame up */
  47084. + if (frame > -1) {
  47085. + qh->sched_frame &= ~0x7;
  47086. + qh->sched_frame |= (frame & 7);
  47087. + }
  47088. +
  47089. + if (status != -1)
  47090. + status = 0;
  47091. + } else {
  47092. + status = periodic_channel_available(hcd);
  47093. + if (status) {
  47094. + DWC_INFO("%s: No host channel available for periodic " "transfer.\n", __func__); //NOTICE
  47095. + return status;
  47096. + }
  47097. +
  47098. + status = check_periodic_bandwidth(hcd, qh);
  47099. + }
  47100. + if (status) {
  47101. + DWC_INFO("%s: Insufficient periodic bandwidth for "
  47102. + "periodic transfer.\n", __func__);
  47103. + return -DWC_E_NO_SPACE;
  47104. + }
  47105. + status = check_max_xfer_size(hcd, qh);
  47106. + if (status) {
  47107. + DWC_INFO("%s: Channel max transfer size too small "
  47108. + "for periodic transfer.\n", __func__);
  47109. + return status;
  47110. + }
  47111. +
  47112. + if (hcd->core_if->dma_desc_enable) {
  47113. + /* Don't rely on SOF and start in ready schedule */
  47114. + DWC_LIST_INSERT_TAIL(&hcd->periodic_sched_ready, &qh->qh_list_entry);
  47115. + }
  47116. + else {
  47117. + if(fiq_enable && (DWC_LIST_EMPTY(&hcd->periodic_sched_inactive) || dwc_frame_num_le(qh->sched_frame, hcd->fiq_state->next_sched_frame)))
  47118. + {
  47119. + hcd->fiq_state->next_sched_frame = qh->sched_frame;
  47120. +
  47121. + }
  47122. + /* Always start in the inactive schedule. */
  47123. + DWC_LIST_INSERT_TAIL(&hcd->periodic_sched_inactive, &qh->qh_list_entry);
  47124. + }
  47125. +
  47126. + if (!microframe_schedule) {
  47127. + /* Reserve the periodic channel. */
  47128. + hcd->periodic_channels++;
  47129. + }
  47130. +
  47131. + /* Update claimed usecs per (micro)frame. */
  47132. + hcd->periodic_usecs += qh->usecs;
  47133. +
  47134. + return status;
  47135. +}
  47136. +
  47137. +
  47138. +/**
  47139. + * This function adds a QH to either the non periodic or periodic schedule if
  47140. + * it is not already in the schedule. If the QH is already in the schedule, no
  47141. + * action is taken.
  47142. + *
  47143. + * @return 0 if successful, negative error code otherwise.
  47144. + */
  47145. +int dwc_otg_hcd_qh_add(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  47146. +{
  47147. + int status = 0;
  47148. + gintmsk_data_t intr_mask = {.d32 = 0 };
  47149. +
  47150. + if (!DWC_LIST_EMPTY(&qh->qh_list_entry)) {
  47151. + /* QH already in a schedule. */
  47152. + return status;
  47153. + }
  47154. +
  47155. + /* Add the new QH to the appropriate schedule */
  47156. + if (dwc_qh_is_non_per(qh)) {
  47157. + /* Always start in the inactive schedule. */
  47158. + DWC_LIST_INSERT_TAIL(&hcd->non_periodic_sched_inactive,
  47159. + &qh->qh_list_entry);
  47160. + //hcd->fiq_state->kick_np_queues = 1;
  47161. + } else {
  47162. + /* If the QH wasn't in a schedule, then sched_frame is stale. */
  47163. + qh->sched_frame = dwc_frame_num_inc(dwc_otg_hcd_get_frame_number(hcd),
  47164. + max_t(uint32_t, qh->interval, SCHEDULE_SLOP));
  47165. + status = schedule_periodic(hcd, qh);
  47166. + qh->start_split_frame = qh->sched_frame;
  47167. + if ( !hcd->periodic_qh_count ) {
  47168. + intr_mask.b.sofintr = 1;
  47169. + if (fiq_enable) {
  47170. + local_fiq_disable();
  47171. + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
  47172. + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk, intr_mask.d32, intr_mask.d32);
  47173. + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
  47174. + local_fiq_enable();
  47175. + } else {
  47176. + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk, intr_mask.d32, intr_mask.d32);
  47177. + }
  47178. + }
  47179. + hcd->periodic_qh_count++;
  47180. + }
  47181. +
  47182. + return status;
  47183. +}
  47184. +
  47185. +/**
  47186. + * Removes an interrupt or isochronous transfer from the periodic schedule.
  47187. + *
  47188. + * @param hcd The HCD state structure for the DWC OTG controller.
  47189. + * @param qh QH for the periodic transfer.
  47190. + */
  47191. +static void deschedule_periodic(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  47192. +{
  47193. + int i;
  47194. + DWC_LIST_REMOVE_INIT(&qh->qh_list_entry);
  47195. +
  47196. + /* Update claimed usecs per (micro)frame. */
  47197. + hcd->periodic_usecs -= qh->usecs;
  47198. +
  47199. + if (!microframe_schedule) {
  47200. + /* Release the periodic channel reservation. */
  47201. + hcd->periodic_channels--;
  47202. + } else {
  47203. + for (i = 0; i < 8; i++) {
  47204. + hcd->frame_usecs[i] += qh->frame_usecs[i];
  47205. + qh->frame_usecs[i] = 0;
  47206. + }
  47207. + }
  47208. +}
  47209. +
  47210. +/**
  47211. + * Removes a QH from either the non-periodic or periodic schedule. Memory is
  47212. + * not freed.
  47213. + *
  47214. + * @param hcd The HCD state structure.
  47215. + * @param qh QH to remove from schedule. */
  47216. +void dwc_otg_hcd_qh_remove(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  47217. +{
  47218. + gintmsk_data_t intr_mask = {.d32 = 0 };
  47219. +
  47220. + if (DWC_LIST_EMPTY(&qh->qh_list_entry)) {
  47221. + /* QH is not in a schedule. */
  47222. + return;
  47223. + }
  47224. +
  47225. + if (dwc_qh_is_non_per(qh)) {
  47226. + if (hcd->non_periodic_qh_ptr == &qh->qh_list_entry) {
  47227. + hcd->non_periodic_qh_ptr =
  47228. + hcd->non_periodic_qh_ptr->next;
  47229. + }
  47230. + DWC_LIST_REMOVE_INIT(&qh->qh_list_entry);
  47231. + //if (!DWC_LIST_EMPTY(&hcd->non_periodic_sched_inactive))
  47232. + // hcd->fiq_state->kick_np_queues = 1;
  47233. + } else {
  47234. + deschedule_periodic(hcd, qh);
  47235. + hcd->periodic_qh_count--;
  47236. + if( !hcd->periodic_qh_count && !fiq_fsm_enable ) {
  47237. + intr_mask.b.sofintr = 1;
  47238. + if (fiq_enable) {
  47239. + local_fiq_disable();
  47240. + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
  47241. + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk, intr_mask.d32, 0);
  47242. + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
  47243. + local_fiq_enable();
  47244. + } else {
  47245. + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk, intr_mask.d32, 0);
  47246. + }
  47247. + }
  47248. + }
  47249. +}
  47250. +
  47251. +/**
  47252. + * Deactivates a QH. For non-periodic QHs, removes the QH from the active
  47253. + * non-periodic schedule. The QH is added to the inactive non-periodic
  47254. + * schedule if any QTDs are still attached to the QH.
  47255. + *
  47256. + * For periodic QHs, the QH is removed from the periodic queued schedule. If
  47257. + * there are any QTDs still attached to the QH, the QH is added to either the
  47258. + * periodic inactive schedule or the periodic ready schedule and its next
  47259. + * scheduled frame is calculated. The QH is placed in the ready schedule if
  47260. + * the scheduled frame has been reached already. Otherwise it's placed in the
  47261. + * inactive schedule. If there are no QTDs attached to the QH, the QH is
  47262. + * completely removed from the periodic schedule.
  47263. + */
  47264. +void dwc_otg_hcd_qh_deactivate(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
  47265. + int sched_next_periodic_split)
  47266. +{
  47267. + if (dwc_qh_is_non_per(qh)) {
  47268. + dwc_otg_hcd_qh_remove(hcd, qh);
  47269. + if (!DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
  47270. + /* Add back to inactive non-periodic schedule. */
  47271. + dwc_otg_hcd_qh_add(hcd, qh);
  47272. + //hcd->fiq_state->kick_np_queues = 1;
  47273. + } else {
  47274. + if(nak_holdoff && qh->do_split) {
  47275. + qh->nak_frame = 0xFFFF;
  47276. + }
  47277. + }
  47278. + } else {
  47279. + uint16_t frame_number = dwc_otg_hcd_get_frame_number(hcd);
  47280. +
  47281. + if (qh->do_split) {
  47282. + /* Schedule the next continuing periodic split transfer */
  47283. + if (sched_next_periodic_split) {
  47284. +
  47285. + qh->sched_frame = frame_number;
  47286. +
  47287. + if (dwc_frame_num_le(frame_number,
  47288. + dwc_frame_num_inc
  47289. + (qh->start_split_frame,
  47290. + 1))) {
  47291. + /*
  47292. + * Allow one frame to elapse after start
  47293. + * split microframe before scheduling
  47294. + * complete split, but DONT if we are
  47295. + * doing the next start split in the
  47296. + * same frame for an ISOC out.
  47297. + */
  47298. + if ((qh->ep_type != UE_ISOCHRONOUS) ||
  47299. + (qh->ep_is_in != 0)) {
  47300. + qh->sched_frame =
  47301. + dwc_frame_num_inc(qh->sched_frame, 1);
  47302. + }
  47303. + }
  47304. + } else {
  47305. + qh->sched_frame =
  47306. + dwc_frame_num_inc(qh->start_split_frame,
  47307. + qh->interval);
  47308. + if (dwc_frame_num_le
  47309. + (qh->sched_frame, frame_number)) {
  47310. + qh->sched_frame = frame_number;
  47311. + }
  47312. + qh->sched_frame |= 0x7;
  47313. + qh->start_split_frame = qh->sched_frame;
  47314. + }
  47315. + } else {
  47316. + qh->sched_frame =
  47317. + dwc_frame_num_inc(qh->sched_frame, qh->interval);
  47318. + if (dwc_frame_num_le(qh->sched_frame, frame_number)) {
  47319. + qh->sched_frame = frame_number;
  47320. + }
  47321. + }
  47322. +
  47323. + if (DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
  47324. + dwc_otg_hcd_qh_remove(hcd, qh);
  47325. + } else {
  47326. + /*
  47327. + * Remove from periodic_sched_queued and move to
  47328. + * appropriate queue.
  47329. + */
  47330. + if ((microframe_schedule && dwc_frame_num_le(qh->sched_frame, frame_number)) ||
  47331. + (!microframe_schedule && qh->sched_frame == frame_number)) {
  47332. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_ready,
  47333. + &qh->qh_list_entry);
  47334. + } else {
  47335. + if(fiq_enable && !dwc_frame_num_le(hcd->fiq_state->next_sched_frame, qh->sched_frame))
  47336. + {
  47337. + hcd->fiq_state->next_sched_frame = qh->sched_frame;
  47338. + }
  47339. +
  47340. + DWC_LIST_MOVE_HEAD
  47341. + (&hcd->periodic_sched_inactive,
  47342. + &qh->qh_list_entry);
  47343. + }
  47344. + }
  47345. + }
  47346. +}
  47347. +
  47348. +/**
  47349. + * This function allocates and initializes a QTD.
  47350. + *
  47351. + * @param urb The URB to create a QTD from. Each URB-QTD pair will end up
  47352. + * pointing to each other so each pair should have a unique correlation.
  47353. + * @param atomic_alloc Flag to do atomic alloc if needed
  47354. + *
  47355. + * @return Returns pointer to the newly allocated QTD, or NULL on error. */
  47356. +dwc_otg_qtd_t *dwc_otg_hcd_qtd_create(dwc_otg_hcd_urb_t * urb, int atomic_alloc)
  47357. +{
  47358. + dwc_otg_qtd_t *qtd;
  47359. +
  47360. + qtd = dwc_otg_hcd_qtd_alloc(atomic_alloc);
  47361. + if (qtd == NULL) {
  47362. + return NULL;
  47363. + }
  47364. +
  47365. + dwc_otg_hcd_qtd_init(qtd, urb);
  47366. + return qtd;
  47367. +}
  47368. +
  47369. +/**
  47370. + * Initializes a QTD structure.
  47371. + *
  47372. + * @param qtd The QTD to initialize.
  47373. + * @param urb The URB to use for initialization. */
  47374. +void dwc_otg_hcd_qtd_init(dwc_otg_qtd_t * qtd, dwc_otg_hcd_urb_t * urb)
  47375. +{
  47376. + dwc_memset(qtd, 0, sizeof(dwc_otg_qtd_t));
  47377. + qtd->urb = urb;
  47378. + if (dwc_otg_hcd_get_pipe_type(&urb->pipe_info) == UE_CONTROL) {
  47379. + /*
  47380. + * The only time the QTD data toggle is used is on the data
  47381. + * phase of control transfers. This phase always starts with
  47382. + * DATA1.
  47383. + */
  47384. + qtd->data_toggle = DWC_OTG_HC_PID_DATA1;
  47385. + qtd->control_phase = DWC_OTG_CONTROL_SETUP;
  47386. + }
  47387. +
  47388. + /* start split */
  47389. + qtd->complete_split = 0;
  47390. + qtd->isoc_split_pos = DWC_HCSPLIT_XACTPOS_ALL;
  47391. + qtd->isoc_split_offset = 0;
  47392. + qtd->in_process = 0;
  47393. +
  47394. + /* Store the qtd ptr in the urb to reference what QTD. */
  47395. + urb->qtd = qtd;
  47396. + return;
  47397. +}
  47398. +
  47399. +/**
  47400. + * This function adds a QTD to the QTD-list of a QH. It will find the correct
  47401. + * QH to place the QTD into. If it does not find a QH, then it will create a
  47402. + * new QH. If the QH to which the QTD is added is not currently scheduled, it
  47403. + * is placed into the proper schedule based on its EP type.
  47404. + * HCD lock must be held and interrupts must be disabled on entry
  47405. + *
  47406. + * @param[in] qtd The QTD to add
  47407. + * @param[in] hcd The DWC HCD structure
  47408. + * @param[out] qh out parameter to return queue head
  47409. + * @param atomic_alloc Flag to do atomic alloc if needed
  47410. + *
  47411. + * @return 0 if successful, negative error code otherwise.
  47412. + */
  47413. +int dwc_otg_hcd_qtd_add(dwc_otg_qtd_t * qtd,
  47414. + dwc_otg_hcd_t * hcd, dwc_otg_qh_t ** qh, int atomic_alloc)
  47415. +{
  47416. + int retval = 0;
  47417. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  47418. +
  47419. + /*
  47420. + * Get the QH which holds the QTD-list to insert to. Create QH if it
  47421. + * doesn't exist.
  47422. + */
  47423. + if (*qh == NULL) {
  47424. + *qh = dwc_otg_hcd_qh_create(hcd, urb, atomic_alloc);
  47425. + if (*qh == NULL) {
  47426. + retval = -DWC_E_NO_MEMORY;
  47427. + goto done;
  47428. + } else {
  47429. + if (fiq_enable)
  47430. + hcd->fiq_state->kick_np_queues = 1;
  47431. + }
  47432. + }
  47433. + retval = dwc_otg_hcd_qh_add(hcd, *qh);
  47434. + if (retval == 0) {
  47435. + DWC_CIRCLEQ_INSERT_TAIL(&((*qh)->qtd_list), qtd,
  47436. + qtd_list_entry);
  47437. + qtd->qh = *qh;
  47438. + }
  47439. +done:
  47440. +
  47441. + return retval;
  47442. +}
  47443. +
  47444. +#endif /* DWC_DEVICE_ONLY */
  47445. --- /dev/null
  47446. +++ b/drivers/usb/host/dwc_otg/dwc_otg_os_dep.h
  47447. @@ -0,0 +1,200 @@
  47448. +#ifndef _DWC_OS_DEP_H_
  47449. +#define _DWC_OS_DEP_H_
  47450. +
  47451. +/**
  47452. + * @file
  47453. + *
  47454. + * This file contains OS dependent structures.
  47455. + *
  47456. + */
  47457. +
  47458. +#include <linux/kernel.h>
  47459. +#include <linux/module.h>
  47460. +#include <linux/moduleparam.h>
  47461. +#include <linux/init.h>
  47462. +#include <linux/device.h>
  47463. +#include <linux/errno.h>
  47464. +#include <linux/types.h>
  47465. +#include <linux/slab.h>
  47466. +#include <linux/list.h>
  47467. +#include <linux/interrupt.h>
  47468. +#include <linux/ctype.h>
  47469. +#include <linux/string.h>
  47470. +#include <linux/dma-mapping.h>
  47471. +#include <linux/jiffies.h>
  47472. +#include <linux/delay.h>
  47473. +#include <linux/timer.h>
  47474. +#include <linux/workqueue.h>
  47475. +#include <linux/stat.h>
  47476. +#include <linux/pci.h>
  47477. +#include <linux/compiler.h>
  47478. +
  47479. +#include <linux/version.h>
  47480. +
  47481. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
  47482. +# include <linux/irq.h>
  47483. +#endif
  47484. +
  47485. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
  47486. +# include <linux/usb/ch9.h>
  47487. +#else
  47488. +# include <linux/usb_ch9.h>
  47489. +#endif
  47490. +
  47491. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
  47492. +# include <linux/usb/gadget.h>
  47493. +#else
  47494. +# include <linux/usb_gadget.h>
  47495. +#endif
  47496. +
  47497. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
  47498. +# include <asm/irq.h>
  47499. +#endif
  47500. +
  47501. +#ifdef PCI_INTERFACE
  47502. +# include <asm/io.h>
  47503. +#endif
  47504. +
  47505. +#ifdef LM_INTERFACE
  47506. +# include <asm/unaligned.h>
  47507. +# include <asm/sizes.h>
  47508. +# include <asm/param.h>
  47509. +# include <asm/io.h>
  47510. +# if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30))
  47511. +# include <asm/arch/hardware.h>
  47512. +# include <asm/arch/lm.h>
  47513. +# include <asm/arch/irqs.h>
  47514. +# include <asm/arch/regs-irq.h>
  47515. +# else
  47516. +/* in 2.6.31, at least, we seem to have lost the generic LM infrastructure -
  47517. + here we assume that the machine architecture provides definitions
  47518. + in its own header
  47519. +*/
  47520. +# include <mach/lm.h>
  47521. +# include <mach/hardware.h>
  47522. +# endif
  47523. +#endif
  47524. +
  47525. +#ifdef PLATFORM_INTERFACE
  47526. +#include <linux/platform_device.h>
  47527. +#ifdef CONFIG_ARM
  47528. +#include <asm/mach/map.h>
  47529. +#endif
  47530. +#endif
  47531. +
  47532. +/** The OS page size */
  47533. +#define DWC_OS_PAGE_SIZE PAGE_SIZE
  47534. +
  47535. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)
  47536. +typedef int gfp_t;
  47537. +#endif
  47538. +
  47539. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18)
  47540. +# define IRQF_SHARED SA_SHIRQ
  47541. +#endif
  47542. +
  47543. +typedef struct os_dependent {
  47544. + /** Base address returned from ioremap() */
  47545. + void *base;
  47546. +
  47547. + /** Register offset for Diagnostic API */
  47548. + uint32_t reg_offset;
  47549. +
  47550. + /** Base address for MPHI peripheral */
  47551. + void *mphi_base;
  47552. +
  47553. + /** mphi_base actually points to the SWIRQ block */
  47554. + bool use_swirq;
  47555. +
  47556. + /** IRQ number (<0 if not valid) */
  47557. + int irq_num;
  47558. +
  47559. + /** FIQ number (<0 if not valid) */
  47560. + int fiq_num;
  47561. +
  47562. +#ifdef LM_INTERFACE
  47563. + struct lm_device *lmdev;
  47564. +#elif defined(PCI_INTERFACE)
  47565. + struct pci_dev *pcidev;
  47566. +
  47567. + /** Start address of a PCI region */
  47568. + resource_size_t rsrc_start;
  47569. +
  47570. + /** Length address of a PCI region */
  47571. + resource_size_t rsrc_len;
  47572. +#elif defined(PLATFORM_INTERFACE)
  47573. + struct platform_device *platformdev;
  47574. +#endif
  47575. +
  47576. +} os_dependent_t;
  47577. +
  47578. +#ifdef __cplusplus
  47579. +}
  47580. +#endif
  47581. +
  47582. +
  47583. +
  47584. +/* Type for the our device on the chosen bus */
  47585. +#if defined(LM_INTERFACE)
  47586. +typedef struct lm_device dwc_bus_dev_t;
  47587. +#elif defined(PCI_INTERFACE)
  47588. +typedef struct pci_dev dwc_bus_dev_t;
  47589. +#elif defined(PLATFORM_INTERFACE)
  47590. +typedef struct platform_device dwc_bus_dev_t;
  47591. +#endif
  47592. +
  47593. +/* Helper macro to retrieve drvdata from the device on the chosen bus */
  47594. +#if defined(LM_INTERFACE)
  47595. +#define DWC_OTG_BUSDRVDATA(_dev) lm_get_drvdata(_dev)
  47596. +#elif defined(PCI_INTERFACE)
  47597. +#define DWC_OTG_BUSDRVDATA(_dev) pci_get_drvdata(_dev)
  47598. +#elif defined(PLATFORM_INTERFACE)
  47599. +#define DWC_OTG_BUSDRVDATA(_dev) platform_get_drvdata(_dev)
  47600. +#endif
  47601. +
  47602. +/**
  47603. + * Helper macro returning the otg_device structure of a given struct device
  47604. + *
  47605. + * c.f. static dwc_otg_device_t *dwc_otg_drvdev(struct device *_dev)
  47606. + */
  47607. +#ifdef LM_INTERFACE
  47608. +#define DWC_OTG_GETDRVDEV(_var, _dev) do { \
  47609. + struct lm_device *lm_dev = \
  47610. + container_of(_dev, struct lm_device, dev); \
  47611. + _var = lm_get_drvdata(lm_dev); \
  47612. + } while (0)
  47613. +
  47614. +#elif defined(PCI_INTERFACE)
  47615. +#define DWC_OTG_GETDRVDEV(_var, _dev) do { \
  47616. + _var = dev_get_drvdata(_dev); \
  47617. + } while (0)
  47618. +
  47619. +#elif defined(PLATFORM_INTERFACE)
  47620. +#define DWC_OTG_GETDRVDEV(_var, _dev) do { \
  47621. + struct platform_device *platform_dev = \
  47622. + container_of(_dev, struct platform_device, dev); \
  47623. + _var = platform_get_drvdata(platform_dev); \
  47624. + } while (0)
  47625. +#endif
  47626. +
  47627. +
  47628. +/**
  47629. + * Helper macro returning the struct dev of the given struct os_dependent
  47630. + *
  47631. + * c.f. static struct device *dwc_otg_getdev(struct os_dependent *osdep)
  47632. + */
  47633. +#ifdef LM_INTERFACE
  47634. +#define DWC_OTG_OS_GETDEV(_osdep) \
  47635. + ((_osdep).lmdev == NULL? NULL: &(_osdep).lmdev->dev)
  47636. +#elif defined(PCI_INTERFACE)
  47637. +#define DWC_OTG_OS_GETDEV(_osdep) \
  47638. + ((_osdep).pci_dev == NULL? NULL: &(_osdep).pci_dev->dev)
  47639. +#elif defined(PLATFORM_INTERFACE)
  47640. +#define DWC_OTG_OS_GETDEV(_osdep) \
  47641. + ((_osdep).platformdev == NULL? NULL: &(_osdep).platformdev->dev)
  47642. +#endif
  47643. +
  47644. +
  47645. +
  47646. +
  47647. +#endif /* _DWC_OS_DEP_H_ */
  47648. --- /dev/null
  47649. +++ b/drivers/usb/host/dwc_otg/dwc_otg_pcd.c
  47650. @@ -0,0 +1,2725 @@
  47651. +/* ==========================================================================
  47652. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd.c $
  47653. + * $Revision: #101 $
  47654. + * $Date: 2012/08/10 $
  47655. + * $Change: 2047372 $
  47656. + *
  47657. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  47658. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  47659. + * otherwise expressly agreed to in writing between Synopsys and you.
  47660. + *
  47661. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  47662. + * any End User Software License Agreement or Agreement for Licensed Product
  47663. + * with Synopsys or any supplement thereto. You are permitted to use and
  47664. + * redistribute this Software in source and binary forms, with or without
  47665. + * modification, provided that redistributions of source code must retain this
  47666. + * notice. You may not view, use, disclose, copy or distribute this file or
  47667. + * any information contained herein except pursuant to this license grant from
  47668. + * Synopsys. If you do not agree with this notice, including the disclaimer
  47669. + * below, then you are not authorized to use the Software.
  47670. + *
  47671. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  47672. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  47673. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  47674. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  47675. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  47676. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  47677. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  47678. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  47679. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  47680. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  47681. + * DAMAGE.
  47682. + * ========================================================================== */
  47683. +#ifndef DWC_HOST_ONLY
  47684. +
  47685. +/** @file
  47686. + * This file implements PCD Core. All code in this file is portable and doesn't
  47687. + * use any OS specific functions.
  47688. + * PCD Core provides Interface, defined in <code><dwc_otg_pcd_if.h></code>
  47689. + * header file, which can be used to implement OS specific PCD interface.
  47690. + *
  47691. + * An important function of the PCD is managing interrupts generated
  47692. + * by the DWC_otg controller. The implementation of the DWC_otg device
  47693. + * mode interrupt service routines is in dwc_otg_pcd_intr.c.
  47694. + *
  47695. + * @todo Add Device Mode test modes (Test J mode, Test K mode, etc).
  47696. + * @todo Does it work when the request size is greater than DEPTSIZ
  47697. + * transfer size
  47698. + *
  47699. + */
  47700. +
  47701. +#include "dwc_otg_pcd.h"
  47702. +
  47703. +#ifdef DWC_UTE_CFI
  47704. +#include "dwc_otg_cfi.h"
  47705. +
  47706. +extern int init_cfi(cfiobject_t * cfiobj);
  47707. +#endif
  47708. +
  47709. +/**
  47710. + * Choose endpoint from ep arrays using usb_ep structure.
  47711. + */
  47712. +static dwc_otg_pcd_ep_t *get_ep_from_handle(dwc_otg_pcd_t * pcd, void *handle)
  47713. +{
  47714. + int i;
  47715. + if (pcd->ep0.priv == handle) {
  47716. + return &pcd->ep0;
  47717. + }
  47718. + for (i = 0; i < MAX_EPS_CHANNELS - 1; i++) {
  47719. + if (pcd->in_ep[i].priv == handle)
  47720. + return &pcd->in_ep[i];
  47721. + if (pcd->out_ep[i].priv == handle)
  47722. + return &pcd->out_ep[i];
  47723. + }
  47724. +
  47725. + return NULL;
  47726. +}
  47727. +
  47728. +/**
  47729. + * This function completes a request. It call's the request call back.
  47730. + */
  47731. +void dwc_otg_request_done(dwc_otg_pcd_ep_t * ep, dwc_otg_pcd_request_t * req,
  47732. + int32_t status)
  47733. +{
  47734. + unsigned stopped = ep->stopped;
  47735. +
  47736. + DWC_DEBUGPL(DBG_PCDV, "%s(ep %p req %p)\n", __func__, ep, req);
  47737. + DWC_CIRCLEQ_REMOVE_INIT(&ep->queue, req, queue_entry);
  47738. +
  47739. + /* don't modify queue heads during completion callback */
  47740. + ep->stopped = 1;
  47741. + /* spin_unlock/spin_lock now done in fops->complete() */
  47742. + ep->pcd->fops->complete(ep->pcd, ep->priv, req->priv, status,
  47743. + req->actual);
  47744. +
  47745. + if (ep->pcd->request_pending > 0) {
  47746. + --ep->pcd->request_pending;
  47747. + }
  47748. +
  47749. + ep->stopped = stopped;
  47750. + DWC_FREE(req);
  47751. +}
  47752. +
  47753. +/**
  47754. + * This function terminates all the requsts in the EP request queue.
  47755. + */
  47756. +void dwc_otg_request_nuke(dwc_otg_pcd_ep_t * ep)
  47757. +{
  47758. + dwc_otg_pcd_request_t *req;
  47759. +
  47760. + ep->stopped = 1;
  47761. +
  47762. + /* called with irqs blocked?? */
  47763. + while (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  47764. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  47765. + dwc_otg_request_done(ep, req, -DWC_E_SHUTDOWN);
  47766. + }
  47767. +}
  47768. +
  47769. +void dwc_otg_pcd_start(dwc_otg_pcd_t * pcd,
  47770. + const struct dwc_otg_pcd_function_ops *fops)
  47771. +{
  47772. + pcd->fops = fops;
  47773. +}
  47774. +
  47775. +/**
  47776. + * PCD Callback function for initializing the PCD when switching to
  47777. + * device mode.
  47778. + *
  47779. + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
  47780. + */
  47781. +static int32_t dwc_otg_pcd_start_cb(void *p)
  47782. +{
  47783. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
  47784. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  47785. +
  47786. + /*
  47787. + * Initialized the Core for Device mode.
  47788. + */
  47789. + if (dwc_otg_is_device_mode(core_if)) {
  47790. + dwc_otg_core_dev_init(core_if);
  47791. + /* Set core_if's lock pointer to the pcd->lock */
  47792. + core_if->lock = pcd->lock;
  47793. + }
  47794. + return 1;
  47795. +}
  47796. +
  47797. +/** CFI-specific buffer allocation function for EP */
  47798. +#ifdef DWC_UTE_CFI
  47799. +uint8_t *cfiw_ep_alloc_buffer(dwc_otg_pcd_t * pcd, void *pep, dwc_dma_t * addr,
  47800. + size_t buflen, int flags)
  47801. +{
  47802. + dwc_otg_pcd_ep_t *ep;
  47803. + ep = get_ep_from_handle(pcd, pep);
  47804. + if (!ep) {
  47805. + DWC_WARN("bad ep\n");
  47806. + return -DWC_E_INVALID;
  47807. + }
  47808. +
  47809. + return pcd->cfi->ops.ep_alloc_buf(pcd->cfi, pcd, ep, addr, buflen,
  47810. + flags);
  47811. +}
  47812. +#else
  47813. +uint8_t *cfiw_ep_alloc_buffer(dwc_otg_pcd_t * pcd, void *pep, dwc_dma_t * addr,
  47814. + size_t buflen, int flags);
  47815. +#endif
  47816. +
  47817. +/**
  47818. + * PCD Callback function for notifying the PCD when resuming from
  47819. + * suspend.
  47820. + *
  47821. + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
  47822. + */
  47823. +static int32_t dwc_otg_pcd_resume_cb(void *p)
  47824. +{
  47825. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
  47826. +
  47827. + if (pcd->fops->resume) {
  47828. + pcd->fops->resume(pcd);
  47829. + }
  47830. +
  47831. + /* Stop the SRP timeout timer. */
  47832. + if ((GET_CORE_IF(pcd)->core_params->phy_type != DWC_PHY_TYPE_PARAM_FS)
  47833. + || (!GET_CORE_IF(pcd)->core_params->i2c_enable)) {
  47834. + if (GET_CORE_IF(pcd)->srp_timer_started) {
  47835. + GET_CORE_IF(pcd)->srp_timer_started = 0;
  47836. + DWC_TIMER_CANCEL(GET_CORE_IF(pcd)->srp_timer);
  47837. + }
  47838. + }
  47839. + return 1;
  47840. +}
  47841. +
  47842. +/**
  47843. + * PCD Callback function for notifying the PCD device is suspended.
  47844. + *
  47845. + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
  47846. + */
  47847. +static int32_t dwc_otg_pcd_suspend_cb(void *p)
  47848. +{
  47849. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
  47850. +
  47851. + if (pcd->fops->suspend) {
  47852. + DWC_SPINUNLOCK(pcd->lock);
  47853. + pcd->fops->suspend(pcd);
  47854. + DWC_SPINLOCK(pcd->lock);
  47855. + }
  47856. +
  47857. + return 1;
  47858. +}
  47859. +
  47860. +/**
  47861. + * PCD Callback function for stopping the PCD when switching to Host
  47862. + * mode.
  47863. + *
  47864. + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
  47865. + */
  47866. +static int32_t dwc_otg_pcd_stop_cb(void *p)
  47867. +{
  47868. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
  47869. + extern void dwc_otg_pcd_stop(dwc_otg_pcd_t * _pcd);
  47870. +
  47871. + dwc_otg_pcd_stop(pcd);
  47872. + return 1;
  47873. +}
  47874. +
  47875. +/**
  47876. + * PCD Callback structure for handling mode switching.
  47877. + */
  47878. +static dwc_otg_cil_callbacks_t pcd_callbacks = {
  47879. + .start = dwc_otg_pcd_start_cb,
  47880. + .stop = dwc_otg_pcd_stop_cb,
  47881. + .suspend = dwc_otg_pcd_suspend_cb,
  47882. + .resume_wakeup = dwc_otg_pcd_resume_cb,
  47883. + .p = 0, /* Set at registration */
  47884. +};
  47885. +
  47886. +/**
  47887. + * This function allocates a DMA Descriptor chain for the Endpoint
  47888. + * buffer to be used for a transfer to/from the specified endpoint.
  47889. + */
  47890. +dwc_otg_dev_dma_desc_t *dwc_otg_ep_alloc_desc_chain(struct device *dev,
  47891. + dwc_dma_t * dma_desc_addr,
  47892. + uint32_t count)
  47893. +{
  47894. + return DWC_DMA_ALLOC_ATOMIC(dev, count * sizeof(dwc_otg_dev_dma_desc_t),
  47895. + dma_desc_addr);
  47896. +}
  47897. +
  47898. +/**
  47899. + * This function frees a DMA Descriptor chain that was allocated by ep_alloc_desc.
  47900. + */
  47901. +void dwc_otg_ep_free_desc_chain(struct device *dev,
  47902. + dwc_otg_dev_dma_desc_t * desc_addr,
  47903. + uint32_t dma_desc_addr, uint32_t count)
  47904. +{
  47905. + DWC_DMA_FREE(dev, count * sizeof(dwc_otg_dev_dma_desc_t), desc_addr,
  47906. + dma_desc_addr);
  47907. +}
  47908. +
  47909. +#ifdef DWC_EN_ISOC
  47910. +
  47911. +/**
  47912. + * This function initializes a descriptor chain for Isochronous transfer
  47913. + *
  47914. + * @param core_if Programming view of DWC_otg controller.
  47915. + * @param dwc_ep The EP to start the transfer on.
  47916. + *
  47917. + */
  47918. +void dwc_otg_iso_ep_start_ddma_transfer(dwc_otg_core_if_t * core_if,
  47919. + dwc_ep_t * dwc_ep)
  47920. +{
  47921. +
  47922. + dsts_data_t dsts = {.d32 = 0 };
  47923. + depctl_data_t depctl = {.d32 = 0 };
  47924. + volatile uint32_t *addr;
  47925. + int i, j;
  47926. + uint32_t len;
  47927. +
  47928. + if (dwc_ep->is_in)
  47929. + dwc_ep->desc_cnt = dwc_ep->buf_proc_intrvl / dwc_ep->bInterval;
  47930. + else
  47931. + dwc_ep->desc_cnt =
  47932. + dwc_ep->buf_proc_intrvl * dwc_ep->pkt_per_frm /
  47933. + dwc_ep->bInterval;
  47934. +
  47935. + /** Allocate descriptors for double buffering */
  47936. + dwc_ep->iso_desc_addr =
  47937. + dwc_otg_ep_alloc_desc_chain(&dwc_ep->iso_dma_desc_addr,
  47938. + dwc_ep->desc_cnt * 2);
  47939. + if (dwc_ep->desc_addr) {
  47940. + DWC_WARN("%s, can't allocate DMA descriptor chain\n", __func__);
  47941. + return;
  47942. + }
  47943. +
  47944. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  47945. +
  47946. + /** ISO OUT EP */
  47947. + if (dwc_ep->is_in == 0) {
  47948. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  47949. + dwc_otg_dev_dma_desc_t *dma_desc = dwc_ep->iso_desc_addr;
  47950. + dma_addr_t dma_ad;
  47951. + uint32_t data_per_desc;
  47952. + dwc_otg_dev_out_ep_regs_t *out_regs =
  47953. + core_if->dev_if->out_ep_regs[dwc_ep->num];
  47954. + int offset;
  47955. +
  47956. + addr = &core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl;
  47957. + dma_ad = (dma_addr_t) DWC_READ_REG32(&(out_regs->doepdma));
  47958. +
  47959. + /** Buffer 0 descriptors setup */
  47960. + dma_ad = dwc_ep->dma_addr0;
  47961. +
  47962. + sts.b_iso_out.bs = BS_HOST_READY;
  47963. + sts.b_iso_out.rxsts = 0;
  47964. + sts.b_iso_out.l = 0;
  47965. + sts.b_iso_out.sp = 0;
  47966. + sts.b_iso_out.ioc = 0;
  47967. + sts.b_iso_out.pid = 0;
  47968. + sts.b_iso_out.framenum = 0;
  47969. +
  47970. + offset = 0;
  47971. + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
  47972. + i += dwc_ep->pkt_per_frm) {
  47973. +
  47974. + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
  47975. + uint32_t len = (j + 1) * dwc_ep->maxpacket;
  47976. + if (len > dwc_ep->data_per_frame)
  47977. + data_per_desc =
  47978. + dwc_ep->data_per_frame -
  47979. + j * dwc_ep->maxpacket;
  47980. + else
  47981. + data_per_desc = dwc_ep->maxpacket;
  47982. + len = data_per_desc % 4;
  47983. + if (len)
  47984. + data_per_desc += 4 - len;
  47985. +
  47986. + sts.b_iso_out.rxbytes = data_per_desc;
  47987. + dma_desc->buf = dma_ad;
  47988. + dma_desc->status.d32 = sts.d32;
  47989. +
  47990. + offset += data_per_desc;
  47991. + dma_desc++;
  47992. + dma_ad += data_per_desc;
  47993. + }
  47994. + }
  47995. +
  47996. + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
  47997. + uint32_t len = (j + 1) * dwc_ep->maxpacket;
  47998. + if (len > dwc_ep->data_per_frame)
  47999. + data_per_desc =
  48000. + dwc_ep->data_per_frame -
  48001. + j * dwc_ep->maxpacket;
  48002. + else
  48003. + data_per_desc = dwc_ep->maxpacket;
  48004. + len = data_per_desc % 4;
  48005. + if (len)
  48006. + data_per_desc += 4 - len;
  48007. + sts.b_iso_out.rxbytes = data_per_desc;
  48008. + dma_desc->buf = dma_ad;
  48009. + dma_desc->status.d32 = sts.d32;
  48010. +
  48011. + offset += data_per_desc;
  48012. + dma_desc++;
  48013. + dma_ad += data_per_desc;
  48014. + }
  48015. +
  48016. + sts.b_iso_out.ioc = 1;
  48017. + len = (j + 1) * dwc_ep->maxpacket;
  48018. + if (len > dwc_ep->data_per_frame)
  48019. + data_per_desc =
  48020. + dwc_ep->data_per_frame - j * dwc_ep->maxpacket;
  48021. + else
  48022. + data_per_desc = dwc_ep->maxpacket;
  48023. + len = data_per_desc % 4;
  48024. + if (len)
  48025. + data_per_desc += 4 - len;
  48026. + sts.b_iso_out.rxbytes = data_per_desc;
  48027. +
  48028. + dma_desc->buf = dma_ad;
  48029. + dma_desc->status.d32 = sts.d32;
  48030. + dma_desc++;
  48031. +
  48032. + /** Buffer 1 descriptors setup */
  48033. + sts.b_iso_out.ioc = 0;
  48034. + dma_ad = dwc_ep->dma_addr1;
  48035. +
  48036. + offset = 0;
  48037. + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
  48038. + i += dwc_ep->pkt_per_frm) {
  48039. + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
  48040. + uint32_t len = (j + 1) * dwc_ep->maxpacket;
  48041. + if (len > dwc_ep->data_per_frame)
  48042. + data_per_desc =
  48043. + dwc_ep->data_per_frame -
  48044. + j * dwc_ep->maxpacket;
  48045. + else
  48046. + data_per_desc = dwc_ep->maxpacket;
  48047. + len = data_per_desc % 4;
  48048. + if (len)
  48049. + data_per_desc += 4 - len;
  48050. +
  48051. + data_per_desc =
  48052. + sts.b_iso_out.rxbytes = data_per_desc;
  48053. + dma_desc->buf = dma_ad;
  48054. + dma_desc->status.d32 = sts.d32;
  48055. +
  48056. + offset += data_per_desc;
  48057. + dma_desc++;
  48058. + dma_ad += data_per_desc;
  48059. + }
  48060. + }
  48061. + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
  48062. + data_per_desc =
  48063. + ((j + 1) * dwc_ep->maxpacket >
  48064. + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
  48065. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  48066. + data_per_desc +=
  48067. + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
  48068. + sts.b_iso_out.rxbytes = data_per_desc;
  48069. + dma_desc->buf = dma_ad;
  48070. + dma_desc->status.d32 = sts.d32;
  48071. +
  48072. + offset += data_per_desc;
  48073. + dma_desc++;
  48074. + dma_ad += data_per_desc;
  48075. + }
  48076. +
  48077. + sts.b_iso_out.ioc = 1;
  48078. + sts.b_iso_out.l = 1;
  48079. + data_per_desc =
  48080. + ((j + 1) * dwc_ep->maxpacket >
  48081. + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
  48082. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  48083. + data_per_desc +=
  48084. + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
  48085. + sts.b_iso_out.rxbytes = data_per_desc;
  48086. +
  48087. + dma_desc->buf = dma_ad;
  48088. + dma_desc->status.d32 = sts.d32;
  48089. +
  48090. + dwc_ep->next_frame = 0;
  48091. +
  48092. + /** Write dma_ad into DOEPDMA register */
  48093. + DWC_WRITE_REG32(&(out_regs->doepdma),
  48094. + (uint32_t) dwc_ep->iso_dma_desc_addr);
  48095. +
  48096. + }
  48097. + /** ISO IN EP */
  48098. + else {
  48099. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  48100. + dwc_otg_dev_dma_desc_t *dma_desc = dwc_ep->iso_desc_addr;
  48101. + dma_addr_t dma_ad;
  48102. + dwc_otg_dev_in_ep_regs_t *in_regs =
  48103. + core_if->dev_if->in_ep_regs[dwc_ep->num];
  48104. + unsigned int frmnumber;
  48105. + fifosize_data_t txfifosize, rxfifosize;
  48106. +
  48107. + txfifosize.d32 =
  48108. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[dwc_ep->num]->
  48109. + dtxfsts);
  48110. + rxfifosize.d32 =
  48111. + DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
  48112. +
  48113. + addr = &core_if->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
  48114. +
  48115. + dma_ad = dwc_ep->dma_addr0;
  48116. +
  48117. + dsts.d32 =
  48118. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  48119. +
  48120. + sts.b_iso_in.bs = BS_HOST_READY;
  48121. + sts.b_iso_in.txsts = 0;
  48122. + sts.b_iso_in.sp =
  48123. + (dwc_ep->data_per_frame % dwc_ep->maxpacket) ? 1 : 0;
  48124. + sts.b_iso_in.ioc = 0;
  48125. + sts.b_iso_in.pid = dwc_ep->pkt_per_frm;
  48126. +
  48127. + frmnumber = dwc_ep->next_frame;
  48128. +
  48129. + sts.b_iso_in.framenum = frmnumber;
  48130. + sts.b_iso_in.txbytes = dwc_ep->data_per_frame;
  48131. + sts.b_iso_in.l = 0;
  48132. +
  48133. + /** Buffer 0 descriptors setup */
  48134. + for (i = 0; i < dwc_ep->desc_cnt - 1; i++) {
  48135. + dma_desc->buf = dma_ad;
  48136. + dma_desc->status.d32 = sts.d32;
  48137. + dma_desc++;
  48138. +
  48139. + dma_ad += dwc_ep->data_per_frame;
  48140. + sts.b_iso_in.framenum += dwc_ep->bInterval;
  48141. + }
  48142. +
  48143. + sts.b_iso_in.ioc = 1;
  48144. + dma_desc->buf = dma_ad;
  48145. + dma_desc->status.d32 = sts.d32;
  48146. + ++dma_desc;
  48147. +
  48148. + /** Buffer 1 descriptors setup */
  48149. + sts.b_iso_in.ioc = 0;
  48150. + dma_ad = dwc_ep->dma_addr1;
  48151. +
  48152. + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
  48153. + i += dwc_ep->pkt_per_frm) {
  48154. + dma_desc->buf = dma_ad;
  48155. + dma_desc->status.d32 = sts.d32;
  48156. + dma_desc++;
  48157. +
  48158. + dma_ad += dwc_ep->data_per_frame;
  48159. + sts.b_iso_in.framenum += dwc_ep->bInterval;
  48160. +
  48161. + sts.b_iso_in.ioc = 0;
  48162. + }
  48163. + sts.b_iso_in.ioc = 1;
  48164. + sts.b_iso_in.l = 1;
  48165. +
  48166. + dma_desc->buf = dma_ad;
  48167. + dma_desc->status.d32 = sts.d32;
  48168. +
  48169. + dwc_ep->next_frame = sts.b_iso_in.framenum + dwc_ep->bInterval;
  48170. +
  48171. + /** Write dma_ad into diepdma register */
  48172. + DWC_WRITE_REG32(&(in_regs->diepdma),
  48173. + (uint32_t) dwc_ep->iso_dma_desc_addr);
  48174. + }
  48175. + /** Enable endpoint, clear nak */
  48176. + depctl.d32 = 0;
  48177. + depctl.b.epena = 1;
  48178. + depctl.b.usbactep = 1;
  48179. + depctl.b.cnak = 1;
  48180. +
  48181. + DWC_MODIFY_REG32(addr, depctl.d32, depctl.d32);
  48182. + depctl.d32 = DWC_READ_REG32(addr);
  48183. +}
  48184. +
  48185. +/**
  48186. + * This function initializes a descriptor chain for Isochronous transfer
  48187. + *
  48188. + * @param core_if Programming view of DWC_otg controller.
  48189. + * @param ep The EP to start the transfer on.
  48190. + *
  48191. + */
  48192. +void dwc_otg_iso_ep_start_buf_transfer(dwc_otg_core_if_t * core_if,
  48193. + dwc_ep_t * ep)
  48194. +{
  48195. + depctl_data_t depctl = {.d32 = 0 };
  48196. + volatile uint32_t *addr;
  48197. +
  48198. + if (ep->is_in) {
  48199. + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
  48200. + } else {
  48201. + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
  48202. + }
  48203. +
  48204. + if (core_if->dma_enable == 0 || core_if->dma_desc_enable != 0) {
  48205. + return;
  48206. + } else {
  48207. + deptsiz_data_t deptsiz = {.d32 = 0 };
  48208. +
  48209. + ep->xfer_len =
  48210. + ep->data_per_frame * ep->buf_proc_intrvl / ep->bInterval;
  48211. + ep->pkt_cnt =
  48212. + (ep->xfer_len - 1 + ep->maxpacket) / ep->maxpacket;
  48213. + ep->xfer_count = 0;
  48214. + ep->xfer_buff =
  48215. + (ep->proc_buf_num) ? ep->xfer_buff1 : ep->xfer_buff0;
  48216. + ep->dma_addr =
  48217. + (ep->proc_buf_num) ? ep->dma_addr1 : ep->dma_addr0;
  48218. +
  48219. + if (ep->is_in) {
  48220. + /* Program the transfer size and packet count
  48221. + * as follows: xfersize = N * maxpacket +
  48222. + * short_packet pktcnt = N + (short_packet
  48223. + * exist ? 1 : 0)
  48224. + */
  48225. + deptsiz.b.mc = ep->pkt_per_frm;
  48226. + deptsiz.b.xfersize = ep->xfer_len;
  48227. + deptsiz.b.pktcnt =
  48228. + (ep->xfer_len - 1 + ep->maxpacket) / ep->maxpacket;
  48229. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  48230. + dieptsiz, deptsiz.d32);
  48231. +
  48232. + /* Write the DMA register */
  48233. + DWC_WRITE_REG32(&
  48234. + (core_if->dev_if->in_ep_regs[ep->num]->
  48235. + diepdma), (uint32_t) ep->dma_addr);
  48236. +
  48237. + } else {
  48238. + deptsiz.b.pktcnt =
  48239. + (ep->xfer_len + (ep->maxpacket - 1)) /
  48240. + ep->maxpacket;
  48241. + deptsiz.b.xfersize = deptsiz.b.pktcnt * ep->maxpacket;
  48242. +
  48243. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[ep->num]->
  48244. + doeptsiz, deptsiz.d32);
  48245. +
  48246. + /* Write the DMA register */
  48247. + DWC_WRITE_REG32(&
  48248. + (core_if->dev_if->out_ep_regs[ep->num]->
  48249. + doepdma), (uint32_t) ep->dma_addr);
  48250. +
  48251. + }
  48252. + /** Enable endpoint, clear nak */
  48253. + depctl.d32 = 0;
  48254. + depctl.b.epena = 1;
  48255. + depctl.b.cnak = 1;
  48256. +
  48257. + DWC_MODIFY_REG32(addr, depctl.d32, depctl.d32);
  48258. + }
  48259. +}
  48260. +
  48261. +/**
  48262. + * This function does the setup for a data transfer for an EP and
  48263. + * starts the transfer. For an IN transfer, the packets will be
  48264. + * loaded into the appropriate Tx FIFO in the ISR. For OUT transfers,
  48265. + * the packets are unloaded from the Rx FIFO in the ISR.
  48266. + *
  48267. + * @param core_if Programming view of DWC_otg controller.
  48268. + * @param ep The EP to start the transfer on.
  48269. + */
  48270. +
  48271. +static void dwc_otg_iso_ep_start_transfer(dwc_otg_core_if_t * core_if,
  48272. + dwc_ep_t * ep)
  48273. +{
  48274. + if (core_if->dma_enable) {
  48275. + if (core_if->dma_desc_enable) {
  48276. + if (ep->is_in) {
  48277. + ep->desc_cnt = ep->pkt_cnt / ep->pkt_per_frm;
  48278. + } else {
  48279. + ep->desc_cnt = ep->pkt_cnt;
  48280. + }
  48281. + dwc_otg_iso_ep_start_ddma_transfer(core_if, ep);
  48282. + } else {
  48283. + if (core_if->pti_enh_enable) {
  48284. + dwc_otg_iso_ep_start_buf_transfer(core_if, ep);
  48285. + } else {
  48286. + ep->cur_pkt_addr =
  48287. + (ep->proc_buf_num) ? ep->xfer_buff1 : ep->
  48288. + xfer_buff0;
  48289. + ep->cur_pkt_dma_addr =
  48290. + (ep->proc_buf_num) ? ep->dma_addr1 : ep->
  48291. + dma_addr0;
  48292. + dwc_otg_iso_ep_start_frm_transfer(core_if, ep);
  48293. + }
  48294. + }
  48295. + } else {
  48296. + ep->cur_pkt_addr =
  48297. + (ep->proc_buf_num) ? ep->xfer_buff1 : ep->xfer_buff0;
  48298. + ep->cur_pkt_dma_addr =
  48299. + (ep->proc_buf_num) ? ep->dma_addr1 : ep->dma_addr0;
  48300. + dwc_otg_iso_ep_start_frm_transfer(core_if, ep);
  48301. + }
  48302. +}
  48303. +
  48304. +/**
  48305. + * This function stops transfer for an EP and
  48306. + * resets the ep's variables.
  48307. + *
  48308. + * @param core_if Programming view of DWC_otg controller.
  48309. + * @param ep The EP to start the transfer on.
  48310. + */
  48311. +
  48312. +void dwc_otg_iso_ep_stop_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  48313. +{
  48314. + depctl_data_t depctl = {.d32 = 0 };
  48315. + volatile uint32_t *addr;
  48316. +
  48317. + if (ep->is_in == 1) {
  48318. + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
  48319. + } else {
  48320. + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
  48321. + }
  48322. +
  48323. + /* disable the ep */
  48324. + depctl.d32 = DWC_READ_REG32(addr);
  48325. +
  48326. + depctl.b.epdis = 1;
  48327. + depctl.b.snak = 1;
  48328. +
  48329. + DWC_WRITE_REG32(addr, depctl.d32);
  48330. +
  48331. + if (core_if->dma_desc_enable &&
  48332. + ep->iso_desc_addr && ep->iso_dma_desc_addr) {
  48333. + dwc_otg_ep_free_desc_chain(ep->iso_desc_addr,
  48334. + ep->iso_dma_desc_addr,
  48335. + ep->desc_cnt * 2);
  48336. + }
  48337. +
  48338. + /* reset varibales */
  48339. + ep->dma_addr0 = 0;
  48340. + ep->dma_addr1 = 0;
  48341. + ep->xfer_buff0 = 0;
  48342. + ep->xfer_buff1 = 0;
  48343. + ep->data_per_frame = 0;
  48344. + ep->data_pattern_frame = 0;
  48345. + ep->sync_frame = 0;
  48346. + ep->buf_proc_intrvl = 0;
  48347. + ep->bInterval = 0;
  48348. + ep->proc_buf_num = 0;
  48349. + ep->pkt_per_frm = 0;
  48350. + ep->pkt_per_frm = 0;
  48351. + ep->desc_cnt = 0;
  48352. + ep->iso_desc_addr = 0;
  48353. + ep->iso_dma_desc_addr = 0;
  48354. +}
  48355. +
  48356. +int dwc_otg_pcd_iso_ep_start(dwc_otg_pcd_t * pcd, void *ep_handle,
  48357. + uint8_t * buf0, uint8_t * buf1, dwc_dma_t dma0,
  48358. + dwc_dma_t dma1, int sync_frame, int dp_frame,
  48359. + int data_per_frame, int start_frame,
  48360. + int buf_proc_intrvl, void *req_handle,
  48361. + int atomic_alloc)
  48362. +{
  48363. + dwc_otg_pcd_ep_t *ep;
  48364. + dwc_irqflags_t flags = 0;
  48365. + dwc_ep_t *dwc_ep;
  48366. + int32_t frm_data;
  48367. + dsts_data_t dsts;
  48368. + dwc_otg_core_if_t *core_if;
  48369. +
  48370. + ep = get_ep_from_handle(pcd, ep_handle);
  48371. +
  48372. + if (!ep || !ep->desc || ep->dwc_ep.num == 0) {
  48373. + DWC_WARN("bad ep\n");
  48374. + return -DWC_E_INVALID;
  48375. + }
  48376. +
  48377. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  48378. + core_if = GET_CORE_IF(pcd);
  48379. + dwc_ep = &ep->dwc_ep;
  48380. +
  48381. + if (ep->iso_req_handle) {
  48382. + DWC_WARN("ISO request in progress\n");
  48383. + }
  48384. +
  48385. + dwc_ep->dma_addr0 = dma0;
  48386. + dwc_ep->dma_addr1 = dma1;
  48387. +
  48388. + dwc_ep->xfer_buff0 = buf0;
  48389. + dwc_ep->xfer_buff1 = buf1;
  48390. +
  48391. + dwc_ep->data_per_frame = data_per_frame;
  48392. +
  48393. + /** @todo - pattern data support is to be implemented in the future */
  48394. + dwc_ep->data_pattern_frame = dp_frame;
  48395. + dwc_ep->sync_frame = sync_frame;
  48396. +
  48397. + dwc_ep->buf_proc_intrvl = buf_proc_intrvl;
  48398. +
  48399. + dwc_ep->bInterval = 1 << (ep->desc->bInterval - 1);
  48400. +
  48401. + dwc_ep->proc_buf_num = 0;
  48402. +
  48403. + dwc_ep->pkt_per_frm = 0;
  48404. + frm_data = ep->dwc_ep.data_per_frame;
  48405. + while (frm_data > 0) {
  48406. + dwc_ep->pkt_per_frm++;
  48407. + frm_data -= ep->dwc_ep.maxpacket;
  48408. + }
  48409. +
  48410. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  48411. +
  48412. + if (start_frame == -1) {
  48413. + dwc_ep->next_frame = dsts.b.soffn + 1;
  48414. + if (dwc_ep->bInterval != 1) {
  48415. + dwc_ep->next_frame =
  48416. + dwc_ep->next_frame + (dwc_ep->bInterval - 1 -
  48417. + dwc_ep->next_frame %
  48418. + dwc_ep->bInterval);
  48419. + }
  48420. + } else {
  48421. + dwc_ep->next_frame = start_frame;
  48422. + }
  48423. +
  48424. + if (!core_if->pti_enh_enable) {
  48425. + dwc_ep->pkt_cnt =
  48426. + dwc_ep->buf_proc_intrvl * dwc_ep->pkt_per_frm /
  48427. + dwc_ep->bInterval;
  48428. + } else {
  48429. + dwc_ep->pkt_cnt =
  48430. + (dwc_ep->data_per_frame *
  48431. + (dwc_ep->buf_proc_intrvl / dwc_ep->bInterval)
  48432. + - 1 + dwc_ep->maxpacket) / dwc_ep->maxpacket;
  48433. + }
  48434. +
  48435. + if (core_if->dma_desc_enable) {
  48436. + dwc_ep->desc_cnt =
  48437. + dwc_ep->buf_proc_intrvl * dwc_ep->pkt_per_frm /
  48438. + dwc_ep->bInterval;
  48439. + }
  48440. +
  48441. + if (atomic_alloc) {
  48442. + dwc_ep->pkt_info =
  48443. + DWC_ALLOC_ATOMIC(sizeof(iso_pkt_info_t) * dwc_ep->pkt_cnt);
  48444. + } else {
  48445. + dwc_ep->pkt_info =
  48446. + DWC_ALLOC(sizeof(iso_pkt_info_t) * dwc_ep->pkt_cnt);
  48447. + }
  48448. + if (!dwc_ep->pkt_info) {
  48449. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  48450. + return -DWC_E_NO_MEMORY;
  48451. + }
  48452. + if (core_if->pti_enh_enable) {
  48453. + dwc_memset(dwc_ep->pkt_info, 0,
  48454. + sizeof(iso_pkt_info_t) * dwc_ep->pkt_cnt);
  48455. + }
  48456. +
  48457. + dwc_ep->cur_pkt = 0;
  48458. + ep->iso_req_handle = req_handle;
  48459. +
  48460. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  48461. + dwc_otg_iso_ep_start_transfer(core_if, dwc_ep);
  48462. + return 0;
  48463. +}
  48464. +
  48465. +int dwc_otg_pcd_iso_ep_stop(dwc_otg_pcd_t * pcd, void *ep_handle,
  48466. + void *req_handle)
  48467. +{
  48468. + dwc_irqflags_t flags = 0;
  48469. + dwc_otg_pcd_ep_t *ep;
  48470. + dwc_ep_t *dwc_ep;
  48471. +
  48472. + ep = get_ep_from_handle(pcd, ep_handle);
  48473. + if (!ep || !ep->desc || ep->dwc_ep.num == 0) {
  48474. + DWC_WARN("bad ep\n");
  48475. + return -DWC_E_INVALID;
  48476. + }
  48477. + dwc_ep = &ep->dwc_ep;
  48478. +
  48479. + dwc_otg_iso_ep_stop_transfer(GET_CORE_IF(pcd), dwc_ep);
  48480. +
  48481. + DWC_FREE(dwc_ep->pkt_info);
  48482. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  48483. + if (ep->iso_req_handle != req_handle) {
  48484. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  48485. + return -DWC_E_INVALID;
  48486. + }
  48487. +
  48488. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  48489. +
  48490. + ep->iso_req_handle = 0;
  48491. + return 0;
  48492. +}
  48493. +
  48494. +/**
  48495. + * This function is used for perodical data exchnage between PCD and gadget drivers.
  48496. + * for Isochronous EPs
  48497. + *
  48498. + * - Every time a sync period completes this function is called to
  48499. + * perform data exchange between PCD and gadget
  48500. + */
  48501. +void dwc_otg_iso_buffer_done(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep,
  48502. + void *req_handle)
  48503. +{
  48504. + int i;
  48505. + dwc_ep_t *dwc_ep;
  48506. +
  48507. + dwc_ep = &ep->dwc_ep;
  48508. +
  48509. + DWC_SPINUNLOCK(ep->pcd->lock);
  48510. + pcd->fops->isoc_complete(pcd, ep->priv, ep->iso_req_handle,
  48511. + dwc_ep->proc_buf_num ^ 0x1);
  48512. + DWC_SPINLOCK(ep->pcd->lock);
  48513. +
  48514. + for (i = 0; i < dwc_ep->pkt_cnt; ++i) {
  48515. + dwc_ep->pkt_info[i].status = 0;
  48516. + dwc_ep->pkt_info[i].offset = 0;
  48517. + dwc_ep->pkt_info[i].length = 0;
  48518. + }
  48519. +}
  48520. +
  48521. +int dwc_otg_pcd_get_iso_packet_count(dwc_otg_pcd_t * pcd, void *ep_handle,
  48522. + void *iso_req_handle)
  48523. +{
  48524. + dwc_otg_pcd_ep_t *ep;
  48525. + dwc_ep_t *dwc_ep;
  48526. +
  48527. + ep = get_ep_from_handle(pcd, ep_handle);
  48528. + if (!ep->desc || ep->dwc_ep.num == 0) {
  48529. + DWC_WARN("bad ep\n");
  48530. + return -DWC_E_INVALID;
  48531. + }
  48532. + dwc_ep = &ep->dwc_ep;
  48533. +
  48534. + return dwc_ep->pkt_cnt;
  48535. +}
  48536. +
  48537. +void dwc_otg_pcd_get_iso_packet_params(dwc_otg_pcd_t * pcd, void *ep_handle,
  48538. + void *iso_req_handle, int packet,
  48539. + int *status, int *actual, int *offset)
  48540. +{
  48541. + dwc_otg_pcd_ep_t *ep;
  48542. + dwc_ep_t *dwc_ep;
  48543. +
  48544. + ep = get_ep_from_handle(pcd, ep_handle);
  48545. + if (!ep)
  48546. + DWC_WARN("bad ep\n");
  48547. +
  48548. + dwc_ep = &ep->dwc_ep;
  48549. +
  48550. + *status = dwc_ep->pkt_info[packet].status;
  48551. + *actual = dwc_ep->pkt_info[packet].length;
  48552. + *offset = dwc_ep->pkt_info[packet].offset;
  48553. +}
  48554. +
  48555. +#endif /* DWC_EN_ISOC */
  48556. +
  48557. +static void dwc_otg_pcd_init_ep(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * pcd_ep,
  48558. + uint32_t is_in, uint32_t ep_num)
  48559. +{
  48560. + /* Init EP structure */
  48561. + pcd_ep->desc = 0;
  48562. + pcd_ep->pcd = pcd;
  48563. + pcd_ep->stopped = 1;
  48564. + pcd_ep->queue_sof = 0;
  48565. +
  48566. + /* Init DWC ep structure */
  48567. + pcd_ep->dwc_ep.is_in = is_in;
  48568. + pcd_ep->dwc_ep.num = ep_num;
  48569. + pcd_ep->dwc_ep.active = 0;
  48570. + pcd_ep->dwc_ep.tx_fifo_num = 0;
  48571. + /* Control until ep is actvated */
  48572. + pcd_ep->dwc_ep.type = DWC_OTG_EP_TYPE_CONTROL;
  48573. + pcd_ep->dwc_ep.maxpacket = MAX_PACKET_SIZE;
  48574. + pcd_ep->dwc_ep.dma_addr = 0;
  48575. + pcd_ep->dwc_ep.start_xfer_buff = 0;
  48576. + pcd_ep->dwc_ep.xfer_buff = 0;
  48577. + pcd_ep->dwc_ep.xfer_len = 0;
  48578. + pcd_ep->dwc_ep.xfer_count = 0;
  48579. + pcd_ep->dwc_ep.sent_zlp = 0;
  48580. + pcd_ep->dwc_ep.total_len = 0;
  48581. + pcd_ep->dwc_ep.desc_addr = 0;
  48582. + pcd_ep->dwc_ep.dma_desc_addr = 0;
  48583. + DWC_CIRCLEQ_INIT(&pcd_ep->queue);
  48584. +}
  48585. +
  48586. +/**
  48587. + * Initialize ep's
  48588. + */
  48589. +static void dwc_otg_pcd_reinit(dwc_otg_pcd_t * pcd)
  48590. +{
  48591. + int i;
  48592. + uint32_t hwcfg1;
  48593. + dwc_otg_pcd_ep_t *ep;
  48594. + int in_ep_cntr, out_ep_cntr;
  48595. + uint32_t num_in_eps = (GET_CORE_IF(pcd))->dev_if->num_in_eps;
  48596. + uint32_t num_out_eps = (GET_CORE_IF(pcd))->dev_if->num_out_eps;
  48597. +
  48598. + /**
  48599. + * Initialize the EP0 structure.
  48600. + */
  48601. + ep = &pcd->ep0;
  48602. + dwc_otg_pcd_init_ep(pcd, ep, 0, 0);
  48603. +
  48604. + in_ep_cntr = 0;
  48605. + hwcfg1 = (GET_CORE_IF(pcd))->hwcfg1.d32 >> 3;
  48606. + for (i = 1; in_ep_cntr < num_in_eps; i++) {
  48607. + if ((hwcfg1 & 0x1) == 0) {
  48608. + dwc_otg_pcd_ep_t *ep = &pcd->in_ep[in_ep_cntr];
  48609. + in_ep_cntr++;
  48610. + /**
  48611. + * @todo NGS: Add direction to EP, based on contents
  48612. + * of HWCFG1. Need a copy of HWCFG1 in pcd structure?
  48613. + * sprintf(";r
  48614. + */
  48615. + dwc_otg_pcd_init_ep(pcd, ep, 1 /* IN */ , i);
  48616. +
  48617. + DWC_CIRCLEQ_INIT(&ep->queue);
  48618. + }
  48619. + hwcfg1 >>= 2;
  48620. + }
  48621. +
  48622. + out_ep_cntr = 0;
  48623. + hwcfg1 = (GET_CORE_IF(pcd))->hwcfg1.d32 >> 2;
  48624. + for (i = 1; out_ep_cntr < num_out_eps; i++) {
  48625. + if ((hwcfg1 & 0x1) == 0) {
  48626. + dwc_otg_pcd_ep_t *ep = &pcd->out_ep[out_ep_cntr];
  48627. + out_ep_cntr++;
  48628. + /**
  48629. + * @todo NGS: Add direction to EP, based on contents
  48630. + * of HWCFG1. Need a copy of HWCFG1 in pcd structure?
  48631. + * sprintf(";r
  48632. + */
  48633. + dwc_otg_pcd_init_ep(pcd, ep, 0 /* OUT */ , i);
  48634. + DWC_CIRCLEQ_INIT(&ep->queue);
  48635. + }
  48636. + hwcfg1 >>= 2;
  48637. + }
  48638. +
  48639. + pcd->ep0state = EP0_DISCONNECT;
  48640. + pcd->ep0.dwc_ep.maxpacket = MAX_EP0_SIZE;
  48641. + pcd->ep0.dwc_ep.type = DWC_OTG_EP_TYPE_CONTROL;
  48642. +}
  48643. +
  48644. +/**
  48645. + * This function is called when the SRP timer expires. The SRP should
  48646. + * complete within 6 seconds.
  48647. + */
  48648. +static void srp_timeout(void *ptr)
  48649. +{
  48650. + gotgctl_data_t gotgctl;
  48651. + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
  48652. + volatile uint32_t *addr = &core_if->core_global_regs->gotgctl;
  48653. +
  48654. + gotgctl.d32 = DWC_READ_REG32(addr);
  48655. +
  48656. + core_if->srp_timer_started = 0;
  48657. +
  48658. + if (core_if->adp_enable) {
  48659. + if (gotgctl.b.bsesvld == 0) {
  48660. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  48661. + DWC_PRINTF("SRP Timeout BSESSVLD = 0\n");
  48662. + /* Power off the core */
  48663. + if (core_if->power_down == 2) {
  48664. + gpwrdn.b.pwrdnswtch = 1;
  48665. + DWC_MODIFY_REG32(&core_if->
  48666. + core_global_regs->gpwrdn,
  48667. + gpwrdn.d32, 0);
  48668. + }
  48669. +
  48670. + gpwrdn.d32 = 0;
  48671. + gpwrdn.b.pmuintsel = 1;
  48672. + gpwrdn.b.pmuactv = 1;
  48673. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
  48674. + gpwrdn.d32);
  48675. + dwc_otg_adp_probe_start(core_if);
  48676. + } else {
  48677. + DWC_PRINTF("SRP Timeout BSESSVLD = 1\n");
  48678. + core_if->op_state = B_PERIPHERAL;
  48679. + dwc_otg_core_init(core_if);
  48680. + dwc_otg_enable_global_interrupts(core_if);
  48681. + cil_pcd_start(core_if);
  48682. + }
  48683. + }
  48684. +
  48685. + if ((core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS) &&
  48686. + (core_if->core_params->i2c_enable)) {
  48687. + DWC_PRINTF("SRP Timeout\n");
  48688. +
  48689. + if ((core_if->srp_success) && (gotgctl.b.bsesvld)) {
  48690. + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
  48691. + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
  48692. + }
  48693. +
  48694. + /* Clear Session Request */
  48695. + gotgctl.d32 = 0;
  48696. + gotgctl.b.sesreq = 1;
  48697. + DWC_MODIFY_REG32(&core_if->core_global_regs->gotgctl,
  48698. + gotgctl.d32, 0);
  48699. +
  48700. + core_if->srp_success = 0;
  48701. + } else {
  48702. + __DWC_ERROR("Device not connected/responding\n");
  48703. + gotgctl.b.sesreq = 0;
  48704. + DWC_WRITE_REG32(addr, gotgctl.d32);
  48705. + }
  48706. + } else if (gotgctl.b.sesreq) {
  48707. + DWC_PRINTF("SRP Timeout\n");
  48708. +
  48709. + __DWC_ERROR("Device not connected/responding\n");
  48710. + gotgctl.b.sesreq = 0;
  48711. + DWC_WRITE_REG32(addr, gotgctl.d32);
  48712. + } else {
  48713. + DWC_PRINTF(" SRP GOTGCTL=%0x\n", gotgctl.d32);
  48714. + }
  48715. +}
  48716. +
  48717. +/**
  48718. + * Tasklet
  48719. + *
  48720. + */
  48721. +extern void start_next_request(dwc_otg_pcd_ep_t * ep);
  48722. +
  48723. +static void start_xfer_tasklet_func(void *data)
  48724. +{
  48725. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) data;
  48726. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  48727. +
  48728. + int i;
  48729. + depctl_data_t diepctl;
  48730. +
  48731. + DWC_DEBUGPL(DBG_PCDV, "Start xfer tasklet\n");
  48732. +
  48733. + diepctl.d32 = DWC_READ_REG32(&core_if->dev_if->in_ep_regs[0]->diepctl);
  48734. +
  48735. + if (pcd->ep0.queue_sof) {
  48736. + pcd->ep0.queue_sof = 0;
  48737. + start_next_request(&pcd->ep0);
  48738. + // break;
  48739. + }
  48740. +
  48741. + for (i = 0; i < core_if->dev_if->num_in_eps; i++) {
  48742. + depctl_data_t diepctl;
  48743. + diepctl.d32 =
  48744. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->diepctl);
  48745. +
  48746. + if (pcd->in_ep[i].queue_sof) {
  48747. + pcd->in_ep[i].queue_sof = 0;
  48748. + start_next_request(&pcd->in_ep[i]);
  48749. + // break;
  48750. + }
  48751. + }
  48752. +
  48753. + return;
  48754. +}
  48755. +
  48756. +/**
  48757. + * This function initialized the PCD portion of the driver.
  48758. + *
  48759. + */
  48760. +dwc_otg_pcd_t *dwc_otg_pcd_init(dwc_otg_device_t *otg_dev)
  48761. +{
  48762. + struct device *dev = &otg_dev->os_dep.platformdev->dev;
  48763. + dwc_otg_core_if_t *core_if = otg_dev->core_if;
  48764. + dwc_otg_pcd_t *pcd = NULL;
  48765. + dwc_otg_dev_if_t *dev_if;
  48766. + int i;
  48767. +
  48768. + /*
  48769. + * Allocate PCD structure
  48770. + */
  48771. + pcd = DWC_ALLOC(sizeof(dwc_otg_pcd_t));
  48772. +
  48773. + if (pcd == NULL) {
  48774. + return NULL;
  48775. + }
  48776. +
  48777. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_SPINLOCK))
  48778. + DWC_SPINLOCK_ALLOC_LINUX_DEBUG(pcd->lock);
  48779. +#else
  48780. + pcd->lock = DWC_SPINLOCK_ALLOC();
  48781. +#endif
  48782. + DWC_DEBUGPL(DBG_HCDV, "Init of PCD %p given core_if %p\n",
  48783. + pcd, core_if);//GRAYG
  48784. + if (!pcd->lock) {
  48785. + DWC_ERROR("Could not allocate lock for pcd");
  48786. + DWC_FREE(pcd);
  48787. + return NULL;
  48788. + }
  48789. + /* Set core_if's lock pointer to hcd->lock */
  48790. + core_if->lock = pcd->lock;
  48791. + pcd->core_if = core_if;
  48792. +
  48793. + dev_if = core_if->dev_if;
  48794. + dev_if->isoc_ep = NULL;
  48795. +
  48796. + if (core_if->hwcfg4.b.ded_fifo_en) {
  48797. + DWC_PRINTF("Dedicated Tx FIFOs mode\n");
  48798. + } else {
  48799. + DWC_PRINTF("Shared Tx FIFO mode\n");
  48800. + }
  48801. +
  48802. + /*
  48803. + * Initialized the Core for Device mode here if there is nod ADP support.
  48804. + * Otherwise it will be done later in dwc_otg_adp_start routine.
  48805. + */
  48806. + if (dwc_otg_is_device_mode(core_if) /*&& !core_if->adp_enable*/) {
  48807. + dwc_otg_core_dev_init(core_if);
  48808. + }
  48809. +
  48810. + /*
  48811. + * Register the PCD Callbacks.
  48812. + */
  48813. + dwc_otg_cil_register_pcd_callbacks(core_if, &pcd_callbacks, pcd);
  48814. +
  48815. + /*
  48816. + * Initialize the DMA buffer for SETUP packets
  48817. + */
  48818. + if (GET_CORE_IF(pcd)->dma_enable) {
  48819. + pcd->setup_pkt =
  48820. + DWC_DMA_ALLOC(dev, sizeof(*pcd->setup_pkt) * 5,
  48821. + &pcd->setup_pkt_dma_handle);
  48822. + if (pcd->setup_pkt == NULL) {
  48823. + DWC_FREE(pcd);
  48824. + return NULL;
  48825. + }
  48826. +
  48827. + pcd->status_buf =
  48828. + DWC_DMA_ALLOC(dev, sizeof(uint16_t),
  48829. + &pcd->status_buf_dma_handle);
  48830. + if (pcd->status_buf == NULL) {
  48831. + DWC_DMA_FREE(dev, sizeof(*pcd->setup_pkt) * 5,
  48832. + pcd->setup_pkt, pcd->setup_pkt_dma_handle);
  48833. + DWC_FREE(pcd);
  48834. + return NULL;
  48835. + }
  48836. +
  48837. + if (GET_CORE_IF(pcd)->dma_desc_enable) {
  48838. + dev_if->setup_desc_addr[0] =
  48839. + dwc_otg_ep_alloc_desc_chain(dev,
  48840. + &dev_if->dma_setup_desc_addr[0], 1);
  48841. + dev_if->setup_desc_addr[1] =
  48842. + dwc_otg_ep_alloc_desc_chain(dev,
  48843. + &dev_if->dma_setup_desc_addr[1], 1);
  48844. + dev_if->in_desc_addr =
  48845. + dwc_otg_ep_alloc_desc_chain(dev,
  48846. + &dev_if->dma_in_desc_addr, 1);
  48847. + dev_if->out_desc_addr =
  48848. + dwc_otg_ep_alloc_desc_chain(dev,
  48849. + &dev_if->dma_out_desc_addr, 1);
  48850. + pcd->data_terminated = 0;
  48851. +
  48852. + if (dev_if->setup_desc_addr[0] == 0
  48853. + || dev_if->setup_desc_addr[1] == 0
  48854. + || dev_if->in_desc_addr == 0
  48855. + || dev_if->out_desc_addr == 0) {
  48856. +
  48857. + if (dev_if->out_desc_addr)
  48858. + dwc_otg_ep_free_desc_chain(dev,
  48859. + dev_if->out_desc_addr,
  48860. + dev_if->dma_out_desc_addr, 1);
  48861. + if (dev_if->in_desc_addr)
  48862. + dwc_otg_ep_free_desc_chain(dev,
  48863. + dev_if->in_desc_addr,
  48864. + dev_if->dma_in_desc_addr, 1);
  48865. + if (dev_if->setup_desc_addr[1])
  48866. + dwc_otg_ep_free_desc_chain(dev,
  48867. + dev_if->setup_desc_addr[1],
  48868. + dev_if->dma_setup_desc_addr[1], 1);
  48869. + if (dev_if->setup_desc_addr[0])
  48870. + dwc_otg_ep_free_desc_chain(dev,
  48871. + dev_if->setup_desc_addr[0],
  48872. + dev_if->dma_setup_desc_addr[0], 1);
  48873. +
  48874. + DWC_DMA_FREE(dev, sizeof(*pcd->setup_pkt) * 5,
  48875. + pcd->setup_pkt,
  48876. + pcd->setup_pkt_dma_handle);
  48877. + DWC_DMA_FREE(dev, sizeof(*pcd->status_buf),
  48878. + pcd->status_buf,
  48879. + pcd->status_buf_dma_handle);
  48880. +
  48881. + DWC_FREE(pcd);
  48882. +
  48883. + return NULL;
  48884. + }
  48885. + }
  48886. + } else {
  48887. + pcd->setup_pkt = DWC_ALLOC(sizeof(*pcd->setup_pkt) * 5);
  48888. + if (pcd->setup_pkt == NULL) {
  48889. + DWC_FREE(pcd);
  48890. + return NULL;
  48891. + }
  48892. +
  48893. + pcd->status_buf = DWC_ALLOC(sizeof(uint16_t));
  48894. + if (pcd->status_buf == NULL) {
  48895. + DWC_FREE(pcd->setup_pkt);
  48896. + DWC_FREE(pcd);
  48897. + return NULL;
  48898. + }
  48899. + }
  48900. +
  48901. + dwc_otg_pcd_reinit(pcd);
  48902. +
  48903. + /* Allocate the cfi object for the PCD */
  48904. +#ifdef DWC_UTE_CFI
  48905. + pcd->cfi = DWC_ALLOC(sizeof(cfiobject_t));
  48906. + if (NULL == pcd->cfi)
  48907. + goto fail;
  48908. + if (init_cfi(pcd->cfi)) {
  48909. + CFI_INFO("%s: Failed to init the CFI object\n", __func__);
  48910. + goto fail;
  48911. + }
  48912. +#endif
  48913. +
  48914. + /* Initialize tasklets */
  48915. + pcd->start_xfer_tasklet = DWC_TASK_ALLOC("xfer_tasklet",
  48916. + start_xfer_tasklet_func, pcd);
  48917. + pcd->test_mode_tasklet = DWC_TASK_ALLOC("test_mode_tasklet",
  48918. + do_test_mode, pcd);
  48919. +
  48920. + /* Initialize SRP timer */
  48921. + core_if->srp_timer = DWC_TIMER_ALLOC("SRP TIMER", srp_timeout, core_if);
  48922. +
  48923. + if (core_if->core_params->dev_out_nak) {
  48924. + /**
  48925. + * Initialize xfer timeout timer. Implemented for
  48926. + * 2.93a feature "Device DDMA OUT NAK Enhancement"
  48927. + */
  48928. + for(i = 0; i < MAX_EPS_CHANNELS; i++) {
  48929. + pcd->core_if->ep_xfer_timer[i] =
  48930. + DWC_TIMER_ALLOC("ep timer", ep_xfer_timeout,
  48931. + &pcd->core_if->ep_xfer_info[i]);
  48932. + }
  48933. + }
  48934. +
  48935. + return pcd;
  48936. +#ifdef DWC_UTE_CFI
  48937. +fail:
  48938. +#endif
  48939. + if (pcd->setup_pkt)
  48940. + DWC_FREE(pcd->setup_pkt);
  48941. + if (pcd->status_buf)
  48942. + DWC_FREE(pcd->status_buf);
  48943. +#ifdef DWC_UTE_CFI
  48944. + if (pcd->cfi)
  48945. + DWC_FREE(pcd->cfi);
  48946. +#endif
  48947. + if (pcd)
  48948. + DWC_FREE(pcd);
  48949. + return NULL;
  48950. +
  48951. +}
  48952. +
  48953. +/**
  48954. + * Remove PCD specific data
  48955. + */
  48956. +void dwc_otg_pcd_remove(dwc_otg_pcd_t * pcd)
  48957. +{
  48958. + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
  48959. + struct device *dev = dwc_otg_pcd_to_dev(pcd);
  48960. + int i;
  48961. +
  48962. + if (pcd->core_if->core_params->dev_out_nak) {
  48963. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  48964. + DWC_TIMER_CANCEL(pcd->core_if->ep_xfer_timer[i]);
  48965. + pcd->core_if->ep_xfer_info[i].state = 0;
  48966. + }
  48967. + }
  48968. +
  48969. + if (GET_CORE_IF(pcd)->dma_enable) {
  48970. + DWC_DMA_FREE(dev, sizeof(*pcd->setup_pkt) * 5, pcd->setup_pkt,
  48971. + pcd->setup_pkt_dma_handle);
  48972. + DWC_DMA_FREE(dev, sizeof(uint16_t), pcd->status_buf,
  48973. + pcd->status_buf_dma_handle);
  48974. + if (GET_CORE_IF(pcd)->dma_desc_enable) {
  48975. + dwc_otg_ep_free_desc_chain(dev,
  48976. + dev_if->setup_desc_addr[0],
  48977. + dev_if->dma_setup_desc_addr
  48978. + [0], 1);
  48979. + dwc_otg_ep_free_desc_chain(dev,
  48980. + dev_if->setup_desc_addr[1],
  48981. + dev_if->dma_setup_desc_addr
  48982. + [1], 1);
  48983. + dwc_otg_ep_free_desc_chain(dev,
  48984. + dev_if->in_desc_addr,
  48985. + dev_if->dma_in_desc_addr, 1);
  48986. + dwc_otg_ep_free_desc_chain(dev,
  48987. + dev_if->out_desc_addr,
  48988. + dev_if->dma_out_desc_addr,
  48989. + 1);
  48990. + }
  48991. + } else {
  48992. + DWC_FREE(pcd->setup_pkt);
  48993. + DWC_FREE(pcd->status_buf);
  48994. + }
  48995. + DWC_SPINLOCK_FREE(pcd->lock);
  48996. + /* Set core_if's lock pointer to NULL */
  48997. + pcd->core_if->lock = NULL;
  48998. +
  48999. + DWC_TASK_FREE(pcd->start_xfer_tasklet);
  49000. + DWC_TASK_FREE(pcd->test_mode_tasklet);
  49001. + if (pcd->core_if->core_params->dev_out_nak) {
  49002. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  49003. + if (pcd->core_if->ep_xfer_timer[i]) {
  49004. + DWC_TIMER_FREE(pcd->core_if->ep_xfer_timer[i]);
  49005. + }
  49006. + }
  49007. + }
  49008. +
  49009. +/* Release the CFI object's dynamic memory */
  49010. +#ifdef DWC_UTE_CFI
  49011. + if (pcd->cfi->ops.release) {
  49012. + pcd->cfi->ops.release(pcd->cfi);
  49013. + }
  49014. +#endif
  49015. +
  49016. + DWC_FREE(pcd);
  49017. +}
  49018. +
  49019. +/**
  49020. + * Returns whether registered pcd is dual speed or not
  49021. + */
  49022. +uint32_t dwc_otg_pcd_is_dualspeed(dwc_otg_pcd_t * pcd)
  49023. +{
  49024. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  49025. +
  49026. + if ((core_if->core_params->speed == DWC_SPEED_PARAM_FULL) ||
  49027. + ((core_if->hwcfg2.b.hs_phy_type == 2) &&
  49028. + (core_if->hwcfg2.b.fs_phy_type == 1) &&
  49029. + (core_if->core_params->ulpi_fs_ls))) {
  49030. + return 0;
  49031. + }
  49032. +
  49033. + return 1;
  49034. +}
  49035. +
  49036. +/**
  49037. + * Returns whether registered pcd is OTG capable or not
  49038. + */
  49039. +uint32_t dwc_otg_pcd_is_otg(dwc_otg_pcd_t * pcd)
  49040. +{
  49041. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  49042. + gusbcfg_data_t usbcfg = {.d32 = 0 };
  49043. +
  49044. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  49045. + if (!usbcfg.b.srpcap || !usbcfg.b.hnpcap) {
  49046. + return 0;
  49047. + }
  49048. +
  49049. + return 1;
  49050. +}
  49051. +
  49052. +/**
  49053. + * This function assigns periodic Tx FIFO to an periodic EP
  49054. + * in shared Tx FIFO mode
  49055. + */
  49056. +static uint32_t assign_tx_fifo(dwc_otg_core_if_t * core_if)
  49057. +{
  49058. + uint32_t TxMsk = 1;
  49059. + int i;
  49060. +
  49061. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; ++i) {
  49062. + if ((TxMsk & core_if->tx_msk) == 0) {
  49063. + core_if->tx_msk |= TxMsk;
  49064. + return i + 1;
  49065. + }
  49066. + TxMsk <<= 1;
  49067. + }
  49068. + return 0;
  49069. +}
  49070. +
  49071. +/**
  49072. + * This function assigns periodic Tx FIFO to an periodic EP
  49073. + * in shared Tx FIFO mode
  49074. + */
  49075. +static uint32_t assign_perio_tx_fifo(dwc_otg_core_if_t * core_if)
  49076. +{
  49077. + uint32_t PerTxMsk = 1;
  49078. + int i;
  49079. + for (i = 0; i < core_if->hwcfg4.b.num_dev_perio_in_ep; ++i) {
  49080. + if ((PerTxMsk & core_if->p_tx_msk) == 0) {
  49081. + core_if->p_tx_msk |= PerTxMsk;
  49082. + return i + 1;
  49083. + }
  49084. + PerTxMsk <<= 1;
  49085. + }
  49086. + return 0;
  49087. +}
  49088. +
  49089. +/**
  49090. + * This function releases periodic Tx FIFO
  49091. + * in shared Tx FIFO mode
  49092. + */
  49093. +static void release_perio_tx_fifo(dwc_otg_core_if_t * core_if,
  49094. + uint32_t fifo_num)
  49095. +{
  49096. + core_if->p_tx_msk =
  49097. + (core_if->p_tx_msk & (1 << (fifo_num - 1))) ^ core_if->p_tx_msk;
  49098. +}
  49099. +
  49100. +/**
  49101. + * This function releases periodic Tx FIFO
  49102. + * in shared Tx FIFO mode
  49103. + */
  49104. +static void release_tx_fifo(dwc_otg_core_if_t * core_if, uint32_t fifo_num)
  49105. +{
  49106. + core_if->tx_msk =
  49107. + (core_if->tx_msk & (1 << (fifo_num - 1))) ^ core_if->tx_msk;
  49108. +}
  49109. +
  49110. +/**
  49111. + * This function is being called from gadget
  49112. + * to enable PCD endpoint.
  49113. + */
  49114. +int dwc_otg_pcd_ep_enable(dwc_otg_pcd_t * pcd,
  49115. + const uint8_t * ep_desc, void *usb_ep)
  49116. +{
  49117. + int num, dir;
  49118. + dwc_otg_pcd_ep_t *ep = NULL;
  49119. + const usb_endpoint_descriptor_t *desc;
  49120. + dwc_irqflags_t flags;
  49121. + fifosize_data_t dptxfsiz = {.d32 = 0 };
  49122. + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
  49123. + gdfifocfg_data_t gdfifocfgbase = {.d32 = 0 };
  49124. + int retval = 0;
  49125. + int i, epcount;
  49126. + struct device *dev = dwc_otg_pcd_to_dev(pcd);
  49127. +
  49128. + desc = (const usb_endpoint_descriptor_t *)ep_desc;
  49129. +
  49130. + if (!desc) {
  49131. + pcd->ep0.priv = usb_ep;
  49132. + ep = &pcd->ep0;
  49133. + retval = -DWC_E_INVALID;
  49134. + goto out;
  49135. + }
  49136. +
  49137. + num = UE_GET_ADDR(desc->bEndpointAddress);
  49138. + dir = UE_GET_DIR(desc->bEndpointAddress);
  49139. +
  49140. + if (!UGETW(desc->wMaxPacketSize)) {
  49141. + DWC_WARN("bad maxpacketsize\n");
  49142. + retval = -DWC_E_INVALID;
  49143. + goto out;
  49144. + }
  49145. +
  49146. + if (dir == UE_DIR_IN) {
  49147. + epcount = pcd->core_if->dev_if->num_in_eps;
  49148. + for (i = 0; i < epcount; i++) {
  49149. + if (num == pcd->in_ep[i].dwc_ep.num) {
  49150. + ep = &pcd->in_ep[i];
  49151. + break;
  49152. + }
  49153. + }
  49154. + } else {
  49155. + epcount = pcd->core_if->dev_if->num_out_eps;
  49156. + for (i = 0; i < epcount; i++) {
  49157. + if (num == pcd->out_ep[i].dwc_ep.num) {
  49158. + ep = &pcd->out_ep[i];
  49159. + break;
  49160. + }
  49161. + }
  49162. + }
  49163. +
  49164. + if (!ep) {
  49165. + DWC_WARN("bad address\n");
  49166. + retval = -DWC_E_INVALID;
  49167. + goto out;
  49168. + }
  49169. +
  49170. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  49171. +
  49172. + ep->desc = desc;
  49173. + ep->priv = usb_ep;
  49174. +
  49175. + /*
  49176. + * Activate the EP
  49177. + */
  49178. + ep->stopped = 0;
  49179. +
  49180. + ep->dwc_ep.is_in = (dir == UE_DIR_IN);
  49181. + ep->dwc_ep.maxpacket = UGETW(desc->wMaxPacketSize);
  49182. +
  49183. + ep->dwc_ep.type = desc->bmAttributes & UE_XFERTYPE;
  49184. +
  49185. + if (ep->dwc_ep.is_in) {
  49186. + if (!GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
  49187. + ep->dwc_ep.tx_fifo_num = 0;
  49188. +
  49189. + if (ep->dwc_ep.type == UE_ISOCHRONOUS) {
  49190. + /*
  49191. + * if ISOC EP then assign a Periodic Tx FIFO.
  49192. + */
  49193. + ep->dwc_ep.tx_fifo_num =
  49194. + assign_perio_tx_fifo(GET_CORE_IF(pcd));
  49195. + }
  49196. + } else {
  49197. + /*
  49198. + * if Dedicated FIFOs mode is on then assign a Tx FIFO.
  49199. + */
  49200. + ep->dwc_ep.tx_fifo_num =
  49201. + assign_tx_fifo(GET_CORE_IF(pcd));
  49202. + }
  49203. +
  49204. + /* Calculating EP info controller base address */
  49205. + if (ep->dwc_ep.tx_fifo_num
  49206. + && GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
  49207. + gdfifocfg.d32 =
  49208. + DWC_READ_REG32(&GET_CORE_IF(pcd)->
  49209. + core_global_regs->gdfifocfg);
  49210. + gdfifocfgbase.d32 = gdfifocfg.d32 >> 16;
  49211. + dptxfsiz.d32 =
  49212. + (DWC_READ_REG32
  49213. + (&GET_CORE_IF(pcd)->core_global_regs->
  49214. + dtxfsiz[ep->dwc_ep.tx_fifo_num - 1]) >> 16);
  49215. + gdfifocfg.b.epinfobase =
  49216. + gdfifocfgbase.d32 + dptxfsiz.d32;
  49217. + if (GET_CORE_IF(pcd)->snpsid <= OTG_CORE_REV_2_94a) {
  49218. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->
  49219. + core_global_regs->gdfifocfg,
  49220. + gdfifocfg.d32);
  49221. + }
  49222. + }
  49223. + }
  49224. + /* Set initial data PID. */
  49225. + if (ep->dwc_ep.type == UE_BULK) {
  49226. + ep->dwc_ep.data_pid_start = 0;
  49227. + }
  49228. +
  49229. + /* Alloc DMA Descriptors */
  49230. + if (GET_CORE_IF(pcd)->dma_desc_enable) {
  49231. +#ifndef DWC_UTE_PER_IO
  49232. + if (ep->dwc_ep.type != UE_ISOCHRONOUS) {
  49233. +#endif
  49234. + ep->dwc_ep.desc_addr =
  49235. + dwc_otg_ep_alloc_desc_chain(dev,
  49236. + &ep->dwc_ep.dma_desc_addr,
  49237. + MAX_DMA_DESC_CNT);
  49238. + if (!ep->dwc_ep.desc_addr) {
  49239. + DWC_WARN("%s, can't allocate DMA descriptor\n",
  49240. + __func__);
  49241. + retval = -DWC_E_SHUTDOWN;
  49242. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  49243. + goto out;
  49244. + }
  49245. +#ifndef DWC_UTE_PER_IO
  49246. + }
  49247. +#endif
  49248. + }
  49249. +
  49250. + DWC_DEBUGPL(DBG_PCD, "Activate %s: type=%d, mps=%d desc=%p\n",
  49251. + (ep->dwc_ep.is_in ? "IN" : "OUT"),
  49252. + ep->dwc_ep.type, ep->dwc_ep.maxpacket, ep->desc);
  49253. +#ifdef DWC_UTE_PER_IO
  49254. + ep->dwc_ep.xiso_bInterval = 1 << (ep->desc->bInterval - 1);
  49255. +#endif
  49256. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  49257. + ep->dwc_ep.bInterval = 1 << (ep->desc->bInterval - 1);
  49258. + ep->dwc_ep.frame_num = 0xFFFFFFFF;
  49259. + }
  49260. +
  49261. + dwc_otg_ep_activate(GET_CORE_IF(pcd), &ep->dwc_ep);
  49262. +
  49263. +#ifdef DWC_UTE_CFI
  49264. + if (pcd->cfi->ops.ep_enable) {
  49265. + pcd->cfi->ops.ep_enable(pcd->cfi, pcd, ep);
  49266. + }
  49267. +#endif
  49268. +
  49269. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  49270. +
  49271. +out:
  49272. + return retval;
  49273. +}
  49274. +
  49275. +/**
  49276. + * This function is being called from gadget
  49277. + * to disable PCD endpoint.
  49278. + */
  49279. +int dwc_otg_pcd_ep_disable(dwc_otg_pcd_t * pcd, void *ep_handle)
  49280. +{
  49281. + dwc_otg_pcd_ep_t *ep;
  49282. + dwc_irqflags_t flags;
  49283. + dwc_otg_dev_dma_desc_t *desc_addr;
  49284. + dwc_dma_t dma_desc_addr;
  49285. + gdfifocfg_data_t gdfifocfgbase = {.d32 = 0 };
  49286. + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
  49287. + fifosize_data_t dptxfsiz = {.d32 = 0 };
  49288. + struct device *dev = dwc_otg_pcd_to_dev(pcd);
  49289. +
  49290. + ep = get_ep_from_handle(pcd, ep_handle);
  49291. +
  49292. + if (!ep || !ep->desc) {
  49293. + DWC_DEBUGPL(DBG_PCD, "bad ep address\n");
  49294. + return -DWC_E_INVALID;
  49295. + }
  49296. +
  49297. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  49298. +
  49299. + dwc_otg_request_nuke(ep);
  49300. +
  49301. + dwc_otg_ep_deactivate(GET_CORE_IF(pcd), &ep->dwc_ep);
  49302. + if (pcd->core_if->core_params->dev_out_nak) {
  49303. + DWC_TIMER_CANCEL(pcd->core_if->ep_xfer_timer[ep->dwc_ep.num]);
  49304. + pcd->core_if->ep_xfer_info[ep->dwc_ep.num].state = 0;
  49305. + }
  49306. + ep->desc = NULL;
  49307. + ep->stopped = 1;
  49308. +
  49309. + gdfifocfg.d32 =
  49310. + DWC_READ_REG32(&GET_CORE_IF(pcd)->core_global_regs->gdfifocfg);
  49311. + gdfifocfgbase.d32 = gdfifocfg.d32 >> 16;
  49312. +
  49313. + if (ep->dwc_ep.is_in) {
  49314. + if (GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
  49315. + /* Flush the Tx FIFO */
  49316. + dwc_otg_flush_tx_fifo(GET_CORE_IF(pcd),
  49317. + ep->dwc_ep.tx_fifo_num);
  49318. + }
  49319. + release_perio_tx_fifo(GET_CORE_IF(pcd), ep->dwc_ep.tx_fifo_num);
  49320. + release_tx_fifo(GET_CORE_IF(pcd), ep->dwc_ep.tx_fifo_num);
  49321. + if (GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
  49322. + /* Decreasing EPinfo Base Addr */
  49323. + dptxfsiz.d32 =
  49324. + (DWC_READ_REG32
  49325. + (&GET_CORE_IF(pcd)->
  49326. + core_global_regs->dtxfsiz[ep->dwc_ep.tx_fifo_num-1]) >> 16);
  49327. + gdfifocfg.b.epinfobase = gdfifocfgbase.d32 - dptxfsiz.d32;
  49328. + if (GET_CORE_IF(pcd)->snpsid <= OTG_CORE_REV_2_94a) {
  49329. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gdfifocfg,
  49330. + gdfifocfg.d32);
  49331. + }
  49332. + }
  49333. + }
  49334. +
  49335. + /* Free DMA Descriptors */
  49336. + if (GET_CORE_IF(pcd)->dma_desc_enable) {
  49337. + if (ep->dwc_ep.type != UE_ISOCHRONOUS) {
  49338. + desc_addr = ep->dwc_ep.desc_addr;
  49339. + dma_desc_addr = ep->dwc_ep.dma_desc_addr;
  49340. +
  49341. + /* Cannot call dma_free_coherent() with IRQs disabled */
  49342. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  49343. + dwc_otg_ep_free_desc_chain(dev, desc_addr, dma_desc_addr,
  49344. + MAX_DMA_DESC_CNT);
  49345. +
  49346. + goto out_unlocked;
  49347. + }
  49348. + }
  49349. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  49350. +
  49351. +out_unlocked:
  49352. + DWC_DEBUGPL(DBG_PCD, "%d %s disabled\n", ep->dwc_ep.num,
  49353. + ep->dwc_ep.is_in ? "IN" : "OUT");
  49354. + return 0;
  49355. +
  49356. +}
  49357. +
  49358. +/******************************************************************************/
  49359. +#ifdef DWC_UTE_PER_IO
  49360. +
  49361. +/**
  49362. + * Free the request and its extended parts
  49363. + *
  49364. + */
  49365. +void dwc_pcd_xiso_ereq_free(dwc_otg_pcd_ep_t * ep, dwc_otg_pcd_request_t * req)
  49366. +{
  49367. + DWC_FREE(req->ext_req.per_io_frame_descs);
  49368. + DWC_FREE(req);
  49369. +}
  49370. +
  49371. +/**
  49372. + * Start the next request in the endpoint's queue.
  49373. + *
  49374. + */
  49375. +int dwc_otg_pcd_xiso_start_next_request(dwc_otg_pcd_t * pcd,
  49376. + dwc_otg_pcd_ep_t * ep)
  49377. +{
  49378. + int i;
  49379. + dwc_otg_pcd_request_t *req = NULL;
  49380. + dwc_ep_t *dwcep = NULL;
  49381. + struct dwc_iso_xreq_port *ereq = NULL;
  49382. + struct dwc_iso_pkt_desc_port *ddesc_iso;
  49383. + uint16_t nat;
  49384. + depctl_data_t diepctl;
  49385. +
  49386. + dwcep = &ep->dwc_ep;
  49387. +
  49388. + if (dwcep->xiso_active_xfers > 0) {
  49389. +#if 0 //Disable this to decrease s/w overhead that is crucial for Isoc transfers
  49390. + DWC_WARN("There are currently active transfers for EP%d \
  49391. + (active=%d; queued=%d)", dwcep->num, dwcep->xiso_active_xfers,
  49392. + dwcep->xiso_queued_xfers);
  49393. +#endif
  49394. + return 0;
  49395. + }
  49396. +
  49397. + nat = UGETW(ep->desc->wMaxPacketSize);
  49398. + nat = (nat >> 11) & 0x03;
  49399. +
  49400. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  49401. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  49402. + ereq = &req->ext_req;
  49403. + ep->stopped = 0;
  49404. +
  49405. + /* Get the frame number */
  49406. + dwcep->xiso_frame_num =
  49407. + dwc_otg_get_frame_number(GET_CORE_IF(pcd));
  49408. + DWC_DEBUG("FRM_NUM=%d", dwcep->xiso_frame_num);
  49409. +
  49410. + ddesc_iso = ereq->per_io_frame_descs;
  49411. +
  49412. + if (dwcep->is_in) {
  49413. + /* Setup DMA Descriptor chain for IN Isoc request */
  49414. + for (i = 0; i < ereq->pio_pkt_count; i++) {
  49415. + //if ((i % (nat + 1)) == 0)
  49416. + if ( i > 0 )
  49417. + dwcep->xiso_frame_num =
  49418. + (dwcep->xiso_bInterval +
  49419. + dwcep->xiso_frame_num) & 0x3FFF;
  49420. + dwcep->desc_addr[i].buf =
  49421. + req->dma + ddesc_iso[i].offset;
  49422. + dwcep->desc_addr[i].status.b_iso_in.txbytes =
  49423. + ddesc_iso[i].length;
  49424. + dwcep->desc_addr[i].status.b_iso_in.framenum =
  49425. + dwcep->xiso_frame_num;
  49426. + dwcep->desc_addr[i].status.b_iso_in.bs =
  49427. + BS_HOST_READY;
  49428. + dwcep->desc_addr[i].status.b_iso_in.txsts = 0;
  49429. + dwcep->desc_addr[i].status.b_iso_in.sp =
  49430. + (ddesc_iso[i].length %
  49431. + dwcep->maxpacket) ? 1 : 0;
  49432. + dwcep->desc_addr[i].status.b_iso_in.ioc = 0;
  49433. + dwcep->desc_addr[i].status.b_iso_in.pid = nat + 1;
  49434. + dwcep->desc_addr[i].status.b_iso_in.l = 0;
  49435. +
  49436. + /* Process the last descriptor */
  49437. + if (i == ereq->pio_pkt_count - 1) {
  49438. + dwcep->desc_addr[i].status.b_iso_in.ioc = 1;
  49439. + dwcep->desc_addr[i].status.b_iso_in.l = 1;
  49440. + }
  49441. + }
  49442. +
  49443. + /* Setup and start the transfer for this endpoint */
  49444. + dwcep->xiso_active_xfers++;
  49445. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->dev_if->
  49446. + in_ep_regs[dwcep->num]->diepdma,
  49447. + dwcep->dma_desc_addr);
  49448. + diepctl.d32 = 0;
  49449. + diepctl.b.epena = 1;
  49450. + diepctl.b.cnak = 1;
  49451. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->dev_if->
  49452. + in_ep_regs[dwcep->num]->diepctl, 0,
  49453. + diepctl.d32);
  49454. + } else {
  49455. + /* Setup DMA Descriptor chain for OUT Isoc request */
  49456. + for (i = 0; i < ereq->pio_pkt_count; i++) {
  49457. + //if ((i % (nat + 1)) == 0)
  49458. + dwcep->xiso_frame_num = (dwcep->xiso_bInterval +
  49459. + dwcep->xiso_frame_num) & 0x3FFF;
  49460. + dwcep->desc_addr[i].buf =
  49461. + req->dma + ddesc_iso[i].offset;
  49462. + dwcep->desc_addr[i].status.b_iso_out.rxbytes =
  49463. + ddesc_iso[i].length;
  49464. + dwcep->desc_addr[i].status.b_iso_out.framenum =
  49465. + dwcep->xiso_frame_num;
  49466. + dwcep->desc_addr[i].status.b_iso_out.bs =
  49467. + BS_HOST_READY;
  49468. + dwcep->desc_addr[i].status.b_iso_out.rxsts = 0;
  49469. + dwcep->desc_addr[i].status.b_iso_out.sp =
  49470. + (ddesc_iso[i].length %
  49471. + dwcep->maxpacket) ? 1 : 0;
  49472. + dwcep->desc_addr[i].status.b_iso_out.ioc = 0;
  49473. + dwcep->desc_addr[i].status.b_iso_out.pid = nat + 1;
  49474. + dwcep->desc_addr[i].status.b_iso_out.l = 0;
  49475. +
  49476. + /* Process the last descriptor */
  49477. + if (i == ereq->pio_pkt_count - 1) {
  49478. + dwcep->desc_addr[i].status.b_iso_out.ioc = 1;
  49479. + dwcep->desc_addr[i].status.b_iso_out.l = 1;
  49480. + }
  49481. + }
  49482. +
  49483. + /* Setup and start the transfer for this endpoint */
  49484. + dwcep->xiso_active_xfers++;
  49485. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->
  49486. + dev_if->out_ep_regs[dwcep->num]->
  49487. + doepdma, dwcep->dma_desc_addr);
  49488. + diepctl.d32 = 0;
  49489. + diepctl.b.epena = 1;
  49490. + diepctl.b.cnak = 1;
  49491. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->
  49492. + dev_if->out_ep_regs[dwcep->num]->
  49493. + doepctl, 0, diepctl.d32);
  49494. + }
  49495. +
  49496. + } else {
  49497. + ep->stopped = 1;
  49498. + }
  49499. +
  49500. + return 0;
  49501. +}
  49502. +
  49503. +/**
  49504. + * - Remove the request from the queue
  49505. + */
  49506. +void complete_xiso_ep(dwc_otg_pcd_ep_t * ep)
  49507. +{
  49508. + dwc_otg_pcd_request_t *req = NULL;
  49509. + struct dwc_iso_xreq_port *ereq = NULL;
  49510. + struct dwc_iso_pkt_desc_port *ddesc_iso = NULL;
  49511. + dwc_ep_t *dwcep = NULL;
  49512. + int i;
  49513. +
  49514. + //DWC_DEBUG();
  49515. + dwcep = &ep->dwc_ep;
  49516. +
  49517. + /* Get the first pending request from the queue */
  49518. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  49519. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  49520. + if (!req) {
  49521. + DWC_PRINTF("complete_ep 0x%p, req = NULL!\n", ep);
  49522. + return;
  49523. + }
  49524. + dwcep->xiso_active_xfers--;
  49525. + dwcep->xiso_queued_xfers--;
  49526. + /* Remove this request from the queue */
  49527. + DWC_CIRCLEQ_REMOVE_INIT(&ep->queue, req, queue_entry);
  49528. + } else {
  49529. + DWC_PRINTF("complete_ep 0x%p, ep->queue empty!\n", ep);
  49530. + return;
  49531. + }
  49532. +
  49533. + ep->stopped = 1;
  49534. + ereq = &req->ext_req;
  49535. + ddesc_iso = ereq->per_io_frame_descs;
  49536. +
  49537. + if (dwcep->xiso_active_xfers < 0) {
  49538. + DWC_WARN("EP#%d (xiso_active_xfers=%d)", dwcep->num,
  49539. + dwcep->xiso_active_xfers);
  49540. + }
  49541. +
  49542. + /* Fill the Isoc descs of portable extended req from dma descriptors */
  49543. + for (i = 0; i < ereq->pio_pkt_count; i++) {
  49544. + if (dwcep->is_in) { /* IN endpoints */
  49545. + ddesc_iso[i].actual_length = ddesc_iso[i].length -
  49546. + dwcep->desc_addr[i].status.b_iso_in.txbytes;
  49547. + ddesc_iso[i].status =
  49548. + dwcep->desc_addr[i].status.b_iso_in.txsts;
  49549. + } else { /* OUT endpoints */
  49550. + ddesc_iso[i].actual_length = ddesc_iso[i].length -
  49551. + dwcep->desc_addr[i].status.b_iso_out.rxbytes;
  49552. + ddesc_iso[i].status =
  49553. + dwcep->desc_addr[i].status.b_iso_out.rxsts;
  49554. + }
  49555. + }
  49556. +
  49557. + DWC_SPINUNLOCK(ep->pcd->lock);
  49558. +
  49559. + /* Call the completion function in the non-portable logic */
  49560. + ep->pcd->fops->xisoc_complete(ep->pcd, ep->priv, req->priv, 0,
  49561. + &req->ext_req);
  49562. +
  49563. + DWC_SPINLOCK(ep->pcd->lock);
  49564. +
  49565. + /* Free the request - specific freeing needed for extended request object */
  49566. + dwc_pcd_xiso_ereq_free(ep, req);
  49567. +
  49568. + /* Start the next request */
  49569. + dwc_otg_pcd_xiso_start_next_request(ep->pcd, ep);
  49570. +
  49571. + return;
  49572. +}
  49573. +
  49574. +/**
  49575. + * Create and initialize the Isoc pkt descriptors of the extended request.
  49576. + *
  49577. + */
  49578. +static int dwc_otg_pcd_xiso_create_pkt_descs(dwc_otg_pcd_request_t * req,
  49579. + void *ereq_nonport,
  49580. + int atomic_alloc)
  49581. +{
  49582. + struct dwc_iso_xreq_port *ereq = NULL;
  49583. + struct dwc_iso_xreq_port *req_mapped = NULL;
  49584. + struct dwc_iso_pkt_desc_port *ipds = NULL; /* To be created in this function */
  49585. + uint32_t pkt_count;
  49586. + int i;
  49587. +
  49588. + ereq = &req->ext_req;
  49589. + req_mapped = (struct dwc_iso_xreq_port *)ereq_nonport;
  49590. + pkt_count = req_mapped->pio_pkt_count;
  49591. +
  49592. + /* Create the isoc descs */
  49593. + if (atomic_alloc) {
  49594. + ipds = DWC_ALLOC_ATOMIC(sizeof(*ipds) * pkt_count);
  49595. + } else {
  49596. + ipds = DWC_ALLOC(sizeof(*ipds) * pkt_count);
  49597. + }
  49598. +
  49599. + if (!ipds) {
  49600. + DWC_ERROR("Failed to allocate isoc descriptors");
  49601. + return -DWC_E_NO_MEMORY;
  49602. + }
  49603. +
  49604. + /* Initialize the extended request fields */
  49605. + ereq->per_io_frame_descs = ipds;
  49606. + ereq->error_count = 0;
  49607. + ereq->pio_alloc_pkt_count = pkt_count;
  49608. + ereq->pio_pkt_count = pkt_count;
  49609. + ereq->tr_sub_flags = req_mapped->tr_sub_flags;
  49610. +
  49611. + /* Init the Isoc descriptors */
  49612. + for (i = 0; i < pkt_count; i++) {
  49613. + ipds[i].length = req_mapped->per_io_frame_descs[i].length;
  49614. + ipds[i].offset = req_mapped->per_io_frame_descs[i].offset;
  49615. + ipds[i].status = req_mapped->per_io_frame_descs[i].status; /* 0 */
  49616. + ipds[i].actual_length =
  49617. + req_mapped->per_io_frame_descs[i].actual_length;
  49618. + }
  49619. +
  49620. + return 0;
  49621. +}
  49622. +
  49623. +static void prn_ext_request(struct dwc_iso_xreq_port *ereq)
  49624. +{
  49625. + struct dwc_iso_pkt_desc_port *xfd = NULL;
  49626. + int i;
  49627. +
  49628. + DWC_DEBUG("per_io_frame_descs=%p", ereq->per_io_frame_descs);
  49629. + DWC_DEBUG("tr_sub_flags=%d", ereq->tr_sub_flags);
  49630. + DWC_DEBUG("error_count=%d", ereq->error_count);
  49631. + DWC_DEBUG("pio_alloc_pkt_count=%d", ereq->pio_alloc_pkt_count);
  49632. + DWC_DEBUG("pio_pkt_count=%d", ereq->pio_pkt_count);
  49633. + DWC_DEBUG("res=%d", ereq->res);
  49634. +
  49635. + for (i = 0; i < ereq->pio_pkt_count; i++) {
  49636. + xfd = &ereq->per_io_frame_descs[0];
  49637. + DWC_DEBUG("FD #%d", i);
  49638. +
  49639. + DWC_DEBUG("xfd->actual_length=%d", xfd->actual_length);
  49640. + DWC_DEBUG("xfd->length=%d", xfd->length);
  49641. + DWC_DEBUG("xfd->offset=%d", xfd->offset);
  49642. + DWC_DEBUG("xfd->status=%d", xfd->status);
  49643. + }
  49644. +}
  49645. +
  49646. +/**
  49647. + *
  49648. + */
  49649. +int dwc_otg_pcd_xiso_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
  49650. + uint8_t * buf, dwc_dma_t dma_buf, uint32_t buflen,
  49651. + int zero, void *req_handle, int atomic_alloc,
  49652. + void *ereq_nonport)
  49653. +{
  49654. + dwc_otg_pcd_request_t *req = NULL;
  49655. + dwc_otg_pcd_ep_t *ep;
  49656. + dwc_irqflags_t flags;
  49657. + int res;
  49658. +
  49659. + ep = get_ep_from_handle(pcd, ep_handle);
  49660. + if (!ep) {
  49661. + DWC_WARN("bad ep\n");
  49662. + return -DWC_E_INVALID;
  49663. + }
  49664. +
  49665. + /* We support this extension only for DDMA mode */
  49666. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC)
  49667. + if (!GET_CORE_IF(pcd)->dma_desc_enable)
  49668. + return -DWC_E_INVALID;
  49669. +
  49670. + /* Create a dwc_otg_pcd_request_t object */
  49671. + if (atomic_alloc) {
  49672. + req = DWC_ALLOC_ATOMIC(sizeof(*req));
  49673. + } else {
  49674. + req = DWC_ALLOC(sizeof(*req));
  49675. + }
  49676. +
  49677. + if (!req) {
  49678. + return -DWC_E_NO_MEMORY;
  49679. + }
  49680. +
  49681. + /* Create the Isoc descs for this request which shall be the exact match
  49682. + * of the structure sent to us from the non-portable logic */
  49683. + res =
  49684. + dwc_otg_pcd_xiso_create_pkt_descs(req, ereq_nonport, atomic_alloc);
  49685. + if (res) {
  49686. + DWC_WARN("Failed to init the Isoc descriptors");
  49687. + DWC_FREE(req);
  49688. + return res;
  49689. + }
  49690. +
  49691. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  49692. +
  49693. + DWC_CIRCLEQ_INIT_ENTRY(req, queue_entry);
  49694. + req->buf = buf;
  49695. + req->dma = dma_buf;
  49696. + req->length = buflen;
  49697. + req->sent_zlp = zero;
  49698. + req->priv = req_handle;
  49699. +
  49700. + //DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  49701. + ep->dwc_ep.dma_addr = dma_buf;
  49702. + ep->dwc_ep.start_xfer_buff = buf;
  49703. + ep->dwc_ep.xfer_buff = buf;
  49704. + ep->dwc_ep.xfer_len = 0;
  49705. + ep->dwc_ep.xfer_count = 0;
  49706. + ep->dwc_ep.sent_zlp = 0;
  49707. + ep->dwc_ep.total_len = buflen;
  49708. +
  49709. + /* Add this request to the tail */
  49710. + DWC_CIRCLEQ_INSERT_TAIL(&ep->queue, req, queue_entry);
  49711. + ep->dwc_ep.xiso_queued_xfers++;
  49712. +
  49713. +//DWC_DEBUG("CP_0");
  49714. +//DWC_DEBUG("req->ext_req.tr_sub_flags=%d", req->ext_req.tr_sub_flags);
  49715. +//prn_ext_request((struct dwc_iso_xreq_port *) ereq_nonport);
  49716. +//prn_ext_request(&req->ext_req);
  49717. +
  49718. + //DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  49719. +
  49720. + /* If the req->status == ASAP then check if there is any active transfer
  49721. + * for this endpoint. If no active transfers, then get the first entry
  49722. + * from the queue and start that transfer
  49723. + */
  49724. + if (req->ext_req.tr_sub_flags == DWC_EREQ_TF_ASAP) {
  49725. + res = dwc_otg_pcd_xiso_start_next_request(pcd, ep);
  49726. + if (res) {
  49727. + DWC_WARN("Failed to start the next Isoc transfer");
  49728. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  49729. + DWC_FREE(req);
  49730. + return res;
  49731. + }
  49732. + }
  49733. +
  49734. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  49735. + return 0;
  49736. +}
  49737. +
  49738. +#endif
  49739. +/* END ifdef DWC_UTE_PER_IO ***************************************************/
  49740. +int dwc_otg_pcd_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
  49741. + uint8_t * buf, dwc_dma_t dma_buf, uint32_t buflen,
  49742. + int zero, void *req_handle, int atomic_alloc)
  49743. +{
  49744. + struct device *dev = dwc_otg_pcd_to_dev(pcd);
  49745. + dwc_irqflags_t flags;
  49746. + dwc_otg_pcd_request_t *req;
  49747. + dwc_otg_pcd_ep_t *ep;
  49748. + uint32_t max_transfer;
  49749. +
  49750. + ep = get_ep_from_handle(pcd, ep_handle);
  49751. + if (!ep || (!ep->desc && ep->dwc_ep.num != 0)) {
  49752. + DWC_WARN("bad ep\n");
  49753. + return -DWC_E_INVALID;
  49754. + }
  49755. +
  49756. + if (atomic_alloc) {
  49757. + req = DWC_ALLOC_ATOMIC(sizeof(*req));
  49758. + } else {
  49759. + req = DWC_ALLOC(sizeof(*req));
  49760. + }
  49761. +
  49762. + if (!req) {
  49763. + return -DWC_E_NO_MEMORY;
  49764. + }
  49765. + DWC_CIRCLEQ_INIT_ENTRY(req, queue_entry);
  49766. + if (!GET_CORE_IF(pcd)->core_params->opt) {
  49767. + if (ep->dwc_ep.num != 0) {
  49768. + DWC_ERROR("queue req %p, len %d buf %p\n",
  49769. + req_handle, buflen, buf);
  49770. + }
  49771. + }
  49772. +
  49773. + req->buf = buf;
  49774. + req->dma = dma_buf;
  49775. + req->length = buflen;
  49776. + req->sent_zlp = zero;
  49777. + req->priv = req_handle;
  49778. + req->dw_align_buf = NULL;
  49779. + if ((dma_buf & 0x3) && GET_CORE_IF(pcd)->dma_enable
  49780. + && !GET_CORE_IF(pcd)->dma_desc_enable)
  49781. + req->dw_align_buf = DWC_DMA_ALLOC(dev, buflen,
  49782. + &req->dw_align_buf_dma);
  49783. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  49784. +
  49785. + /*
  49786. + * After adding request to the queue for IN ISOC wait for In Token Received
  49787. + * when TX FIFO is empty interrupt and for OUT ISOC wait for OUT Token
  49788. + * Received when EP is disabled interrupt to obtain starting microframe
  49789. + * (odd/even) start transfer
  49790. + */
  49791. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  49792. + if (req != 0) {
  49793. + depctl_data_t depctl = {.d32 =
  49794. + DWC_READ_REG32(&pcd->core_if->dev_if->
  49795. + in_ep_regs[ep->dwc_ep.num]->
  49796. + diepctl) };
  49797. + ++pcd->request_pending;
  49798. +
  49799. + DWC_CIRCLEQ_INSERT_TAIL(&ep->queue, req, queue_entry);
  49800. + if (ep->dwc_ep.is_in) {
  49801. + depctl.b.cnak = 1;
  49802. + DWC_WRITE_REG32(&pcd->core_if->dev_if->
  49803. + in_ep_regs[ep->dwc_ep.num]->
  49804. + diepctl, depctl.d32);
  49805. + }
  49806. +
  49807. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  49808. + }
  49809. + return 0;
  49810. + }
  49811. +
  49812. + /*
  49813. + * For EP0 IN without premature status, zlp is required?
  49814. + */
  49815. + if (ep->dwc_ep.num == 0 && ep->dwc_ep.is_in) {
  49816. + DWC_DEBUGPL(DBG_PCDV, "%d-OUT ZLP\n", ep->dwc_ep.num);
  49817. + //_req->zero = 1;
  49818. + }
  49819. +
  49820. + /* Start the transfer */
  49821. + if (DWC_CIRCLEQ_EMPTY(&ep->queue) && !ep->stopped) {
  49822. + /* EP0 Transfer? */
  49823. + if (ep->dwc_ep.num == 0) {
  49824. + switch (pcd->ep0state) {
  49825. + case EP0_IN_DATA_PHASE:
  49826. + DWC_DEBUGPL(DBG_PCD,
  49827. + "%s ep0: EP0_IN_DATA_PHASE\n",
  49828. + __func__);
  49829. + break;
  49830. +
  49831. + case EP0_OUT_DATA_PHASE:
  49832. + DWC_DEBUGPL(DBG_PCD,
  49833. + "%s ep0: EP0_OUT_DATA_PHASE\n",
  49834. + __func__);
  49835. + if (pcd->request_config) {
  49836. + /* Complete STATUS PHASE */
  49837. + ep->dwc_ep.is_in = 1;
  49838. + pcd->ep0state = EP0_IN_STATUS_PHASE;
  49839. + }
  49840. + break;
  49841. +
  49842. + case EP0_IN_STATUS_PHASE:
  49843. + DWC_DEBUGPL(DBG_PCD,
  49844. + "%s ep0: EP0_IN_STATUS_PHASE\n",
  49845. + __func__);
  49846. + break;
  49847. +
  49848. + default:
  49849. + DWC_DEBUGPL(DBG_ANY, "ep0: odd state %d\n",
  49850. + pcd->ep0state);
  49851. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  49852. + return -DWC_E_SHUTDOWN;
  49853. + }
  49854. +
  49855. + ep->dwc_ep.dma_addr = dma_buf;
  49856. + ep->dwc_ep.start_xfer_buff = buf;
  49857. + ep->dwc_ep.xfer_buff = buf;
  49858. + ep->dwc_ep.xfer_len = buflen;
  49859. + ep->dwc_ep.xfer_count = 0;
  49860. + ep->dwc_ep.sent_zlp = 0;
  49861. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  49862. +
  49863. + if (zero) {
  49864. + if ((ep->dwc_ep.xfer_len %
  49865. + ep->dwc_ep.maxpacket == 0)
  49866. + && (ep->dwc_ep.xfer_len != 0)) {
  49867. + ep->dwc_ep.sent_zlp = 1;
  49868. + }
  49869. +
  49870. + }
  49871. +
  49872. + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd),
  49873. + &ep->dwc_ep);
  49874. + } // non-ep0 endpoints
  49875. + else {
  49876. +#ifdef DWC_UTE_CFI
  49877. + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
  49878. + /* store the request length */
  49879. + ep->dwc_ep.cfi_req_len = buflen;
  49880. + pcd->cfi->ops.build_descriptors(pcd->cfi, pcd,
  49881. + ep, req);
  49882. + } else {
  49883. +#endif
  49884. + max_transfer =
  49885. + GET_CORE_IF(ep->pcd)->core_params->
  49886. + max_transfer_size;
  49887. +
  49888. + /* Setup and start the Transfer */
  49889. + if (req->dw_align_buf){
  49890. + if (ep->dwc_ep.is_in)
  49891. + dwc_memcpy(req->dw_align_buf,
  49892. + buf, buflen);
  49893. + ep->dwc_ep.dma_addr =
  49894. + req->dw_align_buf_dma;
  49895. + ep->dwc_ep.start_xfer_buff =
  49896. + req->dw_align_buf;
  49897. + ep->dwc_ep.xfer_buff =
  49898. + req->dw_align_buf;
  49899. + } else {
  49900. + ep->dwc_ep.dma_addr = dma_buf;
  49901. + ep->dwc_ep.start_xfer_buff = buf;
  49902. + ep->dwc_ep.xfer_buff = buf;
  49903. + }
  49904. + ep->dwc_ep.xfer_len = 0;
  49905. + ep->dwc_ep.xfer_count = 0;
  49906. + ep->dwc_ep.sent_zlp = 0;
  49907. + ep->dwc_ep.total_len = buflen;
  49908. +
  49909. + ep->dwc_ep.maxxfer = max_transfer;
  49910. + if (GET_CORE_IF(pcd)->dma_desc_enable) {
  49911. + uint32_t out_max_xfer =
  49912. + DDMA_MAX_TRANSFER_SIZE -
  49913. + (DDMA_MAX_TRANSFER_SIZE % 4);
  49914. + if (ep->dwc_ep.is_in) {
  49915. + if (ep->dwc_ep.maxxfer >
  49916. + DDMA_MAX_TRANSFER_SIZE) {
  49917. + ep->dwc_ep.maxxfer =
  49918. + DDMA_MAX_TRANSFER_SIZE;
  49919. + }
  49920. + } else {
  49921. + if (ep->dwc_ep.maxxfer >
  49922. + out_max_xfer) {
  49923. + ep->dwc_ep.maxxfer =
  49924. + out_max_xfer;
  49925. + }
  49926. + }
  49927. + }
  49928. + if (ep->dwc_ep.maxxfer < ep->dwc_ep.total_len) {
  49929. + ep->dwc_ep.maxxfer -=
  49930. + (ep->dwc_ep.maxxfer %
  49931. + ep->dwc_ep.maxpacket);
  49932. + }
  49933. +
  49934. + if (zero) {
  49935. + if ((ep->dwc_ep.total_len %
  49936. + ep->dwc_ep.maxpacket == 0)
  49937. + && (ep->dwc_ep.total_len != 0)) {
  49938. + ep->dwc_ep.sent_zlp = 1;
  49939. + }
  49940. + }
  49941. +#ifdef DWC_UTE_CFI
  49942. + }
  49943. +#endif
  49944. + dwc_otg_ep_start_transfer(GET_CORE_IF(pcd),
  49945. + &ep->dwc_ep);
  49946. + }
  49947. + }
  49948. +
  49949. + if (req != 0) {
  49950. + ++pcd->request_pending;
  49951. + DWC_CIRCLEQ_INSERT_TAIL(&ep->queue, req, queue_entry);
  49952. + if (ep->dwc_ep.is_in && ep->stopped
  49953. + && !(GET_CORE_IF(pcd)->dma_enable)) {
  49954. + /** @todo NGS Create a function for this. */
  49955. + diepmsk_data_t diepmsk = {.d32 = 0 };
  49956. + diepmsk.b.intktxfemp = 1;
  49957. + if (GET_CORE_IF(pcd)->multiproc_int_enable) {
  49958. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->
  49959. + dev_if->dev_global_regs->diepeachintmsk
  49960. + [ep->dwc_ep.num], 0,
  49961. + diepmsk.d32);
  49962. + } else {
  49963. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->
  49964. + dev_if->dev_global_regs->
  49965. + diepmsk, 0, diepmsk.d32);
  49966. + }
  49967. +
  49968. + }
  49969. + }
  49970. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  49971. +
  49972. + return 0;
  49973. +}
  49974. +
  49975. +int dwc_otg_pcd_ep_dequeue(dwc_otg_pcd_t * pcd, void *ep_handle,
  49976. + void *req_handle)
  49977. +{
  49978. + dwc_irqflags_t flags;
  49979. + dwc_otg_pcd_request_t *req;
  49980. + dwc_otg_pcd_ep_t *ep;
  49981. +
  49982. + ep = get_ep_from_handle(pcd, ep_handle);
  49983. + if (!ep || (!ep->desc && ep->dwc_ep.num != 0)) {
  49984. + DWC_WARN("bad argument\n");
  49985. + return -DWC_E_INVALID;
  49986. + }
  49987. +
  49988. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  49989. +
  49990. + /* make sure it's actually queued on this endpoint */
  49991. + DWC_CIRCLEQ_FOREACH(req, &ep->queue, queue_entry) {
  49992. + if (req->priv == (void *)req_handle) {
  49993. + break;
  49994. + }
  49995. + }
  49996. +
  49997. + if (req->priv != (void *)req_handle) {
  49998. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  49999. + return -DWC_E_INVALID;
  50000. + }
  50001. +
  50002. + if (!DWC_CIRCLEQ_EMPTY_ENTRY(req, queue_entry)) {
  50003. + dwc_otg_request_done(ep, req, -DWC_E_RESTART);
  50004. + } else {
  50005. + req = NULL;
  50006. + }
  50007. +
  50008. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  50009. +
  50010. + return req ? 0 : -DWC_E_SHUTDOWN;
  50011. +
  50012. +}
  50013. +
  50014. +/**
  50015. + * dwc_otg_pcd_ep_wedge - sets the halt feature and ignores clear requests
  50016. + *
  50017. + * Use this to stall an endpoint and ignore CLEAR_FEATURE(HALT_ENDPOINT)
  50018. + * requests. If the gadget driver clears the halt status, it will
  50019. + * automatically unwedge the endpoint.
  50020. + *
  50021. + * Returns zero on success, else negative DWC error code.
  50022. + */
  50023. +int dwc_otg_pcd_ep_wedge(dwc_otg_pcd_t * pcd, void *ep_handle)
  50024. +{
  50025. + dwc_otg_pcd_ep_t *ep;
  50026. + dwc_irqflags_t flags;
  50027. + int retval = 0;
  50028. +
  50029. + ep = get_ep_from_handle(pcd, ep_handle);
  50030. +
  50031. + if ((!ep->desc && ep != &pcd->ep0) ||
  50032. + (ep->desc && (ep->desc->bmAttributes == UE_ISOCHRONOUS))) {
  50033. + DWC_WARN("%s, bad ep\n", __func__);
  50034. + return -DWC_E_INVALID;
  50035. + }
  50036. +
  50037. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  50038. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  50039. + DWC_WARN("%d %s XFer In process\n", ep->dwc_ep.num,
  50040. + ep->dwc_ep.is_in ? "IN" : "OUT");
  50041. + retval = -DWC_E_AGAIN;
  50042. + } else {
  50043. + /* This code needs to be reviewed */
  50044. + if (ep->dwc_ep.is_in == 1 && GET_CORE_IF(pcd)->dma_desc_enable) {
  50045. + dtxfsts_data_t txstatus;
  50046. + fifosize_data_t txfifosize;
  50047. +
  50048. + txfifosize.d32 =
  50049. + DWC_READ_REG32(&GET_CORE_IF(pcd)->
  50050. + core_global_regs->dtxfsiz[ep->dwc_ep.
  50051. + tx_fifo_num]);
  50052. + txstatus.d32 =
  50053. + DWC_READ_REG32(&GET_CORE_IF(pcd)->
  50054. + dev_if->in_ep_regs[ep->dwc_ep.num]->
  50055. + dtxfsts);
  50056. +
  50057. + if (txstatus.b.txfspcavail < txfifosize.b.depth) {
  50058. + DWC_WARN("%s() Data In Tx Fifo\n", __func__);
  50059. + retval = -DWC_E_AGAIN;
  50060. + } else {
  50061. + if (ep->dwc_ep.num == 0) {
  50062. + pcd->ep0state = EP0_STALL;
  50063. + }
  50064. +
  50065. + ep->stopped = 1;
  50066. + dwc_otg_ep_set_stall(GET_CORE_IF(pcd),
  50067. + &ep->dwc_ep);
  50068. + }
  50069. + } else {
  50070. + if (ep->dwc_ep.num == 0) {
  50071. + pcd->ep0state = EP0_STALL;
  50072. + }
  50073. +
  50074. + ep->stopped = 1;
  50075. + dwc_otg_ep_set_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
  50076. + }
  50077. + }
  50078. +
  50079. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  50080. +
  50081. + return retval;
  50082. +}
  50083. +
  50084. +int dwc_otg_pcd_ep_halt(dwc_otg_pcd_t * pcd, void *ep_handle, int value)
  50085. +{
  50086. + dwc_otg_pcd_ep_t *ep;
  50087. + dwc_irqflags_t flags;
  50088. + int retval = 0;
  50089. +
  50090. + ep = get_ep_from_handle(pcd, ep_handle);
  50091. +
  50092. + if (!ep || (!ep->desc && ep != &pcd->ep0) ||
  50093. + (ep->desc && (ep->desc->bmAttributes == UE_ISOCHRONOUS))) {
  50094. + DWC_WARN("%s, bad ep\n", __func__);
  50095. + return -DWC_E_INVALID;
  50096. + }
  50097. +
  50098. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  50099. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  50100. + DWC_WARN("%d %s XFer In process\n", ep->dwc_ep.num,
  50101. + ep->dwc_ep.is_in ? "IN" : "OUT");
  50102. + retval = -DWC_E_AGAIN;
  50103. + } else if (value == 0) {
  50104. + dwc_otg_ep_clear_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
  50105. + } else if (value == 1) {
  50106. + if (ep->dwc_ep.is_in == 1 && GET_CORE_IF(pcd)->dma_desc_enable) {
  50107. + dtxfsts_data_t txstatus;
  50108. + fifosize_data_t txfifosize;
  50109. +
  50110. + txfifosize.d32 =
  50111. + DWC_READ_REG32(&GET_CORE_IF(pcd)->core_global_regs->
  50112. + dtxfsiz[ep->dwc_ep.tx_fifo_num]);
  50113. + txstatus.d32 =
  50114. + DWC_READ_REG32(&GET_CORE_IF(pcd)->dev_if->
  50115. + in_ep_regs[ep->dwc_ep.num]->dtxfsts);
  50116. +
  50117. + if (txstatus.b.txfspcavail < txfifosize.b.depth) {
  50118. + DWC_WARN("%s() Data In Tx Fifo\n", __func__);
  50119. + retval = -DWC_E_AGAIN;
  50120. + } else {
  50121. + if (ep->dwc_ep.num == 0) {
  50122. + pcd->ep0state = EP0_STALL;
  50123. + }
  50124. +
  50125. + ep->stopped = 1;
  50126. + dwc_otg_ep_set_stall(GET_CORE_IF(pcd),
  50127. + &ep->dwc_ep);
  50128. + }
  50129. + } else {
  50130. + if (ep->dwc_ep.num == 0) {
  50131. + pcd->ep0state = EP0_STALL;
  50132. + }
  50133. +
  50134. + ep->stopped = 1;
  50135. + dwc_otg_ep_set_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
  50136. + }
  50137. + } else if (value == 2) {
  50138. + ep->dwc_ep.stall_clear_flag = 0;
  50139. + } else if (value == 3) {
  50140. + ep->dwc_ep.stall_clear_flag = 1;
  50141. + }
  50142. +
  50143. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  50144. +
  50145. + return retval;
  50146. +}
  50147. +
  50148. +/**
  50149. + * This function initiates remote wakeup of the host from suspend state.
  50150. + */
  50151. +void dwc_otg_pcd_rem_wkup_from_suspend(dwc_otg_pcd_t * pcd, int set)
  50152. +{
  50153. + dctl_data_t dctl = { 0 };
  50154. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  50155. + dsts_data_t dsts;
  50156. +
  50157. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  50158. + if (!dsts.b.suspsts) {
  50159. + DWC_WARN("Remote wakeup while is not in suspend state\n");
  50160. + }
  50161. + /* Check if DEVICE_REMOTE_WAKEUP feature enabled */
  50162. + if (pcd->remote_wakeup_enable) {
  50163. + if (set) {
  50164. +
  50165. + if (core_if->adp_enable) {
  50166. + gpwrdn_data_t gpwrdn;
  50167. +
  50168. + dwc_otg_adp_probe_stop(core_if);
  50169. +
  50170. + /* Mask SRP detected interrupt from Power Down Logic */
  50171. + gpwrdn.d32 = 0;
  50172. + gpwrdn.b.srp_det_msk = 1;
  50173. + DWC_MODIFY_REG32(&core_if->
  50174. + core_global_regs->gpwrdn,
  50175. + gpwrdn.d32, 0);
  50176. +
  50177. + /* Disable Power Down Logic */
  50178. + gpwrdn.d32 = 0;
  50179. + gpwrdn.b.pmuactv = 1;
  50180. + DWC_MODIFY_REG32(&core_if->
  50181. + core_global_regs->gpwrdn,
  50182. + gpwrdn.d32, 0);
  50183. +
  50184. + /*
  50185. + * Initialize the Core for Device mode.
  50186. + */
  50187. + core_if->op_state = B_PERIPHERAL;
  50188. + dwc_otg_core_init(core_if);
  50189. + dwc_otg_enable_global_interrupts(core_if);
  50190. + cil_pcd_start(core_if);
  50191. +
  50192. + dwc_otg_initiate_srp(core_if);
  50193. + }
  50194. +
  50195. + dctl.b.rmtwkupsig = 1;
  50196. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  50197. + dctl, 0, dctl.d32);
  50198. + DWC_DEBUGPL(DBG_PCD, "Set Remote Wakeup\n");
  50199. +
  50200. + dwc_mdelay(2);
  50201. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  50202. + dctl, dctl.d32, 0);
  50203. + DWC_DEBUGPL(DBG_PCD, "Clear Remote Wakeup\n");
  50204. + }
  50205. + } else {
  50206. + DWC_DEBUGPL(DBG_PCD, "Remote Wakeup is disabled\n");
  50207. + }
  50208. +}
  50209. +
  50210. +#ifdef CONFIG_USB_DWC_OTG_LPM
  50211. +/**
  50212. + * This function initiates remote wakeup of the host from L1 sleep state.
  50213. + */
  50214. +void dwc_otg_pcd_rem_wkup_from_sleep(dwc_otg_pcd_t * pcd, int set)
  50215. +{
  50216. + glpmcfg_data_t lpmcfg;
  50217. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  50218. +
  50219. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  50220. +
  50221. + /* Check if we are in L1 state */
  50222. + if (!lpmcfg.b.prt_sleep_sts) {
  50223. + DWC_DEBUGPL(DBG_PCD, "Device is not in sleep state\n");
  50224. + return;
  50225. + }
  50226. +
  50227. + /* Check if host allows remote wakeup */
  50228. + if (!lpmcfg.b.rem_wkup_en) {
  50229. + DWC_DEBUGPL(DBG_PCD, "Host does not allow remote wakeup\n");
  50230. + return;
  50231. + }
  50232. +
  50233. + /* Check if Resume OK */
  50234. + if (!lpmcfg.b.sleep_state_resumeok) {
  50235. + DWC_DEBUGPL(DBG_PCD, "Sleep state resume is not OK\n");
  50236. + return;
  50237. + }
  50238. +
  50239. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  50240. + lpmcfg.b.en_utmi_sleep = 0;
  50241. + lpmcfg.b.hird_thres &= (~(1 << 4));
  50242. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  50243. +
  50244. + if (set) {
  50245. + dctl_data_t dctl = {.d32 = 0 };
  50246. + dctl.b.rmtwkupsig = 1;
  50247. + /* Set RmtWkUpSig bit to start remote wakup signaling.
  50248. + * Hardware will automatically clear this bit.
  50249. + */
  50250. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl,
  50251. + 0, dctl.d32);
  50252. + DWC_DEBUGPL(DBG_PCD, "Set Remote Wakeup\n");
  50253. + }
  50254. +
  50255. +}
  50256. +#endif
  50257. +
  50258. +/**
  50259. + * Performs remote wakeup.
  50260. + */
  50261. +void dwc_otg_pcd_remote_wakeup(dwc_otg_pcd_t * pcd, int set)
  50262. +{
  50263. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  50264. + dwc_irqflags_t flags;
  50265. + if (dwc_otg_is_device_mode(core_if)) {
  50266. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  50267. +#ifdef CONFIG_USB_DWC_OTG_LPM
  50268. + if (core_if->lx_state == DWC_OTG_L1) {
  50269. + dwc_otg_pcd_rem_wkup_from_sleep(pcd, set);
  50270. + } else {
  50271. +#endif
  50272. + dwc_otg_pcd_rem_wkup_from_suspend(pcd, set);
  50273. +#ifdef CONFIG_USB_DWC_OTG_LPM
  50274. + }
  50275. +#endif
  50276. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  50277. + }
  50278. + return;
  50279. +}
  50280. +
  50281. +void dwc_otg_pcd_disconnect_us(dwc_otg_pcd_t * pcd, int no_of_usecs)
  50282. +{
  50283. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  50284. + dctl_data_t dctl = { 0 };
  50285. +
  50286. + if (dwc_otg_is_device_mode(core_if)) {
  50287. + dctl.b.sftdiscon = 1;
  50288. + DWC_PRINTF("Soft disconnect for %d useconds\n",no_of_usecs);
  50289. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  50290. + dwc_udelay(no_of_usecs);
  50291. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32,0);
  50292. +
  50293. + } else{
  50294. + DWC_PRINTF("NOT SUPPORTED IN HOST MODE\n");
  50295. + }
  50296. + return;
  50297. +
  50298. +}
  50299. +
  50300. +int dwc_otg_pcd_wakeup(dwc_otg_pcd_t * pcd)
  50301. +{
  50302. + dsts_data_t dsts;
  50303. + gotgctl_data_t gotgctl;
  50304. +
  50305. + /*
  50306. + * This function starts the Protocol if no session is in progress. If
  50307. + * a session is already in progress, but the device is suspended,
  50308. + * remote wakeup signaling is started.
  50309. + */
  50310. +
  50311. + /* Check if valid session */
  50312. + gotgctl.d32 =
  50313. + DWC_READ_REG32(&(GET_CORE_IF(pcd)->core_global_regs->gotgctl));
  50314. + if (gotgctl.b.bsesvld) {
  50315. + /* Check if suspend state */
  50316. + dsts.d32 =
  50317. + DWC_READ_REG32(&
  50318. + (GET_CORE_IF(pcd)->dev_if->
  50319. + dev_global_regs->dsts));
  50320. + if (dsts.b.suspsts) {
  50321. + dwc_otg_pcd_remote_wakeup(pcd, 1);
  50322. + }
  50323. + } else {
  50324. + dwc_otg_pcd_initiate_srp(pcd);
  50325. + }
  50326. +
  50327. + return 0;
  50328. +
  50329. +}
  50330. +
  50331. +/**
  50332. + * Start the SRP timer to detect when the SRP does not complete within
  50333. + * 6 seconds.
  50334. + *
  50335. + * @param pcd the pcd structure.
  50336. + */
  50337. +void dwc_otg_pcd_initiate_srp(dwc_otg_pcd_t * pcd)
  50338. +{
  50339. + dwc_irqflags_t flags;
  50340. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  50341. + dwc_otg_initiate_srp(GET_CORE_IF(pcd));
  50342. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  50343. +}
  50344. +
  50345. +int dwc_otg_pcd_get_frame_number(dwc_otg_pcd_t * pcd)
  50346. +{
  50347. + return dwc_otg_get_frame_number(GET_CORE_IF(pcd));
  50348. +}
  50349. +
  50350. +int dwc_otg_pcd_is_lpm_enabled(dwc_otg_pcd_t * pcd)
  50351. +{
  50352. + return GET_CORE_IF(pcd)->core_params->lpm_enable;
  50353. +}
  50354. +
  50355. +uint32_t get_b_hnp_enable(dwc_otg_pcd_t * pcd)
  50356. +{
  50357. + return pcd->b_hnp_enable;
  50358. +}
  50359. +
  50360. +uint32_t get_a_hnp_support(dwc_otg_pcd_t * pcd)
  50361. +{
  50362. + return pcd->a_hnp_support;
  50363. +}
  50364. +
  50365. +uint32_t get_a_alt_hnp_support(dwc_otg_pcd_t * pcd)
  50366. +{
  50367. + return pcd->a_alt_hnp_support;
  50368. +}
  50369. +
  50370. +int dwc_otg_pcd_get_rmwkup_enable(dwc_otg_pcd_t * pcd)
  50371. +{
  50372. + return pcd->remote_wakeup_enable;
  50373. +}
  50374. +
  50375. +#endif /* DWC_HOST_ONLY */
  50376. --- /dev/null
  50377. +++ b/drivers/usb/host/dwc_otg/dwc_otg_pcd.h
  50378. @@ -0,0 +1,273 @@
  50379. +/* ==========================================================================
  50380. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd.h $
  50381. + * $Revision: #48 $
  50382. + * $Date: 2012/08/10 $
  50383. + * $Change: 2047372 $
  50384. + *
  50385. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  50386. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  50387. + * otherwise expressly agreed to in writing between Synopsys and you.
  50388. + *
  50389. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  50390. + * any End User Software License Agreement or Agreement for Licensed Product
  50391. + * with Synopsys or any supplement thereto. You are permitted to use and
  50392. + * redistribute this Software in source and binary forms, with or without
  50393. + * modification, provided that redistributions of source code must retain this
  50394. + * notice. You may not view, use, disclose, copy or distribute this file or
  50395. + * any information contained herein except pursuant to this license grant from
  50396. + * Synopsys. If you do not agree with this notice, including the disclaimer
  50397. + * below, then you are not authorized to use the Software.
  50398. + *
  50399. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  50400. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50401. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  50402. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  50403. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  50404. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  50405. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  50406. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  50407. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50408. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  50409. + * DAMAGE.
  50410. + * ========================================================================== */
  50411. +#ifndef DWC_HOST_ONLY
  50412. +#if !defined(__DWC_PCD_H__)
  50413. +#define __DWC_PCD_H__
  50414. +
  50415. +#include "dwc_otg_os_dep.h"
  50416. +#include "usb.h"
  50417. +#include "dwc_otg_cil.h"
  50418. +#include "dwc_otg_pcd_if.h"
  50419. +#include "dwc_otg_driver.h"
  50420. +
  50421. +struct cfiobject;
  50422. +
  50423. +/**
  50424. + * @file
  50425. + *
  50426. + * This file contains the structures, constants, and interfaces for
  50427. + * the Perpherial Contoller Driver (PCD).
  50428. + *
  50429. + * The Peripheral Controller Driver (PCD) for Linux will implement the
  50430. + * Gadget API, so that the existing Gadget drivers can be used. For
  50431. + * the Mass Storage Function driver the File-backed USB Storage Gadget
  50432. + * (FBS) driver will be used. The FBS driver supports the
  50433. + * Control-Bulk (CB), Control-Bulk-Interrupt (CBI), and Bulk-Only
  50434. + * transports.
  50435. + *
  50436. + */
  50437. +
  50438. +/** Invalid DMA Address */
  50439. +#define DWC_DMA_ADDR_INVALID (~(dwc_dma_t)0)
  50440. +
  50441. +/** Max Transfer size for any EP */
  50442. +#define DDMA_MAX_TRANSFER_SIZE 65535
  50443. +
  50444. +/**
  50445. + * Get the pointer to the core_if from the pcd pointer.
  50446. + */
  50447. +#define GET_CORE_IF( _pcd ) (_pcd->core_if)
  50448. +
  50449. +/**
  50450. + * States of EP0.
  50451. + */
  50452. +typedef enum ep0_state {
  50453. + EP0_DISCONNECT, /* no host */
  50454. + EP0_IDLE,
  50455. + EP0_IN_DATA_PHASE,
  50456. + EP0_OUT_DATA_PHASE,
  50457. + EP0_IN_STATUS_PHASE,
  50458. + EP0_OUT_STATUS_PHASE,
  50459. + EP0_STALL,
  50460. +} ep0state_e;
  50461. +
  50462. +/** Fordward declaration.*/
  50463. +struct dwc_otg_pcd;
  50464. +
  50465. +/** DWC_otg iso request structure.
  50466. + *
  50467. + */
  50468. +typedef struct usb_iso_request dwc_otg_pcd_iso_request_t;
  50469. +
  50470. +#ifdef DWC_UTE_PER_IO
  50471. +
  50472. +/**
  50473. + * This shall be the exact analogy of the same type structure defined in the
  50474. + * usb_gadget.h. Each descriptor contains
  50475. + */
  50476. +struct dwc_iso_pkt_desc_port {
  50477. + uint32_t offset;
  50478. + uint32_t length; /* expected length */
  50479. + uint32_t actual_length;
  50480. + uint32_t status;
  50481. +};
  50482. +
  50483. +struct dwc_iso_xreq_port {
  50484. + /** transfer/submission flag */
  50485. + uint32_t tr_sub_flags;
  50486. + /** Start the request ASAP */
  50487. +#define DWC_EREQ_TF_ASAP 0x00000002
  50488. + /** Just enqueue the request w/o initiating a transfer */
  50489. +#define DWC_EREQ_TF_ENQUEUE 0x00000004
  50490. +
  50491. + /**
  50492. + * count of ISO packets attached to this request - shall
  50493. + * not exceed the pio_alloc_pkt_count
  50494. + */
  50495. + uint32_t pio_pkt_count;
  50496. + /** count of ISO packets allocated for this request */
  50497. + uint32_t pio_alloc_pkt_count;
  50498. + /** number of ISO packet errors */
  50499. + uint32_t error_count;
  50500. + /** reserved for future extension */
  50501. + uint32_t res;
  50502. + /** Will be allocated and freed in the UTE gadget and based on the CFC value */
  50503. + struct dwc_iso_pkt_desc_port *per_io_frame_descs;
  50504. +};
  50505. +#endif
  50506. +/** DWC_otg request structure.
  50507. + * This structure is a list of requests.
  50508. + */
  50509. +typedef struct dwc_otg_pcd_request {
  50510. + void *priv;
  50511. + void *buf;
  50512. + dwc_dma_t dma;
  50513. + uint32_t length;
  50514. + uint32_t actual;
  50515. + unsigned sent_zlp:1;
  50516. + /**
  50517. + * Used instead of original buffer if
  50518. + * it(physical address) is not dword-aligned.
  50519. + **/
  50520. + uint8_t *dw_align_buf;
  50521. + dwc_dma_t dw_align_buf_dma;
  50522. +
  50523. + DWC_CIRCLEQ_ENTRY(dwc_otg_pcd_request) queue_entry;
  50524. +#ifdef DWC_UTE_PER_IO
  50525. + struct dwc_iso_xreq_port ext_req;
  50526. + //void *priv_ereq_nport; /* */
  50527. +#endif
  50528. +} dwc_otg_pcd_request_t;
  50529. +
  50530. +DWC_CIRCLEQ_HEAD(req_list, dwc_otg_pcd_request);
  50531. +
  50532. +/** PCD EP structure.
  50533. + * This structure describes an EP, there is an array of EPs in the PCD
  50534. + * structure.
  50535. + */
  50536. +typedef struct dwc_otg_pcd_ep {
  50537. + /** USB EP Descriptor */
  50538. + const usb_endpoint_descriptor_t *desc;
  50539. +
  50540. + /** queue of dwc_otg_pcd_requests. */
  50541. + struct req_list queue;
  50542. + unsigned stopped:1;
  50543. + unsigned disabling:1;
  50544. + unsigned dma:1;
  50545. + unsigned queue_sof:1;
  50546. +
  50547. +#ifdef DWC_EN_ISOC
  50548. + /** ISOC req handle passed */
  50549. + void *iso_req_handle;
  50550. +#endif //_EN_ISOC_
  50551. +
  50552. + /** DWC_otg ep data. */
  50553. + dwc_ep_t dwc_ep;
  50554. +
  50555. + /** Pointer to PCD */
  50556. + struct dwc_otg_pcd *pcd;
  50557. +
  50558. + void *priv;
  50559. +} dwc_otg_pcd_ep_t;
  50560. +
  50561. +/** DWC_otg PCD Structure.
  50562. + * This structure encapsulates the data for the dwc_otg PCD.
  50563. + */
  50564. +struct dwc_otg_pcd {
  50565. + const struct dwc_otg_pcd_function_ops *fops;
  50566. + /** The DWC otg device pointer */
  50567. + struct dwc_otg_device *otg_dev;
  50568. + /** Core Interface */
  50569. + dwc_otg_core_if_t *core_if;
  50570. + /** State of EP0 */
  50571. + ep0state_e ep0state;
  50572. + /** EP0 Request is pending */
  50573. + unsigned ep0_pending:1;
  50574. + /** Indicates when SET CONFIGURATION Request is in process */
  50575. + unsigned request_config:1;
  50576. + /** The state of the Remote Wakeup Enable. */
  50577. + unsigned remote_wakeup_enable:1;
  50578. + /** The state of the B-Device HNP Enable. */
  50579. + unsigned b_hnp_enable:1;
  50580. + /** The state of A-Device HNP Support. */
  50581. + unsigned a_hnp_support:1;
  50582. + /** The state of the A-Device Alt HNP support. */
  50583. + unsigned a_alt_hnp_support:1;
  50584. + /** Count of pending Requests */
  50585. + unsigned request_pending;
  50586. +
  50587. + /** SETUP packet for EP0
  50588. + * This structure is allocated as a DMA buffer on PCD initialization
  50589. + * with enough space for up to 3 setup packets.
  50590. + */
  50591. + union {
  50592. + usb_device_request_t req;
  50593. + uint32_t d32[2];
  50594. + } *setup_pkt;
  50595. +
  50596. + dwc_dma_t setup_pkt_dma_handle;
  50597. +
  50598. + /* Additional buffer and flag for CTRL_WR premature case */
  50599. + uint8_t *backup_buf;
  50600. + unsigned data_terminated;
  50601. +
  50602. + /** 2-byte dma buffer used to return status from GET_STATUS */
  50603. + uint16_t *status_buf;
  50604. + dwc_dma_t status_buf_dma_handle;
  50605. +
  50606. + /** EP0 */
  50607. + dwc_otg_pcd_ep_t ep0;
  50608. +
  50609. + /** Array of IN EPs. */
  50610. + dwc_otg_pcd_ep_t in_ep[MAX_EPS_CHANNELS - 1];
  50611. + /** Array of OUT EPs. */
  50612. + dwc_otg_pcd_ep_t out_ep[MAX_EPS_CHANNELS - 1];
  50613. + /** number of valid EPs in the above array. */
  50614. +// unsigned num_eps : 4;
  50615. + dwc_spinlock_t *lock;
  50616. +
  50617. + /** Tasklet to defer starting of TEST mode transmissions until
  50618. + * Status Phase has been completed.
  50619. + */
  50620. + dwc_tasklet_t *test_mode_tasklet;
  50621. +
  50622. + /** Tasklet to delay starting of xfer in DMA mode */
  50623. + dwc_tasklet_t *start_xfer_tasklet;
  50624. +
  50625. + /** The test mode to enter when the tasklet is executed. */
  50626. + unsigned test_mode;
  50627. + /** The cfi_api structure that implements most of the CFI API
  50628. + * and OTG specific core configuration functionality
  50629. + */
  50630. +#ifdef DWC_UTE_CFI
  50631. + struct cfiobject *cfi;
  50632. +#endif
  50633. +
  50634. +};
  50635. +
  50636. +static inline struct device *dwc_otg_pcd_to_dev(struct dwc_otg_pcd *pcd)
  50637. +{
  50638. + return &pcd->otg_dev->os_dep.platformdev->dev;
  50639. +}
  50640. +
  50641. +//FIXME this functions should be static, and this prototypes should be removed
  50642. +extern void dwc_otg_request_nuke(dwc_otg_pcd_ep_t * ep);
  50643. +extern void dwc_otg_request_done(dwc_otg_pcd_ep_t * ep,
  50644. + dwc_otg_pcd_request_t * req, int32_t status);
  50645. +
  50646. +void dwc_otg_iso_buffer_done(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep,
  50647. + void *req_handle);
  50648. +
  50649. +extern void do_test_mode(void *data);
  50650. +#endif
  50651. +#endif /* DWC_HOST_ONLY */
  50652. --- /dev/null
  50653. +++ b/drivers/usb/host/dwc_otg/dwc_otg_pcd_if.h
  50654. @@ -0,0 +1,361 @@
  50655. +/* ==========================================================================
  50656. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd_if.h $
  50657. + * $Revision: #11 $
  50658. + * $Date: 2011/10/26 $
  50659. + * $Change: 1873028 $
  50660. + *
  50661. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  50662. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  50663. + * otherwise expressly agreed to in writing between Synopsys and you.
  50664. + *
  50665. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  50666. + * any End User Software License Agreement or Agreement for Licensed Product
  50667. + * with Synopsys or any supplement thereto. You are permitted to use and
  50668. + * redistribute this Software in source and binary forms, with or without
  50669. + * modification, provided that redistributions of source code must retain this
  50670. + * notice. You may not view, use, disclose, copy or distribute this file or
  50671. + * any information contained herein except pursuant to this license grant from
  50672. + * Synopsys. If you do not agree with this notice, including the disclaimer
  50673. + * below, then you are not authorized to use the Software.
  50674. + *
  50675. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  50676. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50677. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  50678. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  50679. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  50680. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  50681. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  50682. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  50683. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50684. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  50685. + * DAMAGE.
  50686. + * ========================================================================== */
  50687. +#ifndef DWC_HOST_ONLY
  50688. +
  50689. +#if !defined(__DWC_PCD_IF_H__)
  50690. +#define __DWC_PCD_IF_H__
  50691. +
  50692. +//#include "dwc_os.h"
  50693. +#include "dwc_otg_core_if.h"
  50694. +#include "dwc_otg_driver.h"
  50695. +
  50696. +/** @file
  50697. + * This file defines DWC_OTG PCD Core API.
  50698. + */
  50699. +
  50700. +struct dwc_otg_pcd;
  50701. +typedef struct dwc_otg_pcd dwc_otg_pcd_t;
  50702. +
  50703. +/** Maxpacket size for EP0 */
  50704. +#define MAX_EP0_SIZE 64
  50705. +/** Maxpacket size for any EP */
  50706. +#define MAX_PACKET_SIZE 1024
  50707. +
  50708. +/** @name Function Driver Callbacks */
  50709. +/** @{ */
  50710. +
  50711. +/** This function will be called whenever a previously queued request has
  50712. + * completed. The status value will be set to -DWC_E_SHUTDOWN to indicated a
  50713. + * failed or aborted transfer, or -DWC_E_RESTART to indicate the device was reset,
  50714. + * or -DWC_E_TIMEOUT to indicate it timed out, or -DWC_E_INVALID to indicate invalid
  50715. + * parameters. */
  50716. +typedef int (*dwc_completion_cb_t) (dwc_otg_pcd_t * pcd, void *ep_handle,
  50717. + void *req_handle, int32_t status,
  50718. + uint32_t actual);
  50719. +/**
  50720. + * This function will be called whenever a previousle queued ISOC request has
  50721. + * completed. Count of ISOC packets could be read using dwc_otg_pcd_get_iso_packet_count
  50722. + * function.
  50723. + * The status of each ISOC packet could be read using dwc_otg_pcd_get_iso_packet_*
  50724. + * functions.
  50725. + */
  50726. +typedef int (*dwc_isoc_completion_cb_t) (dwc_otg_pcd_t * pcd, void *ep_handle,
  50727. + void *req_handle, int proc_buf_num);
  50728. +/** This function should handle any SETUP request that cannot be handled by the
  50729. + * PCD Core. This includes most GET_DESCRIPTORs, SET_CONFIGS, Any
  50730. + * class-specific requests, etc. The function must non-blocking.
  50731. + *
  50732. + * Returns 0 on success.
  50733. + * Returns -DWC_E_NOT_SUPPORTED if the request is not supported.
  50734. + * Returns -DWC_E_INVALID if the setup request had invalid parameters or bytes.
  50735. + * Returns -DWC_E_SHUTDOWN on any other error. */
  50736. +typedef int (*dwc_setup_cb_t) (dwc_otg_pcd_t * pcd, uint8_t * bytes);
  50737. +/** This is called whenever the device has been disconnected. The function
  50738. + * driver should take appropriate action to clean up all pending requests in the
  50739. + * PCD Core, remove all endpoints (except ep0), and initialize back to reset
  50740. + * state. */
  50741. +typedef int (*dwc_disconnect_cb_t) (dwc_otg_pcd_t * pcd);
  50742. +/** This function is called when device has been connected. */
  50743. +typedef int (*dwc_connect_cb_t) (dwc_otg_pcd_t * pcd, int speed);
  50744. +/** This function is called when device has been suspended */
  50745. +typedef int (*dwc_suspend_cb_t) (dwc_otg_pcd_t * pcd);
  50746. +/** This function is called when device has received LPM tokens, i.e.
  50747. + * device has been sent to sleep state. */
  50748. +typedef int (*dwc_sleep_cb_t) (dwc_otg_pcd_t * pcd);
  50749. +/** This function is called when device has been resumed
  50750. + * from suspend(L2) or L1 sleep state. */
  50751. +typedef int (*dwc_resume_cb_t) (dwc_otg_pcd_t * pcd);
  50752. +/** This function is called whenever hnp params has been changed.
  50753. + * User can call get_b_hnp_enable, get_a_hnp_support, get_a_alt_hnp_support functions
  50754. + * to get hnp parameters. */
  50755. +typedef int (*dwc_hnp_params_changed_cb_t) (dwc_otg_pcd_t * pcd);
  50756. +/** This function is called whenever USB RESET is detected. */
  50757. +typedef int (*dwc_reset_cb_t) (dwc_otg_pcd_t * pcd);
  50758. +
  50759. +typedef int (*cfi_setup_cb_t) (dwc_otg_pcd_t * pcd, void *ctrl_req_bytes);
  50760. +
  50761. +/**
  50762. + *
  50763. + * @param ep_handle Void pointer to the usb_ep structure
  50764. + * @param ereq_port Pointer to the extended request structure created in the
  50765. + * portable part.
  50766. + */
  50767. +typedef int (*xiso_completion_cb_t) (dwc_otg_pcd_t * pcd, void *ep_handle,
  50768. + void *req_handle, int32_t status,
  50769. + void *ereq_port);
  50770. +/** Function Driver Ops Data Structure */
  50771. +struct dwc_otg_pcd_function_ops {
  50772. + dwc_connect_cb_t connect;
  50773. + dwc_disconnect_cb_t disconnect;
  50774. + dwc_setup_cb_t setup;
  50775. + dwc_completion_cb_t complete;
  50776. + dwc_isoc_completion_cb_t isoc_complete;
  50777. + dwc_suspend_cb_t suspend;
  50778. + dwc_sleep_cb_t sleep;
  50779. + dwc_resume_cb_t resume;
  50780. + dwc_reset_cb_t reset;
  50781. + dwc_hnp_params_changed_cb_t hnp_changed;
  50782. + cfi_setup_cb_t cfi_setup;
  50783. +#ifdef DWC_UTE_PER_IO
  50784. + xiso_completion_cb_t xisoc_complete;
  50785. +#endif
  50786. +};
  50787. +/** @} */
  50788. +
  50789. +/** @name Function Driver Functions */
  50790. +/** @{ */
  50791. +
  50792. +/** Call this function to get pointer on dwc_otg_pcd_t,
  50793. + * this pointer will be used for all PCD API functions.
  50794. + *
  50795. + * @param core_if The DWC_OTG Core
  50796. + */
  50797. +extern dwc_otg_pcd_t *dwc_otg_pcd_init(dwc_otg_device_t *otg_dev);
  50798. +
  50799. +/** Frees PCD allocated by dwc_otg_pcd_init
  50800. + *
  50801. + * @param pcd The PCD
  50802. + */
  50803. +extern void dwc_otg_pcd_remove(dwc_otg_pcd_t * pcd);
  50804. +
  50805. +/** Call this to bind the function driver to the PCD Core.
  50806. + *
  50807. + * @param pcd Pointer on dwc_otg_pcd_t returned by dwc_otg_pcd_init function.
  50808. + * @param fops The Function Driver Ops data structure containing pointers to all callbacks.
  50809. + */
  50810. +extern void dwc_otg_pcd_start(dwc_otg_pcd_t * pcd,
  50811. + const struct dwc_otg_pcd_function_ops *fops);
  50812. +
  50813. +/** Enables an endpoint for use. This function enables an endpoint in
  50814. + * the PCD. The endpoint is described by the ep_desc which has the
  50815. + * same format as a USB ep descriptor. The ep_handle parameter is used to refer
  50816. + * to the endpoint from other API functions and in callbacks. Normally this
  50817. + * should be called after a SET_CONFIGURATION/SET_INTERFACE to configure the
  50818. + * core for that interface.
  50819. + *
  50820. + * Returns -DWC_E_INVALID if invalid parameters were passed.
  50821. + * Returns -DWC_E_SHUTDOWN if any other error ocurred.
  50822. + * Returns 0 on success.
  50823. + *
  50824. + * @param pcd The PCD
  50825. + * @param ep_desc Endpoint descriptor
  50826. + * @param usb_ep Handle on endpoint, that will be used to identify endpoint.
  50827. + */
  50828. +extern int dwc_otg_pcd_ep_enable(dwc_otg_pcd_t * pcd,
  50829. + const uint8_t * ep_desc, void *usb_ep);
  50830. +
  50831. +/** Disable the endpoint referenced by ep_handle.
  50832. + *
  50833. + * Returns -DWC_E_INVALID if invalid parameters were passed.
  50834. + * Returns -DWC_E_SHUTDOWN if any other error occurred.
  50835. + * Returns 0 on success. */
  50836. +extern int dwc_otg_pcd_ep_disable(dwc_otg_pcd_t * pcd, void *ep_handle);
  50837. +
  50838. +/** Queue a data transfer request on the endpoint referenced by ep_handle.
  50839. + * After the transfer is completes, the complete callback will be called with
  50840. + * the request status.
  50841. + *
  50842. + * @param pcd The PCD
  50843. + * @param ep_handle The handle of the endpoint
  50844. + * @param buf The buffer for the data
  50845. + * @param dma_buf The DMA buffer for the data
  50846. + * @param buflen The length of the data transfer
  50847. + * @param zero Specifies whether to send zero length last packet.
  50848. + * @param req_handle Set this handle to any value to use to reference this
  50849. + * request in the ep_dequeue function or from the complete callback
  50850. + * @param atomic_alloc If driver need to perform atomic allocations
  50851. + * for internal data structures.
  50852. + *
  50853. + * Returns -DWC_E_INVALID if invalid parameters were passed.
  50854. + * Returns -DWC_E_SHUTDOWN if any other error ocurred.
  50855. + * Returns 0 on success. */
  50856. +extern int dwc_otg_pcd_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
  50857. + uint8_t * buf, dwc_dma_t dma_buf,
  50858. + uint32_t buflen, int zero, void *req_handle,
  50859. + int atomic_alloc);
  50860. +#ifdef DWC_UTE_PER_IO
  50861. +/**
  50862. + *
  50863. + * @param ereq_nonport Pointer to the extended request part of the
  50864. + * usb_request structure defined in usb_gadget.h file.
  50865. + */
  50866. +extern int dwc_otg_pcd_xiso_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
  50867. + uint8_t * buf, dwc_dma_t dma_buf,
  50868. + uint32_t buflen, int zero,
  50869. + void *req_handle, int atomic_alloc,
  50870. + void *ereq_nonport);
  50871. +
  50872. +#endif
  50873. +
  50874. +/** De-queue the specified data transfer that has not yet completed.
  50875. + *
  50876. + * Returns -DWC_E_INVALID if invalid parameters were passed.
  50877. + * Returns -DWC_E_SHUTDOWN if any other error ocurred.
  50878. + * Returns 0 on success. */
  50879. +extern int dwc_otg_pcd_ep_dequeue(dwc_otg_pcd_t * pcd, void *ep_handle,
  50880. + void *req_handle);
  50881. +
  50882. +/** Halt (STALL) an endpoint or clear it.
  50883. + *
  50884. + * Returns -DWC_E_INVALID if invalid parameters were passed.
  50885. + * Returns -DWC_E_SHUTDOWN if any other error ocurred.
  50886. + * Returns -DWC_E_AGAIN if the STALL cannot be sent and must be tried again later
  50887. + * Returns 0 on success. */
  50888. +extern int dwc_otg_pcd_ep_halt(dwc_otg_pcd_t * pcd, void *ep_handle, int value);
  50889. +
  50890. +/** This function */
  50891. +extern int dwc_otg_pcd_ep_wedge(dwc_otg_pcd_t * pcd, void *ep_handle);
  50892. +
  50893. +/** This function should be called on every hardware interrupt */
  50894. +extern int32_t dwc_otg_pcd_handle_intr(dwc_otg_pcd_t * pcd);
  50895. +
  50896. +/** This function returns current frame number */
  50897. +extern int dwc_otg_pcd_get_frame_number(dwc_otg_pcd_t * pcd);
  50898. +
  50899. +/**
  50900. + * Start isochronous transfers on the endpoint referenced by ep_handle.
  50901. + * For isochronous transfers duble buffering is used.
  50902. + * After processing each of buffers comlete callback will be called with
  50903. + * status for each transaction.
  50904. + *
  50905. + * @param pcd The PCD
  50906. + * @param ep_handle The handle of the endpoint
  50907. + * @param buf0 The virtual address of first data buffer
  50908. + * @param buf1 The virtual address of second data buffer
  50909. + * @param dma0 The DMA address of first data buffer
  50910. + * @param dma1 The DMA address of second data buffer
  50911. + * @param sync_frame Data pattern frame number
  50912. + * @param dp_frame Data size for pattern frame
  50913. + * @param data_per_frame Data size for regular frame
  50914. + * @param start_frame Frame number to start transfers, if -1 then start transfers ASAP.
  50915. + * @param buf_proc_intrvl Interval of ISOC Buffer processing
  50916. + * @param req_handle Handle of ISOC request
  50917. + * @param atomic_alloc Specefies whether to perform atomic allocation for
  50918. + * internal data structures.
  50919. + *
  50920. + * Returns -DWC_E_NO_MEMORY if there is no enough memory.
  50921. + * Returns -DWC_E_INVALID if incorrect arguments are passed to the function.
  50922. + * Returns -DW_E_SHUTDOWN for any other error.
  50923. + * Returns 0 on success
  50924. + */
  50925. +extern int dwc_otg_pcd_iso_ep_start(dwc_otg_pcd_t * pcd, void *ep_handle,
  50926. + uint8_t * buf0, uint8_t * buf1,
  50927. + dwc_dma_t dma0, dwc_dma_t dma1,
  50928. + int sync_frame, int dp_frame,
  50929. + int data_per_frame, int start_frame,
  50930. + int buf_proc_intrvl, void *req_handle,
  50931. + int atomic_alloc);
  50932. +
  50933. +/** Stop ISOC transfers on endpoint referenced by ep_handle.
  50934. + *
  50935. + * @param pcd The PCD
  50936. + * @param ep_handle The handle of the endpoint
  50937. + * @param req_handle Handle of ISOC request
  50938. + *
  50939. + * Returns -DWC_E_INVALID if incorrect arguments are passed to the function
  50940. + * Returns 0 on success
  50941. + */
  50942. +int dwc_otg_pcd_iso_ep_stop(dwc_otg_pcd_t * pcd, void *ep_handle,
  50943. + void *req_handle);
  50944. +
  50945. +/** Get ISOC packet status.
  50946. + *
  50947. + * @param pcd The PCD
  50948. + * @param ep_handle The handle of the endpoint
  50949. + * @param iso_req_handle Isochronoush request handle
  50950. + * @param packet Number of packet
  50951. + * @param status Out parameter for returning status
  50952. + * @param actual Out parameter for returning actual length
  50953. + * @param offset Out parameter for returning offset
  50954. + *
  50955. + */
  50956. +extern void dwc_otg_pcd_get_iso_packet_params(dwc_otg_pcd_t * pcd,
  50957. + void *ep_handle,
  50958. + void *iso_req_handle, int packet,
  50959. + int *status, int *actual,
  50960. + int *offset);
  50961. +
  50962. +/** Get ISOC packet count.
  50963. + *
  50964. + * @param pcd The PCD
  50965. + * @param ep_handle The handle of the endpoint
  50966. + * @param iso_req_handle
  50967. + */
  50968. +extern int dwc_otg_pcd_get_iso_packet_count(dwc_otg_pcd_t * pcd,
  50969. + void *ep_handle,
  50970. + void *iso_req_handle);
  50971. +
  50972. +/** This function starts the SRP Protocol if no session is in progress. If
  50973. + * a session is already in progress, but the device is suspended,
  50974. + * remote wakeup signaling is started.
  50975. + */
  50976. +extern int dwc_otg_pcd_wakeup(dwc_otg_pcd_t * pcd);
  50977. +
  50978. +/** This function returns 1 if LPM support is enabled, and 0 otherwise. */
  50979. +extern int dwc_otg_pcd_is_lpm_enabled(dwc_otg_pcd_t * pcd);
  50980. +
  50981. +/** This function returns 1 if remote wakeup is allowed and 0, otherwise. */
  50982. +extern int dwc_otg_pcd_get_rmwkup_enable(dwc_otg_pcd_t * pcd);
  50983. +
  50984. +/** Initiate SRP */
  50985. +extern void dwc_otg_pcd_initiate_srp(dwc_otg_pcd_t * pcd);
  50986. +
  50987. +/** Starts remote wakeup signaling. */
  50988. +extern void dwc_otg_pcd_remote_wakeup(dwc_otg_pcd_t * pcd, int set);
  50989. +
  50990. +/** Starts micorsecond soft disconnect. */
  50991. +extern void dwc_otg_pcd_disconnect_us(dwc_otg_pcd_t * pcd, int no_of_usecs);
  50992. +/** This function returns whether device is dualspeed.*/
  50993. +extern uint32_t dwc_otg_pcd_is_dualspeed(dwc_otg_pcd_t * pcd);
  50994. +
  50995. +/** This function returns whether device is otg. */
  50996. +extern uint32_t dwc_otg_pcd_is_otg(dwc_otg_pcd_t * pcd);
  50997. +
  50998. +/** These functions allow to get hnp parameters */
  50999. +extern uint32_t get_b_hnp_enable(dwc_otg_pcd_t * pcd);
  51000. +extern uint32_t get_a_hnp_support(dwc_otg_pcd_t * pcd);
  51001. +extern uint32_t get_a_alt_hnp_support(dwc_otg_pcd_t * pcd);
  51002. +
  51003. +/** CFI specific Interface functions */
  51004. +/** Allocate a cfi buffer */
  51005. +extern uint8_t *cfiw_ep_alloc_buffer(dwc_otg_pcd_t * pcd, void *pep,
  51006. + dwc_dma_t * addr, size_t buflen,
  51007. + int flags);
  51008. +
  51009. +/******************************************************************************/
  51010. +
  51011. +/** @} */
  51012. +
  51013. +#endif /* __DWC_PCD_IF_H__ */
  51014. +
  51015. +#endif /* DWC_HOST_ONLY */
  51016. --- /dev/null
  51017. +++ b/drivers/usb/host/dwc_otg/dwc_otg_pcd_intr.c
  51018. @@ -0,0 +1,5148 @@
  51019. +/* ==========================================================================
  51020. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd_intr.c $
  51021. + * $Revision: #116 $
  51022. + * $Date: 2012/08/10 $
  51023. + * $Change: 2047372 $
  51024. + *
  51025. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  51026. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  51027. + * otherwise expressly agreed to in writing between Synopsys and you.
  51028. + *
  51029. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  51030. + * any End User Software License Agreement or Agreement for Licensed Product
  51031. + * with Synopsys or any supplement thereto. You are permitted to use and
  51032. + * redistribute this Software in source and binary forms, with or without
  51033. + * modification, provided that redistributions of source code must retain this
  51034. + * notice. You may not view, use, disclose, copy or distribute this file or
  51035. + * any information contained herein except pursuant to this license grant from
  51036. + * Synopsys. If you do not agree with this notice, including the disclaimer
  51037. + * below, then you are not authorized to use the Software.
  51038. + *
  51039. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  51040. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  51041. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  51042. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  51043. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  51044. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  51045. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  51046. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  51047. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  51048. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  51049. + * DAMAGE.
  51050. + * ========================================================================== */
  51051. +#ifndef DWC_HOST_ONLY
  51052. +
  51053. +#include "dwc_otg_pcd.h"
  51054. +
  51055. +#ifdef DWC_UTE_CFI
  51056. +#include "dwc_otg_cfi.h"
  51057. +#endif
  51058. +
  51059. +#ifdef DWC_UTE_PER_IO
  51060. +extern void complete_xiso_ep(dwc_otg_pcd_ep_t * ep);
  51061. +#endif
  51062. +//#define PRINT_CFI_DMA_DESCS
  51063. +
  51064. +#define DEBUG_EP0
  51065. +
  51066. +/**
  51067. + * This function updates OTG.
  51068. + */
  51069. +static void dwc_otg_pcd_update_otg(dwc_otg_pcd_t * pcd, const unsigned reset)
  51070. +{
  51071. +
  51072. + if (reset) {
  51073. + pcd->b_hnp_enable = 0;
  51074. + pcd->a_hnp_support = 0;
  51075. + pcd->a_alt_hnp_support = 0;
  51076. + }
  51077. +
  51078. + if (pcd->fops->hnp_changed) {
  51079. + pcd->fops->hnp_changed(pcd);
  51080. + }
  51081. +}
  51082. +
  51083. +/** @file
  51084. + * This file contains the implementation of the PCD Interrupt handlers.
  51085. + *
  51086. + * The PCD handles the device interrupts. Many conditions can cause a
  51087. + * device interrupt. When an interrupt occurs, the device interrupt
  51088. + * service routine determines the cause of the interrupt and
  51089. + * dispatches handling to the appropriate function. These interrupt
  51090. + * handling functions are described below.
  51091. + * All interrupt registers are processed from LSB to MSB.
  51092. + */
  51093. +
  51094. +/**
  51095. + * This function prints the ep0 state for debug purposes.
  51096. + */
  51097. +static inline void print_ep0_state(dwc_otg_pcd_t * pcd)
  51098. +{
  51099. +#ifdef DEBUG
  51100. + char str[40];
  51101. +
  51102. + switch (pcd->ep0state) {
  51103. + case EP0_DISCONNECT:
  51104. + dwc_strcpy(str, "EP0_DISCONNECT");
  51105. + break;
  51106. + case EP0_IDLE:
  51107. + dwc_strcpy(str, "EP0_IDLE");
  51108. + break;
  51109. + case EP0_IN_DATA_PHASE:
  51110. + dwc_strcpy(str, "EP0_IN_DATA_PHASE");
  51111. + break;
  51112. + case EP0_OUT_DATA_PHASE:
  51113. + dwc_strcpy(str, "EP0_OUT_DATA_PHASE");
  51114. + break;
  51115. + case EP0_IN_STATUS_PHASE:
  51116. + dwc_strcpy(str, "EP0_IN_STATUS_PHASE");
  51117. + break;
  51118. + case EP0_OUT_STATUS_PHASE:
  51119. + dwc_strcpy(str, "EP0_OUT_STATUS_PHASE");
  51120. + break;
  51121. + case EP0_STALL:
  51122. + dwc_strcpy(str, "EP0_STALL");
  51123. + break;
  51124. + default:
  51125. + dwc_strcpy(str, "EP0_INVALID");
  51126. + }
  51127. +
  51128. + DWC_DEBUGPL(DBG_ANY, "%s(%d)\n", str, pcd->ep0state);
  51129. +#endif
  51130. +}
  51131. +
  51132. +/**
  51133. + * This function calculate the size of the payload in the memory
  51134. + * for out endpoints and prints size for debug purposes(used in
  51135. + * 2.93a DevOutNak feature).
  51136. + */
  51137. +static inline void print_memory_payload(dwc_otg_pcd_t * pcd, dwc_ep_t * ep)
  51138. +{
  51139. +#ifdef DEBUG
  51140. + deptsiz_data_t deptsiz_init = {.d32 = 0 };
  51141. + deptsiz_data_t deptsiz_updt = {.d32 = 0 };
  51142. + int pack_num;
  51143. + unsigned payload;
  51144. +
  51145. + deptsiz_init.d32 = pcd->core_if->start_doeptsiz_val[ep->num];
  51146. + deptsiz_updt.d32 =
  51147. + DWC_READ_REG32(&pcd->core_if->dev_if->
  51148. + out_ep_regs[ep->num]->doeptsiz);
  51149. + /* Payload will be */
  51150. + payload = deptsiz_init.b.xfersize - deptsiz_updt.b.xfersize;
  51151. + /* Packet count is decremented every time a packet
  51152. + * is written to the RxFIFO not in to the external memory
  51153. + * So, if payload == 0, then it means no packet was sent to ext memory*/
  51154. + pack_num = (!payload) ? 0 : (deptsiz_init.b.pktcnt - deptsiz_updt.b.pktcnt);
  51155. + DWC_DEBUGPL(DBG_PCDV,
  51156. + "Payload for EP%d-%s\n",
  51157. + ep->num, (ep->is_in ? "IN" : "OUT"));
  51158. + DWC_DEBUGPL(DBG_PCDV,
  51159. + "Number of transfered bytes = 0x%08x\n", payload);
  51160. + DWC_DEBUGPL(DBG_PCDV,
  51161. + "Number of transfered packets = %d\n", pack_num);
  51162. +#endif
  51163. +}
  51164. +
  51165. +
  51166. +#ifdef DWC_UTE_CFI
  51167. +static inline void print_desc(struct dwc_otg_dma_desc *ddesc,
  51168. + const uint8_t * epname, int descnum)
  51169. +{
  51170. + CFI_INFO
  51171. + ("%s DMA_DESC(%d) buf=0x%08x bytes=0x%04x; sp=0x%x; l=0x%x; sts=0x%02x; bs=0x%02x\n",
  51172. + epname, descnum, ddesc->buf, ddesc->status.b.bytes,
  51173. + ddesc->status.b.sp, ddesc->status.b.l, ddesc->status.b.sts,
  51174. + ddesc->status.b.bs);
  51175. +}
  51176. +#endif
  51177. +
  51178. +/**
  51179. + * This function returns pointer to in ep struct with number ep_num
  51180. + */
  51181. +static inline dwc_otg_pcd_ep_t *get_in_ep(dwc_otg_pcd_t * pcd, uint32_t ep_num)
  51182. +{
  51183. + int i;
  51184. + int num_in_eps = GET_CORE_IF(pcd)->dev_if->num_in_eps;
  51185. + if (ep_num == 0) {
  51186. + return &pcd->ep0;
  51187. + } else {
  51188. + for (i = 0; i < num_in_eps; ++i) {
  51189. + if (pcd->in_ep[i].dwc_ep.num == ep_num)
  51190. + return &pcd->in_ep[i];
  51191. + }
  51192. + return 0;
  51193. + }
  51194. +}
  51195. +
  51196. +/**
  51197. + * This function returns pointer to out ep struct with number ep_num
  51198. + */
  51199. +static inline dwc_otg_pcd_ep_t *get_out_ep(dwc_otg_pcd_t * pcd, uint32_t ep_num)
  51200. +{
  51201. + int i;
  51202. + int num_out_eps = GET_CORE_IF(pcd)->dev_if->num_out_eps;
  51203. + if (ep_num == 0) {
  51204. + return &pcd->ep0;
  51205. + } else {
  51206. + for (i = 0; i < num_out_eps; ++i) {
  51207. + if (pcd->out_ep[i].dwc_ep.num == ep_num)
  51208. + return &pcd->out_ep[i];
  51209. + }
  51210. + return 0;
  51211. + }
  51212. +}
  51213. +
  51214. +/**
  51215. + * This functions gets a pointer to an EP from the wIndex address
  51216. + * value of the control request.
  51217. + */
  51218. +dwc_otg_pcd_ep_t *get_ep_by_addr(dwc_otg_pcd_t * pcd, u16 wIndex)
  51219. +{
  51220. + dwc_otg_pcd_ep_t *ep;
  51221. + uint32_t ep_num = UE_GET_ADDR(wIndex);
  51222. +
  51223. + if (ep_num == 0) {
  51224. + ep = &pcd->ep0;
  51225. + } else if (UE_GET_DIR(wIndex) == UE_DIR_IN) { /* in ep */
  51226. + ep = &pcd->in_ep[ep_num - 1];
  51227. + } else {
  51228. + ep = &pcd->out_ep[ep_num - 1];
  51229. + }
  51230. +
  51231. + return ep;
  51232. +}
  51233. +
  51234. +/**
  51235. + * This function checks the EP request queue, if the queue is not
  51236. + * empty the next request is started.
  51237. + */
  51238. +void start_next_request(dwc_otg_pcd_ep_t * ep)
  51239. +{
  51240. + dwc_otg_pcd_request_t *req = 0;
  51241. + uint32_t max_transfer =
  51242. + GET_CORE_IF(ep->pcd)->core_params->max_transfer_size;
  51243. +
  51244. +#ifdef DWC_UTE_CFI
  51245. + struct dwc_otg_pcd *pcd;
  51246. + pcd = ep->pcd;
  51247. +#endif
  51248. +
  51249. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  51250. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  51251. +
  51252. +#ifdef DWC_UTE_CFI
  51253. + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
  51254. + ep->dwc_ep.cfi_req_len = req->length;
  51255. + pcd->cfi->ops.build_descriptors(pcd->cfi, pcd, ep, req);
  51256. + } else {
  51257. +#endif
  51258. + /* Setup and start the Transfer */
  51259. + if (req->dw_align_buf) {
  51260. + ep->dwc_ep.dma_addr = req->dw_align_buf_dma;
  51261. + ep->dwc_ep.start_xfer_buff = req->dw_align_buf;
  51262. + ep->dwc_ep.xfer_buff = req->dw_align_buf;
  51263. + } else {
  51264. + ep->dwc_ep.dma_addr = req->dma;
  51265. + ep->dwc_ep.start_xfer_buff = req->buf;
  51266. + ep->dwc_ep.xfer_buff = req->buf;
  51267. + }
  51268. + ep->dwc_ep.sent_zlp = 0;
  51269. + ep->dwc_ep.total_len = req->length;
  51270. + ep->dwc_ep.xfer_len = 0;
  51271. + ep->dwc_ep.xfer_count = 0;
  51272. +
  51273. + ep->dwc_ep.maxxfer = max_transfer;
  51274. + if (GET_CORE_IF(ep->pcd)->dma_desc_enable) {
  51275. + uint32_t out_max_xfer = DDMA_MAX_TRANSFER_SIZE
  51276. + - (DDMA_MAX_TRANSFER_SIZE % 4);
  51277. + if (ep->dwc_ep.is_in) {
  51278. + if (ep->dwc_ep.maxxfer >
  51279. + DDMA_MAX_TRANSFER_SIZE) {
  51280. + ep->dwc_ep.maxxfer =
  51281. + DDMA_MAX_TRANSFER_SIZE;
  51282. + }
  51283. + } else {
  51284. + if (ep->dwc_ep.maxxfer > out_max_xfer) {
  51285. + ep->dwc_ep.maxxfer =
  51286. + out_max_xfer;
  51287. + }
  51288. + }
  51289. + }
  51290. + if (ep->dwc_ep.maxxfer < ep->dwc_ep.total_len) {
  51291. + ep->dwc_ep.maxxfer -=
  51292. + (ep->dwc_ep.maxxfer % ep->dwc_ep.maxpacket);
  51293. + }
  51294. + if (req->sent_zlp) {
  51295. + if ((ep->dwc_ep.total_len %
  51296. + ep->dwc_ep.maxpacket == 0)
  51297. + && (ep->dwc_ep.total_len != 0)) {
  51298. + ep->dwc_ep.sent_zlp = 1;
  51299. + }
  51300. +
  51301. + }
  51302. +#ifdef DWC_UTE_CFI
  51303. + }
  51304. +#endif
  51305. + dwc_otg_ep_start_transfer(GET_CORE_IF(ep->pcd), &ep->dwc_ep);
  51306. + } else if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  51307. + DWC_PRINTF("There are no more ISOC requests \n");
  51308. + ep->dwc_ep.frame_num = 0xFFFFFFFF;
  51309. + }
  51310. +}
  51311. +
  51312. +/**
  51313. + * This function handles the SOF Interrupts. At this time the SOF
  51314. + * Interrupt is disabled.
  51315. + */
  51316. +int32_t dwc_otg_pcd_handle_sof_intr(dwc_otg_pcd_t * pcd)
  51317. +{
  51318. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  51319. +
  51320. + gintsts_data_t gintsts;
  51321. +
  51322. + DWC_DEBUGPL(DBG_PCD, "SOF\n");
  51323. +
  51324. + /* Clear interrupt */
  51325. + gintsts.d32 = 0;
  51326. + gintsts.b.sofintr = 1;
  51327. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  51328. +
  51329. + return 1;
  51330. +}
  51331. +
  51332. +/**
  51333. + * This function handles the Rx Status Queue Level Interrupt, which
  51334. + * indicates that there is a least one packet in the Rx FIFO. The
  51335. + * packets are moved from the FIFO to memory, where they will be
  51336. + * processed when the Endpoint Interrupt Register indicates Transfer
  51337. + * Complete or SETUP Phase Done.
  51338. + *
  51339. + * Repeat the following until the Rx Status Queue is empty:
  51340. + * -# Read the Receive Status Pop Register (GRXSTSP) to get Packet
  51341. + * info
  51342. + * -# If Receive FIFO is empty then skip to step Clear the interrupt
  51343. + * and exit
  51344. + * -# If SETUP Packet call dwc_otg_read_setup_packet to copy the
  51345. + * SETUP data to the buffer
  51346. + * -# If OUT Data Packet call dwc_otg_read_packet to copy the data
  51347. + * to the destination buffer
  51348. + */
  51349. +int32_t dwc_otg_pcd_handle_rx_status_q_level_intr(dwc_otg_pcd_t * pcd)
  51350. +{
  51351. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  51352. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  51353. + gintmsk_data_t gintmask = {.d32 = 0 };
  51354. + device_grxsts_data_t status;
  51355. + dwc_otg_pcd_ep_t *ep;
  51356. + gintsts_data_t gintsts;
  51357. +#ifdef DEBUG
  51358. + static char *dpid_str[] = { "D0", "D2", "D1", "MDATA" };
  51359. +#endif
  51360. +
  51361. + //DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, _pcd);
  51362. + /* Disable the Rx Status Queue Level interrupt */
  51363. + gintmask.b.rxstsqlvl = 1;
  51364. + DWC_MODIFY_REG32(&global_regs->gintmsk, gintmask.d32, 0);
  51365. +
  51366. + /* Get the Status from the top of the FIFO */
  51367. + status.d32 = DWC_READ_REG32(&global_regs->grxstsp);
  51368. +
  51369. + DWC_DEBUGPL(DBG_PCD, "EP:%d BCnt:%d DPID:%s "
  51370. + "pktsts:%x Frame:%d(0x%0x)\n",
  51371. + status.b.epnum, status.b.bcnt,
  51372. + dpid_str[status.b.dpid],
  51373. + status.b.pktsts, status.b.fn, status.b.fn);
  51374. + /* Get pointer to EP structure */
  51375. + ep = get_out_ep(pcd, status.b.epnum);
  51376. +
  51377. + switch (status.b.pktsts) {
  51378. + case DWC_DSTS_GOUT_NAK:
  51379. + DWC_DEBUGPL(DBG_PCDV, "Global OUT NAK\n");
  51380. + break;
  51381. + case DWC_STS_DATA_UPDT:
  51382. + DWC_DEBUGPL(DBG_PCDV, "OUT Data Packet\n");
  51383. + if (status.b.bcnt && ep->dwc_ep.xfer_buff) {
  51384. + /** @todo NGS Check for buffer overflow? */
  51385. + dwc_otg_read_packet(core_if,
  51386. + ep->dwc_ep.xfer_buff,
  51387. + status.b.bcnt);
  51388. + ep->dwc_ep.xfer_count += status.b.bcnt;
  51389. + ep->dwc_ep.xfer_buff += status.b.bcnt;
  51390. + }
  51391. + break;
  51392. + case DWC_STS_XFER_COMP:
  51393. + DWC_DEBUGPL(DBG_PCDV, "OUT Complete\n");
  51394. + break;
  51395. + case DWC_DSTS_SETUP_COMP:
  51396. +#ifdef DEBUG_EP0
  51397. + DWC_DEBUGPL(DBG_PCDV, "Setup Complete\n");
  51398. +#endif
  51399. + break;
  51400. + case DWC_DSTS_SETUP_UPDT:
  51401. + dwc_otg_read_setup_packet(core_if, pcd->setup_pkt->d32);
  51402. +#ifdef DEBUG_EP0
  51403. + DWC_DEBUGPL(DBG_PCD,
  51404. + "SETUP PKT: %02x.%02x v%04x i%04x l%04x\n",
  51405. + pcd->setup_pkt->req.bmRequestType,
  51406. + pcd->setup_pkt->req.bRequest,
  51407. + UGETW(pcd->setup_pkt->req.wValue),
  51408. + UGETW(pcd->setup_pkt->req.wIndex),
  51409. + UGETW(pcd->setup_pkt->req.wLength));
  51410. +#endif
  51411. + ep->dwc_ep.xfer_count += status.b.bcnt;
  51412. + break;
  51413. + default:
  51414. + DWC_DEBUGPL(DBG_PCDV, "Invalid Packet Status (0x%0x)\n",
  51415. + status.b.pktsts);
  51416. + break;
  51417. + }
  51418. +
  51419. + /* Enable the Rx Status Queue Level interrupt */
  51420. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmask.d32);
  51421. + /* Clear interrupt */
  51422. + gintsts.d32 = 0;
  51423. + gintsts.b.rxstsqlvl = 1;
  51424. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  51425. +
  51426. + //DWC_DEBUGPL(DBG_PCDV, "EXIT: %s\n", __func__);
  51427. + return 1;
  51428. +}
  51429. +
  51430. +/**
  51431. + * This function examines the Device IN Token Learning Queue to
  51432. + * determine the EP number of the last IN token received. This
  51433. + * implementation is for the Mass Storage device where there are only
  51434. + * 2 IN EPs (Control-IN and BULK-IN).
  51435. + *
  51436. + * The EP numbers for the first six IN Tokens are in DTKNQR1 and there
  51437. + * are 8 EP Numbers in each of the other possible DTKNQ Registers.
  51438. + *
  51439. + * @param core_if Programming view of DWC_otg controller.
  51440. + *
  51441. + */
  51442. +static inline int get_ep_of_last_in_token(dwc_otg_core_if_t * core_if)
  51443. +{
  51444. + dwc_otg_device_global_regs_t *dev_global_regs =
  51445. + core_if->dev_if->dev_global_regs;
  51446. + const uint32_t TOKEN_Q_DEPTH = core_if->hwcfg2.b.dev_token_q_depth;
  51447. + /* Number of Token Queue Registers */
  51448. + const int DTKNQ_REG_CNT = (TOKEN_Q_DEPTH + 7) / 8;
  51449. + dtknq1_data_t dtknqr1;
  51450. + uint32_t in_tkn_epnums[4];
  51451. + int ndx = 0;
  51452. + int i = 0;
  51453. + volatile uint32_t *addr = &dev_global_regs->dtknqr1;
  51454. + int epnum = 0;
  51455. +
  51456. + //DWC_DEBUGPL(DBG_PCD,"dev_token_q_depth=%d\n",TOKEN_Q_DEPTH);
  51457. +
  51458. + /* Read the DTKNQ Registers */
  51459. + for (i = 0; i < DTKNQ_REG_CNT; i++) {
  51460. + in_tkn_epnums[i] = DWC_READ_REG32(addr);
  51461. + DWC_DEBUGPL(DBG_PCDV, "DTKNQR%d=0x%08x\n", i + 1,
  51462. + in_tkn_epnums[i]);
  51463. + if (addr == &dev_global_regs->dvbusdis) {
  51464. + addr = &dev_global_regs->dtknqr3_dthrctl;
  51465. + } else {
  51466. + ++addr;
  51467. + }
  51468. +
  51469. + }
  51470. +
  51471. + /* Copy the DTKNQR1 data to the bit field. */
  51472. + dtknqr1.d32 = in_tkn_epnums[0];
  51473. + /* Get the EP numbers */
  51474. + in_tkn_epnums[0] = dtknqr1.b.epnums0_5;
  51475. + ndx = dtknqr1.b.intknwptr - 1;
  51476. +
  51477. + //DWC_DEBUGPL(DBG_PCDV,"ndx=%d\n",ndx);
  51478. + if (ndx == -1) {
  51479. + /** @todo Find a simpler way to calculate the max
  51480. + * queue position.*/
  51481. + int cnt = TOKEN_Q_DEPTH;
  51482. + if (TOKEN_Q_DEPTH <= 6) {
  51483. + cnt = TOKEN_Q_DEPTH - 1;
  51484. + } else if (TOKEN_Q_DEPTH <= 14) {
  51485. + cnt = TOKEN_Q_DEPTH - 7;
  51486. + } else if (TOKEN_Q_DEPTH <= 22) {
  51487. + cnt = TOKEN_Q_DEPTH - 15;
  51488. + } else {
  51489. + cnt = TOKEN_Q_DEPTH - 23;
  51490. + }
  51491. + epnum = (in_tkn_epnums[DTKNQ_REG_CNT - 1] >> (cnt * 4)) & 0xF;
  51492. + } else {
  51493. + if (ndx <= 5) {
  51494. + epnum = (in_tkn_epnums[0] >> (ndx * 4)) & 0xF;
  51495. + } else if (ndx <= 13) {
  51496. + ndx -= 6;
  51497. + epnum = (in_tkn_epnums[1] >> (ndx * 4)) & 0xF;
  51498. + } else if (ndx <= 21) {
  51499. + ndx -= 14;
  51500. + epnum = (in_tkn_epnums[2] >> (ndx * 4)) & 0xF;
  51501. + } else if (ndx <= 29) {
  51502. + ndx -= 22;
  51503. + epnum = (in_tkn_epnums[3] >> (ndx * 4)) & 0xF;
  51504. + }
  51505. + }
  51506. + //DWC_DEBUGPL(DBG_PCD,"epnum=%d\n",epnum);
  51507. + return epnum;
  51508. +}
  51509. +
  51510. +/**
  51511. + * This interrupt occurs when the non-periodic Tx FIFO is half-empty.
  51512. + * The active request is checked for the next packet to be loaded into
  51513. + * the non-periodic Tx FIFO.
  51514. + */
  51515. +int32_t dwc_otg_pcd_handle_np_tx_fifo_empty_intr(dwc_otg_pcd_t * pcd)
  51516. +{
  51517. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  51518. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  51519. + dwc_otg_dev_in_ep_regs_t *ep_regs;
  51520. + gnptxsts_data_t txstatus = {.d32 = 0 };
  51521. + gintsts_data_t gintsts;
  51522. +
  51523. + int epnum = 0;
  51524. + dwc_otg_pcd_ep_t *ep = 0;
  51525. + uint32_t len = 0;
  51526. + int dwords;
  51527. +
  51528. + /* Get the epnum from the IN Token Learning Queue. */
  51529. + epnum = get_ep_of_last_in_token(core_if);
  51530. + ep = get_in_ep(pcd, epnum);
  51531. +
  51532. + DWC_DEBUGPL(DBG_PCD, "NP TxFifo Empty: %d \n", epnum);
  51533. +
  51534. + ep_regs = core_if->dev_if->in_ep_regs[epnum];
  51535. +
  51536. + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
  51537. + if (len > ep->dwc_ep.maxpacket) {
  51538. + len = ep->dwc_ep.maxpacket;
  51539. + }
  51540. + dwords = (len + 3) / 4;
  51541. +
  51542. + /* While there is space in the queue and space in the FIFO and
  51543. + * More data to tranfer, Write packets to the Tx FIFO */
  51544. + txstatus.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  51545. + DWC_DEBUGPL(DBG_PCDV, "b4 GNPTXSTS=0x%08x\n", txstatus.d32);
  51546. +
  51547. + while (txstatus.b.nptxqspcavail > 0 &&
  51548. + txstatus.b.nptxfspcavail > dwords &&
  51549. + ep->dwc_ep.xfer_count < ep->dwc_ep.xfer_len) {
  51550. + /* Write the FIFO */
  51551. + dwc_otg_ep_write_packet(core_if, &ep->dwc_ep, 0);
  51552. + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
  51553. +
  51554. + if (len > ep->dwc_ep.maxpacket) {
  51555. + len = ep->dwc_ep.maxpacket;
  51556. + }
  51557. +
  51558. + dwords = (len + 3) / 4;
  51559. + txstatus.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  51560. + DWC_DEBUGPL(DBG_PCDV, "GNPTXSTS=0x%08x\n", txstatus.d32);
  51561. + }
  51562. +
  51563. + DWC_DEBUGPL(DBG_PCDV, "GNPTXSTS=0x%08x\n",
  51564. + DWC_READ_REG32(&global_regs->gnptxsts));
  51565. +
  51566. + /* Clear interrupt */
  51567. + gintsts.d32 = 0;
  51568. + gintsts.b.nptxfempty = 1;
  51569. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  51570. +
  51571. + return 1;
  51572. +}
  51573. +
  51574. +/**
  51575. + * This function is called when dedicated Tx FIFO Empty interrupt occurs.
  51576. + * The active request is checked for the next packet to be loaded into
  51577. + * apropriate Tx FIFO.
  51578. + */
  51579. +static int32_t write_empty_tx_fifo(dwc_otg_pcd_t * pcd, uint32_t epnum)
  51580. +{
  51581. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  51582. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  51583. + dwc_otg_dev_in_ep_regs_t *ep_regs;
  51584. + dtxfsts_data_t txstatus = {.d32 = 0 };
  51585. + dwc_otg_pcd_ep_t *ep = 0;
  51586. + uint32_t len = 0;
  51587. + int dwords;
  51588. +
  51589. + ep = get_in_ep(pcd, epnum);
  51590. +
  51591. + DWC_DEBUGPL(DBG_PCD, "Dedicated TxFifo Empty: %d \n", epnum);
  51592. +
  51593. + ep_regs = core_if->dev_if->in_ep_regs[epnum];
  51594. +
  51595. + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
  51596. +
  51597. + if (len > ep->dwc_ep.maxpacket) {
  51598. + len = ep->dwc_ep.maxpacket;
  51599. + }
  51600. +
  51601. + dwords = (len + 3) / 4;
  51602. +
  51603. + /* While there is space in the queue and space in the FIFO and
  51604. + * More data to tranfer, Write packets to the Tx FIFO */
  51605. + txstatus.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
  51606. + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum, txstatus.d32);
  51607. +
  51608. + while (txstatus.b.txfspcavail > dwords &&
  51609. + ep->dwc_ep.xfer_count < ep->dwc_ep.xfer_len &&
  51610. + ep->dwc_ep.xfer_len != 0) {
  51611. + /* Write the FIFO */
  51612. + dwc_otg_ep_write_packet(core_if, &ep->dwc_ep, 0);
  51613. +
  51614. + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
  51615. + if (len > ep->dwc_ep.maxpacket) {
  51616. + len = ep->dwc_ep.maxpacket;
  51617. + }
  51618. +
  51619. + dwords = (len + 3) / 4;
  51620. + txstatus.d32 =
  51621. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
  51622. + DWC_DEBUGPL(DBG_PCDV, "dtxfsts[%d]=0x%08x\n", epnum,
  51623. + txstatus.d32);
  51624. + }
  51625. +
  51626. + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum,
  51627. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts));
  51628. +
  51629. + return 1;
  51630. +}
  51631. +
  51632. +/**
  51633. + * This function is called when the Device is disconnected. It stops
  51634. + * any active requests and informs the Gadget driver of the
  51635. + * disconnect.
  51636. + */
  51637. +void dwc_otg_pcd_stop(dwc_otg_pcd_t * pcd)
  51638. +{
  51639. + int i, num_in_eps, num_out_eps;
  51640. + dwc_otg_pcd_ep_t *ep;
  51641. +
  51642. + gintmsk_data_t intr_mask = {.d32 = 0 };
  51643. +
  51644. + DWC_SPINLOCK(pcd->lock);
  51645. +
  51646. + num_in_eps = GET_CORE_IF(pcd)->dev_if->num_in_eps;
  51647. + num_out_eps = GET_CORE_IF(pcd)->dev_if->num_out_eps;
  51648. +
  51649. + DWC_DEBUGPL(DBG_PCDV, "%s() \n", __func__);
  51650. + /* don't disconnect drivers more than once */
  51651. + if (pcd->ep0state == EP0_DISCONNECT) {
  51652. + DWC_DEBUGPL(DBG_ANY, "%s() Already Disconnected\n", __func__);
  51653. + DWC_SPINUNLOCK(pcd->lock);
  51654. + return;
  51655. + }
  51656. + pcd->ep0state = EP0_DISCONNECT;
  51657. +
  51658. + /* Reset the OTG state. */
  51659. + dwc_otg_pcd_update_otg(pcd, 1);
  51660. +
  51661. + /* Disable the NP Tx Fifo Empty Interrupt. */
  51662. + intr_mask.b.nptxfempty = 1;
  51663. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  51664. + intr_mask.d32, 0);
  51665. +
  51666. + /* Flush the FIFOs */
  51667. + /**@todo NGS Flush Periodic FIFOs */
  51668. + dwc_otg_flush_tx_fifo(GET_CORE_IF(pcd), 0x10);
  51669. + dwc_otg_flush_rx_fifo(GET_CORE_IF(pcd));
  51670. +
  51671. + /* prevent new request submissions, kill any outstanding requests */
  51672. + ep = &pcd->ep0;
  51673. + dwc_otg_request_nuke(ep);
  51674. + /* prevent new request submissions, kill any outstanding requests */
  51675. + for (i = 0; i < num_in_eps; i++) {
  51676. + dwc_otg_pcd_ep_t *ep = &pcd->in_ep[i];
  51677. + dwc_otg_request_nuke(ep);
  51678. + }
  51679. + /* prevent new request submissions, kill any outstanding requests */
  51680. + for (i = 0; i < num_out_eps; i++) {
  51681. + dwc_otg_pcd_ep_t *ep = &pcd->out_ep[i];
  51682. + dwc_otg_request_nuke(ep);
  51683. + }
  51684. +
  51685. + /* report disconnect; the driver is already quiesced */
  51686. + if (pcd->fops->disconnect) {
  51687. + DWC_SPINUNLOCK(pcd->lock);
  51688. + pcd->fops->disconnect(pcd);
  51689. + DWC_SPINLOCK(pcd->lock);
  51690. + }
  51691. + DWC_SPINUNLOCK(pcd->lock);
  51692. +}
  51693. +
  51694. +/**
  51695. + * This interrupt indicates that ...
  51696. + */
  51697. +int32_t dwc_otg_pcd_handle_i2c_intr(dwc_otg_pcd_t * pcd)
  51698. +{
  51699. + gintmsk_data_t intr_mask = {.d32 = 0 };
  51700. + gintsts_data_t gintsts;
  51701. +
  51702. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "i2cintr");
  51703. + intr_mask.b.i2cintr = 1;
  51704. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  51705. + intr_mask.d32, 0);
  51706. +
  51707. + /* Clear interrupt */
  51708. + gintsts.d32 = 0;
  51709. + gintsts.b.i2cintr = 1;
  51710. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  51711. + gintsts.d32);
  51712. + return 1;
  51713. +}
  51714. +
  51715. +/**
  51716. + * This interrupt indicates that ...
  51717. + */
  51718. +int32_t dwc_otg_pcd_handle_early_suspend_intr(dwc_otg_pcd_t * pcd)
  51719. +{
  51720. + gintsts_data_t gintsts;
  51721. +#if defined(VERBOSE)
  51722. + DWC_PRINTF("Early Suspend Detected\n");
  51723. +#endif
  51724. +
  51725. + /* Clear interrupt */
  51726. + gintsts.d32 = 0;
  51727. + gintsts.b.erlysuspend = 1;
  51728. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  51729. + gintsts.d32);
  51730. + return 1;
  51731. +}
  51732. +
  51733. +/**
  51734. + * This function configures EPO to receive SETUP packets.
  51735. + *
  51736. + * @todo NGS: Update the comments from the HW FS.
  51737. + *
  51738. + * -# Program the following fields in the endpoint specific registers
  51739. + * for Control OUT EP 0, in order to receive a setup packet
  51740. + * - DOEPTSIZ0.Packet Count = 3 (To receive up to 3 back to back
  51741. + * setup packets)
  51742. + * - DOEPTSIZE0.Transfer Size = 24 Bytes (To receive up to 3 back
  51743. + * to back setup packets)
  51744. + * - In DMA mode, DOEPDMA0 Register with a memory address to
  51745. + * store any setup packets received
  51746. + *
  51747. + * @param core_if Programming view of DWC_otg controller.
  51748. + * @param pcd Programming view of the PCD.
  51749. + */
  51750. +static inline void ep0_out_start(dwc_otg_core_if_t * core_if,
  51751. + dwc_otg_pcd_t * pcd)
  51752. +{
  51753. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  51754. + deptsiz0_data_t doeptsize0 = {.d32 = 0 };
  51755. + dwc_otg_dev_dma_desc_t *dma_desc;
  51756. + depctl_data_t doepctl = {.d32 = 0 };
  51757. +
  51758. +#ifdef VERBOSE
  51759. + DWC_DEBUGPL(DBG_PCDV, "%s() doepctl0=%0x\n", __func__,
  51760. + DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl));
  51761. +#endif
  51762. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  51763. + doepctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl);
  51764. + if (doepctl.b.epena) {
  51765. + return;
  51766. + }
  51767. + }
  51768. +
  51769. + doeptsize0.b.supcnt = 3;
  51770. + doeptsize0.b.pktcnt = 1;
  51771. + doeptsize0.b.xfersize = 8 * 3;
  51772. +
  51773. + if (core_if->dma_enable) {
  51774. + if (!core_if->dma_desc_enable) {
  51775. + /** put here as for Hermes mode deptisz register should not be written */
  51776. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doeptsiz,
  51777. + doeptsize0.d32);
  51778. +
  51779. + /** @todo dma needs to handle multiple setup packets (up to 3) */
  51780. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepdma,
  51781. + pcd->setup_pkt_dma_handle);
  51782. + } else {
  51783. + dev_if->setup_desc_index =
  51784. + (dev_if->setup_desc_index + 1) & 1;
  51785. + dma_desc =
  51786. + dev_if->setup_desc_addr[dev_if->setup_desc_index];
  51787. +
  51788. + /** DMA Descriptor Setup */
  51789. + dma_desc->status.b.bs = BS_HOST_BUSY;
  51790. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  51791. + dma_desc->status.b.sr = 0;
  51792. + dma_desc->status.b.mtrf = 0;
  51793. + }
  51794. + dma_desc->status.b.l = 1;
  51795. + dma_desc->status.b.ioc = 1;
  51796. + dma_desc->status.b.bytes = pcd->ep0.dwc_ep.maxpacket;
  51797. + dma_desc->buf = pcd->setup_pkt_dma_handle;
  51798. + dma_desc->status.b.sts = 0;
  51799. + dma_desc->status.b.bs = BS_HOST_READY;
  51800. +
  51801. + /** DOEPDMA0 Register write */
  51802. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepdma,
  51803. + dev_if->dma_setup_desc_addr
  51804. + [dev_if->setup_desc_index]);
  51805. + }
  51806. +
  51807. + } else {
  51808. + /** put here as for Hermes mode deptisz register should not be written */
  51809. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doeptsiz,
  51810. + doeptsize0.d32);
  51811. + }
  51812. +
  51813. + /** DOEPCTL0 Register write cnak will be set after setup interrupt */
  51814. + doepctl.d32 = 0;
  51815. + doepctl.b.epena = 1;
  51816. + if (core_if->snpsid <= OTG_CORE_REV_2_94a) {
  51817. + doepctl.b.cnak = 1;
  51818. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepctl, doepctl.d32);
  51819. + } else {
  51820. + DWC_MODIFY_REG32(&dev_if->out_ep_regs[0]->doepctl, 0, doepctl.d32);
  51821. + }
  51822. +
  51823. +#ifdef VERBOSE
  51824. + DWC_DEBUGPL(DBG_PCDV, "doepctl0=%0x\n",
  51825. + DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl));
  51826. + DWC_DEBUGPL(DBG_PCDV, "diepctl0=%0x\n",
  51827. + DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl));
  51828. +#endif
  51829. +}
  51830. +
  51831. +/**
  51832. + * This interrupt occurs when a USB Reset is detected. When the USB
  51833. + * Reset Interrupt occurs the device state is set to DEFAULT and the
  51834. + * EP0 state is set to IDLE.
  51835. + * -# Set the NAK bit for all OUT endpoints (DOEPCTLn.SNAK = 1)
  51836. + * -# Unmask the following interrupt bits
  51837. + * - DAINTMSK.INEP0 = 1 (Control 0 IN endpoint)
  51838. + * - DAINTMSK.OUTEP0 = 1 (Control 0 OUT endpoint)
  51839. + * - DOEPMSK.SETUP = 1
  51840. + * - DOEPMSK.XferCompl = 1
  51841. + * - DIEPMSK.XferCompl = 1
  51842. + * - DIEPMSK.TimeOut = 1
  51843. + * -# Program the following fields in the endpoint specific registers
  51844. + * for Control OUT EP 0, in order to receive a setup packet
  51845. + * - DOEPTSIZ0.Packet Count = 3 (To receive up to 3 back to back
  51846. + * setup packets)
  51847. + * - DOEPTSIZE0.Transfer Size = 24 Bytes (To receive up to 3 back
  51848. + * to back setup packets)
  51849. + * - In DMA mode, DOEPDMA0 Register with a memory address to
  51850. + * store any setup packets received
  51851. + * At this point, all the required initialization, except for enabling
  51852. + * the control 0 OUT endpoint is done, for receiving SETUP packets.
  51853. + */
  51854. +int32_t dwc_otg_pcd_handle_usb_reset_intr(dwc_otg_pcd_t * pcd)
  51855. +{
  51856. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  51857. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  51858. + depctl_data_t doepctl = {.d32 = 0 };
  51859. + depctl_data_t diepctl = {.d32 = 0 };
  51860. + daint_data_t daintmsk = {.d32 = 0 };
  51861. + doepmsk_data_t doepmsk = {.d32 = 0 };
  51862. + diepmsk_data_t diepmsk = {.d32 = 0 };
  51863. + dcfg_data_t dcfg = {.d32 = 0 };
  51864. + grstctl_t resetctl = {.d32 = 0 };
  51865. + dctl_data_t dctl = {.d32 = 0 };
  51866. + int i = 0;
  51867. + gintsts_data_t gintsts;
  51868. + pcgcctl_data_t power = {.d32 = 0 };
  51869. +
  51870. + power.d32 = DWC_READ_REG32(core_if->pcgcctl);
  51871. + if (power.b.stoppclk) {
  51872. + power.d32 = 0;
  51873. + power.b.stoppclk = 1;
  51874. + DWC_MODIFY_REG32(core_if->pcgcctl, power.d32, 0);
  51875. +
  51876. + power.b.pwrclmp = 1;
  51877. + DWC_MODIFY_REG32(core_if->pcgcctl, power.d32, 0);
  51878. +
  51879. + power.b.rstpdwnmodule = 1;
  51880. + DWC_MODIFY_REG32(core_if->pcgcctl, power.d32, 0);
  51881. + }
  51882. +
  51883. + core_if->lx_state = DWC_OTG_L0;
  51884. +
  51885. + DWC_PRINTF("USB RESET\n");
  51886. +#ifdef DWC_EN_ISOC
  51887. + for (i = 1; i < 16; ++i) {
  51888. + dwc_otg_pcd_ep_t *ep;
  51889. + dwc_ep_t *dwc_ep;
  51890. + ep = get_in_ep(pcd, i);
  51891. + if (ep != 0) {
  51892. + dwc_ep = &ep->dwc_ep;
  51893. + dwc_ep->next_frame = 0xffffffff;
  51894. + }
  51895. + }
  51896. +#endif /* DWC_EN_ISOC */
  51897. +
  51898. + /* reset the HNP settings */
  51899. + dwc_otg_pcd_update_otg(pcd, 1);
  51900. +
  51901. + /* Clear the Remote Wakeup Signalling */
  51902. + dctl.b.rmtwkupsig = 1;
  51903. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, 0);
  51904. +
  51905. + /* Set NAK for all OUT EPs */
  51906. + doepctl.b.snak = 1;
  51907. + for (i = 0; i <= dev_if->num_out_eps; i++) {
  51908. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepctl, doepctl.d32);
  51909. + }
  51910. +
  51911. + /* Flush the NP Tx FIFO */
  51912. + dwc_otg_flush_tx_fifo(core_if, 0x10);
  51913. + /* Flush the Learning Queue */
  51914. + resetctl.b.intknqflsh = 1;
  51915. + DWC_WRITE_REG32(&core_if->core_global_regs->grstctl, resetctl.d32);
  51916. +
  51917. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable) {
  51918. + core_if->start_predict = 0;
  51919. + for (i = 0; i<= core_if->dev_if->num_in_eps; ++i) {
  51920. + core_if->nextep_seq[i] = 0xff; // 0xff - EP not active
  51921. + }
  51922. + core_if->nextep_seq[0] = 0;
  51923. + core_if->first_in_nextep_seq = 0;
  51924. + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl);
  51925. + diepctl.b.nextep = 0;
  51926. + DWC_WRITE_REG32(&dev_if->in_ep_regs[0]->diepctl, diepctl.d32);
  51927. +
  51928. + /* Update IN Endpoint Mismatch Count by active IN NP EP count + 1 */
  51929. + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
  51930. + dcfg.b.epmscnt = 2;
  51931. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
  51932. +
  51933. + DWC_DEBUGPL(DBG_PCDV,
  51934. + "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
  51935. + __func__, core_if->first_in_nextep_seq);
  51936. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  51937. + DWC_DEBUGPL(DBG_PCDV, "%2d\n", core_if->nextep_seq[i]);
  51938. + }
  51939. + }
  51940. +
  51941. + if (core_if->multiproc_int_enable) {
  51942. + daintmsk.b.inep0 = 1;
  51943. + daintmsk.b.outep0 = 1;
  51944. + DWC_WRITE_REG32(&dev_if->dev_global_regs->deachintmsk,
  51945. + daintmsk.d32);
  51946. +
  51947. + doepmsk.b.setup = 1;
  51948. + doepmsk.b.xfercompl = 1;
  51949. + doepmsk.b.ahberr = 1;
  51950. + doepmsk.b.epdisabled = 1;
  51951. +
  51952. + if ((core_if->dma_desc_enable) ||
  51953. + (core_if->dma_enable
  51954. + && core_if->snpsid >= OTG_CORE_REV_3_00a)) {
  51955. + doepmsk.b.stsphsercvd = 1;
  51956. + }
  51957. + if (core_if->dma_desc_enable)
  51958. + doepmsk.b.bna = 1;
  51959. +/*
  51960. + doepmsk.b.babble = 1;
  51961. + doepmsk.b.nyet = 1;
  51962. +
  51963. + if (core_if->dma_enable) {
  51964. + doepmsk.b.nak = 1;
  51965. + }
  51966. +*/
  51967. + DWC_WRITE_REG32(&dev_if->dev_global_regs->doepeachintmsk[0],
  51968. + doepmsk.d32);
  51969. +
  51970. + diepmsk.b.xfercompl = 1;
  51971. + diepmsk.b.timeout = 1;
  51972. + diepmsk.b.epdisabled = 1;
  51973. + diepmsk.b.ahberr = 1;
  51974. + diepmsk.b.intknepmis = 1;
  51975. + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable)
  51976. + diepmsk.b.intknepmis = 0;
  51977. +
  51978. +/* if (core_if->dma_desc_enable) {
  51979. + diepmsk.b.bna = 1;
  51980. + }
  51981. +*/
  51982. +/*
  51983. + if (core_if->dma_enable) {
  51984. + diepmsk.b.nak = 1;
  51985. + }
  51986. +*/
  51987. + DWC_WRITE_REG32(&dev_if->dev_global_regs->diepeachintmsk[0],
  51988. + diepmsk.d32);
  51989. + } else {
  51990. + daintmsk.b.inep0 = 1;
  51991. + daintmsk.b.outep0 = 1;
  51992. + DWC_WRITE_REG32(&dev_if->dev_global_regs->daintmsk,
  51993. + daintmsk.d32);
  51994. +
  51995. + doepmsk.b.setup = 1;
  51996. + doepmsk.b.xfercompl = 1;
  51997. + doepmsk.b.ahberr = 1;
  51998. + doepmsk.b.epdisabled = 1;
  51999. +
  52000. + if ((core_if->dma_desc_enable) ||
  52001. + (core_if->dma_enable
  52002. + && core_if->snpsid >= OTG_CORE_REV_3_00a)) {
  52003. + doepmsk.b.stsphsercvd = 1;
  52004. + }
  52005. + if (core_if->dma_desc_enable)
  52006. + doepmsk.b.bna = 1;
  52007. + DWC_WRITE_REG32(&dev_if->dev_global_regs->doepmsk, doepmsk.d32);
  52008. +
  52009. + diepmsk.b.xfercompl = 1;
  52010. + diepmsk.b.timeout = 1;
  52011. + diepmsk.b.epdisabled = 1;
  52012. + diepmsk.b.ahberr = 1;
  52013. + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable)
  52014. + diepmsk.b.intknepmis = 0;
  52015. +/*
  52016. + if (core_if->dma_desc_enable) {
  52017. + diepmsk.b.bna = 1;
  52018. + }
  52019. +*/
  52020. +
  52021. + DWC_WRITE_REG32(&dev_if->dev_global_regs->diepmsk, diepmsk.d32);
  52022. + }
  52023. +
  52024. + /* Reset Device Address */
  52025. + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
  52026. + dcfg.b.devaddr = 0;
  52027. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
  52028. +
  52029. + /* setup EP0 to receive SETUP packets */
  52030. + if (core_if->snpsid <= OTG_CORE_REV_2_94a)
  52031. + ep0_out_start(core_if, pcd);
  52032. +
  52033. + /* Clear interrupt */
  52034. + gintsts.d32 = 0;
  52035. + gintsts.b.usbreset = 1;
  52036. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  52037. +
  52038. + return 1;
  52039. +}
  52040. +
  52041. +/**
  52042. + * Get the device speed from the device status register and convert it
  52043. + * to USB speed constant.
  52044. + *
  52045. + * @param core_if Programming view of DWC_otg controller.
  52046. + */
  52047. +static int get_device_speed(dwc_otg_core_if_t * core_if)
  52048. +{
  52049. + dsts_data_t dsts;
  52050. + int speed = 0;
  52051. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  52052. +
  52053. + switch (dsts.b.enumspd) {
  52054. + case DWC_DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ:
  52055. + speed = USB_SPEED_HIGH;
  52056. + break;
  52057. + case DWC_DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ:
  52058. + case DWC_DSTS_ENUMSPD_FS_PHY_48MHZ:
  52059. + speed = USB_SPEED_FULL;
  52060. + break;
  52061. +
  52062. + case DWC_DSTS_ENUMSPD_LS_PHY_6MHZ:
  52063. + speed = USB_SPEED_LOW;
  52064. + break;
  52065. + }
  52066. +
  52067. + return speed;
  52068. +}
  52069. +
  52070. +/**
  52071. + * Read the device status register and set the device speed in the
  52072. + * data structure.
  52073. + * Set up EP0 to receive SETUP packets by calling dwc_ep0_activate.
  52074. + */
  52075. +int32_t dwc_otg_pcd_handle_enum_done_intr(dwc_otg_pcd_t * pcd)
  52076. +{
  52077. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  52078. + gintsts_data_t gintsts;
  52079. + gusbcfg_data_t gusbcfg;
  52080. + dwc_otg_core_global_regs_t *global_regs =
  52081. + GET_CORE_IF(pcd)->core_global_regs;
  52082. + uint8_t utmi16b, utmi8b;
  52083. + int speed;
  52084. + DWC_DEBUGPL(DBG_PCD, "SPEED ENUM\n");
  52085. +
  52086. + if (GET_CORE_IF(pcd)->snpsid >= OTG_CORE_REV_2_60a) {
  52087. + utmi16b = 6; //vahrama old value was 6;
  52088. + utmi8b = 9;
  52089. + } else {
  52090. + utmi16b = 4;
  52091. + utmi8b = 8;
  52092. + }
  52093. + dwc_otg_ep0_activate(GET_CORE_IF(pcd), &ep0->dwc_ep);
  52094. + if (GET_CORE_IF(pcd)->snpsid >= OTG_CORE_REV_3_00a) {
  52095. + ep0_out_start(GET_CORE_IF(pcd), pcd);
  52096. + }
  52097. +
  52098. +#ifdef DEBUG_EP0
  52099. + print_ep0_state(pcd);
  52100. +#endif
  52101. +
  52102. + if (pcd->ep0state == EP0_DISCONNECT) {
  52103. + pcd->ep0state = EP0_IDLE;
  52104. + } else if (pcd->ep0state == EP0_STALL) {
  52105. + pcd->ep0state = EP0_IDLE;
  52106. + }
  52107. +
  52108. + pcd->ep0state = EP0_IDLE;
  52109. +
  52110. + ep0->stopped = 0;
  52111. +
  52112. + speed = get_device_speed(GET_CORE_IF(pcd));
  52113. + pcd->fops->connect(pcd, speed);
  52114. +
  52115. + /* Set USB turnaround time based on device speed and PHY interface. */
  52116. + gusbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  52117. + if (speed == USB_SPEED_HIGH) {
  52118. + if (GET_CORE_IF(pcd)->hwcfg2.b.hs_phy_type ==
  52119. + DWC_HWCFG2_HS_PHY_TYPE_ULPI) {
  52120. + /* ULPI interface */
  52121. + gusbcfg.b.usbtrdtim = 9;
  52122. + }
  52123. + if (GET_CORE_IF(pcd)->hwcfg2.b.hs_phy_type ==
  52124. + DWC_HWCFG2_HS_PHY_TYPE_UTMI) {
  52125. + /* UTMI+ interface */
  52126. + if (GET_CORE_IF(pcd)->hwcfg4.b.utmi_phy_data_width == 0) {
  52127. + gusbcfg.b.usbtrdtim = utmi8b;
  52128. + } else if (GET_CORE_IF(pcd)->hwcfg4.
  52129. + b.utmi_phy_data_width == 1) {
  52130. + gusbcfg.b.usbtrdtim = utmi16b;
  52131. + } else if (GET_CORE_IF(pcd)->
  52132. + core_params->phy_utmi_width == 8) {
  52133. + gusbcfg.b.usbtrdtim = utmi8b;
  52134. + } else {
  52135. + gusbcfg.b.usbtrdtim = utmi16b;
  52136. + }
  52137. + }
  52138. + if (GET_CORE_IF(pcd)->hwcfg2.b.hs_phy_type ==
  52139. + DWC_HWCFG2_HS_PHY_TYPE_UTMI_ULPI) {
  52140. + /* UTMI+ OR ULPI interface */
  52141. + if (gusbcfg.b.ulpi_utmi_sel == 1) {
  52142. + /* ULPI interface */
  52143. + gusbcfg.b.usbtrdtim = 9;
  52144. + } else {
  52145. + /* UTMI+ interface */
  52146. + if (GET_CORE_IF(pcd)->
  52147. + core_params->phy_utmi_width == 16) {
  52148. + gusbcfg.b.usbtrdtim = utmi16b;
  52149. + } else {
  52150. + gusbcfg.b.usbtrdtim = utmi8b;
  52151. + }
  52152. + }
  52153. + }
  52154. + } else {
  52155. + /* Full or low speed */
  52156. + gusbcfg.b.usbtrdtim = 9;
  52157. + }
  52158. + DWC_WRITE_REG32(&global_regs->gusbcfg, gusbcfg.d32);
  52159. +
  52160. + /* Clear interrupt */
  52161. + gintsts.d32 = 0;
  52162. + gintsts.b.enumdone = 1;
  52163. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  52164. + gintsts.d32);
  52165. + return 1;
  52166. +}
  52167. +
  52168. +/**
  52169. + * This interrupt indicates that the ISO OUT Packet was dropped due to
  52170. + * Rx FIFO full or Rx Status Queue Full. If this interrupt occurs
  52171. + * read all the data from the Rx FIFO.
  52172. + */
  52173. +int32_t dwc_otg_pcd_handle_isoc_out_packet_dropped_intr(dwc_otg_pcd_t * pcd)
  52174. +{
  52175. + gintmsk_data_t intr_mask = {.d32 = 0 };
  52176. + gintsts_data_t gintsts;
  52177. +
  52178. + DWC_WARN("INTERRUPT Handler not implemented for %s\n",
  52179. + "ISOC Out Dropped");
  52180. +
  52181. + intr_mask.b.isooutdrop = 1;
  52182. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  52183. + intr_mask.d32, 0);
  52184. +
  52185. + /* Clear interrupt */
  52186. + gintsts.d32 = 0;
  52187. + gintsts.b.isooutdrop = 1;
  52188. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  52189. + gintsts.d32);
  52190. +
  52191. + return 1;
  52192. +}
  52193. +
  52194. +/**
  52195. + * This interrupt indicates the end of the portion of the micro-frame
  52196. + * for periodic transactions. If there is a periodic transaction for
  52197. + * the next frame, load the packets into the EP periodic Tx FIFO.
  52198. + */
  52199. +int32_t dwc_otg_pcd_handle_end_periodic_frame_intr(dwc_otg_pcd_t * pcd)
  52200. +{
  52201. + gintmsk_data_t intr_mask = {.d32 = 0 };
  52202. + gintsts_data_t gintsts;
  52203. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "EOP");
  52204. +
  52205. + intr_mask.b.eopframe = 1;
  52206. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  52207. + intr_mask.d32, 0);
  52208. +
  52209. + /* Clear interrupt */
  52210. + gintsts.d32 = 0;
  52211. + gintsts.b.eopframe = 1;
  52212. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  52213. + gintsts.d32);
  52214. +
  52215. + return 1;
  52216. +}
  52217. +
  52218. +/**
  52219. + * This interrupt indicates that EP of the packet on the top of the
  52220. + * non-periodic Tx FIFO does not match EP of the IN Token received.
  52221. + *
  52222. + * The "Device IN Token Queue" Registers are read to determine the
  52223. + * order the IN Tokens have been received. The non-periodic Tx FIFO
  52224. + * is flushed, so it can be reloaded in the order seen in the IN Token
  52225. + * Queue.
  52226. + */
  52227. +int32_t dwc_otg_pcd_handle_ep_mismatch_intr(dwc_otg_pcd_t * pcd)
  52228. +{
  52229. + gintsts_data_t gintsts;
  52230. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  52231. + dctl_data_t dctl;
  52232. + gintmsk_data_t intr_mask = {.d32 = 0 };
  52233. +
  52234. + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable) {
  52235. + core_if->start_predict = 1;
  52236. +
  52237. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, core_if);
  52238. +
  52239. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  52240. + if (!gintsts.b.ginnakeff) {
  52241. + /* Disable EP Mismatch interrupt */
  52242. + intr_mask.d32 = 0;
  52243. + intr_mask.b.epmismatch = 1;
  52244. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, intr_mask.d32, 0);
  52245. + /* Enable the Global IN NAK Effective Interrupt */
  52246. + intr_mask.d32 = 0;
  52247. + intr_mask.b.ginnakeff = 1;
  52248. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, intr_mask.d32);
  52249. + /* Set the global non-periodic IN NAK handshake */
  52250. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
  52251. + dctl.b.sgnpinnak = 1;
  52252. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
  52253. + } else {
  52254. + DWC_PRINTF("gintsts.b.ginnakeff = 1! dctl.b.sgnpinnak not set\n");
  52255. + }
  52256. + /* Disabling of all EP's will be done in dwc_otg_pcd_handle_in_nak_effective()
  52257. + * handler after Global IN NAK Effective interrupt will be asserted */
  52258. + }
  52259. + /* Clear interrupt */
  52260. + gintsts.d32 = 0;
  52261. + gintsts.b.epmismatch = 1;
  52262. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  52263. +
  52264. + return 1;
  52265. +}
  52266. +
  52267. +/**
  52268. + * This interrupt is valid only in DMA mode. This interrupt indicates that the
  52269. + * core has stopped fetching data for IN endpoints due to the unavailability of
  52270. + * TxFIFO space or Request Queue space. This interrupt is used by the
  52271. + * application for an endpoint mismatch algorithm.
  52272. + *
  52273. + * @param pcd The PCD
  52274. + */
  52275. +int32_t dwc_otg_pcd_handle_ep_fetsusp_intr(dwc_otg_pcd_t * pcd)
  52276. +{
  52277. + gintsts_data_t gintsts;
  52278. + gintmsk_data_t gintmsk_data;
  52279. + dctl_data_t dctl;
  52280. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  52281. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, core_if);
  52282. +
  52283. + /* Clear the global non-periodic IN NAK handshake */
  52284. + dctl.d32 = 0;
  52285. + dctl.b.cgnpinnak = 1;
  52286. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
  52287. +
  52288. + /* Mask GINTSTS.FETSUSP interrupt */
  52289. + gintmsk_data.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  52290. + gintmsk_data.b.fetsusp = 0;
  52291. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk_data.d32);
  52292. +
  52293. + /* Clear interrupt */
  52294. + gintsts.d32 = 0;
  52295. + gintsts.b.fetsusp = 1;
  52296. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  52297. +
  52298. + return 1;
  52299. +}
  52300. +/**
  52301. + * This funcion stalls EP0.
  52302. + */
  52303. +static inline void ep0_do_stall(dwc_otg_pcd_t * pcd, const int err_val)
  52304. +{
  52305. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  52306. + usb_device_request_t *ctrl = &pcd->setup_pkt->req;
  52307. + DWC_WARN("req %02x.%02x protocol STALL; err %d\n",
  52308. + ctrl->bmRequestType, ctrl->bRequest, err_val);
  52309. +
  52310. + ep0->dwc_ep.is_in = 1;
  52311. + dwc_otg_ep_set_stall(GET_CORE_IF(pcd), &ep0->dwc_ep);
  52312. + pcd->ep0.stopped = 1;
  52313. + pcd->ep0state = EP0_IDLE;
  52314. + ep0_out_start(GET_CORE_IF(pcd), pcd);
  52315. +}
  52316. +
  52317. +/**
  52318. + * This functions delegates the setup command to the gadget driver.
  52319. + */
  52320. +static inline void do_gadget_setup(dwc_otg_pcd_t * pcd,
  52321. + usb_device_request_t * ctrl)
  52322. +{
  52323. + int ret = 0;
  52324. + DWC_SPINUNLOCK(pcd->lock);
  52325. + ret = pcd->fops->setup(pcd, (uint8_t *) ctrl);
  52326. + DWC_SPINLOCK(pcd->lock);
  52327. + if (ret < 0) {
  52328. + ep0_do_stall(pcd, ret);
  52329. + }
  52330. +
  52331. + /** @todo This is a g_file_storage gadget driver specific
  52332. + * workaround: a DELAYED_STATUS result from the fsg_setup
  52333. + * routine will result in the gadget queueing a EP0 IN status
  52334. + * phase for a two-stage control transfer. Exactly the same as
  52335. + * a SET_CONFIGURATION/SET_INTERFACE except that this is a class
  52336. + * specific request. Need a generic way to know when the gadget
  52337. + * driver will queue the status phase. Can we assume when we
  52338. + * call the gadget driver setup() function that it will always
  52339. + * queue and require the following flag? Need to look into
  52340. + * this.
  52341. + */
  52342. +
  52343. + if (ret == 256 + 999) {
  52344. + pcd->request_config = 1;
  52345. + }
  52346. +}
  52347. +
  52348. +#ifdef DWC_UTE_CFI
  52349. +/**
  52350. + * This functions delegates the CFI setup commands to the gadget driver.
  52351. + * This function will return a negative value to indicate a failure.
  52352. + */
  52353. +static inline int cfi_gadget_setup(dwc_otg_pcd_t * pcd,
  52354. + struct cfi_usb_ctrlrequest *ctrl_req)
  52355. +{
  52356. + int ret = 0;
  52357. +
  52358. + if (pcd->fops && pcd->fops->cfi_setup) {
  52359. + DWC_SPINUNLOCK(pcd->lock);
  52360. + ret = pcd->fops->cfi_setup(pcd, ctrl_req);
  52361. + DWC_SPINLOCK(pcd->lock);
  52362. + if (ret < 0) {
  52363. + ep0_do_stall(pcd, ret);
  52364. + return ret;
  52365. + }
  52366. + }
  52367. +
  52368. + return ret;
  52369. +}
  52370. +#endif
  52371. +
  52372. +/**
  52373. + * This function starts the Zero-Length Packet for the IN status phase
  52374. + * of a 2 stage control transfer.
  52375. + */
  52376. +static inline void do_setup_in_status_phase(dwc_otg_pcd_t * pcd)
  52377. +{
  52378. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  52379. + if (pcd->ep0state == EP0_STALL) {
  52380. + return;
  52381. + }
  52382. +
  52383. + pcd->ep0state = EP0_IN_STATUS_PHASE;
  52384. +
  52385. + /* Prepare for more SETUP Packets */
  52386. + DWC_DEBUGPL(DBG_PCD, "EP0 IN ZLP\n");
  52387. + if ((GET_CORE_IF(pcd)->snpsid >= OTG_CORE_REV_3_00a)
  52388. + && (pcd->core_if->dma_desc_enable)
  52389. + && (ep0->dwc_ep.xfer_count < ep0->dwc_ep.total_len)) {
  52390. + DWC_DEBUGPL(DBG_PCDV,
  52391. + "Data terminated wait next packet in out_desc_addr\n");
  52392. + pcd->backup_buf = phys_to_virt(ep0->dwc_ep.dma_addr);
  52393. + pcd->data_terminated = 1;
  52394. + }
  52395. + ep0->dwc_ep.xfer_len = 0;
  52396. + ep0->dwc_ep.xfer_count = 0;
  52397. + ep0->dwc_ep.is_in = 1;
  52398. + ep0->dwc_ep.dma_addr = pcd->setup_pkt_dma_handle;
  52399. + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd), &ep0->dwc_ep);
  52400. +
  52401. + /* Prepare for more SETUP Packets */
  52402. + //ep0_out_start(GET_CORE_IF(pcd), pcd);
  52403. +}
  52404. +
  52405. +/**
  52406. + * This function starts the Zero-Length Packet for the OUT status phase
  52407. + * of a 2 stage control transfer.
  52408. + */
  52409. +static inline void do_setup_out_status_phase(dwc_otg_pcd_t * pcd)
  52410. +{
  52411. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  52412. + if (pcd->ep0state == EP0_STALL) {
  52413. + DWC_DEBUGPL(DBG_PCD, "EP0 STALLED\n");
  52414. + return;
  52415. + }
  52416. + pcd->ep0state = EP0_OUT_STATUS_PHASE;
  52417. +
  52418. + DWC_DEBUGPL(DBG_PCD, "EP0 OUT ZLP\n");
  52419. + ep0->dwc_ep.xfer_len = 0;
  52420. + ep0->dwc_ep.xfer_count = 0;
  52421. + ep0->dwc_ep.is_in = 0;
  52422. + ep0->dwc_ep.dma_addr = pcd->setup_pkt_dma_handle;
  52423. + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd), &ep0->dwc_ep);
  52424. +
  52425. + /* Prepare for more SETUP Packets */
  52426. + if (GET_CORE_IF(pcd)->dma_enable == 0) {
  52427. + ep0_out_start(GET_CORE_IF(pcd), pcd);
  52428. + }
  52429. +}
  52430. +
  52431. +/**
  52432. + * Clear the EP halt (STALL) and if pending requests start the
  52433. + * transfer.
  52434. + */
  52435. +static inline void pcd_clear_halt(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep)
  52436. +{
  52437. + if (ep->dwc_ep.stall_clear_flag == 0)
  52438. + dwc_otg_ep_clear_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
  52439. +
  52440. + /* Reactive the EP */
  52441. + dwc_otg_ep_activate(GET_CORE_IF(pcd), &ep->dwc_ep);
  52442. + if (ep->stopped) {
  52443. + ep->stopped = 0;
  52444. + /* If there is a request in the EP queue start it */
  52445. +
  52446. + /** @todo FIXME: this causes an EP mismatch in DMA mode.
  52447. + * epmismatch not yet implemented. */
  52448. +
  52449. + /*
  52450. + * Above fixme is solved by implmenting a tasklet to call the
  52451. + * start_next_request(), outside of interrupt context at some
  52452. + * time after the current time, after a clear-halt setup packet.
  52453. + * Still need to implement ep mismatch in the future if a gadget
  52454. + * ever uses more than one endpoint at once
  52455. + */
  52456. + ep->queue_sof = 1;
  52457. + DWC_TASK_SCHEDULE(pcd->start_xfer_tasklet);
  52458. + }
  52459. + /* Start Control Status Phase */
  52460. + do_setup_in_status_phase(pcd);
  52461. +}
  52462. +
  52463. +/**
  52464. + * This function is called when the SET_FEATURE TEST_MODE Setup packet
  52465. + * is sent from the host. The Device Control register is written with
  52466. + * the Test Mode bits set to the specified Test Mode. This is done as
  52467. + * a tasklet so that the "Status" phase of the control transfer
  52468. + * completes before transmitting the TEST packets.
  52469. + *
  52470. + * @todo This has not been tested since the tasklet struct was put
  52471. + * into the PCD struct!
  52472. + *
  52473. + */
  52474. +void do_test_mode(void *data)
  52475. +{
  52476. + dctl_data_t dctl;
  52477. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) data;
  52478. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  52479. + int test_mode = pcd->test_mode;
  52480. +
  52481. +// DWC_WARN("%s() has not been tested since being rewritten!\n", __func__);
  52482. +
  52483. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
  52484. + switch (test_mode) {
  52485. + case 1: // TEST_J
  52486. + dctl.b.tstctl = 1;
  52487. + break;
  52488. +
  52489. + case 2: // TEST_K
  52490. + dctl.b.tstctl = 2;
  52491. + break;
  52492. +
  52493. + case 3: // TEST_SE0_NAK
  52494. + dctl.b.tstctl = 3;
  52495. + break;
  52496. +
  52497. + case 4: // TEST_PACKET
  52498. + dctl.b.tstctl = 4;
  52499. + break;
  52500. +
  52501. + case 5: // TEST_FORCE_ENABLE
  52502. + dctl.b.tstctl = 5;
  52503. + break;
  52504. + }
  52505. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
  52506. +}
  52507. +
  52508. +/**
  52509. + * This function process the GET_STATUS Setup Commands.
  52510. + */
  52511. +static inline void do_get_status(dwc_otg_pcd_t * pcd)
  52512. +{
  52513. + usb_device_request_t ctrl = pcd->setup_pkt->req;
  52514. + dwc_otg_pcd_ep_t *ep;
  52515. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  52516. + uint16_t *status = pcd->status_buf;
  52517. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  52518. +
  52519. +#ifdef DEBUG_EP0
  52520. + DWC_DEBUGPL(DBG_PCD,
  52521. + "GET_STATUS %02x.%02x v%04x i%04x l%04x\n",
  52522. + ctrl.bmRequestType, ctrl.bRequest,
  52523. + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
  52524. + UGETW(ctrl.wLength));
  52525. +#endif
  52526. +
  52527. + switch (UT_GET_RECIPIENT(ctrl.bmRequestType)) {
  52528. + case UT_DEVICE:
  52529. + if(UGETW(ctrl.wIndex) == 0xF000) { /* OTG Status selector */
  52530. + DWC_PRINTF("wIndex - %d\n", UGETW(ctrl.wIndex));
  52531. + DWC_PRINTF("OTG VERSION - %d\n", core_if->otg_ver);
  52532. + DWC_PRINTF("OTG CAP - %d, %d\n",
  52533. + core_if->core_params->otg_cap,
  52534. + DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE);
  52535. + if (core_if->otg_ver == 1
  52536. + && core_if->core_params->otg_cap ==
  52537. + DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
  52538. + uint8_t *otgsts = (uint8_t*)pcd->status_buf;
  52539. + *otgsts = (core_if->otg_sts & 0x1);
  52540. + pcd->ep0_pending = 1;
  52541. + ep0->dwc_ep.start_xfer_buff =
  52542. + (uint8_t *) otgsts;
  52543. + ep0->dwc_ep.xfer_buff = (uint8_t *) otgsts;
  52544. + ep0->dwc_ep.dma_addr =
  52545. + pcd->status_buf_dma_handle;
  52546. + ep0->dwc_ep.xfer_len = 1;
  52547. + ep0->dwc_ep.xfer_count = 0;
  52548. + ep0->dwc_ep.total_len = ep0->dwc_ep.xfer_len;
  52549. + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd),
  52550. + &ep0->dwc_ep);
  52551. + return;
  52552. + } else {
  52553. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  52554. + return;
  52555. + }
  52556. + break;
  52557. + } else {
  52558. + *status = 0x1; /* Self powered */
  52559. + *status |= pcd->remote_wakeup_enable << 1;
  52560. + break;
  52561. + }
  52562. + case UT_INTERFACE:
  52563. + *status = 0;
  52564. + break;
  52565. +
  52566. + case UT_ENDPOINT:
  52567. + ep = get_ep_by_addr(pcd, UGETW(ctrl.wIndex));
  52568. + if (ep == 0 || UGETW(ctrl.wLength) > 2) {
  52569. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  52570. + return;
  52571. + }
  52572. + /** @todo check for EP stall */
  52573. + *status = ep->stopped;
  52574. + break;
  52575. + }
  52576. + pcd->ep0_pending = 1;
  52577. + ep0->dwc_ep.start_xfer_buff = (uint8_t *) status;
  52578. + ep0->dwc_ep.xfer_buff = (uint8_t *) status;
  52579. + ep0->dwc_ep.dma_addr = pcd->status_buf_dma_handle;
  52580. + ep0->dwc_ep.xfer_len = 2;
  52581. + ep0->dwc_ep.xfer_count = 0;
  52582. + ep0->dwc_ep.total_len = ep0->dwc_ep.xfer_len;
  52583. + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd), &ep0->dwc_ep);
  52584. +}
  52585. +
  52586. +/**
  52587. + * This function process the SET_FEATURE Setup Commands.
  52588. + */
  52589. +static inline void do_set_feature(dwc_otg_pcd_t * pcd)
  52590. +{
  52591. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  52592. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  52593. + usb_device_request_t ctrl = pcd->setup_pkt->req;
  52594. + dwc_otg_pcd_ep_t *ep = 0;
  52595. + int32_t otg_cap_param = core_if->core_params->otg_cap;
  52596. + gotgctl_data_t gotgctl = {.d32 = 0 };
  52597. +
  52598. + DWC_DEBUGPL(DBG_PCD, "SET_FEATURE:%02x.%02x v%04x i%04x l%04x\n",
  52599. + ctrl.bmRequestType, ctrl.bRequest,
  52600. + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
  52601. + UGETW(ctrl.wLength));
  52602. + DWC_DEBUGPL(DBG_PCD, "otg_cap=%d\n", otg_cap_param);
  52603. +
  52604. + switch (UT_GET_RECIPIENT(ctrl.bmRequestType)) {
  52605. + case UT_DEVICE:
  52606. + switch (UGETW(ctrl.wValue)) {
  52607. + case UF_DEVICE_REMOTE_WAKEUP:
  52608. + pcd->remote_wakeup_enable = 1;
  52609. + break;
  52610. +
  52611. + case UF_TEST_MODE:
  52612. + /* Setup the Test Mode tasklet to do the Test
  52613. + * Packet generation after the SETUP Status
  52614. + * phase has completed. */
  52615. +
  52616. + /** @todo This has not been tested since the
  52617. + * tasklet struct was put into the PCD
  52618. + * struct! */
  52619. + pcd->test_mode = UGETW(ctrl.wIndex) >> 8;
  52620. + DWC_TASK_SCHEDULE(pcd->test_mode_tasklet);
  52621. + break;
  52622. +
  52623. + case UF_DEVICE_B_HNP_ENABLE:
  52624. + DWC_DEBUGPL(DBG_PCDV,
  52625. + "SET_FEATURE: USB_DEVICE_B_HNP_ENABLE\n");
  52626. +
  52627. + /* dev may initiate HNP */
  52628. + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
  52629. + pcd->b_hnp_enable = 1;
  52630. + dwc_otg_pcd_update_otg(pcd, 0);
  52631. + DWC_DEBUGPL(DBG_PCD, "Request B HNP\n");
  52632. + /**@todo Is the gotgctl.devhnpen cleared
  52633. + * by a USB Reset? */
  52634. + gotgctl.b.devhnpen = 1;
  52635. + gotgctl.b.hnpreq = 1;
  52636. + DWC_WRITE_REG32(&global_regs->gotgctl,
  52637. + gotgctl.d32);
  52638. + } else {
  52639. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  52640. + return;
  52641. + }
  52642. + break;
  52643. +
  52644. + case UF_DEVICE_A_HNP_SUPPORT:
  52645. + /* RH port supports HNP */
  52646. + DWC_DEBUGPL(DBG_PCDV,
  52647. + "SET_FEATURE: USB_DEVICE_A_HNP_SUPPORT\n");
  52648. + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
  52649. + pcd->a_hnp_support = 1;
  52650. + dwc_otg_pcd_update_otg(pcd, 0);
  52651. + } else {
  52652. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  52653. + return;
  52654. + }
  52655. + break;
  52656. +
  52657. + case UF_DEVICE_A_ALT_HNP_SUPPORT:
  52658. + /* other RH port does */
  52659. + DWC_DEBUGPL(DBG_PCDV,
  52660. + "SET_FEATURE: USB_DEVICE_A_ALT_HNP_SUPPORT\n");
  52661. + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
  52662. + pcd->a_alt_hnp_support = 1;
  52663. + dwc_otg_pcd_update_otg(pcd, 0);
  52664. + } else {
  52665. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  52666. + return;
  52667. + }
  52668. + break;
  52669. +
  52670. + default:
  52671. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  52672. + return;
  52673. +
  52674. + }
  52675. + do_setup_in_status_phase(pcd);
  52676. + break;
  52677. +
  52678. + case UT_INTERFACE:
  52679. + do_gadget_setup(pcd, &ctrl);
  52680. + break;
  52681. +
  52682. + case UT_ENDPOINT:
  52683. + if (UGETW(ctrl.wValue) == UF_ENDPOINT_HALT) {
  52684. + ep = get_ep_by_addr(pcd, UGETW(ctrl.wIndex));
  52685. + if (ep == 0) {
  52686. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  52687. + return;
  52688. + }
  52689. + ep->stopped = 1;
  52690. + dwc_otg_ep_set_stall(core_if, &ep->dwc_ep);
  52691. + }
  52692. + do_setup_in_status_phase(pcd);
  52693. + break;
  52694. + }
  52695. +}
  52696. +
  52697. +/**
  52698. + * This function process the CLEAR_FEATURE Setup Commands.
  52699. + */
  52700. +static inline void do_clear_feature(dwc_otg_pcd_t * pcd)
  52701. +{
  52702. + usb_device_request_t ctrl = pcd->setup_pkt->req;
  52703. + dwc_otg_pcd_ep_t *ep = 0;
  52704. +
  52705. + DWC_DEBUGPL(DBG_PCD,
  52706. + "CLEAR_FEATURE:%02x.%02x v%04x i%04x l%04x\n",
  52707. + ctrl.bmRequestType, ctrl.bRequest,
  52708. + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
  52709. + UGETW(ctrl.wLength));
  52710. +
  52711. + switch (UT_GET_RECIPIENT(ctrl.bmRequestType)) {
  52712. + case UT_DEVICE:
  52713. + switch (UGETW(ctrl.wValue)) {
  52714. + case UF_DEVICE_REMOTE_WAKEUP:
  52715. + pcd->remote_wakeup_enable = 0;
  52716. + break;
  52717. +
  52718. + case UF_TEST_MODE:
  52719. + /** @todo Add CLEAR_FEATURE for TEST modes. */
  52720. + break;
  52721. +
  52722. + default:
  52723. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  52724. + return;
  52725. + }
  52726. + do_setup_in_status_phase(pcd);
  52727. + break;
  52728. +
  52729. + case UT_ENDPOINT:
  52730. + ep = get_ep_by_addr(pcd, UGETW(ctrl.wIndex));
  52731. + if (ep == 0) {
  52732. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  52733. + return;
  52734. + }
  52735. +
  52736. + pcd_clear_halt(pcd, ep);
  52737. +
  52738. + break;
  52739. + }
  52740. +}
  52741. +
  52742. +/**
  52743. + * This function process the SET_ADDRESS Setup Commands.
  52744. + */
  52745. +static inline void do_set_address(dwc_otg_pcd_t * pcd)
  52746. +{
  52747. + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
  52748. + usb_device_request_t ctrl = pcd->setup_pkt->req;
  52749. +
  52750. + if (ctrl.bmRequestType == UT_DEVICE) {
  52751. + dcfg_data_t dcfg = {.d32 = 0 };
  52752. +
  52753. +#ifdef DEBUG_EP0
  52754. +// DWC_DEBUGPL(DBG_PCDV, "SET_ADDRESS:%d\n", ctrl.wValue);
  52755. +#endif
  52756. + dcfg.b.devaddr = UGETW(ctrl.wValue);
  52757. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dcfg, 0, dcfg.d32);
  52758. + do_setup_in_status_phase(pcd);
  52759. + }
  52760. +}
  52761. +
  52762. +/**
  52763. + * This function processes SETUP commands. In Linux, the USB Command
  52764. + * processing is done in two places - the first being the PCD and the
  52765. + * second in the Gadget Driver (for example, the File-Backed Storage
  52766. + * Gadget Driver).
  52767. + *
  52768. + * <table>
  52769. + * <tr><td>Command </td><td>Driver </td><td>Description</td></tr>
  52770. + *
  52771. + * <tr><td>GET_STATUS </td><td>PCD </td><td>Command is processed as
  52772. + * defined in chapter 9 of the USB 2.0 Specification chapter 9
  52773. + * </td></tr>
  52774. + *
  52775. + * <tr><td>CLEAR_FEATURE </td><td>PCD </td><td>The Device and Endpoint
  52776. + * requests are the ENDPOINT_HALT feature is procesed, all others the
  52777. + * interface requests are ignored.</td></tr>
  52778. + *
  52779. + * <tr><td>SET_FEATURE </td><td>PCD </td><td>The Device and Endpoint
  52780. + * requests are processed by the PCD. Interface requests are passed
  52781. + * to the Gadget Driver.</td></tr>
  52782. + *
  52783. + * <tr><td>SET_ADDRESS </td><td>PCD </td><td>Program the DCFG reg,
  52784. + * with device address received </td></tr>
  52785. + *
  52786. + * <tr><td>GET_DESCRIPTOR </td><td>Gadget Driver </td><td>Return the
  52787. + * requested descriptor</td></tr>
  52788. + *
  52789. + * <tr><td>SET_DESCRIPTOR </td><td>Gadget Driver </td><td>Optional -
  52790. + * not implemented by any of the existing Gadget Drivers.</td></tr>
  52791. + *
  52792. + * <tr><td>SET_CONFIGURATION </td><td>Gadget Driver </td><td>Disable
  52793. + * all EPs and enable EPs for new configuration.</td></tr>
  52794. + *
  52795. + * <tr><td>GET_CONFIGURATION </td><td>Gadget Driver </td><td>Return
  52796. + * the current configuration</td></tr>
  52797. + *
  52798. + * <tr><td>SET_INTERFACE </td><td>Gadget Driver </td><td>Disable all
  52799. + * EPs and enable EPs for new configuration.</td></tr>
  52800. + *
  52801. + * <tr><td>GET_INTERFACE </td><td>Gadget Driver </td><td>Return the
  52802. + * current interface.</td></tr>
  52803. + *
  52804. + * <tr><td>SYNC_FRAME </td><td>PCD </td><td>Display debug
  52805. + * message.</td></tr>
  52806. + * </table>
  52807. + *
  52808. + * When the SETUP Phase Done interrupt occurs, the PCD SETUP commands are
  52809. + * processed by pcd_setup. Calling the Function Driver's setup function from
  52810. + * pcd_setup processes the gadget SETUP commands.
  52811. + */
  52812. +static inline void pcd_setup(dwc_otg_pcd_t * pcd)
  52813. +{
  52814. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  52815. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  52816. + usb_device_request_t ctrl = pcd->setup_pkt->req;
  52817. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  52818. +
  52819. + deptsiz0_data_t doeptsize0 = {.d32 = 0 };
  52820. +
  52821. +#ifdef DWC_UTE_CFI
  52822. + int retval = 0;
  52823. + struct cfi_usb_ctrlrequest cfi_req;
  52824. +#endif
  52825. +
  52826. + doeptsize0.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[0]->doeptsiz);
  52827. +
  52828. + /** In BDMA more then 1 setup packet is not supported till 3.00a */
  52829. + if (core_if->dma_enable && core_if->dma_desc_enable == 0
  52830. + && (doeptsize0.b.supcnt < 2)
  52831. + && (core_if->snpsid < OTG_CORE_REV_2_94a)) {
  52832. + DWC_ERROR
  52833. + ("\n\n----------- CANNOT handle > 1 setup packet in DMA mode\n\n");
  52834. + }
  52835. + if ((core_if->snpsid >= OTG_CORE_REV_3_00a)
  52836. + && (core_if->dma_enable == 1) && (core_if->dma_desc_enable == 0)) {
  52837. + ctrl =
  52838. + (pcd->setup_pkt +
  52839. + (3 - doeptsize0.b.supcnt - 1 +
  52840. + ep0->dwc_ep.stp_rollover))->req;
  52841. + }
  52842. +#ifdef DEBUG_EP0
  52843. + DWC_DEBUGPL(DBG_PCD, "SETUP %02x.%02x v%04x i%04x l%04x\n",
  52844. + ctrl.bmRequestType, ctrl.bRequest,
  52845. + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
  52846. + UGETW(ctrl.wLength));
  52847. +#endif
  52848. +
  52849. + /* Clean up the request queue */
  52850. + dwc_otg_request_nuke(ep0);
  52851. + ep0->stopped = 0;
  52852. +
  52853. + if (ctrl.bmRequestType & UE_DIR_IN) {
  52854. + ep0->dwc_ep.is_in = 1;
  52855. + pcd->ep0state = EP0_IN_DATA_PHASE;
  52856. + } else {
  52857. + ep0->dwc_ep.is_in = 0;
  52858. + pcd->ep0state = EP0_OUT_DATA_PHASE;
  52859. + }
  52860. +
  52861. + if (UGETW(ctrl.wLength) == 0) {
  52862. + ep0->dwc_ep.is_in = 1;
  52863. + pcd->ep0state = EP0_IN_STATUS_PHASE;
  52864. + }
  52865. +
  52866. + if (UT_GET_TYPE(ctrl.bmRequestType) != UT_STANDARD) {
  52867. +
  52868. +#ifdef DWC_UTE_CFI
  52869. + DWC_MEMCPY(&cfi_req, &ctrl, sizeof(usb_device_request_t));
  52870. +
  52871. + //printk(KERN_ALERT "CFI: req_type=0x%02x; req=0x%02x\n",
  52872. + ctrl.bRequestType, ctrl.bRequest);
  52873. + if (UT_GET_TYPE(cfi_req.bRequestType) == UT_VENDOR) {
  52874. + if (cfi_req.bRequest > 0xB0 && cfi_req.bRequest < 0xBF) {
  52875. + retval = cfi_setup(pcd, &cfi_req);
  52876. + if (retval < 0) {
  52877. + ep0_do_stall(pcd, retval);
  52878. + pcd->ep0_pending = 0;
  52879. + return;
  52880. + }
  52881. +
  52882. + /* if need gadget setup then call it and check the retval */
  52883. + if (pcd->cfi->need_gadget_att) {
  52884. + retval =
  52885. + cfi_gadget_setup(pcd,
  52886. + &pcd->
  52887. + cfi->ctrl_req);
  52888. + if (retval < 0) {
  52889. + pcd->ep0_pending = 0;
  52890. + return;
  52891. + }
  52892. + }
  52893. +
  52894. + if (pcd->cfi->need_status_in_complete) {
  52895. + do_setup_in_status_phase(pcd);
  52896. + }
  52897. + return;
  52898. + }
  52899. + }
  52900. +#endif
  52901. +
  52902. + /* handle non-standard (class/vendor) requests in the gadget driver */
  52903. + do_gadget_setup(pcd, &ctrl);
  52904. + return;
  52905. + }
  52906. +
  52907. + /** @todo NGS: Handle bad setup packet? */
  52908. +
  52909. +///////////////////////////////////////////
  52910. +//// --- Standard Request handling --- ////
  52911. +
  52912. + switch (ctrl.bRequest) {
  52913. + case UR_GET_STATUS:
  52914. + do_get_status(pcd);
  52915. + break;
  52916. +
  52917. + case UR_CLEAR_FEATURE:
  52918. + do_clear_feature(pcd);
  52919. + break;
  52920. +
  52921. + case UR_SET_FEATURE:
  52922. + do_set_feature(pcd);
  52923. + break;
  52924. +
  52925. + case UR_SET_ADDRESS:
  52926. + do_set_address(pcd);
  52927. + break;
  52928. +
  52929. + case UR_SET_INTERFACE:
  52930. + case UR_SET_CONFIG:
  52931. +// _pcd->request_config = 1; /* Configuration changed */
  52932. + do_gadget_setup(pcd, &ctrl);
  52933. + break;
  52934. +
  52935. + case UR_SYNCH_FRAME:
  52936. + do_gadget_setup(pcd, &ctrl);
  52937. + break;
  52938. +
  52939. + default:
  52940. + /* Call the Gadget Driver's setup functions */
  52941. + do_gadget_setup(pcd, &ctrl);
  52942. + break;
  52943. + }
  52944. +}
  52945. +
  52946. +/**
  52947. + * This function completes the ep0 control transfer.
  52948. + */
  52949. +static int32_t ep0_complete_request(dwc_otg_pcd_ep_t * ep)
  52950. +{
  52951. + dwc_otg_core_if_t *core_if = GET_CORE_IF(ep->pcd);
  52952. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  52953. + dwc_otg_dev_in_ep_regs_t *in_ep_regs =
  52954. + dev_if->in_ep_regs[ep->dwc_ep.num];
  52955. +#ifdef DEBUG_EP0
  52956. + dwc_otg_dev_out_ep_regs_t *out_ep_regs =
  52957. + dev_if->out_ep_regs[ep->dwc_ep.num];
  52958. +#endif
  52959. + deptsiz0_data_t deptsiz;
  52960. + dev_dma_desc_sts_t desc_sts;
  52961. + dwc_otg_pcd_request_t *req;
  52962. + int is_last = 0;
  52963. + dwc_otg_pcd_t *pcd = ep->pcd;
  52964. +
  52965. +#ifdef DWC_UTE_CFI
  52966. + struct cfi_usb_ctrlrequest *ctrlreq;
  52967. + int retval = -DWC_E_NOT_SUPPORTED;
  52968. +#endif
  52969. +
  52970. + desc_sts.b.bytes = 0;
  52971. +
  52972. + if (pcd->ep0_pending && DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  52973. + if (ep->dwc_ep.is_in) {
  52974. +#ifdef DEBUG_EP0
  52975. + DWC_DEBUGPL(DBG_PCDV, "Do setup OUT status phase\n");
  52976. +#endif
  52977. + do_setup_out_status_phase(pcd);
  52978. + } else {
  52979. +#ifdef DEBUG_EP0
  52980. + DWC_DEBUGPL(DBG_PCDV, "Do setup IN status phase\n");
  52981. +#endif
  52982. +
  52983. +#ifdef DWC_UTE_CFI
  52984. + ctrlreq = &pcd->cfi->ctrl_req;
  52985. +
  52986. + if (UT_GET_TYPE(ctrlreq->bRequestType) == UT_VENDOR) {
  52987. + if (ctrlreq->bRequest > 0xB0
  52988. + && ctrlreq->bRequest < 0xBF) {
  52989. +
  52990. + /* Return if the PCD failed to handle the request */
  52991. + if ((retval =
  52992. + pcd->cfi->ops.
  52993. + ctrl_write_complete(pcd->cfi,
  52994. + pcd)) < 0) {
  52995. + CFI_INFO
  52996. + ("ERROR setting a new value in the PCD(%d)\n",
  52997. + retval);
  52998. + ep0_do_stall(pcd, retval);
  52999. + pcd->ep0_pending = 0;
  53000. + return 0;
  53001. + }
  53002. +
  53003. + /* If the gadget needs to be notified on the request */
  53004. + if (pcd->cfi->need_gadget_att == 1) {
  53005. + //retval = do_gadget_setup(pcd, &pcd->cfi->ctrl_req);
  53006. + retval =
  53007. + cfi_gadget_setup(pcd,
  53008. + &pcd->cfi->
  53009. + ctrl_req);
  53010. +
  53011. + /* Return from the function if the gadget failed to process
  53012. + * the request properly - this should never happen !!!
  53013. + */
  53014. + if (retval < 0) {
  53015. + CFI_INFO
  53016. + ("ERROR setting a new value in the gadget(%d)\n",
  53017. + retval);
  53018. + pcd->ep0_pending = 0;
  53019. + return 0;
  53020. + }
  53021. + }
  53022. +
  53023. + CFI_INFO("%s: RETVAL=%d\n", __func__,
  53024. + retval);
  53025. + /* If we hit here then the PCD and the gadget has properly
  53026. + * handled the request - so send the ZLP IN to the host.
  53027. + */
  53028. + /* @todo: MAS - decide whether we need to start the setup
  53029. + * stage based on the need_setup value of the cfi object
  53030. + */
  53031. + do_setup_in_status_phase(pcd);
  53032. + pcd->ep0_pending = 0;
  53033. + return 1;
  53034. + }
  53035. + }
  53036. +#endif
  53037. +
  53038. + do_setup_in_status_phase(pcd);
  53039. + }
  53040. + pcd->ep0_pending = 0;
  53041. + return 1;
  53042. + }
  53043. +
  53044. + if (DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  53045. + return 0;
  53046. + }
  53047. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  53048. +
  53049. + if (pcd->ep0state == EP0_OUT_STATUS_PHASE
  53050. + || pcd->ep0state == EP0_IN_STATUS_PHASE) {
  53051. + is_last = 1;
  53052. + } else if (ep->dwc_ep.is_in) {
  53053. + deptsiz.d32 = DWC_READ_REG32(&in_ep_regs->dieptsiz);
  53054. + if (core_if->dma_desc_enable != 0)
  53055. + desc_sts = dev_if->in_desc_addr->status;
  53056. +#ifdef DEBUG_EP0
  53057. + DWC_DEBUGPL(DBG_PCDV, "%d len=%d xfersize=%d pktcnt=%d\n",
  53058. + ep->dwc_ep.num, ep->dwc_ep.xfer_len,
  53059. + deptsiz.b.xfersize, deptsiz.b.pktcnt);
  53060. +#endif
  53061. +
  53062. + if (((core_if->dma_desc_enable == 0)
  53063. + && (deptsiz.b.xfersize == 0))
  53064. + || ((core_if->dma_desc_enable != 0)
  53065. + && (desc_sts.b.bytes == 0))) {
  53066. + req->actual = ep->dwc_ep.xfer_count;
  53067. + /* Is a Zero Len Packet needed? */
  53068. + if (req->sent_zlp) {
  53069. +#ifdef DEBUG_EP0
  53070. + DWC_DEBUGPL(DBG_PCD, "Setup Rx ZLP\n");
  53071. +#endif
  53072. + req->sent_zlp = 0;
  53073. + }
  53074. + do_setup_out_status_phase(pcd);
  53075. + }
  53076. + } else {
  53077. + /* ep0-OUT */
  53078. +#ifdef DEBUG_EP0
  53079. + deptsiz.d32 = DWC_READ_REG32(&out_ep_regs->doeptsiz);
  53080. + DWC_DEBUGPL(DBG_PCDV, "%d len=%d xsize=%d pktcnt=%d\n",
  53081. + ep->dwc_ep.num, ep->dwc_ep.xfer_len,
  53082. + deptsiz.b.xfersize, deptsiz.b.pktcnt);
  53083. +#endif
  53084. + req->actual = ep->dwc_ep.xfer_count;
  53085. +
  53086. + /* Is a Zero Len Packet needed? */
  53087. + if (req->sent_zlp) {
  53088. +#ifdef DEBUG_EP0
  53089. + DWC_DEBUGPL(DBG_PCDV, "Setup Tx ZLP\n");
  53090. +#endif
  53091. + req->sent_zlp = 0;
  53092. + }
  53093. + /* For older cores do setup in status phase in Slave/BDMA modes,
  53094. + * starting from 3.00 do that only in slave, and for DMA modes
  53095. + * just re-enable ep 0 OUT here*/
  53096. + if (core_if->dma_enable == 0
  53097. + || (core_if->dma_desc_enable == 0
  53098. + && core_if->snpsid <= OTG_CORE_REV_2_94a)) {
  53099. + do_setup_in_status_phase(pcd);
  53100. + } else if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  53101. + DWC_DEBUGPL(DBG_PCDV,
  53102. + "Enable out ep before in status phase\n");
  53103. + ep0_out_start(core_if, pcd);
  53104. + }
  53105. + }
  53106. +
  53107. + /* Complete the request */
  53108. + if (is_last) {
  53109. + dwc_otg_request_done(ep, req, 0);
  53110. + ep->dwc_ep.start_xfer_buff = 0;
  53111. + ep->dwc_ep.xfer_buff = 0;
  53112. + ep->dwc_ep.xfer_len = 0;
  53113. + return 1;
  53114. + }
  53115. + return 0;
  53116. +}
  53117. +
  53118. +#ifdef DWC_UTE_CFI
  53119. +/**
  53120. + * This function calculates traverses all the CFI DMA descriptors and
  53121. + * and accumulates the bytes that are left to be transfered.
  53122. + *
  53123. + * @return The total bytes left to transfered, or a negative value as failure
  53124. + */
  53125. +static inline int cfi_calc_desc_residue(dwc_otg_pcd_ep_t * ep)
  53126. +{
  53127. + int32_t ret = 0;
  53128. + int i;
  53129. + struct dwc_otg_dma_desc *ddesc = NULL;
  53130. + struct cfi_ep *cfiep;
  53131. +
  53132. + /* See if the pcd_ep has its respective cfi_ep mapped */
  53133. + cfiep = get_cfi_ep_by_pcd_ep(ep->pcd->cfi, ep);
  53134. + if (!cfiep) {
  53135. + CFI_INFO("%s: Failed to find ep\n", __func__);
  53136. + return -1;
  53137. + }
  53138. +
  53139. + ddesc = ep->dwc_ep.descs;
  53140. +
  53141. + for (i = 0; (i < cfiep->desc_count) && (i < MAX_DMA_DESCS_PER_EP); i++) {
  53142. +
  53143. +#if defined(PRINT_CFI_DMA_DESCS)
  53144. + print_desc(ddesc, ep->ep.name, i);
  53145. +#endif
  53146. + ret += ddesc->status.b.bytes;
  53147. + ddesc++;
  53148. + }
  53149. +
  53150. + if (ret)
  53151. + CFI_INFO("!!!!!!!!!! WARNING (%s) - residue=%d\n", __func__,
  53152. + ret);
  53153. +
  53154. + return ret;
  53155. +}
  53156. +#endif
  53157. +
  53158. +/**
  53159. + * This function completes the request for the EP. If there are
  53160. + * additional requests for the EP in the queue they will be started.
  53161. + */
  53162. +static void complete_ep(dwc_otg_pcd_ep_t * ep)
  53163. +{
  53164. + dwc_otg_core_if_t *core_if = GET_CORE_IF(ep->pcd);
  53165. + struct device *dev = dwc_otg_pcd_to_dev(ep->pcd);
  53166. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  53167. + dwc_otg_dev_in_ep_regs_t *in_ep_regs =
  53168. + dev_if->in_ep_regs[ep->dwc_ep.num];
  53169. + deptsiz_data_t deptsiz;
  53170. + dev_dma_desc_sts_t desc_sts;
  53171. + dwc_otg_pcd_request_t *req = 0;
  53172. + dwc_otg_dev_dma_desc_t *dma_desc;
  53173. + uint32_t byte_count = 0;
  53174. + int is_last = 0;
  53175. + int i;
  53176. +
  53177. + DWC_DEBUGPL(DBG_PCDV, "%s() %d-%s\n", __func__, ep->dwc_ep.num,
  53178. + (ep->dwc_ep.is_in ? "IN" : "OUT"));
  53179. +
  53180. + /* Get any pending requests */
  53181. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  53182. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  53183. + if (!req) {
  53184. + DWC_PRINTF("complete_ep 0x%p, req = NULL!\n", ep);
  53185. + return;
  53186. + }
  53187. + } else {
  53188. + DWC_PRINTF("complete_ep 0x%p, ep->queue empty!\n", ep);
  53189. + return;
  53190. + }
  53191. +
  53192. + DWC_DEBUGPL(DBG_PCD, "Requests %d\n", ep->pcd->request_pending);
  53193. +
  53194. + if (ep->dwc_ep.is_in) {
  53195. + deptsiz.d32 = DWC_READ_REG32(&in_ep_regs->dieptsiz);
  53196. +
  53197. + if (core_if->dma_enable) {
  53198. + if (core_if->dma_desc_enable == 0) {
  53199. + if (deptsiz.b.xfersize == 0
  53200. + && deptsiz.b.pktcnt == 0) {
  53201. + byte_count =
  53202. + ep->dwc_ep.xfer_len -
  53203. + ep->dwc_ep.xfer_count;
  53204. +
  53205. + ep->dwc_ep.xfer_buff += byte_count;
  53206. + ep->dwc_ep.dma_addr += byte_count;
  53207. + ep->dwc_ep.xfer_count += byte_count;
  53208. +
  53209. + DWC_DEBUGPL(DBG_PCDV,
  53210. + "%d-%s len=%d xfersize=%d pktcnt=%d\n",
  53211. + ep->dwc_ep.num,
  53212. + (ep->dwc_ep.
  53213. + is_in ? "IN" : "OUT"),
  53214. + ep->dwc_ep.xfer_len,
  53215. + deptsiz.b.xfersize,
  53216. + deptsiz.b.pktcnt);
  53217. +
  53218. + if (ep->dwc_ep.xfer_len <
  53219. + ep->dwc_ep.total_len) {
  53220. + dwc_otg_ep_start_transfer
  53221. + (core_if, &ep->dwc_ep);
  53222. + } else if (ep->dwc_ep.sent_zlp) {
  53223. + /*
  53224. + * This fragment of code should initiate 0
  53225. + * length transfer in case if it is queued
  53226. + * a transfer with size divisible to EPs max
  53227. + * packet size and with usb_request zero field
  53228. + * is set, which means that after data is transfered,
  53229. + * it is also should be transfered
  53230. + * a 0 length packet at the end. For Slave and
  53231. + * Buffer DMA modes in this case SW has
  53232. + * to initiate 2 transfers one with transfer size,
  53233. + * and the second with 0 size. For Descriptor
  53234. + * DMA mode SW is able to initiate a transfer,
  53235. + * which will handle all the packets including
  53236. + * the last 0 length.
  53237. + */
  53238. + ep->dwc_ep.sent_zlp = 0;
  53239. + dwc_otg_ep_start_zl_transfer
  53240. + (core_if, &ep->dwc_ep);
  53241. + } else {
  53242. + is_last = 1;
  53243. + }
  53244. + } else {
  53245. + if (ep->dwc_ep.type ==
  53246. + DWC_OTG_EP_TYPE_ISOC) {
  53247. + req->actual = 0;
  53248. + dwc_otg_request_done(ep, req, 0);
  53249. +
  53250. + ep->dwc_ep.start_xfer_buff = 0;
  53251. + ep->dwc_ep.xfer_buff = 0;
  53252. + ep->dwc_ep.xfer_len = 0;
  53253. +
  53254. + /* If there is a request in the queue start it. */
  53255. + start_next_request(ep);
  53256. + } else
  53257. + DWC_WARN
  53258. + ("Incomplete transfer (%d - %s [siz=%d pkt=%d])\n",
  53259. + ep->dwc_ep.num,
  53260. + (ep->dwc_ep.is_in ? "IN" : "OUT"),
  53261. + deptsiz.b.xfersize,
  53262. + deptsiz.b.pktcnt);
  53263. + }
  53264. + } else {
  53265. + dma_desc = ep->dwc_ep.desc_addr;
  53266. + byte_count = 0;
  53267. + ep->dwc_ep.sent_zlp = 0;
  53268. +
  53269. +#ifdef DWC_UTE_CFI
  53270. + CFI_INFO("%s: BUFFER_MODE=%d\n", __func__,
  53271. + ep->dwc_ep.buff_mode);
  53272. + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
  53273. + int residue;
  53274. +
  53275. + residue = cfi_calc_desc_residue(ep);
  53276. + if (residue < 0)
  53277. + return;
  53278. +
  53279. + byte_count = residue;
  53280. + } else {
  53281. +#endif
  53282. + for (i = 0; i < ep->dwc_ep.desc_cnt;
  53283. + ++i) {
  53284. + desc_sts = dma_desc->status;
  53285. + byte_count += desc_sts.b.bytes;
  53286. + dma_desc++;
  53287. + }
  53288. +#ifdef DWC_UTE_CFI
  53289. + }
  53290. +#endif
  53291. + if (byte_count == 0) {
  53292. + ep->dwc_ep.xfer_count =
  53293. + ep->dwc_ep.total_len;
  53294. + is_last = 1;
  53295. + } else {
  53296. + DWC_WARN("Incomplete transfer\n");
  53297. + }
  53298. + }
  53299. + } else {
  53300. + if (deptsiz.b.xfersize == 0 && deptsiz.b.pktcnt == 0) {
  53301. + DWC_DEBUGPL(DBG_PCDV,
  53302. + "%d-%s len=%d xfersize=%d pktcnt=%d\n",
  53303. + ep->dwc_ep.num,
  53304. + ep->dwc_ep.is_in ? "IN" : "OUT",
  53305. + ep->dwc_ep.xfer_len,
  53306. + deptsiz.b.xfersize,
  53307. + deptsiz.b.pktcnt);
  53308. +
  53309. + /* Check if the whole transfer was completed,
  53310. + * if no, setup transfer for next portion of data
  53311. + */
  53312. + if (ep->dwc_ep.xfer_len < ep->dwc_ep.total_len) {
  53313. + dwc_otg_ep_start_transfer(core_if,
  53314. + &ep->dwc_ep);
  53315. + } else if (ep->dwc_ep.sent_zlp) {
  53316. + /*
  53317. + * This fragment of code should initiate 0
  53318. + * length trasfer in case if it is queued
  53319. + * a trasfer with size divisible to EPs max
  53320. + * packet size and with usb_request zero field
  53321. + * is set, which means that after data is transfered,
  53322. + * it is also should be transfered
  53323. + * a 0 length packet at the end. For Slave and
  53324. + * Buffer DMA modes in this case SW has
  53325. + * to initiate 2 transfers one with transfer size,
  53326. + * and the second with 0 size. For Desriptor
  53327. + * DMA mode SW is able to initiate a transfer,
  53328. + * which will handle all the packets including
  53329. + * the last 0 legth.
  53330. + */
  53331. + ep->dwc_ep.sent_zlp = 0;
  53332. + dwc_otg_ep_start_zl_transfer(core_if,
  53333. + &ep->dwc_ep);
  53334. + } else {
  53335. + is_last = 1;
  53336. + }
  53337. + } else {
  53338. + DWC_WARN
  53339. + ("Incomplete transfer (%d-%s [siz=%d pkt=%d])\n",
  53340. + ep->dwc_ep.num,
  53341. + (ep->dwc_ep.is_in ? "IN" : "OUT"),
  53342. + deptsiz.b.xfersize, deptsiz.b.pktcnt);
  53343. + }
  53344. + }
  53345. + } else {
  53346. + dwc_otg_dev_out_ep_regs_t *out_ep_regs =
  53347. + dev_if->out_ep_regs[ep->dwc_ep.num];
  53348. + desc_sts.d32 = 0;
  53349. + if (core_if->dma_enable) {
  53350. + if (core_if->dma_desc_enable) {
  53351. + dma_desc = ep->dwc_ep.desc_addr;
  53352. + byte_count = 0;
  53353. + ep->dwc_ep.sent_zlp = 0;
  53354. +
  53355. +#ifdef DWC_UTE_CFI
  53356. + CFI_INFO("%s: BUFFER_MODE=%d\n", __func__,
  53357. + ep->dwc_ep.buff_mode);
  53358. + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
  53359. + int residue;
  53360. + residue = cfi_calc_desc_residue(ep);
  53361. + if (residue < 0)
  53362. + return;
  53363. + byte_count = residue;
  53364. + } else {
  53365. +#endif
  53366. +
  53367. + for (i = 0; i < ep->dwc_ep.desc_cnt;
  53368. + ++i) {
  53369. + desc_sts = dma_desc->status;
  53370. + byte_count += desc_sts.b.bytes;
  53371. + dma_desc++;
  53372. + }
  53373. +
  53374. +#ifdef DWC_UTE_CFI
  53375. + }
  53376. +#endif
  53377. + /* Checking for interrupt Out transfers with not
  53378. + * dword aligned mps sizes
  53379. + */
  53380. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_INTR &&
  53381. + (ep->dwc_ep.maxpacket%4)) {
  53382. + ep->dwc_ep.xfer_count =
  53383. + ep->dwc_ep.total_len - byte_count;
  53384. + if ((ep->dwc_ep.xfer_len %
  53385. + ep->dwc_ep.maxpacket)
  53386. + && (ep->dwc_ep.xfer_len /
  53387. + ep->dwc_ep.maxpacket <
  53388. + MAX_DMA_DESC_CNT))
  53389. + ep->dwc_ep.xfer_len -=
  53390. + (ep->dwc_ep.desc_cnt -
  53391. + 1) * ep->dwc_ep.maxpacket +
  53392. + ep->dwc_ep.xfer_len %
  53393. + ep->dwc_ep.maxpacket;
  53394. + else
  53395. + ep->dwc_ep.xfer_len -=
  53396. + ep->dwc_ep.desc_cnt *
  53397. + ep->dwc_ep.maxpacket;
  53398. + if (ep->dwc_ep.xfer_len > 0) {
  53399. + dwc_otg_ep_start_transfer
  53400. + (core_if, &ep->dwc_ep);
  53401. + } else {
  53402. + is_last = 1;
  53403. + }
  53404. + } else {
  53405. + ep->dwc_ep.xfer_count =
  53406. + ep->dwc_ep.total_len - byte_count +
  53407. + ((4 -
  53408. + (ep->dwc_ep.
  53409. + total_len & 0x3)) & 0x3);
  53410. + is_last = 1;
  53411. + }
  53412. + } else {
  53413. + deptsiz.d32 = 0;
  53414. + deptsiz.d32 =
  53415. + DWC_READ_REG32(&out_ep_regs->doeptsiz);
  53416. +
  53417. + byte_count = (ep->dwc_ep.xfer_len -
  53418. + ep->dwc_ep.xfer_count -
  53419. + deptsiz.b.xfersize);
  53420. + ep->dwc_ep.xfer_buff += byte_count;
  53421. + ep->dwc_ep.dma_addr += byte_count;
  53422. + ep->dwc_ep.xfer_count += byte_count;
  53423. +
  53424. + /* Check if the whole transfer was completed,
  53425. + * if no, setup transfer for next portion of data
  53426. + */
  53427. + if (ep->dwc_ep.xfer_len < ep->dwc_ep.total_len) {
  53428. + dwc_otg_ep_start_transfer(core_if,
  53429. + &ep->dwc_ep);
  53430. + } else if (ep->dwc_ep.sent_zlp) {
  53431. + /*
  53432. + * This fragment of code should initiate 0
  53433. + * length trasfer in case if it is queued
  53434. + * a trasfer with size divisible to EPs max
  53435. + * packet size and with usb_request zero field
  53436. + * is set, which means that after data is transfered,
  53437. + * it is also should be transfered
  53438. + * a 0 length packet at the end. For Slave and
  53439. + * Buffer DMA modes in this case SW has
  53440. + * to initiate 2 transfers one with transfer size,
  53441. + * and the second with 0 size. For Desriptor
  53442. + * DMA mode SW is able to initiate a transfer,
  53443. + * which will handle all the packets including
  53444. + * the last 0 legth.
  53445. + */
  53446. + ep->dwc_ep.sent_zlp = 0;
  53447. + dwc_otg_ep_start_zl_transfer(core_if,
  53448. + &ep->dwc_ep);
  53449. + } else {
  53450. + is_last = 1;
  53451. + }
  53452. + }
  53453. + } else {
  53454. + /* Check if the whole transfer was completed,
  53455. + * if no, setup transfer for next portion of data
  53456. + */
  53457. + if (ep->dwc_ep.xfer_len < ep->dwc_ep.total_len) {
  53458. + dwc_otg_ep_start_transfer(core_if, &ep->dwc_ep);
  53459. + } else if (ep->dwc_ep.sent_zlp) {
  53460. + /*
  53461. + * This fragment of code should initiate 0
  53462. + * length transfer in case if it is queued
  53463. + * a transfer with size divisible to EPs max
  53464. + * packet size and with usb_request zero field
  53465. + * is set, which means that after data is transfered,
  53466. + * it is also should be transfered
  53467. + * a 0 length packet at the end. For Slave and
  53468. + * Buffer DMA modes in this case SW has
  53469. + * to initiate 2 transfers one with transfer size,
  53470. + * and the second with 0 size. For Descriptor
  53471. + * DMA mode SW is able to initiate a transfer,
  53472. + * which will handle all the packets including
  53473. + * the last 0 length.
  53474. + */
  53475. + ep->dwc_ep.sent_zlp = 0;
  53476. + dwc_otg_ep_start_zl_transfer(core_if,
  53477. + &ep->dwc_ep);
  53478. + } else {
  53479. + is_last = 1;
  53480. + }
  53481. + }
  53482. +
  53483. + DWC_DEBUGPL(DBG_PCDV,
  53484. + "addr %p, %d-%s len=%d cnt=%d xsize=%d pktcnt=%d\n",
  53485. + &out_ep_regs->doeptsiz, ep->dwc_ep.num,
  53486. + ep->dwc_ep.is_in ? "IN" : "OUT",
  53487. + ep->dwc_ep.xfer_len, ep->dwc_ep.xfer_count,
  53488. + deptsiz.b.xfersize, deptsiz.b.pktcnt);
  53489. + }
  53490. +
  53491. + /* Complete the request */
  53492. + if (is_last) {
  53493. +#ifdef DWC_UTE_CFI
  53494. + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
  53495. + req->actual = ep->dwc_ep.cfi_req_len - byte_count;
  53496. + } else {
  53497. +#endif
  53498. + req->actual = ep->dwc_ep.xfer_count;
  53499. +#ifdef DWC_UTE_CFI
  53500. + }
  53501. +#endif
  53502. + if (req->dw_align_buf) {
  53503. + if (!ep->dwc_ep.is_in) {
  53504. + dwc_memcpy(req->buf, req->dw_align_buf, req->length);
  53505. + }
  53506. + DWC_DMA_FREE(dev, req->length, req->dw_align_buf,
  53507. + req->dw_align_buf_dma);
  53508. + }
  53509. +
  53510. + dwc_otg_request_done(ep, req, 0);
  53511. +
  53512. + ep->dwc_ep.start_xfer_buff = 0;
  53513. + ep->dwc_ep.xfer_buff = 0;
  53514. + ep->dwc_ep.xfer_len = 0;
  53515. +
  53516. + /* If there is a request in the queue start it. */
  53517. + start_next_request(ep);
  53518. + }
  53519. +}
  53520. +
  53521. +#ifdef DWC_EN_ISOC
  53522. +
  53523. +/**
  53524. + * This function BNA interrupt for Isochronous EPs
  53525. + *
  53526. + */
  53527. +static void dwc_otg_pcd_handle_iso_bna(dwc_otg_pcd_ep_t * ep)
  53528. +{
  53529. + dwc_ep_t *dwc_ep = &ep->dwc_ep;
  53530. + volatile uint32_t *addr;
  53531. + depctl_data_t depctl = {.d32 = 0 };
  53532. + dwc_otg_pcd_t *pcd = ep->pcd;
  53533. + dwc_otg_dev_dma_desc_t *dma_desc;
  53534. + int i;
  53535. +
  53536. + dma_desc =
  53537. + dwc_ep->iso_desc_addr + dwc_ep->desc_cnt * (dwc_ep->proc_buf_num);
  53538. +
  53539. + if (dwc_ep->is_in) {
  53540. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  53541. + for (i = 0; i < dwc_ep->desc_cnt; ++i, ++dma_desc) {
  53542. + sts.d32 = dma_desc->status.d32;
  53543. + sts.b_iso_in.bs = BS_HOST_READY;
  53544. + dma_desc->status.d32 = sts.d32;
  53545. + }
  53546. + } else {
  53547. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  53548. + for (i = 0; i < dwc_ep->desc_cnt; ++i, ++dma_desc) {
  53549. + sts.d32 = dma_desc->status.d32;
  53550. + sts.b_iso_out.bs = BS_HOST_READY;
  53551. + dma_desc->status.d32 = sts.d32;
  53552. + }
  53553. + }
  53554. +
  53555. + if (dwc_ep->is_in == 0) {
  53556. + addr =
  53557. + &GET_CORE_IF(pcd)->dev_if->out_ep_regs[dwc_ep->
  53558. + num]->doepctl;
  53559. + } else {
  53560. + addr =
  53561. + &GET_CORE_IF(pcd)->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
  53562. + }
  53563. + depctl.b.epena = 1;
  53564. + DWC_MODIFY_REG32(addr, depctl.d32, depctl.d32);
  53565. +}
  53566. +
  53567. +/**
  53568. + * This function sets latest iso packet information(non-PTI mode)
  53569. + *
  53570. + * @param core_if Programming view of DWC_otg controller.
  53571. + * @param ep The EP to start the transfer on.
  53572. + *
  53573. + */
  53574. +void set_current_pkt_info(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  53575. +{
  53576. + deptsiz_data_t deptsiz = {.d32 = 0 };
  53577. + dma_addr_t dma_addr;
  53578. + uint32_t offset;
  53579. +
  53580. + if (ep->proc_buf_num)
  53581. + dma_addr = ep->dma_addr1;
  53582. + else
  53583. + dma_addr = ep->dma_addr0;
  53584. +
  53585. + if (ep->is_in) {
  53586. + deptsiz.d32 =
  53587. + DWC_READ_REG32(&core_if->dev_if->
  53588. + in_ep_regs[ep->num]->dieptsiz);
  53589. + offset = ep->data_per_frame;
  53590. + } else {
  53591. + deptsiz.d32 =
  53592. + DWC_READ_REG32(&core_if->dev_if->
  53593. + out_ep_regs[ep->num]->doeptsiz);
  53594. + offset =
  53595. + ep->data_per_frame +
  53596. + (0x4 & (0x4 - (ep->data_per_frame & 0x3)));
  53597. + }
  53598. +
  53599. + if (!deptsiz.b.xfersize) {
  53600. + ep->pkt_info[ep->cur_pkt].length = ep->data_per_frame;
  53601. + ep->pkt_info[ep->cur_pkt].offset =
  53602. + ep->cur_pkt_dma_addr - dma_addr;
  53603. + ep->pkt_info[ep->cur_pkt].status = 0;
  53604. + } else {
  53605. + ep->pkt_info[ep->cur_pkt].length = ep->data_per_frame;
  53606. + ep->pkt_info[ep->cur_pkt].offset =
  53607. + ep->cur_pkt_dma_addr - dma_addr;
  53608. + ep->pkt_info[ep->cur_pkt].status = -DWC_E_NO_DATA;
  53609. + }
  53610. + ep->cur_pkt_addr += offset;
  53611. + ep->cur_pkt_dma_addr += offset;
  53612. + ep->cur_pkt++;
  53613. +}
  53614. +
  53615. +/**
  53616. + * This function sets latest iso packet information(DDMA mode)
  53617. + *
  53618. + * @param core_if Programming view of DWC_otg controller.
  53619. + * @param dwc_ep The EP to start the transfer on.
  53620. + *
  53621. + */
  53622. +static void set_ddma_iso_pkts_info(dwc_otg_core_if_t * core_if,
  53623. + dwc_ep_t * dwc_ep)
  53624. +{
  53625. + dwc_otg_dev_dma_desc_t *dma_desc;
  53626. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  53627. + iso_pkt_info_t *iso_packet;
  53628. + uint32_t data_per_desc;
  53629. + uint32_t offset;
  53630. + int i, j;
  53631. +
  53632. + iso_packet = dwc_ep->pkt_info;
  53633. +
  53634. + /** Reinit closed DMA Descriptors*/
  53635. + /** ISO OUT EP */
  53636. + if (dwc_ep->is_in == 0) {
  53637. + dma_desc =
  53638. + dwc_ep->iso_desc_addr +
  53639. + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
  53640. + offset = 0;
  53641. +
  53642. + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
  53643. + i += dwc_ep->pkt_per_frm) {
  53644. + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
  53645. + data_per_desc =
  53646. + ((j + 1) * dwc_ep->maxpacket >
  53647. + dwc_ep->
  53648. + data_per_frame) ? dwc_ep->data_per_frame -
  53649. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  53650. + data_per_desc +=
  53651. + (data_per_desc % 4) ? (4 -
  53652. + data_per_desc %
  53653. + 4) : 0;
  53654. +
  53655. + sts.d32 = dma_desc->status.d32;
  53656. +
  53657. + /* Write status in iso_packet_decsriptor */
  53658. + iso_packet->status =
  53659. + sts.b_iso_out.rxsts +
  53660. + (sts.b_iso_out.bs ^ BS_DMA_DONE);
  53661. + if (iso_packet->status) {
  53662. + iso_packet->status = -DWC_E_NO_DATA;
  53663. + }
  53664. +
  53665. + /* Received data length */
  53666. + if (!sts.b_iso_out.rxbytes) {
  53667. + iso_packet->length =
  53668. + data_per_desc -
  53669. + sts.b_iso_out.rxbytes;
  53670. + } else {
  53671. + iso_packet->length =
  53672. + data_per_desc -
  53673. + sts.b_iso_out.rxbytes + (4 -
  53674. + dwc_ep->data_per_frame
  53675. + % 4);
  53676. + }
  53677. +
  53678. + iso_packet->offset = offset;
  53679. +
  53680. + offset += data_per_desc;
  53681. + dma_desc++;
  53682. + iso_packet++;
  53683. + }
  53684. + }
  53685. +
  53686. + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
  53687. + data_per_desc =
  53688. + ((j + 1) * dwc_ep->maxpacket >
  53689. + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
  53690. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  53691. + data_per_desc +=
  53692. + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
  53693. +
  53694. + sts.d32 = dma_desc->status.d32;
  53695. +
  53696. + /* Write status in iso_packet_decsriptor */
  53697. + iso_packet->status =
  53698. + sts.b_iso_out.rxsts +
  53699. + (sts.b_iso_out.bs ^ BS_DMA_DONE);
  53700. + if (iso_packet->status) {
  53701. + iso_packet->status = -DWC_E_NO_DATA;
  53702. + }
  53703. +
  53704. + /* Received data length */
  53705. + iso_packet->length =
  53706. + dwc_ep->data_per_frame - sts.b_iso_out.rxbytes;
  53707. +
  53708. + iso_packet->offset = offset;
  53709. +
  53710. + offset += data_per_desc;
  53711. + iso_packet++;
  53712. + dma_desc++;
  53713. + }
  53714. +
  53715. + sts.d32 = dma_desc->status.d32;
  53716. +
  53717. + /* Write status in iso_packet_decsriptor */
  53718. + iso_packet->status =
  53719. + sts.b_iso_out.rxsts + (sts.b_iso_out.bs ^ BS_DMA_DONE);
  53720. + if (iso_packet->status) {
  53721. + iso_packet->status = -DWC_E_NO_DATA;
  53722. + }
  53723. + /* Received data length */
  53724. + if (!sts.b_iso_out.rxbytes) {
  53725. + iso_packet->length =
  53726. + dwc_ep->data_per_frame - sts.b_iso_out.rxbytes;
  53727. + } else {
  53728. + iso_packet->length =
  53729. + dwc_ep->data_per_frame - sts.b_iso_out.rxbytes +
  53730. + (4 - dwc_ep->data_per_frame % 4);
  53731. + }
  53732. +
  53733. + iso_packet->offset = offset;
  53734. + } else {
  53735. +/** ISO IN EP */
  53736. +
  53737. + dma_desc =
  53738. + dwc_ep->iso_desc_addr +
  53739. + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
  53740. +
  53741. + for (i = 0; i < dwc_ep->desc_cnt - 1; i++) {
  53742. + sts.d32 = dma_desc->status.d32;
  53743. +
  53744. + /* Write status in iso packet descriptor */
  53745. + iso_packet->status =
  53746. + sts.b_iso_in.txsts +
  53747. + (sts.b_iso_in.bs ^ BS_DMA_DONE);
  53748. + if (iso_packet->status != 0) {
  53749. + iso_packet->status = -DWC_E_NO_DATA;
  53750. +
  53751. + }
  53752. + /* Bytes has been transfered */
  53753. + iso_packet->length =
  53754. + dwc_ep->data_per_frame - sts.b_iso_in.txbytes;
  53755. +
  53756. + dma_desc++;
  53757. + iso_packet++;
  53758. + }
  53759. +
  53760. + sts.d32 = dma_desc->status.d32;
  53761. + while (sts.b_iso_in.bs == BS_DMA_BUSY) {
  53762. + sts.d32 = dma_desc->status.d32;
  53763. + }
  53764. +
  53765. + /* Write status in iso packet descriptor ??? do be done with ERROR codes */
  53766. + iso_packet->status =
  53767. + sts.b_iso_in.txsts + (sts.b_iso_in.bs ^ BS_DMA_DONE);
  53768. + if (iso_packet->status != 0) {
  53769. + iso_packet->status = -DWC_E_NO_DATA;
  53770. + }
  53771. +
  53772. + /* Bytes has been transfered */
  53773. + iso_packet->length =
  53774. + dwc_ep->data_per_frame - sts.b_iso_in.txbytes;
  53775. + }
  53776. +}
  53777. +
  53778. +/**
  53779. + * This function reinitialize DMA Descriptors for Isochronous transfer
  53780. + *
  53781. + * @param core_if Programming view of DWC_otg controller.
  53782. + * @param dwc_ep The EP to start the transfer on.
  53783. + *
  53784. + */
  53785. +static void reinit_ddma_iso_xfer(dwc_otg_core_if_t * core_if, dwc_ep_t * dwc_ep)
  53786. +{
  53787. + int i, j;
  53788. + dwc_otg_dev_dma_desc_t *dma_desc;
  53789. + dma_addr_t dma_ad;
  53790. + volatile uint32_t *addr;
  53791. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  53792. + uint32_t data_per_desc;
  53793. +
  53794. + if (dwc_ep->is_in == 0) {
  53795. + addr = &core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl;
  53796. + } else {
  53797. + addr = &core_if->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
  53798. + }
  53799. +
  53800. + if (dwc_ep->proc_buf_num == 0) {
  53801. + /** Buffer 0 descriptors setup */
  53802. + dma_ad = dwc_ep->dma_addr0;
  53803. + } else {
  53804. + /** Buffer 1 descriptors setup */
  53805. + dma_ad = dwc_ep->dma_addr1;
  53806. + }
  53807. +
  53808. + /** Reinit closed DMA Descriptors*/
  53809. + /** ISO OUT EP */
  53810. + if (dwc_ep->is_in == 0) {
  53811. + dma_desc =
  53812. + dwc_ep->iso_desc_addr +
  53813. + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
  53814. +
  53815. + sts.b_iso_out.bs = BS_HOST_READY;
  53816. + sts.b_iso_out.rxsts = 0;
  53817. + sts.b_iso_out.l = 0;
  53818. + sts.b_iso_out.sp = 0;
  53819. + sts.b_iso_out.ioc = 0;
  53820. + sts.b_iso_out.pid = 0;
  53821. + sts.b_iso_out.framenum = 0;
  53822. +
  53823. + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
  53824. + i += dwc_ep->pkt_per_frm) {
  53825. + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
  53826. + data_per_desc =
  53827. + ((j + 1) * dwc_ep->maxpacket >
  53828. + dwc_ep->
  53829. + data_per_frame) ? dwc_ep->data_per_frame -
  53830. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  53831. + data_per_desc +=
  53832. + (data_per_desc % 4) ? (4 -
  53833. + data_per_desc %
  53834. + 4) : 0;
  53835. + sts.b_iso_out.rxbytes = data_per_desc;
  53836. + dma_desc->buf = dma_ad;
  53837. + dma_desc->status.d32 = sts.d32;
  53838. +
  53839. + dma_ad += data_per_desc;
  53840. + dma_desc++;
  53841. + }
  53842. + }
  53843. +
  53844. + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
  53845. +
  53846. + data_per_desc =
  53847. + ((j + 1) * dwc_ep->maxpacket >
  53848. + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
  53849. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  53850. + data_per_desc +=
  53851. + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
  53852. + sts.b_iso_out.rxbytes = data_per_desc;
  53853. +
  53854. + dma_desc->buf = dma_ad;
  53855. + dma_desc->status.d32 = sts.d32;
  53856. +
  53857. + dma_desc++;
  53858. + dma_ad += data_per_desc;
  53859. + }
  53860. +
  53861. + sts.b_iso_out.ioc = 1;
  53862. + sts.b_iso_out.l = dwc_ep->proc_buf_num;
  53863. +
  53864. + data_per_desc =
  53865. + ((j + 1) * dwc_ep->maxpacket >
  53866. + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
  53867. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  53868. + data_per_desc +=
  53869. + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
  53870. + sts.b_iso_out.rxbytes = data_per_desc;
  53871. +
  53872. + dma_desc->buf = dma_ad;
  53873. + dma_desc->status.d32 = sts.d32;
  53874. + } else {
  53875. +/** ISO IN EP */
  53876. +
  53877. + dma_desc =
  53878. + dwc_ep->iso_desc_addr +
  53879. + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
  53880. +
  53881. + sts.b_iso_in.bs = BS_HOST_READY;
  53882. + sts.b_iso_in.txsts = 0;
  53883. + sts.b_iso_in.sp = 0;
  53884. + sts.b_iso_in.ioc = 0;
  53885. + sts.b_iso_in.pid = dwc_ep->pkt_per_frm;
  53886. + sts.b_iso_in.framenum = dwc_ep->next_frame;
  53887. + sts.b_iso_in.txbytes = dwc_ep->data_per_frame;
  53888. + sts.b_iso_in.l = 0;
  53889. +
  53890. + for (i = 0; i < dwc_ep->desc_cnt - 1; i++) {
  53891. + dma_desc->buf = dma_ad;
  53892. + dma_desc->status.d32 = sts.d32;
  53893. +
  53894. + sts.b_iso_in.framenum += dwc_ep->bInterval;
  53895. + dma_ad += dwc_ep->data_per_frame;
  53896. + dma_desc++;
  53897. + }
  53898. +
  53899. + sts.b_iso_in.ioc = 1;
  53900. + sts.b_iso_in.l = dwc_ep->proc_buf_num;
  53901. +
  53902. + dma_desc->buf = dma_ad;
  53903. + dma_desc->status.d32 = sts.d32;
  53904. +
  53905. + dwc_ep->next_frame =
  53906. + sts.b_iso_in.framenum + dwc_ep->bInterval * 1;
  53907. + }
  53908. + dwc_ep->proc_buf_num = (dwc_ep->proc_buf_num ^ 1) & 0x1;
  53909. +}
  53910. +
  53911. +/**
  53912. + * This function is to handle Iso EP transfer complete interrupt
  53913. + * in case Iso out packet was dropped
  53914. + *
  53915. + * @param core_if Programming view of DWC_otg controller.
  53916. + * @param dwc_ep The EP for wihich transfer complete was asserted
  53917. + *
  53918. + */
  53919. +static uint32_t handle_iso_out_pkt_dropped(dwc_otg_core_if_t * core_if,
  53920. + dwc_ep_t * dwc_ep)
  53921. +{
  53922. + uint32_t dma_addr;
  53923. + uint32_t drp_pkt;
  53924. + uint32_t drp_pkt_cnt;
  53925. + deptsiz_data_t deptsiz = {.d32 = 0 };
  53926. + depctl_data_t depctl = {.d32 = 0 };
  53927. + int i;
  53928. +
  53929. + deptsiz.d32 =
  53930. + DWC_READ_REG32(&core_if->dev_if->
  53931. + out_ep_regs[dwc_ep->num]->doeptsiz);
  53932. +
  53933. + drp_pkt = dwc_ep->pkt_cnt - deptsiz.b.pktcnt;
  53934. + drp_pkt_cnt = dwc_ep->pkt_per_frm - (drp_pkt % dwc_ep->pkt_per_frm);
  53935. +
  53936. + /* Setting dropped packets status */
  53937. + for (i = 0; i < drp_pkt_cnt; ++i) {
  53938. + dwc_ep->pkt_info[drp_pkt].status = -DWC_E_NO_DATA;
  53939. + drp_pkt++;
  53940. + deptsiz.b.pktcnt--;
  53941. + }
  53942. +
  53943. + if (deptsiz.b.pktcnt > 0) {
  53944. + deptsiz.b.xfersize =
  53945. + dwc_ep->xfer_len - (dwc_ep->pkt_cnt -
  53946. + deptsiz.b.pktcnt) * dwc_ep->maxpacket;
  53947. + } else {
  53948. + deptsiz.b.xfersize = 0;
  53949. + deptsiz.b.pktcnt = 0;
  53950. + }
  53951. +
  53952. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doeptsiz,
  53953. + deptsiz.d32);
  53954. +
  53955. + if (deptsiz.b.pktcnt > 0) {
  53956. + if (dwc_ep->proc_buf_num) {
  53957. + dma_addr =
  53958. + dwc_ep->dma_addr1 + dwc_ep->xfer_len -
  53959. + deptsiz.b.xfersize;
  53960. + } else {
  53961. + dma_addr =
  53962. + dwc_ep->dma_addr0 + dwc_ep->xfer_len -
  53963. + deptsiz.b.xfersize;;
  53964. + }
  53965. +
  53966. + DWC_WRITE_REG32(&core_if->dev_if->
  53967. + out_ep_regs[dwc_ep->num]->doepdma, dma_addr);
  53968. +
  53969. + /** Re-enable endpoint, clear nak */
  53970. + depctl.d32 = 0;
  53971. + depctl.b.epena = 1;
  53972. + depctl.b.cnak = 1;
  53973. +
  53974. + DWC_MODIFY_REG32(&core_if->dev_if->
  53975. + out_ep_regs[dwc_ep->num]->doepctl, depctl.d32,
  53976. + depctl.d32);
  53977. + return 0;
  53978. + } else {
  53979. + return 1;
  53980. + }
  53981. +}
  53982. +
  53983. +/**
  53984. + * This function sets iso packets information(PTI mode)
  53985. + *
  53986. + * @param core_if Programming view of DWC_otg controller.
  53987. + * @param ep The EP to start the transfer on.
  53988. + *
  53989. + */
  53990. +static uint32_t set_iso_pkts_info(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  53991. +{
  53992. + int i, j;
  53993. + dma_addr_t dma_ad;
  53994. + iso_pkt_info_t *packet_info = ep->pkt_info;
  53995. + uint32_t offset;
  53996. + uint32_t frame_data;
  53997. + deptsiz_data_t deptsiz;
  53998. +
  53999. + if (ep->proc_buf_num == 0) {
  54000. + /** Buffer 0 descriptors setup */
  54001. + dma_ad = ep->dma_addr0;
  54002. + } else {
  54003. + /** Buffer 1 descriptors setup */
  54004. + dma_ad = ep->dma_addr1;
  54005. + }
  54006. +
  54007. + if (ep->is_in) {
  54008. + deptsiz.d32 =
  54009. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  54010. + dieptsiz);
  54011. + } else {
  54012. + deptsiz.d32 =
  54013. + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[ep->num]->
  54014. + doeptsiz);
  54015. + }
  54016. +
  54017. + if (!deptsiz.b.xfersize) {
  54018. + offset = 0;
  54019. + for (i = 0; i < ep->pkt_cnt; i += ep->pkt_per_frm) {
  54020. + frame_data = ep->data_per_frame;
  54021. + for (j = 0; j < ep->pkt_per_frm; ++j) {
  54022. +
  54023. + /* Packet status - is not set as initially
  54024. + * it is set to 0 and if packet was sent
  54025. + successfully, status field will remain 0*/
  54026. +
  54027. + /* Bytes has been transfered */
  54028. + packet_info->length =
  54029. + (ep->maxpacket <
  54030. + frame_data) ? ep->maxpacket : frame_data;
  54031. +
  54032. + /* Received packet offset */
  54033. + packet_info->offset = offset;
  54034. + offset += packet_info->length;
  54035. + frame_data -= packet_info->length;
  54036. +
  54037. + packet_info++;
  54038. + }
  54039. + }
  54040. + return 1;
  54041. + } else {
  54042. + /* This is a workaround for in case of Transfer Complete with
  54043. + * PktDrpSts interrupts merging - in this case Transfer complete
  54044. + * interrupt for Isoc Out Endpoint is asserted without PktDrpSts
  54045. + * set and with DOEPTSIZ register non zero. Investigations showed,
  54046. + * that this happens when Out packet is dropped, but because of
  54047. + * interrupts merging during first interrupt handling PktDrpSts
  54048. + * bit is cleared and for next merged interrupts it is not reset.
  54049. + * In this case SW hadles the interrupt as if PktDrpSts bit is set.
  54050. + */
  54051. + if (ep->is_in) {
  54052. + return 1;
  54053. + } else {
  54054. + return handle_iso_out_pkt_dropped(core_if, ep);
  54055. + }
  54056. + }
  54057. +}
  54058. +
  54059. +/**
  54060. + * This function is to handle Iso EP transfer complete interrupt
  54061. + *
  54062. + * @param pcd The PCD
  54063. + * @param ep The EP for which transfer complete was asserted
  54064. + *
  54065. + */
  54066. +static void complete_iso_ep(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep)
  54067. +{
  54068. + dwc_otg_core_if_t *core_if = GET_CORE_IF(ep->pcd);
  54069. + dwc_ep_t *dwc_ep = &ep->dwc_ep;
  54070. + uint8_t is_last = 0;
  54071. +
  54072. + if (ep->dwc_ep.next_frame == 0xffffffff) {
  54073. + DWC_WARN("Next frame is not set!\n");
  54074. + return;
  54075. + }
  54076. +
  54077. + if (core_if->dma_enable) {
  54078. + if (core_if->dma_desc_enable) {
  54079. + set_ddma_iso_pkts_info(core_if, dwc_ep);
  54080. + reinit_ddma_iso_xfer(core_if, dwc_ep);
  54081. + is_last = 1;
  54082. + } else {
  54083. + if (core_if->pti_enh_enable) {
  54084. + if (set_iso_pkts_info(core_if, dwc_ep)) {
  54085. + dwc_ep->proc_buf_num =
  54086. + (dwc_ep->proc_buf_num ^ 1) & 0x1;
  54087. + dwc_otg_iso_ep_start_buf_transfer
  54088. + (core_if, dwc_ep);
  54089. + is_last = 1;
  54090. + }
  54091. + } else {
  54092. + set_current_pkt_info(core_if, dwc_ep);
  54093. + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
  54094. + is_last = 1;
  54095. + dwc_ep->cur_pkt = 0;
  54096. + dwc_ep->proc_buf_num =
  54097. + (dwc_ep->proc_buf_num ^ 1) & 0x1;
  54098. + if (dwc_ep->proc_buf_num) {
  54099. + dwc_ep->cur_pkt_addr =
  54100. + dwc_ep->xfer_buff1;
  54101. + dwc_ep->cur_pkt_dma_addr =
  54102. + dwc_ep->dma_addr1;
  54103. + } else {
  54104. + dwc_ep->cur_pkt_addr =
  54105. + dwc_ep->xfer_buff0;
  54106. + dwc_ep->cur_pkt_dma_addr =
  54107. + dwc_ep->dma_addr0;
  54108. + }
  54109. +
  54110. + }
  54111. + dwc_otg_iso_ep_start_frm_transfer(core_if,
  54112. + dwc_ep);
  54113. + }
  54114. + }
  54115. + } else {
  54116. + set_current_pkt_info(core_if, dwc_ep);
  54117. + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
  54118. + is_last = 1;
  54119. + dwc_ep->cur_pkt = 0;
  54120. + dwc_ep->proc_buf_num = (dwc_ep->proc_buf_num ^ 1) & 0x1;
  54121. + if (dwc_ep->proc_buf_num) {
  54122. + dwc_ep->cur_pkt_addr = dwc_ep->xfer_buff1;
  54123. + dwc_ep->cur_pkt_dma_addr = dwc_ep->dma_addr1;
  54124. + } else {
  54125. + dwc_ep->cur_pkt_addr = dwc_ep->xfer_buff0;
  54126. + dwc_ep->cur_pkt_dma_addr = dwc_ep->dma_addr0;
  54127. + }
  54128. +
  54129. + }
  54130. + dwc_otg_iso_ep_start_frm_transfer(core_if, dwc_ep);
  54131. + }
  54132. + if (is_last)
  54133. + dwc_otg_iso_buffer_done(pcd, ep, ep->iso_req_handle);
  54134. +}
  54135. +#endif /* DWC_EN_ISOC */
  54136. +
  54137. +/**
  54138. + * This function handle BNA interrupt for Non Isochronous EPs
  54139. + *
  54140. + */
  54141. +static void dwc_otg_pcd_handle_noniso_bna(dwc_otg_pcd_ep_t * ep)
  54142. +{
  54143. + dwc_ep_t *dwc_ep = &ep->dwc_ep;
  54144. + volatile uint32_t *addr;
  54145. + depctl_data_t depctl = {.d32 = 0 };
  54146. + dwc_otg_pcd_t *pcd = ep->pcd;
  54147. + dwc_otg_dev_dma_desc_t *dma_desc;
  54148. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  54149. + dwc_otg_core_if_t *core_if = ep->pcd->core_if;
  54150. + int i, start;
  54151. +
  54152. + if (!dwc_ep->desc_cnt)
  54153. + DWC_WARN("Ep%d %s Descriptor count = %d \n", dwc_ep->num,
  54154. + (dwc_ep->is_in ? "IN" : "OUT"), dwc_ep->desc_cnt);
  54155. +
  54156. + if (core_if->core_params->cont_on_bna && !dwc_ep->is_in
  54157. + && dwc_ep->type != DWC_OTG_EP_TYPE_CONTROL) {
  54158. + uint32_t doepdma;
  54159. + dwc_otg_dev_out_ep_regs_t *out_regs =
  54160. + core_if->dev_if->out_ep_regs[dwc_ep->num];
  54161. + doepdma = DWC_READ_REG32(&(out_regs->doepdma));
  54162. + start = (doepdma - dwc_ep->dma_desc_addr)/sizeof(dwc_otg_dev_dma_desc_t);
  54163. + dma_desc = &(dwc_ep->desc_addr[start]);
  54164. + } else {
  54165. + start = 0;
  54166. + dma_desc = dwc_ep->desc_addr;
  54167. + }
  54168. +
  54169. +
  54170. + for (i = start; i < dwc_ep->desc_cnt; ++i, ++dma_desc) {
  54171. + sts.d32 = dma_desc->status.d32;
  54172. + sts.b.bs = BS_HOST_READY;
  54173. + dma_desc->status.d32 = sts.d32;
  54174. + }
  54175. +
  54176. + if (dwc_ep->is_in == 0) {
  54177. + addr =
  54178. + &GET_CORE_IF(pcd)->dev_if->out_ep_regs[dwc_ep->num]->
  54179. + doepctl;
  54180. + } else {
  54181. + addr =
  54182. + &GET_CORE_IF(pcd)->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
  54183. + }
  54184. + depctl.b.epena = 1;
  54185. + depctl.b.cnak = 1;
  54186. + DWC_MODIFY_REG32(addr, 0, depctl.d32);
  54187. +}
  54188. +
  54189. +/**
  54190. + * This function handles EP0 Control transfers.
  54191. + *
  54192. + * The state of the control transfers are tracked in
  54193. + * <code>ep0state</code>.
  54194. + */
  54195. +static void handle_ep0(dwc_otg_pcd_t * pcd)
  54196. +{
  54197. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  54198. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  54199. + dev_dma_desc_sts_t desc_sts;
  54200. + deptsiz0_data_t deptsiz;
  54201. + uint32_t byte_count;
  54202. +
  54203. +#ifdef DEBUG_EP0
  54204. + DWC_DEBUGPL(DBG_PCDV, "%s()\n", __func__);
  54205. + print_ep0_state(pcd);
  54206. +#endif
  54207. +
  54208. +// DWC_PRINTF("HANDLE EP0\n");
  54209. +
  54210. + switch (pcd->ep0state) {
  54211. + case EP0_DISCONNECT:
  54212. + break;
  54213. +
  54214. + case EP0_IDLE:
  54215. + pcd->request_config = 0;
  54216. +
  54217. + pcd_setup(pcd);
  54218. + break;
  54219. +
  54220. + case EP0_IN_DATA_PHASE:
  54221. +#ifdef DEBUG_EP0
  54222. + DWC_DEBUGPL(DBG_PCD, "DATA_IN EP%d-%s: type=%d, mps=%d\n",
  54223. + ep0->dwc_ep.num, (ep0->dwc_ep.is_in ? "IN" : "OUT"),
  54224. + ep0->dwc_ep.type, ep0->dwc_ep.maxpacket);
  54225. +#endif
  54226. +
  54227. + if (core_if->dma_enable != 0) {
  54228. + /*
  54229. + * For EP0 we can only program 1 packet at a time so we
  54230. + * need to do the make calculations after each complete.
  54231. + * Call write_packet to make the calculations, as in
  54232. + * slave mode, and use those values to determine if we
  54233. + * can complete.
  54234. + */
  54235. + if (core_if->dma_desc_enable == 0) {
  54236. + deptsiz.d32 =
  54237. + DWC_READ_REG32(&core_if->
  54238. + dev_if->in_ep_regs[0]->
  54239. + dieptsiz);
  54240. + byte_count =
  54241. + ep0->dwc_ep.xfer_len - deptsiz.b.xfersize;
  54242. + } else {
  54243. + desc_sts =
  54244. + core_if->dev_if->in_desc_addr->status;
  54245. + byte_count =
  54246. + ep0->dwc_ep.xfer_len - desc_sts.b.bytes;
  54247. + }
  54248. + ep0->dwc_ep.xfer_count += byte_count;
  54249. + ep0->dwc_ep.xfer_buff += byte_count;
  54250. + ep0->dwc_ep.dma_addr += byte_count;
  54251. + }
  54252. + if (ep0->dwc_ep.xfer_count < ep0->dwc_ep.total_len) {
  54253. + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
  54254. + &ep0->dwc_ep);
  54255. + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER\n");
  54256. + } else if (ep0->dwc_ep.sent_zlp) {
  54257. + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
  54258. + &ep0->dwc_ep);
  54259. + ep0->dwc_ep.sent_zlp = 0;
  54260. + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER sent zlp\n");
  54261. + } else {
  54262. + ep0_complete_request(ep0);
  54263. + DWC_DEBUGPL(DBG_PCD, "COMPLETE TRANSFER\n");
  54264. + }
  54265. + break;
  54266. + case EP0_OUT_DATA_PHASE:
  54267. +#ifdef DEBUG_EP0
  54268. + DWC_DEBUGPL(DBG_PCD, "DATA_OUT EP%d-%s: type=%d, mps=%d\n",
  54269. + ep0->dwc_ep.num, (ep0->dwc_ep.is_in ? "IN" : "OUT"),
  54270. + ep0->dwc_ep.type, ep0->dwc_ep.maxpacket);
  54271. +#endif
  54272. + if (core_if->dma_enable != 0) {
  54273. + if (core_if->dma_desc_enable == 0) {
  54274. + deptsiz.d32 =
  54275. + DWC_READ_REG32(&core_if->
  54276. + dev_if->out_ep_regs[0]->
  54277. + doeptsiz);
  54278. + byte_count =
  54279. + ep0->dwc_ep.maxpacket - deptsiz.b.xfersize;
  54280. + } else {
  54281. + desc_sts =
  54282. + core_if->dev_if->out_desc_addr->status;
  54283. + byte_count =
  54284. + ep0->dwc_ep.maxpacket - desc_sts.b.bytes;
  54285. + }
  54286. + ep0->dwc_ep.xfer_count += byte_count;
  54287. + ep0->dwc_ep.xfer_buff += byte_count;
  54288. + ep0->dwc_ep.dma_addr += byte_count;
  54289. + }
  54290. + if (ep0->dwc_ep.xfer_count < ep0->dwc_ep.total_len) {
  54291. + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
  54292. + &ep0->dwc_ep);
  54293. + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER\n");
  54294. + } else if (ep0->dwc_ep.sent_zlp) {
  54295. + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
  54296. + &ep0->dwc_ep);
  54297. + ep0->dwc_ep.sent_zlp = 0;
  54298. + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER sent zlp\n");
  54299. + } else {
  54300. + ep0_complete_request(ep0);
  54301. + DWC_DEBUGPL(DBG_PCD, "COMPLETE TRANSFER\n");
  54302. + }
  54303. + break;
  54304. +
  54305. + case EP0_IN_STATUS_PHASE:
  54306. + case EP0_OUT_STATUS_PHASE:
  54307. + DWC_DEBUGPL(DBG_PCD, "CASE: EP0_STATUS\n");
  54308. + ep0_complete_request(ep0);
  54309. + pcd->ep0state = EP0_IDLE;
  54310. + ep0->stopped = 1;
  54311. + ep0->dwc_ep.is_in = 0; /* OUT for next SETUP */
  54312. +
  54313. + /* Prepare for more SETUP Packets */
  54314. + if (core_if->dma_enable) {
  54315. + ep0_out_start(core_if, pcd);
  54316. + }
  54317. + break;
  54318. +
  54319. + case EP0_STALL:
  54320. + DWC_ERROR("EP0 STALLed, should not get here pcd_setup()\n");
  54321. + break;
  54322. + }
  54323. +#ifdef DEBUG_EP0
  54324. + print_ep0_state(pcd);
  54325. +#endif
  54326. +}
  54327. +
  54328. +/**
  54329. + * Restart transfer
  54330. + */
  54331. +static void restart_transfer(dwc_otg_pcd_t * pcd, const uint32_t epnum)
  54332. +{
  54333. + dwc_otg_core_if_t *core_if;
  54334. + dwc_otg_dev_if_t *dev_if;
  54335. + deptsiz_data_t dieptsiz = {.d32 = 0 };
  54336. + dwc_otg_pcd_ep_t *ep;
  54337. +
  54338. + ep = get_in_ep(pcd, epnum);
  54339. +
  54340. +#ifdef DWC_EN_ISOC
  54341. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  54342. + return;
  54343. + }
  54344. +#endif /* DWC_EN_ISOC */
  54345. +
  54346. + core_if = GET_CORE_IF(pcd);
  54347. + dev_if = core_if->dev_if;
  54348. +
  54349. + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dieptsiz);
  54350. +
  54351. + DWC_DEBUGPL(DBG_PCD, "xfer_buff=%p xfer_count=%0x xfer_len=%0x"
  54352. + " stopped=%d\n", ep->dwc_ep.xfer_buff,
  54353. + ep->dwc_ep.xfer_count, ep->dwc_ep.xfer_len, ep->stopped);
  54354. + /*
  54355. + * If xfersize is 0 and pktcnt in not 0, resend the last packet.
  54356. + */
  54357. + if (dieptsiz.b.pktcnt && dieptsiz.b.xfersize == 0 &&
  54358. + ep->dwc_ep.start_xfer_buff != 0) {
  54359. + if (ep->dwc_ep.total_len <= ep->dwc_ep.maxpacket) {
  54360. + ep->dwc_ep.xfer_count = 0;
  54361. + ep->dwc_ep.xfer_buff = ep->dwc_ep.start_xfer_buff;
  54362. + ep->dwc_ep.xfer_len = ep->dwc_ep.xfer_count;
  54363. + } else {
  54364. + ep->dwc_ep.xfer_count -= ep->dwc_ep.maxpacket;
  54365. + /* convert packet size to dwords. */
  54366. + ep->dwc_ep.xfer_buff -= ep->dwc_ep.maxpacket;
  54367. + ep->dwc_ep.xfer_len = ep->dwc_ep.xfer_count;
  54368. + }
  54369. + ep->stopped = 0;
  54370. + DWC_DEBUGPL(DBG_PCD, "xfer_buff=%p xfer_count=%0x "
  54371. + "xfer_len=%0x stopped=%d\n",
  54372. + ep->dwc_ep.xfer_buff,
  54373. + ep->dwc_ep.xfer_count, ep->dwc_ep.xfer_len,
  54374. + ep->stopped);
  54375. + if (epnum == 0) {
  54376. + dwc_otg_ep0_start_transfer(core_if, &ep->dwc_ep);
  54377. + } else {
  54378. + dwc_otg_ep_start_transfer(core_if, &ep->dwc_ep);
  54379. + }
  54380. + }
  54381. +}
  54382. +
  54383. +/*
  54384. + * This function create new nextep sequnce based on Learn Queue.
  54385. + *
  54386. + * @param core_if Programming view of DWC_otg controller
  54387. + */
  54388. +void predict_nextep_seq( dwc_otg_core_if_t * core_if)
  54389. +{
  54390. + dwc_otg_device_global_regs_t *dev_global_regs =
  54391. + core_if->dev_if->dev_global_regs;
  54392. + const uint32_t TOKEN_Q_DEPTH = core_if->hwcfg2.b.dev_token_q_depth;
  54393. + /* Number of Token Queue Registers */
  54394. + const int DTKNQ_REG_CNT = (TOKEN_Q_DEPTH + 7) / 8;
  54395. + dtknq1_data_t dtknqr1;
  54396. + uint32_t in_tkn_epnums[4];
  54397. + uint8_t seqnum[MAX_EPS_CHANNELS];
  54398. + uint8_t intkn_seq[1 << 5];
  54399. + grstctl_t resetctl = {.d32 = 0 };
  54400. + uint8_t temp;
  54401. + int ndx = 0;
  54402. + int start = 0;
  54403. + int end = 0;
  54404. + int sort_done = 0;
  54405. + int i = 0;
  54406. + volatile uint32_t *addr = &dev_global_regs->dtknqr1;
  54407. +
  54408. +
  54409. + DWC_DEBUGPL(DBG_PCD,"dev_token_q_depth=%d\n",TOKEN_Q_DEPTH);
  54410. +
  54411. + /* Read the DTKNQ Registers */
  54412. + for (i = 0; i < DTKNQ_REG_CNT; i++) {
  54413. + in_tkn_epnums[i] = DWC_READ_REG32(addr);
  54414. + DWC_DEBUGPL(DBG_PCDV, "DTKNQR%d=0x%08x\n", i + 1,
  54415. + in_tkn_epnums[i]);
  54416. + if (addr == &dev_global_regs->dvbusdis) {
  54417. + addr = &dev_global_regs->dtknqr3_dthrctl;
  54418. + } else {
  54419. + ++addr;
  54420. + }
  54421. +
  54422. + }
  54423. +
  54424. + /* Copy the DTKNQR1 data to the bit field. */
  54425. + dtknqr1.d32 = in_tkn_epnums[0];
  54426. + if (dtknqr1.b.wrap_bit) {
  54427. + ndx = dtknqr1.b.intknwptr;
  54428. + end = ndx -1;
  54429. + if (end < 0)
  54430. + end = TOKEN_Q_DEPTH -1;
  54431. + } else {
  54432. + ndx = 0;
  54433. + end = dtknqr1.b.intknwptr -1;
  54434. + if (end < 0)
  54435. + end = 0;
  54436. + }
  54437. + start = ndx;
  54438. +
  54439. + /* Fill seqnum[] by initial values: EP number + 31 */
  54440. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  54441. + seqnum[i] = i +31;
  54442. + }
  54443. +
  54444. + /* Fill intkn_seq[] from in_tkn_epnums[0] */
  54445. + for (i=0; i < 6; i++)
  54446. + intkn_seq[i] = (in_tkn_epnums[0] >> ((7-i) * 4)) & 0xf;
  54447. +
  54448. + if (TOKEN_Q_DEPTH > 6) {
  54449. + /* Fill intkn_seq[] from in_tkn_epnums[1] */
  54450. + for (i=6; i < 14; i++)
  54451. + intkn_seq[i] =
  54452. + (in_tkn_epnums[1] >> ((7 - (i - 6)) * 4)) & 0xf;
  54453. + }
  54454. +
  54455. + if (TOKEN_Q_DEPTH > 14) {
  54456. + /* Fill intkn_seq[] from in_tkn_epnums[1] */
  54457. + for (i=14; i < 22; i++)
  54458. + intkn_seq[i] =
  54459. + (in_tkn_epnums[2] >> ((7 - (i - 14)) * 4)) & 0xf;
  54460. + }
  54461. +
  54462. + if (TOKEN_Q_DEPTH > 22) {
  54463. + /* Fill intkn_seq[] from in_tkn_epnums[1] */
  54464. + for (i=22; i < 30; i++)
  54465. + intkn_seq[i] =
  54466. + (in_tkn_epnums[3] >> ((7 - (i - 22)) * 4)) & 0xf;
  54467. + }
  54468. +
  54469. + DWC_DEBUGPL(DBG_PCDV, "%s start=%d end=%d intkn_seq[]:\n", __func__,
  54470. + start, end);
  54471. + for (i=0; i<TOKEN_Q_DEPTH; i++)
  54472. + DWC_DEBUGPL(DBG_PCDV,"%d\n", intkn_seq[i]);
  54473. +
  54474. + /* Update seqnum based on intkn_seq[] */
  54475. + i = 0;
  54476. + do {
  54477. + seqnum[intkn_seq[ndx]] = i;
  54478. + ndx++;
  54479. + i++;
  54480. + if (ndx == TOKEN_Q_DEPTH)
  54481. + ndx = 0;
  54482. + } while ( i < TOKEN_Q_DEPTH );
  54483. +
  54484. + /* Mark non active EP's in seqnum[] by 0xff */
  54485. + for (i=0; i<=core_if->dev_if->num_in_eps; i++) {
  54486. + if (core_if->nextep_seq[i] == 0xff )
  54487. + seqnum[i] = 0xff;
  54488. + }
  54489. +
  54490. + /* Sort seqnum[] */
  54491. + sort_done = 0;
  54492. + while (!sort_done) {
  54493. + sort_done = 1;
  54494. + for (i=0; i<core_if->dev_if->num_in_eps; i++) {
  54495. + if (seqnum[i] > seqnum[i+1]) {
  54496. + temp = seqnum[i];
  54497. + seqnum[i] = seqnum[i+1];
  54498. + seqnum[i+1] = temp;
  54499. + sort_done = 0;
  54500. + }
  54501. + }
  54502. + }
  54503. +
  54504. + ndx = start + seqnum[0];
  54505. + if (ndx >= TOKEN_Q_DEPTH)
  54506. + ndx = ndx % TOKEN_Q_DEPTH;
  54507. + core_if->first_in_nextep_seq = intkn_seq[ndx];
  54508. +
  54509. + /* Update seqnum[] by EP numbers */
  54510. + for (i=0; i<=core_if->dev_if->num_in_eps; i++) {
  54511. + ndx = start + i;
  54512. + if (seqnum[i] < 31) {
  54513. + ndx = start + seqnum[i];
  54514. + if (ndx >= TOKEN_Q_DEPTH)
  54515. + ndx = ndx % TOKEN_Q_DEPTH;
  54516. + seqnum[i] = intkn_seq[ndx];
  54517. + } else {
  54518. + if (seqnum[i] < 0xff) {
  54519. + seqnum[i] = seqnum[i] - 31;
  54520. + } else {
  54521. + break;
  54522. + }
  54523. + }
  54524. + }
  54525. +
  54526. + /* Update nextep_seq[] based on seqnum[] */
  54527. + for (i=0; i<core_if->dev_if->num_in_eps; i++) {
  54528. + if (seqnum[i] != 0xff) {
  54529. + if (seqnum[i+1] != 0xff) {
  54530. + core_if->nextep_seq[seqnum[i]] = seqnum[i+1];
  54531. + } else {
  54532. + core_if->nextep_seq[seqnum[i]] = core_if->first_in_nextep_seq;
  54533. + break;
  54534. + }
  54535. + } else {
  54536. + break;
  54537. + }
  54538. + }
  54539. +
  54540. + DWC_DEBUGPL(DBG_PCDV, "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
  54541. + __func__, core_if->first_in_nextep_seq);
  54542. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  54543. + DWC_DEBUGPL(DBG_PCDV,"%2d\n", core_if->nextep_seq[i]);
  54544. + }
  54545. +
  54546. + /* Flush the Learning Queue */
  54547. + resetctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->grstctl);
  54548. + resetctl.b.intknqflsh = 1;
  54549. + DWC_WRITE_REG32(&core_if->core_global_regs->grstctl, resetctl.d32);
  54550. +
  54551. +
  54552. +}
  54553. +
  54554. +/**
  54555. + * handle the IN EP disable interrupt.
  54556. + */
  54557. +static inline void handle_in_ep_disable_intr(dwc_otg_pcd_t * pcd,
  54558. + const uint32_t epnum)
  54559. +{
  54560. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  54561. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  54562. + deptsiz_data_t dieptsiz = {.d32 = 0 };
  54563. + dctl_data_t dctl = {.d32 = 0 };
  54564. + dwc_otg_pcd_ep_t *ep;
  54565. + dwc_ep_t *dwc_ep;
  54566. + gintmsk_data_t gintmsk_data;
  54567. + depctl_data_t depctl;
  54568. + uint32_t diepdma;
  54569. + uint32_t remain_to_transfer = 0;
  54570. + uint8_t i;
  54571. + uint32_t xfer_size;
  54572. +
  54573. + ep = get_in_ep(pcd, epnum);
  54574. + dwc_ep = &ep->dwc_ep;
  54575. +
  54576. + if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  54577. + dwc_otg_flush_tx_fifo(core_if, dwc_ep->tx_fifo_num);
  54578. + complete_ep(ep);
  54579. + return;
  54580. + }
  54581. +
  54582. + DWC_DEBUGPL(DBG_PCD, "diepctl%d=%0x\n", epnum,
  54583. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl));
  54584. + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dieptsiz);
  54585. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl);
  54586. +
  54587. + DWC_DEBUGPL(DBG_ANY, "pktcnt=%d size=%d\n",
  54588. + dieptsiz.b.pktcnt, dieptsiz.b.xfersize);
  54589. +
  54590. + if ((core_if->start_predict == 0) || (depctl.b.eptype & 1)) {
  54591. + if (ep->stopped) {
  54592. + if (core_if->en_multiple_tx_fifo)
  54593. + /* Flush the Tx FIFO */
  54594. + dwc_otg_flush_tx_fifo(core_if, dwc_ep->tx_fifo_num);
  54595. + /* Clear the Global IN NP NAK */
  54596. + dctl.d32 = 0;
  54597. + dctl.b.cgnpinnak = 1;
  54598. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
  54599. + /* Restart the transaction */
  54600. + if (dieptsiz.b.pktcnt != 0 || dieptsiz.b.xfersize != 0) {
  54601. + restart_transfer(pcd, epnum);
  54602. + }
  54603. + } else {
  54604. + /* Restart the transaction */
  54605. + if (dieptsiz.b.pktcnt != 0 || dieptsiz.b.xfersize != 0) {
  54606. + restart_transfer(pcd, epnum);
  54607. + }
  54608. + DWC_DEBUGPL(DBG_ANY, "STOPPED!!!\n");
  54609. + }
  54610. + return;
  54611. + }
  54612. +
  54613. + if (core_if->start_predict > 2) { // NP IN EP
  54614. + core_if->start_predict--;
  54615. + return;
  54616. + }
  54617. +
  54618. + core_if->start_predict--;
  54619. +
  54620. + if (core_if->start_predict == 1) { // All NP IN Ep's disabled now
  54621. +
  54622. + predict_nextep_seq(core_if);
  54623. +
  54624. + /* Update all active IN EP's NextEP field based of nextep_seq[] */
  54625. + for ( i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  54626. + depctl.d32 =
  54627. + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  54628. + if (core_if->nextep_seq[i] != 0xff) { // Active NP IN EP
  54629. + depctl.b.nextep = core_if->nextep_seq[i];
  54630. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32);
  54631. + }
  54632. + }
  54633. + /* Flush Shared NP TxFIFO */
  54634. + dwc_otg_flush_tx_fifo(core_if, 0);
  54635. + /* Rewind buffers */
  54636. + if (!core_if->dma_desc_enable) {
  54637. + i = core_if->first_in_nextep_seq;
  54638. + do {
  54639. + ep = get_in_ep(pcd, i);
  54640. + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->dieptsiz);
  54641. + xfer_size = ep->dwc_ep.total_len - ep->dwc_ep.xfer_count;
  54642. + if (xfer_size > ep->dwc_ep.maxxfer)
  54643. + xfer_size = ep->dwc_ep.maxxfer;
  54644. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  54645. + if (dieptsiz.b.pktcnt != 0) {
  54646. + if (xfer_size == 0) {
  54647. + remain_to_transfer = 0;
  54648. + } else {
  54649. + if ((xfer_size % ep->dwc_ep.maxpacket) == 0) {
  54650. + remain_to_transfer =
  54651. + dieptsiz.b.pktcnt * ep->dwc_ep.maxpacket;
  54652. + } else {
  54653. + remain_to_transfer = ((dieptsiz.b.pktcnt -1) * ep->dwc_ep.maxpacket)
  54654. + + (xfer_size % ep->dwc_ep.maxpacket);
  54655. + }
  54656. + }
  54657. + diepdma = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepdma);
  54658. + dieptsiz.b.xfersize = remain_to_transfer;
  54659. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->dieptsiz, dieptsiz.d32);
  54660. + diepdma = ep->dwc_ep.dma_addr + (xfer_size - remain_to_transfer);
  54661. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepdma, diepdma);
  54662. + }
  54663. + i = core_if->nextep_seq[i];
  54664. + } while (i != core_if->first_in_nextep_seq);
  54665. + } else { // dma_desc_enable
  54666. + DWC_PRINTF("%s Learning Queue not supported in DDMA\n", __func__);
  54667. + }
  54668. +
  54669. + /* Restart transfers in predicted sequences */
  54670. + i = core_if->first_in_nextep_seq;
  54671. + do {
  54672. + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->dieptsiz);
  54673. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  54674. + if (dieptsiz.b.pktcnt != 0) {
  54675. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  54676. + depctl.b.epena = 1;
  54677. + depctl.b.cnak = 1;
  54678. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32);
  54679. + }
  54680. + i = core_if->nextep_seq[i];
  54681. + } while (i != core_if->first_in_nextep_seq);
  54682. +
  54683. + /* Clear the global non-periodic IN NAK handshake */
  54684. + dctl.d32 = 0;
  54685. + dctl.b.cgnpinnak = 1;
  54686. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
  54687. +
  54688. + /* Unmask EP Mismatch interrupt */
  54689. + gintmsk_data.d32 = 0;
  54690. + gintmsk_data.b.epmismatch = 1;
  54691. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, gintmsk_data.d32);
  54692. +
  54693. + core_if->start_predict = 0;
  54694. +
  54695. + }
  54696. +}
  54697. +
  54698. +/**
  54699. + * Handler for the IN EP timeout handshake interrupt.
  54700. + */
  54701. +static inline void handle_in_ep_timeout_intr(dwc_otg_pcd_t * pcd,
  54702. + const uint32_t epnum)
  54703. +{
  54704. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  54705. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  54706. +
  54707. +#ifdef DEBUG
  54708. + deptsiz_data_t dieptsiz = {.d32 = 0 };
  54709. + uint32_t num = 0;
  54710. +#endif
  54711. + dctl_data_t dctl = {.d32 = 0 };
  54712. + dwc_otg_pcd_ep_t *ep;
  54713. +
  54714. + gintmsk_data_t intr_mask = {.d32 = 0 };
  54715. +
  54716. + ep = get_in_ep(pcd, epnum);
  54717. +
  54718. + /* Disable the NP Tx Fifo Empty Interrrupt */
  54719. + if (!core_if->dma_enable) {
  54720. + intr_mask.b.nptxfempty = 1;
  54721. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
  54722. + intr_mask.d32, 0);
  54723. + }
  54724. + /** @todo NGS Check EP type.
  54725. + * Implement for Periodic EPs */
  54726. + /*
  54727. + * Non-periodic EP
  54728. + */
  54729. + /* Enable the Global IN NAK Effective Interrupt */
  54730. + intr_mask.b.ginnakeff = 1;
  54731. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, intr_mask.d32);
  54732. +
  54733. + /* Set Global IN NAK */
  54734. + dctl.b.sgnpinnak = 1;
  54735. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
  54736. +
  54737. + ep->stopped = 1;
  54738. +
  54739. +#ifdef DEBUG
  54740. + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[num]->dieptsiz);
  54741. + DWC_DEBUGPL(DBG_ANY, "pktcnt=%d size=%d\n",
  54742. + dieptsiz.b.pktcnt, dieptsiz.b.xfersize);
  54743. +#endif
  54744. +
  54745. +#ifdef DISABLE_PERIODIC_EP
  54746. + /*
  54747. + * Set the NAK bit for this EP to
  54748. + * start the disable process.
  54749. + */
  54750. + diepctl.d32 = 0;
  54751. + diepctl.b.snak = 1;
  54752. + DWC_MODIFY_REG32(&dev_if->in_ep_regs[num]->diepctl, diepctl.d32,
  54753. + diepctl.d32);
  54754. + ep->disabling = 1;
  54755. + ep->stopped = 1;
  54756. +#endif
  54757. +}
  54758. +
  54759. +/**
  54760. + * Handler for the IN EP NAK interrupt.
  54761. + */
  54762. +static inline int32_t handle_in_ep_nak_intr(dwc_otg_pcd_t * pcd,
  54763. + const uint32_t epnum)
  54764. +{
  54765. + /** @todo implement ISR */
  54766. + dwc_otg_core_if_t *core_if;
  54767. + diepmsk_data_t intr_mask = {.d32 = 0 };
  54768. +
  54769. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "IN EP NAK");
  54770. + core_if = GET_CORE_IF(pcd);
  54771. + intr_mask.b.nak = 1;
  54772. +
  54773. + if (core_if->multiproc_int_enable) {
  54774. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  54775. + diepeachintmsk[epnum], intr_mask.d32, 0);
  54776. + } else {
  54777. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->diepmsk,
  54778. + intr_mask.d32, 0);
  54779. + }
  54780. +
  54781. + return 1;
  54782. +}
  54783. +
  54784. +/**
  54785. + * Handler for the OUT EP Babble interrupt.
  54786. + */
  54787. +static inline int32_t handle_out_ep_babble_intr(dwc_otg_pcd_t * pcd,
  54788. + const uint32_t epnum)
  54789. +{
  54790. + /** @todo implement ISR */
  54791. + dwc_otg_core_if_t *core_if;
  54792. + doepmsk_data_t intr_mask = {.d32 = 0 };
  54793. +
  54794. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n",
  54795. + "OUT EP Babble");
  54796. + core_if = GET_CORE_IF(pcd);
  54797. + intr_mask.b.babble = 1;
  54798. +
  54799. + if (core_if->multiproc_int_enable) {
  54800. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  54801. + doepeachintmsk[epnum], intr_mask.d32, 0);
  54802. + } else {
  54803. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
  54804. + intr_mask.d32, 0);
  54805. + }
  54806. +
  54807. + return 1;
  54808. +}
  54809. +
  54810. +/**
  54811. + * Handler for the OUT EP NAK interrupt.
  54812. + */
  54813. +static inline int32_t handle_out_ep_nak_intr(dwc_otg_pcd_t * pcd,
  54814. + const uint32_t epnum)
  54815. +{
  54816. + /** @todo implement ISR */
  54817. + dwc_otg_core_if_t *core_if;
  54818. + doepmsk_data_t intr_mask = {.d32 = 0 };
  54819. +
  54820. + DWC_DEBUGPL(DBG_ANY, "INTERRUPT Handler not implemented for %s\n", "OUT EP NAK");
  54821. + core_if = GET_CORE_IF(pcd);
  54822. + intr_mask.b.nak = 1;
  54823. +
  54824. + if (core_if->multiproc_int_enable) {
  54825. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  54826. + doepeachintmsk[epnum], intr_mask.d32, 0);
  54827. + } else {
  54828. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
  54829. + intr_mask.d32, 0);
  54830. + }
  54831. +
  54832. + return 1;
  54833. +}
  54834. +
  54835. +/**
  54836. + * Handler for the OUT EP NYET interrupt.
  54837. + */
  54838. +static inline int32_t handle_out_ep_nyet_intr(dwc_otg_pcd_t * pcd,
  54839. + const uint32_t epnum)
  54840. +{
  54841. + /** @todo implement ISR */
  54842. + dwc_otg_core_if_t *core_if;
  54843. + doepmsk_data_t intr_mask = {.d32 = 0 };
  54844. +
  54845. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "OUT EP NYET");
  54846. + core_if = GET_CORE_IF(pcd);
  54847. + intr_mask.b.nyet = 1;
  54848. +
  54849. + if (core_if->multiproc_int_enable) {
  54850. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  54851. + doepeachintmsk[epnum], intr_mask.d32, 0);
  54852. + } else {
  54853. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
  54854. + intr_mask.d32, 0);
  54855. + }
  54856. +
  54857. + return 1;
  54858. +}
  54859. +
  54860. +/**
  54861. + * This interrupt indicates that an IN EP has a pending Interrupt.
  54862. + * The sequence for handling the IN EP interrupt is shown below:
  54863. + * -# Read the Device All Endpoint Interrupt register
  54864. + * -# Repeat the following for each IN EP interrupt bit set (from
  54865. + * LSB to MSB).
  54866. + * -# Read the Device Endpoint Interrupt (DIEPINTn) register
  54867. + * -# If "Transfer Complete" call the request complete function
  54868. + * -# If "Endpoint Disabled" complete the EP disable procedure.
  54869. + * -# If "AHB Error Interrupt" log error
  54870. + * -# If "Time-out Handshake" log error
  54871. + * -# If "IN Token Received when TxFIFO Empty" write packet to Tx
  54872. + * FIFO.
  54873. + * -# If "IN Token EP Mismatch" (disable, this is handled by EP
  54874. + * Mismatch Interrupt)
  54875. + */
  54876. +static int32_t dwc_otg_pcd_handle_in_ep_intr(dwc_otg_pcd_t * pcd)
  54877. +{
  54878. +#define CLEAR_IN_EP_INTR(__core_if,__epnum,__intr) \
  54879. +do { \
  54880. + diepint_data_t diepint = {.d32=0}; \
  54881. + diepint.b.__intr = 1; \
  54882. + DWC_WRITE_REG32(&__core_if->dev_if->in_ep_regs[__epnum]->diepint, \
  54883. + diepint.d32); \
  54884. +} while (0)
  54885. +
  54886. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  54887. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  54888. + diepint_data_t diepint = {.d32 = 0 };
  54889. + depctl_data_t depctl = {.d32 = 0 };
  54890. + uint32_t ep_intr;
  54891. + uint32_t epnum = 0;
  54892. + dwc_otg_pcd_ep_t *ep;
  54893. + dwc_ep_t *dwc_ep;
  54894. + gintmsk_data_t intr_mask = {.d32 = 0 };
  54895. +
  54896. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, pcd);
  54897. +
  54898. + /* Read in the device interrupt bits */
  54899. + ep_intr = dwc_otg_read_dev_all_in_ep_intr(core_if);
  54900. +
  54901. + /* Service the Device IN interrupts for each endpoint */
  54902. + while (ep_intr) {
  54903. + if (ep_intr & 0x1) {
  54904. + uint32_t empty_msk;
  54905. + /* Get EP pointer */
  54906. + ep = get_in_ep(pcd, epnum);
  54907. + dwc_ep = &ep->dwc_ep;
  54908. +
  54909. + depctl.d32 =
  54910. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl);
  54911. + empty_msk =
  54912. + DWC_READ_REG32(&dev_if->
  54913. + dev_global_regs->dtknqr4_fifoemptymsk);
  54914. +
  54915. + DWC_DEBUGPL(DBG_PCDV,
  54916. + "IN EP INTERRUPT - %d\nepmty_msk - %8x diepctl - %8x\n",
  54917. + epnum, empty_msk, depctl.d32);
  54918. +
  54919. + DWC_DEBUGPL(DBG_PCD,
  54920. + "EP%d-%s: type=%d, mps=%d\n",
  54921. + dwc_ep->num, (dwc_ep->is_in ? "IN" : "OUT"),
  54922. + dwc_ep->type, dwc_ep->maxpacket);
  54923. +
  54924. + diepint.d32 =
  54925. + dwc_otg_read_dev_in_ep_intr(core_if, dwc_ep);
  54926. +
  54927. + DWC_DEBUGPL(DBG_PCDV,
  54928. + "EP %d Interrupt Register - 0x%x\n", epnum,
  54929. + diepint.d32);
  54930. + /* Transfer complete */
  54931. + if (diepint.b.xfercompl) {
  54932. + /* Disable the NP Tx FIFO Empty
  54933. + * Interrupt */
  54934. + if (core_if->en_multiple_tx_fifo == 0) {
  54935. + intr_mask.b.nptxfempty = 1;
  54936. + DWC_MODIFY_REG32
  54937. + (&core_if->core_global_regs->gintmsk,
  54938. + intr_mask.d32, 0);
  54939. + } else {
  54940. + /* Disable the Tx FIFO Empty Interrupt for this EP */
  54941. + uint32_t fifoemptymsk =
  54942. + 0x1 << dwc_ep->num;
  54943. + DWC_MODIFY_REG32(&core_if->
  54944. + dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
  54945. + fifoemptymsk, 0);
  54946. + }
  54947. + /* Clear the bit in DIEPINTn for this interrupt */
  54948. + CLEAR_IN_EP_INTR(core_if, epnum, xfercompl);
  54949. +
  54950. + /* Complete the transfer */
  54951. + if (epnum == 0) {
  54952. + handle_ep0(pcd);
  54953. + }
  54954. +#ifdef DWC_EN_ISOC
  54955. + else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  54956. + if (!ep->stopped)
  54957. + complete_iso_ep(pcd, ep);
  54958. + }
  54959. +#endif /* DWC_EN_ISOC */
  54960. +#ifdef DWC_UTE_PER_IO
  54961. + else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  54962. + if (!ep->stopped)
  54963. + complete_xiso_ep(ep);
  54964. + }
  54965. +#endif /* DWC_UTE_PER_IO */
  54966. + else {
  54967. + if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC &&
  54968. + dwc_ep->bInterval > 1) {
  54969. + dwc_ep->frame_num += dwc_ep->bInterval;
  54970. + if (dwc_ep->frame_num > 0x3FFF)
  54971. + {
  54972. + dwc_ep->frm_overrun = 1;
  54973. + dwc_ep->frame_num &= 0x3FFF;
  54974. + } else
  54975. + dwc_ep->frm_overrun = 0;
  54976. + }
  54977. + complete_ep(ep);
  54978. + if(diepint.b.nak)
  54979. + CLEAR_IN_EP_INTR(core_if, epnum, nak);
  54980. + }
  54981. + }
  54982. + /* Endpoint disable */
  54983. + if (diepint.b.epdisabled) {
  54984. + DWC_DEBUGPL(DBG_ANY, "EP%d IN disabled\n",
  54985. + epnum);
  54986. + handle_in_ep_disable_intr(pcd, epnum);
  54987. +
  54988. + /* Clear the bit in DIEPINTn for this interrupt */
  54989. + CLEAR_IN_EP_INTR(core_if, epnum, epdisabled);
  54990. + }
  54991. + /* AHB Error */
  54992. + if (diepint.b.ahberr) {
  54993. + DWC_ERROR("EP%d IN AHB Error\n", epnum);
  54994. + /* Clear the bit in DIEPINTn for this interrupt */
  54995. + CLEAR_IN_EP_INTR(core_if, epnum, ahberr);
  54996. + }
  54997. + /* TimeOUT Handshake (non-ISOC IN EPs) */
  54998. + if (diepint.b.timeout) {
  54999. + DWC_ERROR("EP%d IN Time-out\n", epnum);
  55000. + handle_in_ep_timeout_intr(pcd, epnum);
  55001. +
  55002. + CLEAR_IN_EP_INTR(core_if, epnum, timeout);
  55003. + }
  55004. + /** IN Token received with TxF Empty */
  55005. + if (diepint.b.intktxfemp) {
  55006. + DWC_DEBUGPL(DBG_ANY,
  55007. + "EP%d IN TKN TxFifo Empty\n",
  55008. + epnum);
  55009. + if (!ep->stopped && epnum != 0) {
  55010. +
  55011. + diepmsk_data_t diepmsk = {.d32 = 0 };
  55012. + diepmsk.b.intktxfemp = 1;
  55013. +
  55014. + if (core_if->multiproc_int_enable) {
  55015. + DWC_MODIFY_REG32
  55016. + (&dev_if->dev_global_regs->diepeachintmsk
  55017. + [epnum], diepmsk.d32, 0);
  55018. + } else {
  55019. + DWC_MODIFY_REG32
  55020. + (&dev_if->dev_global_regs->diepmsk,
  55021. + diepmsk.d32, 0);
  55022. + }
  55023. + } else if (core_if->dma_desc_enable
  55024. + && epnum == 0
  55025. + && pcd->ep0state ==
  55026. + EP0_OUT_STATUS_PHASE) {
  55027. + // EP0 IN set STALL
  55028. + depctl.d32 =
  55029. + DWC_READ_REG32(&dev_if->in_ep_regs
  55030. + [epnum]->diepctl);
  55031. +
  55032. + /* set the disable and stall bits */
  55033. + if (depctl.b.epena) {
  55034. + depctl.b.epdis = 1;
  55035. + }
  55036. + depctl.b.stall = 1;
  55037. + DWC_WRITE_REG32(&dev_if->in_ep_regs
  55038. + [epnum]->diepctl,
  55039. + depctl.d32);
  55040. + }
  55041. + CLEAR_IN_EP_INTR(core_if, epnum, intktxfemp);
  55042. + }
  55043. + /** IN Token Received with EP mismatch */
  55044. + if (diepint.b.intknepmis) {
  55045. + DWC_DEBUGPL(DBG_ANY,
  55046. + "EP%d IN TKN EP Mismatch\n", epnum);
  55047. + CLEAR_IN_EP_INTR(core_if, epnum, intknepmis);
  55048. + }
  55049. + /** IN Endpoint NAK Effective */
  55050. + if (diepint.b.inepnakeff) {
  55051. + DWC_DEBUGPL(DBG_ANY,
  55052. + "EP%d IN EP NAK Effective\n",
  55053. + epnum);
  55054. + /* Periodic EP */
  55055. + if (ep->disabling) {
  55056. + depctl.d32 = 0;
  55057. + depctl.b.snak = 1;
  55058. + depctl.b.epdis = 1;
  55059. + DWC_MODIFY_REG32(&dev_if->in_ep_regs
  55060. + [epnum]->diepctl,
  55061. + depctl.d32,
  55062. + depctl.d32);
  55063. + }
  55064. + CLEAR_IN_EP_INTR(core_if, epnum, inepnakeff);
  55065. +
  55066. + }
  55067. +
  55068. + /** IN EP Tx FIFO Empty Intr */
  55069. + if (diepint.b.emptyintr) {
  55070. + DWC_DEBUGPL(DBG_ANY,
  55071. + "EP%d Tx FIFO Empty Intr \n",
  55072. + epnum);
  55073. + write_empty_tx_fifo(pcd, epnum);
  55074. +
  55075. + CLEAR_IN_EP_INTR(core_if, epnum, emptyintr);
  55076. +
  55077. + }
  55078. +
  55079. + /** IN EP BNA Intr */
  55080. + if (diepint.b.bna) {
  55081. + CLEAR_IN_EP_INTR(core_if, epnum, bna);
  55082. + if (core_if->dma_desc_enable) {
  55083. +#ifdef DWC_EN_ISOC
  55084. + if (dwc_ep->type ==
  55085. + DWC_OTG_EP_TYPE_ISOC) {
  55086. + /*
  55087. + * This checking is performed to prevent first "false" BNA
  55088. + * handling occuring right after reconnect
  55089. + */
  55090. + if (dwc_ep->next_frame !=
  55091. + 0xffffffff)
  55092. + dwc_otg_pcd_handle_iso_bna(ep);
  55093. + } else
  55094. +#endif /* DWC_EN_ISOC */
  55095. + {
  55096. + dwc_otg_pcd_handle_noniso_bna(ep);
  55097. + }
  55098. + }
  55099. + }
  55100. + /* NAK Interrutp */
  55101. + if (diepint.b.nak) {
  55102. + DWC_DEBUGPL(DBG_ANY, "EP%d IN NAK Interrupt\n",
  55103. + epnum);
  55104. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  55105. + depctl_data_t depctl;
  55106. + if (ep->dwc_ep.frame_num == 0xFFFFFFFF) {
  55107. + ep->dwc_ep.frame_num = core_if->frame_num;
  55108. + if (ep->dwc_ep.bInterval > 1) {
  55109. + depctl.d32 = 0;
  55110. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl);
  55111. + if (ep->dwc_ep.frame_num & 0x1) {
  55112. + depctl.b.setd1pid = 1;
  55113. + depctl.b.setd0pid = 0;
  55114. + } else {
  55115. + depctl.b.setd0pid = 1;
  55116. + depctl.b.setd1pid = 0;
  55117. + }
  55118. + DWC_WRITE_REG32(&dev_if->in_ep_regs[epnum]->diepctl, depctl.d32);
  55119. + }
  55120. + start_next_request(ep);
  55121. + }
  55122. + ep->dwc_ep.frame_num += ep->dwc_ep.bInterval;
  55123. + if (dwc_ep->frame_num > 0x3FFF) {
  55124. + dwc_ep->frm_overrun = 1;
  55125. + dwc_ep->frame_num &= 0x3FFF;
  55126. + } else
  55127. + dwc_ep->frm_overrun = 0;
  55128. + }
  55129. +
  55130. + CLEAR_IN_EP_INTR(core_if, epnum, nak);
  55131. + }
  55132. + }
  55133. + epnum++;
  55134. + ep_intr >>= 1;
  55135. + }
  55136. +
  55137. + return 1;
  55138. +#undef CLEAR_IN_EP_INTR
  55139. +}
  55140. +
  55141. +/**
  55142. + * This interrupt indicates that an OUT EP has a pending Interrupt.
  55143. + * The sequence for handling the OUT EP interrupt is shown below:
  55144. + * -# Read the Device All Endpoint Interrupt register
  55145. + * -# Repeat the following for each OUT EP interrupt bit set (from
  55146. + * LSB to MSB).
  55147. + * -# Read the Device Endpoint Interrupt (DOEPINTn) register
  55148. + * -# If "Transfer Complete" call the request complete function
  55149. + * -# If "Endpoint Disabled" complete the EP disable procedure.
  55150. + * -# If "AHB Error Interrupt" log error
  55151. + * -# If "Setup Phase Done" process Setup Packet (See Standard USB
  55152. + * Command Processing)
  55153. + */
  55154. +static int32_t dwc_otg_pcd_handle_out_ep_intr(dwc_otg_pcd_t * pcd)
  55155. +{
  55156. +#define CLEAR_OUT_EP_INTR(__core_if,__epnum,__intr) \
  55157. +do { \
  55158. + doepint_data_t doepint = {.d32=0}; \
  55159. + doepint.b.__intr = 1; \
  55160. + DWC_WRITE_REG32(&__core_if->dev_if->out_ep_regs[__epnum]->doepint, \
  55161. + doepint.d32); \
  55162. +} while (0)
  55163. +
  55164. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  55165. + uint32_t ep_intr;
  55166. + doepint_data_t doepint = {.d32 = 0 };
  55167. + uint32_t epnum = 0;
  55168. + dwc_otg_pcd_ep_t *ep;
  55169. + dwc_ep_t *dwc_ep;
  55170. + dctl_data_t dctl = {.d32 = 0 };
  55171. + gintmsk_data_t gintmsk = {.d32 = 0 };
  55172. +
  55173. +
  55174. + DWC_DEBUGPL(DBG_PCDV, "%s()\n", __func__);
  55175. +
  55176. + /* Read in the device interrupt bits */
  55177. + ep_intr = dwc_otg_read_dev_all_out_ep_intr(core_if);
  55178. +
  55179. + while (ep_intr) {
  55180. + if (ep_intr & 0x1) {
  55181. + /* Get EP pointer */
  55182. + ep = get_out_ep(pcd, epnum);
  55183. + dwc_ep = &ep->dwc_ep;
  55184. +
  55185. +#ifdef VERBOSE
  55186. + DWC_DEBUGPL(DBG_PCDV,
  55187. + "EP%d-%s: type=%d, mps=%d\n",
  55188. + dwc_ep->num, (dwc_ep->is_in ? "IN" : "OUT"),
  55189. + dwc_ep->type, dwc_ep->maxpacket);
  55190. +#endif
  55191. + doepint.d32 =
  55192. + dwc_otg_read_dev_out_ep_intr(core_if, dwc_ep);
  55193. + /* Moved this interrupt upper due to core deffect of asserting
  55194. + * OUT EP 0 xfercompl along with stsphsrcvd in BDMA */
  55195. + if (doepint.b.stsphsercvd) {
  55196. + deptsiz0_data_t deptsiz;
  55197. + CLEAR_OUT_EP_INTR(core_if, epnum, stsphsercvd);
  55198. + deptsiz.d32 =
  55199. + DWC_READ_REG32(&core_if->dev_if->
  55200. + out_ep_regs[0]->doeptsiz);
  55201. + if (core_if->snpsid >= OTG_CORE_REV_3_00a
  55202. + && core_if->dma_enable
  55203. + && core_if->dma_desc_enable == 0
  55204. + && doepint.b.xfercompl
  55205. + && deptsiz.b.xfersize == 24) {
  55206. + CLEAR_OUT_EP_INTR(core_if, epnum,
  55207. + xfercompl);
  55208. + doepint.b.xfercompl = 0;
  55209. + ep0_out_start(core_if, pcd);
  55210. + }
  55211. + if ((core_if->dma_desc_enable) ||
  55212. + (core_if->dma_enable
  55213. + && core_if->snpsid >=
  55214. + OTG_CORE_REV_3_00a)) {
  55215. + do_setup_in_status_phase(pcd);
  55216. + }
  55217. + }
  55218. + /* Transfer complete */
  55219. + if (doepint.b.xfercompl) {
  55220. +
  55221. + if (epnum == 0) {
  55222. + /* Clear the bit in DOEPINTn for this interrupt */
  55223. + CLEAR_OUT_EP_INTR(core_if, epnum, xfercompl);
  55224. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  55225. + DWC_DEBUGPL(DBG_PCDV, "DOEPINT=%x doepint=%x\n",
  55226. + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[0]->doepint),
  55227. + doepint.d32);
  55228. + DWC_DEBUGPL(DBG_PCDV, "DOEPCTL=%x \n",
  55229. + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[0]->doepctl));
  55230. +
  55231. + if (core_if->snpsid >= OTG_CORE_REV_3_00a
  55232. + && core_if->dma_enable == 0) {
  55233. + doepint_data_t doepint;
  55234. + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  55235. + out_ep_regs[0]->doepint);
  55236. + if (pcd->ep0state == EP0_IDLE && doepint.b.sr) {
  55237. + CLEAR_OUT_EP_INTR(core_if, epnum, sr);
  55238. + goto exit_xfercompl;
  55239. + }
  55240. + }
  55241. + /* In case of DDMA look at SR bit to go to the Data Stage */
  55242. + if (core_if->dma_desc_enable) {
  55243. + dev_dma_desc_sts_t status = {.d32 = 0};
  55244. + if (pcd->ep0state == EP0_IDLE) {
  55245. + status.d32 = core_if->dev_if->setup_desc_addr[core_if->
  55246. + dev_if->setup_desc_index]->status.d32;
  55247. + if(pcd->data_terminated) {
  55248. + pcd->data_terminated = 0;
  55249. + status.d32 = core_if->dev_if->out_desc_addr->status.d32;
  55250. + dwc_memcpy(&pcd->setup_pkt->req, pcd->backup_buf, 8);
  55251. + }
  55252. + if (status.b.sr) {
  55253. + if (doepint.b.setup) {
  55254. + DWC_DEBUGPL(DBG_PCDV, "DMA DESC EP0_IDLE SR=1 setup=1\n");
  55255. + /* Already started data stage, clear setup */
  55256. + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
  55257. + doepint.b.setup = 0;
  55258. + handle_ep0(pcd);
  55259. + /* Prepare for more setup packets */
  55260. + if (pcd->ep0state == EP0_IN_STATUS_PHASE ||
  55261. + pcd->ep0state == EP0_IN_DATA_PHASE) {
  55262. + ep0_out_start(core_if, pcd);
  55263. + }
  55264. +
  55265. + goto exit_xfercompl;
  55266. + } else {
  55267. + /* Prepare for more setup packets */
  55268. + DWC_DEBUGPL(DBG_PCDV,
  55269. + "EP0_IDLE SR=1 setup=0 new setup comes\n");
  55270. + ep0_out_start(core_if, pcd);
  55271. + }
  55272. + }
  55273. + } else {
  55274. + dwc_otg_pcd_request_t *req;
  55275. + dev_dma_desc_sts_t status = {.d32 = 0};
  55276. + diepint_data_t diepint0;
  55277. + diepint0.d32 = DWC_READ_REG32(&core_if->dev_if->
  55278. + in_ep_regs[0]->diepint);
  55279. +
  55280. + if (pcd->ep0state == EP0_STALL || pcd->ep0state == EP0_DISCONNECT) {
  55281. + DWC_ERROR("EP0 is stalled/disconnected\n");
  55282. + }
  55283. +
  55284. + /* Clear IN xfercompl if set */
  55285. + if (diepint0.b.xfercompl && (pcd->ep0state == EP0_IN_STATUS_PHASE
  55286. + || pcd->ep0state == EP0_IN_DATA_PHASE)) {
  55287. + DWC_WRITE_REG32(&core_if->dev_if->
  55288. + in_ep_regs[0]->diepint, diepint0.d32);
  55289. + }
  55290. +
  55291. + status.d32 = core_if->dev_if->setup_desc_addr[core_if->
  55292. + dev_if->setup_desc_index]->status.d32;
  55293. +
  55294. + if (ep->dwc_ep.xfer_count != ep->dwc_ep.total_len
  55295. + && (pcd->ep0state == EP0_OUT_DATA_PHASE))
  55296. + status.d32 = core_if->dev_if->out_desc_addr->status.d32;
  55297. + if (pcd->ep0state == EP0_OUT_STATUS_PHASE)
  55298. + status.d32 = core_if->dev_if->
  55299. + out_desc_addr->status.d32;
  55300. +
  55301. + if (status.b.sr) {
  55302. + if (DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  55303. + DWC_DEBUGPL(DBG_PCDV, "Request queue empty!!\n");
  55304. + } else {
  55305. + DWC_DEBUGPL(DBG_PCDV, "complete req!!\n");
  55306. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  55307. + if (ep->dwc_ep.xfer_count != ep->dwc_ep.total_len &&
  55308. + pcd->ep0state == EP0_OUT_DATA_PHASE) {
  55309. + /* Read arrived setup packet from req->buf */
  55310. + dwc_memcpy(&pcd->setup_pkt->req,
  55311. + req->buf + ep->dwc_ep.xfer_count, 8);
  55312. + }
  55313. + req->actual = ep->dwc_ep.xfer_count;
  55314. + dwc_otg_request_done(ep, req, -ECONNRESET);
  55315. + ep->dwc_ep.start_xfer_buff = 0;
  55316. + ep->dwc_ep.xfer_buff = 0;
  55317. + ep->dwc_ep.xfer_len = 0;
  55318. + }
  55319. + pcd->ep0state = EP0_IDLE;
  55320. + if (doepint.b.setup) {
  55321. + DWC_DEBUGPL(DBG_PCDV, "EP0_IDLE SR=1 setup=1\n");
  55322. + /* Data stage started, clear setup */
  55323. + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
  55324. + doepint.b.setup = 0;
  55325. + handle_ep0(pcd);
  55326. + /* Prepare for setup packets if ep0in was enabled*/
  55327. + if (pcd->ep0state == EP0_IN_STATUS_PHASE) {
  55328. + ep0_out_start(core_if, pcd);
  55329. + }
  55330. +
  55331. + goto exit_xfercompl;
  55332. + } else {
  55333. + /* Prepare for more setup packets */
  55334. + DWC_DEBUGPL(DBG_PCDV,
  55335. + "EP0_IDLE SR=1 setup=0 new setup comes 2\n");
  55336. + ep0_out_start(core_if, pcd);
  55337. + }
  55338. + }
  55339. + }
  55340. + }
  55341. + if (core_if->snpsid >= OTG_CORE_REV_2_94a && core_if->dma_enable
  55342. + && core_if->dma_desc_enable == 0) {
  55343. + doepint_data_t doepint_temp = {.d32 = 0};
  55344. + deptsiz0_data_t doeptsize0 = {.d32 = 0 };
  55345. + doepint_temp.d32 = DWC_READ_REG32(&core_if->dev_if->
  55346. + out_ep_regs[ep->dwc_ep.num]->doepint);
  55347. + doeptsize0.d32 = DWC_READ_REG32(&core_if->dev_if->
  55348. + out_ep_regs[ep->dwc_ep.num]->doeptsiz);
  55349. + if (pcd->ep0state == EP0_IDLE) {
  55350. + if (doepint_temp.b.sr) {
  55351. + CLEAR_OUT_EP_INTR(core_if, epnum, sr);
  55352. + }
  55353. + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  55354. + out_ep_regs[0]->doepint);
  55355. + if (doeptsize0.b.supcnt == 3) {
  55356. + DWC_DEBUGPL(DBG_ANY, "Rolling over!!!!!!!\n");
  55357. + ep->dwc_ep.stp_rollover = 1;
  55358. + }
  55359. + if (doepint.b.setup) {
  55360. +retry:
  55361. + /* Already started data stage, clear setup */
  55362. + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
  55363. + doepint.b.setup = 0;
  55364. + handle_ep0(pcd);
  55365. + ep->dwc_ep.stp_rollover = 0;
  55366. + /* Prepare for more setup packets */
  55367. + if (pcd->ep0state == EP0_IN_STATUS_PHASE ||
  55368. + pcd->ep0state == EP0_IN_DATA_PHASE) {
  55369. + ep0_out_start(core_if, pcd);
  55370. + }
  55371. + goto exit_xfercompl;
  55372. + } else {
  55373. + /* Prepare for more setup packets */
  55374. + DWC_DEBUGPL(DBG_ANY,
  55375. + "EP0_IDLE SR=1 setup=0 new setup comes\n");
  55376. + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  55377. + out_ep_regs[0]->doepint);
  55378. + if(doepint.b.setup)
  55379. + goto retry;
  55380. + ep0_out_start(core_if, pcd);
  55381. + }
  55382. + } else {
  55383. + dwc_otg_pcd_request_t *req;
  55384. + diepint_data_t diepint0 = {.d32 = 0};
  55385. + doepint_data_t doepint_temp = {.d32 = 0};
  55386. + depctl_data_t diepctl0;
  55387. + diepint0.d32 = DWC_READ_REG32(&core_if->dev_if->
  55388. + in_ep_regs[0]->diepint);
  55389. + diepctl0.d32 = DWC_READ_REG32(&core_if->dev_if->
  55390. + in_ep_regs[0]->diepctl);
  55391. +
  55392. + if (pcd->ep0state == EP0_IN_DATA_PHASE
  55393. + || pcd->ep0state == EP0_IN_STATUS_PHASE) {
  55394. + if (diepint0.b.xfercompl) {
  55395. + DWC_WRITE_REG32(&core_if->dev_if->
  55396. + in_ep_regs[0]->diepint, diepint0.d32);
  55397. + }
  55398. + if (diepctl0.b.epena) {
  55399. + diepint_data_t diepint = {.d32 = 0};
  55400. + diepctl0.b.snak = 1;
  55401. + DWC_WRITE_REG32(&core_if->dev_if->
  55402. + in_ep_regs[0]->diepctl, diepctl0.d32);
  55403. + do {
  55404. + dwc_udelay(10);
  55405. + diepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  55406. + in_ep_regs[0]->diepint);
  55407. + } while (!diepint.b.inepnakeff);
  55408. + diepint.b.inepnakeff = 1;
  55409. + DWC_WRITE_REG32(&core_if->dev_if->
  55410. + in_ep_regs[0]->diepint, diepint.d32);
  55411. + diepctl0.d32 = 0;
  55412. + diepctl0.b.epdis = 1;
  55413. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[0]->diepctl,
  55414. + diepctl0.d32);
  55415. + do {
  55416. + dwc_udelay(10);
  55417. + diepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  55418. + in_ep_regs[0]->diepint);
  55419. + } while (!diepint.b.epdisabled);
  55420. + diepint.b.epdisabled = 1;
  55421. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[0]->diepint,
  55422. + diepint.d32);
  55423. + }
  55424. + }
  55425. + doepint_temp.d32 = DWC_READ_REG32(&core_if->dev_if->
  55426. + out_ep_regs[ep->dwc_ep.num]->doepint);
  55427. + if (doepint_temp.b.sr) {
  55428. + CLEAR_OUT_EP_INTR(core_if, epnum, sr);
  55429. + if (DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  55430. + DWC_DEBUGPL(DBG_PCDV, "Request queue empty!!\n");
  55431. + } else {
  55432. + DWC_DEBUGPL(DBG_PCDV, "complete req!!\n");
  55433. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  55434. + if (ep->dwc_ep.xfer_count != ep->dwc_ep.total_len &&
  55435. + pcd->ep0state == EP0_OUT_DATA_PHASE) {
  55436. + /* Read arrived setup packet from req->buf */
  55437. + dwc_memcpy(&pcd->setup_pkt->req,
  55438. + req->buf + ep->dwc_ep.xfer_count, 8);
  55439. + }
  55440. + req->actual = ep->dwc_ep.xfer_count;
  55441. + dwc_otg_request_done(ep, req, -ECONNRESET);
  55442. + ep->dwc_ep.start_xfer_buff = 0;
  55443. + ep->dwc_ep.xfer_buff = 0;
  55444. + ep->dwc_ep.xfer_len = 0;
  55445. + }
  55446. + pcd->ep0state = EP0_IDLE;
  55447. + if (doepint.b.setup) {
  55448. + DWC_DEBUGPL(DBG_PCDV, "EP0_IDLE SR=1 setup=1\n");
  55449. + /* Data stage started, clear setup */
  55450. + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
  55451. + doepint.b.setup = 0;
  55452. + handle_ep0(pcd);
  55453. + /* Prepare for setup packets if ep0in was enabled*/
  55454. + if (pcd->ep0state == EP0_IN_STATUS_PHASE) {
  55455. + ep0_out_start(core_if, pcd);
  55456. + }
  55457. + goto exit_xfercompl;
  55458. + } else {
  55459. + /* Prepare for more setup packets */
  55460. + DWC_DEBUGPL(DBG_PCDV,
  55461. + "EP0_IDLE SR=1 setup=0 new setup comes 2\n");
  55462. + ep0_out_start(core_if, pcd);
  55463. + }
  55464. + }
  55465. + }
  55466. + }
  55467. + if (core_if->dma_enable == 0 || pcd->ep0state != EP0_IDLE)
  55468. + handle_ep0(pcd);
  55469. +exit_xfercompl:
  55470. + DWC_DEBUGPL(DBG_PCDV, "DOEPINT=%x doepint=%x\n",
  55471. + dwc_otg_read_dev_out_ep_intr(core_if, dwc_ep), doepint.d32);
  55472. + } else {
  55473. + if (core_if->dma_desc_enable == 0
  55474. + || pcd->ep0state != EP0_IDLE)
  55475. + handle_ep0(pcd);
  55476. + }
  55477. +#ifdef DWC_EN_ISOC
  55478. + } else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  55479. + if (doepint.b.pktdrpsts == 0) {
  55480. + /* Clear the bit in DOEPINTn for this interrupt */
  55481. + CLEAR_OUT_EP_INTR(core_if,
  55482. + epnum,
  55483. + xfercompl);
  55484. + complete_iso_ep(pcd, ep);
  55485. + } else {
  55486. +
  55487. + doepint_data_t doepint = {.d32 = 0 };
  55488. + doepint.b.xfercompl = 1;
  55489. + doepint.b.pktdrpsts = 1;
  55490. + DWC_WRITE_REG32
  55491. + (&core_if->dev_if->out_ep_regs
  55492. + [epnum]->doepint,
  55493. + doepint.d32);
  55494. + if (handle_iso_out_pkt_dropped
  55495. + (core_if, dwc_ep)) {
  55496. + complete_iso_ep(pcd,
  55497. + ep);
  55498. + }
  55499. + }
  55500. +#endif /* DWC_EN_ISOC */
  55501. +#ifdef DWC_UTE_PER_IO
  55502. + } else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  55503. + CLEAR_OUT_EP_INTR(core_if, epnum, xfercompl);
  55504. + if (!ep->stopped)
  55505. + complete_xiso_ep(ep);
  55506. +#endif /* DWC_UTE_PER_IO */
  55507. + } else {
  55508. + /* Clear the bit in DOEPINTn for this interrupt */
  55509. + CLEAR_OUT_EP_INTR(core_if, epnum,
  55510. + xfercompl);
  55511. +
  55512. + if (core_if->core_params->dev_out_nak) {
  55513. + DWC_TIMER_CANCEL(pcd->core_if->ep_xfer_timer[epnum]);
  55514. + pcd->core_if->ep_xfer_info[epnum].state = 0;
  55515. +#ifdef DEBUG
  55516. + print_memory_payload(pcd, dwc_ep);
  55517. +#endif
  55518. + }
  55519. + complete_ep(ep);
  55520. + }
  55521. +
  55522. + }
  55523. +
  55524. + /* Endpoint disable */
  55525. + if (doepint.b.epdisabled) {
  55526. +
  55527. + /* Clear the bit in DOEPINTn for this interrupt */
  55528. + CLEAR_OUT_EP_INTR(core_if, epnum, epdisabled);
  55529. + if (core_if->core_params->dev_out_nak) {
  55530. +#ifdef DEBUG
  55531. + print_memory_payload(pcd, dwc_ep);
  55532. +#endif
  55533. + /* In case of timeout condition */
  55534. + if (core_if->ep_xfer_info[epnum].state == 2) {
  55535. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->
  55536. + dev_global_regs->dctl);
  55537. + dctl.b.cgoutnak = 1;
  55538. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl,
  55539. + dctl.d32);
  55540. + /* Unmask goutnakeff interrupt which was masked
  55541. + * during handle nak out interrupt */
  55542. + gintmsk.b.goutnakeff = 1;
  55543. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
  55544. + 0, gintmsk.d32);
  55545. +
  55546. + complete_ep(ep);
  55547. + }
  55548. + }
  55549. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC)
  55550. + {
  55551. + dctl_data_t dctl;
  55552. + gintmsk_data_t intr_mask = {.d32 = 0};
  55553. + dwc_otg_pcd_request_t *req = 0;
  55554. +
  55555. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->
  55556. + dev_global_regs->dctl);
  55557. + dctl.b.cgoutnak = 1;
  55558. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl,
  55559. + dctl.d32);
  55560. +
  55561. + intr_mask.d32 = 0;
  55562. + intr_mask.b.incomplisoout = 1;
  55563. +
  55564. + /* Get any pending requests */
  55565. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  55566. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  55567. + if (!req) {
  55568. + DWC_PRINTF("complete_ep 0x%p, req = NULL!\n", ep);
  55569. + } else {
  55570. + dwc_otg_request_done(ep, req, 0);
  55571. + start_next_request(ep);
  55572. + }
  55573. + } else {
  55574. + DWC_PRINTF("complete_ep 0x%p, ep->queue empty!\n", ep);
  55575. + }
  55576. + }
  55577. + }
  55578. + /* AHB Error */
  55579. + if (doepint.b.ahberr) {
  55580. + DWC_ERROR("EP%d OUT AHB Error\n", epnum);
  55581. + DWC_ERROR("EP%d DEPDMA=0x%08x \n",
  55582. + epnum, core_if->dev_if->out_ep_regs[epnum]->doepdma);
  55583. + CLEAR_OUT_EP_INTR(core_if, epnum, ahberr);
  55584. + }
  55585. + /* Setup Phase Done (contorl EPs) */
  55586. + if (doepint.b.setup) {
  55587. +#ifdef DEBUG_EP0
  55588. + DWC_DEBUGPL(DBG_PCD, "EP%d SETUP Done\n", epnum);
  55589. +#endif
  55590. + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
  55591. +
  55592. + handle_ep0(pcd);
  55593. + }
  55594. +
  55595. + /** OUT EP BNA Intr */
  55596. + if (doepint.b.bna) {
  55597. + CLEAR_OUT_EP_INTR(core_if, epnum, bna);
  55598. + if (core_if->dma_desc_enable) {
  55599. +#ifdef DWC_EN_ISOC
  55600. + if (dwc_ep->type ==
  55601. + DWC_OTG_EP_TYPE_ISOC) {
  55602. + /*
  55603. + * This checking is performed to prevent first "false" BNA
  55604. + * handling occuring right after reconnect
  55605. + */
  55606. + if (dwc_ep->next_frame !=
  55607. + 0xffffffff)
  55608. + dwc_otg_pcd_handle_iso_bna(ep);
  55609. + } else
  55610. +#endif /* DWC_EN_ISOC */
  55611. + {
  55612. + dwc_otg_pcd_handle_noniso_bna(ep);
  55613. + }
  55614. + }
  55615. + }
  55616. + /* Babble Interrupt */
  55617. + if (doepint.b.babble) {
  55618. + DWC_DEBUGPL(DBG_ANY, "EP%d OUT Babble\n",
  55619. + epnum);
  55620. + handle_out_ep_babble_intr(pcd, epnum);
  55621. +
  55622. + CLEAR_OUT_EP_INTR(core_if, epnum, babble);
  55623. + }
  55624. + if (doepint.b.outtknepdis) {
  55625. + DWC_DEBUGPL(DBG_ANY, "EP%d OUT Token received when EP is \
  55626. + disabled\n",epnum);
  55627. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  55628. + doepmsk_data_t doepmsk = {.d32 = 0};
  55629. + ep->dwc_ep.frame_num = core_if->frame_num;
  55630. + if (ep->dwc_ep.bInterval > 1) {
  55631. + depctl_data_t depctl;
  55632. + depctl.d32 = DWC_READ_REG32(&core_if->dev_if->
  55633. + out_ep_regs[epnum]->doepctl);
  55634. + if (ep->dwc_ep.frame_num & 0x1) {
  55635. + depctl.b.setd1pid = 1;
  55636. + depctl.b.setd0pid = 0;
  55637. + } else {
  55638. + depctl.b.setd0pid = 1;
  55639. + depctl.b.setd1pid = 0;
  55640. + }
  55641. + DWC_WRITE_REG32(&core_if->dev_if->
  55642. + out_ep_regs[epnum]->doepctl, depctl.d32);
  55643. + }
  55644. + start_next_request(ep);
  55645. + doepmsk.b.outtknepdis = 1;
  55646. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
  55647. + doepmsk.d32, 0);
  55648. + }
  55649. + CLEAR_OUT_EP_INTR(core_if, epnum, outtknepdis);
  55650. + }
  55651. +
  55652. + /* NAK Interrutp */
  55653. + if (doepint.b.nak) {
  55654. + DWC_DEBUGPL(DBG_ANY, "EP%d OUT NAK\n", epnum);
  55655. + handle_out_ep_nak_intr(pcd, epnum);
  55656. +
  55657. + CLEAR_OUT_EP_INTR(core_if, epnum, nak);
  55658. + }
  55659. + /* NYET Interrutp */
  55660. + if (doepint.b.nyet) {
  55661. + DWC_DEBUGPL(DBG_ANY, "EP%d OUT NYET\n", epnum);
  55662. + handle_out_ep_nyet_intr(pcd, epnum);
  55663. +
  55664. + CLEAR_OUT_EP_INTR(core_if, epnum, nyet);
  55665. + }
  55666. + }
  55667. +
  55668. + epnum++;
  55669. + ep_intr >>= 1;
  55670. + }
  55671. +
  55672. + return 1;
  55673. +
  55674. +#undef CLEAR_OUT_EP_INTR
  55675. +}
  55676. +static int drop_transfer(uint32_t trgt_fr, uint32_t curr_fr, uint8_t frm_overrun)
  55677. +{
  55678. + int retval = 0;
  55679. + if(!frm_overrun && curr_fr >= trgt_fr)
  55680. + retval = 1;
  55681. + else if (frm_overrun
  55682. + && (curr_fr >= trgt_fr && ((curr_fr - trgt_fr) < 0x3FFF / 2)))
  55683. + retval = 1;
  55684. + return retval;
  55685. +}
  55686. +/**
  55687. + * Incomplete ISO IN Transfer Interrupt.
  55688. + * This interrupt indicates one of the following conditions occurred
  55689. + * while transmitting an ISOC transaction.
  55690. + * - Corrupted IN Token for ISOC EP.
  55691. + * - Packet not complete in FIFO.
  55692. + * The follow actions will be taken:
  55693. + * -# Determine the EP
  55694. + * -# Set incomplete flag in dwc_ep structure
  55695. + * -# Disable EP; when "Endpoint Disabled" interrupt is received
  55696. + * Flush FIFO
  55697. + */
  55698. +int32_t dwc_otg_pcd_handle_incomplete_isoc_in_intr(dwc_otg_pcd_t * pcd)
  55699. +{
  55700. + gintsts_data_t gintsts;
  55701. +
  55702. +#ifdef DWC_EN_ISOC
  55703. + dwc_otg_dev_if_t *dev_if;
  55704. + deptsiz_data_t deptsiz = {.d32 = 0 };
  55705. + depctl_data_t depctl = {.d32 = 0 };
  55706. + dsts_data_t dsts = {.d32 = 0 };
  55707. + dwc_ep_t *dwc_ep;
  55708. + int i;
  55709. +
  55710. + dev_if = GET_CORE_IF(pcd)->dev_if;
  55711. +
  55712. + for (i = 1; i <= dev_if->num_in_eps; ++i) {
  55713. + dwc_ep = &pcd->in_ep[i].dwc_ep;
  55714. + if (dwc_ep->active && dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  55715. + deptsiz.d32 =
  55716. + DWC_READ_REG32(&dev_if->in_ep_regs[i]->dieptsiz);
  55717. + depctl.d32 =
  55718. + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  55719. +
  55720. + if (depctl.b.epdis && deptsiz.d32) {
  55721. + set_current_pkt_info(GET_CORE_IF(pcd), dwc_ep);
  55722. + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
  55723. + dwc_ep->cur_pkt = 0;
  55724. + dwc_ep->proc_buf_num =
  55725. + (dwc_ep->proc_buf_num ^ 1) & 0x1;
  55726. +
  55727. + if (dwc_ep->proc_buf_num) {
  55728. + dwc_ep->cur_pkt_addr =
  55729. + dwc_ep->xfer_buff1;
  55730. + dwc_ep->cur_pkt_dma_addr =
  55731. + dwc_ep->dma_addr1;
  55732. + } else {
  55733. + dwc_ep->cur_pkt_addr =
  55734. + dwc_ep->xfer_buff0;
  55735. + dwc_ep->cur_pkt_dma_addr =
  55736. + dwc_ep->dma_addr0;
  55737. + }
  55738. +
  55739. + }
  55740. +
  55741. + dsts.d32 =
  55742. + DWC_READ_REG32(&GET_CORE_IF(pcd)->dev_if->
  55743. + dev_global_regs->dsts);
  55744. + dwc_ep->next_frame = dsts.b.soffn;
  55745. +
  55746. + dwc_otg_iso_ep_start_frm_transfer(GET_CORE_IF
  55747. + (pcd),
  55748. + dwc_ep);
  55749. + }
  55750. + }
  55751. + }
  55752. +
  55753. +#else
  55754. + depctl_data_t depctl = {.d32 = 0 };
  55755. + dwc_ep_t *dwc_ep;
  55756. + dwc_otg_dev_if_t *dev_if;
  55757. + int i;
  55758. + dev_if = GET_CORE_IF(pcd)->dev_if;
  55759. +
  55760. + DWC_DEBUGPL(DBG_PCD,"Incomplete ISO IN \n");
  55761. +
  55762. + for (i = 1; i <= dev_if->num_in_eps; ++i) {
  55763. + dwc_ep = &pcd->in_ep[i-1].dwc_ep;
  55764. + depctl.d32 =
  55765. + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  55766. + if (depctl.b.epena && dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  55767. + if (drop_transfer(dwc_ep->frame_num, GET_CORE_IF(pcd)->frame_num,
  55768. + dwc_ep->frm_overrun))
  55769. + {
  55770. + depctl.d32 =
  55771. + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  55772. + depctl.b.snak = 1;
  55773. + depctl.b.epdis = 1;
  55774. + DWC_MODIFY_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32, depctl.d32);
  55775. + }
  55776. + }
  55777. + }
  55778. +
  55779. + /*intr_mask.b.incomplisoin = 1;
  55780. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  55781. + intr_mask.d32, 0); */
  55782. +#endif //DWC_EN_ISOC
  55783. +
  55784. + /* Clear interrupt */
  55785. + gintsts.d32 = 0;
  55786. + gintsts.b.incomplisoin = 1;
  55787. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  55788. + gintsts.d32);
  55789. +
  55790. + return 1;
  55791. +}
  55792. +
  55793. +/**
  55794. + * Incomplete ISO OUT Transfer Interrupt.
  55795. + *
  55796. + * This interrupt indicates that the core has dropped an ISO OUT
  55797. + * packet. The following conditions can be the cause:
  55798. + * - FIFO Full, the entire packet would not fit in the FIFO.
  55799. + * - CRC Error
  55800. + * - Corrupted Token
  55801. + * The follow actions will be taken:
  55802. + * -# Determine the EP
  55803. + * -# Set incomplete flag in dwc_ep structure
  55804. + * -# Read any data from the FIFO
  55805. + * -# Disable EP. When "Endpoint Disabled" interrupt is received
  55806. + * re-enable EP.
  55807. + */
  55808. +int32_t dwc_otg_pcd_handle_incomplete_isoc_out_intr(dwc_otg_pcd_t * pcd)
  55809. +{
  55810. +
  55811. + gintsts_data_t gintsts;
  55812. +
  55813. +#ifdef DWC_EN_ISOC
  55814. + dwc_otg_dev_if_t *dev_if;
  55815. + deptsiz_data_t deptsiz = {.d32 = 0 };
  55816. + depctl_data_t depctl = {.d32 = 0 };
  55817. + dsts_data_t dsts = {.d32 = 0 };
  55818. + dwc_ep_t *dwc_ep;
  55819. + int i;
  55820. +
  55821. + dev_if = GET_CORE_IF(pcd)->dev_if;
  55822. +
  55823. + for (i = 1; i <= dev_if->num_out_eps; ++i) {
  55824. + dwc_ep = &pcd->in_ep[i].dwc_ep;
  55825. + if (pcd->out_ep[i].dwc_ep.active &&
  55826. + pcd->out_ep[i].dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  55827. + deptsiz.d32 =
  55828. + DWC_READ_REG32(&dev_if->out_ep_regs[i]->doeptsiz);
  55829. + depctl.d32 =
  55830. + DWC_READ_REG32(&dev_if->out_ep_regs[i]->doepctl);
  55831. +
  55832. + if (depctl.b.epdis && deptsiz.d32) {
  55833. + set_current_pkt_info(GET_CORE_IF(pcd),
  55834. + &pcd->out_ep[i].dwc_ep);
  55835. + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
  55836. + dwc_ep->cur_pkt = 0;
  55837. + dwc_ep->proc_buf_num =
  55838. + (dwc_ep->proc_buf_num ^ 1) & 0x1;
  55839. +
  55840. + if (dwc_ep->proc_buf_num) {
  55841. + dwc_ep->cur_pkt_addr =
  55842. + dwc_ep->xfer_buff1;
  55843. + dwc_ep->cur_pkt_dma_addr =
  55844. + dwc_ep->dma_addr1;
  55845. + } else {
  55846. + dwc_ep->cur_pkt_addr =
  55847. + dwc_ep->xfer_buff0;
  55848. + dwc_ep->cur_pkt_dma_addr =
  55849. + dwc_ep->dma_addr0;
  55850. + }
  55851. +
  55852. + }
  55853. +
  55854. + dsts.d32 =
  55855. + DWC_READ_REG32(&GET_CORE_IF(pcd)->dev_if->
  55856. + dev_global_regs->dsts);
  55857. + dwc_ep->next_frame = dsts.b.soffn;
  55858. +
  55859. + dwc_otg_iso_ep_start_frm_transfer(GET_CORE_IF
  55860. + (pcd),
  55861. + dwc_ep);
  55862. + }
  55863. + }
  55864. + }
  55865. +#else
  55866. + /** @todo implement ISR */
  55867. + gintmsk_data_t intr_mask = {.d32 = 0 };
  55868. + dwc_otg_core_if_t *core_if;
  55869. + deptsiz_data_t deptsiz = {.d32 = 0 };
  55870. + depctl_data_t depctl = {.d32 = 0 };
  55871. + dctl_data_t dctl = {.d32 = 0 };
  55872. + dwc_ep_t *dwc_ep = NULL;
  55873. + int i;
  55874. + core_if = GET_CORE_IF(pcd);
  55875. +
  55876. + for (i = 0; i < core_if->dev_if->num_out_eps; ++i) {
  55877. + dwc_ep = &pcd->out_ep[i].dwc_ep;
  55878. + depctl.d32 =
  55879. + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl);
  55880. + if (depctl.b.epena && depctl.b.dpid == (core_if->frame_num & 0x1)) {
  55881. + core_if->dev_if->isoc_ep = dwc_ep;
  55882. + deptsiz.d32 =
  55883. + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doeptsiz);
  55884. + break;
  55885. + }
  55886. + }
  55887. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
  55888. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  55889. + intr_mask.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  55890. +
  55891. + if (!intr_mask.b.goutnakeff) {
  55892. + /* Unmask it */
  55893. + intr_mask.b.goutnakeff = 1;
  55894. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, intr_mask.d32);
  55895. + }
  55896. + if (!gintsts.b.goutnakeff) {
  55897. + dctl.b.sgoutnak = 1;
  55898. + }
  55899. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
  55900. +
  55901. + depctl.d32 = DWC_READ_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl);
  55902. + if (depctl.b.epena) {
  55903. + depctl.b.epdis = 1;
  55904. + depctl.b.snak = 1;
  55905. + }
  55906. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl, depctl.d32);
  55907. +
  55908. + intr_mask.d32 = 0;
  55909. + intr_mask.b.incomplisoout = 1;
  55910. +
  55911. +#endif /* DWC_EN_ISOC */
  55912. +
  55913. + /* Clear interrupt */
  55914. + gintsts.d32 = 0;
  55915. + gintsts.b.incomplisoout = 1;
  55916. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  55917. + gintsts.d32);
  55918. +
  55919. + return 1;
  55920. +}
  55921. +
  55922. +/**
  55923. + * This function handles the Global IN NAK Effective interrupt.
  55924. + *
  55925. + */
  55926. +int32_t dwc_otg_pcd_handle_in_nak_effective(dwc_otg_pcd_t * pcd)
  55927. +{
  55928. + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
  55929. + depctl_data_t diepctl = {.d32 = 0 };
  55930. + gintmsk_data_t intr_mask = {.d32 = 0 };
  55931. + gintsts_data_t gintsts;
  55932. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  55933. + int i;
  55934. +
  55935. + DWC_DEBUGPL(DBG_PCD, "Global IN NAK Effective\n");
  55936. +
  55937. + /* Disable all active IN EPs */
  55938. + for (i = 0; i <= dev_if->num_in_eps; i++) {
  55939. + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  55940. + if (!(diepctl.b.eptype & 1) && diepctl.b.epena) {
  55941. + if (core_if->start_predict > 0)
  55942. + core_if->start_predict++;
  55943. + diepctl.b.epdis = 1;
  55944. + diepctl.b.snak = 1;
  55945. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, diepctl.d32);
  55946. + }
  55947. + }
  55948. +
  55949. +
  55950. + /* Disable the Global IN NAK Effective Interrupt */
  55951. + intr_mask.b.ginnakeff = 1;
  55952. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  55953. + intr_mask.d32, 0);
  55954. +
  55955. + /* Clear interrupt */
  55956. + gintsts.d32 = 0;
  55957. + gintsts.b.ginnakeff = 1;
  55958. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  55959. + gintsts.d32);
  55960. +
  55961. + return 1;
  55962. +}
  55963. +
  55964. +/**
  55965. + * OUT NAK Effective.
  55966. + *
  55967. + */
  55968. +int32_t dwc_otg_pcd_handle_out_nak_effective(dwc_otg_pcd_t * pcd)
  55969. +{
  55970. + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
  55971. + gintmsk_data_t intr_mask = {.d32 = 0 };
  55972. + gintsts_data_t gintsts;
  55973. + depctl_data_t doepctl;
  55974. + int i;
  55975. +
  55976. + /* Disable the Global OUT NAK Effective Interrupt */
  55977. + intr_mask.b.goutnakeff = 1;
  55978. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  55979. + intr_mask.d32, 0);
  55980. +
  55981. + /* If DEV OUT NAK enabled*/
  55982. + if (pcd->core_if->core_params->dev_out_nak) {
  55983. + /* Run over all out endpoints to determine the ep number on
  55984. + * which the timeout has happened
  55985. + */
  55986. + for (i = 0; i <= dev_if->num_out_eps; i++) {
  55987. + if ( pcd->core_if->ep_xfer_info[i].state == 2 )
  55988. + break;
  55989. + }
  55990. + if (i > dev_if->num_out_eps) {
  55991. + dctl_data_t dctl;
  55992. + dctl.d32 =
  55993. + DWC_READ_REG32(&dev_if->dev_global_regs->dctl);
  55994. + dctl.b.cgoutnak = 1;
  55995. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dctl,
  55996. + dctl.d32);
  55997. + goto out;
  55998. + }
  55999. +
  56000. + /* Disable the endpoint */
  56001. + doepctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[i]->doepctl);
  56002. + if (doepctl.b.epena) {
  56003. + doepctl.b.epdis = 1;
  56004. + doepctl.b.snak = 1;
  56005. + }
  56006. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepctl, doepctl.d32);
  56007. + return 1;
  56008. + }
  56009. + /* We come here from Incomplete ISO OUT handler */
  56010. + if (dev_if->isoc_ep) {
  56011. + dwc_ep_t *dwc_ep = (dwc_ep_t *)dev_if->isoc_ep;
  56012. + uint32_t epnum = dwc_ep->num;
  56013. + doepint_data_t doepint;
  56014. + doepint.d32 =
  56015. + DWC_READ_REG32(&dev_if->out_ep_regs[dwc_ep->num]->doepint);
  56016. + dev_if->isoc_ep = NULL;
  56017. + doepctl.d32 =
  56018. + DWC_READ_REG32(&dev_if->out_ep_regs[epnum]->doepctl);
  56019. + DWC_PRINTF("Before disable DOEPCTL = %08x\n", doepctl.d32);
  56020. + if (doepctl.b.epena) {
  56021. + doepctl.b.epdis = 1;
  56022. + doepctl.b.snak = 1;
  56023. + }
  56024. + DWC_WRITE_REG32(&dev_if->out_ep_regs[epnum]->doepctl,
  56025. + doepctl.d32);
  56026. + return 1;
  56027. + } else
  56028. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n",
  56029. + "Global OUT NAK Effective\n");
  56030. +
  56031. +out:
  56032. + /* Clear interrupt */
  56033. + gintsts.d32 = 0;
  56034. + gintsts.b.goutnakeff = 1;
  56035. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  56036. + gintsts.d32);
  56037. +
  56038. + return 1;
  56039. +}
  56040. +
  56041. +/**
  56042. + * PCD interrupt handler.
  56043. + *
  56044. + * The PCD handles the device interrupts. Many conditions can cause a
  56045. + * device interrupt. When an interrupt occurs, the device interrupt
  56046. + * service routine determines the cause of the interrupt and
  56047. + * dispatches handling to the appropriate function. These interrupt
  56048. + * handling functions are described below.
  56049. + *
  56050. + * All interrupt registers are processed from LSB to MSB.
  56051. + *
  56052. + */
  56053. +int32_t dwc_otg_pcd_handle_intr(dwc_otg_pcd_t * pcd)
  56054. +{
  56055. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  56056. +#ifdef VERBOSE
  56057. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  56058. +#endif
  56059. + gintsts_data_t gintr_status;
  56060. + int32_t retval = 0;
  56061. +
  56062. + /* Exit from ISR if core is hibernated */
  56063. + if (core_if->hibernation_suspend == 1) {
  56064. + return retval;
  56065. + }
  56066. +#ifdef VERBOSE
  56067. + DWC_DEBUGPL(DBG_ANY, "%s() gintsts=%08x gintmsk=%08x\n",
  56068. + __func__,
  56069. + DWC_READ_REG32(&global_regs->gintsts),
  56070. + DWC_READ_REG32(&global_regs->gintmsk));
  56071. +#endif
  56072. +
  56073. + if (dwc_otg_is_device_mode(core_if)) {
  56074. + DWC_SPINLOCK(pcd->lock);
  56075. +#ifdef VERBOSE
  56076. + DWC_DEBUGPL(DBG_PCDV, "%s() gintsts=%08x gintmsk=%08x\n",
  56077. + __func__,
  56078. + DWC_READ_REG32(&global_regs->gintsts),
  56079. + DWC_READ_REG32(&global_regs->gintmsk));
  56080. +#endif
  56081. +
  56082. + gintr_status.d32 = dwc_otg_read_core_intr(core_if);
  56083. +
  56084. + DWC_DEBUGPL(DBG_PCDV, "%s: gintsts&gintmsk=%08x\n",
  56085. + __func__, gintr_status.d32);
  56086. +
  56087. + if (gintr_status.b.sofintr) {
  56088. + retval |= dwc_otg_pcd_handle_sof_intr(pcd);
  56089. + }
  56090. + if (gintr_status.b.rxstsqlvl) {
  56091. + retval |=
  56092. + dwc_otg_pcd_handle_rx_status_q_level_intr(pcd);
  56093. + }
  56094. + if (gintr_status.b.nptxfempty) {
  56095. + retval |= dwc_otg_pcd_handle_np_tx_fifo_empty_intr(pcd);
  56096. + }
  56097. + if (gintr_status.b.goutnakeff) {
  56098. + retval |= dwc_otg_pcd_handle_out_nak_effective(pcd);
  56099. + }
  56100. + if (gintr_status.b.i2cintr) {
  56101. + retval |= dwc_otg_pcd_handle_i2c_intr(pcd);
  56102. + }
  56103. + if (gintr_status.b.erlysuspend) {
  56104. + retval |= dwc_otg_pcd_handle_early_suspend_intr(pcd);
  56105. + }
  56106. + if (gintr_status.b.usbreset) {
  56107. + retval |= dwc_otg_pcd_handle_usb_reset_intr(pcd);
  56108. + }
  56109. + if (gintr_status.b.enumdone) {
  56110. + retval |= dwc_otg_pcd_handle_enum_done_intr(pcd);
  56111. + }
  56112. + if (gintr_status.b.isooutdrop) {
  56113. + retval |=
  56114. + dwc_otg_pcd_handle_isoc_out_packet_dropped_intr
  56115. + (pcd);
  56116. + }
  56117. + if (gintr_status.b.eopframe) {
  56118. + retval |=
  56119. + dwc_otg_pcd_handle_end_periodic_frame_intr(pcd);
  56120. + }
  56121. + if (gintr_status.b.inepint) {
  56122. + if (!core_if->multiproc_int_enable) {
  56123. + retval |= dwc_otg_pcd_handle_in_ep_intr(pcd);
  56124. + }
  56125. + }
  56126. + if (gintr_status.b.outepintr) {
  56127. + if (!core_if->multiproc_int_enable) {
  56128. + retval |= dwc_otg_pcd_handle_out_ep_intr(pcd);
  56129. + }
  56130. + }
  56131. + if (gintr_status.b.epmismatch) {
  56132. + retval |= dwc_otg_pcd_handle_ep_mismatch_intr(pcd);
  56133. + }
  56134. + if (gintr_status.b.fetsusp) {
  56135. + retval |= dwc_otg_pcd_handle_ep_fetsusp_intr(pcd);
  56136. + }
  56137. + if (gintr_status.b.ginnakeff) {
  56138. + retval |= dwc_otg_pcd_handle_in_nak_effective(pcd);
  56139. + }
  56140. + if (gintr_status.b.incomplisoin) {
  56141. + retval |=
  56142. + dwc_otg_pcd_handle_incomplete_isoc_in_intr(pcd);
  56143. + }
  56144. + if (gintr_status.b.incomplisoout) {
  56145. + retval |=
  56146. + dwc_otg_pcd_handle_incomplete_isoc_out_intr(pcd);
  56147. + }
  56148. +
  56149. + /* In MPI mode Device Endpoints interrupts are asserted
  56150. + * without setting outepintr and inepint bits set, so these
  56151. + * Interrupt handlers are called without checking these bit-fields
  56152. + */
  56153. + if (core_if->multiproc_int_enable) {
  56154. + retval |= dwc_otg_pcd_handle_in_ep_intr(pcd);
  56155. + retval |= dwc_otg_pcd_handle_out_ep_intr(pcd);
  56156. + }
  56157. +#ifdef VERBOSE
  56158. + DWC_DEBUGPL(DBG_PCDV, "%s() gintsts=%0x\n", __func__,
  56159. + DWC_READ_REG32(&global_regs->gintsts));
  56160. +#endif
  56161. + DWC_SPINUNLOCK(pcd->lock);
  56162. + }
  56163. + return retval;
  56164. +}
  56165. +
  56166. +#endif /* DWC_HOST_ONLY */
  56167. --- /dev/null
  56168. +++ b/drivers/usb/host/dwc_otg/dwc_otg_pcd_linux.c
  56169. @@ -0,0 +1,1262 @@
  56170. + /* ==========================================================================
  56171. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd_linux.c $
  56172. + * $Revision: #21 $
  56173. + * $Date: 2012/08/10 $
  56174. + * $Change: 2047372 $
  56175. + *
  56176. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  56177. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  56178. + * otherwise expressly agreed to in writing between Synopsys and you.
  56179. + *
  56180. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  56181. + * any End User Software License Agreement or Agreement for Licensed Product
  56182. + * with Synopsys or any supplement thereto. You are permitted to use and
  56183. + * redistribute this Software in source and binary forms, with or without
  56184. + * modification, provided that redistributions of source code must retain this
  56185. + * notice. You may not view, use, disclose, copy or distribute this file or
  56186. + * any information contained herein except pursuant to this license grant from
  56187. + * Synopsys. If you do not agree with this notice, including the disclaimer
  56188. + * below, then you are not authorized to use the Software.
  56189. + *
  56190. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  56191. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  56192. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  56193. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  56194. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  56195. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  56196. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  56197. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  56198. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  56199. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  56200. + * DAMAGE.
  56201. + * ========================================================================== */
  56202. +#ifndef DWC_HOST_ONLY
  56203. +
  56204. +/** @file
  56205. + * This file implements the Peripheral Controller Driver.
  56206. + *
  56207. + * The Peripheral Controller Driver (PCD) is responsible for
  56208. + * translating requests from the Function Driver into the appropriate
  56209. + * actions on the DWC_otg controller. It isolates the Function Driver
  56210. + * from the specifics of the controller by providing an API to the
  56211. + * Function Driver.
  56212. + *
  56213. + * The Peripheral Controller Driver for Linux will implement the
  56214. + * Gadget API, so that the existing Gadget drivers can be used.
  56215. + * (Gadget Driver is the Linux terminology for a Function Driver.)
  56216. + *
  56217. + * The Linux Gadget API is defined in the header file
  56218. + * <code><linux/usb_gadget.h></code>. The USB EP operations API is
  56219. + * defined in the structure <code>usb_ep_ops</code> and the USB
  56220. + * Controller API is defined in the structure
  56221. + * <code>usb_gadget_ops</code>.
  56222. + *
  56223. + */
  56224. +
  56225. +#include "dwc_otg_os_dep.h"
  56226. +#include "dwc_otg_pcd_if.h"
  56227. +#include "dwc_otg_pcd.h"
  56228. +#include "dwc_otg_driver.h"
  56229. +#include "dwc_otg_dbg.h"
  56230. +
  56231. +extern bool fiq_enable;
  56232. +
  56233. +static struct gadget_wrapper {
  56234. + dwc_otg_pcd_t *pcd;
  56235. +
  56236. + struct usb_gadget gadget;
  56237. + struct usb_gadget_driver *driver;
  56238. +
  56239. + struct usb_ep ep0;
  56240. + struct usb_ep in_ep[16];
  56241. + struct usb_ep out_ep[16];
  56242. +
  56243. +} *gadget_wrapper;
  56244. +
  56245. +/* Display the contents of the buffer */
  56246. +extern void dump_msg(const u8 * buf, unsigned int length);
  56247. +/**
  56248. + * Get the dwc_otg_pcd_ep_t* from usb_ep* pointer - NULL in case
  56249. + * if the endpoint is not found
  56250. + */
  56251. +static struct dwc_otg_pcd_ep *ep_from_handle(dwc_otg_pcd_t * pcd, void *handle)
  56252. +{
  56253. + int i;
  56254. + if (pcd->ep0.priv == handle) {
  56255. + return &pcd->ep0;
  56256. + }
  56257. +
  56258. + for (i = 0; i < MAX_EPS_CHANNELS - 1; i++) {
  56259. + if (pcd->in_ep[i].priv == handle)
  56260. + return &pcd->in_ep[i];
  56261. + if (pcd->out_ep[i].priv == handle)
  56262. + return &pcd->out_ep[i];
  56263. + }
  56264. +
  56265. + return NULL;
  56266. +}
  56267. +
  56268. +/* USB Endpoint Operations */
  56269. +/*
  56270. + * The following sections briefly describe the behavior of the Gadget
  56271. + * API endpoint operations implemented in the DWC_otg driver
  56272. + * software. Detailed descriptions of the generic behavior of each of
  56273. + * these functions can be found in the Linux header file
  56274. + * include/linux/usb_gadget.h.
  56275. + *
  56276. + * The Gadget API provides wrapper functions for each of the function
  56277. + * pointers defined in usb_ep_ops. The Gadget Driver calls the wrapper
  56278. + * function, which then calls the underlying PCD function. The
  56279. + * following sections are named according to the wrapper
  56280. + * functions. Within each section, the corresponding DWC_otg PCD
  56281. + * function name is specified.
  56282. + *
  56283. + */
  56284. +
  56285. +/**
  56286. + * This function is called by the Gadget Driver for each EP to be
  56287. + * configured for the current configuration (SET_CONFIGURATION).
  56288. + *
  56289. + * This function initializes the dwc_otg_ep_t data structure, and then
  56290. + * calls dwc_otg_ep_activate.
  56291. + */
  56292. +static int ep_enable(struct usb_ep *usb_ep,
  56293. + const struct usb_endpoint_descriptor *ep_desc)
  56294. +{
  56295. + int retval;
  56296. +
  56297. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p)\n", __func__, usb_ep, ep_desc);
  56298. +
  56299. + if (!usb_ep || !ep_desc || ep_desc->bDescriptorType != USB_DT_ENDPOINT) {
  56300. + DWC_WARN("%s, bad ep or descriptor\n", __func__);
  56301. + return -EINVAL;
  56302. + }
  56303. + if (usb_ep == &gadget_wrapper->ep0) {
  56304. + DWC_WARN("%s, bad ep(0)\n", __func__);
  56305. + return -EINVAL;
  56306. + }
  56307. +
  56308. + /* Check FIFO size? */
  56309. + if (!ep_desc->wMaxPacketSize) {
  56310. + DWC_WARN("%s, bad %s maxpacket\n", __func__, usb_ep->name);
  56311. + return -ERANGE;
  56312. + }
  56313. +
  56314. + if (!gadget_wrapper->driver ||
  56315. + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
  56316. + DWC_WARN("%s, bogus device state\n", __func__);
  56317. + return -ESHUTDOWN;
  56318. + }
  56319. +
  56320. + /* Delete after check - MAS */
  56321. +#if 0
  56322. + nat = (uint32_t) ep_desc->wMaxPacketSize;
  56323. + printk(KERN_ALERT "%s: nat (before) =%d\n", __func__, nat);
  56324. + nat = (nat >> 11) & 0x03;
  56325. + printk(KERN_ALERT "%s: nat (after) =%d\n", __func__, nat);
  56326. +#endif
  56327. + retval = dwc_otg_pcd_ep_enable(gadget_wrapper->pcd,
  56328. + (const uint8_t *)ep_desc,
  56329. + (void *)usb_ep);
  56330. + if (retval) {
  56331. + DWC_WARN("dwc_otg_pcd_ep_enable failed\n");
  56332. + return -EINVAL;
  56333. + }
  56334. +
  56335. + usb_ep->maxpacket = le16_to_cpu(ep_desc->wMaxPacketSize);
  56336. +
  56337. + return 0;
  56338. +}
  56339. +
  56340. +/**
  56341. + * This function is called when an EP is disabled due to disconnect or
  56342. + * change in configuration. Any pending requests will terminate with a
  56343. + * status of -ESHUTDOWN.
  56344. + *
  56345. + * This function modifies the dwc_otg_ep_t data structure for this EP,
  56346. + * and then calls dwc_otg_ep_deactivate.
  56347. + */
  56348. +static int ep_disable(struct usb_ep *usb_ep)
  56349. +{
  56350. + int retval;
  56351. +
  56352. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, usb_ep);
  56353. + if (!usb_ep) {
  56354. + DWC_DEBUGPL(DBG_PCD, "%s, %s not enabled\n", __func__,
  56355. + usb_ep ? usb_ep->name : NULL);
  56356. + return -EINVAL;
  56357. + }
  56358. +
  56359. + retval = dwc_otg_pcd_ep_disable(gadget_wrapper->pcd, usb_ep);
  56360. + if (retval) {
  56361. + retval = -EINVAL;
  56362. + }
  56363. +
  56364. + return retval;
  56365. +}
  56366. +
  56367. +/**
  56368. + * This function allocates a request object to use with the specified
  56369. + * endpoint.
  56370. + *
  56371. + * @param ep The endpoint to be used with with the request
  56372. + * @param gfp_flags the GFP_* flags to use.
  56373. + */
  56374. +static struct usb_request *dwc_otg_pcd_alloc_request(struct usb_ep *ep,
  56375. + gfp_t gfp_flags)
  56376. +{
  56377. + struct usb_request *usb_req;
  56378. +
  56379. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%d)\n", __func__, ep, gfp_flags);
  56380. + if (0 == ep) {
  56381. + DWC_WARN("%s() %s\n", __func__, "Invalid EP!\n");
  56382. + return 0;
  56383. + }
  56384. + usb_req = kzalloc(sizeof(*usb_req), gfp_flags);
  56385. + if (0 == usb_req) {
  56386. + DWC_WARN("%s() %s\n", __func__, "request allocation failed!\n");
  56387. + return 0;
  56388. + }
  56389. + usb_req->dma = DWC_DMA_ADDR_INVALID;
  56390. +
  56391. + return usb_req;
  56392. +}
  56393. +
  56394. +/**
  56395. + * This function frees a request object.
  56396. + *
  56397. + * @param ep The endpoint associated with the request
  56398. + * @param req The request being freed
  56399. + */
  56400. +static void dwc_otg_pcd_free_request(struct usb_ep *ep, struct usb_request *req)
  56401. +{
  56402. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p)\n", __func__, ep, req);
  56403. +
  56404. + if (0 == ep || 0 == req) {
  56405. + DWC_WARN("%s() %s\n", __func__,
  56406. + "Invalid ep or req argument!\n");
  56407. + return;
  56408. + }
  56409. +
  56410. + kfree(req);
  56411. +}
  56412. +
  56413. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  56414. +/**
  56415. + * This function allocates an I/O buffer to be used for a transfer
  56416. + * to/from the specified endpoint.
  56417. + *
  56418. + * @param usb_ep The endpoint to be used with with the request
  56419. + * @param bytes The desired number of bytes for the buffer
  56420. + * @param dma Pointer to the buffer's DMA address; must be valid
  56421. + * @param gfp_flags the GFP_* flags to use.
  56422. + * @return address of a new buffer or null is buffer could not be allocated.
  56423. + */
  56424. +static void *dwc_otg_pcd_alloc_buffer(struct usb_ep *usb_ep, unsigned bytes,
  56425. + dma_addr_t * dma, gfp_t gfp_flags)
  56426. +{
  56427. + void *buf;
  56428. + dwc_otg_pcd_t *pcd = 0;
  56429. +
  56430. + pcd = gadget_wrapper->pcd;
  56431. +
  56432. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%d,%p,%0x)\n", __func__, usb_ep, bytes,
  56433. + dma, gfp_flags);
  56434. +
  56435. + /* Check dword alignment */
  56436. + if ((bytes & 0x3UL) != 0) {
  56437. + DWC_WARN("%s() Buffer size is not a multiple of"
  56438. + "DWORD size (%d)", __func__, bytes);
  56439. + }
  56440. +
  56441. + buf = dma_alloc_coherent(NULL, bytes, dma, gfp_flags);
  56442. + WARN_ON(!buf);
  56443. +
  56444. + /* Check dword alignment */
  56445. + if (((int)buf & 0x3UL) != 0) {
  56446. + DWC_WARN("%s() Buffer is not DWORD aligned (%p)",
  56447. + __func__, buf);
  56448. + }
  56449. +
  56450. + return buf;
  56451. +}
  56452. +
  56453. +/**
  56454. + * This function frees an I/O buffer that was allocated by alloc_buffer.
  56455. + *
  56456. + * @param usb_ep the endpoint associated with the buffer
  56457. + * @param buf address of the buffer
  56458. + * @param dma The buffer's DMA address
  56459. + * @param bytes The number of bytes of the buffer
  56460. + */
  56461. +static void dwc_otg_pcd_free_buffer(struct usb_ep *usb_ep, void *buf,
  56462. + dma_addr_t dma, unsigned bytes)
  56463. +{
  56464. + dwc_otg_pcd_t *pcd = 0;
  56465. +
  56466. + pcd = gadget_wrapper->pcd;
  56467. +
  56468. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%0x,%d)\n", __func__, buf, dma, bytes);
  56469. +
  56470. + dma_free_coherent(NULL, bytes, buf, dma);
  56471. +}
  56472. +#endif
  56473. +
  56474. +/**
  56475. + * This function is used to submit an I/O Request to an EP.
  56476. + *
  56477. + * - When the request completes the request's completion callback
  56478. + * is called to return the request to the driver.
  56479. + * - An EP, except control EPs, may have multiple requests
  56480. + * pending.
  56481. + * - Once submitted the request cannot be examined or modified.
  56482. + * - Each request is turned into one or more packets.
  56483. + * - A BULK EP can queue any amount of data; the transfer is
  56484. + * packetized.
  56485. + * - Zero length Packets are specified with the request 'zero'
  56486. + * flag.
  56487. + */
  56488. +static int ep_queue(struct usb_ep *usb_ep, struct usb_request *usb_req,
  56489. + gfp_t gfp_flags)
  56490. +{
  56491. + dwc_otg_pcd_t *pcd;
  56492. + struct dwc_otg_pcd_ep *ep = NULL;
  56493. + int retval = 0, is_isoc_ep = 0;
  56494. + dma_addr_t dma_addr = DWC_DMA_ADDR_INVALID;
  56495. +
  56496. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p,%d)\n",
  56497. + __func__, usb_ep, usb_req, gfp_flags);
  56498. +
  56499. + if (!usb_req || !usb_req->complete || !usb_req->buf) {
  56500. + DWC_WARN("bad params\n");
  56501. + return -EINVAL;
  56502. + }
  56503. +
  56504. + if (!usb_ep) {
  56505. + DWC_WARN("bad ep\n");
  56506. + return -EINVAL;
  56507. + }
  56508. +
  56509. + pcd = gadget_wrapper->pcd;
  56510. + if (!gadget_wrapper->driver ||
  56511. + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
  56512. + DWC_DEBUGPL(DBG_PCDV, "gadget.speed=%d\n",
  56513. + gadget_wrapper->gadget.speed);
  56514. + DWC_WARN("bogus device state\n");
  56515. + return -ESHUTDOWN;
  56516. + }
  56517. +
  56518. + DWC_DEBUGPL(DBG_PCD, "%s queue req %p, len %d buf %p\n",
  56519. + usb_ep->name, usb_req, usb_req->length, usb_req->buf);
  56520. +
  56521. + usb_req->status = -EINPROGRESS;
  56522. + usb_req->actual = 0;
  56523. +
  56524. + ep = ep_from_handle(pcd, usb_ep);
  56525. + if (ep == NULL)
  56526. + is_isoc_ep = 0;
  56527. + else
  56528. + is_isoc_ep = (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) ? 1 : 0;
  56529. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  56530. + dma_addr = usb_req->dma;
  56531. +#else
  56532. + if (GET_CORE_IF(pcd)->dma_enable) {
  56533. + dwc_otg_device_t *otg_dev = gadget_wrapper->pcd->otg_dev;
  56534. + struct device *dev = NULL;
  56535. +
  56536. + if (otg_dev != NULL)
  56537. + dev = DWC_OTG_OS_GETDEV(otg_dev->os_dep);
  56538. +
  56539. + if (usb_req->length != 0 &&
  56540. + usb_req->dma == DWC_DMA_ADDR_INVALID) {
  56541. + dma_addr = dma_map_single(dev, usb_req->buf,
  56542. + usb_req->length,
  56543. + ep->dwc_ep.is_in ?
  56544. + DMA_TO_DEVICE:
  56545. + DMA_FROM_DEVICE);
  56546. + }
  56547. + }
  56548. +#endif
  56549. +
  56550. +#ifdef DWC_UTE_PER_IO
  56551. + if (is_isoc_ep == 1) {
  56552. + retval = dwc_otg_pcd_xiso_ep_queue(pcd, usb_ep, usb_req->buf, dma_addr,
  56553. + usb_req->length, usb_req->zero, usb_req,
  56554. + gfp_flags == GFP_ATOMIC ? 1 : 0, &usb_req->ext_req);
  56555. + if (retval)
  56556. + return -EINVAL;
  56557. +
  56558. + return 0;
  56559. + }
  56560. +#endif
  56561. + retval = dwc_otg_pcd_ep_queue(pcd, usb_ep, usb_req->buf, dma_addr,
  56562. + usb_req->length, usb_req->zero, usb_req,
  56563. + gfp_flags == GFP_ATOMIC ? 1 : 0);
  56564. + if (retval) {
  56565. + return -EINVAL;
  56566. + }
  56567. +
  56568. + return 0;
  56569. +}
  56570. +
  56571. +/**
  56572. + * This function cancels an I/O request from an EP.
  56573. + */
  56574. +static int ep_dequeue(struct usb_ep *usb_ep, struct usb_request *usb_req)
  56575. +{
  56576. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p)\n", __func__, usb_ep, usb_req);
  56577. +
  56578. + if (!usb_ep || !usb_req) {
  56579. + DWC_WARN("bad argument\n");
  56580. + return -EINVAL;
  56581. + }
  56582. + if (!gadget_wrapper->driver ||
  56583. + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
  56584. + DWC_WARN("bogus device state\n");
  56585. + return -ESHUTDOWN;
  56586. + }
  56587. + if (dwc_otg_pcd_ep_dequeue(gadget_wrapper->pcd, usb_ep, usb_req)) {
  56588. + return -EINVAL;
  56589. + }
  56590. +
  56591. + return 0;
  56592. +}
  56593. +
  56594. +/**
  56595. + * usb_ep_set_halt stalls an endpoint.
  56596. + *
  56597. + * usb_ep_clear_halt clears an endpoint halt and resets its data
  56598. + * toggle.
  56599. + *
  56600. + * Both of these functions are implemented with the same underlying
  56601. + * function. The behavior depends on the value argument.
  56602. + *
  56603. + * @param[in] usb_ep the Endpoint to halt or clear halt.
  56604. + * @param[in] value
  56605. + * - 0 means clear_halt.
  56606. + * - 1 means set_halt,
  56607. + * - 2 means clear stall lock flag.
  56608. + * - 3 means set stall lock flag.
  56609. + */
  56610. +static int ep_halt(struct usb_ep *usb_ep, int value)
  56611. +{
  56612. + int retval = 0;
  56613. +
  56614. + DWC_DEBUGPL(DBG_PCD, "HALT %s %d\n", usb_ep->name, value);
  56615. +
  56616. + if (!usb_ep) {
  56617. + DWC_WARN("bad ep\n");
  56618. + return -EINVAL;
  56619. + }
  56620. +
  56621. + retval = dwc_otg_pcd_ep_halt(gadget_wrapper->pcd, usb_ep, value);
  56622. + if (retval == -DWC_E_AGAIN) {
  56623. + return -EAGAIN;
  56624. + } else if (retval) {
  56625. + retval = -EINVAL;
  56626. + }
  56627. +
  56628. + return retval;
  56629. +}
  56630. +
  56631. +//#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30))
  56632. +#if 0
  56633. +/**
  56634. + * ep_wedge: sets the halt feature and ignores clear requests
  56635. + *
  56636. + * @usb_ep: the endpoint being wedged
  56637. + *
  56638. + * Use this to stall an endpoint and ignore CLEAR_FEATURE(HALT_ENDPOINT)
  56639. + * requests. If the gadget driver clears the halt status, it will
  56640. + * automatically unwedge the endpoint.
  56641. + *
  56642. + * Returns zero on success, else negative errno. *
  56643. + * Check usb_ep_set_wedge() at "usb_gadget.h" for details
  56644. + */
  56645. +static int ep_wedge(struct usb_ep *usb_ep)
  56646. +{
  56647. + int retval = 0;
  56648. +
  56649. + DWC_DEBUGPL(DBG_PCD, "WEDGE %s\n", usb_ep->name);
  56650. +
  56651. + if (!usb_ep) {
  56652. + DWC_WARN("bad ep\n");
  56653. + return -EINVAL;
  56654. + }
  56655. +
  56656. + retval = dwc_otg_pcd_ep_wedge(gadget_wrapper->pcd, usb_ep);
  56657. + if (retval == -DWC_E_AGAIN) {
  56658. + retval = -EAGAIN;
  56659. + } else if (retval) {
  56660. + retval = -EINVAL;
  56661. + }
  56662. +
  56663. + return retval;
  56664. +}
  56665. +#endif
  56666. +
  56667. +#ifdef DWC_EN_ISOC
  56668. +/**
  56669. + * This function is used to submit an ISOC Transfer Request to an EP.
  56670. + *
  56671. + * - Every time a sync period completes the request's completion callback
  56672. + * is called to provide data to the gadget driver.
  56673. + * - Once submitted the request cannot be modified.
  56674. + * - Each request is turned into periodic data packets untill ISO
  56675. + * Transfer is stopped..
  56676. + */
  56677. +static int iso_ep_start(struct usb_ep *usb_ep, struct usb_iso_request *req,
  56678. + gfp_t gfp_flags)
  56679. +{
  56680. + int retval = 0;
  56681. +
  56682. + if (!req || !req->process_buffer || !req->buf0 || !req->buf1) {
  56683. + DWC_WARN("bad params\n");
  56684. + return -EINVAL;
  56685. + }
  56686. +
  56687. + if (!usb_ep) {
  56688. + DWC_PRINTF("bad params\n");
  56689. + return -EINVAL;
  56690. + }
  56691. +
  56692. + req->status = -EINPROGRESS;
  56693. +
  56694. + retval =
  56695. + dwc_otg_pcd_iso_ep_start(gadget_wrapper->pcd, usb_ep, req->buf0,
  56696. + req->buf1, req->dma0, req->dma1,
  56697. + req->sync_frame, req->data_pattern_frame,
  56698. + req->data_per_frame,
  56699. + req->
  56700. + flags & USB_REQ_ISO_ASAP ? -1 :
  56701. + req->start_frame, req->buf_proc_intrvl,
  56702. + req, gfp_flags == GFP_ATOMIC ? 1 : 0);
  56703. +
  56704. + if (retval) {
  56705. + return -EINVAL;
  56706. + }
  56707. +
  56708. + return retval;
  56709. +}
  56710. +
  56711. +/**
  56712. + * This function stops ISO EP Periodic Data Transfer.
  56713. + */
  56714. +static int iso_ep_stop(struct usb_ep *usb_ep, struct usb_iso_request *req)
  56715. +{
  56716. + int retval = 0;
  56717. + if (!usb_ep) {
  56718. + DWC_WARN("bad ep\n");
  56719. + }
  56720. +
  56721. + if (!gadget_wrapper->driver ||
  56722. + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
  56723. + DWC_DEBUGPL(DBG_PCDV, "gadget.speed=%d\n",
  56724. + gadget_wrapper->gadget.speed);
  56725. + DWC_WARN("bogus device state\n");
  56726. + }
  56727. +
  56728. + dwc_otg_pcd_iso_ep_stop(gadget_wrapper->pcd, usb_ep, req);
  56729. + if (retval) {
  56730. + retval = -EINVAL;
  56731. + }
  56732. +
  56733. + return retval;
  56734. +}
  56735. +
  56736. +static struct usb_iso_request *alloc_iso_request(struct usb_ep *ep,
  56737. + int packets, gfp_t gfp_flags)
  56738. +{
  56739. + struct usb_iso_request *pReq = NULL;
  56740. + uint32_t req_size;
  56741. +
  56742. + req_size = sizeof(struct usb_iso_request);
  56743. + req_size +=
  56744. + (2 * packets * (sizeof(struct usb_gadget_iso_packet_descriptor)));
  56745. +
  56746. + pReq = kmalloc(req_size, gfp_flags);
  56747. + if (!pReq) {
  56748. + DWC_WARN("Can't allocate Iso Request\n");
  56749. + return 0;
  56750. + }
  56751. + pReq->iso_packet_desc0 = (void *)(pReq + 1);
  56752. +
  56753. + pReq->iso_packet_desc1 = pReq->iso_packet_desc0 + packets;
  56754. +
  56755. + return pReq;
  56756. +}
  56757. +
  56758. +static void free_iso_request(struct usb_ep *ep, struct usb_iso_request *req)
  56759. +{
  56760. + kfree(req);
  56761. +}
  56762. +
  56763. +static struct usb_isoc_ep_ops dwc_otg_pcd_ep_ops = {
  56764. + .ep_ops = {
  56765. + .enable = ep_enable,
  56766. + .disable = ep_disable,
  56767. +
  56768. + .alloc_request = dwc_otg_pcd_alloc_request,
  56769. + .free_request = dwc_otg_pcd_free_request,
  56770. +
  56771. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  56772. + .alloc_buffer = dwc_otg_pcd_alloc_buffer,
  56773. + .free_buffer = dwc_otg_pcd_free_buffer,
  56774. +#endif
  56775. +
  56776. + .queue = ep_queue,
  56777. + .dequeue = ep_dequeue,
  56778. +
  56779. + .set_halt = ep_halt,
  56780. + .fifo_status = 0,
  56781. + .fifo_flush = 0,
  56782. + },
  56783. + .iso_ep_start = iso_ep_start,
  56784. + .iso_ep_stop = iso_ep_stop,
  56785. + .alloc_iso_request = alloc_iso_request,
  56786. + .free_iso_request = free_iso_request,
  56787. +};
  56788. +
  56789. +#else
  56790. +
  56791. + int (*enable) (struct usb_ep *ep,
  56792. + const struct usb_endpoint_descriptor *desc);
  56793. + int (*disable) (struct usb_ep *ep);
  56794. +
  56795. + struct usb_request *(*alloc_request) (struct usb_ep *ep,
  56796. + gfp_t gfp_flags);
  56797. + void (*free_request) (struct usb_ep *ep, struct usb_request *req);
  56798. +
  56799. + int (*queue) (struct usb_ep *ep, struct usb_request *req,
  56800. + gfp_t gfp_flags);
  56801. + int (*dequeue) (struct usb_ep *ep, struct usb_request *req);
  56802. +
  56803. + int (*set_halt) (struct usb_ep *ep, int value);
  56804. + int (*set_wedge) (struct usb_ep *ep);
  56805. +
  56806. + int (*fifo_status) (struct usb_ep *ep);
  56807. + void (*fifo_flush) (struct usb_ep *ep);
  56808. +static struct usb_ep_ops dwc_otg_pcd_ep_ops = {
  56809. + .enable = ep_enable,
  56810. + .disable = ep_disable,
  56811. +
  56812. + .alloc_request = dwc_otg_pcd_alloc_request,
  56813. + .free_request = dwc_otg_pcd_free_request,
  56814. +
  56815. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  56816. + .alloc_buffer = dwc_otg_pcd_alloc_buffer,
  56817. + .free_buffer = dwc_otg_pcd_free_buffer,
  56818. +#else
  56819. + /* .set_wedge = ep_wedge, */
  56820. + .set_wedge = NULL, /* uses set_halt instead */
  56821. +#endif
  56822. +
  56823. + .queue = ep_queue,
  56824. + .dequeue = ep_dequeue,
  56825. +
  56826. + .set_halt = ep_halt,
  56827. + .fifo_status = 0,
  56828. + .fifo_flush = 0,
  56829. +
  56830. +};
  56831. +
  56832. +#endif /* _EN_ISOC_ */
  56833. +/* Gadget Operations */
  56834. +/**
  56835. + * The following gadget operations will be implemented in the DWC_otg
  56836. + * PCD. Functions in the API that are not described below are not
  56837. + * implemented.
  56838. + *
  56839. + * The Gadget API provides wrapper functions for each of the function
  56840. + * pointers defined in usb_gadget_ops. The Gadget Driver calls the
  56841. + * wrapper function, which then calls the underlying PCD function. The
  56842. + * following sections are named according to the wrapper functions
  56843. + * (except for ioctl, which doesn't have a wrapper function). Within
  56844. + * each section, the corresponding DWC_otg PCD function name is
  56845. + * specified.
  56846. + *
  56847. + */
  56848. +
  56849. +/**
  56850. + *Gets the USB Frame number of the last SOF.
  56851. + */
  56852. +static int get_frame_number(struct usb_gadget *gadget)
  56853. +{
  56854. + struct gadget_wrapper *d;
  56855. +
  56856. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, gadget);
  56857. +
  56858. + if (gadget == 0) {
  56859. + return -ENODEV;
  56860. + }
  56861. +
  56862. + d = container_of(gadget, struct gadget_wrapper, gadget);
  56863. + return dwc_otg_pcd_get_frame_number(d->pcd);
  56864. +}
  56865. +
  56866. +#ifdef CONFIG_USB_DWC_OTG_LPM
  56867. +static int test_lpm_enabled(struct usb_gadget *gadget)
  56868. +{
  56869. + struct gadget_wrapper *d;
  56870. +
  56871. + d = container_of(gadget, struct gadget_wrapper, gadget);
  56872. +
  56873. + return dwc_otg_pcd_is_lpm_enabled(d->pcd);
  56874. +}
  56875. +#endif
  56876. +
  56877. +/**
  56878. + * Initiates Session Request Protocol (SRP) to wakeup the host if no
  56879. + * session is in progress. If a session is already in progress, but
  56880. + * the device is suspended, remote wakeup signaling is started.
  56881. + *
  56882. + */
  56883. +static int wakeup(struct usb_gadget *gadget)
  56884. +{
  56885. + struct gadget_wrapper *d;
  56886. +
  56887. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, gadget);
  56888. +
  56889. + if (gadget == 0) {
  56890. + return -ENODEV;
  56891. + } else {
  56892. + d = container_of(gadget, struct gadget_wrapper, gadget);
  56893. + }
  56894. + dwc_otg_pcd_wakeup(d->pcd);
  56895. + return 0;
  56896. +}
  56897. +
  56898. +static const struct usb_gadget_ops dwc_otg_pcd_ops = {
  56899. + .get_frame = get_frame_number,
  56900. + .wakeup = wakeup,
  56901. +#ifdef CONFIG_USB_DWC_OTG_LPM
  56902. + .lpm_support = test_lpm_enabled,
  56903. +#endif
  56904. + // current versions must always be self-powered
  56905. +};
  56906. +
  56907. +static int _setup(dwc_otg_pcd_t * pcd, uint8_t * bytes)
  56908. +{
  56909. + int retval = -DWC_E_NOT_SUPPORTED;
  56910. + if (gadget_wrapper->driver && gadget_wrapper->driver->setup) {
  56911. + retval = gadget_wrapper->driver->setup(&gadget_wrapper->gadget,
  56912. + (struct usb_ctrlrequest
  56913. + *)bytes);
  56914. + }
  56915. +
  56916. + if (retval == -ENOTSUPP) {
  56917. + retval = -DWC_E_NOT_SUPPORTED;
  56918. + } else if (retval < 0) {
  56919. + retval = -DWC_E_INVALID;
  56920. + }
  56921. +
  56922. + return retval;
  56923. +}
  56924. +
  56925. +#ifdef DWC_EN_ISOC
  56926. +static int _isoc_complete(dwc_otg_pcd_t * pcd, void *ep_handle,
  56927. + void *req_handle, int proc_buf_num)
  56928. +{
  56929. + int i, packet_count;
  56930. + struct usb_gadget_iso_packet_descriptor *iso_packet = 0;
  56931. + struct usb_iso_request *iso_req = req_handle;
  56932. +
  56933. + if (proc_buf_num) {
  56934. + iso_packet = iso_req->iso_packet_desc1;
  56935. + } else {
  56936. + iso_packet = iso_req->iso_packet_desc0;
  56937. + }
  56938. + packet_count =
  56939. + dwc_otg_pcd_get_iso_packet_count(pcd, ep_handle, req_handle);
  56940. + for (i = 0; i < packet_count; ++i) {
  56941. + int status;
  56942. + int actual;
  56943. + int offset;
  56944. + dwc_otg_pcd_get_iso_packet_params(pcd, ep_handle, req_handle,
  56945. + i, &status, &actual, &offset);
  56946. + switch (status) {
  56947. + case -DWC_E_NO_DATA:
  56948. + status = -ENODATA;
  56949. + break;
  56950. + default:
  56951. + if (status) {
  56952. + DWC_PRINTF("unknown status in isoc packet\n");
  56953. + }
  56954. +
  56955. + }
  56956. + iso_packet[i].status = status;
  56957. + iso_packet[i].offset = offset;
  56958. + iso_packet[i].actual_length = actual;
  56959. + }
  56960. +
  56961. + iso_req->status = 0;
  56962. + iso_req->process_buffer(ep_handle, iso_req);
  56963. +
  56964. + return 0;
  56965. +}
  56966. +#endif /* DWC_EN_ISOC */
  56967. +
  56968. +#ifdef DWC_UTE_PER_IO
  56969. +/**
  56970. + * Copy the contents of the extended request to the Linux usb_request's
  56971. + * extended part and call the gadget's completion.
  56972. + *
  56973. + * @param pcd Pointer to the pcd structure
  56974. + * @param ep_handle Void pointer to the usb_ep structure
  56975. + * @param req_handle Void pointer to the usb_request structure
  56976. + * @param status Request status returned from the portable logic
  56977. + * @param ereq_port Void pointer to the extended request structure
  56978. + * created in the the portable part that contains the
  56979. + * results of the processed iso packets.
  56980. + */
  56981. +static int _xisoc_complete(dwc_otg_pcd_t * pcd, void *ep_handle,
  56982. + void *req_handle, int32_t status, void *ereq_port)
  56983. +{
  56984. + struct dwc_ute_iso_req_ext *ereqorg = NULL;
  56985. + struct dwc_iso_xreq_port *ereqport = NULL;
  56986. + struct dwc_ute_iso_packet_descriptor *desc_org = NULL;
  56987. + int i;
  56988. + struct usb_request *req;
  56989. + //struct dwc_ute_iso_packet_descriptor *
  56990. + //int status = 0;
  56991. +
  56992. + req = (struct usb_request *)req_handle;
  56993. + ereqorg = &req->ext_req;
  56994. + ereqport = (struct dwc_iso_xreq_port *)ereq_port;
  56995. + desc_org = ereqorg->per_io_frame_descs;
  56996. +
  56997. + if (req && req->complete) {
  56998. + /* Copy the request data from the portable logic to our request */
  56999. + for (i = 0; i < ereqport->pio_pkt_count; i++) {
  57000. + desc_org[i].actual_length =
  57001. + ereqport->per_io_frame_descs[i].actual_length;
  57002. + desc_org[i].status =
  57003. + ereqport->per_io_frame_descs[i].status;
  57004. + }
  57005. +
  57006. + switch (status) {
  57007. + case -DWC_E_SHUTDOWN:
  57008. + req->status = -ESHUTDOWN;
  57009. + break;
  57010. + case -DWC_E_RESTART:
  57011. + req->status = -ECONNRESET;
  57012. + break;
  57013. + case -DWC_E_INVALID:
  57014. + req->status = -EINVAL;
  57015. + break;
  57016. + case -DWC_E_TIMEOUT:
  57017. + req->status = -ETIMEDOUT;
  57018. + break;
  57019. + default:
  57020. + req->status = status;
  57021. + }
  57022. +
  57023. + /* And call the gadget's completion */
  57024. + req->complete(ep_handle, req);
  57025. + }
  57026. +
  57027. + return 0;
  57028. +}
  57029. +#endif /* DWC_UTE_PER_IO */
  57030. +
  57031. +static int _complete(dwc_otg_pcd_t * pcd, void *ep_handle,
  57032. + void *req_handle, int32_t status, uint32_t actual)
  57033. +{
  57034. + struct usb_request *req = (struct usb_request *)req_handle;
  57035. +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,27)
  57036. + struct dwc_otg_pcd_ep *ep = NULL;
  57037. +#endif
  57038. +
  57039. + if (req && req->complete) {
  57040. + switch (status) {
  57041. + case -DWC_E_SHUTDOWN:
  57042. + req->status = -ESHUTDOWN;
  57043. + break;
  57044. + case -DWC_E_RESTART:
  57045. + req->status = -ECONNRESET;
  57046. + break;
  57047. + case -DWC_E_INVALID:
  57048. + req->status = -EINVAL;
  57049. + break;
  57050. + case -DWC_E_TIMEOUT:
  57051. + req->status = -ETIMEDOUT;
  57052. + break;
  57053. + default:
  57054. + req->status = status;
  57055. +
  57056. + }
  57057. +
  57058. + req->actual = actual;
  57059. + DWC_SPINUNLOCK(pcd->lock);
  57060. + req->complete(ep_handle, req);
  57061. + DWC_SPINLOCK(pcd->lock);
  57062. + }
  57063. +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,27)
  57064. + ep = ep_from_handle(pcd, ep_handle);
  57065. + if (GET_CORE_IF(pcd)->dma_enable) {
  57066. + if (req->length != 0) {
  57067. + dwc_otg_device_t *otg_dev = gadget_wrapper->pcd->otg_dev;
  57068. + struct device *dev = NULL;
  57069. +
  57070. + if (otg_dev != NULL)
  57071. + dev = DWC_OTG_OS_GETDEV(otg_dev->os_dep);
  57072. +
  57073. + dma_unmap_single(dev, req->dma, req->length,
  57074. + ep->dwc_ep.is_in ?
  57075. + DMA_TO_DEVICE: DMA_FROM_DEVICE);
  57076. + }
  57077. + }
  57078. +#endif
  57079. +
  57080. + return 0;
  57081. +}
  57082. +
  57083. +static int _connect(dwc_otg_pcd_t * pcd, int speed)
  57084. +{
  57085. + gadget_wrapper->gadget.speed = speed;
  57086. + return 0;
  57087. +}
  57088. +
  57089. +static int _disconnect(dwc_otg_pcd_t * pcd)
  57090. +{
  57091. + if (gadget_wrapper->driver && gadget_wrapper->driver->disconnect) {
  57092. + gadget_wrapper->driver->disconnect(&gadget_wrapper->gadget);
  57093. + }
  57094. + return 0;
  57095. +}
  57096. +
  57097. +static int _resume(dwc_otg_pcd_t * pcd)
  57098. +{
  57099. + if (gadget_wrapper->driver && gadget_wrapper->driver->resume) {
  57100. + gadget_wrapper->driver->resume(&gadget_wrapper->gadget);
  57101. + }
  57102. +
  57103. + return 0;
  57104. +}
  57105. +
  57106. +static int _suspend(dwc_otg_pcd_t * pcd)
  57107. +{
  57108. + if (gadget_wrapper->driver && gadget_wrapper->driver->suspend) {
  57109. + gadget_wrapper->driver->suspend(&gadget_wrapper->gadget);
  57110. + }
  57111. + return 0;
  57112. +}
  57113. +
  57114. +/**
  57115. + * This function updates the otg values in the gadget structure.
  57116. + */
  57117. +static int _hnp_changed(dwc_otg_pcd_t * pcd)
  57118. +{
  57119. +
  57120. + if (!gadget_wrapper->gadget.is_otg)
  57121. + return 0;
  57122. +
  57123. + gadget_wrapper->gadget.b_hnp_enable = get_b_hnp_enable(pcd);
  57124. + gadget_wrapper->gadget.a_hnp_support = get_a_hnp_support(pcd);
  57125. + gadget_wrapper->gadget.a_alt_hnp_support = get_a_alt_hnp_support(pcd);
  57126. + return 0;
  57127. +}
  57128. +
  57129. +static int _reset(dwc_otg_pcd_t * pcd)
  57130. +{
  57131. + return 0;
  57132. +}
  57133. +
  57134. +#ifdef DWC_UTE_CFI
  57135. +static int _cfi_setup(dwc_otg_pcd_t * pcd, void *cfi_req)
  57136. +{
  57137. + int retval = -DWC_E_INVALID;
  57138. + if (gadget_wrapper->driver->cfi_feature_setup) {
  57139. + retval =
  57140. + gadget_wrapper->driver->
  57141. + cfi_feature_setup(&gadget_wrapper->gadget,
  57142. + (struct cfi_usb_ctrlrequest *)cfi_req);
  57143. + }
  57144. +
  57145. + return retval;
  57146. +}
  57147. +#endif
  57148. +
  57149. +static const struct dwc_otg_pcd_function_ops fops = {
  57150. + .complete = _complete,
  57151. +#ifdef DWC_EN_ISOC
  57152. + .isoc_complete = _isoc_complete,
  57153. +#endif
  57154. + .setup = _setup,
  57155. + .disconnect = _disconnect,
  57156. + .connect = _connect,
  57157. + .resume = _resume,
  57158. + .suspend = _suspend,
  57159. + .hnp_changed = _hnp_changed,
  57160. + .reset = _reset,
  57161. +#ifdef DWC_UTE_CFI
  57162. + .cfi_setup = _cfi_setup,
  57163. +#endif
  57164. +#ifdef DWC_UTE_PER_IO
  57165. + .xisoc_complete = _xisoc_complete,
  57166. +#endif
  57167. +};
  57168. +
  57169. +/**
  57170. + * This function is the top level PCD interrupt handler.
  57171. + */
  57172. +static irqreturn_t dwc_otg_pcd_irq(int irq, void *dev)
  57173. +{
  57174. + dwc_otg_pcd_t *pcd = dev;
  57175. + int32_t retval = IRQ_NONE;
  57176. +
  57177. + retval = dwc_otg_pcd_handle_intr(pcd);
  57178. + if (retval != 0) {
  57179. + S3C2410X_CLEAR_EINTPEND();
  57180. + }
  57181. + return IRQ_RETVAL(retval);
  57182. +}
  57183. +
  57184. +/**
  57185. + * This function initialized the usb_ep structures to there default
  57186. + * state.
  57187. + *
  57188. + * @param d Pointer on gadget_wrapper.
  57189. + */
  57190. +void gadget_add_eps(struct gadget_wrapper *d)
  57191. +{
  57192. + static const char *names[] = {
  57193. +
  57194. + "ep0",
  57195. + "ep1in",
  57196. + "ep2in",
  57197. + "ep3in",
  57198. + "ep4in",
  57199. + "ep5in",
  57200. + "ep6in",
  57201. + "ep7in",
  57202. + "ep8in",
  57203. + "ep9in",
  57204. + "ep10in",
  57205. + "ep11in",
  57206. + "ep12in",
  57207. + "ep13in",
  57208. + "ep14in",
  57209. + "ep15in",
  57210. + "ep1out",
  57211. + "ep2out",
  57212. + "ep3out",
  57213. + "ep4out",
  57214. + "ep5out",
  57215. + "ep6out",
  57216. + "ep7out",
  57217. + "ep8out",
  57218. + "ep9out",
  57219. + "ep10out",
  57220. + "ep11out",
  57221. + "ep12out",
  57222. + "ep13out",
  57223. + "ep14out",
  57224. + "ep15out"
  57225. + };
  57226. +
  57227. + int i;
  57228. + struct usb_ep *ep;
  57229. + int8_t dev_endpoints;
  57230. +
  57231. + DWC_DEBUGPL(DBG_PCDV, "%s\n", __func__);
  57232. +
  57233. + INIT_LIST_HEAD(&d->gadget.ep_list);
  57234. + d->gadget.ep0 = &d->ep0;
  57235. + d->gadget.speed = USB_SPEED_UNKNOWN;
  57236. +
  57237. + INIT_LIST_HEAD(&d->gadget.ep0->ep_list);
  57238. +
  57239. + /**
  57240. + * Initialize the EP0 structure.
  57241. + */
  57242. + ep = &d->ep0;
  57243. +
  57244. + /* Init the usb_ep structure. */
  57245. + ep->name = names[0];
  57246. + ep->ops = (struct usb_ep_ops *)&dwc_otg_pcd_ep_ops;
  57247. +
  57248. + /**
  57249. + * @todo NGS: What should the max packet size be set to
  57250. + * here? Before EP type is set?
  57251. + */
  57252. + ep->maxpacket = MAX_PACKET_SIZE;
  57253. + dwc_otg_pcd_ep_enable(d->pcd, NULL, ep);
  57254. +
  57255. + list_add_tail(&ep->ep_list, &d->gadget.ep_list);
  57256. +
  57257. + /**
  57258. + * Initialize the EP structures.
  57259. + */
  57260. + dev_endpoints = d->pcd->core_if->dev_if->num_in_eps;
  57261. +
  57262. + for (i = 0; i < dev_endpoints; i++) {
  57263. + ep = &d->in_ep[i];
  57264. +
  57265. + /* Init the usb_ep structure. */
  57266. + ep->name = names[d->pcd->in_ep[i].dwc_ep.num];
  57267. + ep->ops = (struct usb_ep_ops *)&dwc_otg_pcd_ep_ops;
  57268. +
  57269. + /**
  57270. + * @todo NGS: What should the max packet size be set to
  57271. + * here? Before EP type is set?
  57272. + */
  57273. + ep->maxpacket = MAX_PACKET_SIZE;
  57274. + list_add_tail(&ep->ep_list, &d->gadget.ep_list);
  57275. + }
  57276. +
  57277. + dev_endpoints = d->pcd->core_if->dev_if->num_out_eps;
  57278. +
  57279. + for (i = 0; i < dev_endpoints; i++) {
  57280. + ep = &d->out_ep[i];
  57281. +
  57282. + /* Init the usb_ep structure. */
  57283. + ep->name = names[15 + d->pcd->out_ep[i].dwc_ep.num];
  57284. + ep->ops = (struct usb_ep_ops *)&dwc_otg_pcd_ep_ops;
  57285. +
  57286. + /**
  57287. + * @todo NGS: What should the max packet size be set to
  57288. + * here? Before EP type is set?
  57289. + */
  57290. + ep->maxpacket = MAX_PACKET_SIZE;
  57291. +
  57292. + list_add_tail(&ep->ep_list, &d->gadget.ep_list);
  57293. + }
  57294. +
  57295. + /* remove ep0 from the list. There is a ep0 pointer. */
  57296. + list_del_init(&d->ep0.ep_list);
  57297. +
  57298. + d->ep0.maxpacket = MAX_EP0_SIZE;
  57299. +}
  57300. +
  57301. +/**
  57302. + * This function releases the Gadget device.
  57303. + * required by device_unregister().
  57304. + *
  57305. + * @todo Should this do something? Should it free the PCD?
  57306. + */
  57307. +static void dwc_otg_pcd_gadget_release(struct device *dev)
  57308. +{
  57309. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, dev);
  57310. +}
  57311. +
  57312. +static struct gadget_wrapper *alloc_wrapper(dwc_bus_dev_t *_dev)
  57313. +{
  57314. + static char pcd_name[] = "dwc_otg_pcd";
  57315. + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
  57316. + struct gadget_wrapper *d;
  57317. + int retval;
  57318. +
  57319. + d = DWC_ALLOC(sizeof(*d));
  57320. + if (d == NULL) {
  57321. + return NULL;
  57322. + }
  57323. +
  57324. + memset(d, 0, sizeof(*d));
  57325. +
  57326. + d->gadget.name = pcd_name;
  57327. + d->pcd = otg_dev->pcd;
  57328. +
  57329. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
  57330. + strcpy(d->gadget.dev.bus_id, "gadget");
  57331. +#else
  57332. + dev_set_name(&d->gadget.dev, "%s", "gadget");
  57333. +#endif
  57334. +
  57335. + d->gadget.dev.parent = &_dev->dev;
  57336. + d->gadget.dev.release = dwc_otg_pcd_gadget_release;
  57337. + d->gadget.ops = &dwc_otg_pcd_ops;
  57338. + d->gadget.max_speed = dwc_otg_pcd_is_dualspeed(otg_dev->pcd) ? USB_SPEED_HIGH:USB_SPEED_FULL;
  57339. + d->gadget.is_otg = dwc_otg_pcd_is_otg(otg_dev->pcd);
  57340. +
  57341. + d->driver = 0;
  57342. + /* Register the gadget device */
  57343. + retval = device_register(&d->gadget.dev);
  57344. + if (retval != 0) {
  57345. + DWC_ERROR("device_register failed\n");
  57346. + DWC_FREE(d);
  57347. + return NULL;
  57348. + }
  57349. +
  57350. + return d;
  57351. +}
  57352. +
  57353. +static void free_wrapper(struct gadget_wrapper *d)
  57354. +{
  57355. + if (d->driver) {
  57356. + /* should have been done already by driver model core */
  57357. + DWC_WARN("driver '%s' is still registered\n",
  57358. + d->driver->driver.name);
  57359. +#ifdef CONFIG_USB_GADGET
  57360. + usb_gadget_unregister_driver(d->driver);
  57361. +#endif
  57362. + }
  57363. +
  57364. + device_unregister(&d->gadget.dev);
  57365. + DWC_FREE(d);
  57366. +}
  57367. +
  57368. +/**
  57369. + * This function initialized the PCD portion of the driver.
  57370. + *
  57371. + */
  57372. +int pcd_init(dwc_bus_dev_t *_dev)
  57373. +{
  57374. + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
  57375. + int retval = 0;
  57376. +
  57377. + DWC_DEBUGPL(DBG_PCDV, "%s(%p) otg_dev=%p\n", __func__, _dev, otg_dev);
  57378. +
  57379. + otg_dev->pcd = dwc_otg_pcd_init(otg_dev);
  57380. +
  57381. + if (!otg_dev->pcd) {
  57382. + DWC_ERROR("dwc_otg_pcd_init failed\n");
  57383. + return -ENOMEM;
  57384. + }
  57385. +
  57386. + otg_dev->pcd->otg_dev = otg_dev;
  57387. + gadget_wrapper = alloc_wrapper(_dev);
  57388. +
  57389. + /*
  57390. + * Initialize EP structures
  57391. + */
  57392. + gadget_add_eps(gadget_wrapper);
  57393. + /*
  57394. + * Setup interupt handler
  57395. + */
  57396. + DWC_DEBUGPL(DBG_ANY, "registering handler for irq%d\n",
  57397. + otg_dev->os_dep.irq_num);
  57398. + retval = request_irq(otg_dev->os_dep.irq_num, dwc_otg_pcd_irq,
  57399. + IRQF_SHARED, gadget_wrapper->gadget.name,
  57400. + otg_dev->pcd);
  57401. + if (retval != 0) {
  57402. + DWC_ERROR("request of irq%d failed\n", otg_dev->os_dep.irq_num);
  57403. + free_wrapper(gadget_wrapper);
  57404. + return -EBUSY;
  57405. + }
  57406. +
  57407. + dwc_otg_pcd_start(gadget_wrapper->pcd, &fops);
  57408. +
  57409. + return retval;
  57410. +}
  57411. +
  57412. +/**
  57413. + * Cleanup the PCD.
  57414. + */
  57415. +void pcd_remove(dwc_bus_dev_t *_dev)
  57416. +{
  57417. + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
  57418. + dwc_otg_pcd_t *pcd = otg_dev->pcd;
  57419. +
  57420. + DWC_DEBUGPL(DBG_PCDV, "%s(%p) otg_dev %p\n", __func__, _dev, otg_dev);
  57421. +
  57422. + /*
  57423. + * Free the IRQ
  57424. + */
  57425. + free_irq(otg_dev->os_dep.irq_num, pcd);
  57426. + dwc_otg_pcd_remove(otg_dev->pcd);
  57427. + free_wrapper(gadget_wrapper);
  57428. + otg_dev->pcd = 0;
  57429. +}
  57430. +
  57431. +#endif /* DWC_HOST_ONLY */
  57432. --- /dev/null
  57433. +++ b/drivers/usb/host/dwc_otg/dwc_otg_regs.h
  57434. @@ -0,0 +1,2550 @@
  57435. +/* ==========================================================================
  57436. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_regs.h $
  57437. + * $Revision: #98 $
  57438. + * $Date: 2012/08/10 $
  57439. + * $Change: 2047372 $
  57440. + *
  57441. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  57442. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  57443. + * otherwise expressly agreed to in writing between Synopsys and you.
  57444. + *
  57445. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  57446. + * any End User Software License Agreement or Agreement for Licensed Product
  57447. + * with Synopsys or any supplement thereto. You are permitted to use and
  57448. + * redistribute this Software in source and binary forms, with or without
  57449. + * modification, provided that redistributions of source code must retain this
  57450. + * notice. You may not view, use, disclose, copy or distribute this file or
  57451. + * any information contained herein except pursuant to this license grant from
  57452. + * Synopsys. If you do not agree with this notice, including the disclaimer
  57453. + * below, then you are not authorized to use the Software.
  57454. + *
  57455. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  57456. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  57457. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  57458. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  57459. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  57460. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  57461. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  57462. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  57463. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  57464. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  57465. + * DAMAGE.
  57466. + * ========================================================================== */
  57467. +
  57468. +#ifndef __DWC_OTG_REGS_H__
  57469. +#define __DWC_OTG_REGS_H__
  57470. +
  57471. +#include "dwc_otg_core_if.h"
  57472. +
  57473. +/**
  57474. + * @file
  57475. + *
  57476. + * This file contains the data structures for accessing the DWC_otg core registers.
  57477. + *
  57478. + * The application interfaces with the HS OTG core by reading from and
  57479. + * writing to the Control and Status Register (CSR) space through the
  57480. + * AHB Slave interface. These registers are 32 bits wide, and the
  57481. + * addresses are 32-bit-block aligned.
  57482. + * CSRs are classified as follows:
  57483. + * - Core Global Registers
  57484. + * - Device Mode Registers
  57485. + * - Device Global Registers
  57486. + * - Device Endpoint Specific Registers
  57487. + * - Host Mode Registers
  57488. + * - Host Global Registers
  57489. + * - Host Port CSRs
  57490. + * - Host Channel Specific Registers
  57491. + *
  57492. + * Only the Core Global registers can be accessed in both Device and
  57493. + * Host modes. When the HS OTG core is operating in one mode, either
  57494. + * Device or Host, the application must not access registers from the
  57495. + * other mode. When the core switches from one mode to another, the
  57496. + * registers in the new mode of operation must be reprogrammed as they
  57497. + * would be after a power-on reset.
  57498. + */
  57499. +
  57500. +/****************************************************************************/
  57501. +/** DWC_otg Core registers .
  57502. + * The dwc_otg_core_global_regs structure defines the size
  57503. + * and relative field offsets for the Core Global registers.
  57504. + */
  57505. +typedef struct dwc_otg_core_global_regs {
  57506. + /** OTG Control and Status Register. <i>Offset: 000h</i> */
  57507. + volatile uint32_t gotgctl;
  57508. + /** OTG Interrupt Register. <i>Offset: 004h</i> */
  57509. + volatile uint32_t gotgint;
  57510. + /**Core AHB Configuration Register. <i>Offset: 008h</i> */
  57511. + volatile uint32_t gahbcfg;
  57512. +
  57513. +#define DWC_GLBINTRMASK 0x0001
  57514. +#define DWC_DMAENABLE 0x0020
  57515. +#define DWC_NPTXEMPTYLVL_EMPTY 0x0080
  57516. +#define DWC_NPTXEMPTYLVL_HALFEMPTY 0x0000
  57517. +#define DWC_PTXEMPTYLVL_EMPTY 0x0100
  57518. +#define DWC_PTXEMPTYLVL_HALFEMPTY 0x0000
  57519. +
  57520. + /**Core USB Configuration Register. <i>Offset: 00Ch</i> */
  57521. + volatile uint32_t gusbcfg;
  57522. + /**Core Reset Register. <i>Offset: 010h</i> */
  57523. + volatile uint32_t grstctl;
  57524. + /**Core Interrupt Register. <i>Offset: 014h</i> */
  57525. + volatile uint32_t gintsts;
  57526. + /**Core Interrupt Mask Register. <i>Offset: 018h</i> */
  57527. + volatile uint32_t gintmsk;
  57528. + /**Receive Status Queue Read Register (Read Only). <i>Offset: 01Ch</i> */
  57529. + volatile uint32_t grxstsr;
  57530. + /**Receive Status Queue Read & POP Register (Read Only). <i>Offset: 020h</i>*/
  57531. + volatile uint32_t grxstsp;
  57532. + /**Receive FIFO Size Register. <i>Offset: 024h</i> */
  57533. + volatile uint32_t grxfsiz;
  57534. + /**Non Periodic Transmit FIFO Size Register. <i>Offset: 028h</i> */
  57535. + volatile uint32_t gnptxfsiz;
  57536. + /**Non Periodic Transmit FIFO/Queue Status Register (Read
  57537. + * Only). <i>Offset: 02Ch</i> */
  57538. + volatile uint32_t gnptxsts;
  57539. + /**I2C Access Register. <i>Offset: 030h</i> */
  57540. + volatile uint32_t gi2cctl;
  57541. + /**PHY Vendor Control Register. <i>Offset: 034h</i> */
  57542. + volatile uint32_t gpvndctl;
  57543. + /**General Purpose Input/Output Register. <i>Offset: 038h</i> */
  57544. + volatile uint32_t ggpio;
  57545. + /**User ID Register. <i>Offset: 03Ch</i> */
  57546. + volatile uint32_t guid;
  57547. + /**Synopsys ID Register (Read Only). <i>Offset: 040h</i> */
  57548. + volatile uint32_t gsnpsid;
  57549. + /**User HW Config1 Register (Read Only). <i>Offset: 044h</i> */
  57550. + volatile uint32_t ghwcfg1;
  57551. + /**User HW Config2 Register (Read Only). <i>Offset: 048h</i> */
  57552. + volatile uint32_t ghwcfg2;
  57553. +#define DWC_SLAVE_ONLY_ARCH 0
  57554. +#define DWC_EXT_DMA_ARCH 1
  57555. +#define DWC_INT_DMA_ARCH 2
  57556. +
  57557. +#define DWC_MODE_HNP_SRP_CAPABLE 0
  57558. +#define DWC_MODE_SRP_ONLY_CAPABLE 1
  57559. +#define DWC_MODE_NO_HNP_SRP_CAPABLE 2
  57560. +#define DWC_MODE_SRP_CAPABLE_DEVICE 3
  57561. +#define DWC_MODE_NO_SRP_CAPABLE_DEVICE 4
  57562. +#define DWC_MODE_SRP_CAPABLE_HOST 5
  57563. +#define DWC_MODE_NO_SRP_CAPABLE_HOST 6
  57564. +
  57565. + /**User HW Config3 Register (Read Only). <i>Offset: 04Ch</i> */
  57566. + volatile uint32_t ghwcfg3;
  57567. + /**User HW Config4 Register (Read Only). <i>Offset: 050h</i>*/
  57568. + volatile uint32_t ghwcfg4;
  57569. + /** Core LPM Configuration register <i>Offset: 054h</i>*/
  57570. + volatile uint32_t glpmcfg;
  57571. + /** Global PowerDn Register <i>Offset: 058h</i> */
  57572. + volatile uint32_t gpwrdn;
  57573. + /** Global DFIFO SW Config Register <i>Offset: 05Ch</i> */
  57574. + volatile uint32_t gdfifocfg;
  57575. + /** ADP Control Register <i>Offset: 060h</i> */
  57576. + volatile uint32_t adpctl;
  57577. + /** Reserved <i>Offset: 064h-0FFh</i> */
  57578. + volatile uint32_t reserved39[39];
  57579. + /** Host Periodic Transmit FIFO Size Register. <i>Offset: 100h</i> */
  57580. + volatile uint32_t hptxfsiz;
  57581. + /** Device Periodic Transmit FIFO#n Register if dedicated fifos are disabled,
  57582. + otherwise Device Transmit FIFO#n Register.
  57583. + * <i>Offset: 104h + (FIFO_Number-1)*04h, 1 <= FIFO Number <= 15 (1<=n<=15).</i> */
  57584. + volatile uint32_t dtxfsiz[15];
  57585. +} dwc_otg_core_global_regs_t;
  57586. +
  57587. +/**
  57588. + * This union represents the bit fields of the Core OTG Control
  57589. + * and Status Register (GOTGCTL). Set the bits using the bit
  57590. + * fields then write the <i>d32</i> value to the register.
  57591. + */
  57592. +typedef union gotgctl_data {
  57593. + /** raw register data */
  57594. + uint32_t d32;
  57595. + /** register bits */
  57596. + struct {
  57597. + unsigned sesreqscs:1;
  57598. + unsigned sesreq:1;
  57599. + unsigned vbvalidoven:1;
  57600. + unsigned vbvalidovval:1;
  57601. + unsigned avalidoven:1;
  57602. + unsigned avalidovval:1;
  57603. + unsigned bvalidoven:1;
  57604. + unsigned bvalidovval:1;
  57605. + unsigned hstnegscs:1;
  57606. + unsigned hnpreq:1;
  57607. + unsigned hstsethnpen:1;
  57608. + unsigned devhnpen:1;
  57609. + unsigned reserved12_15:4;
  57610. + unsigned conidsts:1;
  57611. + unsigned dbnctime:1;
  57612. + unsigned asesvld:1;
  57613. + unsigned bsesvld:1;
  57614. + unsigned otgver:1;
  57615. + unsigned reserved1:1;
  57616. + unsigned multvalidbc:5;
  57617. + unsigned chirpen:1;
  57618. + unsigned reserved28_31:4;
  57619. + } b;
  57620. +} gotgctl_data_t;
  57621. +
  57622. +/**
  57623. + * This union represents the bit fields of the Core OTG Interrupt Register
  57624. + * (GOTGINT). Set/clear the bits using the bit fields then write the <i>d32</i>
  57625. + * value to the register.
  57626. + */
  57627. +typedef union gotgint_data {
  57628. + /** raw register data */
  57629. + uint32_t d32;
  57630. + /** register bits */
  57631. + struct {
  57632. + /** Current Mode */
  57633. + unsigned reserved0_1:2;
  57634. +
  57635. + /** Session End Detected */
  57636. + unsigned sesenddet:1;
  57637. +
  57638. + unsigned reserved3_7:5;
  57639. +
  57640. + /** Session Request Success Status Change */
  57641. + unsigned sesreqsucstschng:1;
  57642. + /** Host Negotiation Success Status Change */
  57643. + unsigned hstnegsucstschng:1;
  57644. +
  57645. + unsigned reserved10_16:7;
  57646. +
  57647. + /** Host Negotiation Detected */
  57648. + unsigned hstnegdet:1;
  57649. + /** A-Device Timeout Change */
  57650. + unsigned adevtoutchng:1;
  57651. + /** Debounce Done */
  57652. + unsigned debdone:1;
  57653. + /** Multi-Valued input changed */
  57654. + unsigned mvic:1;
  57655. +
  57656. + unsigned reserved31_21:11;
  57657. +
  57658. + } b;
  57659. +} gotgint_data_t;
  57660. +
  57661. +/**
  57662. + * This union represents the bit fields of the Core AHB Configuration
  57663. + * Register (GAHBCFG). Set/clear the bits using the bit fields then
  57664. + * write the <i>d32</i> value to the register.
  57665. + */
  57666. +typedef union gahbcfg_data {
  57667. + /** raw register data */
  57668. + uint32_t d32;
  57669. + /** register bits */
  57670. + struct {
  57671. + unsigned glblintrmsk:1;
  57672. +#define DWC_GAHBCFG_GLBINT_ENABLE 1
  57673. +
  57674. + unsigned hburstlen:4;
  57675. +#define DWC_GAHBCFG_INT_DMA_BURST_SINGLE 0
  57676. +#define DWC_GAHBCFG_INT_DMA_BURST_INCR 1
  57677. +#define DWC_GAHBCFG_INT_DMA_BURST_INCR4 3
  57678. +#define DWC_GAHBCFG_INT_DMA_BURST_INCR8 5
  57679. +#define DWC_GAHBCFG_INT_DMA_BURST_INCR16 7
  57680. +
  57681. + unsigned dmaenable:1;
  57682. +#define DWC_GAHBCFG_DMAENABLE 1
  57683. + unsigned reserved:1;
  57684. + unsigned nptxfemplvl_txfemplvl:1;
  57685. + unsigned ptxfemplvl:1;
  57686. +#define DWC_GAHBCFG_TXFEMPTYLVL_EMPTY 1
  57687. +#define DWC_GAHBCFG_TXFEMPTYLVL_HALFEMPTY 0
  57688. + unsigned reserved9_20:12;
  57689. + unsigned remmemsupp:1;
  57690. + unsigned notialldmawrit:1;
  57691. + unsigned ahbsingle:1;
  57692. + unsigned reserved24_31:8;
  57693. + } b;
  57694. +} gahbcfg_data_t;
  57695. +
  57696. +/**
  57697. + * This union represents the bit fields of the Core USB Configuration
  57698. + * Register (GUSBCFG). Set the bits using the bit fields then write
  57699. + * the <i>d32</i> value to the register.
  57700. + */
  57701. +typedef union gusbcfg_data {
  57702. + /** raw register data */
  57703. + uint32_t d32;
  57704. + /** register bits */
  57705. + struct {
  57706. + unsigned toutcal:3;
  57707. + unsigned phyif:1;
  57708. + unsigned ulpi_utmi_sel:1;
  57709. + unsigned fsintf:1;
  57710. + unsigned physel:1;
  57711. + unsigned ddrsel:1;
  57712. + unsigned srpcap:1;
  57713. + unsigned hnpcap:1;
  57714. + unsigned usbtrdtim:4;
  57715. + unsigned reserved1:1;
  57716. + unsigned phylpwrclksel:1;
  57717. + unsigned otgutmifssel:1;
  57718. + unsigned ulpi_fsls:1;
  57719. + unsigned ulpi_auto_res:1;
  57720. + unsigned ulpi_clk_sus_m:1;
  57721. + unsigned ulpi_ext_vbus_drv:1;
  57722. + unsigned ulpi_int_vbus_indicator:1;
  57723. + unsigned term_sel_dl_pulse:1;
  57724. + unsigned indicator_complement:1;
  57725. + unsigned indicator_pass_through:1;
  57726. + unsigned ulpi_int_prot_dis:1;
  57727. + unsigned ic_usb_cap:1;
  57728. + unsigned ic_traffic_pull_remove:1;
  57729. + unsigned tx_end_delay:1;
  57730. + unsigned force_host_mode:1;
  57731. + unsigned force_dev_mode:1;
  57732. + unsigned reserved31:1;
  57733. + } b;
  57734. +} gusbcfg_data_t;
  57735. +
  57736. +/**
  57737. + * This union represents the bit fields of the Core Reset Register
  57738. + * (GRSTCTL). Set/clear the bits using the bit fields then write the
  57739. + * <i>d32</i> value to the register.
  57740. + */
  57741. +typedef union grstctl_data {
  57742. + /** raw register data */
  57743. + uint32_t d32;
  57744. + /** register bits */
  57745. + struct {
  57746. + /** Core Soft Reset (CSftRst) (Device and Host)
  57747. + *
  57748. + * The application can flush the control logic in the
  57749. + * entire core using this bit. This bit resets the
  57750. + * pipelines in the AHB Clock domain as well as the
  57751. + * PHY Clock domain.
  57752. + *
  57753. + * The state machines are reset to an IDLE state, the
  57754. + * control bits in the CSRs are cleared, all the
  57755. + * transmit FIFOs and the receive FIFO are flushed.
  57756. + *
  57757. + * The status mask bits that control the generation of
  57758. + * the interrupt, are cleared, to clear the
  57759. + * interrupt. The interrupt status bits are not
  57760. + * cleared, so the application can get the status of
  57761. + * any events that occurred in the core after it has
  57762. + * set this bit.
  57763. + *
  57764. + * Any transactions on the AHB are terminated as soon
  57765. + * as possible following the protocol. Any
  57766. + * transactions on the USB are terminated immediately.
  57767. + *
  57768. + * The configuration settings in the CSRs are
  57769. + * unchanged, so the software doesn't have to
  57770. + * reprogram these registers (Device
  57771. + * Configuration/Host Configuration/Core System
  57772. + * Configuration/Core PHY Configuration).
  57773. + *
  57774. + * The application can write to this bit, any time it
  57775. + * wants to reset the core. This is a self clearing
  57776. + * bit and the core clears this bit after all the
  57777. + * necessary logic is reset in the core, which may
  57778. + * take several clocks, depending on the current state
  57779. + * of the core.
  57780. + */
  57781. + unsigned csftrst:1;
  57782. + /** Hclk Soft Reset
  57783. + *
  57784. + * The application uses this bit to reset the control logic in
  57785. + * the AHB clock domain. Only AHB clock domain pipelines are
  57786. + * reset.
  57787. + */
  57788. + unsigned hsftrst:1;
  57789. + /** Host Frame Counter Reset (Host Only)<br>
  57790. + *
  57791. + * The application can reset the (micro)frame number
  57792. + * counter inside the core, using this bit. When the
  57793. + * (micro)frame counter is reset, the subsequent SOF
  57794. + * sent out by the core, will have a (micro)frame
  57795. + * number of 0.
  57796. + */
  57797. + unsigned hstfrm:1;
  57798. + /** In Token Sequence Learning Queue Flush
  57799. + * (INTknQFlsh) (Device Only)
  57800. + */
  57801. + unsigned intknqflsh:1;
  57802. + /** RxFIFO Flush (RxFFlsh) (Device and Host)
  57803. + *
  57804. + * The application can flush the entire Receive FIFO
  57805. + * using this bit. The application must first
  57806. + * ensure that the core is not in the middle of a
  57807. + * transaction. The application should write into
  57808. + * this bit, only after making sure that neither the
  57809. + * DMA engine is reading from the RxFIFO nor the MAC
  57810. + * is writing the data in to the FIFO. The
  57811. + * application should wait until the bit is cleared
  57812. + * before performing any other operations. This bit
  57813. + * will takes 8 clocks (slowest of PHY or AHB clock)
  57814. + * to clear.
  57815. + */
  57816. + unsigned rxfflsh:1;
  57817. + /** TxFIFO Flush (TxFFlsh) (Device and Host).
  57818. + *
  57819. + * This bit is used to selectively flush a single or
  57820. + * all transmit FIFOs. The application must first
  57821. + * ensure that the core is not in the middle of a
  57822. + * transaction. The application should write into
  57823. + * this bit, only after making sure that neither the
  57824. + * DMA engine is writing into the TxFIFO nor the MAC
  57825. + * is reading the data out of the FIFO. The
  57826. + * application should wait until the core clears this
  57827. + * bit, before performing any operations. This bit
  57828. + * will takes 8 clocks (slowest of PHY or AHB clock)
  57829. + * to clear.
  57830. + */
  57831. + unsigned txfflsh:1;
  57832. +
  57833. + /** TxFIFO Number (TxFNum) (Device and Host).
  57834. + *
  57835. + * This is the FIFO number which needs to be flushed,
  57836. + * using the TxFIFO Flush bit. This field should not
  57837. + * be changed until the TxFIFO Flush bit is cleared by
  57838. + * the core.
  57839. + * - 0x0 : Non Periodic TxFIFO Flush
  57840. + * - 0x1 : Periodic TxFIFO #1 Flush in device mode
  57841. + * or Periodic TxFIFO in host mode
  57842. + * - 0x2 : Periodic TxFIFO #2 Flush in device mode.
  57843. + * - ...
  57844. + * - 0xF : Periodic TxFIFO #15 Flush in device mode
  57845. + * - 0x10: Flush all the Transmit NonPeriodic and
  57846. + * Transmit Periodic FIFOs in the core
  57847. + */
  57848. + unsigned txfnum:5;
  57849. + /** Reserved */
  57850. + unsigned reserved11_29:19;
  57851. + /** DMA Request Signal. Indicated DMA request is in
  57852. + * probress. Used for debug purpose. */
  57853. + unsigned dmareq:1;
  57854. + /** AHB Master Idle. Indicates the AHB Master State
  57855. + * Machine is in IDLE condition. */
  57856. + unsigned ahbidle:1;
  57857. + } b;
  57858. +} grstctl_t;
  57859. +
  57860. +/**
  57861. + * This union represents the bit fields of the Core Interrupt Mask
  57862. + * Register (GINTMSK). Set/clear the bits using the bit fields then
  57863. + * write the <i>d32</i> value to the register.
  57864. + */
  57865. +typedef union gintmsk_data {
  57866. + /** raw register data */
  57867. + uint32_t d32;
  57868. + /** register bits */
  57869. + struct {
  57870. + unsigned reserved0:1;
  57871. + unsigned modemismatch:1;
  57872. + unsigned otgintr:1;
  57873. + unsigned sofintr:1;
  57874. + unsigned rxstsqlvl:1;
  57875. + unsigned nptxfempty:1;
  57876. + unsigned ginnakeff:1;
  57877. + unsigned goutnakeff:1;
  57878. + unsigned ulpickint:1;
  57879. + unsigned i2cintr:1;
  57880. + unsigned erlysuspend:1;
  57881. + unsigned usbsuspend:1;
  57882. + unsigned usbreset:1;
  57883. + unsigned enumdone:1;
  57884. + unsigned isooutdrop:1;
  57885. + unsigned eopframe:1;
  57886. + unsigned restoredone:1;
  57887. + unsigned epmismatch:1;
  57888. + unsigned inepintr:1;
  57889. + unsigned outepintr:1;
  57890. + unsigned incomplisoin:1;
  57891. + unsigned incomplisoout:1;
  57892. + unsigned fetsusp:1;
  57893. + unsigned resetdet:1;
  57894. + unsigned portintr:1;
  57895. + unsigned hcintr:1;
  57896. + unsigned ptxfempty:1;
  57897. + unsigned lpmtranrcvd:1;
  57898. + unsigned conidstschng:1;
  57899. + unsigned disconnect:1;
  57900. + unsigned sessreqintr:1;
  57901. + unsigned wkupintr:1;
  57902. + } b;
  57903. +} gintmsk_data_t;
  57904. +/**
  57905. + * This union represents the bit fields of the Core Interrupt Register
  57906. + * (GINTSTS). Set/clear the bits using the bit fields then write the
  57907. + * <i>d32</i> value to the register.
  57908. + */
  57909. +typedef union gintsts_data {
  57910. + /** raw register data */
  57911. + uint32_t d32;
  57912. +#define DWC_SOF_INTR_MASK 0x0008
  57913. + /** register bits */
  57914. + struct {
  57915. +#define DWC_HOST_MODE 1
  57916. + unsigned curmode:1;
  57917. + unsigned modemismatch:1;
  57918. + unsigned otgintr:1;
  57919. + unsigned sofintr:1;
  57920. + unsigned rxstsqlvl:1;
  57921. + unsigned nptxfempty:1;
  57922. + unsigned ginnakeff:1;
  57923. + unsigned goutnakeff:1;
  57924. + unsigned ulpickint:1;
  57925. + unsigned i2cintr:1;
  57926. + unsigned erlysuspend:1;
  57927. + unsigned usbsuspend:1;
  57928. + unsigned usbreset:1;
  57929. + unsigned enumdone:1;
  57930. + unsigned isooutdrop:1;
  57931. + unsigned eopframe:1;
  57932. + unsigned restoredone:1;
  57933. + unsigned epmismatch:1;
  57934. + unsigned inepint:1;
  57935. + unsigned outepintr:1;
  57936. + unsigned incomplisoin:1;
  57937. + unsigned incomplisoout:1;
  57938. + unsigned fetsusp:1;
  57939. + unsigned resetdet:1;
  57940. + unsigned portintr:1;
  57941. + unsigned hcintr:1;
  57942. + unsigned ptxfempty:1;
  57943. + unsigned lpmtranrcvd:1;
  57944. + unsigned conidstschng:1;
  57945. + unsigned disconnect:1;
  57946. + unsigned sessreqintr:1;
  57947. + unsigned wkupintr:1;
  57948. + } b;
  57949. +} gintsts_data_t;
  57950. +
  57951. +/**
  57952. + * This union represents the bit fields in the Device Receive Status Read and
  57953. + * Pop Registers (GRXSTSR, GRXSTSP) Read the register into the <i>d32</i>
  57954. + * element then read out the bits using the <i>b</i>it elements.
  57955. + */
  57956. +typedef union device_grxsts_data {
  57957. + /** raw register data */
  57958. + uint32_t d32;
  57959. + /** register bits */
  57960. + struct {
  57961. + unsigned epnum:4;
  57962. + unsigned bcnt:11;
  57963. + unsigned dpid:2;
  57964. +
  57965. +#define DWC_STS_DATA_UPDT 0x2 // OUT Data Packet
  57966. +#define DWC_STS_XFER_COMP 0x3 // OUT Data Transfer Complete
  57967. +
  57968. +#define DWC_DSTS_GOUT_NAK 0x1 // Global OUT NAK
  57969. +#define DWC_DSTS_SETUP_COMP 0x4 // Setup Phase Complete
  57970. +#define DWC_DSTS_SETUP_UPDT 0x6 // SETUP Packet
  57971. + unsigned pktsts:4;
  57972. + unsigned fn:4;
  57973. + unsigned reserved25_31:7;
  57974. + } b;
  57975. +} device_grxsts_data_t;
  57976. +
  57977. +/**
  57978. + * This union represents the bit fields in the Host Receive Status Read and
  57979. + * Pop Registers (GRXSTSR, GRXSTSP) Read the register into the <i>d32</i>
  57980. + * element then read out the bits using the <i>b</i>it elements.
  57981. + */
  57982. +typedef union host_grxsts_data {
  57983. + /** raw register data */
  57984. + uint32_t d32;
  57985. + /** register bits */
  57986. + struct {
  57987. + unsigned chnum:4;
  57988. + unsigned bcnt:11;
  57989. + unsigned dpid:2;
  57990. +
  57991. + unsigned pktsts:4;
  57992. +#define DWC_GRXSTS_PKTSTS_IN 0x2
  57993. +#define DWC_GRXSTS_PKTSTS_IN_XFER_COMP 0x3
  57994. +#define DWC_GRXSTS_PKTSTS_DATA_TOGGLE_ERR 0x5
  57995. +#define DWC_GRXSTS_PKTSTS_CH_HALTED 0x7
  57996. +
  57997. + unsigned reserved21_31:11;
  57998. + } b;
  57999. +} host_grxsts_data_t;
  58000. +
  58001. +/**
  58002. + * This union represents the bit fields in the FIFO Size Registers (HPTXFSIZ,
  58003. + * GNPTXFSIZ, DPTXFSIZn, DIEPTXFn). Read the register into the <i>d32</i> element
  58004. + * then read out the bits using the <i>b</i>it elements.
  58005. + */
  58006. +typedef union fifosize_data {
  58007. + /** raw register data */
  58008. + uint32_t d32;
  58009. + /** register bits */
  58010. + struct {
  58011. + unsigned startaddr:16;
  58012. + unsigned depth:16;
  58013. + } b;
  58014. +} fifosize_data_t;
  58015. +
  58016. +/**
  58017. + * This union represents the bit fields in the Non-Periodic Transmit
  58018. + * FIFO/Queue Status Register (GNPTXSTS). Read the register into the
  58019. + * <i>d32</i> element then read out the bits using the <i>b</i>it
  58020. + * elements.
  58021. + */
  58022. +typedef union gnptxsts_data {
  58023. + /** raw register data */
  58024. + uint32_t d32;
  58025. + /** register bits */
  58026. + struct {
  58027. + unsigned nptxfspcavail:16;
  58028. + unsigned nptxqspcavail:8;
  58029. + /** Top of the Non-Periodic Transmit Request Queue
  58030. + * - bit 24 - Terminate (Last entry for the selected
  58031. + * channel/EP)
  58032. + * - bits 26:25 - Token Type
  58033. + * - 2'b00 - IN/OUT
  58034. + * - 2'b01 - Zero Length OUT
  58035. + * - 2'b10 - PING/Complete Split
  58036. + * - 2'b11 - Channel Halt
  58037. + * - bits 30:27 - Channel/EP Number
  58038. + */
  58039. + unsigned nptxqtop_terminate:1;
  58040. + unsigned nptxqtop_token:2;
  58041. + unsigned nptxqtop_chnep:4;
  58042. + unsigned reserved:1;
  58043. + } b;
  58044. +} gnptxsts_data_t;
  58045. +
  58046. +/**
  58047. + * This union represents the bit fields in the Transmit
  58048. + * FIFO Status Register (DTXFSTS). Read the register into the
  58049. + * <i>d32</i> element then read out the bits using the <i>b</i>it
  58050. + * elements.
  58051. + */
  58052. +typedef union dtxfsts_data {
  58053. + /** raw register data */
  58054. + uint32_t d32;
  58055. + /** register bits */
  58056. + struct {
  58057. + unsigned txfspcavail:16;
  58058. + unsigned reserved:16;
  58059. + } b;
  58060. +} dtxfsts_data_t;
  58061. +
  58062. +/**
  58063. + * This union represents the bit fields in the I2C Control Register
  58064. + * (I2CCTL). Read the register into the <i>d32</i> element then read out the
  58065. + * bits using the <i>b</i>it elements.
  58066. + */
  58067. +typedef union gi2cctl_data {
  58068. + /** raw register data */
  58069. + uint32_t d32;
  58070. + /** register bits */
  58071. + struct {
  58072. + unsigned rwdata:8;
  58073. + unsigned regaddr:8;
  58074. + unsigned addr:7;
  58075. + unsigned i2cen:1;
  58076. + unsigned ack:1;
  58077. + unsigned i2csuspctl:1;
  58078. + unsigned i2cdevaddr:2;
  58079. + unsigned i2cdatse0:1;
  58080. + unsigned reserved:1;
  58081. + unsigned rw:1;
  58082. + unsigned bsydne:1;
  58083. + } b;
  58084. +} gi2cctl_data_t;
  58085. +
  58086. +/**
  58087. + * This union represents the bit fields in the PHY Vendor Control Register
  58088. + * (GPVNDCTL). Read the register into the <i>d32</i> element then read out the
  58089. + * bits using the <i>b</i>it elements.
  58090. + */
  58091. +typedef union gpvndctl_data {
  58092. + /** raw register data */
  58093. + uint32_t d32;
  58094. + /** register bits */
  58095. + struct {
  58096. + unsigned regdata:8;
  58097. + unsigned vctrl:8;
  58098. + unsigned regaddr16_21:6;
  58099. + unsigned regwr:1;
  58100. + unsigned reserved23_24:2;
  58101. + unsigned newregreq:1;
  58102. + unsigned vstsbsy:1;
  58103. + unsigned vstsdone:1;
  58104. + unsigned reserved28_30:3;
  58105. + unsigned disulpidrvr:1;
  58106. + } b;
  58107. +} gpvndctl_data_t;
  58108. +
  58109. +/**
  58110. + * This union represents the bit fields in the General Purpose
  58111. + * Input/Output Register (GGPIO).
  58112. + * Read the register into the <i>d32</i> element then read out the
  58113. + * bits using the <i>b</i>it elements.
  58114. + */
  58115. +typedef union ggpio_data {
  58116. + /** raw register data */
  58117. + uint32_t d32;
  58118. + /** register bits */
  58119. + struct {
  58120. + unsigned gpi:16;
  58121. + unsigned gpo:16;
  58122. + } b;
  58123. +} ggpio_data_t;
  58124. +
  58125. +/**
  58126. + * This union represents the bit fields in the User ID Register
  58127. + * (GUID). Read the register into the <i>d32</i> element then read out the
  58128. + * bits using the <i>b</i>it elements.
  58129. + */
  58130. +typedef union guid_data {
  58131. + /** raw register data */
  58132. + uint32_t d32;
  58133. + /** register bits */
  58134. + struct {
  58135. + unsigned rwdata:32;
  58136. + } b;
  58137. +} guid_data_t;
  58138. +
  58139. +/**
  58140. + * This union represents the bit fields in the Synopsys ID Register
  58141. + * (GSNPSID). Read the register into the <i>d32</i> element then read out the
  58142. + * bits using the <i>b</i>it elements.
  58143. + */
  58144. +typedef union gsnpsid_data {
  58145. + /** raw register data */
  58146. + uint32_t d32;
  58147. + /** register bits */
  58148. + struct {
  58149. + unsigned rwdata:32;
  58150. + } b;
  58151. +} gsnpsid_data_t;
  58152. +
  58153. +/**
  58154. + * This union represents the bit fields in the User HW Config1
  58155. + * Register. Read the register into the <i>d32</i> element then read
  58156. + * out the bits using the <i>b</i>it elements.
  58157. + */
  58158. +typedef union hwcfg1_data {
  58159. + /** raw register data */
  58160. + uint32_t d32;
  58161. + /** register bits */
  58162. + struct {
  58163. + unsigned ep_dir0:2;
  58164. + unsigned ep_dir1:2;
  58165. + unsigned ep_dir2:2;
  58166. + unsigned ep_dir3:2;
  58167. + unsigned ep_dir4:2;
  58168. + unsigned ep_dir5:2;
  58169. + unsigned ep_dir6:2;
  58170. + unsigned ep_dir7:2;
  58171. + unsigned ep_dir8:2;
  58172. + unsigned ep_dir9:2;
  58173. + unsigned ep_dir10:2;
  58174. + unsigned ep_dir11:2;
  58175. + unsigned ep_dir12:2;
  58176. + unsigned ep_dir13:2;
  58177. + unsigned ep_dir14:2;
  58178. + unsigned ep_dir15:2;
  58179. + } b;
  58180. +} hwcfg1_data_t;
  58181. +
  58182. +/**
  58183. + * This union represents the bit fields in the User HW Config2
  58184. + * Register. Read the register into the <i>d32</i> element then read
  58185. + * out the bits using the <i>b</i>it elements.
  58186. + */
  58187. +typedef union hwcfg2_data {
  58188. + /** raw register data */
  58189. + uint32_t d32;
  58190. + /** register bits */
  58191. + struct {
  58192. + /* GHWCFG2 */
  58193. + unsigned op_mode:3;
  58194. +#define DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG 0
  58195. +#define DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG 1
  58196. +#define DWC_HWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE_OTG 2
  58197. +#define DWC_HWCFG2_OP_MODE_SRP_CAPABLE_DEVICE 3
  58198. +#define DWC_HWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE 4
  58199. +#define DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST 5
  58200. +#define DWC_HWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST 6
  58201. +
  58202. + unsigned architecture:2;
  58203. + unsigned point2point:1;
  58204. + unsigned hs_phy_type:2;
  58205. +#define DWC_HWCFG2_HS_PHY_TYPE_NOT_SUPPORTED 0
  58206. +#define DWC_HWCFG2_HS_PHY_TYPE_UTMI 1
  58207. +#define DWC_HWCFG2_HS_PHY_TYPE_ULPI 2
  58208. +#define DWC_HWCFG2_HS_PHY_TYPE_UTMI_ULPI 3
  58209. +
  58210. + unsigned fs_phy_type:2;
  58211. + unsigned num_dev_ep:4;
  58212. + unsigned num_host_chan:4;
  58213. + unsigned perio_ep_supported:1;
  58214. + unsigned dynamic_fifo:1;
  58215. + unsigned multi_proc_int:1;
  58216. + unsigned reserved21:1;
  58217. + unsigned nonperio_tx_q_depth:2;
  58218. + unsigned host_perio_tx_q_depth:2;
  58219. + unsigned dev_token_q_depth:5;
  58220. + unsigned otg_enable_ic_usb:1;
  58221. + } b;
  58222. +} hwcfg2_data_t;
  58223. +
  58224. +/**
  58225. + * This union represents the bit fields in the User HW Config3
  58226. + * Register. Read the register into the <i>d32</i> element then read
  58227. + * out the bits using the <i>b</i>it elements.
  58228. + */
  58229. +typedef union hwcfg3_data {
  58230. + /** raw register data */
  58231. + uint32_t d32;
  58232. + /** register bits */
  58233. + struct {
  58234. + /* GHWCFG3 */
  58235. + unsigned xfer_size_cntr_width:4;
  58236. + unsigned packet_size_cntr_width:3;
  58237. + unsigned otg_func:1;
  58238. + unsigned i2c:1;
  58239. + unsigned vendor_ctrl_if:1;
  58240. + unsigned optional_features:1;
  58241. + unsigned synch_reset_type:1;
  58242. + unsigned adp_supp:1;
  58243. + unsigned otg_enable_hsic:1;
  58244. + unsigned bc_support:1;
  58245. + unsigned otg_lpm_en:1;
  58246. + unsigned dfifo_depth:16;
  58247. + } b;
  58248. +} hwcfg3_data_t;
  58249. +
  58250. +/**
  58251. + * This union represents the bit fields in the User HW Config4
  58252. + * Register. Read the register into the <i>d32</i> element then read
  58253. + * out the bits using the <i>b</i>it elements.
  58254. + */
  58255. +typedef union hwcfg4_data {
  58256. + /** raw register data */
  58257. + uint32_t d32;
  58258. + /** register bits */
  58259. + struct {
  58260. + unsigned num_dev_perio_in_ep:4;
  58261. + unsigned power_optimiz:1;
  58262. + unsigned min_ahb_freq:1;
  58263. + unsigned hiber:1;
  58264. + unsigned xhiber:1;
  58265. + unsigned reserved:6;
  58266. + unsigned utmi_phy_data_width:2;
  58267. + unsigned num_dev_mode_ctrl_ep:4;
  58268. + unsigned iddig_filt_en:1;
  58269. + unsigned vbus_valid_filt_en:1;
  58270. + unsigned a_valid_filt_en:1;
  58271. + unsigned b_valid_filt_en:1;
  58272. + unsigned session_end_filt_en:1;
  58273. + unsigned ded_fifo_en:1;
  58274. + unsigned num_in_eps:4;
  58275. + unsigned desc_dma:1;
  58276. + unsigned desc_dma_dyn:1;
  58277. + } b;
  58278. +} hwcfg4_data_t;
  58279. +
  58280. +/**
  58281. + * This union represents the bit fields of the Core LPM Configuration
  58282. + * Register (GLPMCFG). Set the bits using bit fields then write
  58283. + * the <i>d32</i> value to the register.
  58284. + */
  58285. +typedef union glpmctl_data {
  58286. + /** raw register data */
  58287. + uint32_t d32;
  58288. + /** register bits */
  58289. + struct {
  58290. + /** LPM-Capable (LPMCap) (Device and Host)
  58291. + * The application uses this bit to control
  58292. + * the DWC_otg core LPM capabilities.
  58293. + */
  58294. + unsigned lpm_cap_en:1;
  58295. + /** LPM response programmed by application (AppL1Res) (Device)
  58296. + * Handshake response to LPM token pre-programmed
  58297. + * by device application software.
  58298. + */
  58299. + unsigned appl_resp:1;
  58300. + /** Host Initiated Resume Duration (HIRD) (Device and Host)
  58301. + * In Host mode this field indicates the value of HIRD
  58302. + * to be sent in an LPM transaction.
  58303. + * In Device mode this field is updated with the
  58304. + * Received LPM Token HIRD bmAttribute
  58305. + * when an ACK/NYET/STALL response is sent
  58306. + * to an LPM transaction.
  58307. + */
  58308. + unsigned hird:4;
  58309. + /** RemoteWakeEnable (bRemoteWake) (Device and Host)
  58310. + * In Host mode this bit indicates the value of remote
  58311. + * wake up to be sent in wIndex field of LPM transaction.
  58312. + * In Device mode this field is updated with the
  58313. + * Received LPM Token bRemoteWake bmAttribute
  58314. + * when an ACK/NYET/STALL response is sent
  58315. + * to an LPM transaction.
  58316. + */
  58317. + unsigned rem_wkup_en:1;
  58318. + /** Enable utmi_sleep_n (EnblSlpM) (Device and Host)
  58319. + * The application uses this bit to control
  58320. + * the utmi_sleep_n assertion to the PHY when in L1 state.
  58321. + */
  58322. + unsigned en_utmi_sleep:1;
  58323. + /** HIRD Threshold (HIRD_Thres) (Device and Host)
  58324. + */
  58325. + unsigned hird_thres:5;
  58326. + /** LPM Response (CoreL1Res) (Device and Host)
  58327. + * In Host mode this bit contains handsake response to
  58328. + * LPM transaction.
  58329. + * In Device mode the response of the core to
  58330. + * LPM transaction received is reflected in these two bits.
  58331. + - 0x0 : ERROR (No handshake response)
  58332. + - 0x1 : STALL
  58333. + - 0x2 : NYET
  58334. + - 0x3 : ACK
  58335. + */
  58336. + unsigned lpm_resp:2;
  58337. + /** Port Sleep Status (SlpSts) (Device and Host)
  58338. + * This bit is set as long as a Sleep condition
  58339. + * is present on the USB bus.
  58340. + */
  58341. + unsigned prt_sleep_sts:1;
  58342. + /** Sleep State Resume OK (L1ResumeOK) (Device and Host)
  58343. + * Indicates that the application or host
  58344. + * can start resume from Sleep state.
  58345. + */
  58346. + unsigned sleep_state_resumeok:1;
  58347. + /** LPM channel Index (LPM_Chnl_Indx) (Host)
  58348. + * The channel number on which the LPM transaction
  58349. + * has to be applied while sending
  58350. + * an LPM transaction to the local device.
  58351. + */
  58352. + unsigned lpm_chan_index:4;
  58353. + /** LPM Retry Count (LPM_Retry_Cnt) (Host)
  58354. + * Number host retries that would be performed
  58355. + * if the device response was not valid response.
  58356. + */
  58357. + unsigned retry_count:3;
  58358. + /** Send LPM Transaction (SndLPM) (Host)
  58359. + * When set by application software,
  58360. + * an LPM transaction containing two tokens
  58361. + * is sent.
  58362. + */
  58363. + unsigned send_lpm:1;
  58364. + /** LPM Retry status (LPM_RetryCnt_Sts) (Host)
  58365. + * Number of LPM Host Retries still remaining
  58366. + * to be transmitted for the current LPM sequence
  58367. + */
  58368. + unsigned retry_count_sts:3;
  58369. + unsigned reserved28_29:2;
  58370. + /** In host mode once this bit is set, the host
  58371. + * configures to drive the HSIC Idle state on the bus.
  58372. + * It then waits for the device to initiate the Connect sequence.
  58373. + * In device mode once this bit is set, the device waits for
  58374. + * the HSIC Idle line state on the bus. Upon receving the Idle
  58375. + * line state, it initiates the HSIC Connect sequence.
  58376. + */
  58377. + unsigned hsic_connect:1;
  58378. + /** This bit overrides and functionally inverts
  58379. + * the if_select_hsic input port signal.
  58380. + */
  58381. + unsigned inv_sel_hsic:1;
  58382. + } b;
  58383. +} glpmcfg_data_t;
  58384. +
  58385. +/**
  58386. + * This union represents the bit fields of the Core ADP Timer, Control and
  58387. + * Status Register (ADPTIMCTLSTS). Set the bits using bit fields then write
  58388. + * the <i>d32</i> value to the register.
  58389. + */
  58390. +typedef union adpctl_data {
  58391. + /** raw register data */
  58392. + uint32_t d32;
  58393. + /** register bits */
  58394. + struct {
  58395. + /** Probe Discharge (PRB_DSCHG)
  58396. + * These bits set the times for TADP_DSCHG.
  58397. + * These bits are defined as follows:
  58398. + * 2'b00 - 4 msec
  58399. + * 2'b01 - 8 msec
  58400. + * 2'b10 - 16 msec
  58401. + * 2'b11 - 32 msec
  58402. + */
  58403. + unsigned prb_dschg:2;
  58404. + /** Probe Delta (PRB_DELTA)
  58405. + * These bits set the resolution for RTIM value.
  58406. + * The bits are defined in units of 32 kHz clock cycles as follows:
  58407. + * 2'b00 - 1 cycles
  58408. + * 2'b01 - 2 cycles
  58409. + * 2'b10 - 3 cycles
  58410. + * 2'b11 - 4 cycles
  58411. + * For example if this value is chosen to 2'b01, it means that RTIM
  58412. + * increments for every 3(three) 32Khz clock cycles.
  58413. + */
  58414. + unsigned prb_delta:2;
  58415. + /** Probe Period (PRB_PER)
  58416. + * These bits sets the TADP_PRD as shown in Figure 4 as follows:
  58417. + * 2'b00 - 0.625 to 0.925 sec (typical 0.775 sec)
  58418. + * 2'b01 - 1.25 to 1.85 sec (typical 1.55 sec)
  58419. + * 2'b10 - 1.9 to 2.6 sec (typical 2.275 sec)
  58420. + * 2'b11 - Reserved
  58421. + */
  58422. + unsigned prb_per:2;
  58423. + /** These bits capture the latest time it took for VBUS to ramp from
  58424. + * VADP_SINK to VADP_PRB.
  58425. + * 0x000 - 1 cycles
  58426. + * 0x001 - 2 cycles
  58427. + * 0x002 - 3 cycles
  58428. + * etc
  58429. + * 0x7FF - 2048 cycles
  58430. + * A time of 1024 cycles at 32 kHz corresponds to a time of 32 msec.
  58431. + */
  58432. + unsigned rtim:11;
  58433. + /** Enable Probe (EnaPrb)
  58434. + * When programmed to 1'b1, the core performs a probe operation.
  58435. + * This bit is valid only if OTG_Ver = 1'b1.
  58436. + */
  58437. + unsigned enaprb:1;
  58438. + /** Enable Sense (EnaSns)
  58439. + * When programmed to 1'b1, the core performs a Sense operation.
  58440. + * This bit is valid only if OTG_Ver = 1'b1.
  58441. + */
  58442. + unsigned enasns:1;
  58443. + /** ADP Reset (ADPRes)
  58444. + * When set, ADP controller is reset.
  58445. + * This bit is valid only if OTG_Ver = 1'b1.
  58446. + */
  58447. + unsigned adpres:1;
  58448. + /** ADP Enable (ADPEn)
  58449. + * When set, the core performs either ADP probing or sensing
  58450. + * based on EnaPrb or EnaSns.
  58451. + * This bit is valid only if OTG_Ver = 1'b1.
  58452. + */
  58453. + unsigned adpen:1;
  58454. + /** ADP Probe Interrupt (ADP_PRB_INT)
  58455. + * When this bit is set, it means that the VBUS
  58456. + * voltage is greater than VADP_PRB or VADP_PRB is reached.
  58457. + * This bit is valid only if OTG_Ver = 1'b1.
  58458. + */
  58459. + unsigned adp_prb_int:1;
  58460. + /**
  58461. + * ADP Sense Interrupt (ADP_SNS_INT)
  58462. + * When this bit is set, it means that the VBUS voltage is greater than
  58463. + * VADP_SNS value or VADP_SNS is reached.
  58464. + * This bit is valid only if OTG_Ver = 1'b1.
  58465. + */
  58466. + unsigned adp_sns_int:1;
  58467. + /** ADP Tomeout Interrupt (ADP_TMOUT_INT)
  58468. + * This bit is relevant only for an ADP probe.
  58469. + * When this bit is set, it means that the ramp time has
  58470. + * completed ie ADPCTL.RTIM has reached its terminal value
  58471. + * of 0x7FF. This is a debug feature that allows software
  58472. + * to read the ramp time after each cycle.
  58473. + * This bit is valid only if OTG_Ver = 1'b1.
  58474. + */
  58475. + unsigned adp_tmout_int:1;
  58476. + /** ADP Probe Interrupt Mask (ADP_PRB_INT_MSK)
  58477. + * When this bit is set, it unmasks the interrupt due to ADP_PRB_INT.
  58478. + * This bit is valid only if OTG_Ver = 1'b1.
  58479. + */
  58480. + unsigned adp_prb_int_msk:1;
  58481. + /** ADP Sense Interrupt Mask (ADP_SNS_INT_MSK)
  58482. + * When this bit is set, it unmasks the interrupt due to ADP_SNS_INT.
  58483. + * This bit is valid only if OTG_Ver = 1'b1.
  58484. + */
  58485. + unsigned adp_sns_int_msk:1;
  58486. + /** ADP Timoeout Interrupt Mask (ADP_TMOUT_MSK)
  58487. + * When this bit is set, it unmasks the interrupt due to ADP_TMOUT_INT.
  58488. + * This bit is valid only if OTG_Ver = 1'b1.
  58489. + */
  58490. + unsigned adp_tmout_int_msk:1;
  58491. + /** Access Request
  58492. + * 2'b00 - Read/Write Valid (updated by the core)
  58493. + * 2'b01 - Read
  58494. + * 2'b00 - Write
  58495. + * 2'b00 - Reserved
  58496. + */
  58497. + unsigned ar:2;
  58498. + /** Reserved */
  58499. + unsigned reserved29_31:3;
  58500. + } b;
  58501. +} adpctl_data_t;
  58502. +
  58503. +////////////////////////////////////////////
  58504. +// Device Registers
  58505. +/**
  58506. + * Device Global Registers. <i>Offsets 800h-BFFh</i>
  58507. + *
  58508. + * The following structures define the size and relative field offsets
  58509. + * for the Device Mode Registers.
  58510. + *
  58511. + * <i>These registers are visible only in Device mode and must not be
  58512. + * accessed in Host mode, as the results are unknown.</i>
  58513. + */
  58514. +typedef struct dwc_otg_dev_global_regs {
  58515. + /** Device Configuration Register. <i>Offset 800h</i> */
  58516. + volatile uint32_t dcfg;
  58517. + /** Device Control Register. <i>Offset: 804h</i> */
  58518. + volatile uint32_t dctl;
  58519. + /** Device Status Register (Read Only). <i>Offset: 808h</i> */
  58520. + volatile uint32_t dsts;
  58521. + /** Reserved. <i>Offset: 80Ch</i> */
  58522. + uint32_t unused;
  58523. + /** Device IN Endpoint Common Interrupt Mask
  58524. + * Register. <i>Offset: 810h</i> */
  58525. + volatile uint32_t diepmsk;
  58526. + /** Device OUT Endpoint Common Interrupt Mask
  58527. + * Register. <i>Offset: 814h</i> */
  58528. + volatile uint32_t doepmsk;
  58529. + /** Device All Endpoints Interrupt Register. <i>Offset: 818h</i> */
  58530. + volatile uint32_t daint;
  58531. + /** Device All Endpoints Interrupt Mask Register. <i>Offset:
  58532. + * 81Ch</i> */
  58533. + volatile uint32_t daintmsk;
  58534. + /** Device IN Token Queue Read Register-1 (Read Only).
  58535. + * <i>Offset: 820h</i> */
  58536. + volatile uint32_t dtknqr1;
  58537. + /** Device IN Token Queue Read Register-2 (Read Only).
  58538. + * <i>Offset: 824h</i> */
  58539. + volatile uint32_t dtknqr2;
  58540. + /** Device VBUS discharge Register. <i>Offset: 828h</i> */
  58541. + volatile uint32_t dvbusdis;
  58542. + /** Device VBUS Pulse Register. <i>Offset: 82Ch</i> */
  58543. + volatile uint32_t dvbuspulse;
  58544. + /** Device IN Token Queue Read Register-3 (Read Only). /
  58545. + * Device Thresholding control register (Read/Write)
  58546. + * <i>Offset: 830h</i> */
  58547. + volatile uint32_t dtknqr3_dthrctl;
  58548. + /** Device IN Token Queue Read Register-4 (Read Only). /
  58549. + * Device IN EPs empty Inr. Mask Register (Read/Write)
  58550. + * <i>Offset: 834h</i> */
  58551. + volatile uint32_t dtknqr4_fifoemptymsk;
  58552. + /** Device Each Endpoint Interrupt Register (Read Only). /
  58553. + * <i>Offset: 838h</i> */
  58554. + volatile uint32_t deachint;
  58555. + /** Device Each Endpoint Interrupt mask Register (Read/Write). /
  58556. + * <i>Offset: 83Ch</i> */
  58557. + volatile uint32_t deachintmsk;
  58558. + /** Device Each In Endpoint Interrupt mask Register (Read/Write). /
  58559. + * <i>Offset: 840h</i> */
  58560. + volatile uint32_t diepeachintmsk[MAX_EPS_CHANNELS];
  58561. + /** Device Each Out Endpoint Interrupt mask Register (Read/Write). /
  58562. + * <i>Offset: 880h</i> */
  58563. + volatile uint32_t doepeachintmsk[MAX_EPS_CHANNELS];
  58564. +} dwc_otg_device_global_regs_t;
  58565. +
  58566. +/**
  58567. + * This union represents the bit fields in the Device Configuration
  58568. + * Register. Read the register into the <i>d32</i> member then
  58569. + * set/clear the bits using the <i>b</i>it elements. Write the
  58570. + * <i>d32</i> member to the dcfg register.
  58571. + */
  58572. +typedef union dcfg_data {
  58573. + /** raw register data */
  58574. + uint32_t d32;
  58575. + /** register bits */
  58576. + struct {
  58577. + /** Device Speed */
  58578. + unsigned devspd:2;
  58579. + /** Non Zero Length Status OUT Handshake */
  58580. + unsigned nzstsouthshk:1;
  58581. +#define DWC_DCFG_SEND_STALL 1
  58582. +
  58583. + unsigned ena32khzs:1;
  58584. + /** Device Addresses */
  58585. + unsigned devaddr:7;
  58586. + /** Periodic Frame Interval */
  58587. + unsigned perfrint:2;
  58588. +#define DWC_DCFG_FRAME_INTERVAL_80 0
  58589. +#define DWC_DCFG_FRAME_INTERVAL_85 1
  58590. +#define DWC_DCFG_FRAME_INTERVAL_90 2
  58591. +#define DWC_DCFG_FRAME_INTERVAL_95 3
  58592. +
  58593. + /** Enable Device OUT NAK for bulk in DDMA mode */
  58594. + unsigned endevoutnak:1;
  58595. +
  58596. + unsigned reserved14_17:4;
  58597. + /** In Endpoint Mis-match count */
  58598. + unsigned epmscnt:5;
  58599. + /** Enable Descriptor DMA in Device mode */
  58600. + unsigned descdma:1;
  58601. + unsigned perschintvl:2;
  58602. + unsigned resvalid:6;
  58603. + } b;
  58604. +} dcfg_data_t;
  58605. +
  58606. +/**
  58607. + * This union represents the bit fields in the Device Control
  58608. + * Register. Read the register into the <i>d32</i> member then
  58609. + * set/clear the bits using the <i>b</i>it elements.
  58610. + */
  58611. +typedef union dctl_data {
  58612. + /** raw register data */
  58613. + uint32_t d32;
  58614. + /** register bits */
  58615. + struct {
  58616. + /** Remote Wakeup */
  58617. + unsigned rmtwkupsig:1;
  58618. + /** Soft Disconnect */
  58619. + unsigned sftdiscon:1;
  58620. + /** Global Non-Periodic IN NAK Status */
  58621. + unsigned gnpinnaksts:1;
  58622. + /** Global OUT NAK Status */
  58623. + unsigned goutnaksts:1;
  58624. + /** Test Control */
  58625. + unsigned tstctl:3;
  58626. + /** Set Global Non-Periodic IN NAK */
  58627. + unsigned sgnpinnak:1;
  58628. + /** Clear Global Non-Periodic IN NAK */
  58629. + unsigned cgnpinnak:1;
  58630. + /** Set Global OUT NAK */
  58631. + unsigned sgoutnak:1;
  58632. + /** Clear Global OUT NAK */
  58633. + unsigned cgoutnak:1;
  58634. + /** Power-On Programming Done */
  58635. + unsigned pwronprgdone:1;
  58636. + /** Reserved */
  58637. + unsigned reserved:1;
  58638. + /** Global Multi Count */
  58639. + unsigned gmc:2;
  58640. + /** Ignore Frame Number for ISOC EPs */
  58641. + unsigned ifrmnum:1;
  58642. + /** NAK on Babble */
  58643. + unsigned nakonbble:1;
  58644. + /** Enable Continue on BNA */
  58645. + unsigned encontonbna:1;
  58646. +
  58647. + unsigned reserved18_31:14;
  58648. + } b;
  58649. +} dctl_data_t;
  58650. +
  58651. +/**
  58652. + * This union represents the bit fields in the Device Status
  58653. + * Register. Read the register into the <i>d32</i> member then
  58654. + * set/clear the bits using the <i>b</i>it elements.
  58655. + */
  58656. +typedef union dsts_data {
  58657. + /** raw register data */
  58658. + uint32_t d32;
  58659. + /** register bits */
  58660. + struct {
  58661. + /** Suspend Status */
  58662. + unsigned suspsts:1;
  58663. + /** Enumerated Speed */
  58664. + unsigned enumspd:2;
  58665. +#define DWC_DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ 0
  58666. +#define DWC_DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ 1
  58667. +#define DWC_DSTS_ENUMSPD_LS_PHY_6MHZ 2
  58668. +#define DWC_DSTS_ENUMSPD_FS_PHY_48MHZ 3
  58669. + /** Erratic Error */
  58670. + unsigned errticerr:1;
  58671. + unsigned reserved4_7:4;
  58672. + /** Frame or Microframe Number of the received SOF */
  58673. + unsigned soffn:14;
  58674. + unsigned reserved22_31:10;
  58675. + } b;
  58676. +} dsts_data_t;
  58677. +
  58678. +/**
  58679. + * This union represents the bit fields in the Device IN EP Interrupt
  58680. + * Register and the Device IN EP Common Mask Register.
  58681. + *
  58682. + * - Read the register into the <i>d32</i> member then set/clear the
  58683. + * bits using the <i>b</i>it elements.
  58684. + */
  58685. +typedef union diepint_data {
  58686. + /** raw register data */
  58687. + uint32_t d32;
  58688. + /** register bits */
  58689. + struct {
  58690. + /** Transfer complete mask */
  58691. + unsigned xfercompl:1;
  58692. + /** Endpoint disable mask */
  58693. + unsigned epdisabled:1;
  58694. + /** AHB Error mask */
  58695. + unsigned ahberr:1;
  58696. + /** TimeOUT Handshake mask (non-ISOC EPs) */
  58697. + unsigned timeout:1;
  58698. + /** IN Token received with TxF Empty mask */
  58699. + unsigned intktxfemp:1;
  58700. + /** IN Token Received with EP mismatch mask */
  58701. + unsigned intknepmis:1;
  58702. + /** IN Endpoint NAK Effective mask */
  58703. + unsigned inepnakeff:1;
  58704. + /** Reserved */
  58705. + unsigned emptyintr:1;
  58706. +
  58707. + unsigned txfifoundrn:1;
  58708. +
  58709. + /** BNA Interrupt mask */
  58710. + unsigned bna:1;
  58711. +
  58712. + unsigned reserved10_12:3;
  58713. + /** BNA Interrupt mask */
  58714. + unsigned nak:1;
  58715. +
  58716. + unsigned reserved14_31:18;
  58717. + } b;
  58718. +} diepint_data_t;
  58719. +
  58720. +/**
  58721. + * This union represents the bit fields in the Device IN EP
  58722. + * Common/Dedicated Interrupt Mask Register.
  58723. + */
  58724. +typedef union diepint_data diepmsk_data_t;
  58725. +
  58726. +/**
  58727. + * This union represents the bit fields in the Device OUT EP Interrupt
  58728. + * Registerand Device OUT EP Common Interrupt Mask Register.
  58729. + *
  58730. + * - Read the register into the <i>d32</i> member then set/clear the
  58731. + * bits using the <i>b</i>it elements.
  58732. + */
  58733. +typedef union doepint_data {
  58734. + /** raw register data */
  58735. + uint32_t d32;
  58736. + /** register bits */
  58737. + struct {
  58738. + /** Transfer complete */
  58739. + unsigned xfercompl:1;
  58740. + /** Endpoint disable */
  58741. + unsigned epdisabled:1;
  58742. + /** AHB Error */
  58743. + unsigned ahberr:1;
  58744. + /** Setup Phase Done (contorl EPs) */
  58745. + unsigned setup:1;
  58746. + /** OUT Token Received when Endpoint Disabled */
  58747. + unsigned outtknepdis:1;
  58748. +
  58749. + unsigned stsphsercvd:1;
  58750. + /** Back-to-Back SETUP Packets Received */
  58751. + unsigned back2backsetup:1;
  58752. +
  58753. + unsigned reserved7:1;
  58754. + /** OUT packet Error */
  58755. + unsigned outpkterr:1;
  58756. + /** BNA Interrupt */
  58757. + unsigned bna:1;
  58758. +
  58759. + unsigned reserved10:1;
  58760. + /** Packet Drop Status */
  58761. + unsigned pktdrpsts:1;
  58762. + /** Babble Interrupt */
  58763. + unsigned babble:1;
  58764. + /** NAK Interrupt */
  58765. + unsigned nak:1;
  58766. + /** NYET Interrupt */
  58767. + unsigned nyet:1;
  58768. + /** Bit indicating setup packet received */
  58769. + unsigned sr:1;
  58770. +
  58771. + unsigned reserved16_31:16;
  58772. + } b;
  58773. +} doepint_data_t;
  58774. +
  58775. +/**
  58776. + * This union represents the bit fields in the Device OUT EP
  58777. + * Common/Dedicated Interrupt Mask Register.
  58778. + */
  58779. +typedef union doepint_data doepmsk_data_t;
  58780. +
  58781. +/**
  58782. + * This union represents the bit fields in the Device All EP Interrupt
  58783. + * and Mask Registers.
  58784. + * - Read the register into the <i>d32</i> member then set/clear the
  58785. + * bits using the <i>b</i>it elements.
  58786. + */
  58787. +typedef union daint_data {
  58788. + /** raw register data */
  58789. + uint32_t d32;
  58790. + /** register bits */
  58791. + struct {
  58792. + /** IN Endpoint bits */
  58793. + unsigned in:16;
  58794. + /** OUT Endpoint bits */
  58795. + unsigned out:16;
  58796. + } ep;
  58797. + struct {
  58798. + /** IN Endpoint bits */
  58799. + unsigned inep0:1;
  58800. + unsigned inep1:1;
  58801. + unsigned inep2:1;
  58802. + unsigned inep3:1;
  58803. + unsigned inep4:1;
  58804. + unsigned inep5:1;
  58805. + unsigned inep6:1;
  58806. + unsigned inep7:1;
  58807. + unsigned inep8:1;
  58808. + unsigned inep9:1;
  58809. + unsigned inep10:1;
  58810. + unsigned inep11:1;
  58811. + unsigned inep12:1;
  58812. + unsigned inep13:1;
  58813. + unsigned inep14:1;
  58814. + unsigned inep15:1;
  58815. + /** OUT Endpoint bits */
  58816. + unsigned outep0:1;
  58817. + unsigned outep1:1;
  58818. + unsigned outep2:1;
  58819. + unsigned outep3:1;
  58820. + unsigned outep4:1;
  58821. + unsigned outep5:1;
  58822. + unsigned outep6:1;
  58823. + unsigned outep7:1;
  58824. + unsigned outep8:1;
  58825. + unsigned outep9:1;
  58826. + unsigned outep10:1;
  58827. + unsigned outep11:1;
  58828. + unsigned outep12:1;
  58829. + unsigned outep13:1;
  58830. + unsigned outep14:1;
  58831. + unsigned outep15:1;
  58832. + } b;
  58833. +} daint_data_t;
  58834. +
  58835. +/**
  58836. + * This union represents the bit fields in the Device IN Token Queue
  58837. + * Read Registers.
  58838. + * - Read the register into the <i>d32</i> member.
  58839. + * - READ-ONLY Register
  58840. + */
  58841. +typedef union dtknq1_data {
  58842. + /** raw register data */
  58843. + uint32_t d32;
  58844. + /** register bits */
  58845. + struct {
  58846. + /** In Token Queue Write Pointer */
  58847. + unsigned intknwptr:5;
  58848. + /** Reserved */
  58849. + unsigned reserved05_06:2;
  58850. + /** write pointer has wrapped. */
  58851. + unsigned wrap_bit:1;
  58852. + /** EP Numbers of IN Tokens 0 ... 4 */
  58853. + unsigned epnums0_5:24;
  58854. + } b;
  58855. +} dtknq1_data_t;
  58856. +
  58857. +/**
  58858. + * This union represents Threshold control Register
  58859. + * - Read and write the register into the <i>d32</i> member.
  58860. + * - READ-WRITABLE Register
  58861. + */
  58862. +typedef union dthrctl_data {
  58863. + /** raw register data */
  58864. + uint32_t d32;
  58865. + /** register bits */
  58866. + struct {
  58867. + /** non ISO Tx Thr. Enable */
  58868. + unsigned non_iso_thr_en:1;
  58869. + /** ISO Tx Thr. Enable */
  58870. + unsigned iso_thr_en:1;
  58871. + /** Tx Thr. Length */
  58872. + unsigned tx_thr_len:9;
  58873. + /** AHB Threshold ratio */
  58874. + unsigned ahb_thr_ratio:2;
  58875. + /** Reserved */
  58876. + unsigned reserved13_15:3;
  58877. + /** Rx Thr. Enable */
  58878. + unsigned rx_thr_en:1;
  58879. + /** Rx Thr. Length */
  58880. + unsigned rx_thr_len:9;
  58881. + unsigned reserved26:1;
  58882. + /** Arbiter Parking Enable*/
  58883. + unsigned arbprken:1;
  58884. + /** Reserved */
  58885. + unsigned reserved28_31:4;
  58886. + } b;
  58887. +} dthrctl_data_t;
  58888. +
  58889. +/**
  58890. + * Device Logical IN Endpoint-Specific Registers. <i>Offsets
  58891. + * 900h-AFCh</i>
  58892. + *
  58893. + * There will be one set of endpoint registers per logical endpoint
  58894. + * implemented.
  58895. + *
  58896. + * <i>These registers are visible only in Device mode and must not be
  58897. + * accessed in Host mode, as the results are unknown.</i>
  58898. + */
  58899. +typedef struct dwc_otg_dev_in_ep_regs {
  58900. + /** Device IN Endpoint Control Register. <i>Offset:900h +
  58901. + * (ep_num * 20h) + 00h</i> */
  58902. + volatile uint32_t diepctl;
  58903. + /** Reserved. <i>Offset:900h + (ep_num * 20h) + 04h</i> */
  58904. + uint32_t reserved04;
  58905. + /** Device IN Endpoint Interrupt Register. <i>Offset:900h +
  58906. + * (ep_num * 20h) + 08h</i> */
  58907. + volatile uint32_t diepint;
  58908. + /** Reserved. <i>Offset:900h + (ep_num * 20h) + 0Ch</i> */
  58909. + uint32_t reserved0C;
  58910. + /** Device IN Endpoint Transfer Size
  58911. + * Register. <i>Offset:900h + (ep_num * 20h) + 10h</i> */
  58912. + volatile uint32_t dieptsiz;
  58913. + /** Device IN Endpoint DMA Address Register. <i>Offset:900h +
  58914. + * (ep_num * 20h) + 14h</i> */
  58915. + volatile uint32_t diepdma;
  58916. + /** Device IN Endpoint Transmit FIFO Status Register. <i>Offset:900h +
  58917. + * (ep_num * 20h) + 18h</i> */
  58918. + volatile uint32_t dtxfsts;
  58919. + /** Device IN Endpoint DMA Buffer Register. <i>Offset:900h +
  58920. + * (ep_num * 20h) + 1Ch</i> */
  58921. + volatile uint32_t diepdmab;
  58922. +} dwc_otg_dev_in_ep_regs_t;
  58923. +
  58924. +/**
  58925. + * Device Logical OUT Endpoint-Specific Registers. <i>Offsets:
  58926. + * B00h-CFCh</i>
  58927. + *
  58928. + * There will be one set of endpoint registers per logical endpoint
  58929. + * implemented.
  58930. + *
  58931. + * <i>These registers are visible only in Device mode and must not be
  58932. + * accessed in Host mode, as the results are unknown.</i>
  58933. + */
  58934. +typedef struct dwc_otg_dev_out_ep_regs {
  58935. + /** Device OUT Endpoint Control Register. <i>Offset:B00h +
  58936. + * (ep_num * 20h) + 00h</i> */
  58937. + volatile uint32_t doepctl;
  58938. + /** Reserved. <i>Offset:B00h + (ep_num * 20h) + 04h</i> */
  58939. + uint32_t reserved04;
  58940. + /** Device OUT Endpoint Interrupt Register. <i>Offset:B00h +
  58941. + * (ep_num * 20h) + 08h</i> */
  58942. + volatile uint32_t doepint;
  58943. + /** Reserved. <i>Offset:B00h + (ep_num * 20h) + 0Ch</i> */
  58944. + uint32_t reserved0C;
  58945. + /** Device OUT Endpoint Transfer Size Register. <i>Offset:
  58946. + * B00h + (ep_num * 20h) + 10h</i> */
  58947. + volatile uint32_t doeptsiz;
  58948. + /** Device OUT Endpoint DMA Address Register. <i>Offset:B00h
  58949. + * + (ep_num * 20h) + 14h</i> */
  58950. + volatile uint32_t doepdma;
  58951. + /** Reserved. <i>Offset:B00h + * (ep_num * 20h) + 18h</i> */
  58952. + uint32_t unused;
  58953. + /** Device OUT Endpoint DMA Buffer Register. <i>Offset:B00h
  58954. + * + (ep_num * 20h) + 1Ch</i> */
  58955. + uint32_t doepdmab;
  58956. +} dwc_otg_dev_out_ep_regs_t;
  58957. +
  58958. +/**
  58959. + * This union represents the bit fields in the Device EP Control
  58960. + * Register. Read the register into the <i>d32</i> member then
  58961. + * set/clear the bits using the <i>b</i>it elements.
  58962. + */
  58963. +typedef union depctl_data {
  58964. + /** raw register data */
  58965. + uint32_t d32;
  58966. + /** register bits */
  58967. + struct {
  58968. + /** Maximum Packet Size
  58969. + * IN/OUT EPn
  58970. + * IN/OUT EP0 - 2 bits
  58971. + * 2'b00: 64 Bytes
  58972. + * 2'b01: 32
  58973. + * 2'b10: 16
  58974. + * 2'b11: 8 */
  58975. + unsigned mps:11;
  58976. +#define DWC_DEP0CTL_MPS_64 0
  58977. +#define DWC_DEP0CTL_MPS_32 1
  58978. +#define DWC_DEP0CTL_MPS_16 2
  58979. +#define DWC_DEP0CTL_MPS_8 3
  58980. +
  58981. + /** Next Endpoint
  58982. + * IN EPn/IN EP0
  58983. + * OUT EPn/OUT EP0 - reserved */
  58984. + unsigned nextep:4;
  58985. +
  58986. + /** USB Active Endpoint */
  58987. + unsigned usbactep:1;
  58988. +
  58989. + /** Endpoint DPID (INTR/Bulk IN and OUT endpoints)
  58990. + * This field contains the PID of the packet going to
  58991. + * be received or transmitted on this endpoint. The
  58992. + * application should program the PID of the first
  58993. + * packet going to be received or transmitted on this
  58994. + * endpoint , after the endpoint is
  58995. + * activated. Application use the SetD1PID and
  58996. + * SetD0PID fields of this register to program either
  58997. + * D0 or D1 PID.
  58998. + *
  58999. + * The encoding for this field is
  59000. + * - 0: D0
  59001. + * - 1: D1
  59002. + */
  59003. + unsigned dpid:1;
  59004. +
  59005. + /** NAK Status */
  59006. + unsigned naksts:1;
  59007. +
  59008. + /** Endpoint Type
  59009. + * 2'b00: Control
  59010. + * 2'b01: Isochronous
  59011. + * 2'b10: Bulk
  59012. + * 2'b11: Interrupt */
  59013. + unsigned eptype:2;
  59014. +
  59015. + /** Snoop Mode
  59016. + * OUT EPn/OUT EP0
  59017. + * IN EPn/IN EP0 - reserved */
  59018. + unsigned snp:1;
  59019. +
  59020. + /** Stall Handshake */
  59021. + unsigned stall:1;
  59022. +
  59023. + /** Tx Fifo Number
  59024. + * IN EPn/IN EP0
  59025. + * OUT EPn/OUT EP0 - reserved */
  59026. + unsigned txfnum:4;
  59027. +
  59028. + /** Clear NAK */
  59029. + unsigned cnak:1;
  59030. + /** Set NAK */
  59031. + unsigned snak:1;
  59032. + /** Set DATA0 PID (INTR/Bulk IN and OUT endpoints)
  59033. + * Writing to this field sets the Endpoint DPID (DPID)
  59034. + * field in this register to DATA0. Set Even
  59035. + * (micro)frame (SetEvenFr) (ISO IN and OUT Endpoints)
  59036. + * Writing to this field sets the Even/Odd
  59037. + * (micro)frame (EO_FrNum) field to even (micro)
  59038. + * frame.
  59039. + */
  59040. + unsigned setd0pid:1;
  59041. + /** Set DATA1 PID (INTR/Bulk IN and OUT endpoints)
  59042. + * Writing to this field sets the Endpoint DPID (DPID)
  59043. + * field in this register to DATA1 Set Odd
  59044. + * (micro)frame (SetOddFr) (ISO IN and OUT Endpoints)
  59045. + * Writing to this field sets the Even/Odd
  59046. + * (micro)frame (EO_FrNum) field to odd (micro) frame.
  59047. + */
  59048. + unsigned setd1pid:1;
  59049. +
  59050. + /** Endpoint Disable */
  59051. + unsigned epdis:1;
  59052. + /** Endpoint Enable */
  59053. + unsigned epena:1;
  59054. + } b;
  59055. +} depctl_data_t;
  59056. +
  59057. +/**
  59058. + * This union represents the bit fields in the Device EP Transfer
  59059. + * Size Register. Read the register into the <i>d32</i> member then
  59060. + * set/clear the bits using the <i>b</i>it elements.
  59061. + */
  59062. +typedef union deptsiz_data {
  59063. + /** raw register data */
  59064. + uint32_t d32;
  59065. + /** register bits */
  59066. + struct {
  59067. + /** Transfer size */
  59068. + unsigned xfersize:19;
  59069. +/** Max packet count for EP (pow(2,10)-1) */
  59070. +#define MAX_PKT_CNT 1023
  59071. + /** Packet Count */
  59072. + unsigned pktcnt:10;
  59073. + /** Multi Count - Periodic IN endpoints */
  59074. + unsigned mc:2;
  59075. + unsigned reserved:1;
  59076. + } b;
  59077. +} deptsiz_data_t;
  59078. +
  59079. +/**
  59080. + * This union represents the bit fields in the Device EP 0 Transfer
  59081. + * Size Register. Read the register into the <i>d32</i> member then
  59082. + * set/clear the bits using the <i>b</i>it elements.
  59083. + */
  59084. +typedef union deptsiz0_data {
  59085. + /** raw register data */
  59086. + uint32_t d32;
  59087. + /** register bits */
  59088. + struct {
  59089. + /** Transfer size */
  59090. + unsigned xfersize:7;
  59091. + /** Reserved */
  59092. + unsigned reserved7_18:12;
  59093. + /** Packet Count */
  59094. + unsigned pktcnt:2;
  59095. + /** Reserved */
  59096. + unsigned reserved21_28:8;
  59097. + /**Setup Packet Count (DOEPTSIZ0 Only) */
  59098. + unsigned supcnt:2;
  59099. + unsigned reserved31;
  59100. + } b;
  59101. +} deptsiz0_data_t;
  59102. +
  59103. +/////////////////////////////////////////////////
  59104. +// DMA Descriptor Specific Structures
  59105. +//
  59106. +
  59107. +/** Buffer status definitions */
  59108. +
  59109. +#define BS_HOST_READY 0x0
  59110. +#define BS_DMA_BUSY 0x1
  59111. +#define BS_DMA_DONE 0x2
  59112. +#define BS_HOST_BUSY 0x3
  59113. +
  59114. +/** Receive/Transmit status definitions */
  59115. +
  59116. +#define RTS_SUCCESS 0x0
  59117. +#define RTS_BUFFLUSH 0x1
  59118. +#define RTS_RESERVED 0x2
  59119. +#define RTS_BUFERR 0x3
  59120. +
  59121. +/**
  59122. + * This union represents the bit fields in the DMA Descriptor
  59123. + * status quadlet. Read the quadlet into the <i>d32</i> member then
  59124. + * set/clear the bits using the <i>b</i>it, <i>b_iso_out</i> and
  59125. + * <i>b_iso_in</i> elements.
  59126. + */
  59127. +typedef union dev_dma_desc_sts {
  59128. + /** raw register data */
  59129. + uint32_t d32;
  59130. + /** quadlet bits */
  59131. + struct {
  59132. + /** Received number of bytes */
  59133. + unsigned bytes:16;
  59134. + /** NAK bit - only for OUT EPs */
  59135. + unsigned nak:1;
  59136. + unsigned reserved17_22:6;
  59137. + /** Multiple Transfer - only for OUT EPs */
  59138. + unsigned mtrf:1;
  59139. + /** Setup Packet received - only for OUT EPs */
  59140. + unsigned sr:1;
  59141. + /** Interrupt On Complete */
  59142. + unsigned ioc:1;
  59143. + /** Short Packet */
  59144. + unsigned sp:1;
  59145. + /** Last */
  59146. + unsigned l:1;
  59147. + /** Receive Status */
  59148. + unsigned sts:2;
  59149. + /** Buffer Status */
  59150. + unsigned bs:2;
  59151. + } b;
  59152. +
  59153. +//#ifdef DWC_EN_ISOC
  59154. + /** iso out quadlet bits */
  59155. + struct {
  59156. + /** Received number of bytes */
  59157. + unsigned rxbytes:11;
  59158. +
  59159. + unsigned reserved11:1;
  59160. + /** Frame Number */
  59161. + unsigned framenum:11;
  59162. + /** Received ISO Data PID */
  59163. + unsigned pid:2;
  59164. + /** Interrupt On Complete */
  59165. + unsigned ioc:1;
  59166. + /** Short Packet */
  59167. + unsigned sp:1;
  59168. + /** Last */
  59169. + unsigned l:1;
  59170. + /** Receive Status */
  59171. + unsigned rxsts:2;
  59172. + /** Buffer Status */
  59173. + unsigned bs:2;
  59174. + } b_iso_out;
  59175. +
  59176. + /** iso in quadlet bits */
  59177. + struct {
  59178. + /** Transmited number of bytes */
  59179. + unsigned txbytes:12;
  59180. + /** Frame Number */
  59181. + unsigned framenum:11;
  59182. + /** Transmited ISO Data PID */
  59183. + unsigned pid:2;
  59184. + /** Interrupt On Complete */
  59185. + unsigned ioc:1;
  59186. + /** Short Packet */
  59187. + unsigned sp:1;
  59188. + /** Last */
  59189. + unsigned l:1;
  59190. + /** Transmit Status */
  59191. + unsigned txsts:2;
  59192. + /** Buffer Status */
  59193. + unsigned bs:2;
  59194. + } b_iso_in;
  59195. +//#endif /* DWC_EN_ISOC */
  59196. +} dev_dma_desc_sts_t;
  59197. +
  59198. +/**
  59199. + * DMA Descriptor structure
  59200. + *
  59201. + * DMA Descriptor structure contains two quadlets:
  59202. + * Status quadlet and Data buffer pointer.
  59203. + */
  59204. +typedef struct dwc_otg_dev_dma_desc {
  59205. + /** DMA Descriptor status quadlet */
  59206. + dev_dma_desc_sts_t status;
  59207. + /** DMA Descriptor data buffer pointer */
  59208. + uint32_t buf;
  59209. +} dwc_otg_dev_dma_desc_t;
  59210. +
  59211. +/**
  59212. + * The dwc_otg_dev_if structure contains information needed to manage
  59213. + * the DWC_otg controller acting in device mode. It represents the
  59214. + * programming view of the device-specific aspects of the controller.
  59215. + */
  59216. +typedef struct dwc_otg_dev_if {
  59217. + /** Pointer to device Global registers.
  59218. + * Device Global Registers starting at offset 800h
  59219. + */
  59220. + dwc_otg_device_global_regs_t *dev_global_regs;
  59221. +#define DWC_DEV_GLOBAL_REG_OFFSET 0x800
  59222. +
  59223. + /**
  59224. + * Device Logical IN Endpoint-Specific Registers 900h-AFCh
  59225. + */
  59226. + dwc_otg_dev_in_ep_regs_t *in_ep_regs[MAX_EPS_CHANNELS];
  59227. +#define DWC_DEV_IN_EP_REG_OFFSET 0x900
  59228. +#define DWC_EP_REG_OFFSET 0x20
  59229. +
  59230. + /** Device Logical OUT Endpoint-Specific Registers B00h-CFCh */
  59231. + dwc_otg_dev_out_ep_regs_t *out_ep_regs[MAX_EPS_CHANNELS];
  59232. +#define DWC_DEV_OUT_EP_REG_OFFSET 0xB00
  59233. +
  59234. + /* Device configuration information */
  59235. + uint8_t speed; /**< Device Speed 0: Unknown, 1: LS, 2:FS, 3: HS */
  59236. + uint8_t num_in_eps; /**< Number # of Tx EP range: 0-15 exept ep0 */
  59237. + uint8_t num_out_eps; /**< Number # of Rx EP range: 0-15 exept ep 0*/
  59238. +
  59239. + /** Size of periodic FIFOs (Bytes) */
  59240. + uint16_t perio_tx_fifo_size[MAX_PERIO_FIFOS];
  59241. +
  59242. + /** Size of Tx FIFOs (Bytes) */
  59243. + uint16_t tx_fifo_size[MAX_TX_FIFOS];
  59244. +
  59245. + /** Thresholding enable flags and length varaiables **/
  59246. + uint16_t rx_thr_en;
  59247. + uint16_t iso_tx_thr_en;
  59248. + uint16_t non_iso_tx_thr_en;
  59249. +
  59250. + uint16_t rx_thr_length;
  59251. + uint16_t tx_thr_length;
  59252. +
  59253. + /**
  59254. + * Pointers to the DMA Descriptors for EP0 Control
  59255. + * transfers (virtual and physical)
  59256. + */
  59257. +
  59258. + /** 2 descriptors for SETUP packets */
  59259. + dwc_dma_t dma_setup_desc_addr[2];
  59260. + dwc_otg_dev_dma_desc_t *setup_desc_addr[2];
  59261. +
  59262. + /** Pointer to Descriptor with latest SETUP packet */
  59263. + dwc_otg_dev_dma_desc_t *psetup;
  59264. +
  59265. + /** Index of current SETUP handler descriptor */
  59266. + uint32_t setup_desc_index;
  59267. +
  59268. + /** Descriptor for Data In or Status In phases */
  59269. + dwc_dma_t dma_in_desc_addr;
  59270. + dwc_otg_dev_dma_desc_t *in_desc_addr;
  59271. +
  59272. + /** Descriptor for Data Out or Status Out phases */
  59273. + dwc_dma_t dma_out_desc_addr;
  59274. + dwc_otg_dev_dma_desc_t *out_desc_addr;
  59275. +
  59276. + /** Setup Packet Detected - if set clear NAK when queueing */
  59277. + uint32_t spd;
  59278. + /** Isoc ep pointer on which incomplete happens */
  59279. + void *isoc_ep;
  59280. +
  59281. +} dwc_otg_dev_if_t;
  59282. +
  59283. +/////////////////////////////////////////////////
  59284. +// Host Mode Register Structures
  59285. +//
  59286. +/**
  59287. + * The Host Global Registers structure defines the size and relative
  59288. + * field offsets for the Host Mode Global Registers. Host Global
  59289. + * Registers offsets 400h-7FFh.
  59290. +*/
  59291. +typedef struct dwc_otg_host_global_regs {
  59292. + /** Host Configuration Register. <i>Offset: 400h</i> */
  59293. + volatile uint32_t hcfg;
  59294. + /** Host Frame Interval Register. <i>Offset: 404h</i> */
  59295. + volatile uint32_t hfir;
  59296. + /** Host Frame Number / Frame Remaining Register. <i>Offset: 408h</i> */
  59297. + volatile uint32_t hfnum;
  59298. + /** Reserved. <i>Offset: 40Ch</i> */
  59299. + uint32_t reserved40C;
  59300. + /** Host Periodic Transmit FIFO/ Queue Status Register. <i>Offset: 410h</i> */
  59301. + volatile uint32_t hptxsts;
  59302. + /** Host All Channels Interrupt Register. <i>Offset: 414h</i> */
  59303. + volatile uint32_t haint;
  59304. + /** Host All Channels Interrupt Mask Register. <i>Offset: 418h</i> */
  59305. + volatile uint32_t haintmsk;
  59306. + /** Host Frame List Base Address Register . <i>Offset: 41Ch</i> */
  59307. + volatile uint32_t hflbaddr;
  59308. +} dwc_otg_host_global_regs_t;
  59309. +
  59310. +/**
  59311. + * This union represents the bit fields in the Host Configuration Register.
  59312. + * Read the register into the <i>d32</i> member then set/clear the bits using
  59313. + * the <i>b</i>it elements. Write the <i>d32</i> member to the hcfg register.
  59314. + */
  59315. +typedef union hcfg_data {
  59316. + /** raw register data */
  59317. + uint32_t d32;
  59318. +
  59319. + /** register bits */
  59320. + struct {
  59321. + /** FS/LS Phy Clock Select */
  59322. + unsigned fslspclksel:2;
  59323. +#define DWC_HCFG_30_60_MHZ 0
  59324. +#define DWC_HCFG_48_MHZ 1
  59325. +#define DWC_HCFG_6_MHZ 2
  59326. +
  59327. + /** FS/LS Only Support */
  59328. + unsigned fslssupp:1;
  59329. + unsigned reserved3_6:4;
  59330. + /** Enable 32-KHz Suspend Mode */
  59331. + unsigned ena32khzs:1;
  59332. + /** Resume Validation Periiod */
  59333. + unsigned resvalid:8;
  59334. + unsigned reserved16_22:7;
  59335. + /** Enable Scatter/gather DMA in Host mode */
  59336. + unsigned descdma:1;
  59337. + /** Frame List Entries */
  59338. + unsigned frlisten:2;
  59339. + /** Enable Periodic Scheduling */
  59340. + unsigned perschedena:1;
  59341. + unsigned reserved27_30:4;
  59342. + unsigned modechtimen:1;
  59343. + } b;
  59344. +} hcfg_data_t;
  59345. +
  59346. +/**
  59347. + * This union represents the bit fields in the Host Frame Remaing/Number
  59348. + * Register.
  59349. + */
  59350. +typedef union hfir_data {
  59351. + /** raw register data */
  59352. + uint32_t d32;
  59353. +
  59354. + /** register bits */
  59355. + struct {
  59356. + unsigned frint:16;
  59357. + unsigned hfirrldctrl:1;
  59358. + unsigned reserved:15;
  59359. + } b;
  59360. +} hfir_data_t;
  59361. +
  59362. +/**
  59363. + * This union represents the bit fields in the Host Frame Remaing/Number
  59364. + * Register.
  59365. + */
  59366. +typedef union hfnum_data {
  59367. + /** raw register data */
  59368. + uint32_t d32;
  59369. +
  59370. + /** register bits */
  59371. + struct {
  59372. + unsigned frnum:16;
  59373. +#define DWC_HFNUM_MAX_FRNUM 0x3FFF
  59374. + unsigned frrem:16;
  59375. + } b;
  59376. +} hfnum_data_t;
  59377. +
  59378. +typedef union hptxsts_data {
  59379. + /** raw register data */
  59380. + uint32_t d32;
  59381. +
  59382. + /** register bits */
  59383. + struct {
  59384. + unsigned ptxfspcavail:16;
  59385. + unsigned ptxqspcavail:8;
  59386. + /** Top of the Periodic Transmit Request Queue
  59387. + * - bit 24 - Terminate (last entry for the selected channel)
  59388. + * - bits 26:25 - Token Type
  59389. + * - 2'b00 - Zero length
  59390. + * - 2'b01 - Ping
  59391. + * - 2'b10 - Disable
  59392. + * - bits 30:27 - Channel Number
  59393. + * - bit 31 - Odd/even microframe
  59394. + */
  59395. + unsigned ptxqtop_terminate:1;
  59396. + unsigned ptxqtop_token:2;
  59397. + unsigned ptxqtop_chnum:4;
  59398. + unsigned ptxqtop_odd:1;
  59399. + } b;
  59400. +} hptxsts_data_t;
  59401. +
  59402. +/**
  59403. + * This union represents the bit fields in the Host Port Control and Status
  59404. + * Register. Read the register into the <i>d32</i> member then set/clear the
  59405. + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
  59406. + * hprt0 register.
  59407. + */
  59408. +typedef union hprt0_data {
  59409. + /** raw register data */
  59410. + uint32_t d32;
  59411. + /** register bits */
  59412. + struct {
  59413. + unsigned prtconnsts:1;
  59414. + unsigned prtconndet:1;
  59415. + unsigned prtena:1;
  59416. + unsigned prtenchng:1;
  59417. + unsigned prtovrcurract:1;
  59418. + unsigned prtovrcurrchng:1;
  59419. + unsigned prtres:1;
  59420. + unsigned prtsusp:1;
  59421. + unsigned prtrst:1;
  59422. + unsigned reserved9:1;
  59423. + unsigned prtlnsts:2;
  59424. + unsigned prtpwr:1;
  59425. + unsigned prttstctl:4;
  59426. + unsigned prtspd:2;
  59427. +#define DWC_HPRT0_PRTSPD_HIGH_SPEED 0
  59428. +#define DWC_HPRT0_PRTSPD_FULL_SPEED 1
  59429. +#define DWC_HPRT0_PRTSPD_LOW_SPEED 2
  59430. + unsigned reserved19_31:13;
  59431. + } b;
  59432. +} hprt0_data_t;
  59433. +
  59434. +/**
  59435. + * This union represents the bit fields in the Host All Interrupt
  59436. + * Register.
  59437. + */
  59438. +typedef union haint_data {
  59439. + /** raw register data */
  59440. + uint32_t d32;
  59441. + /** register bits */
  59442. + struct {
  59443. + unsigned ch0:1;
  59444. + unsigned ch1:1;
  59445. + unsigned ch2:1;
  59446. + unsigned ch3:1;
  59447. + unsigned ch4:1;
  59448. + unsigned ch5:1;
  59449. + unsigned ch6:1;
  59450. + unsigned ch7:1;
  59451. + unsigned ch8:1;
  59452. + unsigned ch9:1;
  59453. + unsigned ch10:1;
  59454. + unsigned ch11:1;
  59455. + unsigned ch12:1;
  59456. + unsigned ch13:1;
  59457. + unsigned ch14:1;
  59458. + unsigned ch15:1;
  59459. + unsigned reserved:16;
  59460. + } b;
  59461. +
  59462. + struct {
  59463. + unsigned chint:16;
  59464. + unsigned reserved:16;
  59465. + } b2;
  59466. +} haint_data_t;
  59467. +
  59468. +/**
  59469. + * This union represents the bit fields in the Host All Interrupt
  59470. + * Register.
  59471. + */
  59472. +typedef union haintmsk_data {
  59473. + /** raw register data */
  59474. + uint32_t d32;
  59475. + /** register bits */
  59476. + struct {
  59477. + unsigned ch0:1;
  59478. + unsigned ch1:1;
  59479. + unsigned ch2:1;
  59480. + unsigned ch3:1;
  59481. + unsigned ch4:1;
  59482. + unsigned ch5:1;
  59483. + unsigned ch6:1;
  59484. + unsigned ch7:1;
  59485. + unsigned ch8:1;
  59486. + unsigned ch9:1;
  59487. + unsigned ch10:1;
  59488. + unsigned ch11:1;
  59489. + unsigned ch12:1;
  59490. + unsigned ch13:1;
  59491. + unsigned ch14:1;
  59492. + unsigned ch15:1;
  59493. + unsigned reserved:16;
  59494. + } b;
  59495. +
  59496. + struct {
  59497. + unsigned chint:16;
  59498. + unsigned reserved:16;
  59499. + } b2;
  59500. +} haintmsk_data_t;
  59501. +
  59502. +/**
  59503. + * Host Channel Specific Registers. <i>500h-5FCh</i>
  59504. + */
  59505. +typedef struct dwc_otg_hc_regs {
  59506. + /** Host Channel 0 Characteristic Register. <i>Offset: 500h + (chan_num * 20h) + 00h</i> */
  59507. + volatile uint32_t hcchar;
  59508. + /** Host Channel 0 Split Control Register. <i>Offset: 500h + (chan_num * 20h) + 04h</i> */
  59509. + volatile uint32_t hcsplt;
  59510. + /** Host Channel 0 Interrupt Register. <i>Offset: 500h + (chan_num * 20h) + 08h</i> */
  59511. + volatile uint32_t hcint;
  59512. + /** Host Channel 0 Interrupt Mask Register. <i>Offset: 500h + (chan_num * 20h) + 0Ch</i> */
  59513. + volatile uint32_t hcintmsk;
  59514. + /** Host Channel 0 Transfer Size Register. <i>Offset: 500h + (chan_num * 20h) + 10h</i> */
  59515. + volatile uint32_t hctsiz;
  59516. + /** Host Channel 0 DMA Address Register. <i>Offset: 500h + (chan_num * 20h) + 14h</i> */
  59517. + volatile uint32_t hcdma;
  59518. + volatile uint32_t reserved;
  59519. + /** Host Channel 0 DMA Buffer Address Register. <i>Offset: 500h + (chan_num * 20h) + 1Ch</i> */
  59520. + volatile uint32_t hcdmab;
  59521. +} dwc_otg_hc_regs_t;
  59522. +
  59523. +/**
  59524. + * This union represents the bit fields in the Host Channel Characteristics
  59525. + * Register. Read the register into the <i>d32</i> member then set/clear the
  59526. + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
  59527. + * hcchar register.
  59528. + */
  59529. +typedef union hcchar_data {
  59530. + /** raw register data */
  59531. + uint32_t d32;
  59532. +
  59533. + /** register bits */
  59534. + struct {
  59535. + /** Maximum packet size in bytes */
  59536. + unsigned mps:11;
  59537. +
  59538. + /** Endpoint number */
  59539. + unsigned epnum:4;
  59540. +
  59541. + /** 0: OUT, 1: IN */
  59542. + unsigned epdir:1;
  59543. +
  59544. + unsigned reserved:1;
  59545. +
  59546. + /** 0: Full/high speed device, 1: Low speed device */
  59547. + unsigned lspddev:1;
  59548. +
  59549. + /** 0: Control, 1: Isoc, 2: Bulk, 3: Intr */
  59550. + unsigned eptype:2;
  59551. +
  59552. + /** Packets per frame for periodic transfers. 0 is reserved. */
  59553. + unsigned multicnt:2;
  59554. +
  59555. + /** Device address */
  59556. + unsigned devaddr:7;
  59557. +
  59558. + /**
  59559. + * Frame to transmit periodic transaction.
  59560. + * 0: even, 1: odd
  59561. + */
  59562. + unsigned oddfrm:1;
  59563. +
  59564. + /** Channel disable */
  59565. + unsigned chdis:1;
  59566. +
  59567. + /** Channel enable */
  59568. + unsigned chen:1;
  59569. + } b;
  59570. +} hcchar_data_t;
  59571. +
  59572. +typedef union hcsplt_data {
  59573. + /** raw register data */
  59574. + uint32_t d32;
  59575. +
  59576. + /** register bits */
  59577. + struct {
  59578. + /** Port Address */
  59579. + unsigned prtaddr:7;
  59580. +
  59581. + /** Hub Address */
  59582. + unsigned hubaddr:7;
  59583. +
  59584. + /** Transaction Position */
  59585. + unsigned xactpos:2;
  59586. +#define DWC_HCSPLIT_XACTPOS_MID 0
  59587. +#define DWC_HCSPLIT_XACTPOS_END 1
  59588. +#define DWC_HCSPLIT_XACTPOS_BEGIN 2
  59589. +#define DWC_HCSPLIT_XACTPOS_ALL 3
  59590. +
  59591. + /** Do Complete Split */
  59592. + unsigned compsplt:1;
  59593. +
  59594. + /** Reserved */
  59595. + unsigned reserved:14;
  59596. +
  59597. + /** Split Enble */
  59598. + unsigned spltena:1;
  59599. + } b;
  59600. +} hcsplt_data_t;
  59601. +
  59602. +/**
  59603. + * This union represents the bit fields in the Host All Interrupt
  59604. + * Register.
  59605. + */
  59606. +typedef union hcint_data {
  59607. + /** raw register data */
  59608. + uint32_t d32;
  59609. + /** register bits */
  59610. + struct {
  59611. + /** Transfer Complete */
  59612. + unsigned xfercomp:1;
  59613. + /** Channel Halted */
  59614. + unsigned chhltd:1;
  59615. + /** AHB Error */
  59616. + unsigned ahberr:1;
  59617. + /** STALL Response Received */
  59618. + unsigned stall:1;
  59619. + /** NAK Response Received */
  59620. + unsigned nak:1;
  59621. + /** ACK Response Received */
  59622. + unsigned ack:1;
  59623. + /** NYET Response Received */
  59624. + unsigned nyet:1;
  59625. + /** Transaction Err */
  59626. + unsigned xacterr:1;
  59627. + /** Babble Error */
  59628. + unsigned bblerr:1;
  59629. + /** Frame Overrun */
  59630. + unsigned frmovrun:1;
  59631. + /** Data Toggle Error */
  59632. + unsigned datatglerr:1;
  59633. + /** Buffer Not Available (only for DDMA mode) */
  59634. + unsigned bna:1;
  59635. + /** Exessive transaction error (only for DDMA mode) */
  59636. + unsigned xcs_xact:1;
  59637. + /** Frame List Rollover interrupt */
  59638. + unsigned frm_list_roll:1;
  59639. + /** Reserved */
  59640. + unsigned reserved14_31:18;
  59641. + } b;
  59642. +} hcint_data_t;
  59643. +
  59644. +/**
  59645. + * This union represents the bit fields in the Host Channel Interrupt Mask
  59646. + * Register. Read the register into the <i>d32</i> member then set/clear the
  59647. + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
  59648. + * hcintmsk register.
  59649. + */
  59650. +typedef union hcintmsk_data {
  59651. + /** raw register data */
  59652. + uint32_t d32;
  59653. +
  59654. + /** register bits */
  59655. + struct {
  59656. + unsigned xfercompl:1;
  59657. + unsigned chhltd:1;
  59658. + unsigned ahberr:1;
  59659. + unsigned stall:1;
  59660. + unsigned nak:1;
  59661. + unsigned ack:1;
  59662. + unsigned nyet:1;
  59663. + unsigned xacterr:1;
  59664. + unsigned bblerr:1;
  59665. + unsigned frmovrun:1;
  59666. + unsigned datatglerr:1;
  59667. + unsigned bna:1;
  59668. + unsigned xcs_xact:1;
  59669. + unsigned frm_list_roll:1;
  59670. + unsigned reserved14_31:18;
  59671. + } b;
  59672. +} hcintmsk_data_t;
  59673. +
  59674. +/**
  59675. + * This union represents the bit fields in the Host Channel Transfer Size
  59676. + * Register. Read the register into the <i>d32</i> member then set/clear the
  59677. + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
  59678. + * hcchar register.
  59679. + */
  59680. +
  59681. +typedef union hctsiz_data {
  59682. + /** raw register data */
  59683. + uint32_t d32;
  59684. +
  59685. + /** register bits */
  59686. + struct {
  59687. + /** Total transfer size in bytes */
  59688. + unsigned xfersize:19;
  59689. +
  59690. + /** Data packets to transfer */
  59691. + unsigned pktcnt:10;
  59692. +
  59693. + /**
  59694. + * Packet ID for next data packet
  59695. + * 0: DATA0
  59696. + * 1: DATA2
  59697. + * 2: DATA1
  59698. + * 3: MDATA (non-Control), SETUP (Control)
  59699. + */
  59700. + unsigned pid:2;
  59701. +#define DWC_HCTSIZ_DATA0 0
  59702. +#define DWC_HCTSIZ_DATA1 2
  59703. +#define DWC_HCTSIZ_DATA2 1
  59704. +#define DWC_HCTSIZ_MDATA 3
  59705. +#define DWC_HCTSIZ_SETUP 3
  59706. +
  59707. + /** Do PING protocol when 1 */
  59708. + unsigned dopng:1;
  59709. + } b;
  59710. +
  59711. + /** register bits */
  59712. + struct {
  59713. + /** Scheduling information */
  59714. + unsigned schinfo:8;
  59715. +
  59716. + /** Number of transfer descriptors.
  59717. + * Max value:
  59718. + * 64 in general,
  59719. + * 256 only for HS isochronous endpoint.
  59720. + */
  59721. + unsigned ntd:8;
  59722. +
  59723. + /** Data packets to transfer */
  59724. + unsigned reserved16_28:13;
  59725. +
  59726. + /**
  59727. + * Packet ID for next data packet
  59728. + * 0: DATA0
  59729. + * 1: DATA2
  59730. + * 2: DATA1
  59731. + * 3: MDATA (non-Control)
  59732. + */
  59733. + unsigned pid:2;
  59734. +
  59735. + /** Do PING protocol when 1 */
  59736. + unsigned dopng:1;
  59737. + } b_ddma;
  59738. +} hctsiz_data_t;
  59739. +
  59740. +/**
  59741. + * This union represents the bit fields in the Host DMA Address
  59742. + * Register used in Descriptor DMA mode.
  59743. + */
  59744. +typedef union hcdma_data {
  59745. + /** raw register data */
  59746. + uint32_t d32;
  59747. + /** register bits */
  59748. + struct {
  59749. + unsigned reserved0_2:3;
  59750. + /** Current Transfer Descriptor. Not used for ISOC */
  59751. + unsigned ctd:8;
  59752. + /** Start Address of Descriptor List */
  59753. + unsigned dma_addr:21;
  59754. + } b;
  59755. +} hcdma_data_t;
  59756. +
  59757. +/**
  59758. + * This union represents the bit fields in the DMA Descriptor
  59759. + * status quadlet for host mode. Read the quadlet into the <i>d32</i> member then
  59760. + * set/clear the bits using the <i>b</i>it elements.
  59761. + */
  59762. +typedef union host_dma_desc_sts {
  59763. + /** raw register data */
  59764. + uint32_t d32;
  59765. + /** quadlet bits */
  59766. +
  59767. + /* for non-isochronous */
  59768. + struct {
  59769. + /** Number of bytes */
  59770. + unsigned n_bytes:17;
  59771. + /** QTD offset to jump when Short Packet received - only for IN EPs */
  59772. + unsigned qtd_offset:6;
  59773. + /**
  59774. + * Set to request the core to jump to alternate QTD if
  59775. + * Short Packet received - only for IN EPs
  59776. + */
  59777. + unsigned a_qtd:1;
  59778. + /**
  59779. + * Setup Packet bit. When set indicates that buffer contains
  59780. + * setup packet.
  59781. + */
  59782. + unsigned sup:1;
  59783. + /** Interrupt On Complete */
  59784. + unsigned ioc:1;
  59785. + /** End of List */
  59786. + unsigned eol:1;
  59787. + unsigned reserved27:1;
  59788. + /** Rx/Tx Status */
  59789. + unsigned sts:2;
  59790. +#define DMA_DESC_STS_PKTERR 1
  59791. + unsigned reserved30:1;
  59792. + /** Active Bit */
  59793. + unsigned a:1;
  59794. + } b;
  59795. + /* for isochronous */
  59796. + struct {
  59797. + /** Number of bytes */
  59798. + unsigned n_bytes:12;
  59799. + unsigned reserved12_24:13;
  59800. + /** Interrupt On Complete */
  59801. + unsigned ioc:1;
  59802. + unsigned reserved26_27:2;
  59803. + /** Rx/Tx Status */
  59804. + unsigned sts:2;
  59805. + unsigned reserved30:1;
  59806. + /** Active Bit */
  59807. + unsigned a:1;
  59808. + } b_isoc;
  59809. +} host_dma_desc_sts_t;
  59810. +
  59811. +#define MAX_DMA_DESC_SIZE 131071
  59812. +#define MAX_DMA_DESC_NUM_GENERIC 64
  59813. +#define MAX_DMA_DESC_NUM_HS_ISOC 256
  59814. +#define MAX_FRLIST_EN_NUM 64
  59815. +/**
  59816. + * Host-mode DMA Descriptor structure
  59817. + *
  59818. + * DMA Descriptor structure contains two quadlets:
  59819. + * Status quadlet and Data buffer pointer.
  59820. + */
  59821. +typedef struct dwc_otg_host_dma_desc {
  59822. + /** DMA Descriptor status quadlet */
  59823. + host_dma_desc_sts_t status;
  59824. + /** DMA Descriptor data buffer pointer */
  59825. + uint32_t buf;
  59826. +} dwc_otg_host_dma_desc_t;
  59827. +
  59828. +/** OTG Host Interface Structure.
  59829. + *
  59830. + * The OTG Host Interface Structure structure contains information
  59831. + * needed to manage the DWC_otg controller acting in host mode. It
  59832. + * represents the programming view of the host-specific aspects of the
  59833. + * controller.
  59834. + */
  59835. +typedef struct dwc_otg_host_if {
  59836. + /** Host Global Registers starting at offset 400h.*/
  59837. + dwc_otg_host_global_regs_t *host_global_regs;
  59838. +#define DWC_OTG_HOST_GLOBAL_REG_OFFSET 0x400
  59839. +
  59840. + /** Host Port 0 Control and Status Register */
  59841. + volatile uint32_t *hprt0;
  59842. +#define DWC_OTG_HOST_PORT_REGS_OFFSET 0x440
  59843. +
  59844. + /** Host Channel Specific Registers at offsets 500h-5FCh. */
  59845. + dwc_otg_hc_regs_t *hc_regs[MAX_EPS_CHANNELS];
  59846. +#define DWC_OTG_HOST_CHAN_REGS_OFFSET 0x500
  59847. +#define DWC_OTG_CHAN_REGS_OFFSET 0x20
  59848. +
  59849. + /* Host configuration information */
  59850. + /** Number of Host Channels (range: 1-16) */
  59851. + uint8_t num_host_channels;
  59852. + /** Periodic EPs supported (0: no, 1: yes) */
  59853. + uint8_t perio_eps_supported;
  59854. + /** Periodic Tx FIFO Size (Only 1 host periodic Tx FIFO) */
  59855. + uint16_t perio_tx_fifo_size;
  59856. +
  59857. +} dwc_otg_host_if_t;
  59858. +
  59859. +/**
  59860. + * This union represents the bit fields in the Power and Clock Gating Control
  59861. + * Register. Read the register into the <i>d32</i> member then set/clear the
  59862. + * bits using the <i>b</i>it elements.
  59863. + */
  59864. +typedef union pcgcctl_data {
  59865. + /** raw register data */
  59866. + uint32_t d32;
  59867. +
  59868. + /** register bits */
  59869. + struct {
  59870. + /** Stop Pclk */
  59871. + unsigned stoppclk:1;
  59872. + /** Gate Hclk */
  59873. + unsigned gatehclk:1;
  59874. + /** Power Clamp */
  59875. + unsigned pwrclmp:1;
  59876. + /** Reset Power Down Modules */
  59877. + unsigned rstpdwnmodule:1;
  59878. + /** Reserved */
  59879. + unsigned reserved:1;
  59880. + /** Enable Sleep Clock Gating (Enbl_L1Gating) */
  59881. + unsigned enbl_sleep_gating:1;
  59882. + /** PHY In Sleep (PhySleep) */
  59883. + unsigned phy_in_sleep:1;
  59884. + /** Deep Sleep*/
  59885. + unsigned deep_sleep:1;
  59886. + unsigned resetaftsusp:1;
  59887. + unsigned restoremode:1;
  59888. + unsigned enbl_extnd_hiber:1;
  59889. + unsigned extnd_hiber_pwrclmp:1;
  59890. + unsigned extnd_hiber_switch:1;
  59891. + unsigned ess_reg_restored:1;
  59892. + unsigned prt_clk_sel:2;
  59893. + unsigned port_power:1;
  59894. + unsigned max_xcvrselect:2;
  59895. + unsigned max_termsel:1;
  59896. + unsigned mac_dev_addr:7;
  59897. + unsigned p2hd_dev_enum_spd:2;
  59898. + unsigned p2hd_prt_spd:2;
  59899. + unsigned if_dev_mode:1;
  59900. + } b;
  59901. +} pcgcctl_data_t;
  59902. +
  59903. +/**
  59904. + * This union represents the bit fields in the Global Data FIFO Software
  59905. + * Configuration Register. Read the register into the <i>d32</i> member then
  59906. + * set/clear the bits using the <i>b</i>it elements.
  59907. + */
  59908. +typedef union gdfifocfg_data {
  59909. + /* raw register data */
  59910. + uint32_t d32;
  59911. + /** register bits */
  59912. + struct {
  59913. + /** OTG Data FIFO depth */
  59914. + unsigned gdfifocfg:16;
  59915. + /** Start address of EP info controller */
  59916. + unsigned epinfobase:16;
  59917. + } b;
  59918. +} gdfifocfg_data_t;
  59919. +
  59920. +/**
  59921. + * This union represents the bit fields in the Global Power Down Register
  59922. + * Register. Read the register into the <i>d32</i> member then set/clear the
  59923. + * bits using the <i>b</i>it elements.
  59924. + */
  59925. +typedef union gpwrdn_data {
  59926. + /* raw register data */
  59927. + uint32_t d32;
  59928. +
  59929. + /** register bits */
  59930. + struct {
  59931. + /** PMU Interrupt Select */
  59932. + unsigned pmuintsel:1;
  59933. + /** PMU Active */
  59934. + unsigned pmuactv:1;
  59935. + /** Restore */
  59936. + unsigned restore:1;
  59937. + /** Power Down Clamp */
  59938. + unsigned pwrdnclmp:1;
  59939. + /** Power Down Reset */
  59940. + unsigned pwrdnrstn:1;
  59941. + /** Power Down Switch */
  59942. + unsigned pwrdnswtch:1;
  59943. + /** Disable VBUS */
  59944. + unsigned dis_vbus:1;
  59945. + /** Line State Change */
  59946. + unsigned lnstschng:1;
  59947. + /** Line state change mask */
  59948. + unsigned lnstchng_msk:1;
  59949. + /** Reset Detected */
  59950. + unsigned rst_det:1;
  59951. + /** Reset Detect mask */
  59952. + unsigned rst_det_msk:1;
  59953. + /** Disconnect Detected */
  59954. + unsigned disconn_det:1;
  59955. + /** Disconnect Detect mask */
  59956. + unsigned disconn_det_msk:1;
  59957. + /** Connect Detected*/
  59958. + unsigned connect_det:1;
  59959. + /** Connect Detected Mask*/
  59960. + unsigned connect_det_msk:1;
  59961. + /** SRP Detected */
  59962. + unsigned srp_det:1;
  59963. + /** SRP Detect mask */
  59964. + unsigned srp_det_msk:1;
  59965. + /** Status Change Interrupt */
  59966. + unsigned sts_chngint:1;
  59967. + /** Status Change Interrupt Mask */
  59968. + unsigned sts_chngint_msk:1;
  59969. + /** Line State */
  59970. + unsigned linestate:2;
  59971. + /** Indicates current mode(status of IDDIG signal) */
  59972. + unsigned idsts:1;
  59973. + /** B Session Valid signal status*/
  59974. + unsigned bsessvld:1;
  59975. + /** ADP Event Detected */
  59976. + unsigned adp_int:1;
  59977. + /** Multi Valued ID pin */
  59978. + unsigned mult_val_id_bc:5;
  59979. + /** Reserved 24_31 */
  59980. + unsigned reserved29_31:3;
  59981. + } b;
  59982. +} gpwrdn_data_t;
  59983. +
  59984. +#endif
  59985. --- /dev/null
  59986. +++ b/drivers/usb/host/dwc_otg/test/Makefile
  59987. @@ -0,0 +1,16 @@
  59988. +
  59989. +PERL=/usr/bin/perl
  59990. +PL_TESTS=test_sysfs.pl test_mod_param.pl
  59991. +
  59992. +.PHONY : test
  59993. +test : perl_tests
  59994. +
  59995. +perl_tests :
  59996. + @echo
  59997. + @echo Running perl tests
  59998. + @for test in $(PL_TESTS); do \
  59999. + if $(PERL) ./$$test ; then \
  60000. + echo "=======> $$test, PASSED" ; \
  60001. + else echo "=======> $$test, FAILED" ; \
  60002. + fi \
  60003. + done
  60004. --- /dev/null
  60005. +++ b/drivers/usb/host/dwc_otg/test/dwc_otg_test.pm
  60006. @@ -0,0 +1,337 @@
  60007. +package dwc_otg_test;
  60008. +
  60009. +use strict;
  60010. +use Exporter ();
  60011. +
  60012. +use vars qw(@ISA @EXPORT
  60013. +$sysfsdir $paramdir $errors $params
  60014. +);
  60015. +
  60016. +@ISA = qw(Exporter);
  60017. +
  60018. +#
  60019. +# Globals
  60020. +#
  60021. +$sysfsdir = "/sys/devices/lm0";
  60022. +$paramdir = "/sys/module/dwc_otg";
  60023. +$errors = 0;
  60024. +
  60025. +$params = [
  60026. + {
  60027. + NAME => "otg_cap",
  60028. + DEFAULT => 0,
  60029. + ENUM => [],
  60030. + LOW => 0,
  60031. + HIGH => 2
  60032. + },
  60033. + {
  60034. + NAME => "dma_enable",
  60035. + DEFAULT => 0,
  60036. + ENUM => [],
  60037. + LOW => 0,
  60038. + HIGH => 1
  60039. + },
  60040. + {
  60041. + NAME => "dma_burst_size",
  60042. + DEFAULT => 32,
  60043. + ENUM => [1, 4, 8, 16, 32, 64, 128, 256],
  60044. + LOW => 1,
  60045. + HIGH => 256
  60046. + },
  60047. + {
  60048. + NAME => "host_speed",
  60049. + DEFAULT => 0,
  60050. + ENUM => [],
  60051. + LOW => 0,
  60052. + HIGH => 1
  60053. + },
  60054. + {
  60055. + NAME => "host_support_fs_ls_low_power",
  60056. + DEFAULT => 0,
  60057. + ENUM => [],
  60058. + LOW => 0,
  60059. + HIGH => 1
  60060. + },
  60061. + {
  60062. + NAME => "host_ls_low_power_phy_clk",
  60063. + DEFAULT => 0,
  60064. + ENUM => [],
  60065. + LOW => 0,
  60066. + HIGH => 1
  60067. + },
  60068. + {
  60069. + NAME => "dev_speed",
  60070. + DEFAULT => 0,
  60071. + ENUM => [],
  60072. + LOW => 0,
  60073. + HIGH => 1
  60074. + },
  60075. + {
  60076. + NAME => "enable_dynamic_fifo",
  60077. + DEFAULT => 1,
  60078. + ENUM => [],
  60079. + LOW => 0,
  60080. + HIGH => 1
  60081. + },
  60082. + {
  60083. + NAME => "data_fifo_size",
  60084. + DEFAULT => 8192,
  60085. + ENUM => [],
  60086. + LOW => 32,
  60087. + HIGH => 32768
  60088. + },
  60089. + {
  60090. + NAME => "dev_rx_fifo_size",
  60091. + DEFAULT => 1064,
  60092. + ENUM => [],
  60093. + LOW => 16,
  60094. + HIGH => 32768
  60095. + },
  60096. + {
  60097. + NAME => "dev_nperio_tx_fifo_size",
  60098. + DEFAULT => 1024,
  60099. + ENUM => [],
  60100. + LOW => 16,
  60101. + HIGH => 32768
  60102. + },
  60103. + {
  60104. + NAME => "dev_perio_tx_fifo_size_1",
  60105. + DEFAULT => 256,
  60106. + ENUM => [],
  60107. + LOW => 4,
  60108. + HIGH => 768
  60109. + },
  60110. + {
  60111. + NAME => "dev_perio_tx_fifo_size_2",
  60112. + DEFAULT => 256,
  60113. + ENUM => [],
  60114. + LOW => 4,
  60115. + HIGH => 768
  60116. + },
  60117. + {
  60118. + NAME => "dev_perio_tx_fifo_size_3",
  60119. + DEFAULT => 256,
  60120. + ENUM => [],
  60121. + LOW => 4,
  60122. + HIGH => 768
  60123. + },
  60124. + {
  60125. + NAME => "dev_perio_tx_fifo_size_4",
  60126. + DEFAULT => 256,
  60127. + ENUM => [],
  60128. + LOW => 4,
  60129. + HIGH => 768
  60130. + },
  60131. + {
  60132. + NAME => "dev_perio_tx_fifo_size_5",
  60133. + DEFAULT => 256,
  60134. + ENUM => [],
  60135. + LOW => 4,
  60136. + HIGH => 768
  60137. + },
  60138. + {
  60139. + NAME => "dev_perio_tx_fifo_size_6",
  60140. + DEFAULT => 256,
  60141. + ENUM => [],
  60142. + LOW => 4,
  60143. + HIGH => 768
  60144. + },
  60145. + {
  60146. + NAME => "dev_perio_tx_fifo_size_7",
  60147. + DEFAULT => 256,
  60148. + ENUM => [],
  60149. + LOW => 4,
  60150. + HIGH => 768
  60151. + },
  60152. + {
  60153. + NAME => "dev_perio_tx_fifo_size_8",
  60154. + DEFAULT => 256,
  60155. + ENUM => [],
  60156. + LOW => 4,
  60157. + HIGH => 768
  60158. + },
  60159. + {
  60160. + NAME => "dev_perio_tx_fifo_size_9",
  60161. + DEFAULT => 256,
  60162. + ENUM => [],
  60163. + LOW => 4,
  60164. + HIGH => 768
  60165. + },
  60166. + {
  60167. + NAME => "dev_perio_tx_fifo_size_10",
  60168. + DEFAULT => 256,
  60169. + ENUM => [],
  60170. + LOW => 4,
  60171. + HIGH => 768
  60172. + },
  60173. + {
  60174. + NAME => "dev_perio_tx_fifo_size_11",
  60175. + DEFAULT => 256,
  60176. + ENUM => [],
  60177. + LOW => 4,
  60178. + HIGH => 768
  60179. + },
  60180. + {
  60181. + NAME => "dev_perio_tx_fifo_size_12",
  60182. + DEFAULT => 256,
  60183. + ENUM => [],
  60184. + LOW => 4,
  60185. + HIGH => 768
  60186. + },
  60187. + {
  60188. + NAME => "dev_perio_tx_fifo_size_13",
  60189. + DEFAULT => 256,
  60190. + ENUM => [],
  60191. + LOW => 4,
  60192. + HIGH => 768
  60193. + },
  60194. + {
  60195. + NAME => "dev_perio_tx_fifo_size_14",
  60196. + DEFAULT => 256,
  60197. + ENUM => [],
  60198. + LOW => 4,
  60199. + HIGH => 768
  60200. + },
  60201. + {
  60202. + NAME => "dev_perio_tx_fifo_size_15",
  60203. + DEFAULT => 256,
  60204. + ENUM => [],
  60205. + LOW => 4,
  60206. + HIGH => 768
  60207. + },
  60208. + {
  60209. + NAME => "host_rx_fifo_size",
  60210. + DEFAULT => 1024,
  60211. + ENUM => [],
  60212. + LOW => 16,
  60213. + HIGH => 32768
  60214. + },
  60215. + {
  60216. + NAME => "host_nperio_tx_fifo_size",
  60217. + DEFAULT => 1024,
  60218. + ENUM => [],
  60219. + LOW => 16,
  60220. + HIGH => 32768
  60221. + },
  60222. + {
  60223. + NAME => "host_perio_tx_fifo_size",
  60224. + DEFAULT => 1024,
  60225. + ENUM => [],
  60226. + LOW => 16,
  60227. + HIGH => 32768
  60228. + },
  60229. + {
  60230. + NAME => "max_transfer_size",
  60231. + DEFAULT => 65535,
  60232. + ENUM => [],
  60233. + LOW => 2047,
  60234. + HIGH => 65535
  60235. + },
  60236. + {
  60237. + NAME => "max_packet_count",
  60238. + DEFAULT => 511,
  60239. + ENUM => [],
  60240. + LOW => 15,
  60241. + HIGH => 511
  60242. + },
  60243. + {
  60244. + NAME => "host_channels",
  60245. + DEFAULT => 12,
  60246. + ENUM => [],
  60247. + LOW => 1,
  60248. + HIGH => 16
  60249. + },
  60250. + {
  60251. + NAME => "dev_endpoints",
  60252. + DEFAULT => 6,
  60253. + ENUM => [],
  60254. + LOW => 1,
  60255. + HIGH => 15
  60256. + },
  60257. + {
  60258. + NAME => "phy_type",
  60259. + DEFAULT => 1,
  60260. + ENUM => [],
  60261. + LOW => 0,
  60262. + HIGH => 2
  60263. + },
  60264. + {
  60265. + NAME => "phy_utmi_width",
  60266. + DEFAULT => 16,
  60267. + ENUM => [8, 16],
  60268. + LOW => 8,
  60269. + HIGH => 16
  60270. + },
  60271. + {
  60272. + NAME => "phy_ulpi_ddr",
  60273. + DEFAULT => 0,
  60274. + ENUM => [],
  60275. + LOW => 0,
  60276. + HIGH => 1
  60277. + },
  60278. + ];
  60279. +
  60280. +
  60281. +#
  60282. +#
  60283. +sub check_arch {
  60284. + $_ = `uname -m`;
  60285. + chomp;
  60286. + unless (m/armv4tl/) {
  60287. + warn "# \n# Can't execute on $_. Run on integrator platform.\n# \n";
  60288. + return 0;
  60289. + }
  60290. + return 1;
  60291. +}
  60292. +
  60293. +#
  60294. +#
  60295. +sub load_module {
  60296. + my $params = shift;
  60297. + print "\nRemoving Module\n";
  60298. + system "rmmod dwc_otg";
  60299. + print "Loading Module\n";
  60300. + if ($params ne "") {
  60301. + print "Module Parameters: $params\n";
  60302. + }
  60303. + if (system("modprobe dwc_otg $params")) {
  60304. + warn "Unable to load module\n";
  60305. + return 0;
  60306. + }
  60307. + return 1;
  60308. +}
  60309. +
  60310. +#
  60311. +#
  60312. +sub test_status {
  60313. + my $arg = shift;
  60314. +
  60315. + print "\n";
  60316. +
  60317. + if (defined $arg) {
  60318. + warn "WARNING: $arg\n";
  60319. + }
  60320. +
  60321. + if ($errors > 0) {
  60322. + warn "TEST FAILED with $errors errors\n";
  60323. + return 0;
  60324. + } else {
  60325. + print "TEST PASSED\n";
  60326. + return 0 if (defined $arg);
  60327. + }
  60328. + return 1;
  60329. +}
  60330. +
  60331. +#
  60332. +#
  60333. +@EXPORT = qw(
  60334. +$sysfsdir
  60335. +$paramdir
  60336. +$params
  60337. +$errors
  60338. +check_arch
  60339. +load_module
  60340. +test_status
  60341. +);
  60342. +
  60343. +1;
  60344. --- /dev/null
  60345. +++ b/drivers/usb/host/dwc_otg/test/test_mod_param.pl
  60346. @@ -0,0 +1,133 @@
  60347. +#!/usr/bin/perl -w
  60348. +#
  60349. +# Run this program on the integrator.
  60350. +#
  60351. +# - Tests module parameter default values.
  60352. +# - Tests setting of valid module parameter values via modprobe.
  60353. +# - Tests invalid module parameter values.
  60354. +# -----------------------------------------------------------------------------
  60355. +use strict;
  60356. +use dwc_otg_test;
  60357. +
  60358. +check_arch() or die;
  60359. +
  60360. +#
  60361. +#
  60362. +sub test {
  60363. + my ($param,$expected) = @_;
  60364. + my $value = get($param);
  60365. +
  60366. + if ($value == $expected) {
  60367. + print "$param = $value, okay\n";
  60368. + }
  60369. +
  60370. + else {
  60371. + warn "ERROR: value of $param != $expected, $value\n";
  60372. + $errors ++;
  60373. + }
  60374. +}
  60375. +
  60376. +#
  60377. +#
  60378. +sub get {
  60379. + my $param = shift;
  60380. + my $tmp = `cat $paramdir/$param`;
  60381. + chomp $tmp;
  60382. + return $tmp;
  60383. +}
  60384. +
  60385. +#
  60386. +#
  60387. +sub test_main {
  60388. +
  60389. + print "\nTesting Module Parameters\n";
  60390. +
  60391. + load_module("") or die;
  60392. +
  60393. + # Test initial values
  60394. + print "\nTesting Default Values\n";
  60395. + foreach (@{$params}) {
  60396. + test ($_->{NAME}, $_->{DEFAULT});
  60397. + }
  60398. +
  60399. + # Test low value
  60400. + print "\nTesting Low Value\n";
  60401. + my $cmd_params = "";
  60402. + foreach (@{$params}) {
  60403. + $cmd_params = $cmd_params . "$_->{NAME}=$_->{LOW} ";
  60404. + }
  60405. + load_module($cmd_params) or die;
  60406. +
  60407. + foreach (@{$params}) {
  60408. + test ($_->{NAME}, $_->{LOW});
  60409. + }
  60410. +
  60411. + # Test high value
  60412. + print "\nTesting High Value\n";
  60413. + $cmd_params = "";
  60414. + foreach (@{$params}) {
  60415. + $cmd_params = $cmd_params . "$_->{NAME}=$_->{HIGH} ";
  60416. + }
  60417. + load_module($cmd_params) or die;
  60418. +
  60419. + foreach (@{$params}) {
  60420. + test ($_->{NAME}, $_->{HIGH});
  60421. + }
  60422. +
  60423. + # Test Enum
  60424. + print "\nTesting Enumerated\n";
  60425. + foreach (@{$params}) {
  60426. + if (defined $_->{ENUM}) {
  60427. + my $value;
  60428. + foreach $value (@{$_->{ENUM}}) {
  60429. + $cmd_params = "$_->{NAME}=$value";
  60430. + load_module($cmd_params) or die;
  60431. + test ($_->{NAME}, $value);
  60432. + }
  60433. + }
  60434. + }
  60435. +
  60436. + # Test Invalid Values
  60437. + print "\nTesting Invalid Values\n";
  60438. + $cmd_params = "";
  60439. + foreach (@{$params}) {
  60440. + $cmd_params = $cmd_params . sprintf "$_->{NAME}=%d ", $_->{LOW}-1;
  60441. + }
  60442. + load_module($cmd_params) or die;
  60443. +
  60444. + foreach (@{$params}) {
  60445. + test ($_->{NAME}, $_->{DEFAULT});
  60446. + }
  60447. +
  60448. + $cmd_params = "";
  60449. + foreach (@{$params}) {
  60450. + $cmd_params = $cmd_params . sprintf "$_->{NAME}=%d ", $_->{HIGH}+1;
  60451. + }
  60452. + load_module($cmd_params) or die;
  60453. +
  60454. + foreach (@{$params}) {
  60455. + test ($_->{NAME}, $_->{DEFAULT});
  60456. + }
  60457. +
  60458. + print "\nTesting Enumerated\n";
  60459. + foreach (@{$params}) {
  60460. + if (defined $_->{ENUM}) {
  60461. + my $value;
  60462. + foreach $value (@{$_->{ENUM}}) {
  60463. + $value = $value + 1;
  60464. + $cmd_params = "$_->{NAME}=$value";
  60465. + load_module($cmd_params) or die;
  60466. + test ($_->{NAME}, $_->{DEFAULT});
  60467. + $value = $value - 2;
  60468. + $cmd_params = "$_->{NAME}=$value";
  60469. + load_module($cmd_params) or die;
  60470. + test ($_->{NAME}, $_->{DEFAULT});
  60471. + }
  60472. + }
  60473. + }
  60474. +
  60475. + test_status() or die;
  60476. +}
  60477. +
  60478. +test_main();
  60479. +0;
  60480. --- /dev/null
  60481. +++ b/drivers/usb/host/dwc_otg/test/test_sysfs.pl
  60482. @@ -0,0 +1,193 @@
  60483. +#!/usr/bin/perl -w
  60484. +#
  60485. +# Run this program on the integrator
  60486. +# - Tests select sysfs attributes.
  60487. +# - Todo ... test more attributes, hnp/srp, buspower/bussuspend, etc.
  60488. +# -----------------------------------------------------------------------------
  60489. +use strict;
  60490. +use dwc_otg_test;
  60491. +
  60492. +check_arch() or die;
  60493. +
  60494. +#
  60495. +#
  60496. +sub test {
  60497. + my ($attr,$expected) = @_;
  60498. + my $string = get($attr);
  60499. +
  60500. + if ($string eq $expected) {
  60501. + printf("$attr = $string, okay\n");
  60502. + }
  60503. + else {
  60504. + warn "ERROR: value of $attr != $expected, $string\n";
  60505. + $errors ++;
  60506. + }
  60507. +}
  60508. +
  60509. +#
  60510. +#
  60511. +sub set {
  60512. + my ($reg, $value) = @_;
  60513. + system "echo $value > $sysfsdir/$reg";
  60514. +}
  60515. +
  60516. +#
  60517. +#
  60518. +sub get {
  60519. + my $attr = shift;
  60520. + my $string = `cat $sysfsdir/$attr`;
  60521. + chomp $string;
  60522. + if ($string =~ m/\s\=\s/) {
  60523. + my $tmp;
  60524. + ($tmp, $string) = split /\s=\s/, $string;
  60525. + }
  60526. + return $string;
  60527. +}
  60528. +
  60529. +#
  60530. +#
  60531. +sub test_main {
  60532. + print("\nTesting Sysfs Attributes\n");
  60533. +
  60534. + load_module("") or die;
  60535. +
  60536. + # Test initial values of regoffset/regvalue/guid/gsnpsid
  60537. + print("\nTesting Default Values\n");
  60538. +
  60539. + test("regoffset", "0xffffffff");
  60540. + test("regvalue", "invalid offset");
  60541. + test("guid", "0x12345678"); # this will fail if it has been changed
  60542. + test("gsnpsid", "0x4f54200a");
  60543. +
  60544. + # Test operation of regoffset/regvalue
  60545. + print("\nTesting regoffset\n");
  60546. + set('regoffset', '5a5a5a5a');
  60547. + test("regoffset", "0xffffffff");
  60548. +
  60549. + set('regoffset', '0');
  60550. + test("regoffset", "0x00000000");
  60551. +
  60552. + set('regoffset', '40000');
  60553. + test("regoffset", "0x00000000");
  60554. +
  60555. + set('regoffset', '3ffff');
  60556. + test("regoffset", "0x0003ffff");
  60557. +
  60558. + set('regoffset', '1');
  60559. + test("regoffset", "0x00000001");
  60560. +
  60561. + print("\nTesting regvalue\n");
  60562. + set('regoffset', '3c');
  60563. + test("regvalue", "0x12345678");
  60564. + set('regvalue', '5a5a5a5a');
  60565. + test("regvalue", "0x5a5a5a5a");
  60566. + set('regvalue','a5a5a5a5');
  60567. + test("regvalue", "0xa5a5a5a5");
  60568. + set('guid','12345678');
  60569. +
  60570. + # Test HNP Capable
  60571. + print("\nTesting HNP Capable bit\n");
  60572. + set('hnpcapable', '1');
  60573. + test("hnpcapable", "0x1");
  60574. + set('hnpcapable','0');
  60575. + test("hnpcapable", "0x0");
  60576. +
  60577. + set('regoffset','0c');
  60578. +
  60579. + my $old = get('gusbcfg');
  60580. + print("setting hnpcapable\n");
  60581. + set('hnpcapable', '1');
  60582. + test("hnpcapable", "0x1");
  60583. + test('gusbcfg', sprintf "0x%08x", (oct ($old) | (1<<9)));
  60584. + test('regvalue', sprintf "0x%08x", (oct ($old) | (1<<9)));
  60585. +
  60586. + $old = get('gusbcfg');
  60587. + print("clearing hnpcapable\n");
  60588. + set('hnpcapable', '0');
  60589. + test("hnpcapable", "0x0");
  60590. + test ('gusbcfg', sprintf "0x%08x", oct ($old) & (~(1<<9)));
  60591. + test ('regvalue', sprintf "0x%08x", oct ($old) & (~(1<<9)));
  60592. +
  60593. + # Test SRP Capable
  60594. + print("\nTesting SRP Capable bit\n");
  60595. + set('srpcapable', '1');
  60596. + test("srpcapable", "0x1");
  60597. + set('srpcapable','0');
  60598. + test("srpcapable", "0x0");
  60599. +
  60600. + set('regoffset','0c');
  60601. +
  60602. + $old = get('gusbcfg');
  60603. + print("setting srpcapable\n");
  60604. + set('srpcapable', '1');
  60605. + test("srpcapable", "0x1");
  60606. + test('gusbcfg', sprintf "0x%08x", (oct ($old) | (1<<8)));
  60607. + test('regvalue', sprintf "0x%08x", (oct ($old) | (1<<8)));
  60608. +
  60609. + $old = get('gusbcfg');
  60610. + print("clearing srpcapable\n");
  60611. + set('srpcapable', '0');
  60612. + test("srpcapable", "0x0");
  60613. + test('gusbcfg', sprintf "0x%08x", oct ($old) & (~(1<<8)));
  60614. + test('regvalue', sprintf "0x%08x", oct ($old) & (~(1<<8)));
  60615. +
  60616. + # Test GGPIO
  60617. + print("\nTesting GGPIO\n");
  60618. + set('ggpio','5a5a5a5a');
  60619. + test('ggpio','0x5a5a0000');
  60620. + set('ggpio','a5a5a5a5');
  60621. + test('ggpio','0xa5a50000');
  60622. + set('ggpio','11110000');
  60623. + test('ggpio','0x11110000');
  60624. + set('ggpio','00001111');
  60625. + test('ggpio','0x00000000');
  60626. +
  60627. + # Test DEVSPEED
  60628. + print("\nTesting DEVSPEED\n");
  60629. + set('regoffset','800');
  60630. + $old = get('regvalue');
  60631. + set('devspeed','0');
  60632. + test('devspeed','0x0');
  60633. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3)));
  60634. + set('devspeed','1');
  60635. + test('devspeed','0x1');
  60636. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 1));
  60637. + set('devspeed','2');
  60638. + test('devspeed','0x2');
  60639. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 2));
  60640. + set('devspeed','3');
  60641. + test('devspeed','0x3');
  60642. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 3));
  60643. + set('devspeed','4');
  60644. + test('devspeed','0x0');
  60645. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3)));
  60646. + set('devspeed','5');
  60647. + test('devspeed','0x1');
  60648. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 1));
  60649. +
  60650. +
  60651. + # mode Returns the current mode:0 for device mode1 for host mode Read
  60652. + # hnp Initiate the Host Negotiation Protocol. Read returns the status. Read/Write
  60653. + # srp Initiate the Session Request Protocol. Read returns the status. Read/Write
  60654. + # buspower Get or Set the Power State of the bus (0 - Off or 1 - On) Read/Write
  60655. + # bussuspend Suspend the USB bus. Read/Write
  60656. + # busconnected Get the connection status of the bus Read
  60657. +
  60658. + # gotgctl Get or set the Core Control Status Register. Read/Write
  60659. + ## gusbcfg Get or set the Core USB Configuration Register Read/Write
  60660. + # grxfsiz Get or set the Receive FIFO Size Register Read/Write
  60661. + # gnptxfsiz Get or set the non-periodic Transmit Size Register Read/Write
  60662. + # gpvndctl Get or set the PHY Vendor Control Register Read/Write
  60663. + ## ggpio Get the value in the lower 16-bits of the General Purpose IO Register or Set the upper 16 bits. Read/Write
  60664. + ## guid Get or set the value of the User ID Register Read/Write
  60665. + ## gsnpsid Get the value of the Synopsys ID Regester Read
  60666. + ## devspeed Get or set the device speed setting in the DCFG register Read/Write
  60667. + # enumspeed Gets the device enumeration Speed. Read
  60668. + # hptxfsiz Get the value of the Host Periodic Transmit FIFO Read
  60669. + # hprt0 Get or Set the value in the Host Port Control and Status Register Read/Write
  60670. +
  60671. + test_status("TEST NYI") or die;
  60672. +}
  60673. +
  60674. +test_main();
  60675. +0;